/************************************************************************************** Copyright (c) 2002 The UbixOS Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $Id$ **************************************************************************************/ #include <ubixos/idt.h> #include <ubixos/gdt.h> #include <ubixos/schedule.h> #include <ubixos/syscall.h> #include <ubixos/vitals.h> #include <drivers/video.h> #include <vmm/memory.h> #include <vmm/paging.h> descriptorTable(IDT, 256) { }; struct { unsigned short limit __attribute__ ((packed)); union descriptorTableunion *idt __attribute__ ((packed)); } loadidt= { (256 * sizeof(union descriptorTableunion) - 1), IDT }; /* Sets Up Initial IDT Table */ void initIdt() { int i=0; for (i=0;i<256;i++) { setVector(&intNull, i, dPresent + dInt + dDpl1); } asm ( "cli\n" "lidt (%0) \n" /* Load the IDT */ "pushfl \n" /* Clear the NT flag */ "andl $0xffffbfff,(%%esp) \n" "popfl \n" "sti \n" : : "r" ((char *) &loadidt) ); setVector(&_int0,0,dPresent + dTrap + dDpl1); setVector(&_int1,1,dPresent + dTrap + dDpl1); setVector(&_int2,2,dPresent + dTrap + dDpl1); setVector(&_int3,3,dPresent + dTrap + dDpl1); setVector(&_int4,4,dPresent + dTrap + dDpl1); setVector(&_int5,5,dPresent + dTrap + dDpl1); setVector(&_int6,6,dPresent + dTrap + dDpl1); setVector(&_int7,7,dPresent + dTrap + dDpl1); setVector(&_int8,8,dPresent + dTrap + dDpl1); setVector(&_int9,9,dPresent + dTrap + dDpl1); setVector(&_int10,10,dPresent + dTrap + dDpl1); setVector(&_int11,11,dPresent + dTrap + dDpl1); setVector(&_int12,12,dPresent + dTrap + dDpl1); setVector(&_int13,13,dPresent + dTrap + dDpl1); setVector(&_pageFault,14,dPresent + dTrap + dDpl1); setVector(&_sysCall,128,dPresent + dTrap + dDpl1); setVector(timerInt,0x68, (dInt + dPresent + dDpl3)); } /* Sets Up IDT Vector */ void setVector(void *handler, unsigned char interrupt, unsigned short controlMajor) { unsigned short codesegment = 0x08; asm volatile("movw %%cs,%0":"=g" (codesegment)); IDT[interrupt].gate.offsetLow = (unsigned short) (((unsigned long)handler)&0xffff); IDT[interrupt].gate.selector = codesegment; IDT[interrupt].gate.access = controlMajor; IDT[interrupt].gate.offsetHigh = (unsigned short) (((unsigned long)handler) >> 16); } /* Null Intterupt Descriptor */ void intNull() { kprintf("Woot Invalid Interrupt\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int0() { kprint("int0: Divide-by-Zero\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int1() { kprint("int1: Debug exception\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int2() { kprint("int2: unknown error\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int3() { kprint("int3: Breakpoint\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int4() { kprint("int4: Overflow\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int5() { kprint("int5: Bounds check\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int6() { kprintf("int6: Invalid opcode!\n"); freeProcessPages(_current->id); _current->status = EMPTY; schedule(); while(1); } void _int7() { kprintf("int7: Device Not Available!\n"); while(1); } void _int8() { kprintf("int8: Double Fault!\n"); while(1); } void _int9() { kprintf("int9: Coprocessor Segment Overrun!\n"); while(1); } void _int10() { kprintf("int10: Invalid TSS!\n"); while(1); } void _int11() { kprintf("int11: Segment Not Present!\n"); while(1); } void _int12() { kprintf("int12: Stack-Segment Fault!\n"); while(1); } void _int13() { kprintf("int13: General Protection Fault!\n"); while(1); } /* Timer Interupt */ __asm__ ( ".globl timerInt \n" "timerInt: \n" " pusha \n" " movl systemVitals,%ecx \n" " incl 4(%ecx) \n" " mov $0x20,%dx \n" " mov $0x20,%ax \n" " outb %al,%dx \n" " movl 4(%ecx),%eax \n" " movl $1000,%ebx \n" " xor %edx,%edx \n" " div %ebx \n" " test %edx,%edx \n" " jnz next \n" " incl 8(%ecx) \n" "next: \n" " movl 4(%ecx),%eax \n" " movl $5,%ebx \n" " xor %edx,%edx \n" " div %ebx \n" " test %edx,%edx \n" " jnz done \n" " call schedule \n" " jmp done \n" "done: \n" " popa \n" " iret \n" );