-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscv_insts_capstone_ctrflow.sail
444 lines (357 loc) · 14.1 KB
/
riscv_insts_capstone_ctrflow.sail
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* Control-flow instructions */
mapping clause encdec = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CALL) <-> 0b0100000 @ rs2 @ rs1 @ 0b001 @ rd @ 0b1011011
/* FIXME: all the below implementations need to be atomic with respect to exception handling */
function clause execute (CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CALL)) = {
/* PC address for resuming execution */
let cur_pc_cap = cap_set_cursor(PCCap, nextPC);
let rd_v : regtype = X(rd);
let rs1_v : regtype = X(rs1);
let ra_v : regtype = x1;
if not(capstone_in_secure_world()) then {
handle_illegal();
return RETIRE_FAIL;
};
match rs1_v {
CapVal(c) => {
let (nid, metadata) = decode_cap(c);
if capnode_query(nid) == 0b0 then {
handle_capstone_except(E_Invalid_Cap());
RETIRE_FAIL
} else
match metadata {
SealedCapMetadata(base_addr, async) if async == 0b00 => {
/* Swap pc */
let new_pc_cap_v = capstone_mem_xchg(base_addr, 0, CapVal(cur_pc_cap));
capstone_redirect(new_pc_cap_v);
/* Swap ceh */
ceh = capstone_mem_xchg(base_addr, 1, ceh);
/* Swap csp */
x2 = capstone_mem_xchg(base_addr, 2, x2);
X(rs1) = reg_moved(rs1_v);
reg_overwrite(ra_v);
let new_ra_cap_metadata = SealedRetCapMetadata(base_addr, base_addr, 0b00, rd) in
x1 = CapVal(encode_cap((nid, new_ra_cap_metadata)));
RETIRE_SUCCESS
},
_ => {
handle_capstone_except(E_Cap_Type_Fault());
RETIRE_FAIL
}
}
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CALL)
<-> "call" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)
/************************************************************/
mapping clause encdec = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_RETURN) <-> 0b0100001 @ rs2 @ rs1 @ 0b001 @ rd @ 0b1011011
function clause execute (CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_RETURN)) = {
let cur_pc_cap = PCCap;
let cur_npc = nextPC; /* PC address for resuming execution */
let cur_sp = x2; /* current stack pointer */
let rs1_v : regtype = X(rs1);
let rs2_v : regtype = X(rs2);
if not(capstone_in_secure_world()) then {
handle_illegal();
return RETIRE_FAIL;
};
match (rs1_v, rs2_v) {
(CapVal(c), RawVal(v)) => {
let (nid, metadata) = decode_cap(c);
if capnode_query(nid) == 0b0 then {
handle_capstone_except(E_Invalid_Cap());
return RETIRE_FAIL;
};
match metadata {
SealedRetCapMetadata(cursor, base_addr, async, ret_reg) => {
match async {
0b00 => {
let new_sealed_pc_cap = cap_set_cursor(cur_pc_cap, v);
/* Swap pc */
let new_pc_cap_v = capstone_mem_xchg(base_addr, 0, CapVal(new_sealed_pc_cap));
capstone_redirect(new_pc_cap_v);
/* Swap ceh */
ceh = capstone_mem_xchg(base_addr, 1, ceh);
/* Swap csp */
x2 = capstone_mem_xchg(base_addr, 2, x2);
reg_overwrite(X(ret_reg));
let new_sealed_cap = SealedCapMetadata(base_addr, 0b00) in
X(ret_reg) = CapVal(encode_cap((nid, new_sealed_cap)));
RETIRE_SUCCESS
},
0b01 => {
let new_sealed_pc_cap = cap_set_cursor(cur_pc_cap, v);
/* Swap pc */
let new_pc_cap_v = capstone_mem_xchg(base_addr, 0, CapVal(new_sealed_pc_cap));
match new_pc_cap_v {
CapVal(new_pc_cap) => {
capstone_redirect_to_cap(new_pc_cap);
},
_ => {
handle_illegal();
return RETIRE_FAIL;
}
};
/* Store ceh */
assert(capstone_store_mem(base_addr + to_bits(xlen_val, caplen_bytes_v), caplen_bytes_v, ceh));
ceh = CapVal(encode_cap((nid, SealedCapMetadata(base_addr, 0b00))));
X(rs1) = reg_moved(rs1_v);
/* Swap other GPRs */
reg_idx : int = 1;
while reg_idx < 32 do {
X(to_bits(5, reg_idx)) = capstone_mem_xchg(base_addr, reg_idx + 1, X(to_bits(5, reg_idx)));
reg_idx = reg_idx + 1;
};
RETIRE_SUCCESS
},
0b10 => {
let new_sealed_pc_cap = cap_set_cursor(cur_pc_cap, v);
/* Swap pc */
let new_pc_cap_v = capstone_mem_xchg(base_addr, 0, CapVal(new_sealed_pc_cap));
match new_pc_cap_v {
CapVal(new_pc_cap) => {
capstone_redirect_to_cap(new_pc_cap);
},
_ => {
handle_illegal();
return RETIRE_FAIL;
}
};
/* Swap ceh */
ceh = capstone_mem_xchg(base_addr, 1, ceh);
/* Set cih */
cih = CapVal(encode_cap((nid, SealedCapMetadata(base_addr, 0b00))));
X(rs1) = reg_moved(rs1_v);
/* Swap other GPRs */
reg_idx : int = 1;
while reg_idx < 32 do {
X(to_bits(5, reg_idx)) = capstone_mem_xchg(base_addr, reg_idx + 1, X(to_bits(5, reg_idx)));
reg_idx = reg_idx + 1;
};
RETIRE_SUCCESS
},
_ => {
assert(false);
RETIRE_FAIL
}
}
},
_ => {
handle_capstone_except(E_Cap_Type_Fault());
RETIRE_FAIL
}
}
},
(RawVal(vv), RawVal(v)) if vv == EXTZ(0b0) => {
let new_ceh_cap = cap_set_cursor(cur_pc_cap, v);
capstone_redirect_to_cap(cepc);
cepc = CAP_INVALID;
RETIRE_SUCCESS
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_RETURN)
<-> "return" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)
/************************************************************/
mapping clause encdec = CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CJALR) <-> imm @ rs1 @ 0b101 @ rd @ 0b1011011
function clause execute (CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CJALR)) = {
let rd_v : regtype = X(rd);
let rs1_v : regtype = X(rs1);
if capstone_in_normal_world() then {
handle_illegal();
return RETIRE_FAIL;
};
match rs1_v {
/* we don't check the permission or the type here. invalid values will trigger
faults later */
CapVal(c) => {
let old_pc_cap = PCCap;
let old_npc = nextPC;
reg_overwrite(rd_v);
X(rs1) = reg_moved(rs1_v);
capstone_redirect_to_cap(c);
X(rd) = CapVal(cap_set_cursor(old_pc_cap, old_npc)); /* save the original next PC */
RETIRE_SUCCESS
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CJALR)
<-> "cjalr" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_12(imm)
/************************************************************/
mapping clause encdec = CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CBNZ) <-> imm @ rs1 @ 0b110 @ rd @ 0b1011011
function clause execute (CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CBNZ)) = {
let rd_v : regtype = X(rd);
let rs1_v : regtype = X(rs1);
if capstone_in_normal_world() then {
handle_illegal();
return RETIRE_FAIL;
};
match(rd_v, rs1_v) {
(CapVal(c), RawVal(v)) => {
if(v != EXTZ(0b0)) then {
reg_overwrite(CapVal(PCCap));
X(rd) = reg_moved(rd_v);
capstone_redirect_to_cap(c);
};
RETIRE_SUCCESS
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_I(rs1, rd, imm, CAPSTONE_CBNZ)
<-> "cbnz" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ hex_bits_12(imm)
/************************************************************/
mapping clause encdec = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPENTER) <-> 0b0100010 @ rs2 @ rs1 @ 0b001 @ rd @ 0b1011011
function clause execute (CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPENTER)) = {
let cur_npc = nextPC; /* PC address for resuming execution */
let cur_sp = x2; /* current stack pointer */
if capstone_in_secure_world() then {
handle_illegal();
return RETIRE_FAIL;
};
let rs1_v : regtype = X(rs1);
match rs1_v {
CapVal(c) => {
let (nid, metadata) = decode_cap(c);
if capnode_query(nid) == 0b0 then {
handle_capstone_except(E_Invalid_Cap());
return RETIRE_FAIL;
};
match metadata {
SealedCapMetadata(base_addr, async) => {
if async == 0b00 then {
/* store the pc and sp values */
normal_pc = cur_npc;
normal_sp = cur_sp;
/* load pc */
let new_pc = capstone_mem_load(base_addr, 0);
capstone_redirect(new_pc);
/* load ceh */
ceh = capstone_mem_load(base_addr, 1);
/* load sp */
x2 = capstone_mem_load(base_addr, 2);
/* set ra */
let new_ra_cp_metadata = ExitCapMetadata(base_addr, base_addr) in
x1 = CapVal(encode_cap((nid, new_ra_cp_metadata)));
/* set switch_reg and exit_reg */
switch_reg = rs1;
exit_reg = rd;
capstone_enter_secure_world();
RETIRE_SUCCESS
} else if async == 0b01 | async == 0b10 then {
/* store the pc and sp values */
normal_pc = cur_npc;
normal_sp = cur_sp;
/* load pc */
let new_pc = capstone_mem_load(base_addr, 0);
capstone_redirect(new_pc);
/* load ceh */
ceh = capstone_mem_load(base_addr, 1);
/* load other GPRs */
reg_idx : int = 1;
while reg_idx < 32 do {
X(to_bits(5, reg_idx)) = capstone_mem_load(base_addr, reg_idx + 1);
reg_idx = reg_idx + 1;
};
/* set switch_cap */
let new_switch_cap_metadata = UninitialisedCapMetadata(base_addr, base_addr, base_addr + to_bits(xlen_val, 33 * caplen_bytes_v), CAP_PERM_RW) in
switch_cap = encode_cap((nid, new_switch_cap_metadata));
X(rs1) = RawVal(EXTZ(0b0));
/* set switch_reg and exit_reg */
switch_reg = rs1;
exit_reg = rd;
capstone_enter_secure_world();
RETIRE_SUCCESS
} else {
assert(false);
RETIRE_FAIL
}
},
_ => {
handle_capstone_except(E_Cap_Type_Fault());
RETIRE_FAIL
}
}
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPENTER)
<-> "capenter" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)
/************************************************************/
/* capexit does the following
1. recover the PC and stack top from the special registers
2. (optional) seal the current context
3. switch the CPU mode to the normal world
Note that the capexit instruction does not restore the full context.
*/
mapping clause encdec = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPEXIT) <-> 0b0100011 @ rs2 @ rs1 @ 0b001 @ rd @ 0b1011011
function clause execute (CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPEXIT)) = {
let cur_pc_cap = PCCap;
let cur_npc = nextPC; /* PC address for resuming execution */
let cur_sp = x2; /* current stack pointer */
if capstone_in_normal_world() then {
handle_illegal();
return RETIRE_FAIL;
};
let rs1_v : regtype = X(rs1);
let rs2_v : regtype = X(rs2);
match (rs1_v, rs2_v) {
(CapVal(c), RawVal(v)) => {
let (nid, metadata) = decode_cap(c);
if capnode_query(nid) == 0b0 then {
handle_capstone_except(E_Invalid_Cap());
return RETIRE_FAIL;
};
match metadata {
ExitCapMetadata(cursor, base_addr) => {
/* store pc with modified cursor */
let new_sealed_pc_cap = cap_set_cursor(cur_pc_cap, v) in
assert(capstone_mem_store(base_addr, 0, CapVal(new_sealed_pc_cap)));
/* store ceh */
assert(capstone_mem_store(base_addr, 1, ceh));
/* store sp */
assert(capstone_mem_store(base_addr, 2, cur_sp));
/* load new pc and sp */
capstone_redirect_to_addr(normal_pc);
x2 = normal_sp;
/* write back to switch_reg */
let new_sealed_cap_metadata = SealedCapMetadata(base_addr, 0b00) in
X(switch_reg) = CapVal(encode_cap((nid, new_sealed_cap_metadata)));
/* write exit code */
X(exit_reg) = RawVal(EXTZ(0b0));
capstone_enter_normal_world();
RETIRE_SUCCESS
},
_ => {
handle_capstone_except(E_Cap_Type_Fault());
RETIRE_FAIL
}
}
},
_ => {
handle_capstone_except(E_Op_Type_Fault());
RETIRE_FAIL
}
}
}
mapping clause assembly = CAPSTONE_INSN_R(rs2, rs1, rd, CAPSTONE_CAPEXIT)
<-> "capexit" ^ spc() ^ reg_name(rd) ^ sep() ^ reg_name(rs1) ^ sep() ^ reg_name(rs2)