idt.c
sys/idt.h
sys/gdt.h
sys/io.h
ubixos/sched.h
isa/8259.h
lib/kprintf.h
lib/kmalloc.h
vmm/vmm.h
ubixos/syscall.h
ubixos/kpanic.h
ubixos/endtask.h
string.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/endtask.h
ubixos/syscall.h
ubixos/types.h
sys/_types.h
vmm/paging.h
string.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
vmm/vmm.h
sys/idt.h
vfs/mount.h
lib/kprintf.h
sys/gdt.h
ubixfs/dirCache.h
sys/sysproto.h
sys/signal.h
ubixos/tty.h
sys/io.h
src/sys/sys/idt.c
isa/8259.h
FP_TO_LINEAR
seg
off
((void*) ((((uInt16) (seg)) << 4) + ((uInt16) (off))))
_int13
void void _int0 () _int0 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int1 () _int1 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int10 () _int10 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int11 () _int11 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int12 () _int12 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int13 () _int13 _current _int13 tssStruct::cs DEAD tssStruct::eax tssStruct::ebp tssStruct::edx EFLAG_IF EFLAG_VM tssStruct::eflags tssStruct::eip tssStruct::esp FALSE FP_TO_LINEAR taskStruct::id inportByte inportWord irqDisable irqEnable kpanic kprintf taskStruct::oInfo outportByte outportWord tssStruct::ss taskStruct::state osInfo::timer TRUE taskStruct::tss osInfo::v86If _int13 idt_init
void void _int2 () _int2 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int3 () _int3 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int4 () _int4 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int5 () _int5 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int6 () _int6 _current endTask taskStruct::id kpanic sched_yield idt_init
void void _int7 () _int7
void void _int8 () _int8 _current _int8 tssStruct::cr3 tssStruct::cs tssStruct::ds tssStruct::ebp tssStruct::eflags tssStruct::eip tssStruct::es tssStruct::esp tssStruct::fs tssStruct::gs taskStruct::id tssStruct::io_map kernelPageDirectory kpanic tssStruct::ss _int8 idt_init
void void _int9 () _int9 _current endTask taskStruct::id kpanic sched_yield idt_init
asm
(".globl _int7 \n""_int7: \n"" pushl %eax \n"" clts \n"" movl _current,%eax \n"" cmpl _usedMath,%eax \n"" je mathDone \n"" call mathStateRestore \n""mathDone: \n"" popl %eax \n"" iret \n")
asm
".globl _int7 \n""_int7: \n"" pushl %eax \n"" clts \n"" movl _current
%eax\n""cmpl _usedMath
%eax\n""je mathDone\n""call mathStateRestore\n""mathDone:\n""popl%eax\n""iret\n"
int int idt_init () idt_init _int0 _int1 _int10 _int11 _int12 _int13 _int2 _int3 _int4 _int5 _int6 _int7 _int8 _int9 _sysCall _sysCall_new _vmm_pageFault tssStruct::back_link tssStruct::cr3 tssStruct::cs dDpl0 dDpl3 dInt dPresent tssStruct::ds dTask dTrap tssStruct::ebp tssStruct::edi tssStruct::eflags tssStruct::eip tssStruct::es tssStruct::esi tssStruct::esp tssStruct::esp0 tssStruct::esp1 tssStruct::esp2 tssStruct::fs tssStruct::gs intNull tssStruct::io_map kernelPageDirectory kprintf tssStruct::ldt loadidt memset setTaskVector setVector tssStruct::ss tssStruct::ss0 tssStruct::ss1 tssStruct::ss2 timerInt tssStruct::trace_bitmap
void void intNull () intNull _current taskStruct::id kprintf idt_init
void void mathStateRestore () mathStateRestore _current _usedMath taskStruct::i387 taskStruct::usedMath
void void setTaskVector (uInt8 interrupt, uInt16 controlMajor, uInt8 selector) setTaskVector uInt8 interrupt uInt16 controlMajor uInt8 selector gdtGate::access descriptorTableUnion::gate gdtGate::offsetHigh gdtGate::offsetLow gdtGate::selector idt_init
void void setVector (void *handler, unsigned char interrupt, unsigned short controlMajor) setVector void * handler unsigned char interrupt unsigned short controlMajor gdtGate::access descriptorTableUnion::gate gdtGate::offsetHigh gdtGate::offsetLow gdtGate::selector atkbd_init fdc_init idt_init initLNC mouseInit ne2k_init
static static ubixDescriptorTable (ubixIDT, 256) ubixDescriptorTable ubixIDT 256
struct @8 struct @8 loadidt loadidt idt_init

    /*****************************************************************************************
    Copyright(c)2002-2005TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: idt_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #include<sys/idt.h>
    #include<sys/gdt.h>
    #include<sys/io.h>
    #include<ubixos/sched.h>
    #include<isa/8259.h>
    #include<lib/kprintf.h>
    #include<lib/kmalloc.h>
    #include<vmm/vmm.h>
    #include<ubixos/syscall.h>
    #include<ubixos/kpanic.h>
    #include<ubixos/endtask.h>
    #include<string.h>

    #defineFP_TO_LINEAR(seg,off)((void*)((((uInt16)(seg))<<4)+((uInt16)(off))))

    staticubixDescriptorTable(ubixIDT,256){};

    staticstruct{
    unsignedshortlimit__attribute__((packed));
    uniondescriptorTableUnion*idt__attribute__((packed));
    }loadidt={
    (256*sizeof(uniondescriptorTableUnion)-1),ubixIDT
    };

    /************************************************************************

    Function:intidtInit()
    Description:ThisfunctionisusedtoenableourIDTsubsystem
    Notes:

    02/20/2004-Approvedforquality

    ************************************************************************/
    intidt_init(){
    inti=0x0;

    structtssStruct*sfTSS=(structtssStruct*)0x6200;
    structtssStruct*gpfTSS=(structtssStruct*)0x4200;

    /*Setupdefaultvectortableforallpossible256interrupts*/

    for(i=0x0;i<256;i++){
    setVector(intNull,i,dPresent+dInt+dDpl3);
    }

    /*LoadtheIDTintothesystem*/
    asmvolatile(
    "cli\n"
    "lidt(%0)\n"/*LoadtheIDT*/
    "pushfl\n"/*CleartheNTflag*/
    "andl$0xffffbfff,(%%esp)\n"
    "popfl\n"
    "sti\n"
    :
    :"r"((char*)&loadidt)
    );

    /*Setupthebasicvectorsforthereservedints*/
    setVector(_int0,0,dPresent+dInt+dDpl0);
    setVector(_int1,1,dPresent+dInt+dDpl0);
    setVector(_int2,2,dPresent+dInt+dDpl0);
    setVector(_int3,3,dPresent+dInt+dDpl0);
    setVector(_int4,4,dPresent+dInt+dDpl0);
    setVector(_int5,5,dPresent+dInt+dDpl0);
    setVector(_int6,6,dPresent+dInt+dDpl0);
    setVector(_int7,7,dPresent+dInt+dDpl0);
    setTaskVector(8,dPresent+dTask+dDpl0,0x40);
    setVector(_int9,9,dPresent+dInt+dDpl0);
    setVector(_int10,10,dPresent+dInt+dDpl0);
    setVector(_int11,11,dPresent+dInt+dDpl0);
    setVector(_int12,12,dPresent+dInt+dDpl0);
    setTaskVector(13,dPresent+dTask+dDpl0,0x38);
    setVector(_vmm_pageFault,14,dPresent+dInt+dDpl0);
    setVector(_sysCall,128,dPresent+dTrap+dDpl3);
    setVector(_sysCall_new,0x81,dPresent+dTrap+dDpl3);
    //setVector(_sysCallNew,0x90,dPresent+dTrap+dDpl3);
    setVector(timerInt,0x68,(dInt+dPresent+dDpl0));

    gpfTSS->back_link=0x0;
    gpfTSS->esp0=0x0;
    gpfTSS->ss0=0x0;
    gpfTSS->esp1=0x0;
    gpfTSS->ss1=0x0;
    gpfTSS->esp2=0x0;
    gpfTSS->ss2=0x0;
    gpfTSS->cr3=(unsignedint)kernelPageDirectory;
    gpfTSS->eip=(unsignedint)&_int13;
    gpfTSS->eflags=0x206;
    gpfTSS->esp=0x1D000;
    gpfTSS->ebp=0x1D000;
    gpfTSS->esi=0x0;
    gpfTSS->edi=0x0;
    gpfTSS->es=0x10;
    gpfTSS->cs=0x08;
    gpfTSS->ss=0x10;
    gpfTSS->ds=0x10;
    gpfTSS->fs=0x10;
    gpfTSS->gs=0x10;
    gpfTSS->ldt=0x0;
    gpfTSS->trace_bitmap=0x0000;
    gpfTSS->io_map=0x8000;

    memset(sfTSS,0x0,sizeof(structtssStruct));
    sfTSS->cr3=(unsignedint)kernelPageDirectory;
    sfTSS->eip=(unsignedint)&_int8;
    sfTSS->eflags=0x206;
    sfTSS->esp=0x1C000;
    sfTSS->ebp=0x1C000;
    sfTSS->es=0x10;
    sfTSS->cs=0x08;
    sfTSS->ss=0x10;
    sfTSS->ds=0x10;
    sfTSS->fs=0x10;
    sfTSS->gs=0x10;
    sfTSS->io_map=0x8000;

    /*PrintoutinformationfortheIDT*/
    kprintf("idt0-Address:[0x%X]\n",&ubixIDT);

    /*Returnsoweknowallwentwell*/
    return(0x0);
    }

    /*SetsUpIDTVector*/
    voidsetVector(void*handler,unsignedcharinterrupt,unsignedshortcontrolMajor){
    unsignedshortcodesegment=0x08;
    asmvolatile("movw%%cs,%0":"=g"(codesegment));

    ubixIDT[interrupt].gate.offsetLow=(unsignedshort)(((unsignedlong)handler)&0xffff);
    ubixIDT[interrupt].gate.selector=codesegment;
    ubixIDT[interrupt].gate.access=controlMajor;
    ubixIDT[interrupt].gate.offsetHigh=(unsignedshort)(((unsignedlong)handler)>>16);
    }

    /************************************************************************

    Function:voidsetTaskVector(uInt8,uInt16,uInt8);
    Description:ThisFunctionSetsUpAnIDTTaskVector
    Notes:

    ************************************************************************/
    void
    setTaskVector(uInt8interrupt,uInt16controlMajor,uInt8selector)
    {
    uInt16codesegment=0x08;
    asmvolatile("movw%%cs,%0":"=g"(codesegment));

    ubixIDT[interrupt].gate.offsetLow=0x0;
    ubixIDT[interrupt].gate.selector=selector;
    ubixIDT[interrupt].gate.access=controlMajor;
    ubixIDT[interrupt].gate.offsetHigh=0x0;
    }

    /*NullIntteruptDescriptor*/
    voidintNull(){
    kprintf("InvalidInterrupt[%i]\n",_current->id);
    /*
    kpanic("InvalidInterrupt[%i]\n",_current->id);
    while(1);
    endTask(_current->id);
    sched_yield();
    */
    }

    void_int0(){
    kpanic("int0:Divide-by-Zero[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int1(){
    kpanic("int1:Debugexception[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int2(){
    kpanic("int2:unknownerror[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int3(){
    kpanic("int3:Breakpoint[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int4(){
    kpanic("int4:Overflow[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int5(){
    kpanic("int5:Boundscheck[%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int6(){
    kpanic("int6:Invalidopcode![%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int8(){
    structtssStruct*sfTSS=(structtssStruct*)0x6200;
    kpanic("int8:DoubleFault![%i]\n",_current->id);
    sfTSS->cr3=(unsignedint)kernelPageDirectory;
    sfTSS->eip=(unsignedint)&_int8;
    sfTSS->eflags=0x206;
    sfTSS->esp=0x1C000;
    sfTSS->ebp=0x1C000;
    sfTSS->es=0x10;
    sfTSS->cs=0x08;
    sfTSS->ss=0x10;
    sfTSS->ds=0x10;
    sfTSS->fs=0x10;
    sfTSS->gs=0x10;
    sfTSS->io_map=0x8000;
    while(1);
    }

    void_int9(){
    kpanic("int9:CoprocessorSegmentOverrun![%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int10(){
    kpanic("int10:InvalidTSS![%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int11(){
    kpanic("int11:SegmentNotPresent![%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int12(){
    kpanic("int12:Stack-SegmentFault![%i]\n",_current->id);
    endTask(_current->id);
    sched_yield();
    }

    void_int13(){
    uInt8*ip=0x0;
    uInt16*stack=0x0,*ivt=0x0;
    uInt32*stack32=0x0;
    boolisOperand32=FALSE,isAddress32=FALSE;
    structtssStruct*gpfTSS=(structtssStruct*)0x4200;

    irqDisable(0x0);

    gpfTSS->eip=(unsignedint)&_int13;
    gpfTSS->esp=0x1D000;
    gpfTSS->ebp=0x1D000;
    gpfTSS->eflags=0x206;

    ip=FP_TO_LINEAR(_current->tss.cs,_current->tss.eip);
    ivt=(uInt16*)0x0;
    stack=(uInt16*)FP_TO_LINEAR(_current->tss.ss,_current->tss.esp);
    stack32=(uInt32*)stack;

    gpfStart:
    switch(ip[0]){
    case0xCD:/*INTn*/
    switch(ip[1]){
    case0x69:
    kprintf("ExitBios[0x%X]\n",_current->id);
    _current->state=DEAD;
    break;
    case0x20:
    case0x21:
    kpanic("GPFOP0x20/0x21\n");
    break;
    default:
    stack-=3;
    _current->tss.esp=((_current->tss.esp&0xffff)-6)&0xffff;
    stack[0]=(uInt16)(_current->tss.eip+2);
    stack[1]=_current->tss.cs;stack[2]=(uInt16)_current->tss.eflags;
    if(_current->oInfo.v86If)
    stack[2]|=EFLAG_IF;
    else
    stack[2]&=~EFLAG_IF;
    _current->tss.cs=ivt[ip[1]*2+1]&0xFFFF;
    _current->tss.eip=ivt[ip[1]*2]&0xFFFF;
    break;
    }
    break;
    case0x66:
    isOperand32=TRUE;
    ip++;
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    gotogpfStart;
    break;
    case0x67:
    isAddress32=TRUE;
    ip++;
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    gotogpfStart;
    break;
    case0xF0:
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    kpanic("GPFOP0xF0\n");
    break;
    case0x9C:
    if(isOperand32==TRUE){
    _current->tss.esp=((_current->tss.esp&0xffff)-4)&0xffff;
    stack32--;
    stack32[0]=_current->tss.eflags&0xDFF;
    if(_current->oInfo.v86If==TRUE)
    stack32[0]|=EFLAG_IF;
    elsestack32[0]&=~EFLAG_IF;
    }else{
    _current->tss.esp=((_current->tss.esp&0xffff)-2)&0xffff;
    stack--;

    stack[0]=(uInt16)_current->tss.eflags;
    if(_current->oInfo.v86If==TRUE)stack[0]|=EFLAG_IF;
    elsestack[0]&=~EFLAG_IF;
    _current->tss.eip=(uInt16)(_current->tss.eip+1);

    }
    break;
    case0x9D:
    if(isOperand32==TRUE){
    _current->tss.eflags=EFLAG_IF|EFLAG_VM|(stack32[0]&0xDFF);
    _current->oInfo.v86If=(stack32[0]&EFLAG_IF)!=0;
    _current->tss.esp=((_current->tss.esp&0xffff)+4)&0xffff;
    }else{
    _current->tss.eflags=EFLAG_IF|EFLAG_VM|stack[0];
    _current->oInfo.v86If=(stack[0]&EFLAG_IF)!=0;
    _current->tss.esp=((_current->tss.esp&0xffff)+2)&0xffff;
    }
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    /*kprintf("popf[0x%X]\n",_current->id);*/
    break;
    case0xFA:
    _current->oInfo.v86If=FALSE;
    _current->tss.eflags&=~EFLAG_IF;
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    _current->oInfo.timer=0x1;
    break;
    case0xFB:
    _current->oInfo.v86If=TRUE;
    _current->tss.eflags|=EFLAG_IF;
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    _current->oInfo.timer=0x0;
    /*kprintf("sti[0x%X]\n",_current->id);*/
    break;
    case0xCF:
    _current->tss.eip=stack[0];
    _current->tss.cs=stack[1];
    _current->tss.eflags=EFLAG_IF|EFLAG_VM|stack[2];
    _current->oInfo.v86If=(stack[2]&EFLAG_IF)!=0;
    _current->tss.esp=((_current->tss.esp&0xffff)+6)&0xffff;
    /*kprintf("iret[0x%X]\n",_current->id);*/
    break;
    case0xEC:/*INAL,DX*/
    _current->tss.eax=(_current->tss.eax&~0xFF)|inportByte(_current->tss.edx);
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    break;
    case0xED:/*INAX,DX*/
    _current->tss.eax=(_current->tss.eax&~0xFFFF)|inportWord(_current->tss.edx);
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    break;
    case0xEE:/*OUTDX,AL*/
    outportByte(_current->tss.edx,_current->tss.eax&0xFF);
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    break;
    case0xEF:
    outportWord(_current->tss.edx,_current->tss.eax);
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    break;
    case0xF4:
    _current->tss.eip=(uInt16)(_current->tss.eip+1);
    break;
    default:/*somethingwrong*/
    kprintf("NonHandledOpCode[0x%X:0x%X]\n",_current->id,ip[0]);
    _current->state=DEAD;
    break;
    }
    irqEnable(0);
    while(1);
    }

    /*Removedstatichoweverthisistheonlyplaceit'scalledfrom*/
    voidmathStateRestore(){
    if(_usedMath!=0x0){
    asm(
    "fnsave%0"
    :
    :"m"(_usedMath->i387)
    );
    }
    if(_current->usedMath!=0x0){
    asm(
    "frstor%0"
    :
    :"m"(_current->i387)
    );
    }
    else{
    asm("fninit");
    _current->usedMath=0x1;
    }

    _usedMath=_current;

    //Return
    }

    void_int7();
    asm(
    ".globl_int7\n"
    "_int7:\n"
    "pushl%eax\n"
    "clts\n"
    "movl_current,%eax\n"
    "cmpl_usedMath,%eax\n"
    "jemathDone\n"
    "callmathStateRestore\n"
    "mathDone:\n"
    "popl%eax\n"
    "iret\n"
    );

    /***
    END
    ***/