view cos/kernel/handlers.c @ 16:ddefe6d97cd7

Merged
author windel-eee
date Fri, 18 Nov 2011 14:41:53 +0100
parents a58904747019
children f3e3e0e9c4bc
line wrap: on
line source

#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");
}