changeset 36:91f91ff07ea8

Removed test variables
author windel
date Mon, 16 Jan 2012 20:47:05 +0100
parents bcb3b68c8147
children 5c20bd53cccd
files cos/kernel/Makefile cos/kernel/asmcode.asm cos/kernel/handlers.c cos/kernel/kernel.c cos/kernel/kernel.ld cos/kernel/klib.c cos/kernel/link.ld cos/kernel/task.c
diffstat 8 files changed, 85 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- 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 $@ $<
--- 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
--- 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!");
 
--- 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
       }
-
    }
 }
 
--- /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 = .;
+}
+
--- 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<size; i++) 
-  {
-    if (s1[i] != s2[i]) return 0;
-  }
-  return 1;
+   int i;
+   for (i=0; i<size; i++) 
+   {
+      if (s1[i] != s2[i]) return 0;
+   }
+   return 1;
 }
 
 void memset(void* data, uint8_t value, uint64_t size)
--- a/cos/kernel/link.ld	Mon Jan 16 17:38:00 2012 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-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 = .;
-}
-
--- a/cos/kernel/task.c	Mon Jan 16 17:38:00 2012 +0100
+++ b/cos/kernel/task.c	Mon Jan 16 20:47:05 2012 +0100
@@ -71,8 +71,6 @@
 */
 void task_scheduler()
 {
-   return;
-
    if (current_task == 0)
    {
       return;