view cos/kernel/handlers.c @ 26:dcce92b1efbc

Added mm.c
author windel
date Tue, 27 Dec 2011 17:36:52 +0100
parents d8627924d40d
children 7e3bdcb391dc
line wrap: on
line source

#include "kernel.h"

// 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:
IDT_entry *idt = (IDT_entry*)0x5000;
volatile idtPointer idtP;

void setIDTentry(int num, void (*handler)(), uint16_t selector, uint8_t flags)
{
  // Fill one entry with IDT info:
  uint64_t offset;

  // Typecast the function pointer to a number:
  offset = (uint64_t)handler;

  //panic("Almost setting an IDT entry");

  // 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 reserved fields:
  idt[num].reserved1 = 0;
  idt[num].reserved2 = 0;
}

void setupIDT(void) {
  int i;

  // Fill all vectors with the default handler:
  for (i=0; i<256; i++) {
    setIDTentry(i, INTDEF, 0x08, 0x8E);
  }

  // Now set other then default handler:
  setIDTentry(0, INT0, 0x08, 0x8E);
  setIDTentry(1, INT1, 0x08, 0x8E);
  setIDTentry(2, INT2, 0x08, 0x8E);
  setIDTentry(3, INT3, 0x08, 0x8E);
  setIDTentry(4, INT4, 0x08, 0x8E);
  setIDTentry(5, INT5, 0x08, 0x8E);
  setIDTentry(6, INT6, 0x08, 0x8E);
  setIDTentry(7, INT7, 0x08, 0x8E);
  setIDTentry(8, INT8, 0x08, 0x8E);
  setIDTentry(9, INT9, 0x08, 0x8E);
  setIDTentry(10, INT10, 0x08, 0x8E);
  setIDTentry(11, INT11, 0x08, 0x8E);
  setIDTentry(12, INT12, 0x08, 0x8E);
  setIDTentry(13, INT13, 0x08, 0x8E);
  setIDTentry(14, INT14, 0x08, 0x8E);
  setIDTentry(15, INT15, 0x08, 0x8E);
  setIDTentry(16, INT16, 0x08, 0x8E);
  setIDTentry(17, INT17, 0x08, 0x8E);
  setIDTentry(18, INT18, 0x08, 0x8E);
  setIDTentry(19, INT19, 0x08, 0x8E);
  /* reserved interrupts: */
  // From int20 - int31
  setIDTentry(32, INT32, 0x08, 0x8F);
  setIDTentry(33, INT33, 0x08, 0x8F);
  setIDTentry(34, INT34, 0x08, 0x8F);

  // Set the correct values in the IDT pointer:
  idtP.base = (uint64_t)idt;
  idtP.limit = (sizeof(IDT_entry) * 256) - 1;
  // call load IDT asm function:
  loadIDT();

  PICremap();
  asm("sti");
}

// 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(uint64_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()
{
  uint64_t faulting_address;

  // Retrieve failed page from CR2:
  asm volatile("mov %%cr2, %0" : "=r" (faulting_address));

  printf("INT14 called! Page fault for address 0x%X!\n", faulting_address);
  if ( (faulting_address & 0xF0000000) == 0xD0000000 ) {
    mappage(faulting_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();
  // Acknowledge int:
  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");
}