Compare commits

..

No commits in common. "experimental/user_space" and "master" have entirely different histories.

35 changed files with 446 additions and 421 deletions

View File

@ -9,8 +9,8 @@ GRUB_MKRESCUE = grub-mkrescue
KERNEL = deados.bin
ISO = deados.iso
SOURCES_C = kernel.c tty.c idt.c timer.c io.c vga.c gdt.c sys.c irq.c isr.c keyboard.c shell.c string.c user_space.c syscall.c
SOURCES_ASM = boot.s idt_load.s gdt_flush.s irq_flush.s isr_flush.s tss_flush.s
SOURCES_C = kernel.c tty.c idt.c timer.c io.c vga.c gdt.c sys.c irq.c isr.c keyboard.c shell.c string.c user_space.c syscall.c rtc.c
SOURCES_ASM = boot.s idt_load.s gdt_flush.s tss_flush.s irq_flush.s isr_flush.s
OBJECTS = $(SOURCES_ASM:.s=.o) $(SOURCES_C:.c=.o)
ISO_DIR = isodir

View File

@ -1,16 +1,17 @@
# Simple OS implementation
Just a simple OS implementation in C. For educational purposes only.
DeadOS is a minimalist operating system designed for x86 architecture, created solely for educational purposes. It is an ideal platform for learning and understanding the fundamental concepts of operating system development. While it is not intended to evolve into a fully-fledged OS, DeadOS provides a straightforward and manageable codebase that makes it perfect for experimentation and study.
## Requirements
- make
- GCC Cross-Compiler
- QEMU
## How to run
- `make`
- `qemu-system-i386 -cdrom deados.iso`
- `qemu-system-x86_64 -monitor stdio -cdrom deados.iso`
## Features
@ -23,11 +24,15 @@ Just a simple OS implementation in C. For educational purposes only.
- [x] Timers
- [x] Interrupts
- [x] Kernel Extensions handling
- [ ] Paging
- [] Memory Management
- [] File System
- [] User Space
- [] Syscalls
- [x] User Space
- [x] Syscalls
- [ ] Multitasking
- [] stdlib
## Screenshot:
![image](https://github.com/assada/os/assets/1472664/9b67c053-36e1-4816-ad7f-093b89b03fce)
![image](https://github.com/assada/os/assets/1472664/b4e17d66-cb85-4652-9217-11608ad0753d)

24
boot.s
View File

@ -10,22 +10,20 @@
.long FLAGS
.long CHECKSUM
.section .bss
.align 16
.section .stack, "aw", @nobits
.global stack_top
stack_bottom:
.skip 16384
stack_top:
stack_top:
.section .text
.global _start
.type _start, @function
.section .text
.global _start
.type _start, @function
_start:
mov $stack_top, %esp
push %ebx
call kernel_main
movl $stack_top, %esp
push %ebx
call kernel_main
1:
jmp 1b
.size _start, . - _start
jmp 1b

View File

@ -1,18 +0,0 @@
set -ex
i686-elf-as boot.s -o boot.o
i686-elf-as idt_load.s -o idt_load.o
i686-elf-gcc -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o idt_load.o -lgcc
if grub-file --is-x86-multiboot myos.bin; then
echo multiboot confirmed
else
echo the file is not multiboot
fi
mkdir -p isodir/boot/grub
cp myos.bin isodir/boot/myos.bin
cp grub.cfg isodir/boot/grub/grub.cfg
grub-mkrescue -o myos.iso isodir

34
gdt.c
View File

@ -4,14 +4,14 @@
#define GDT_ENTRY 6
Gdt_entry gdt[GDT_ENTRY];
Gdt_ptr gdt_ptr;
Gdt_ptr gdtp;
extern void gdt_flush(void);
extern void gdt_flush(uint32_t);
Tss_entry tss;
extern void tss_flush(void);
void gdt_set_gate(uint8_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t granularity)
static void gdt_set_gate(uint8_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t granularity)
{
gdt[num].base_low = base & 0xFFFF;
gdt[num].base_middle = (base >> 16) & 0xFF;
@ -25,14 +25,17 @@ void gdt_set_gate(uint8_t num, uint32_t base, uint32_t limit, uint8_t access, ui
gdt[num].access = access;
}
void tss_set_gate(uint32_t num, uint16_t ss0, uint32_t esp0)
static void tss_install(uint8_t num, uint16_t kernel_ss, uint16_t kernel_esp)
{
uint32_t base = (uint32_t)&tss;
uint32_t limit = base + sizeof(Tss_entry);
gdt_set_gate(num, base, limit, 0xE9, 0x0);
memset(&tss, 0x0, sizeof(Tss_entry));
tss.ss0 = ss0;
tss.esp0 = esp0;
memset(&tss, 0, sizeof(Tss_entry));
tss.ss0 = kernel_ss;
tss.esp0 = kernel_esp + 0x1000; // SHIT
tss.cs = 0x1B;
tss.ss = 0x23;
@ -44,18 +47,17 @@ void tss_set_gate(uint32_t num, uint16_t ss0, uint32_t esp0)
void gdt_install()
{
gdt_set_gate(0, 0x0, 0x0, 0x0, 0x0); // Null segment
gdt_set_gate(1, 0x0, 0xFFFFFFFF, 0x9A, 0xC0); // 0x08 Kernel code segment
gdt_set_gate(2, 0x0, 0xFFFFFFFF, 0x92, 0xC0); // 0x10 Kernel data segment
gdt_set_gate(3, 0x0, 0xFFFFFFFF, 0xFA, 0xC0); // 0x18 User code segment
gdt_set_gate(4, 0x0, 0xFFFFFFFF, 0xF2, 0xC0); // 0x20 User data segment
gdt_set_gate(1, 0x0, 0xFFFFFFFF, 0x9A, 0xCF); // 0x08 Kernel code segment
gdt_set_gate(2, 0x0, 0xFFFFFFFF, 0x92, 0xCF); // 0x10 Kernel Data segment
gdt_set_gate(3, 0x0, 0xFFFFFFFF, 0xFA, 0xCF); // 0x18 User code segment
gdt_set_gate(4, 0x0, 0xFFFFFFFF, 0xF2, 0xCF); // 0x20 User data segment
tss_set_gate(5, 0x10, 0);
tss_install(5, 0x10, stack_top);
gdt_ptr.limit = (sizeof(Gdt_entry) * GDT_ENTRY) - 1;
gdt_ptr.base = (uint32_t)&gdt;
gdtp.limit = (sizeof(Gdt_entry) * GDT_ENTRY) - 1;
gdtp.base = (uint32_t)&gdt;
gdt_flush();
gdt_flush((uint32_t)&gdtp);
tss_flush();
}

4
gdt.h
View File

@ -3,6 +3,8 @@
#include <stdint.h>
extern uint32_t stack_top;
typedef struct gdt_entry_t Gdt_entry;
struct gdt_entry_t
{
@ -21,7 +23,7 @@ struct gdt_ptr_t
uint32_t base;
} __attribute__((packed));
void gdt_set_gate(uint8_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t granularity);
static void gdt_set_gate(uint8_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t granularity);
void gdt_install();
void set_kernel_stack(uint32_t stack);

View File

@ -1,10 +1,7 @@
.section .text
.align 4
.global gdt_flush
.type gdt_flush, @function
gdt_flush:
lgdt gdt_ptr
lgdt gdtp
jmp $0x08, $reload_cs
reload_cs:

20
idt.c
View File

@ -1,24 +1,8 @@
#include <stdint.h>
#include "idt.h"
#include "io.h"
#include "string.h"
#include <stdint.h>
#define IDT_ENTRIES 256
struct idt_entry
{
uint16_t base_lo;
uint16_t sel;
uint8_t always0;
uint8_t flags;
uint16_t base_hi;
} __attribute__((packed));
struct idt_ptr
{
uint16_t limit;
uint32_t base;
} __attribute__((packed));
struct idt_entry idt[IDT_ENTRIES];
struct idt_ptr idtp;

17
idt.h
View File

@ -3,6 +3,23 @@
#include <stdint.h>
#define IDT_ENTRIES 256
struct idt_entry
{
uint16_t base_lo;
uint16_t sel;
uint8_t always0;
uint8_t flags;
uint16_t base_hi;
} __attribute__((packed));
struct idt_ptr
{
uint16_t limit;
uint32_t base;
} __attribute__((packed));
extern void idt_load();
void idt_install();

View File

@ -1,8 +1,5 @@
.section .text
.align 4
.global idt_load
.type idt_load, @function
idt_load:
lidt idtp
ret

View File

@ -6,13 +6,9 @@
typedef struct stack_t Stack;
struct stack_t
{
/* Pushed the segs last */
uint32_t gs, fs, es, ds;
/* Pushed by 'pusha' */
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
/* Our 'push #' and ecodes do this */
uint32_t id, err_code;
/* Pushed by the processor automatically */
uint32_t eip, cs, eflags, useresp, ss;
};

14
io.c
View File

@ -18,3 +18,17 @@ void io_wait(void)
"1:jmp 2f\n\t"
"2:");
}
void outb_wait(uint16_t port, uint8_t val)
{
outb(port, val);
io_wait();
}
uint8_t inb_wait(uint16_t port)
{
uint8_t ret;
ret = inb(port);
io_wait();
return ret;
}

3
io.h
View File

@ -7,4 +7,7 @@ void outb(uint16_t port, uint8_t val);
uint8_t inb(uint16_t port);
void io_wait(void);
void outb_wait(uint16_t port, uint8_t val);
uint8_t inb_wait(uint16_t port);
#endif

31
irq.c
View File

@ -4,7 +4,6 @@
#include "io.h"
#include "sys.h"
#include "idt.h"
#include "tty.h"
extern void irq0(void);
extern void irq1(void);
@ -40,19 +39,23 @@ void irq_call_handler(Stack *registers)
void (*handler)(Stack *registers);
handler = irq_routines[registers->id - 32];
if (handler)
{
handler(registers);
}
}
static void irq_remap(void) // PIC REMAP
static void irq_remap(void)
{
outb(PIC1_CMD, ICW1_INIT | ICW1_ICW4);
outb(PIC2_CMD, ICW1_INIT | ICW1_ICW4);
outb(PIC1_DATA, PIC1_OFFSET);
outb(PIC2_DATA, PIC2_OFFSET);
outb(PIC1_DATA, 0x04);
outb(PIC2_DATA, 0x02);
outb(PIC1_DATA, ICW4_8086);
outb(PIC2_DATA, ICW4_8086);
outb(0x20, 0x11);
outb(0xA0, 0x11);
outb(0x21, 0x20);
outb(0xA1, 0x28);
outb(0x21, 0x04);
outb(0xA1, 0x02);
outb(0x21, 0x01);
outb(0xA1, 0x01);
outb(0x21, 0x00);
outb(0xA1, 0x00);
}
void irq_install(void)
@ -84,9 +87,7 @@ void irq_handler(Stack *registers)
irq_call_handler(registers);
if (registers->id >= 40)
{
outb(PIC2_CMD, PIC_EOI);
}
outb(0xA0, 0x20);
outb(PIC1_CMD, PIC_EOI);
}
outb(0x20, 0x20);
}

View File

@ -8,7 +8,7 @@
cli
push $0x00
push $\entry
jmp irq_common_stub
jmp irq_flush
.endm
IRQ 0, 32
@ -28,8 +28,9 @@ IRQ 13, 45
IRQ 14, 46
IRQ 15, 47
irq_common_stub:
irq_flush:
pusha
push %ds
push %es
push %fs
@ -55,4 +56,4 @@ irq_common_stub:
popa
add $8, %esp
iret
iret

12
isr.c
View File

@ -55,7 +55,9 @@ void isr_call_handler(Stack *registers)
void (*handler)(Stack *registers);
handler = isr_routines[registers->id];
if (handler)
{
handler(registers);
}
}
void isr_install(void)
@ -139,20 +141,18 @@ static const char *exception_messages[] =
void fault_handler(Stack *registers)
{
// check this
if (registers->id == 128)
{
isr_call_handler(registers);
}
if (registers->id < 32)
{
terminal_printf("Exception. System Halted!\n");
terminal_printf("Exception: ");
terminal_printf(exception_messages[registers->id]);
isr_call_handler(registers);
for (;;)
{
sys_halt();
}
}
}
}

View File

@ -1,64 +1,45 @@
.section .text
.align 4
.macro ISR_NO_ERR index
.global isr\index
isr\index:
cli
push $0
push $\index
jmp isr_common_stub
.global isr\index
isr\index:
cli
push $0
push $\index
jmp isr_flush
.endm
.macro ISR_ERR index
.global isr\index
isr\index:
cli
push $\index
jmp isr_common_stub
.global isr\index
isr\index:
cli
push $\index
jmp isr_flush
.endm
/* Division By Zero Exception */
ISR_NO_ERR 0
/* Debug Exception */
ISR_NO_ERR 1
/* Non Maskable Interrupt Exception */
ISR_NO_ERR 2
/* Breakpoint Exception */
ISR_NO_ERR 3
/* Into Detected Overflow Exception */
ISR_NO_ERR 4
/* Out of Bounds Exception */
ISR_NO_ERR 5
/* Invalid Opcode Exception */
ISR_NO_ERR 6
/* No Coprocessor Exception */
ISR_NO_ERR 7
/* Double Fault Exception */
ISR_ERR 8
/* Coprocessor Segment Overrun Exception */
ISR_NO_ERR 9
/* Bad TSS Exception */
ISR_ERR 10
/* Segment Not Present Exception */
ISR_ERR 11
/* Stack Fault Exception */
ISR_ERR 12
/* General Protection Fault Exception */
ISR_ERR 13
/* Page Fault Exception */
ISR_ERR 14
/* Unknown Interrupt Exception */
ISR_NO_ERR 15
/* Coprocessor Fault Exception */
ISR_NO_ERR 16
/* Alignment Check Exception */
ISR_NO_ERR 17
/* Machine Check Exception */
ISR_NO_ERR 18
/* All the others are reserved */
ISR_NO_ERR 19
ISR_NO_ERR 0 # Division By Zero Exception
ISR_NO_ERR 1 # Debug Exception
ISR_NO_ERR 2 # Non Maskable Interrupt Exception
ISR_NO_ERR 3 # Breakpoint Exception
ISR_NO_ERR 4 # Into Detected Overflow Exception
ISR_NO_ERR 5 # Out of Bounds Exception
ISR_NO_ERR 6 # Invalid Opcode Exception
ISR_NO_ERR 7 # No Coprocessor Exception
ISR_ERR 8 # Double Fault Exception
ISR_NO_ERR 9 # Coprocessor Segment Overrun Exception
ISR_ERR 10 # Bad TSS Exception
ISR_ERR 11 # Segment Not Present Exception
ISR_ERR 12 # Stack Fault Exception
ISR_ERR 13 # General Protection Fault Exception
ISR_ERR 14 # Page Fault Exception
ISR_NO_ERR 15 # Unknown Interrupt Exception
ISR_NO_ERR 16 # Coprocessor Fault Exception
ISR_NO_ERR 17 # Alignment Check Exception
ISR_NO_ERR 18 # Machine Check Exception
ISR_NO_ERR 19 # Other
ISR_NO_ERR 20
ISR_NO_ERR 21
ISR_NO_ERR 22
@ -71,32 +52,34 @@ ISR_NO_ERR 28
ISR_NO_ERR 29
ISR_NO_ERR 30
ISR_NO_ERR 31
/* Syscall */
ISR_NO_ERR 128
ISR_NO_ERR 128 # 80h syscall
isr_common_stub:
pusha
push %ds
push %es
push %fs
push %gs
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %esp, %eax
push %eax
mov $fault_handler, %eax
call *%eax
pop %eax
isr_flush:
pusha
push %ds
push %es
push %fs
push %gs
pop %gs
pop %fs
pop %es
pop %ds
popa
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
add $8, %esp
iret
mov %esp, %eax
push %eax
mov $fault_handler, %eax
call *%eax
pop %eax
pop %gs
pop %fs
pop %es
pop %ds
popa
add $8, %esp
iret

View File

@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "tty.h"
#include "gdt.h"
#include "idt.h"
@ -11,6 +12,7 @@
#include "shell.h"
#include "user_space.h"
#include "syscall.h"
#include "rtc.h"
void kernel_main(void)
{
@ -24,31 +26,24 @@ void kernel_main(void)
timer_install();
keyboard_install();
syscall_init();
for (int i = 0; i < 3; i++)
{
terminal_printc("&cDead &7");
terminal_putchar('0' + (i / 10));
terminal_putchar('0' + (i % 10));
terminal_printc("\n");
delay(100);
}
rtc_init();
enter_user_space_v2();
syscall_init();
enter_user_space();
}
void user_main(void)
void main(void)
{
terminal_printc("Welcome to &cDeadOS&7.\n");
for (int i = 0; i < 3; i++)
for (int i = 1; i < 4; i++)
{
terminal_printc("&cDead2 &7");
terminal_putchar('0' + (i / 10));
terminal_putchar('0' + (i % 10));
terminal_printc("\n");
delay(100);
terminal_printf("&cTest &7%d\n", i);
delay(200);
}
terminal_clear();
terminal_printf("Welcome to &cDeadOS&7.\n");
terminal_printf("You are now in user space.\n");
terminal_printf("Type 'help' for a list of available commands.\n");
shell_init();
}
}

View File

@ -199,4 +199,4 @@ void keyboard_install(void)
{
keyboard_clear_buffer();
irq_install_handler(1, keyboard_handler);
}
}

View File

@ -15,4 +15,4 @@ int keyboard_getchar(void);
unsigned char keyboard_getscancode(void);
void keyboard_install(void);
#endif
#endif

View File

@ -1,38 +1,27 @@
/* The bootloader will look at this image and start execution at the symbol
designated at the entry point. */
ENTRY(_start)
/* Tell where the various sections of the object files will be put in the final
kernel image. */
SECTIONS
{
/* The kernel is situated at 1 MiB */
. = 1M;
kernel_start = .;
/* First put the multiboot header, as it is required to be put very early
early in the image or the bootloader won't recognize the file format.
Next we'll put the .text section. */
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
/* Read-only data */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
/* Read-write data (initialized) */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(.bss)
@ -40,4 +29,4 @@ SECTIONS
}
kernel_end = .;
}
}

View File

@ -1,12 +0,0 @@
.section .text
.global pit_handler_asm
pit_handler_asm:
push %eax # Save the registers used by the C handler
push %ecx
push %edx
call pit_handler # Call the C handler
pop %edx # Restore the registers in reverse order
pop %ecx
pop %eax
iret # Return from interrupt

79
rtc.c Normal file
View File

@ -0,0 +1,79 @@
#include <stdint.h>
#include "io.h"
#include "interrupts.h"
#include "rtc.h"
uint8_t read_cmos(uint16_t reg)
{
outb_wait(CMOS_ADDRESS, reg);
return inb_wait(CMOS_DATA);
}
uint8_t get_update_in_progress_flag()
{
outb_wait(CMOS_ADDRESS, 0x0A);
return inb_wait(CMOS_DATA) & 0x80;
}
void rtc_handler()
{
outb_wait(CMOS_ADDRESS, 0x0C);
inb_wait(CMOS_DATA);
}
uint8_t bcd_to_bin(uint8_t val)
{
return ((val / 16) * 10) + (val & 0x0F);
}
struct Rtc_time get_rtc_time()
{
struct Rtc_time time;
while (get_update_in_progress_flag())
;
time.sec = read_cmos(REG_SECONDS);
time.min = read_cmos(REG_MINUTES);
time.hour = read_cmos(REG_HOURS);
time.mday = read_cmos(REG_DAY);
time.mon = read_cmos(REG_MONTH);
time.year = read_cmos(REG_YEAR);
uint8_t registerB = read_cmos(0x0B);
if (!(registerB & 0x04))
{
time.sec = bcd_to_bin(time.sec);
time.min = bcd_to_bin(time.min);
time.hour = bcd_to_bin(time.hour);
time.mday = bcd_to_bin(time.mday);
time.mon = bcd_to_bin(time.mon);
time.year = bcd_to_bin(time.year);
}
if (time.year < 70)
{
time.year += 100;
}
else if (time.year >= 100)
{
time.year -= 100;
}
time.year += 1900;
return time;
}
void rtc_init()
{
outb_wait(CMOS_ADDRESS, 0x8A);
uint8_t prev = inb_wait(CMOS_DATA);
outb_wait(CMOS_ADDRESS, 0x8A);
outb_wait(CMOS_DATA, (prev & 0xF0) | 0x0F);
irq_install_handler(8, rtc_handler);
}

29
rtc.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef RTC_H
#define RTC_H
#include <stdint.h>
#define CMOS_ADDRESS 0x70
#define CMOS_DATA 0x71
#define REG_SECONDS 0x00
#define REG_MINUTES 0x02
#define REG_HOURS 0x04
#define REG_DAY 0x07
#define REG_MONTH 0x08
#define REG_YEAR 0x09
struct Rtc_time
{
uint8_t sec;
uint8_t min;
uint8_t hour;
uint8_t mday;
uint8_t mon;
uint16_t year;
};
void rtc_init();
struct Rtc_time get_rtc_time();
#endif

45
shell.c
View File

@ -3,6 +3,7 @@
#include "shell.h"
#include "tty.h"
#include "string.h"
#include "rtc.h"
void shell_init(void)
{
@ -12,8 +13,7 @@ void shell_init(void)
while (true)
{
terminal_printc("\n");
terminal_printc("&7dead@&croot&7: /> ");
terminal_printf("&7dead@&croot&7: /> ");
i = 0;
c = 0;
@ -26,7 +26,7 @@ void shell_init(void)
{
if (i > 0)
{
terminal_printc("\b \b");
terminal_printf("\b \b");
--i;
}
}
@ -49,21 +49,30 @@ void shell_init(void)
void shell_help_command(void)
{
terminal_printc("List of commands:\n");
terminal_printc("help - Print out the list of commands.\n");
terminal_printc("exit - Exit the shell.\n");
terminal_printc("echo - Print out the message. &eWITH colors!&7\n");
terminal_printc("clear - Clear the terminal.\n");
terminal_printf("List of commands:\n");
terminal_printf("help - Print out the list of commands.\n");
terminal_printf("exit - Exit the shell.\n");
terminal_printf("echo - Print out the message. &eWITH colors!&7\n");
terminal_printf("clear - Clear the terminal.\n");
terminal_printf("time - Print out the current time. (UTC)\n");
}
void shell_exit_command(void)
{
terminal_printc("Exiting shell...\n");
terminal_printc("Goodbye!\n");
terminal_printf("Exiting shell...\n");
terminal_printf("Goodbye!\n");
while (1)
;
}
void shell_time_command(void)
{
struct Rtc_time current_time = get_rtc_time();
terminal_printf("Current time: %02d:%02d:%02d %02d.%02d.%d\n",
current_time.hour, current_time.min, current_time.sec,
current_time.mday, current_time.mon, current_time.year);
}
void shell_echo_command(char *input)
{
char *message = input;
@ -73,8 +82,8 @@ void shell_echo_command(char *input)
message++;
}
terminal_printc(message);
terminal_printc("\n");
terminal_printf(message);
terminal_printf("\n");
}
void shell_parse_input(char *input)
@ -103,11 +112,15 @@ void shell_parse_input(char *input)
{
terminal_clear();
}
else if (strcmp(command, "time") == 0)
{
shell_time_command();
}
else
{
terminal_printc("Unknown command: '");
terminal_writestring(command);
terminal_printc("'\n");
terminal_printc("Type 'help' to print out the list of commands.\n");
terminal_printf("Unknown command: '");
terminal_print(command);
terminal_printf("'\n");
terminal_printf("Type 'help' to print out the list of commands.\n");
}
}

View File

@ -6,4 +6,4 @@
void shell_init(void);
void shell_parse_input(char *input);
#endif
#endif

View File

@ -21,6 +21,19 @@ void *memset(void *ptr, int value, size_t size)
return ptr;
}
void memcpy(void *vd, const void *vs, unsigned length)
{
char *d = vd;
const char *s = vs;
while (length)
{
*d = *s;
d++;
s++;
length--;
}
}
int strcmp(const char *str1, const char *str2)
{
while (*str1 && (*str1 == *str2))
@ -94,3 +107,32 @@ char *strtok(char *str, const char *delim)
return token_start;
}
void itoa(int value, char *str, int base)
{
char *rc;
char *ptr;
char *low;
if (value < 0 && base == 10)
{
*str++ = '-';
value = -value;
}
rc = ptr = str;
do
{
*ptr++ = "0123456789abcdef"[value % base];
value /= base;
} while (value);
*ptr-- = '\0';
for (low = rc; low < ptr; low++, ptr--)
{
char tmp = *low;
*low = *ptr;
*ptr = tmp;
}
}

View File

@ -7,7 +7,9 @@ char *strchr(const char *str, int c);
char *strtok(char *str, const char *delim);
int strcmp(const char *str1, const char *str2);
size_t strlen(const char *str);
void itoa(int value, char *str, int base);
void *memset(void *ptr, int value, size_t size);
void memcpy(void *vd, const void *vs, unsigned length);
#endif

View File

@ -5,17 +5,14 @@
#include "sys.h"
#include "syscall.h"
#include "tty.h"
#include "timer.h"
#include "rtc.h"
#define NB_SYSCALL 8
#define NB_SYSCALL 3
void *syscalls[NB_SYSCALL] = {
terminal_writestring,
keyboard_getchar,
terminal_printc,
terminal_clear,
terminal_putchar,
terminal_putentryat,
keyboard_clear_buffer,
get_rtc_time,
sys_halt};
@ -49,4 +46,4 @@ void syscall_handler(Stack *registers)
void syscall_init(void)
{
isr_install_handler(128, syscall_handler);
}
}

35
timer.c
View File

@ -1,25 +1,27 @@
#include "timer.h"
#include "io.h"
#include "sys.h"
#include <stdint.h>
#include "interrupts.h"
#include "io.h"
#include "timer.h"
volatile uint32_t tick = 0;
volatile uint32_t timer_ticks = 0;
void pit_handler(Stack *registers)
void delay(const uint32_t ticks)
{
++tick;
}
void delay(uint32_t milliseconds)
{
uint32_t end = tick + milliseconds;
while (tick < end)
uint32_t total_ticks = timer_ticks + ticks;
while (timer_ticks < total_ticks)
;
}
void pit_set_frequency(uint32_t frequency)
void timer_handler(Stack *registers)
{
uint32_t divisor = PIT_FREQUENCY / frequency;
++timer_ticks;
}
static void timer_phase(const uint32_t hz)
{
uint32_t divisor = PIT_FREQUENCY / hz;
outb(PIT_COMMAND, 0x36);
outb(PIT_CHANNEL_0, divisor & 0xFF);
outb(PIT_CHANNEL_0, divisor >> 8);
@ -27,6 +29,7 @@ void pit_set_frequency(uint32_t frequency)
void timer_install(void)
{
irq_install_handler(0, pit_handler);
pit_set_frequency(1000);
irq_install_handler(0, timer_handler);
timer_phase(1000);
}

View File

@ -7,12 +7,7 @@
#define PIT_CHANNEL_0 0x40
#define PIT_COMMAND 0x43
extern void pit_handler_asm(); // defined in pit_handler.s remove this
void pit_handler();
void delay(uint32_t milliseconds);
void pit_set_frequency(uint32_t frequency);
void pic_remap(int offset1, int offset2); // moved to irq_remap
void timer_install(void);
#endif
#endif

159
tty.c
View File

@ -1,10 +1,11 @@
#include "tty.h"
#include "vga.h"
#include "io.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include "tty.h"
#include "vga.h"
#include "io.h"
#include "string.h"
static const size_t VGA_WIDTH = 80;
@ -21,13 +22,9 @@ void terminal_initialize(void)
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
terminal_buffer = (uint16_t *)0xB8000;
for (size_t y = 0; y < VGA_HEIGHT; y++)
for (size_t i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
terminal_buffer[i] = vga_entry(' ', terminal_color);
}
}
@ -63,37 +60,31 @@ void terminal_scroll(void)
terminal_row = VGA_HEIGHT - 1;
}
void terminal_update_cursor(void)
{
uint16_t pos = terminal_row * VGA_WIDTH + terminal_column;
outb(0x3D4, 14);
outb(0x3D5, pos >> 8);
outb(0x3D4, 15);
outb(0x3D5, pos);
}
static void terminal_backspace(void)
{
if (terminal_column == 0)
{
terminal_column = VGA_WIDTH - 1;
if (terminal_row == 0)
{
terminal_column = 0;
}
else
if (terminal_row > 0)
{
--terminal_row;
}
uint16_t empty = vga_entry(' ', terminal_color);
uint16_t index = terminal_pos(terminal_column, terminal_row);
while (terminal_column > 0 && terminal_buffer[index] == empty)
{
--terminal_column;
index = terminal_pos(terminal_column, terminal_row);
}
if (terminal_buffer[index] != empty && terminal_column + 1 < VGA_WIDTH)
{
++terminal_column;
}
}
else
{
--terminal_column;
}
uint16_t index = terminal_pos(terminal_column, terminal_row);
terminal_buffer[index] = vga_entry(' ', terminal_color);
terminal_buffer[terminal_pos(terminal_column, terminal_row)] = vga_entry(' ', terminal_color);
}
void terminal_putchar(char c)
@ -112,8 +103,7 @@ void terminal_putchar(char c)
}
else
{
uint16_t index = terminal_pos(terminal_column, terminal_row);
terminal_buffer[index] = vga_entry(c, terminal_color);
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH)
{
terminal_column = 0;
@ -134,71 +124,24 @@ void terminal_write(const char *data, size_t size)
}
}
void terminal_writestring(const char *data)
void terminal_print(const char *data)
{
terminal_write(data, strlen(data));
}
// Існуюча функція для виводу одного рядка
void terminal_printc(const char *data)
static void print_number(int value, int base, int padding)
{
while (*data)
{
if (*data == '&' && ((*(data + 1) >= '0' && *(data + 1) <= '9') ||
(*(data + 1) >= 'a' && *(data + 1) <= 'f')))
{
terminal_setcolor(vga_entry_color(vga_color_from_char(*(data + 1)), VGA_COLOR_BLACK));
data += 2;
}
else
{
terminal_putchar(*data);
data++;
}
}
}
char buffer[32];
itoa(value, buffer, base);
int len = strlen(buffer);
void itoa(int value, char *str, int base)
{
char *rc;
char *ptr;
char *low;
if (value < 0 && base == 10)
while (padding > len)
{
*str++ = '-';
value = -value;
terminal_putchar('0');
padding--;
}
rc = ptr = str;
do
{
*ptr++ = "0123456789abcdef"[value % base];
value /= base;
} while (value);
*ptr-- = '\0';
for (low = rc; low < ptr; low++, ptr--)
{
char tmp = *low;
*low = *ptr;
*ptr = tmp;
}
}
void terminal_printf_int(int value)
{
char buffer[12];
itoa(value, buffer, 10);
terminal_printf(buffer);
}
void terminal_printf_hex(unsigned int value)
{
char buffer[12];
itoa(value, buffer, 16);
terminal_printf(buffer);
terminal_print(buffer);
}
void terminal_printf(const char *format, ...)
@ -209,25 +152,36 @@ void terminal_printf(const char *format, ...)
const char *p = format;
while (*p)
{
if (*p == '%' && (*(p + 1) == 's' || *(p + 1) == 'd' || *(p + 1) == 'x'))
if (*p == '%' && (*(p + 1) == 's' || *(p + 1) == 'd' || *(p + 1) == 'x' || *(p + 1) == '0'))
{
p++;
int padding = 0;
if (*p == '0')
{
p++;
padding = *p - '0';
p++;
}
if (*p == 's')
{
const char *str = va_arg(args, const char *);
terminal_printc(str);
terminal_print(va_arg(args, const char *));
}
else if (*p == 'd')
{
int value = va_arg(args, int);
terminal_printf_int(value);
print_number(va_arg(args, int), 10, padding);
}
else if (*p == 'x')
{
unsigned int value = va_arg(args, unsigned int);
terminal_printf_hex(value);
print_number(va_arg(args, unsigned int), 16, padding);
}
}
else if (*p == '&' && ((*(p + 1) >= '0' && *(p + 1) <= '9') || (*(p + 1) >= 'a' && *(p + 1) <= 'f')))
{
terminal_setcolor(vga_entry_color(vga_color_from_char(*(p + 1)), VGA_COLOR_BLACK));
p++;
}
else
{
terminal_putchar(*p);
@ -238,28 +192,13 @@ void terminal_printf(const char *format, ...)
va_end(args);
}
void terminal_update_cursor(void)
{
uint16_t pos = terminal_row * VGA_WIDTH + terminal_column;
outb(0x3D4, 14);
outb(0x3D5, pos >> 8);
outb(0x3D4, 15);
outb(0x3D5, pos);
}
void terminal_clear(void)
{
for (size_t y = 0; y < VGA_HEIGHT; y++)
for (size_t i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
terminal_buffer[i] = vga_entry(' ', terminal_color);
}
terminal_column = 0;
terminal_row = 0;
terminal_update_cursor();
}
}

9
tty.h
View File

@ -5,14 +5,9 @@
#include <stdint.h>
void terminal_initialize(void);
void terminal_setcolor(uint8_t color);
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y);
void terminal_putchar(char c);
void terminal_write(const char *data, size_t size);
void terminal_writestring(const char *data);
void terminal_printf(const char *data, ...);
void terminal_printc(const char *data);
void terminal_update_cursor(void);
void terminal_print(const char *data);
void terminal_clear(void);
void terminal_putchar(char c);
#endif

View File

@ -1,26 +1,4 @@
void enter_user_space(void)
{
asm volatile("cli; \
mov $0x23, %ax; \
mov %ax, %ds; \
mov %ax, %es; \
mov %ax, %fs; \
mov %ax, %gs; \
mov %esp, %eax; \
pushl $0x23; \
pushl %eax; \
pushf; \
popl %eax; \
orl $0x200, %eax; \
pushl %eax; \
pushl $0x1B; \
push $1f; \
iret; \
1: \
");
}
void enter_user_space_v2()
void enter_user_space()
{
asm volatile("cli; \
mov $0x23, %ax; \
@ -39,6 +17,6 @@ void enter_user_space_v2()
push $user_code_entry; \
iret; \
user_code_entry: \
call user_main; \
call main; \
");
}

View File

@ -2,6 +2,5 @@
#define _USER_SPACE_H
void enter_user_space(void);
void enter_user_space_v2(void);
#endif