-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrap.c
79 lines (69 loc) · 1.71 KB
/
trap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <sys/types.h>
#include <xv6/param.h>
#include "defs.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "gaia.h"
#include "traps.h"
#include "spinlock.h"
struct spinlock tickslock;
uint ticks;
extern void alltraps();
void
trapinit(void)
{
*(int*)INTHANDLER = (int)alltraps;
initlock(&tickslock, "time");
}
void
trap(struct trapframe *tf)
{
tf->trapno = readtrapno();
tf->retaddr = readtreturn();
if(tf->trapno == T_SYSCALL){
if(proc->killed)
exit();
proc->tf = tf;
syscall();
if(proc->killed)
exit();
return;
}
switch(tf->trapno){
case T_TIMER:
if(cpu->id == 0){
acquire(&tickslock);
ticks++;
wakeup(&ticks);
release(&tickslock);
}
break;
case T_COM1:
uartintr();
break;
default:
if(proc == 0 || cpu->privilege == PL_KERN){
// In kernel, it must be our mistake.
cprintf("unexpected trap %d from cpu %d pc 0x%x\n",
tf->trapno, cpu->id, tf->retaddr);
panic("trap");
}
// In user space, assume process misbehaved.
cprintf("pid %d %s: trap %d pc 0x%x --kill proc\n",
proc->pid, proc->name, tf->trapno, tf->retaddr);
proc->killed = 1;
}
// 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(proc && proc->killed)
exit();
// Force process to give up CPU on clock tick.
// If interrupts were on while locks held, would need to check nlock.
if(proc && proc->state == RUNNING && tf->trapno == T_TIMER)
yield();
// Check if the process has been killed since we yielded
if(proc && proc->killed)
exit();
}