blob: 3e732139c06417b61d3da6c82f407ade86b64f3d (
plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include "kernel/trap.h"
#include "kernel/riscv.h"
#include "kernel/proc.h"
#include "drivers/uart.h"
#include "kernel/syscall.h"
#include "kernel/mm.h"
#include "kernel/timer.h"
#include "drivers/plic.h"
#include "drivers/virtio.h"
#include "lib/string.h"
extern "C" void kernelvec();
// Handle Function
void panic(const char *s)
{
Drivers::uart_puts("\n\n!!! KERNEL PANIC !!!\n");
Drivers::uart_puts(s);
Drivers::uart_puts("\nSystem Halted.\n");
while (1)
;
}
// Trap Implementation
namespace Trap
{
void init()
{
Drivers::uart_puts("[Trap] Global init done.\n");
}
// Each CPU core needs to call this function when starting to set its own stvec
void inithart()
{
w_stvec((uint64)kernelvec);
// Ensure that the S-Mode interrupt enable bit (SIE) is cleared during initialization
// Interrupt enabling will be done by the scheduler when the first process runs
w_sstatus(r_sstatus() & ~SSTATUS_SIE);
// Enable the external interrupt, clock interrupt, and software interrupt bits in S-Mode (in SIE register)
uint64 sie = r_sie();
sie |= SIE_SEIE | SIE_STIE | SIE_SSIE;
w_sie(sie);
Drivers::uart_puts("[Trap] Hart initialized (stvec set).\n");
}
// Device Interrupt Check
int devintr()
{
uint64 scause = r_scause();
bool is_interrupt = (scause & 0x8000000000000000L) != 0;
uint64 code = scause & 0xff;
if (is_interrupt && code == 9)
{
// Supervisor External Interrupt (IRQ 9)
int irq = PLIC::claim();
if (irq == 10)
{
Drivers::uart_intr();
// Debug
// Drivers::uart_puts("[Interrupt] UART Input detected!\n");
}
else if (irq >= 1 && irq <= 8)
{
VirtIO::intr();
// Debug
// Drivers::uart_puts("[Interrupt] VirtIO disk finished!\n");
}
if (irq > 0)
{
PLIC::complete(irq);
}
return 1;
}
else if (is_interrupt && code == 5)
{
// Supervisor Timer Interrupt (IRQ 5)
Timer::set_next_trigger();
// w_sip(r_sip() & ~2);
return 2;
}
return 0;
}
}
// Exception Handling Entry
extern "C" void kerneltrap(struct Trapframe *tf)
{
uint64 sepc = r_sepc();
uint64 sstatus = r_sstatus();
uint64 scause = r_scause();
if ((sstatus & SSTATUS_SPP) == 0)
panic("kerneltrap: not from supervisor mode");
if (intr_get() != 0)
panic("kerneltrap: interrupts enabled");
if (Trap::devintr() == 0)
{
Drivers::uart_puts("\n--- KERNEL EXCEPTION ---\n");
Drivers::uart_puts("scause: ");
Drivers::print_hex(scause);
Drivers::uart_puts("\n");
Drivers::uart_puts("sepc: ");
Drivers::print_hex(sepc);
Drivers::uart_puts("\n");
Drivers::uart_puts("stval: ");
Drivers::print_hex(r_stval());
Drivers::uart_puts("\n");
panic("kerneltrap exception");
}
w_sepc(sepc);
w_sstatus(sstatus);
}
extern "C" void usertrap()
{
// Drivers::uart_puts("[Debug] Entered usertrap!\n");
int which_dev = 0;
if ((r_sstatus() & SSTATUS_SPP) != 0)
panic("usertrap: not from user mode");
w_stvec((uint64)kernelvec);
struct Proc *p = myproc();
if (!p)
panic("usertrap: no process");
p->tf->epc = r_sepc();
uint64 scause = r_scause();
uint64 stval = r_stval();
if (scause == 8) // Syscall
{
if (p->state == ZOMBIE)
ProcManager::exit(-1);
p->tf->epc += 4;
intr_on();
syscall();
intr_off();
}
else if (scause == 15) // Store Page Fault
{
uint64 pte = VM::walkaddr(p->pagetable, stval);
// Drivers::uart_puts("[Debug] Faulting PTE:");
// Drivers::print_hex(pte);
// Drivers::uart_puts("\n");
if (VM::handle_cow_fault(p->pagetable, PGROUNDDOWN(stval)) != 0)
{
Drivers::uart_puts("usertrap: cow failed at ");
Drivers::print_hex(stval);
Drivers::uart_puts("\n");
p->state = ZOMBIE;
ProcManager::exit(-1);
}
}
else if ((which_dev = Trap::devintr()) != 0)
{
if (which_dev == 2)
{
ProcManager::yield();
}
}
else
{
Drivers::uart_puts("\nusertrap(): unexpected scause ");
Drivers::print_hex(scause);
Drivers::uart_puts(" pid=");
Drivers::uart_put_int(p->pid);
Drivers::uart_puts(" sepc=");
Drivers::print_hex(p->tf->epc);
Drivers::uart_puts(" stval=");
Drivers::print_hex(stval);
Drivers::uart_puts("\n");
p->state = ZOMBIE;
ProcManager::exit(-1);
}
if (p->state == ZOMBIE)
ProcManager::exit(-1);
usertrapret();
}
|