#include "alloc.h" #include "exception.h" #include "gdt.h" #include "idt.h" #include "interrupt.h" #include "io.h" #include "irq.h" #include "klibc.h" #include "kthread.h" #include "mem.h" #include "multiboot.h" #include "paging.h" #include "pit.h" #include "serial.h" #include "stack.h" #include "stdarg.h" #ifdef RUN_TEST #include "test.h" #endif #include "types.h" #include "vga.h" #define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit))) void cpuid(int code, uint32_t *a, uint32_t *d) { asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx"); } void idleThread(void *arg) { (void)arg; int count = 0; while (1) printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1); } // Multiboot information available here : // https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec // https://www.gnu.org/software/grub/manual/multiboot/html_node/Boot-information-format.html#Boot%20information%20format void kmain(unsigned long magic, unsigned long addr) { unsigned long upper_mem = 0; VGASetup(BLACK, GREEN); cursorEnable(14, 15); printf("Setting up Interruptions\n"); gdtSetup(); idtSetup(); irqSetup(); pitSetup(100); if (magic == MULTIBOOT_BOOTLOADER_MAGIC) { // Get loaded by Grub with mutliboot version 1 multiboot_info_t *mbi = (multiboot_info_t *)addr; /* Are mem_* valid? */ if (CHECK_FLAG(mbi->flags, 0)) { printf("mem_lower = %dKiB mem_upper %dKiB\n", mbi->mem_lower, mbi->mem_upper); upper_mem = mbi->mem_upper; } /* Is boot_device valid? */ if (CHECK_FLAG(mbi->flags, 1)) { printf("boot_device = %d\n", mbi->boot_device); } /* Is the command line passed? */ if (CHECK_FLAG(mbi->flags, 2)) { printf("cmdline = %s\n", (char *)mbi->cmdline); } if (CHECK_FLAG(mbi->flags, 3)) { multiboot_module_t *mod; uint32_t i; printf("mods_count = %d, mods_addr = 0x%x\n", (int)mbi->mods_count, (int)mbi->mods_addr); for (i = 0, mod = (multiboot_module_t *)mbi->mods_addr; i < mbi->mods_count; i++, mod++) printf(" mod_start = 0x%x, mod_end = 0x%x, cmdline = %s\n", (unsigned)mod->mod_start, (unsigned)mod->mod_end, (char *)mod->cmdline); } if (CHECK_FLAG(mbi->flags, 6)) { struct multiboot_mmap_entry *mmap = (struct multiboot_mmap_entry *)mbi->mmap_addr; uint size = mbi->mmap_length / sizeof(struct multiboot_mmap_entry); pr_devel("mmap buffer at %d size %d %d\n", mbi->mmap_addr, mbi->mmap_length, sizeof(multiboot_memory_map_t)); for (uint i = 0; i < size; i++) { printf("base_addr 0x%x 0x%x, length = 0x%x 0x%x, type = 0x%x\n", (unsigned)(mmap[i].addr >> 32), (unsigned)(mmap[i].addr & 0xffffffff), (unsigned)(mmap[i].len >> 32), (unsigned)(mmap[i].len & 0xffffffff), (uint32_t)mmap[i].type); } } } if (upper_mem == 0) { printf("Cannot get upper phy mem bound. Using default value 32MB\n"); upper_mem = 32 * 1024; } printf("Setting up Pagination\n"); paddr_t lastUserByMem; memSetup(upper_mem, &lastUserByMem); #ifdef RUN_TEST testPhymem(); #endif pagingSetup(lastUserByMem); printf("Setting up IRQ handlers\n"); irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); printf("Enabling HW interrupts\n"); exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, ACCESS_INTERRUPT(EXCEPTION_DOUBLE_FAULT)); exceptionSetRoutine(EXCEPTION_DIVIDE_ZERO, ACCESS_INTERRUPT(EXCEPTION_DIVIDE_ZERO)); // Used by the DBG // exceptionSetRoutine(EXCEPTION_DEBUG, ACCESS_INTERRUPT(EXCEPTION_DEBUG)); exceptionSetRoutine(EXCEPTION_NMI, ACCESS_INTERRUPT(EXCEPTION_NMI)); exceptionSetRoutine(EXCEPTION_BREAKPOINT, ACCESS_INTERRUPT(EXCEPTION_BREAKPOINT)); exceptionSetRoutine(EXCEPTION_OVERFLOW, ACCESS_INTERRUPT(EXCEPTION_OVERFLOW)); exceptionSetRoutine(EXCEPTION_BOUND_RANGE_EXCEEDED, ACCESS_INTERRUPT(EXCEPTION_BOUND_RANGE_EXCEEDED)); exceptionSetRoutine(EXCEPTION_INVALID_OPCODE, ACCESS_INTERRUPT(EXCEPTION_INVALID_OPCODE)); exceptionSetRoutine(EXCEPTION_DEVICE_NOT_AVAILABLE, ACCESS_INTERRUPT(EXCEPTION_DEVICE_NOT_AVAILABLE)); exceptionSetRoutine(EXCEPTION_COPRO_OVERRUN, ACCESS_INTERRUPT(EXCEPTION_COPRO_OVERRUN)); exceptionSetRoutine(EXCEPTION_INVALID_TSS, ACCESS_INTERRUPT(EXCEPTION_INVALID_TSS)); exceptionSetRoutine(EXCEPTION_SEGMENT_NOT_PRESENT, ACCESS_INTERRUPT(EXCEPTION_SEGMENT_NOT_PRESENT)); exceptionSetRoutine(EXCEPTION_STACK_SEGMENT_FAULT, ACCESS_INTERRUPT(EXCEPTION_STACK_SEGMENT_FAULT)); exceptionSetRoutine(EXCEPTION_GENERAL_PROTECTION_FAULT, ACCESS_INTERRUPT(EXCEPTION_GENERAL_PROTECTION_FAULT)); exceptionSetRoutine(EXCEPTION_PAGE_FAULT, ACCESS_INTERRUPT(EXCEPTION_PAGE_FAULT)); exceptionSetRoutine(EXCEPTION_RESERVED_1, ACCESS_INTERRUPT(EXCEPTION_RESERVED_1)); exceptionSetRoutine(EXCEPTION_X87_FP_EXCEPTION, ACCESS_INTERRUPT(EXCEPTION_X87_FP_EXCEPTION)); exceptionSetRoutine(EXCEPTION_ALIGNMENT_CHECK, ACCESS_INTERRUPT(EXCEPTION_ALIGNMENT_CHECK)); exceptionSetRoutine(EXCEPTION_MACHINE_CHECK, ACCESS_INTERRUPT(EXCEPTION_MACHINE_CHECK)); exceptionSetRoutine(EXCEPTION_SIMD_FP, ACCESS_INTERRUPT(EXCEPTION_SIMD_FP)); exceptionSetRoutine(EXCEPTION_VIRTUALIZATION, ACCESS_INTERRUPT(EXCEPTION_VIRTUALIZATION)); exceptionSetRoutine(EXCEPTION_RESERVED_2, ACCESS_INTERRUPT(EXCEPTION_RESERVED_2)); exceptionSetRoutine(EXCEPTION_RESERVED_3, ACCESS_INTERRUPT(EXCEPTION_RESERVED_3)); exceptionSetRoutine(EXCEPTION_RESERVED_4, ACCESS_INTERRUPT(EXCEPTION_RESERVED_4)); exceptionSetRoutine(EXCEPTION_RESERVED_5, ACCESS_INTERRUPT(EXCEPTION_RESERVED_5)); exceptionSetRoutine(EXCEPTION_RESERVED_6, ACCESS_INTERRUPT(EXCEPTION_RESERVED_6)); exceptionSetRoutine(EXCEPTION_RESERVED_7, ACCESS_INTERRUPT(EXCEPTION_RESERVED_7)); exceptionSetRoutine(EXCEPTION_RESERVED_8, ACCESS_INTERRUPT(EXCEPTION_RESERVED_8)); exceptionSetRoutine(EXCEPTION_RESERVED_9, ACCESS_INTERRUPT(EXCEPTION_RESERVED_9)); exceptionSetRoutine(EXCEPTION_RESERVED_10, ACCESS_INTERRUPT(EXCEPTION_RESERVED_10)); exceptionSetRoutine(EXCEPTION_SECURITY, ACCESS_INTERRUPT(EXCEPTION_SECURITY)); exceptionSetRoutine(EXCEPTION_RESERVED_11, ACCESS_INTERRUPT(EXCEPTION_RESERVED_11)); exceptionSetRoutine(EXCEPTION_PAGE_FAULT, pagefault_handler); // Enabling the HW interrupts asm volatile("sti\n"); printf("Setting up Serial link (115200)\n"); serialSetup(115200); allocInit(); kthreadSetup(_stack_bottom, (_stack_top - _stack_bottom + 1)); kthreadCreate("idle", idleThread, NULL); irqSetRoutine(IRQ_TIMER, pit_handler); #ifdef RUN_TEST run_test(); #endif printf("\nSystem init done\n"); // There is no real caller behind this point // So finish this by ourself kthreadExit(); }