From 95cd78840f0891e60f5ebecc8a8eb4fbaf3c2ebf Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Sun, 12 Jan 2025 01:17:36 -0600 Subject: PROJECT RESTRUCTURING Move the entire kernel into its own directory. Create new directories for system commands, libraries and other required essentials for a complete Unix-like operating system. Signed-off-by: Danny Holman --- arch/i386/kernel/gdt.c | 58 --------------- arch/i386/kernel/idt.c | 149 -------------------------------------- arch/i386/kernel/isr.s | 129 --------------------------------- arch/i386/kernel/jump_userspace.s | 21 ------ arch/i386/kernel/kmalloc.c | 68 ----------------- arch/i386/kernel/multiboot.c | 49 ------------- arch/i386/kernel/paging.c | 130 --------------------------------- arch/i386/kernel/pic.c | 79 -------------------- arch/i386/kernel/pmem.c | 45 ------------ arch/i386/kernel/serial.c | 42 ----------- arch/i386/kernel/spinlock.s | 30 -------- arch/i386/kernel/stack_trace.s | 31 -------- arch/i386/kernel/switch_thread.s | 28 ------- arch/i386/kernel/syscall.c | 25 ------- arch/i386/kernel/timer.c | 24 ------ 15 files changed, 908 deletions(-) delete mode 100644 arch/i386/kernel/gdt.c delete mode 100644 arch/i386/kernel/idt.c delete mode 100644 arch/i386/kernel/isr.s delete mode 100644 arch/i386/kernel/jump_userspace.s delete mode 100644 arch/i386/kernel/kmalloc.c delete mode 100644 arch/i386/kernel/multiboot.c delete mode 100644 arch/i386/kernel/paging.c delete mode 100644 arch/i386/kernel/pic.c delete mode 100644 arch/i386/kernel/pmem.c delete mode 100644 arch/i386/kernel/serial.c delete mode 100644 arch/i386/kernel/spinlock.s delete mode 100644 arch/i386/kernel/stack_trace.s delete mode 100644 arch/i386/kernel/switch_thread.s delete mode 100644 arch/i386/kernel/syscall.c delete mode 100644 arch/i386/kernel/timer.c (limited to 'arch/i386/kernel') diff --git a/arch/i386/kernel/gdt.c b/arch/i386/kernel/gdt.c deleted file mode 100644 index 0d9e1d2..0000000 --- a/arch/i386/kernel/gdt.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include - -struct gdt_entry desc[6]; -struct gdt_ptr gp; -struct tss_entry tss_entry; - -void gdt_set_gate(int num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) { - desc[num].base_low = (base & 0xFFFF); - desc[num].base_middle = (base >> 16) & 0xFF; - desc[num].base_high = (base >> 24) & 0xFF; - - desc[num].limit_low = (limit & 0xFFFF); - desc[num].gran = (limit >> 16) & 0x0F; - desc[num].gran |= (gran & 0xF0); - desc[num].access = access; -} - -void write_tss(int num, uint32_t ss0, uint32_t esp0) { - uint32_t base = (uint32_t)&tss_entry; - uint32_t limit = base + sizeof(struct tss_entry); - - gdt_set_gate(num, base, limit, 0x89, 0x00); - memset(&tss_entry, 0x0, sizeof(tss_entry)); - - tss_entry.ss0 = ss0; - tss_entry.esp0 = esp0; - tss_entry.cs = 0x0B; - tss_entry.ss = 0x13; - tss_entry.ds = 0x13; - tss_entry.es = 0x13; - tss_entry.fs = 0x13; - tss_entry.gs = 0x13; - tss_entry.iomap_base = sizeof(tss_entry); -} - -void set_kernel_esp(uint32_t esp) { - tss_entry.esp0 = esp; -} - -void gdt_install(void) { - gp.limit = (sizeof(struct gdt_entry) * 6) - 1; - gp.base = (uint32_t)&desc; - - gdt_set_gate(0, 0, 0, 0, 0); - gdt_set_gate(1, 0, 0xFFFFF, 0x9A, 0xCF); - gdt_set_gate(2, 0, 0xFFFFF, 0x92, 0xCF); - gdt_set_gate(3, 0, 0xFFFFF, 0xFA, 0xCF); - gdt_set_gate(4, 0, 0xFFFFF, 0xF2, 0xCF); - - uint32_t esp; - __asm__ volatile("movl %%esp, %0" : "=r"(esp)); - write_tss(5, 0x10, esp); - - flush_gdt(); - flush_tss(); -} diff --git a/arch/i386/kernel/idt.c b/arch/i386/kernel/idt.c deleted file mode 100644 index a178801..0000000 --- a/arch/i386/kernel/idt.c +++ /dev/null @@ -1,149 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -__attribute__((aligned(0x10))) -struct idt_entry idt[256]; -struct idt_ptr idtr; - -const char* exceptions[] = { - "Division by zero", - "Debug", - "Non-maskable interrupt", - "Breakpoint", - "Overflow", - "Out-of-bounds", - "Invalid opcode", - "FPU not available", - "Double fault", - "RESERVED", - "Invalid TSS", - "Segment not present", - "Stack fault", - "General protection fault", - "Page fault", - "RESERVED", - "FPU exception", - "Alignment check", - "Machine check", - "FPU-SIMD exception", - "Virtualization exception", - "Control protection" - "RESERVED", - "Hypervisor injection", - "VMM exception", - "Security exception", - "RESERVED", - "Triple fault", - "RESERVED" -}; - -void exception_handler(struct isr_frame *frame) { - switch (frame->isr_vector) { - case 0x00: - panic("Division by zero in kernel address space"); - break; - case 0x06: - panic("Invalid opcode in kernel address space"); - break; - case 0x08: - panic("Double fault in interrupt handler"); - break; - case 0x0D: - panic("Protection fault in kernel address space"); - break; - case 0x0E: - page_fault_handler(frame); - break; - default: - panic("Unhandled exception"); - } -} - -void generic_handler(struct isr_frame *frame) { - kprintf("No handler registered for IRQ %d\n", frame->isr_vector); - if (frame->isr_vector > 32 && frame->isr_vector < 48) - pic_eoi(frame->isr_vector-32); -} - -void idt_set_gate(uint8_t num, void (*handler)(void), uint16_t cs, uint8_t flags) { - struct idt_entry *desc = &idt[num]; - desc->isr_low = (uint16_t)(((uint32_t)handler >> 0) & 0xFFFF); - desc->isr_high = (uint16_t)(((uint32_t)handler >> 16) & 0xFFFF); - desc->reserved = 0; - desc->kernel_cs = cs; - desc->flags = flags; -} - -void idt_install(void) { - idtr.limit = (uint16_t)sizeof(struct idt_entry) * 256 - 1; - idtr.base = (uint32_t)idt; - - for (int i = 0; i < 32; i++) - register_isr_handler(i, exception_handler); - for (int i = 32; i < 224; i++) - register_isr_handler(i, generic_handler); - register_isr_handler(0x80, handle_syscall); - - idt_set_gate(0x0, isr_stub_0, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1, isr_stub_1, 0x08, IDT_EXCEPTION); - idt_set_gate(0x2, isr_stub_2, 0x08, IDT_EXCEPTION); - idt_set_gate(0x3, isr_stub_3, 0x08, IDT_EXCEPTION); - idt_set_gate(0x4, isr_stub_4, 0x08, IDT_EXCEPTION); - idt_set_gate(0x5, isr_stub_5, 0x08, IDT_EXCEPTION); - idt_set_gate(0x6, isr_stub_6, 0x08, IDT_EXCEPTION); - idt_set_gate(0x7, isr_stub_7, 0x08, IDT_EXCEPTION); - idt_set_gate(0x8, isr_stub_8, 0x08, IDT_EXCEPTION); - idt_set_gate(0x9, isr_stub_9, 0x08, IDT_EXCEPTION); - idt_set_gate(0xA, isr_stub_10, 0x08, IDT_EXCEPTION); - idt_set_gate(0xB, isr_stub_11, 0x08, IDT_EXCEPTION); - idt_set_gate(0xC, isr_stub_12, 0x08, IDT_EXCEPTION); - idt_set_gate(0xD, isr_stub_13, 0x08, IDT_EXCEPTION); - idt_set_gate(0xE, isr_stub_14, 0x08, IDT_EXCEPTION); - idt_set_gate(0xF, isr_stub_15, 0x08, IDT_EXCEPTION); - idt_set_gate(0x10, isr_stub_16, 0x08, IDT_EXCEPTION); - idt_set_gate(0x11, isr_stub_17, 0x08, IDT_EXCEPTION); - idt_set_gate(0x12, isr_stub_18, 0x08, IDT_EXCEPTION); - idt_set_gate(0x13, isr_stub_19, 0x08, IDT_EXCEPTION); - idt_set_gate(0x14, isr_stub_20, 0x08, IDT_EXCEPTION); - idt_set_gate(0x15, isr_stub_21, 0x08, IDT_EXCEPTION); - idt_set_gate(0x16, isr_stub_22, 0x08, IDT_EXCEPTION); - idt_set_gate(0x17, isr_stub_23, 0x08, IDT_EXCEPTION); - idt_set_gate(0x18, isr_stub_24, 0x08, IDT_EXCEPTION); - idt_set_gate(0x19, isr_stub_25, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1A, isr_stub_26, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1B, isr_stub_27, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1C, isr_stub_28, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1D, isr_stub_29, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1E, isr_stub_30, 0x08, IDT_EXCEPTION); - idt_set_gate(0x1F, isr_stub_31, 0x08, IDT_EXCEPTION); - - idt_set_gate(0x20, irq_stub_0, 0x08, IDT_INTERRUPT); - idt_set_gate(0x21, irq_stub_1, 0x08, IDT_INTERRUPT); - idt_set_gate(0x22, irq_stub_2, 0x08, IDT_INTERRUPT); - idt_set_gate(0x23, irq_stub_3, 0x08, IDT_INTERRUPT); - idt_set_gate(0x24, irq_stub_4, 0x08, IDT_INTERRUPT); - idt_set_gate(0x25, irq_stub_5, 0x08, IDT_INTERRUPT); - idt_set_gate(0x26, irq_stub_6, 0x08, IDT_INTERRUPT); - idt_set_gate(0x27, irq_stub_7, 0x08, IDT_INTERRUPT); - idt_set_gate(0x28, irq_stub_8, 0x08, IDT_INTERRUPT); - idt_set_gate(0x29, irq_stub_9, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2A, irq_stub_10, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2B, irq_stub_11, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2C, irq_stub_12, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2D, irq_stub_13, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2E, irq_stub_14, 0x08, IDT_INTERRUPT); - idt_set_gate(0x2F, irq_stub_15, 0x08, IDT_INTERRUPT); - - idt_set_gate(0x80, isr_stub_128, 0x08, IDT_INTERRUPT); - - __asm__ volatile("lidt %0" : : "memory"(idtr)); -} diff --git a/arch/i386/kernel/isr.s b/arch/i386/kernel/isr.s deleted file mode 100644 index 55db35e..0000000 --- a/arch/i386/kernel/isr.s +++ /dev/null @@ -1,129 +0,0 @@ -.section .text - -.macro isr_err_stub num -.global isr_stub_\num -.type isr_stub_\num, @function -isr_stub_\num: - cli - pushl $\num - jmp isr_frame_asm -.endm -.macro isr_no_err_stub num -.global isr_stub_\num -.type isr_stub_\num, @function -isr_stub_\num: - cli - pushl $0 - pushl $\num - jmp isr_frame_asm -.endm - -.macro irq_stub num -.global irq_stub_\num -.type irq_stub_\num, @function -irq_stub_\num: - cli - pushl $0 - pushl $\num+32 - jmp isr_frame_asm -.endm - -.global syscall_stub -syscall_stub: - cli - pushl $0 - pushl $0x80 - jmp isr_frame_asm - -isr_frame_asm: - pushl %eax - pushl %ebx - pushl %ecx - pushl %edx - pushl %esi - pushl %edi - - movl %cr0, %eax - pushl %eax - movl %cr2, %eax - pushl %eax - movl %cr3, %eax - pushl %eax - movl %cr4, %eax - pushl %eax - - pushl %esp - call set_kernel_esp - addl $4, %esp - - cld - call interrupt_handler - - popl %eax - movl %eax, %cr4 - popl %eax - movl %eax, %cr3 - popl %eax - movl %eax, %cr2 - popl %eax - movl %eax, %cr0 - - popl %edi - popl %esi - popl %edx - popl %ecx - popl %ebx - popl %eax - addl $8, %esp - - iret - -isr_no_err_stub 0 -isr_no_err_stub 1 -isr_no_err_stub 2 -isr_no_err_stub 3 -isr_no_err_stub 4 -isr_no_err_stub 5 -isr_no_err_stub 6 -isr_no_err_stub 7 -isr_err_stub 8 -isr_no_err_stub 9 -isr_err_stub 10 -isr_err_stub 11 -isr_err_stub 12 -isr_err_stub 13 -isr_err_stub 14 -isr_no_err_stub 15 -isr_no_err_stub 16 -isr_err_stub 17 -isr_no_err_stub 18 -isr_no_err_stub 19 -isr_no_err_stub 20 -isr_err_stub 21 -isr_no_err_stub 22 -isr_no_err_stub 23 -isr_no_err_stub 24 -isr_no_err_stub 25 -isr_no_err_stub 26 -isr_no_err_stub 27 -isr_no_err_stub 28 -isr_err_stub 29 -isr_err_stub 30 -isr_no_err_stub 31 - -irq_stub 0 -irq_stub 1 -irq_stub 2 -irq_stub 3 -irq_stub 4 -irq_stub 5 -irq_stub 6 -irq_stub 7 -irq_stub 8 -irq_stub 9 -irq_stub 10 -irq_stub 11 -irq_stub 12 -irq_stub 13 -irq_stub 14 -irq_stub 15 diff --git a/arch/i386/kernel/jump_userspace.s b/arch/i386/kernel/jump_userspace.s deleted file mode 100644 index add144a..0000000 --- a/arch/i386/kernel/jump_userspace.s +++ /dev/null @@ -1,21 +0,0 @@ -.global jump_userspace -.type jump_userspace, @function -jump_userspace: - movw $0x23, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs - - movl %esp, %eax - pushl $0x23 - pushl %eax - pushf - push $0x1B - pushl $start_init - - pushl %esp - call set_kernel_esp - addl $4, %esp - - iret diff --git a/arch/i386/kernel/kmalloc.c b/arch/i386/kernel/kmalloc.c deleted file mode 100644 index ea77428..0000000 --- a/arch/i386/kernel/kmalloc.c +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -static uintptr_t heap_start = KEND + 0xC0001000; -static uintptr_t heap_end = 0xFFBFF000; - -static void *kbrk = NULL; -static struct mem_block *first = NULL; - -void _request_page(void) { - uintptr_t paddr = pfa_alloc(1); - uintptr_t vaddr = PGROUNDDN((uintptr_t)kbrk + PAGE_SIZE); - kbrk += PAGE_SIZE; - map_page(paddr, vaddr, PD_PRES | PD_RW); -} - -void kmalloc_init(void) { - kbrk = PGROUNDDN(heap_start); - _request_page(); - first = (struct mem_block*)kbrk; - first->start = (uintptr_t)kbrk + sizeof(struct mem_block); - first->size = 0; - first->alloc = 1; - first->next = NULL; -} - -void* kmalloc(size_t sz) { - if (kbrk == NULL) - panic("Attmpted to malloc before initialization"); - if ((uintptr_t)kbrk >= heap_end) - return NULL; - - struct mem_block *temp = first; - while (temp->next != NULL) { - if (temp->next->size <= sz && temp->next->alloc == 0) { - temp->next->alloc = 1; - return (void*)temp->next->start; - } - temp = temp->next; - } - - temp->next = (struct mem_block*)((uintptr_t)temp + sizeof(struct mem_block) + temp->size); - if (temp->next > kbrk) - _request_page(); - temp->next->prev = temp; - temp->next->start = (uintptr_t)(temp->next + sizeof(struct mem_block)); - temp->next->size = sz; - temp->next->alloc = 1; - temp->next->next = NULL; - return (void*)temp->start; -} - -void kfree(void *ptr) { - struct mem_block *temp = first; - while (temp != NULL) { - if (temp->start == (uintptr_t)ptr) { - temp->alloc = 0; - return; - } - temp = temp->next; - } - panic("Attempted to free memory not alloc'd"); -} diff --git a/arch/i386/kernel/multiboot.c b/arch/i386/kernel/multiboot.c deleted file mode 100644 index dedf163..0000000 --- a/arch/i386/kernel/multiboot.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -void kernel_main(char *cmdline); - -void i386_entry(uint32_t mboot_magic, struct mboot_info *header) { - map_page(0xB8000, (uintptr_t)VGA_MEMORY, PD_RW); - fb_init(); - - if (mboot_magic != MBOOT_LOADER_MAGIC) { - disable_ints(); - panic("Not booted with multiboot bootloader"); - } - - map_page(0x9000, 0x9000, PD_RW); - if (!(header->flags >> 6 & 0x1)) { - disable_ints(); - panic("Physical memory map not provided by bootloader"); - } - - char cmdline[4096]; - map_page(header->cmdline, PAGE_TMP_MAP, 0); - memcpy(cmdline, (char*)PAGE_TMP_MAP, strlen((char*)PAGE_TMP_MAP)); - unmap_page(PAGE_TMP_MAP); - pfa_init(header); - gdt_install(); - idt_install(); - paging_init(); - map_page(0xB8000, (uintptr_t)VGA_MEMORY, PD_RW); - pic_remap(); - timer_init(); - - enable_ints(); - kernel_main(cmdline); - - while (1); -} diff --git a/arch/i386/kernel/paging.c b/arch/i386/kernel/paging.c deleted file mode 100644 index a0bbb92..0000000 --- a/arch/i386/kernel/paging.c +++ /dev/null @@ -1,130 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -static int paging_enabled = 0; - -void paging_init(void) { - if (paging_enabled == 1) - return; - - uintptr_t phys_pt = pfa_alloc(1); - map_page(phys_pt, PAGE_TMP_MAP, PD_PRES | PD_RW); - uintptr_t *pt = (uintptr_t*)PAGE_TMP_MAP; - for (uintptr_t i = KSTART; i < KEND; i += PAGE_SIZE) - pt[GET_PTX(GET_VADDR(i))] = i | PD_PRES; - unmap_page(PAGE_TMP_MAP); - - uintptr_t temp_pt = pfa_alloc(1); - - uintptr_t phys_pd = pfa_alloc(1); - map_page(phys_pd, PAGE_TMP_MAP, PD_PRES | PD_RW); - uintptr_t *pd = (uintptr_t*)PAGE_TMP_MAP; - pd[GET_PDX(GET_VADDR(KSTART))] = phys_pt | PD_PRES | PD_RW; - pd[GET_PDX(PAGE_TMP_MAP)] = temp_pt | PD_PRES | PD_RW; - pd[1023] = phys_pd | PD_PRES | PD_RW; - unmap_page(PAGE_TMP_MAP); - - enable_paging(phys_pd); - paging_enabled = 1; - return; -} - -void init_page_table(int pdindex, uint32_t flags) { - uintptr_t *pd = (uintptr_t*)PAGE_DIR_MAP; - if ((pd[pdindex] & PD_PRES) != 0) - return; - - uintptr_t paddr = pfa_alloc(1); - pd[pdindex] = paddr | PD_PRES | PD_RW | flags; - - uintptr_t *pt = ((uintptr_t*)PAGE_TAB_MAP) + (pdindex << 12); - pd[GET_PDX(pt)] = paddr | PD_PRES | PD_RW; -} - -uintptr_t init_page_directory(void) { - uintptr_t ret = pfa_alloc(1); - map_page(ret, PAGE_TMP_MAP, PD_RW); - - uintptr_t *new_pd = (uintptr_t*)PAGE_TMP_MAP; - uintptr_t *cur_pd = (uintptr_t*)PAGE_DIR_MAP; - int kernel_pdx = GET_PDX(GET_VADDR(KSTART)); - new_pd[kernel_pdx] = cur_pd[kernel_pdx]; - - unmap_page(PAGE_TMP_MAP); - return ret; -} - -void map_page(uintptr_t paddr, uintptr_t vaddr, uint32_t flags) { - paddr = PGROUNDDN(paddr); - vaddr = PGROUNDDN(vaddr); - - int pdindex = GET_PDX(vaddr); - int ptindex = GET_PTX(vaddr); - - uintptr_t *pd = (uintptr_t*)PAGE_DIR_MAP; - if ((pd[pdindex] & PD_PRES) == 0) - init_page_table(pdindex, 0); - - uintptr_t *pt = (uintptr_t*)(PAGE_TAB_MAP + (pdindex << 12)); - pt[ptindex] = paddr | PD_PRES | flags; - invlpg((void*)vaddr); -} - -void unmap_page(uintptr_t vaddr) { - uintptr_t pdindex = GET_PDX(vaddr); - uintptr_t ptindex = GET_PTX(vaddr); - - uintptr_t *pd = (uintptr_t*)PAGE_DIR_MAP; - if ((pd[pdindex] & PD_PRES) == 0) - return; - - uintptr_t *pt = (uintptr_t*)(PAGE_TAB_MAP + (pdindex << 12)); - pt[ptindex] = 0; - invlpg((void*)vaddr); -} - -void page_fault_handler(struct isr_frame *frame) { - uintptr_t fault_addr; - __asm__ volatile("movl %%cr2, %0" : "=r"(fault_addr)); - - kprintf("Faulting address: %x\n", fault_addr); - panic("Page fault not operational"); - switch (frame->isr_err) { - case 0: - //map_page((uintptr_t*)GET_VADDR(frame->cr3), pfa_alloc(), fault_addr, PD_PRES); - break; - case 1: - panic("Kernel process caused protection fault on read\n"); - break; - case 2: - //map_page((uintptr_t*)GET_VADDR(frame->cr3), pfa_alloc(), fault_addr, PD_PRES | PD_RW); - break; - case 3: - panic("Kernel process caused protection fault on write\n"); - break; - case 4: - //map_page((uintptr_t*)GET_VADDR(frame->cr3), pfa_alloc(), fault_addr, PD_PRES | PD_USR); - break; - case 5: - // TODO: instead of panicking, kill process - panic("User process caused protection fault on read\n"); - break; - case 6: - //map_page((uintptr_t*)GET_VADDR(frame->cr3), pfa_alloc(), fault_addr, PD_PRES | PD_RW | PD_USR); - break; - case 7: - // TODO: see case 101 - panic("USER process caused protection fault on write\n"); - break; - default: - kprintf("Unknown paging error occured on address %x\n", fault_addr); - panic("Paging error"); - break; - } -} diff --git a/arch/i386/kernel/pic.c b/arch/i386/kernel/pic.c deleted file mode 100644 index 0d29473..0000000 --- a/arch/i386/kernel/pic.c +++ /dev/null @@ -1,79 +0,0 @@ -#include - -static void (*irq_handlers[16])(struct isr_frame *frame); - -void pic_eoi(uint8_t irq) { - if (irq >= 8) - outb(PIC2_COMMAND, 0x20); - outb(PIC1_COMMAND, 0x20); -} - -void pic_remap(void) { - unsigned char a1 = inb(PIC1_DATA); - unsigned char a2 = inb(PIC2_DATA); - - outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); - outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); - outb(PIC1_DATA, PIC1); - outb(PIC2_DATA, PIC2); - outb(PIC1_DATA, 4); - outb(PIC2_DATA, 2); - outb(PIC1_DATA, ICW4_8086); - outb(PIC2_DATA, ICW4_8086); - outb(PIC1_DATA, a1); - outb(PIC2_DATA, a2); -} - -static uint16_t _pic_get_irq_reg(int ocw3) { - outb(PIC1_COMMAND, ocw3); - outb(PIC2_COMMAND, ocw3); - return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND); -} - -uint16_t pic_get_irr(void) { - return _pic_get_irq_reg(PIC_READ_IRR); -} - -uint16_t pic_get_isr(void) { - return _pic_get_irq_reg(PIC_READ_ISR); -} - -void register_irq_handler(uint8_t irq, void (*handler)(struct isr_frame *frame)) { - irq_handlers[irq] = handler; -} - -void irq_dispatch(struct isr_frame *frame) { - (*irq_handlers[frame->isr_vector-32])(frame); - pic_eoi(frame->isr_vector-32); - return; -} - -void irq_set_mask(uint8_t irq) { - uint16_t port; - uint8_t data; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - - data = inb(port) | (1 << irq); - outb(port, data); -} - -void irq_clear_mask(uint8_t irq) { - uint16_t port; - uint8_t data; - - if (irq < 8) { - port = PIC1_DATA; - } else { - port = PIC2_DATA; - irq -= 8; - } - - data = inb(port) & ~(1 << irq); - outb(port, data); -} diff --git a/arch/i386/kernel/pmem.c b/arch/i386/kernel/pmem.c deleted file mode 100644 index 40e3400..0000000 --- a/arch/i386/kernel/pmem.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -static struct pfa_page freelist; - -void pfa_init(struct mboot_info *header) { - struct mboot_mmap_entry *mme; - for (uintptr_t i = 0; i < header->mmap_length; i += sizeof(struct mboot_mmap_entry)) { - mme = (struct mboot_mmap_entry*)(header->mmap_addr + i); - if (mme->type == MBOOT_MEM_AVAILABLE) - pfa_free(mme->addr_low, mme->len_low / PAGE_SIZE); - } -} - -uintptr_t pfa_alloc(size_t num_pages) { - struct pfa_page *temp = (struct pfa_page*)PAGE_TMP_MAP; - map_page(freelist.next, PAGE_TMP_MAP, PD_PRES | PD_RW); - uintptr_t ret = freelist.next; - freelist.next = temp->next; - memset(temp, 0, 32); - unmap_page(PAGE_TMP_MAP); - return ret; -} - -void pfa_free(uintptr_t paddr, size_t num_pages) { - uintptr_t addr; - struct pfa_page *temp = (struct pfa_page*)PAGE_TMP_MAP; - for (size_t i = 0; i < num_pages; i++) { - addr = (i * PAGE_SIZE + paddr); - if (addr >= KSTART && addr < KEND) - continue; - if (addr == 0) - continue; - map_page(addr, PAGE_TMP_MAP, PD_PRES | PD_RW); - memset(PAGE_TMP_MAP, 1, 32); - temp->next = freelist.next; - freelist.next = addr; - unmap_page(PAGE_TMP_MAP); - } -} diff --git a/arch/i386/kernel/serial.c b/arch/i386/kernel/serial.c deleted file mode 100644 index dc2a8bd..0000000 --- a/arch/i386/kernel/serial.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include - -#define PORT 0x3f8 - -static inline int _serial_received(void) { - return inb(PORT + 5) & 1; -} - -static inline int _is_transmit_empty(void) { - return inb(PORT + 5) & 0x20; -} - -int serial_init(void) { - outb(PORT + 1, 0x00); - outb(PORT + 3, 0x80); - outb(PORT + 0, 0x03); - outb(PORT + 1, 0x00); - outb(PORT + 3, 0x03); - outb(PORT + 2, 0xC7); - outb(PORT + 4, 0x0B); - outb(PORT + 4, 0x1E); - outb(PORT + 0, 0xAE); - - if (inb(PORT + 0) != 0xAE) - return -1; - - outb(PORT + 4, 0x0F); - return 0; -} - -void serial_putchar(char c) { - while (_is_transmit_empty() == 0); - outb(PORT, c); -} - -char serial_getchar(void) { - while (_serial_received() == 0); - return inb(PORT); -} diff --git a/arch/i386/kernel/spinlock.s b/arch/i386/kernel/spinlock.s deleted file mode 100644 index c8235ca..0000000 --- a/arch/i386/kernel/spinlock.s +++ /dev/null @@ -1,30 +0,0 @@ -.section .text - -.global aquire_lock -.type aquire_lock, @function -aquire_lock: - pushl %ebp - movl %esp, %ebp -locked: - movl 8(%esp), %eax - lock incl (%eax) - jc spin_wait - - popl %ebp - ret -spin_wait: - test %eax, 1 - jnz spin_wait - jmp locked - -.global release_lock -.type release_lock, @function -release_lock: - pushl %ebp - movl %esp, %ebp - - movl 8(%ebp), %eax - lock decl (%eax) - - popl %ebp - ret diff --git a/arch/i386/kernel/stack_trace.s b/arch/i386/kernel/stack_trace.s deleted file mode 100644 index fbd77ed..0000000 --- a/arch/i386/kernel/stack_trace.s +++ /dev/null @@ -1,31 +0,0 @@ -.section .text - -.global walk_stack -.type walk_stack, @function -walk_stack: - pushl %ebp - movl %esp, %ebp - - pushl %edi - movl -4(%ebp), %edi - pushl %ebx - movl -8(%ebp), %ebx - - xorl %eax, %eax - movl 8(%esp), %ebx - movl 16(%esp), %edi - movl 20(%esp), %ecx -walk: - testl %ebx, %ebx - jz done - movl 4(%ebx), %edx - movl 0(%ebx), %ebx - movl %edx, (%edi) - addl $4, %edi - inc %eax - loop walk -done: - popl %ebx - popl %edi - popl %ebp - ret diff --git a/arch/i386/kernel/switch_thread.s b/arch/i386/kernel/switch_thread.s deleted file mode 100644 index f1168d5..0000000 --- a/arch/i386/kernel/switch_thread.s +++ /dev/null @@ -1,28 +0,0 @@ -.global switch_thread -.type switch_thread, @function -switch_thread: - pushl %ebp - movl %esp, %ebp - - pushl %ebx - pushl %esi - pushl %edi - pushl %ebp - - movl 8(%ebp), %esi - movl 12(%ebp), %edi - - movl %esp, (%edi); - movl %cr3, %ecx - movl %ecx, 4(%edi) - - movl (%esi), %esp - movl 4(%esi), %ecx - movl %ecx, %cr3 - - popl %ebp - popl %edi - popl %esi - popl %ebx - popl %ebp - ret diff --git a/arch/i386/kernel/syscall.c b/arch/i386/kernel/syscall.c deleted file mode 100644 index 200af4f..0000000 --- a/arch/i386/kernel/syscall.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include -#include -#include - -void sys_read(struct isr_frame *frame) { -} - -void sys_write(struct isr_frame *frame) { -} - -void handle_syscall(struct isr_frame *frame) { - switch (frame->eax) { - case SYS_READ: - sys_read(frame); - break; - case SYS_WRITE: - sys_write(frame); - break; - default: - panic("Invalid system call number"); - } - return 0; -} diff --git a/arch/i386/kernel/timer.c b/arch/i386/kernel/timer.c deleted file mode 100644 index a7d88dd..0000000 --- a/arch/i386/kernel/timer.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include - -static uint32_t num_ticks = 0; - -void timer_handler(struct isr_frame *frame) { - num_ticks++; - if (num_ticks == 3) { - num_ticks = 0; - schedule_next(); - } -} - -void timer_init(void) { - disable_ints(); - int divisor = 1193182 / 100; - outb(0x43, 0x34); - outb(0x40, divisor && 0xFF); - outb(0x40, divisor && 0xFF00 >> 8); - enable_ints(); - - register_irq_handler(0, timer_handler); -} -- cgit v1.2.3