diff --git a/kernel/elf.c b/kernel/elf.c index 2b688ab..009a4d6 100644 --- a/kernel/elf.c +++ b/kernel/elf.c @@ -5,11 +5,11 @@ section_t *elf_find_section(elf_t *elf, const char *name) { section_t *str = (section_t *)((uint32_t)elf + elf->e_shoff + elf->e_shstrndx*elf->e_shentsize); section_t *found = 0; for (int i = 0; i < elf->e_shnum; i++) { - section_t *sec = (section_t *)((uint32_t)elf + elf->e_shoff + i*elf->e_shentsize); - const char *n = (char *)((uint32_t)elf + str->sh_offset + sec->sh_name); - if (!strcmp(n, name)) { - found = sec; - } + section_t *sec = (section_t *)((uint32_t)elf + elf->e_shoff + i*elf->e_shentsize); + const char *n = (char *)((uint32_t)elf + str->sh_offset + sec->sh_name); + if (!strcmp(n, name)) { + found = sec; + } } return found; diff --git a/kernel/kernel.c b/kernel/kernel.c index 13ba14a..168c467 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -33,7 +33,7 @@ void syscall_write(stack_frame_t r) { uint32_t fd = r.edi; uint8_t *buf = (uint8_t *)r.esi; if (fd == FD_STDOUT) { - LOG("%s", buf); + LOG("%s", buf); } } @@ -42,14 +42,14 @@ void syscall_read(stack_frame_t r) { uint8_t *buf = (uint8_t *)r.esi; uint32_t len = r.edx; if (fd == FD_STDOUT) { - // Not yet + // Not yet } else if (fd == FD_STDIN) { - asm volatile ("sti" ::); - while (ctx.current_task->stdin[ctx.current_task->stdin_len - 1] != '\n') cpu_relax; - if (ctx.current_task->stdin_len > 0) ctx.current_task->stdin[--ctx.current_task->stdin_len] = 0; - memcpy(buf, ctx.current_task->stdin, len); - for (int i = 0; i < 0x1000; i++) ctx.current_task->stdin[i] = 0; - ctx.current_task->stdin_len = 0; + asm volatile ("sti" ::); + while (ctx.current_task->stdin[ctx.current_task->stdin_len - 1] != '\n') cpu_relax; + if (ctx.current_task->stdin_len > 0) ctx.current_task->stdin[--ctx.current_task->stdin_len] = 0; + memcpy(buf, ctx.current_task->stdin, len); + for (int i = 0; i < 0x1000; i++) ctx.current_task->stdin[i] = 0; + ctx.current_task->stdin_len = 0; } } @@ -69,7 +69,7 @@ void exception_handler(int_stack_frame_t r) { void sleep(int delay) { uint64_t end = ctx.ticks + delay; while (ctx.ticks < end) - cpu_relax; + cpu_relax; } void idt_init(void) { @@ -79,7 +79,7 @@ void idt_init(void) { idt_encode_entry(idt, 32, (uint32_t)pit_isr, GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, RING0), TRAP_GATE_32 | INT_RING0 | INT_PRESENT); idt_encode_entry(idt, 0x21, (uint32_t)ps2_isr, GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, RING0), TRAP_GATE_32 | INT_RING0 | INT_PRESENT); idt_encode_entry(idt, 0x80, (uint32_t)syscall_isr, GDT_SEGMENT_SELECTOR(GDT_USER_CODE, RING3), TRAP_GATE_32 | INT_RING3 | INT_PRESENT); - + idtr.size = (sizeof(struct idt_entry) * 256) - 1; idtr.base = (uint32_t) idt; @@ -127,29 +127,29 @@ void kernel(multiboot_info_t *info) { flush_tss(GDT_SEGMENT_SELECTOR(5, RING3)); /* - task_t task1 = {0}; - task_t task2 = {0}; - task1.cr3 = (uint32_t)boot_page_dir - KERNEL_VMA; - task1.next = (uint32_t *)&task2; - ctx.current_task = &task1; + task_t task1 = {0}; + task_t task2 = {0}; + task1.cr3 = (uint32_t)boot_page_dir - KERNEL_VMA; + task1.next = (uint32_t *)&task2; + ctx.current_task = &task1; - uint32_t *stack = mmap_find_first_free_block(); - asm volatile ("mov %%esp, %0" : "=r" (task1.esp)); - stack[1023] = 0x207; - stack[1022] = 0x8; - stack[1021] = (uint32_t)test; - stack[1020] = 0xDEADBEEF; - stack[1019] = 0xDEADBEEF; - stack[1018] = 0xDEADBEEF; - stack[1017] = 0xDEADBEEF; - stack[1016] = 0xDEADBEEF; - stack[1015] = 0xDEADBEEF; - stack[1014] = 0xDEADBEEF; - stack[1013] = 0xDEADBEEF; - task2.esp = (uint32_t)stack + (0x1000 - 44); - task2.cr3 = (uint32_t)boot_page_dir - KERNEL_VMA; - task2.next = 0; - */ + uint32_t *stack = mmap_find_first_free_block(); + asm volatile ("mov %%esp, %0" : "=r" (task1.esp)); + stack[1023] = 0x207; + stack[1022] = 0x8; + stack[1021] = (uint32_t)test; + stack[1020] = 0xDEADBEEF; + stack[1019] = 0xDEADBEEF; + stack[1018] = 0xDEADBEEF; + stack[1017] = 0xDEADBEEF; + stack[1016] = 0xDEADBEEF; + stack[1015] = 0xDEADBEEF; + stack[1014] = 0xDEADBEEF; + stack[1013] = 0xDEADBEEF; + task2.esp = (uint32_t)stack + (0x1000 - 44); + task2.cr3 = (uint32_t)boot_page_dir - KERNEL_VMA; + task2.next = 0; + */ multi_mod_t *init = (multi_mod_t *)(info->moduleaddress + KERNEL_VMA); elf_t *elf = (elf_t *)(init->mod_start+KERNEL_VMA); @@ -161,35 +161,35 @@ void kernel(multiboot_info_t *info) { // Set the ring0 CS asm volatile ( - "movl $0x174, %%ecx \n\t" - "movl %0, %%eax \n\t" - "xor %%edx, %%edx \n\t" - "wrmsr" - : - : "r"(GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, RING0)) - : "eax", "ecx", "edx" - ); + "movl $0x174, %%ecx \n\t" + "movl %0, %%eax \n\t" + "xor %%edx, %%edx \n\t" + "wrmsr" + : + : "r"(GDT_SEGMENT_SELECTOR(GDT_KERNEL_CODE, RING0)) + : "eax", "ecx", "edx" + ); // Set the ring0 stack uint32_t *k_stack = mmap_find_first_free_block(); asm volatile ( - "movl $0x175, %%ecx \n\t" - "movl %0, %%eax \n\t" - "xor %%edx, %%edx \n\t" - "wrmsr" - : - : "r"((uint32_t)k_stack + 0x1000) - : "eax", "ecx", "edx" - ); + "movl $0x175, %%ecx \n\t" + "movl %0, %%eax \n\t" + "xor %%edx, %%edx \n\t" + "wrmsr" + : + : "r"((uint32_t)k_stack + 0x1000) + : "eax", "ecx", "edx" + ); // Set the ring0 syscall entry asm volatile ( - "movl $0x176, %%ecx \n\t" - "movl %0, %%eax \n\t" - "xor %%edx, %%edx \n\t" - "wrmsr" - : - : "r"(syscall) - : "eax", "ecx", "edx" - ); + "movl $0x176, %%ecx \n\t" + "movl %0, %%eax \n\t" + "xor %%edx, %%edx \n\t" + "wrmsr" + : + : "r"(syscall) + : "eax", "ecx", "edx" + ); LOG("Switching to ring 3... EIP: 0x%08X ESP: 0x%08X\n", task.eip, task.esp); diff --git a/kernel/log.c b/kernel/log.c index 54e72e5..a3ee681 100644 --- a/kernel/log.c +++ b/kernel/log.c @@ -6,20 +6,20 @@ extern kernel_ctx_t ctx; void _putchar(char c) { switch (ctx.log_method) { - case LOG_COM1: - serial_putc(COM1, c); - break; - default: - break; + case LOG_COM1: + serial_putc(COM1, c); + break; + default: + break; } } void logs(const char *str) { switch (ctx.log_method) { - case LOG_COM1: - serial_puts(COM1, str); - break; - default: - break; + case LOG_COM1: + serial_puts(COM1, str); + break; + default: + break; } } diff --git a/kernel/mem.c b/kernel/mem.c index f5d61b5..8794fd2 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -13,13 +13,13 @@ void *mmap_block_to_physical(int block) { uint32_t len = (uint32_t)(entry.len); uint32_t addr = (uint32_t)(entry.addr); - if (len > offset) { - uint32_t phys = (addr + offset); - if (phys < 0x00400000) { - return (void *)(phys + 0xC0000000); - } - return (void *)phys; - } + if (len > offset) { + uint32_t phys = (addr + offset); + if (phys < 0x00400000) { + return (void *)(phys + 0xC0000000); + } + return (void *)phys; + } } return 0; @@ -73,9 +73,9 @@ uint8_t mmap_init(void) { struct multiboot_mmap_entry entry = ctx.multi_mmap[index]; //uint32_t addr = (uint32_t)(entry.addr_low | entry.addr_high); if ((uint32_t)(entry.addr & UINT32_MAX) < 0x00400000) - ctx.mmap = (uint32_t *)((uint32_t)(entry.addr & UINT32_MAX) + 0xC0000000); + ctx.mmap = (uint32_t *)((uint32_t)(entry.addr & UINT32_MAX) + 0xC0000000); else - ctx.mmap = (uint32_t *)(uint32_t)(entry.addr & 0xffffffff); + ctx.mmap = (uint32_t *)(uint32_t)(entry.addr & 0xffffffff); // Zero the map memset(ctx.mmap, 0, ctx.mmap_size); // Reserve the blocks that hold the memory map + 1 @@ -131,6 +131,6 @@ void *mmap_find_first_free_block(void) { return mmap_block_to_physical(i); } } - + return 0; } diff --git a/kernel/ps2.c b/kernel/ps2.c index c9e9dec..8bcb2ac 100644 --- a/kernel/ps2.c +++ b/kernel/ps2.c @@ -33,15 +33,15 @@ void ps2_init() { if (result == 0x55) LOG("Passed PS/2 self test\n") else LOG("Failed!\n") - // perform test - outb(PS2_8042_COMMAND, 0xAB); + // perform test + outb(PS2_8042_COMMAND, 0xAB); while (!(inb(PS2_8042_STATUS) & 0x1)) cpu_relax; result = inb(PS2_8042_DATA); if (!result) LOG("Passed PS/2 port 1 test\n") else LOG("Failed!\n") - // enable ps/2 - outb(PS2_8042_COMMAND, 0xAE); + // enable ps/2 + outb(PS2_8042_COMMAND, 0xAE); // enable IRQ 1 config = 0b01100101; @@ -100,10 +100,10 @@ void ps2_handler() { push_stdin('='); break; case KBD_BACKSPACE: - /* - if (shell_index > 0) { - //popchar(); - shell_buffer[--shell_index] = 0; + /* + if (shell_index > 0) { + //popchar(); + shell_buffer[--shell_index] = 0; }*/ break; case KBD_TAB: @@ -146,25 +146,25 @@ void ps2_handler() { break; case KBD_ENTER: /* - if (strcmp(shell_buffer, "PINGAS") == 0) { - printf("andew pingas detected"); - } else if (strcmp(shell_buffer, "HELLO") == 0) { - printf("hi"); - } else if (strcmp(shell_buffer, "READ") == 0) { - uint8_t sector[512] = {0}; - ata_read_sector(ATA_PRIMARY, ATA_MASTER, 0, sector); - for (int i = 0; i < 512; i++) { - push_stdin(sector[i]); - } - } else { - printf("unknown command '%s' idiot", shell_buffer); - } - for (int i = 0; i < 512; i++) { - shell_buffer[i] = 0; - } - shell_index = 0; - */ - push_stdin('\n'); + if (strcmp(shell_buffer, "PINGAS") == 0) { + printf("andew pingas detected"); + } else if (strcmp(shell_buffer, "HELLO") == 0) { + printf("hi"); + } else if (strcmp(shell_buffer, "READ") == 0) { + uint8_t sector[512] = {0}; + ata_read_sector(ATA_PRIMARY, ATA_MASTER, 0, sector); + for (int i = 0; i < 512; i++) { + push_stdin(sector[i]); + } + } else { + printf("unknown command '%s' idiot", shell_buffer); + } + for (int i = 0; i < 512; i++) { + shell_buffer[i] = 0; + } + shell_index = 0; + */ + push_stdin('\n'); break; case KBD_LCTRL: // Handle left control key diff --git a/kernel/serial.c b/kernel/serial.c index 2b50aef..0920790 100644 --- a/kernel/serial.c +++ b/kernel/serial.c @@ -7,7 +7,7 @@ uint8_t serial_port_init(int port) { uint8_t old_scratch = inb(port + 7); outb(port + 7, old_scratch + 1); if (inb(port + 7) != old_scratch + 1) - return 0; + return 0; outb(port + 7, old_scratch); outb(port + 1, 0x00); // Disable all interrupts @@ -22,7 +22,7 @@ uint8_t serial_port_init(int port) { // Check if serial is faulty (i.e: not same byte as sent) if(inb(port + 0) != 0xAE) - return 0; + return 0; // If serial is not faulty set it in normal operation mode // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) @@ -38,7 +38,7 @@ void serial_putc(int port, const char c) { void serial_puts(int port, const char *str) { while (*str) { - serial_putc(port, str[0]); - str++; + serial_putc(port, str[0]); + str++; } } diff --git a/kernel/task.c b/kernel/task.c index b82f2b9..7a5f83b 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -7,20 +7,20 @@ extern kernel_ctx_t ctx; //int task_init(task_t *task, uint32_t *img) { int task_init(task_t *task, elf_t *elf) { - + if (!elf) return 0; uint8_t *s = (uint8_t *)mmap_find_first_free_block(); /* - memset(s, 0, 0x1000); - int idx = 0; + memset(s, 0, 0x1000); + int idx = 0; // Loop section header to find ALLOC sections then allocate them for (int i = 0; i < elf->e_shnum; i++) { - section_t *sec = (section_t *)((uint32_t)elf + elf->e_shoff + i*elf->e_shentsize); - if (!(sec->sh_flags & 0x2)) continue; - if (!sec->sh_addr) continue; - idx += sec->sh_offset; - memcpy(s + idx, (void *)((uint32_t)elf + sec->sh_offset), sec->sh_offset); - LOG("%d\n", sec->sh_offset); + section_t *sec = (section_t *)((uint32_t)elf + elf->e_shoff + i*elf->e_shentsize); + if (!(sec->sh_flags & 0x2)) continue; + if (!sec->sh_addr) continue; + idx += sec->sh_offset; + memcpy(s + idx, (void *)((uint32_t)elf + sec->sh_offset), sec->sh_offset); + LOG("%d\n", sec->sh_offset); } */ memcpy(s, elf, 0x1000); @@ -39,12 +39,12 @@ int task_init(task_t *task, elf_t *elf) { task->cr3 = (uint32_t)page_dir - KERNEL_VMA; uint32_t *page_tab = mmap_find_first_free_block(); for (int i = 0; i < 1024; i++) { - // Map kernel to 3GiB - if (i == 768) page_dir[i] = ((uint32_t)boot_page_tab - KERNEL_VMA) | 7; - // Map 3.4GiB-4GiB as non-present kernel entries - else if (i > 768) page_dir[i] = 4; - // Map remaining entries as non-present u/s - else page_dir[i] = 6; + // Map kernel to 3GiB + if (i == 768) page_dir[i] = ((uint32_t)boot_page_tab - KERNEL_VMA) | 7; + // Map 3.4GiB-4GiB as non-present kernel entries + else if (i > 768) page_dir[i] = 4; + // Map remaining entries as non-present u/s + else page_dir[i] = 6; } // Map the ELF image to its specified entry // This will cause problems if the ELF image is larger than 4 MiB so I need to fix this @@ -52,11 +52,11 @@ int task_init(task_t *task, elf_t *elf) { page_dir[PAGE_DIR_INDEX(elf->e_entry)] = ((uint32_t)page_tab - KERNEL_VMA) | 7; /* - uint32_t elf_phys = (uint32_t)s - KERNEL_VMA; - for (int i = 0; i < 1024; i++) { - page_tab[i] = (elf_phys + 0x1000*i) | 7; - } - */ + uint32_t elf_phys = (uint32_t)s - KERNEL_VMA; + for (int i = 0; i < 1024; i++) { + page_tab[i] = (elf_phys + 0x1000*i) | 7; + } + */ page_tab[PAGE_TAB_INDEX(elf->e_entry)] = ((uint32_t)s - KERNEL_VMA) | 7; return 1; @@ -69,7 +69,7 @@ void task_sort() { task_t* temp = new_head; while (temp->next) { - temp = (task_t *)temp->next; + temp = (task_t *)temp->next; } temp->next = (uint32_t *)ctx.current_task;