changeset 14:a58904747019

Added asm interrupt handler things, not yet working
author windel
date Mon, 14 Nov 2011 22:45:55 +0100
parents d07d4701a103
children ddefe6d97cd7
files cos/Makefile cos/grub/menu.lst cos/images/first64bits.img cos/kernel/asmcode.asm cos/kernel/handlers.c cos/kernel/kernel.c cos/kernel/kernel.h
diffstat 7 files changed, 388 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cos/Makefile	Mon Nov 14 21:44:35 2011 +0100
+++ b/cos/Makefile	Mon Nov 14 22:45:55 2011 +0100
@@ -22,7 +22,10 @@
 OBJECTS = \
 	kernel/video.o \
 	kernel/snprintf.o \
-	kernel/kernel.o
+	kernel/kernel.o \
+	kernel/asmcode.o \
+	kernel/handlers.o
+
 
 lcfosc.elf: $(CRT0) $(OBJECTS) linker.ld
 	ld -T linker.ld -o lcfosc.elf $(CRT0) $(OBJECTS)
--- a/cos/grub/menu.lst	Mon Nov 14 21:44:35 2011 +0100
+++ b/cos/grub/menu.lst	Mon Nov 14 22:45:55 2011 +0100
@@ -1,5 +1,5 @@
 default 0
-timeout 10
+timeout 3
 title lcfosc
 root (fd0)
 kernel /lcfosc.bin
Binary file cos/images/first64bits.img has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos/kernel/asmcode.asm	Mon Nov 14 22:45:55 2011 +0100
@@ -0,0 +1,76 @@
+; The default interrupt handlers.
+; from 20 - 31 are reserved vectors.
+; below are the custom ones!
+
+section .text
+align 4
+
+; Port helpers:
+global outb
+outb:
+  mov eax, [esp + 8]
+  mov edx, [esp + 4]
+  out dx, al
+  ret
+
+global inb
+inb:
+  xor eax, eax
+  mov edx, [esp + 4]
+  in al, dx
+  ret
+
+; Helper functions:
+global enableinterrupts
+enableinterrupts:
+  sti
+  ret
+
+global halt
+halt:
+  cli
+  hlt
+
+global loadIDT
+loadIDT:
+  ret
+
+; Define macro with two arguments:
+%macro INTX 2
+global %1
+%1:
+ ; Do some saving:
+extern %2
+     call %2
+ ; Do restoration
+     iret
+
+%endmacro
+
+; Exception handlers:
+INTX INTDEF, INTDEF_handler
+INTX INT0, INT0handler
+INTX INT1, INT1handler
+INTX INT2, INT2handler
+INTX INT3, INT3handler
+INTX INT4, INT4handler
+INTX INT5, INT5handler
+INTX INT6, INT6handler
+INTX INT7, INT7handler
+INTX INT8, INT8handler
+INTX INT9, INT9handler
+INTX INT10, INT10handler
+INTX INT11, INT11handler
+INTX INT12, INT12handler
+INTX INT13, INT13handler
+INTX INT14, INT14handler
+INTX INT15, INT15handler
+INTX INT16, INT16handler
+INTX INT17, INT17handler
+INTX INT18, INT18handler
+INTX INT19, INT19handler
+
+INTX INT32, INT32handler
+INTX INT33, INT33handler
+INTX INT34, INT34handler
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cos/kernel/handlers.c	Mon Nov 14 22:45:55 2011 +0100
@@ -0,0 +1,296 @@
+#include "kernel.h"
+
+void panic(char *msg) {
+  printf("Kernel panic: ");
+  printf(msg);
+  halt();
+}
+
+// Assembler wrapper prototypes:
+void INTDEF(void);
+void INT0(void);
+void INT1(void);
+void INT2(void);
+void INT3(void);
+void INT4(void);
+void INT5(void);
+void INT6(void);
+void INT7(void);
+void INT8(void);
+void INT9(void);
+void INT10(void);
+void INT11(void);
+void INT12(void);
+void INT13(void);
+void INT14(void);
+void INT15(void);
+void INT16(void);
+void INT17(void);
+void INT18(void);
+void INT19(void);
+// Remapped handlers:
+void INT32(void);
+void INT33(void);
+void INT34(void);
+
+// THE interrupt descriptor table:
+static struct IDT_entry idt[256];
+
+void setIDTentry(int num, void (*handler)())
+{
+  // Fill one entry with IDT info:
+  uint64_t offset;
+  unsigned short selector = 0x8; // Code selector
+  unsigned char type = 0x8E; // 32 bits interrupt gate. Thingy is present 
+
+  offset = (uint64_t) handler;
+
+  idt[num].b[0] = offset & 0xFF;
+  idt[num].b[1] = (offset >> 8) & 0xFF;
+  idt[num].b[2] = selector & 0xFF;
+  idt[num].b[3] = (selector >> 8) & 0xFF;
+  idt[num].b[4] = 0; // reserved
+  idt[num].b[5] = type;
+  idt[num].b[6] = (offset >> 16) & 0xFF;
+  idt[num].b[7] = (offset >> 24) & 0xFF;
+}
+
+void setupIDT(void) {
+  int i;
+  // Fill all vectors with the default handler:
+  for (i=0; i<256; i++) {
+    setIDTentry(i, &INTDEF);
+  }
+
+  // Now set other then default handler:
+  setIDTentry(0, INT0);
+  setIDTentry(1, INT1);
+  setIDTentry(2, INT2);
+  setIDTentry(3, INT3);
+  setIDTentry(4, INT4);
+  setIDTentry(5, INT5);
+  setIDTentry(6, INT6);
+  setIDTentry(7, INT7);
+  setIDTentry(8, INT8);
+  setIDTentry(9, INT9);
+  setIDTentry(10, INT10);
+  setIDTentry(11, INT11);
+  setIDTentry(12, INT12);
+  setIDTentry(13, INT13);
+  setIDTentry(14, INT14);
+  setIDTentry(15, INT15);
+  setIDTentry(16, INT16);
+  setIDTentry(17, INT17);
+  setIDTentry(18, INT18);
+  setIDTentry(19, INT19);
+  /* reserved interrupts: */
+  // From int20 - int31
+  setIDTentry(32, INT32);
+  setIDTentry(33, INT33);
+  setIDTentry(34, INT34);
+
+  // call load IDT asm function:
+  loadIDT(idt, 256*8-1);
+
+  PICremap();
+  enableinterrupts();
+}
+
+// PIC functions:
+void PICremap() {
+  unsigned char maskmaster, maskslave, pic1, pic2;
+  pic1 = 0x20; // remapping location master
+  pic2 = 0x28;
+  maskmaster = inb(0x21); // master cmd=0x20, data=0x21
+  maskslave = inb(0xA1); // slave command=0xA0, data=0xA1
+
+  outb(0x20, 0x20); // end of init
+
+  outb(0x20, 0x11); // init + ICW1_ICW2
+  outb(0xA0, 0x11); // init + ICW1_ICW2
+    
+  outb(0x21, pic1); // ICW2, write offset
+  outb(0xA1, pic2); // ICW2
+
+  outb(0x21, 4); // ICW3, write a 4!
+  outb(0xA1, 2); // ICW3, write a 2!
+
+  outb(0x21, 1); // ICW4, 8086 mode
+  outb(0xA1, 1); // ICW4
+
+  outb(0x21, maskmaster);
+  outb(0xA1, maskslave);
+}
+
+// Interrupt service routines:
+
+void INT0handler() 
+{
+  printf("INT0 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT1handler() 
+{
+  printf("INT1 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT2handler() 
+{
+  printf("INT2 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT3handler() 
+{
+  printf("INT3 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT4handler() 
+{
+  printf("INT4 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT5handler() 
+{
+  printf("INT5 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT6handler() 
+{
+  printf("INT6 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT7handler() 
+{
+  printf("INT7 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT8handler() 
+{
+  printf("INT8 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT9handler() 
+{
+  printf("INT9 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT10handler() 
+{
+  printf("INT10 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT11handler() 
+{
+  printf("INT11 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT12handler() 
+{
+  printf("INT12 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT13handler() 
+{
+  printf("INT13 called!\n");
+  panic("Unhandled exception!");
+}
+
+void* getUnusedPage() {
+ return 0;
+
+}
+
+void mappage(uint32_t address) {
+  uint32_t pageDirIndex = address >> 22;
+  uint32_t pageTableIndex = (address >> 12) & 0x3FF;
+
+  printf("Allocating page at %d, tableindex=%d\n", pageDirIndex, pageTableIndex);
+  //uint32_t *pageTable = (uint32_t*)(pageDirectory[pageDirIndex] & 0xFFFFFC00);
+ 
+
+}
+
+void INT14handler(unsigned int address) 
+{
+  printf("INT14 called! Page fault for address 0x%x!\n", address);
+  if ( (address & 0xF0000000) == 0xD0000000 ) {
+    mappage(address & 0xFFFFF000);
+    return;
+  }
+
+  panic("Unhandled exception!");
+}
+
+void INT15handler() 
+{
+  printf("INT15 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT16handler() 
+{
+  printf("INT16 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT17handler() 
+{
+  printf("INT17 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT18handler() 
+{
+  printf("INT18 called!\n");
+  panic("Unhandled exception!");
+}
+
+void INT19handler() 
+{
+  printf("INT19 called!\n");
+  panic("Unhandled exception!");
+}
+
+// remapped IRQ from master PIC:
+void INT32handler() 
+{
+  // System timer.
+  //printf("INT32 called!\n");
+  // called very frequent, what is this?
+  timerDriverUpdate();
+  outb(0x20, 0x20); // EOI to master
+}
+
+void INT33handler() 
+{
+  //printf("INT33 called, key pressed????\n");
+  unsigned char scancode = inb(0x60);
+  //printf("Scancode = 0x%x\n", scancode);
+  keyboardDriverUpdate(scancode);
+  outb(0x20, 0x20); // EOI to master
+}
+
+void INT34handler() 
+{
+  printf("INT34 called!\n");
+}
+
+void INTDEF_handler() 
+{
+  printf("Default int handler called\n");
+}
+
+
--- a/cos/kernel/kernel.c	Mon Nov 14 21:44:35 2011 +0100
+++ b/cos/kernel/kernel.c	Mon Nov 14 22:45:55 2011 +0100
@@ -129,6 +129,9 @@
   clear_screen();
   printf("Welcome!\n");
   printf("sizeof(uint32_t)=%u, sizeof(uint64_t)=%u\n", sizeof(uint32_t), sizeof(uint64_t));
+
+  printf("Enabling interrupts\n");
+  setupIDT();
   printf("Entering mainloop!\n");
 
   while (1==1) 
--- a/cos/kernel/kernel.h	Mon Nov 14 21:44:35 2011 +0100
+++ b/cos/kernel/kernel.h	Mon Nov 14 22:45:55 2011 +0100
@@ -19,6 +19,10 @@
 void memset(void* ptr, uint32_t value, uint32_t num);
 void memcpy(void* dst, void* src, uint32_t num);
 
+struct IDT_entry {
+  unsigned char b[8];
+};
+
 // memory alloc functions:
 void* malloc(size_t size);
 void free(void* ptr);
@@ -31,6 +35,10 @@
 unsigned char inb(unsigned short);
 void outb(unsigned short, unsigned char);
 
+// ASM helper:
+int loadIDT(struct IDT_entry *table, unsigned short size);
+void halt(void);
+
 void setupIDT(void);
 void PICremap(void);
 // Assembler util functions: