# HG changeset patch # User windel # Date 1326743225 -3600 # Node ID 91f91ff07ea832490e1135e6919637c4ca2ae1a7 # Parent bcb3b68c81471b1c25ac9c43ee80ddfe68199295 Removed test variables diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/Makefile --- a/cos/kernel/Makefile Mon Jan 16 17:38:00 2012 +0100 +++ b/cos/kernel/Makefile Mon Jan 16 20:47:05 2012 +0100 @@ -10,8 +10,8 @@ OBJECTS = video.o snprintf.o kernel.o asmcode.o handlers.o keyboard.o \ klib.o malloc.o task.o mm.o timer.o fs.o initrd.o -lcfos.bin: $(CRT0) $(OBJECTS) link.ld - ld -g -T link.ld -s --cref -Map=kernel.map -o lcfos.bin $(CRT0) $(OBJECTS) +lcfos.bin: $(CRT0) $(OBJECTS) kernel.ld + ld -g -T kernel.ld -s --cref -Map=kernel.map -o lcfos.bin $(CRT0) $(OBJECTS) %.o : %.asm nasm -f elf64 -o $@ $< diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/asmcode.asm --- a/cos/kernel/asmcode.asm Mon Jan 16 17:38:00 2012 +0100 +++ b/cos/kernel/asmcode.asm Mon Jan 16 20:47:05 2012 +0100 @@ -40,24 +40,25 @@ %macro ISR_ERRCODE 1 global INT%1 INT%1: - cli + cli ; disable interrupts push strict qword %1 ; push interrupt number jmp isr_common_stub %endmacro isr_common_stub: ; Do some saving: - push rax + push rax ; regs[6] push rcx push rdx push rbx push rbp push rsi - push rdi + push rdi ; regs[0] ; AMD64 calling convention, first parameter is in rdi: mov rdi, rsp ; Load stack pointer into rdi to indicate where the registers on the stack are (so that we can change them!) + ; TODO: load kernel interrupt stack pointer extern isr_handler call isr_handler @@ -70,7 +71,7 @@ pop rax add rsp, 16 ; cleanup error code and isr number - sti + sti ; enable interrupts again iretq ; Exception handlers: @@ -94,6 +95,7 @@ ISR_ERRCODE 17 ISR_NOERRCODE 18 ISR_NOERRCODE 19 +; 20 - 31 are reserved ; irq handlers: ISR_NOERRCODE 32 diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/handlers.c --- a/cos/kernel/handlers.c Mon Jan 16 17:38:00 2012 +0100 +++ b/cos/kernel/handlers.c Mon Jan 16 20:47:05 2012 +0100 @@ -45,24 +45,24 @@ void setIDTentry(int num, void (*handler)(), uint16_t selector, uint8_t flags) { - // Fill one entry with IDT info: - uint64_t offset; + // Fill one entry with IDT info: + uint64_t offset; - // Typecast the function pointer to a number: - offset = (uint64_t)handler; + // Typecast the function pointer to a number: + offset = (uint64_t)handler; - // Set offset: - idt[num].baseLow = offset & 0xFFFF; - idt[num].baseMid = (offset >> 16) & 0xFFFF; - idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF; + // Set offset: + idt[num].baseLow = offset & 0xFFFF; + idt[num].baseMid = (offset >> 16) & 0xFFFF; + idt[num].baseHigh = (offset >> 32) & 0xFFFFFFFF; - // Set flags and selector: - idt[num].selector = selector; - idt[num].flags = flags; + // Set flags and selector: + idt[num].selector = selector; + idt[num].flags = flags; - // Set reserved fields: - idt[num].reserved1 = 0; - idt[num].reserved2 = 0; + // Set reserved fields: + idt[num].reserved1 = 0; + idt[num].reserved2 = 0; } void setupIDT(void) { @@ -149,11 +149,27 @@ outb(0xA1, maskslave); } +/* Function that prints the contents of all registers + on the interrupted stack. */ +void display_registers(uint64_t *regs) +{ + // Pushed by interrupt handler assembler: + printf("rax: %x\n", regs[6]); + printf("rbx: %x\n", regs[3]); + printf("rcx: %x\n", regs[5]); + printf("rdx: %x\n", regs[4]); + printf("rsi: %x\n", regs[1]); + printf("rdi: %x\n", regs[0]); + printf("rbp: %x\n", regs[2]); + // Pushed by CPU: + printf("rip: %x\n", regs[9]); +} + void gp_fault(uint64_t *regs) { printf("GP fault\n"); printf("Error code: %x\n", regs[8]); - printf("RIP: %x\n", regs[9]); + display_registers(regs); panic("No resolution to general protection fault!"); } @@ -163,7 +179,7 @@ uint64_t faulting_address; asm volatile("mov %%cr2, %0" : "=r" (faulting_address)); printf("Error code: %x\n", regs[8]); - printf("RIP: %x\n", regs[9]); + display_registers(regs); printf("Faulting address: %x\n", faulting_address); panic("No resolution to page fault!"); diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/kernel.c --- a/cos/kernel/kernel.c Mon Jan 16 17:38:00 2012 +0100 +++ b/cos/kernel/kernel.c Mon Jan 16 20:47:05 2012 +0100 @@ -14,9 +14,6 @@ kfree(a); } -uint64_t testvar = 1234; // Ends up in data section -uint64_t testvar2 = 0; // Ends up in bss section - multiboot_info_t *multiboot_info = 0; // Set by startup code. /* This routine initializes the kernel. @@ -27,16 +24,10 @@ // No kmalloc required here yet: init_screen(); setupIDT(); - keyboard_init(); - timer_init(); - - testvar++; - testvar2++; - printf("Test variable = %d, testvar2 = %d\n", testvar, testvar2); /* Retrieve memory information from multiboot header */ uint64_t available_memory = 0; - if ((multiboot_info->flags & (1<<6)) == (1<<6)) + if ((multiboot_info->flags & (1 << 6)) == (1 << 6)) { multiboot_memory_map_t *mmap; for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr; @@ -54,13 +45,19 @@ } } } - else + + if (available_memory == 0) { - panic("Found no GRUB memory map\n"); + panic("Found no usable memory in grub's memory map\n"); } printf("Running with %d MB ram\n", available_memory / 1000000); + init_memory(available_memory); // Setup paging and memory manager + + keyboard_init(); + timer_init(); + /* fs_node_t *fs_root = 0; if ( (multiboot_info->flags & (1<<3)) == (1<<3)) @@ -92,9 +89,6 @@ } */ - // Assume first 16MB: - // TODO: get size from grub - init_memory(0x1000000); // TODO: make below a user space program! printf("Welcome!\n"); @@ -137,7 +131,6 @@ x = (uint64_t*)0x4000000; // Address that is not mapped *x = 0x2; // trigger paging exception } - } } diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/kernel.ld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cos/kernel/kernel.ld Mon Jan 16 20:47:05 2012 +0100 @@ -0,0 +1,27 @@ +OUTPUT_FORMAT("binary") + +SECTIONS { + .text 0x100000: + { + *(.text) + } + + .rodata ALIGN (4096) : + { + *(.rodata) + } + + .data ALIGN (4096) : + { + *(.data) + } + + load_end_address = .; + .bss : { + *(.bss) + } + + bss_end_address = .; + kernel_end = .; +} + diff -r bcb3b68c8147 -r 91f91ff07ea8 cos/kernel/klib.c --- a/cos/kernel/klib.c Mon Jan 16 17:38:00 2012 +0100 +++ b/cos/kernel/klib.c Mon Jan 16 20:47:05 2012 +0100 @@ -16,6 +16,7 @@ void reboot() { + // Use the keyboard to reboot: while ( (inb(0x64) & 0x02) == 0x02) { ; @@ -25,8 +26,8 @@ void halt() { - asm volatile("cli"); - asm volatile("hlt"); + asm volatile("cli"); // Disable interrupts. + asm volatile("hlt"); // Halt processor } /* IO port helpers: */ @@ -52,12 +53,12 @@ /* string functions: */ int strncmp(const char* s1, const char* s2, int size) { - int i; - for (i=0; i