-
Notifications
You must be signed in to change notification settings - Fork 6
/
trap.c
152 lines (136 loc) · 4.13 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "types.h"
#include "defs.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
#include "proc.h"
#include "x86.h"
#include "traps.h"
#include "spinlock.h"
// Interrupt descriptor table (shared by all CPUs).
struct gatedesc idt[256];
extern uint vectors[]; // in vectors.S: array of 256 entry pointers
struct spinlock tickslock;
uint ticks;
void
tvinit(void)
{
int i;
for(i = 0; i < 256; i++)
SETGATE(idt[i], 0, SEG_KCODE<<3, vectors[i], 0);
// User syscalls get the user DPL; see xv6 book p. 42
SETGATE(idt[T_SYSCALL], 1, SEG_KCODE<<3, vectors[T_SYSCALL], DPL_USER);
initlock(&tickslock, "time");
}
void
idtinit(void)
{
lidt(idt, sizeof(idt));
}
//PAGEBREAK: 41
void
trap(struct trapframe *tf)
{
if(tf->trapno == T_SYSCALL){
if(proc->killed)
exit();
proc->tf = tf;
syscall();
if(proc->killed)
exit();
return;
}
char *mem;
switch(tf->trapno){
case T_IRQ0 + IRQ_TIMER:
if(cpunum() == 0){
acquire(&tickslock);
ticks++;
wakeup(&ticks);
release(&tickslock);
}
// Per-process ticking, alarm handling, for sys_alarm.
if (proc && (tf->cs & 3) == DPL_USER) {
proc->elapsed_ticks++;
if (proc->alarm_ticks && proc->elapsed_ticks >= proc->alarm_ticks) {
// Trapframe contains the user-prog's eip and esp
// at time of interrupt. We modify those values as if
// the user-prog had called its alarm handler immediately
// before the interrupt. It's hella sketchy to let the kernel
// jump to a user-specified address--we should check that it's
// within the bounds of the proc's address space.
// Place the original user-prog return address on
// the user's stack, as if the user-prog issued a `call`
// instruction.
tf->esp -= 4;
*(uint*)(tf->esp) = tf->eip;
// Make trapret return to the alarmhandler instead
// of the user-prog. When the alarmhandler calls `ret`,
// it will jump to the original user-prog return address
// we placed on the stack above.
tf->eip = (uint)proc->alarm_fn;
proc->elapsed_ticks = 0;
}
}
lapiceoi();
break;
case T_IRQ0 + IRQ_IDE: // 46
ideintr();
lapiceoi();
break;
case T_IRQ0 + IRQ_IDE+1:
// Bochs generates spurious IDE1 interrupts.
break;
case T_IRQ0 + IRQ_KBD:
kbdintr();
lapiceoi();
break;
case T_IRQ0 + IRQ_COM1:
uartintr();
lapiceoi();
break;
case T_IRQ0 + 7:
case T_IRQ0 + IRQ_SPURIOUS:
cprintf("cpu%d: spurious interrupt at %x:%x\n",
cpunum(), tf->cs, tf->eip);
lapiceoi();
break;
case T_PGFLT:
// TODO: Check that the PFLA isn't in the guard page below the stack.
cprintf("PFLT at: %x\n", rcr2());
if ((mem = kalloc()) == 0)
panic("page fault handler OOM\n");
memset(mem, 0, PGSIZE);
if(mappages(proc->pgdir, (char*)PGROUNDDOWN(rcr2()), PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){
kfree(mem);
panic("page fault handler OOM (2)\n");
}
break;
//PAGEBREAK: 13
default:
if(proc == 0 || (tf->cs&3) == 0){
// In kernel, it must be our mistake.
cprintf("unexpected trap %d from cpu %d eip %x (cr2=0x%x)\n",
tf->trapno, cpunum(), tf->eip, rcr2());
panic("trap");
}
// In user space, assume process misbehaved.
cprintf("pid %d %s: trap %d err %d on cpu %d "
"eip 0x%x addr 0x%x--kill proc\n",
proc->pid, proc->name, tf->trapno, tf->err, cpunum(), tf->eip,
rcr2());
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 on line 44 above.)
if(proc && proc->killed && (tf->cs&3) == DPL_USER)
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_IRQ0+IRQ_TIMER)
yield();
// Check again if the process has been killed since we yielded
if(proc && proc->killed && (tf->cs&3) == DPL_USER)
exit();
}