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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
#include "mtrap.h"
#include "frontend.h"
#include "mcall.h"
#include "vm.h"
#include <errno.h>
void illegal_insn_trap(uintptr_t* regs, uintptr_t mcause, uintptr_t mepc)
{
asm (".pushsection .rodata\n"
"illegal_insn_trap_table:\n"
" .word truly_illegal_insn\n"
#ifdef PK_ENABLE_FP_EMULATION
" .word emulate_float_load\n"
#else
" .word truly_illegal_insn\n"
#endif
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
#ifdef PK_ENABLE_FP_EMULATION
" .word emulate_float_store\n"
#else
" .word truly_illegal_insn\n"
#endif
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word emulate_mul_div\n"
" .word truly_illegal_insn\n"
" .word emulate_mul_div32\n"
" .word truly_illegal_insn\n"
#ifdef PK_ENABLE_FP_EMULATION
" .word emulate_fmadd\n"
" .word emulate_fmadd\n"
" .word emulate_fmadd\n"
" .word emulate_fmadd\n"
" .word emulate_fp\n"
#else
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
#endif
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
#ifdef PK_ENABLE_FP_EMULATION
" .word emulate_system\n"
#else
" .word truly_illegal_insn\n"
#endif
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .word truly_illegal_insn\n"
" .popsection");
uintptr_t mstatus = read_csr(mstatus);
insn_t insn = get_insn(mepc);
if ((insn & 3) != 3)
return truly_illegal_insn(regs, mcause, mepc, mstatus, insn);
write_csr(mepc, mepc + 4);
extern int32_t illegal_insn_trap_table[];
int32_t* pf = (void*)illegal_insn_trap_table + (insn & 0x7c);
emulation_func f = (emulation_func)(uintptr_t)*pf;
f(regs, mcause, mepc, mstatus, insn);
}
void __attribute__((noreturn)) bad_trap()
{
panic("machine mode: unhandlable trap %d @ %p", read_csr(mcause), read_csr(mepc));
}
void htif_interrupt()
{
uintptr_t fromhost = swap_csr(mfromhost, 0);
if (!fromhost)
return;
uintptr_t dev = FROMHOST_DEV(fromhost);
uintptr_t cmd = FROMHOST_CMD(fromhost);
uintptr_t data = FROMHOST_DATA(fromhost);
sbi_device_message* m = HLS()->device_request_queue_head;
sbi_device_message* prev = NULL;
for (size_t i = 0, n = HLS()->device_request_queue_size; i < n; i++) {
if (!supervisor_paddr_valid(m, sizeof(*m))
&& EXTRACT_FIELD(read_csr(mstatus), MSTATUS_MPP) != PRV_M)
panic("htif: page fault");
sbi_device_message* next = (void*)m->sbi_private_data;
if (m->dev == dev && m->cmd == cmd) {
m->data = data;
// dequeue from request queue
if (prev)
prev->sbi_private_data = (uintptr_t)next;
else
HLS()->device_request_queue_head = next;
HLS()->device_request_queue_size = n-1;
m->sbi_private_data = 0;
// enqueue to response queue
if (HLS()->device_response_queue_tail)
HLS()->device_response_queue_tail->sbi_private_data = (uintptr_t)m;
else
HLS()->device_response_queue_head = m;
HLS()->device_response_queue_tail = m;
// signal software interrupt
set_csr(mip, MIP_SSIP);
return;
}
prev = m;
m = (void*)atomic_read(&m->sbi_private_data);
}
panic("htif: no record");
}
static uintptr_t mcall_hart_id()
{
return HLS()->hart_id;
}
static uintptr_t mcall_console_putchar(uint8_t ch)
{
while (swap_csr(mtohost, TOHOST_CMD(1, 1, ch)) != 0);
while (1) {
uintptr_t fromhost = read_csr(mfromhost);
if (FROMHOST_DEV(fromhost) != 1 || FROMHOST_CMD(fromhost) != 1) {
if (fromhost)
htif_interrupt();
continue;
}
write_csr(mfromhost, 0);
break;
}
return 0;
}
void printm(const char* s, ...)
{
char buf[128];
va_list vl;
va_start(vl, s);
vsnprintf(buf, sizeof buf, s, vl);
va_end(vl);
char* p = buf;
while (*p)
mcall_console_putchar(*p++);
}
static uintptr_t mcall_dev_req(sbi_device_message *m)
{
//printm("req %d %p\n", HLS()->device_request_queue_size, m);
if (!supervisor_paddr_valid(m, sizeof(*m))
&& EXTRACT_FIELD(read_csr(mstatus), MSTATUS_MPP) != PRV_M)
return -EFAULT;
if ((m->dev > 0xFFU) | (m->cmd > 0xFFU) | (m->data > 0x0000FFFFFFFFFFFFU))
return -EINVAL;
while (swap_csr(mtohost, TOHOST_CMD(m->dev, m->cmd, m->data)) != 0)
;
m->sbi_private_data = (uintptr_t)HLS()->device_request_queue_head;
HLS()->device_request_queue_head = m;
HLS()->device_request_queue_size++;
return 0;
}
static uintptr_t mcall_dev_resp()
{
htif_interrupt();
sbi_device_message* m = HLS()->device_response_queue_head;
if (m) {
//printm("resp %p\n", m);
sbi_device_message* next = (void*)atomic_read(&m->sbi_private_data);
HLS()->device_response_queue_head = next;
if (!next) {
HLS()->device_response_queue_tail = 0;
// only clear SSIP if no other events are pending
clear_csr(mip, MIP_SSIP);
mb();
if (HLS()->ipi_pending)
set_csr(mip, MIP_SSIP);
}
}
return (uintptr_t)m;
}
static uintptr_t mcall_send_ipi(uintptr_t recipient)
{
if (recipient >= num_harts)
return -1;
if (atomic_swap(&OTHER_HLS(recipient)->ipi_pending, 1) == 0) {
mb();
OTHER_HLS(recipient)->csrs[CSR_MIPI] = 1;
}
return 0;
}
static uintptr_t mcall_clear_ipi()
{
// only clear SSIP if no other events are pending
if (HLS()->device_response_queue_head == NULL) {
clear_csr(mip, MIP_SSIP);
mb();
}
return atomic_swap(&HLS()->ipi_pending, 0);
}
static uintptr_t mcall_shutdown()
{
while (1)
write_csr(mtohost, 1);
return 0;
}
static uintptr_t mcall_set_timer(unsigned long long when)
{
// bbl/pk don't use the timer, so there's no need to virtualize it
write_csr(mtimecmp, when);
#ifndef __riscv64
write_csr(mtimecmph, when >> 32);
#endif
clear_csr(mip, MIP_STIP);
set_csr(mie, MIP_MTIP);
return 0;
}
void mcall_trap(uintptr_t* regs, uintptr_t mcause, uintptr_t mepc)
{
uintptr_t n = regs[17], arg0 = regs[10], retval;
switch (n)
{
case MCALL_HART_ID:
retval = mcall_hart_id();
break;
case MCALL_CONSOLE_PUTCHAR:
retval = mcall_console_putchar(arg0);
break;
case MCALL_SEND_DEVICE_REQUEST:
retval = mcall_dev_req((sbi_device_message*)arg0);
break;
case MCALL_RECEIVE_DEVICE_RESPONSE:
retval = mcall_dev_resp();
break;
case MCALL_SEND_IPI:
retval = mcall_send_ipi(arg0);
break;
case MCALL_CLEAR_IPI:
retval = mcall_clear_ipi();
break;
case MCALL_SHUTDOWN:
retval = mcall_shutdown();
break;
case MCALL_SET_TIMER:
retval = mcall_set_timer(arg0);
break;
default:
retval = -ENOSYS;
break;
}
regs[10] = retval;
write_csr(mepc, mepc + 4);
}
static void machine_page_fault(uintptr_t* regs, uintptr_t mepc)
{
// See if this trap occurred when emulating an instruction on behalf of
// a lower privilege level.
extern int32_t unprivileged_access_ranges[];
extern int32_t unprivileged_access_ranges_end[];
int32_t* p = unprivileged_access_ranges;
do {
if (mepc >= p[0] && mepc < p[1]) {
// Yes. Redirect the trap to the supervisor.
write_csr(sbadaddr, read_csr(mbadaddr));
redirect_trap(regs[14], regs[5]);
return;
}
p += 2;
} while (p < unprivileged_access_ranges_end);
// No. We're boned.
bad_trap();
}
void trap_from_machine_mode(uintptr_t* regs, uintptr_t dummy, uintptr_t mepc)
{
uintptr_t mcause = read_csr(mcause);
// restore mscratch, since we clobbered it.
write_csr(mscratch, MACHINE_STACK_TOP() - MENTRY_FRAME_SIZE);
switch (mcause)
{
case CAUSE_FAULT_LOAD:
case CAUSE_FAULT_STORE:
return machine_page_fault(regs, mepc);
case CAUSE_ILLEGAL_INSTRUCTION:
return bad_trap();
case CAUSE_MACHINE_ECALL:
return mcall_trap(regs, dummy, mepc);
default:
bad_trap();
}
}
|