Skip to content
Snippets Groups Projects
trap.c 2.5 KiB
Newer Older
rtm's avatar
foo
rtm committed
#include "types.h"
rsc's avatar
rsc committed
#include "defs.h"
rtm's avatar
foo
rtm committed
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#include "traps.h"
#include "spinlock.h"
rtm's avatar
foo
rtm committed

rsc's avatar
rsc committed
// Interrupt descriptor table (shared by all CPUs).
struct gatedesc idt[256];
rsc's avatar
rsc committed
extern uint vectors[];  // in vectors.S: array of 256 entry pointers
struct spinlock tickslock;
int ticks;
rtm's avatar
foo
rtm committed

void
rsc's avatar
rsc committed
tvinit(void)
rtm's avatar
foo
rtm committed
{
  int i;

rsc's avatar
rsc committed
  for(i = 0; i < 256; i++)
rsc's avatar
 
rsc committed
    SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0);
  SETGATE(idt[T_SYSCALL], 0, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER);
  
  initlock(&tickslock, "time");
rtm's avatar
rtm committed
}

void
rsc's avatar
rsc committed
idtinit(void)
rtm's avatar
rtm committed
{
rsc's avatar
 
rsc committed
  lidt(idt, sizeof(idt));
rtm's avatar
foo
rtm committed
}

void
trap(struct trapframe *tf)
rtm's avatar
foo
rtm committed
{
rsc's avatar
rsc committed
  if(tf->trapno == T_SYSCALL){
    if(cp->killed)
rtm's avatar
rtm committed
    cp->tf = tf;
    syscall();
    if(cp->killed)
rsc's avatar
 
rsc committed
  // No interrupts during interrupt handling.
  splhi();
rsc's avatar
rsc committed

rsc's avatar
rsc committed
  switch(tf->trapno){
rsc's avatar
rsc committed
  case IRQ_OFFSET + IRQ_TIMER:
    if(cpu() == 0){
      acquire(&tickslock);
      ticks++;
      wakeup(&ticks);
      release(&tickslock);
    lapic_eoi();
    break;
rsc's avatar
rsc committed
  case IRQ_OFFSET + IRQ_IDE:
    ide_intr();
rsc's avatar
rsc committed
    break;
  case IRQ_OFFSET + IRQ_KBD:
rsc's avatar
rsc committed
    break;
  case IRQ_OFFSET + IRQ_SPURIOUS:
rsc's avatar
rsc committed
    cprintf("cpu%d: spurious interrupt at %x:%x\n",
            cpu(), tf->cs, tf->eip);
    lapic_eoi();
rsc's avatar
rsc committed
    break;
    
  default:
rsc's avatar
rsc committed
    if(cp == 0 || (tf->cs&3) == 0){
      // In kernel, it must be our mistake.
      cprintf("unexpected trap %d from cpu %d eip %x\n",
              tf->trapno, cpu(), tf->eip);
      panic("trap");
rsc's avatar
rsc committed
    }
rsc's avatar
rsc committed
    // In user space, assume process misbehaved.
    cprintf("pid %d %s: trap %d err %d on cpu %d eip %x -- kill proc\n",
            cp->pid, cp->name, tf->trapno, tf->err, cpu(), tf->eip);
    cp->killed = 1;
rsc's avatar
 
rsc committed
  // Undo splhi but do not enable interrupts.
  // If you change this to spllo() you can get a 
  // triple fault by just typing too fast at the prompt.
  // An interrupt stops us right here, and when that
  // interrupt tries to return, somehow the segment
  // registers are all invalid.
  --cpus[cpu()].nsplhi;
  // Force process exit if it has been killed and is in user space.
  // (If it is still executing in the kernel, let it keep running 
  // until it gets to the regular system call return.)
  if(cp && cp->killed && (tf->cs&3) == DPL_USER)
  // Force process to give up CPU on clock tick.
  // If interrupts were on while locks held, would need to check nlock.
  if(cp && cp->state == RUNNING && tf->trapno == IRQ_OFFSET+IRQ_TIMER)
    yield();
rtm's avatar
foo
rtm committed
}