1 #include "types.h" 2 #include "defs.h" 3 #include "param.h" 4 #include "mmu.h" 5 #include "proc.h" 6 #include "x86.h" 7 #include "traps.h" 8 #include "spinlock.h" 9 10 // Interrupt descriptor table (shared by all CPUs). 11 struct gatedesc idt[256]; 12 extern uint vectors[]; // in vectors.S: array of 256 entry pointers 13 struct spinlock tickslock; 14 int ticks; 15 16 void 17 tvinit(void) 18 { 19 int i; 20 21 for(i = 0; i < 256; i++) 22 SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0); 23 SETGATE(idt[T_SYSCALL], 0, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER); 24 25 initlock(&tickslock, "time"); 26 } 27 28 void 29 idtinit(void) 30 { 31 lidt(idt, sizeof(idt)); 32 } 33 34 void 35 trap(struct trapframe *tf) 36 { 37 if(tf->trapno == T_SYSCALL){ 38 if(cp->killed) 39 exit(); 40 cp->tf = tf; 41 syscall(); 42 if(cp->killed) 43 exit(); 44 return; 45 } 46 47 // No interrupts during interrupt handling. 48 pushcli(); 49 50 switch(tf->trapno){ 51 case IRQ_OFFSET + IRQ_TIMER: 52 if(cpu() == 0){ 53 acquire(&tickslock); 54 ticks++; 55 wakeup(&ticks); 56 release(&tickslock); 57 } 58 lapic_eoi(); 59 break; 60 case IRQ_OFFSET + IRQ_IDE: 61 ide_intr(); 62 lapic_eoi(); 63 break; 64 case IRQ_OFFSET + IRQ_KBD: 65 kbd_intr(); 66 lapic_eoi(); 67 break; 68 case IRQ_OFFSET + IRQ_SPURIOUS: 69 cprintf("cpu%d: spurious interrupt at %x:%x\n", 70 cpu(), tf->cs, tf->eip); 71 lapic_eoi(); 72 break; 73 74 default: 75 if(cp == 0 || (tf->cs&3) == 0){ 76 // In kernel, it must be our mistake. 77 cprintf("unexpected trap %d from cpu %d eip %x\n", 78 tf->trapno, cpu(), tf->eip); 79 panic("trap"); 80 } 81 // In user space, assume process misbehaved. 82 cprintf("pid %d %s: trap %d err %d on cpu %d eip %x -- kill proc\n", 83 cp->pid, cp->name, tf->trapno, tf->err, cpu(), tf->eip); 84 cp->killed = 1; 85 } 86 87 popcli(); 88 89 // Force process exit if it has been killed and is in user space. 90 // (If it is still executing in the kernel, let it keep running 91 // until it gets to the regular system call return.) 92 if(cp && cp->killed && (tf->cs&3) == DPL_USER) 93 exit(); 94 95 // Force process to give up CPU on clock tick. 96 // If interrupts were on while locks held, would need to check nlock. 97 if(cp && cp->state == RUNNING && tf->trapno == IRQ_OFFSET+IRQ_TIMER) 98 yield(); 99 } 100