Xv6 with picoc & Linkage editor  v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
trap.c
00001 #include "types.h"
00002 #include "defs.h"
00003 #include "param.h"
00004 #include "memlayout.h"
00005 #include "mmu.h"
00006 #include "proc.h"
00007 #include "x86.h"
00008 #include "traps.h"
00009 #include "spinlock.h"
00010 
00011 // Interrupt descriptor table (shared by all CPUs).
00012 struct gatedesc idt[256];
00013 extern uint vectors[];  // in vectors.S: array of 256 entry pointers
00014 struct spinlock tickslock;
00015 uint ticks;
00016 
00017 void
00018 tvinit(void)
00019 {
00020   int i;
00021 
00022   for(i = 0; i < 256; i++)
00023     SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0);
00024   SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER);
00025   
00026   initlock(&tickslock, "time");
00027 }
00028 
00029 void
00030 idtinit(void)
00031 {
00032   lidt(idt, sizeof(idt));
00033 }
00034 
00035 //PAGEBREAK: 41
00036 void
00037 trap(struct trapframe *tf)
00038 {
00039   if(tf->trapno == T_SYSCALL){
00040     if(proc->killed)
00041       exit(1);
00042     proc->tf = tf;
00043     syscall();
00044     if(proc->killed)
00045       exit(1);
00046     return;
00047   }
00048 
00049   switch(tf->trapno){
00050   case T_IRQ0 + IRQ_TIMER:
00051     if(cpu->id == 0){
00052       acquire(&tickslock);
00053       ticks++;
00054       wakeup(&ticks);
00055       release(&tickslock);
00056     }
00057     lapiceoi();
00058     break;
00059   case T_IRQ0 + IRQ_IDE:
00060     ideintr();
00061     lapiceoi();
00062     break;
00063   case T_IRQ0 + IRQ_IDE+1:
00064     // Bochs generates spurious IDE1 interrupts.
00065     break;
00066   case T_IRQ0 + IRQ_KBD:
00067     kbdintr();
00068     lapiceoi();
00069     break;
00070   case T_IRQ0 + IRQ_COM1:
00071     uartintr();
00072     lapiceoi();
00073     break;
00074   case T_IRQ0 + 7:
00075   case T_IRQ0 + IRQ_SPURIOUS:
00076     cprintf("cpu%d: spurious interrupt at %x:%x\n",
00077             cpu->id, tf->cs, tf->eip);
00078     lapiceoi();
00079     break;
00080 /*  case IRQ_IDE:
00081         cprintf("I'm corrent\n");
00082         break; 
00083 */
00084   //PAGEBREAK: 13
00085   default:
00086     if(proc == 0 || (tf->cs&3) == 0){
00087       // In kernel, it must be our mistake.
00088       cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n",
00089               tf->trapno, cpu->id, tf->eip, rcr2());
00090       panic("trap");
00091     }
00092     // In user space, assume process misbehaved.
00093     cprintf("pid %d %s: trap %d err %d on cpu %d "
00094             "eip 0x%x addr 0x%x--kill proc\n",
00095             proc->pid, proc->name, tf->trapno, tf->err, cpu->id, tf->eip, 
00096             rcr2());
00097     proc->killed = 1;
00098   }
00099 
00100   // Force process exit if it has been killed and is in user space.
00101   // (If it is still executing in the kernel, let it keep running 
00102   // until it gets to the regular system call return.)
00103   if(proc && proc->killed && (tf->cs&3) == DPL_USER)
00104     exit(1);
00105 
00106   // Force process to give up CPU on clock tick.
00107   // If interrupts were on while locks held, would need to check nlock.
00108   if(proc && proc->state == RUNNING && tf->trapno == T_IRQ0+IRQ_TIMER)
00109     yield();
00110 
00111   // Check if the process has been killed since we yielded
00112   if(proc && proc->killed && (tf->cs&3) == DPL_USER)
00113     exit(1);
00114 }
 All Data Structures