-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
emulation.c
406 lines (372 loc) · 13.4 KB
/
emulation.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "serial.h"
#include "csr.h"
#include "exception.h"
#include "assert.h"
#include "emulation.h"
extern void isr_vector(void);
//-----------------------------------------------------------------
// Defines:
//-----------------------------------------------------------------
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#define INSN_MATCH_LH 0x1003
#define INSN_MASK_LH 0x707f
#define INSN_MATCH_LW 0x2003
#define INSN_MASK_LW 0x707f
#define INSN_MATCH_LHU 0x5003
#define INSN_MASK_LHU 0x707f
#define INSN_MATCH_LWU 0x6003
#define INSN_MASK_LWU 0x707f
#define INSN_MATCH_SH 0x1023
#define INSN_MASK_SH 0x707f
#define INSN_MATCH_SW 0x2023
#define INSN_MASK_SW 0x707f
//-----------------------------------------------------------------
// Locals
//-----------------------------------------------------------------
static uint32_t load_reservation = 0;
//-----------------------------------------------------------------
// emulation_read_byte: Read byte with fault catching
//-----------------------------------------------------------------
static int32_t emulation_read_byte(uint32_t address, uint8_t *data)
{
int32_t result, tmp;
int32_t fail;
__asm__ __volatile__ (
" li %[tmp], 0x00020000\n" \
" csrs mstatus, %[tmp]\n" \
" la %[tmp], 1f\n" \
" csrw mtvec, %[tmp]\n" \
" li %[fail], 1\n" \
" lbu %[result], 0(%[address])\n"
" li %[fail], 0\n" \
"1:\n" \
" li %[tmp], 0x00020000\n" \
" csrc mstatus, %[tmp]\n" \
: [result]"=&r" (result), [fail]"=&r" (fail), [tmp]"=&r" (tmp)
: [address]"r" (address)
: "memory"
);
*data = result;
return fail;
}
//-----------------------------------------------------------------
// emulation_read_word: Read word with fault catching
//-----------------------------------------------------------------
static int32_t emulation_read_word(uint32_t address, int32_t *data)
{
int32_t result, tmp;
int32_t fail;
__asm__ __volatile__ (
" li %[tmp], 0x00020000\n" \
" csrs mstatus, %[tmp]\n" \
" la %[tmp], 1f\n" \
" csrw mtvec, %[tmp]\n" \
" li %[fail], 1\n" \
" lw %[result], 0(%[address])\n"
" li %[fail], 0\n" \
"1:\n" \
" li %[tmp], 0x00020000\n" \
" csrc mstatus, %[tmp]\n" \
: [result]"=&r" (result), [fail]"=&r" (fail), [tmp]"=&r" (tmp)
: [address]"r" (address)
: "memory"
);
*data = result;
return fail;
}
//-----------------------------------------------------------------
// emulation_write_byte: Write byte with fault catching
//-----------------------------------------------------------------
static int32_t emulation_write_byte(uint32_t address, uint8_t data)
{
int32_t tmp;
int32_t fail;
__asm__ __volatile__ (
" li %[tmp], 0x00020000\n" \
" csrs mstatus, %[tmp]\n" \
" la %[tmp], 1f\n" \
" csrw mtvec, %[tmp]\n" \
" li %[fail], 1\n" \
" sb %[data], 0(%[address])\n"
" li %[fail], 0\n" \
"1:\n" \
" li %[tmp], 0x00020000\n" \
" csrc mstatus, %[tmp]\n" \
: [fail]"=&r" (fail), [tmp]"=&r" (tmp)
: [address]"r" (address), [data]"r" (data)
: "memory"
);
return fail;
}
//-----------------------------------------------------------------
// emulation_write_word: Write word with fault catching
//-----------------------------------------------------------------
static int32_t emulation_write_word(uint32_t address, int32_t data)
{
int32_t tmp;
int32_t fail;
__asm__ __volatile__ (
" li %[tmp], 0x00020000\n" \
" csrs mstatus, %[tmp]\n" \
" la %[tmp], 1f\n" \
" csrw mtvec, %[tmp]\n" \
" li %[fail], 1\n" \
" sw %[data], 0(%[address])\n"
" li %[fail], 0\n" \
"1:\n" \
" li %[tmp], 0x00020000\n" \
" csrc mstatus, %[tmp]\n" \
: [fail]"=&r" (fail), [tmp]"=&r" (tmp)
: [address]"r" (address), [data]"r" (data)
: "memory"
);
return fail;
}
//-----------------------------------------------------------------
// emulation_trap_to_supervisor: Divert fault to supervisor mode
//-----------------------------------------------------------------
static void emulation_trap_to_supervisor(struct irq_context *ctx, uint32_t sepc, uint32_t mstatus)
{
csr_write(mtvec, isr_vector);
csr_write(0x143, csr_read(0x343)); // mbadaddr/mtval -> sbadaddr/stval
csr_write(scause, csr_read(mcause));
csr_write(sepc, sepc);
// Return to supervisor trap address
ctx->pc = csr_read(stvec);
ctx->status = (mstatus & ~(MSTATUS_SPP | MSTATUS_MPP | MSTATUS_SIE | MSTATUS_SPIE))
| ((mstatus >> 3) & MSTATUS_SPP)
| (0x0800 | MSTATUS_MPIE)
| ((mstatus & MSTATUS_SIE) << 4);
}
//-----------------------------------------------------------------
// trap_invalid_inst: Invalid instruction handler
//-----------------------------------------------------------------
static struct irq_context *trap_invalid_inst(struct irq_context *ctx)
{
uint32_t mepc = ctx->pc;
uint32_t mstatus = ctx->status;
uint32_t instr = csr_read(0x343); // mbadaddr or mtval
uint32_t opcode = instr & 0x7f;
uint32_t funct3 = (instr >> 12) & 0x7;
uint32_t sel = (instr >> 27);
uint32_t rd = (instr >> 7) & 0x1f;
uint32_t rs1 = (instr >> 15) & 0x1f;
uint32_t rs2 = (instr >> 20) & 0x1f;
// LR
if (opcode == 0x2f && funct3 == 0x2 && sel == 0x2)
{
uint32_t addr = ctx->reg[rs1];
int32_t data_read = 0;
// Load
if (emulation_read_word(addr, &data_read))
{
// Load fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
// TODO: This should be on the physical address or take into account ctx switches...
load_reservation = addr;
ctx->reg[rd] = data_read;
}
// SC
else if (opcode == 0x2f && funct3 == 0x2 && sel == 0x3)
{
uint32_t addr = (rs1 != 0) ? ctx->reg[rs1] : 0;
int32_t data_write = (rs2 != 0) ? ctx->reg[rs2] : 0;
if (load_reservation == addr)
{
// Store
if (emulation_write_word(addr, data_write))
{
// Store fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
load_reservation = 0;
ctx->reg[rd] = 0;
}
else
ctx->reg[rd] = 1;
}
// Atomics
else if (opcode == 0x2f)
{
switch(funct3)
{
case 0x2:
{
uint32_t addr = (rs1 != 0) ? ctx->reg[rs1] : 0;
int32_t src = (rs2 != 0) ? ctx->reg[rs2] : 0;
int32_t data_read = 0;
int32_t data_write = 0;
// Load
if (emulation_read_word(addr, &data_read))
{
// Load fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
switch(sel)
{
case 0x0: data_write = src + data_read; break; // amoadd.w
case 0x1: data_write = src; break; // amoswap.w
case 0x4: data_write = src ^ data_read; break; // amoxor.w
case 0xC: data_write = src & data_read; break; // amoand.w
case 0x8: data_write = src | data_read; break; // amoor.w
case 0x10: data_write = min((int32_t)src, (int32_t)data_read); break; // amomin.w
case 0x14: data_write = max((int32_t)src, (int32_t)data_read); break; // amomax.w
case 0x18: data_write = min((uint32_t)src, (uint32_t)data_read); break; // amominu.w
case 0x1C: data_write = max((uint32_t)src, (uint32_t)data_read); break; // amomaxu.w
default: assert(!"error"); break;
}
// Store
if (emulation_write_word(addr, data_write))
{
// Store fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
ctx->reg[rd] = data_read;
} break;
default: assert(!"error"); break;
}
}
else
{
serial_putstr_hex("ERROR: Invalid opcode: ", instr);
serial_putstr_hex(" at PC: ", ctx->pc);
assert(!"error");
}
// Skip faulting instruction
ctx->pc += 4;
// Force MTVEC back to default handler
csr_write(mtvec, isr_vector);
return ctx;
}
//-----------------------------------------------------------------
// trap_misaligned_ld: Unaligned load handler
//-----------------------------------------------------------------
static struct irq_context *trap_misaligned_ld(struct irq_context *ctx)
{
uint32_t mepc = ctx->pc;
uint32_t mstatus = ctx->status;
uint32_t instr = 0;
uint32_t addr = csr_read(0x343); // mbadaddr or mtval
uint32_t data = 0;
int32_t opcode = 0;
uint32_t rd = 0;
int len = 0;
int i;
// Load instruction as mtval contains load / store address
if (emulation_read_word(mepc, &opcode))
{
// Load fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
instr = opcode;
rd = (instr >> 7) & 0x1f;
if ((instr & INSN_MASK_LW) == INSN_MATCH_LW || (instr & INSN_MASK_LWU) == INSN_MATCH_LWU)
len = 4;
else if ((instr & INSN_MASK_LH) == INSN_MATCH_LH || (instr & INSN_MASK_LHU) == INSN_MATCH_LHU)
len = 2;
else
{
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
for (i = 0; i < len; i++)
{
uint8_t data_read;
if (emulation_read_byte(addr + i, &data_read))
{
// Load fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
data |= ((uint32_t)data_read) << (8 * i);
}
// Sign extend LH
if (((instr & INSN_MASK_LH) == INSN_MATCH_LH) && (data & 0x8000))
data |= 0xFFFF0000;
// Write back to target
if (rd != 0)
ctx->reg[rd] = data;
// Skip faulting instruction
ctx->pc += 4;
// Force MTVEC back to default handler
csr_write(mtvec, isr_vector);
return ctx;
}
//-----------------------------------------------------------------
// trap_misaligned_st: Unaligned store handler
//-----------------------------------------------------------------
static struct irq_context *trap_misaligned_st(struct irq_context *ctx)
{
uint32_t mepc = ctx->pc;
uint32_t mstatus = ctx->status;
uint32_t instr = 0;
uint32_t addr = csr_read(0x343); // mbadaddr or mtval
uint32_t data = 0;
int32_t opcode = 0;
uint32_t rs2 = 0;
uint32_t rs2_val = 0;
int len = 0;
int i;
// Load instruction as mtval contains load / store address
if (emulation_read_word(mepc, &opcode))
{
// Load fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
instr = opcode;
rs2 = (instr >> 20) & 0x1f;
rs2_val = (rs2 != 0) ? ctx->reg[rs2] : 0;
if ((instr & INSN_MASK_SW) == INSN_MATCH_SW)
len = 4;
else if ((instr & INSN_MASK_SH) == INSN_MATCH_SH)
len = 2;
else
{
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
for (i = 0; i < len; i++)
{
if (emulation_write_byte(addr + i, rs2_val >> (8 * i)))
{
// Store fault - stop and redirect to supervisor
emulation_trap_to_supervisor(ctx, mepc, mstatus);
return ctx;
}
}
// Skip faulting instruction
ctx->pc += 4;
// Force MTVEC back to default handler
csr_write(mtvec, isr_vector);
return ctx;
}
//-----------------------------------------------------------------
// emulation_init: Configure emulation
//-----------------------------------------------------------------
void emulation_init(void)
{
exception_set_handler(CAUSE_ILLEGAL_INSTRUCTION, trap_invalid_inst);
exception_set_handler(CAUSE_MISALIGNED_LOAD, trap_misaligned_ld);
exception_set_handler(CAUSE_MISALIGNED_STORE, trap_misaligned_st);
}
//-----------------------------------------------------------------
// emulation_take_irq: On interrupt, clear load reservation
//-----------------------------------------------------------------
void emulation_take_irq(void)
{
load_reservation = 0;
}