unicorn-engine 2.1.5

Rust bindings for the Unicorn emulator with utility functions
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use unicorn_engine_sys::RegisterRISCV;

use super::*;

#[test]
fn test_riscv32_nop() {
    let code = [
        0x13, 0x00, 0x00, 0x00, // nop
    ];

    let t0 = 0x1234;
    let t1 = 0x5678;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    let t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    let t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    assert_eq!(t0, 0x1234);
    assert_eq!(t1, 0x5678);
}

#[test]
fn test_riscv64_nop() {
    let code = [
        0x13, 0x00, 0x00, 0x00, // nop
    ];

    let t0 = 0x1234;
    let t1 = 0x5678;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    let t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    let t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    assert_eq!(t0, 0x1234);
    assert_eq!(t1, 0x5678);
}

#[test]
fn test_riscv32_until_pc_update() {
    let code = [
        0x93, 0x02, 0x10, 0x00, // addi t0, zero, 1
        0x13, 0x03, 0x00, 0x02, // addi t1, zero, 0x20
        0x13, 0x01, 0x81, 0x00, // addi sp, sp, 8
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());

    let mut t0 = 0x1234;
    let mut t1 = 0x7890;
    let mut sp = 0x1234;

    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();
    uc.reg_write(RegisterRISCV::SP, sp).unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    sp = uc.reg_read(RegisterRISCV::SP).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(t0, 0x1);
    assert_eq!(t1, 0x20);
    assert_eq!(sp, 0x123c);

    assert_eq!(pc, CODE_START + code.len() as u64);
}

#[test]
fn test_riscv64_until_pc_update() {
    let code = [
        0x93, 0x02, 0x10, 0x00, // addi t0, zero, 1
        0x13, 0x03, 0x00, 0x02, // addi t1, zero, 0x20
        0x13, 0x01, 0x81, 0x00, // addi sp, sp, 8
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    let mut t0 = 0x1234;
    let mut t1 = 0x7890;
    let mut sp = 0x1234;

    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();
    uc.reg_write(RegisterRISCV::SP, sp).unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    sp = uc.reg_read(RegisterRISCV::SP).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(t0, 0x1);
    assert_eq!(t1, 0x20);
    assert_eq!(sp, 0x123c);

    assert_eq!(pc, CODE_START + code.len() as u64);
}

#[test]
fn test_riscv32_3steps_pc_update() {
    let code = [
        0x93, 0x02, 0x10, 0x00, // addi t0, zero, 1
        0x13, 0x03, 0x00, 0x02, // addi t1, zero, 0x20
        0x13, 0x01, 0x81, 0x00, // addi sp, sp, 8
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());

    let mut t0 = 0x1234;
    let mut t1 = 0x7890;
    let mut sp = 0x1234;

    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();
    uc.reg_write(RegisterRISCV::SP, sp).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 3).unwrap();

    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    sp = uc.reg_read(RegisterRISCV::SP).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(t0, 0x1);
    assert_eq!(t1, 0x20);
    assert_eq!(sp, 0x123c);

    assert_eq!(pc, CODE_START + code.len() as u64);
}

#[test]
fn test_riscv64_3steps_pc_update() {
    let code = [
        0x93, 0x02, 0x10, 0x00, // addi t0, zero, 1
        0x13, 0x03, 0x00, 0x02, // addi t1, zero, 0x20
        0x13, 0x01, 0x81, 0x00, // addi sp, sp, 8
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    let mut t0 = 0x1234;
    let mut t1 = 0x7890;
    let mut sp = 0x1234;

    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.reg_write(RegisterRISCV::T1, t1).unwrap();
    uc.reg_write(RegisterRISCV::SP, sp).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 3).unwrap();

    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    t1 = uc.reg_read(RegisterRISCV::T1).unwrap();
    sp = uc.reg_read(RegisterRISCV::SP).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(t0, 0x1);
    assert_eq!(t1, 0x20);
    assert_eq!(sp, 0x123c);

    assert_eq!(pc, CODE_START + code.len() as u64);
}

#[test]
fn test_riscv32_fp_move() {
    let code = [
        0xd3, 0x81, 0x10, 0x22, // fmv.d f3, f1
    ];

    let mut f1 = 0x123456781a2b3c4d;
    let mut f3 = 0x56780246aaaabbbb;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());

    uc.reg_write(RegisterRISCV::F1, f1).unwrap();
    uc.reg_write(RegisterRISCV::F3, f3).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 1).unwrap();

    f1 = uc.reg_read(RegisterRISCV::F1).unwrap();
    f3 = uc.reg_read(RegisterRISCV::F3).unwrap();

    assert_eq!(f1, 0x123456781a2b3c4d);
    assert_eq!(f3, 0x123456781a2b3c4d);
}

#[test]
fn test_riscv64_fp_move() {
    let code = [
        0xd3, 0x81, 0x10, 0x22, // fmv.d f3, f1
    ];

    let mut f1 = 0x123456781a2b3c4d;
    let mut f3 = 0x56780246aaaabbbb;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.reg_write(RegisterRISCV::F1, f1).unwrap();
    uc.reg_write(RegisterRISCV::F3, f3).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 1).unwrap();

    f1 = uc.reg_read(RegisterRISCV::F1).unwrap();
    f3 = uc.reg_read(RegisterRISCV::F3).unwrap();

    assert_eq!(f1, 0x123456781a2b3c4d);
    assert_eq!(f3, 0x123456781a2b3c4d);
}

#[test]
fn test_riscv64_fp_move_from_int() {
    let code = [
        0xf3, 0x90, 0x01, 0x30, // csrrw x2, mstatus, x3;
        0x53, 0x00, 0x0b, 0xf2, // fmvd.d.x ft0, s6
    ];

    let mut ft0 = 0x12341234;
    let mut s6 = 0x56785678;
    let x3 = 0x6000;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.reg_write(RegisterRISCV::FT0, ft0).unwrap();
    uc.reg_write(RegisterRISCV::S6, s6).unwrap();

    // mstatus.fs
    uc.reg_write(RegisterRISCV::X3, x3).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 2).unwrap();

    ft0 = uc.reg_read(RegisterRISCV::FT0).unwrap();
    s6 = uc.reg_read(RegisterRISCV::S6).unwrap();

    assert_eq!(ft0, 0x56785678);
    assert_eq!(s6, 0x56785678);
}

#[test]
fn test_riscv64_fp_move_from_int_reg_write() {
    let code = [
        0x53, 0x00, 0x0b, 0xf2, // fmvd.d.x ft0, s6
    ];

    let mut ft0 = 0x12341234;
    let mut s6 = 0x56785678;
    let mstatus = 0x6000;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.reg_write(RegisterRISCV::FT0, ft0).unwrap();
    uc.reg_write(RegisterRISCV::S6, s6).unwrap();

    // mstatus.fs
    uc.reg_write(RegisterRISCV::MSTATUS, mstatus).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 1).unwrap();

    ft0 = uc.reg_read(RegisterRISCV::FT0).unwrap();
    s6 = uc.reg_read(RegisterRISCV::S6).unwrap();

    assert_eq!(ft0, 0x56785678);
    assert_eq!(s6, 0x56785678);
}

#[test]
fn test_riscv64_fp_move_to_int() {
    let code = [
        0xf3, 0x90, 0x01, 0x30, // csrrw x2, mstatus, x3;
        0x53, 0x0b, 0x00, 0xe2, // fmv.x.d s6, ft0
    ];

    let mut ft0 = 0x12341234;
    let mut s6 = 0x56785678;
    let x3 = 0x6000;

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.reg_write(RegisterRISCV::FT0, ft0).unwrap();
    uc.reg_write(RegisterRISCV::S6, s6).unwrap();

    // mstatus.fs
    uc.reg_write(RegisterRISCV::X3, x3).unwrap();

    uc.emu_start(CODE_START, u64::MAX, 0, 2).unwrap();

    ft0 = uc.reg_read(RegisterRISCV::FT0).unwrap();
    s6 = uc.reg_read(RegisterRISCV::S6).unwrap();

    assert_eq!(ft0, 0x12341234);
    assert_eq!(s6, 0x12341234);
}

#[test]
fn test_riscv64_code_patching() {
    let code = [
        0x93, 0x82, 0x12, 0x00, // addi t0, t0, 0x1
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    // Zero out t0 and t1
    let mut t0 = 0x0;
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();

    // emulate the instruction
    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    // check value
    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    assert_eq!(t0, 0x1);

    // patch instruction
    let patch_code = [
        0x93, 0x82, 0xf2, 0x7f, // addi t0, t0, 0x7FF
    ];
    uc.mem_write(CODE_START, &patch_code).unwrap();

    // zero out t0
    t0 = 0x0;
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.emu_start(CODE_START, CODE_START + patch_code.len() as u64, 0, 0)
        .unwrap();

    // check value
    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    assert_eq!(t0, 0x7ff);
}

#[test]
fn test_riscv64_code_patching_count() {
    let code = [
        0x93, 0x82, 0x12, 0x00, // addi t0, t0, 0x1
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    // Zero out t0 and t1
    let mut t0 = 0x0;
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();

    // emulate the instruction
    uc.emu_start(CODE_START, u64::MAX, 0, 1).unwrap();

    // check value
    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    assert_eq!(t0, 0x1);

    // patch instruction
    let patch_code = [
        0x93, 0x82, 0xf2, 0x7f, // addi t0, t0, 0x7FF
    ];
    uc.mem_write(CODE_START, &patch_code).unwrap();
    uc.ctl_remove_cache(CODE_START, CODE_START + patch_code.len() as u64)
        .unwrap();

    // zero out t0
    t0 = 0x0;
    uc.reg_write(RegisterRISCV::T0, t0).unwrap();
    uc.emu_start(CODE_START, u64::MAX, 0, 1).unwrap();

    // check value
    t0 = uc.reg_read(RegisterRISCV::T0).unwrap();
    assert_eq!(t0, 0x7ff);
}

#[test]
fn test_riscv64_ecall() {
    let code = [
        0x73, 0x00, 0x00, 0x00, // ecall
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.add_intr_hook(|uc, _| uc.emu_stop().unwrap()).unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(pc, CODE_START + 4);
}

#[test]
fn test_riscv32_mmio_map() {
    #[rustfmt::skip]
    let code = [
        0x37, 0x17, 0x02, 0x40, // lui  a4, 0x40021
        0x1c, 0x4f,             // c.lw a5, 0x18(a4)
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());

    uc.mmio_map_ro(0x40000000, 0x40000, |uc, offset, _size| {
        let a4 = uc.reg_read(RegisterRISCV::A4).unwrap();
        assert_eq!(a4, 0x40021 << 12);
        assert_eq!(offset, 0x21018);
        0
    })
    .unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();
}

#[test]
fn test_riscv32_map() {
    #[rustfmt::skip]
    let code = [
        0x37, 0x17, 0x02, 0x40, // lui  a4, 0x40021
        0x1c, 0x4f,             // c.lw a5, 0x18(a4)
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV32, None, &code, ());

    let val = 0xdeadbeefu64;
    uc.mem_map(0x40000000, 0x40000, Prot::ALL).unwrap();
    uc.mem_write(0x40000000 + 0x21018, &val.to_le_bytes())
        .unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();

    let a5 = uc.reg_read(RegisterRISCV::A5).unwrap();
    assert_eq!(a5, val);
}

#[test]
fn test_riscv64_mmio_map() {
    #[rustfmt::skip]
    let code = [
        0x37, 0x17, 0x02, 0x40, // lui  a4, 0x40021
        0x1c, 0x4f,             // c.lw a5, 0x18(a4)
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.mmio_map_ro(0x40000000, 0x40000, |uc, offset, _size| {
        let a4 = uc.reg_read(RegisterRISCV::A4).unwrap();
        assert_eq!(a4, 0x40021 << 12);
        assert_eq!(offset, 0x21018);
        0
    })
    .unwrap();

    uc.emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap();
}

#[test]
fn test_riscv_correct_address_in_small_jump_hook() {
    #[rustfmt::skip]
    let code = [
        0xb7, 0x82, 0x00, 0x00, // lui t0, 8
        0x9b, 0x82, 0x02, 0xf0, // addiw t0, t0, -256;
        0x67, 0x80, 0x02, 0x00, // jr x5
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.add_mem_hook(HookType::MEM_UNMAPPED, 1, 0, |uc, _, address, _, _| {
        // Check registers
        let x5 = uc.reg_read(RegisterRISCV::X5).unwrap();
        let pc = uc.reg_read(RegisterRISCV::PC).unwrap();
        assert_eq!(x5, 0x7F00);
        assert_eq!(pc, 0x7F00);

        // Check address
        assert_eq!(address, 0x7F00);
        false
    })
    .unwrap();

    let err = uc
        .emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap_err();
    assert_eq!(err, uc_error::FETCH_UNMAPPED);

    let x5 = uc.reg_read(RegisterRISCV::X5).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(x5, 0x7F00);
    assert_eq!(pc, 0x7F00);
}

#[test]
fn test_riscv_correct_address_in_long_jump_hook() {
    #[rustfmt::skip]
    let code = [
        0x93, 0x02, 0xf0, 0xff, // addi t0, zero, -1
        0x93, 0x92, 0xf2, 0x03, // slli t0, t0, 63
        0x93, 0x82, 0x02, 0xf0, // addi t0, t0, -256
        0x67, 0x80, 0x02, 0x00, // jr x5
    ];

    let mut uc = uc_common_setup(Arch::RISCV, Mode::RISCV64, None, &code, ());

    uc.add_mem_hook(HookType::MEM_UNMAPPED, 1, 0, |uc, _, address, _, _| {
        // Check registers
        let x5 = uc.reg_read(RegisterRISCV::X5).unwrap();
        let pc = uc.reg_read(RegisterRISCV::PC).unwrap();
        assert_eq!(x5, 0x7FFFFFFFFFFFFF00);
        assert_eq!(pc, 0x7FFFFFFFFFFFFF00);

        // Check address
        assert_eq!(address, 0x7FFFFFFFFFFFFF00);
        false
    })
    .unwrap();

    let err = uc
        .emu_start(CODE_START, CODE_START + code.len() as u64, 0, 0)
        .unwrap_err();
    assert_eq!(err, uc_error::FETCH_UNMAPPED);

    let x5 = uc.reg_read(RegisterRISCV::X5).unwrap();
    let pc = uc.reg_read(RegisterRISCV::PC).unwrap();

    assert_eq!(x5, 0x7FFFFFFFFFFFFF00);
    assert_eq!(pc, 0x7FFFFFFFFFFFFF00);
}

#[test]
fn test_riscv_mmu() {
    fn test_riscv_mmu_prepare_tlb(uc: &mut Unicorn<'_, ()>, data_address: u64, code_address: u64) {
        let sptbr = 0x2000;
        uc.mem_map(sptbr, 0x3000, Prot::ALL).unwrap(); // tlb base

        let tlbe = ((sptbr + 0x1000) >> 2) | 1;
        uc.mem_write(sptbr, &tlbe.to_le_bytes()).unwrap();
        let tlbe = ((sptbr + 0x2000) >> 2) | 1;
        uc.mem_write(sptbr + 0x1000, &tlbe.to_le_bytes()).unwrap();

        let tlbe = (code_address >> 2) | (7 << 1) | 1;
        uc.mem_write(sptbr + 0x2000 + 0x15 * 8, &tlbe.to_le_bytes())
            .unwrap();

        let tlbe = (data_address >> 2) | (7 << 1) | 1;
        uc.mem_write(sptbr + 0x2000 + 0x16 * 8, &tlbe.to_le_bytes())
            .unwrap();
    }

    let code_address = 0x5000;
    let data_address = 0x6000;
    let data_value = 0x41414141u32;

    let code_m = [
        0x1b, 0x0e, 0xf0, 0xff, // li t3, (8 << 60) | 2
        0x13, 0x1e, 0xfe, 0x03, // csrw sptbr, t3
        0x13, 0x0e, 0x2e, 0x00, // li t0, (1 << 11) | (1 << 5)
        0x73, 0x10, 0x0e, 0x18, // csrw mstatus, t0
        0xb7, 0x12, 0x00, 0x00, // la t1, 0x15000
        0x9b, 0x82, 0x02, 0x82, // csrw mepc, t1
        0x73, 0x90, 0x02, 0x30, // mret
        0x37, 0x53, 0x01, 0x00, // lui t0, 8
        0x73, 0x10, 0x13, 0x34, // csrw mepc, t1
        0x73, 0x00, 0x20, 0x30, // mret
    ];

    #[rustfmt::skip]
    let code_s = [
        0xb7, 0x42, 0x41, 0x41, 0x9b, 0x82, 0x12, 0x14, // li t0, 0x41414141
        0x37, 0x63, 0x01, 0x00,                         // li t1, 0x16000
        0x23, 0x20, 0x53, 0x00,                         // sw t0, 0(t1)
        0x13, 0x00, 0x00, 0x00,                         // nop
    ];

    let mut uc = Unicorn::new(Arch::RISCV, Mode::RISCV64).unwrap();
    uc.ctl_set_tlb_type(TlbType::CPU).unwrap();
    uc.add_code_hook(1, 0, |uc, address, _| {
        if address == 0x15010 {
            uc.emu_stop().unwrap();
        }
    })
    .unwrap();
    uc.mem_map(0x1000, 0x1000, Prot::ALL).unwrap();
    uc.mem_map(code_address, 0x1000, Prot::ALL).unwrap();
    uc.mem_map(data_address, 0x1000, Prot::ALL).unwrap();
    uc.mem_write(code_address, &code_s).unwrap();
    uc.mem_write(0x1000, &code_m).unwrap();

    test_riscv_mmu_prepare_tlb(&mut uc, data_address, code_address);

    uc.emu_start(0x1000, 0x1000 + code_m.len() as u64, 0, 0)
        .unwrap();
    let data_result = u32::from_le_bytes(
        uc.mem_read_as_vec(data_address, 4)
            .unwrap()
            .try_into()
            .unwrap(),
    );

    assert_eq!(data_value, data_result);
}

#[test]
fn test_riscv_priv() {
    let m_entry_address = 0x1000;
    let main_address = 0x3000;

    #[rustfmt::skip]
    let code_m_entry = [
        0x93, 0x02, 0x00, 0x00, // li t0, 0
        0x73, 0x90, 0x02, 0x30, // csrw mstatus, t0
        0x37, 0x33, 0x00, 0x00, // li t1, 0x3000
        0x73, 0x10, 0x13, 0x34, // csrw mepc, t1
        0x73, 0x00, 0x20, 0x30, // mret
    ];

    #[rustfmt::skip]
    let code_main = [
        0x73, 0x90, 0x02, 0x14, // csrw sscratch, t1
        0x13, 0x00, 0x00, 0x00, // nop
    ];

    let main_end_address = main_address + code_main.len() as u64;

    let mut uc = Unicorn::new(Arch::RISCV, Mode::RISCV64).unwrap();
    uc.ctl_set_tlb_type(TlbType::CPU).unwrap();
    uc.mem_map(m_entry_address, 0x1000, Prot::ALL).unwrap();
    uc.mem_map(main_address, 0x1000, Prot::ALL).unwrap();
    uc.mem_write(m_entry_address, &code_m_entry).unwrap();
    uc.mem_write(main_address, &code_main).unwrap();

    // Before anything executes we should be in M-Mode
    let mut priv_value = uc.reg_read(RegisterRISCV::PRIV).unwrap();
    assert_eq!(priv_value, 3);

    // We'll put a sentinel value in sscratch so we can determine whether we've
    // successfully written to it below.
    let mut reg_value = 0xffff;
    uc.reg_write(RegisterRISCV::SSCRATCH, reg_value).unwrap();

    // Run until we reach the "csrw" at the start of code_main, at which
    // point we should be in U-Mode due to the mret instruction.
    uc.emu_start(m_entry_address, main_address, 0, 10).unwrap();

    let mut pc = uc.reg_read(RegisterRISCV::PC).unwrap();
    assert_eq!(pc, main_address);
    priv_value = uc.reg_read(RegisterRISCV::PRIV).unwrap();
    assert_eq!(priv_value, 0); // Now in U-Mode

    // U-Mode can't write to sscratch, so execution at this point should
    // cause an invalid instruction exception.
    let err = uc
        .emu_start(main_address, main_end_address, 0, 0)
        .unwrap_err();
    assert_eq!(err, uc_error::EXCEPTION);
    pc = uc.reg_read(RegisterRISCV::PC).unwrap();
    assert_eq!(pc, main_address + 4);

    // ...but if we force S-Mode then we should be able to set it successfully.
    priv_value = 1;
    uc.reg_write(RegisterRISCV::PRIV, priv_value).unwrap();
    uc.emu_start(main_address, main_end_address, 0, 0).unwrap();
    reg_value = uc.reg_read(RegisterRISCV::SSCRATCH).unwrap();
    assert_eq!(reg_value, 0);
    pc = uc.reg_read(RegisterRISCV::PC).unwrap();
    assert_eq!(pc, main_end_address);
}