Newer
Older
ubixos-old / src / sys / sys / idt.c
@reddawg reddawg on 15 Apr 2004 15 KB UbixOS v1.0
/*****************************************************************************************
 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.

 $Log$
 Revision 1.29  2004/04/13 16:36:34  reddawg
 Changed our copyright, it is all now under a BSD-Style license



 $Id$

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

#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>

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

void            mathStateRestore();

descriptorTable(IDT, 256)
{
};

struct {
  unsigned short limit __attribute__((packed));
  union descriptorTableunion *idt __attribute__((packed));
}               loadidt = {

  (256 * sizeof(union descriptorTableunion) - 1), IDT
};

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

Function: int idtInit()

Description: This function is used to enable our IDT subsystem

Notes:

02/20/2004 - Approved for quality

************************************************************************/
int 
idtInit()
{
  int             i = 0x0;

  struct tssStruct *gpfTSS     = (struct tssStruct *)0x4200;

  /* Set up default vector table for all possible 256 interrupts */
  for (i = 0x0; i < 256; i++) {
    setVector(&intNull, i, dPresent + dInt + dDpl3);
  }

  /* Load the IDT into the system */
  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)
    );

  /* Set up the basic vectors for the reserved ints */
  setVector(&_int0, 0, dPresent + dTrap + dDpl3);
  setVector(&_int1, 1, dPresent + dTrap + dDpl3);
  setVector(&_int2, 2, dPresent + dTrap + dDpl3);
  setVector(&_int3, 3, dPresent + dTrap + dDpl3);
  setVector(&_int4, 4, dPresent + dTrap + dDpl3);
  setVector(&_int5, 5, dPresent + dTrap + dDpl3);
  setVector(&_int6, 6, dPresent + dTrap + dDpl3);
  setVector(&_int7,7,dPresent + dTrap + dDpl3);
  /* setVector(&_int8,8,dPresent + dTrap + dDpl3); */
  /* setTaskVector(8,dPresent + dTask + dDpl3,0x20); */
  setVector(&_int9, 9, dPresent + dTrap + dDpl3);
  setVector(&_int10, 10, dPresent + dTrap + dDpl3);
  setVector(&_int11, 11, dPresent + dTrap + dDpl3);
  setVector(&_int12, 12, dPresent + dTrap + dDpl3);
  /* setVector(&_int13,13,dPresent + dTrap + dDpl3); */
  setTaskVector(13, dPresent + dTask + dDpl3, 0x38);
  setVector(&_vmmPageFault, 14, dPresent + dTrap + dDpl3);
  setVector(&_sysCall, 128, dPresent + dTrap + dDpl3);
  setVector(&timerInt, 0x68, (dInt + dPresent + dDpl3));
//  setVector(&sched,0x69,(dInt + dPresent + dDpl3));


  gpfTSS->back_link    = 0x0;
  gpfTSS->esp0         = 0x0;//(uInt32)kmalloc(4096,sysID)+4096;
  gpfTSS->ss0          = 0x0;//0x10;
  gpfTSS->esp1         = 0x0;
  gpfTSS->ss1          = 0x0;
  gpfTSS->esp2         = 0x0;
  gpfTSS->ss2          = 0x0;
  gpfTSS->cr3          = (unsigned int)kernelPageDirectory;
  gpfTSS->eip          = (unsigned int)&_int13;
  gpfTSS->eflags       = 0x206;
  gpfTSS->esp          = 0x1CFFF;
  gpfTSS->ebp          = 0x1CFFF;
  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;


  /* Print out information for the IDT */
  kprintf("idt0 - Address: [0x%X]\n", &IDT);

  /* Return so we know all went well */
  return (0x0);
}


/* 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);
}

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

Function: void setTaskVector(uInt8,uInt16,uInt8);
Description: This Function Sets Up An IDT Task Vector
Notes:

************************************************************************/
void 
setTaskVector(uInt8 interrupt, uInt16 controlMajor, uInt8 selector)
{
  uInt16          codesegment = 0x08;
  asm             volatile ("movw %%cs,%0":"=g" (codesegment));

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


/* Null Intterupt Descriptor */
void 
intNull()
{
  kprintf("Woot Invalid Interrupt[%i]\n",_current->id);

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  _current->state = DEAD;
  sched();
  while (1);
  return;
}

void 
_int0()
{
  kprintf("int0: Divide-by-Zero\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; schede();
   */
  while (1);
}
void 
_int1()
{
  kprintf("int1: Debug exception\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  while (1);
}
void 
_int2()
{
  kprintf("int2: unknown error\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  while (1);
}
void 
_int3()
{
  kprintf("int3: Breakpoint\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  while (1);
}
void 
_int4()
{
  kprintf("int4: Overflow\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  while (1);
}
void 
_int5()
{
  kprintf("int5: Bounds check\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  while (1);
}
void 
_int6()
{
  kprintf("int6: Invalid opcode!\n");

  /*
   * freeProcessPages(_current->id); setTaskStatus(_current->id, EXITING);
   * _current->status = EMPTY; sched();
   */
  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");
 * kprintf("Task Id: [%i]\n",_current->id); while(1); }
 */

void 
_int13()
{
  uInt8          *ip = 0x0;
  uInt16         *stack = 0x0, *ivt = 0x0;
  uInt32         *stack32 = 0x0;
  bool            isOperand32 = FALSE, isAddress32 = FALSE;

  irqDisable(0x0);

  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;

  //kprintf("_current->tss.cs: [0x%X:0x%X:0x%X:0x%X]\n",_current->tss.cs,_current->tss.eip,_current->id,_current->tss.ss);
  //kprintf("\n\nSS: [0x%X]\n",_current->tss.eflags);

gpfStart:

  //kprintf("ip[0]: 0x%X:0x%X\n",ip,ip[0]);
  //kprintf("IP: [0x%X][0x%X][0x%X][0x%X][0x%X]\n",ip,ip[0],ip[1],ip[2],ip[3]);
  switch (ip[0]) {
  case 0xCD:			/* INT n */
    switch (ip[1]) {
    case 0x69:
      kprintf("Exit Bios [0x%X]\n",_current->id);
      _current->state = DEAD;
      break;
    case 0x20:
    case 0x21:
      kprintf("Help!!!\n");
      while (1);
      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;
      //kprintf("New Int [0x%X][0x%X][0x%X][0x%X]\n",ip[1],_current->tss.cs,_current->tss.eip,_current->id);
      //while(1);
      break;
    }
    break;
  case 0x66:
    isOperand32 = TRUE;
    ip++;
    _current->tss.eip = (uInt16) (_current->tss.eip + 1); 
    goto gpfStart;
    break;
  case 0x67:
    isAddress32 = TRUE;
    ip++;
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);
    goto gpfStart;
    break;
  case 0xF0:
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);
    kprintf("No!!!\n");
    while (1);
    break;
  case 0x9C:
    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;
     else stack32[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;
       else stack[0] &= ~EFLAG_IF;
       _current->tss.eip = (uInt16) (_current->tss.eip + 1);
       
    }
    break;
  case 0x9D:
    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;
  case 0xFA:
    _current->oInfo.v86If = FALSE;
    _current->tss.eflags &= ~EFLAG_IF;
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);
    _current->oInfo.timer = 0x1;
    /* kprintf("cli [0x%X]\n",_current->id); */
    break;
  case 0xFB:
    _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;
  case 0xCF:
    _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;
  case 0xEC:			/* IN AL,DX */
    _current->tss.eax = (_current->tss.eax & ~0xFF) | inportByte(_current->tss.edx);
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);

    /*
     * kprintf("IN DX[0x%X],AL[0x%X]
     * [0x%X]\n",_current->tss.edx,_current->tss.eax & 0xFF,_current->id);
     */
    break;
  case 0xED:			/* IN AX,DX */
    _current->tss.eax = (_current->tss.eax & ~0xFFFF) | inportWord(_current->tss.edx);
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);

    /*
     * kprintf("IN DX[0x%X],AX[0x%X]
     * [0x%X]\n",_current->tss.edx,_current->tss.eax & 0xFFFF,_current->id);
     */
    break;
  case 0xEE:			/* OUT DX,AL */
    outportByte(_current->tss.edx, _current->tss.eax & 0xFF);
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);

    /*
     * kprintf("OUT DX[0x%X],AL[0x%X]
     * [0x%X]\n",_current->tss.edx,_current->tss.eax & 0xFF,_current->id);
     */
    break;
  case 0xEF:
    outportWord(_current->tss.edx, _current->tss.eax);
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);

    /*
     * kprintf("OUT DX[0x%X],AX[0x%X]
     * [0x%X]\n",_current->tss.edx,_current->tss.eax,_current->id);
     */
    break;
  case 0xF4:
    _current->tss.eip = (uInt16) (_current->tss.eip + 1);
    /* kprintf("HLT [0x%X]\n",_current->id); */
    break;
  default:			/* something wrong */
     kprintf("NonHandled OpCode [0x%X:0x%X]\n",_current->id,ip[0]);
     _current->state = DEAD;
    /*
     * kprintf("Non Handled OpCode [0x%X]\n",_current->id);
     * setTaskStatus(_current->id,EXITING); Temp Reboot For Now
     * while(inportByte(0x64) & 0x02); outportByte(0x64, 0xfe);
     */
    break;
  }
  //kprintf("\nEFLAGS: [0x%X]\n",_current->tss.eflags);
  irqEnable(0);
  while (1);
}

/* Timer Interupt */
__asm__(
	".globl timerInt           \n"
	"timerInt:                 \n"
        "  pushl %edx               \n"
        "  pushl %ecx               \n"
        "  pushl %ebx               \n"
        "  pushl %eax               \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 sched              \n"
	"done:                     \n"
        "  popl %eax               \n"
        "  popl %ebx               \n"
        "  popl %ecx               \n"
        "  popl %edx               \n"
	"  iret                    \n"
);

void _int7();
__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"
  );

void mathStateRestore() {
  if (_usedMath != 0x0)
    __asm__("fnsave %0"::"m" (_usedMath->i387));
  if (_current->usedMath != 0x0)
    __asm__("frstor %0"::"m" (_current->i387));
  else {
    __asm__("fninit"::);
    _current->usedMath=1;
    }
    _usedMath=_current;
  //Return
  }

/***
 END
 ***/