aboutsummaryrefslogtreecommitdiff
path: root/pk/emulation.c
blob: 25c4de07c9590d289032c38f755403f3c3f3f056 (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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#include "mtrap.h"
#include "softfloat.h"
#include <limits.h>

DECLARE_EMULATION_FUNC(truly_illegal_insn)
{
  return -1;
}

uintptr_t misaligned_load_trap(uintptr_t mcause, uintptr_t* regs)
{
  uintptr_t mstatus = read_csr(mstatus);
  uintptr_t mepc = read_csr(mepc);
  insn_fetch_t fetch = get_insn(mcause, mstatus, mepc);
  if (fetch.error)
    return -1;

  uintptr_t val, res, tmp;
  uintptr_t addr = GET_RS1(fetch.insn, regs) + IMM_I(fetch.insn);

  #define DO_LOAD(type_lo, type_hi, insn_lo, insn_hi) ({ \
      type_lo val_lo; \
      type_hi val_hi; \
      uintptr_t addr_lo = addr & -sizeof(type_hi); \
      uintptr_t addr_hi = addr_lo + sizeof(type_hi); \
      uintptr_t masked_addr = sizeof(type_hi) < 4 ? addr % sizeof(type_hi) : addr; \
      res = unpriv_mem_access(mstatus, mepc, \
                              insn_lo " %[val_lo], (%[addr_lo]);" \
                              insn_hi " %[val_hi], (%[addr_hi])", \
                              val_lo, val_hi, addr_lo, addr_hi); \
      val_lo >>= masked_addr * 8; \
      val_hi <<= (sizeof(type_hi) - masked_addr) * 8; \
      val = (type_hi)(val_lo | val_hi); \
  })

  if ((fetch.insn & MASK_LW) == MATCH_LW)
    DO_LOAD(uint32_t, int32_t, "lw", "lw");
#ifdef __riscv64
  else if ((fetch.insn & MASK_LD) == MATCH_LD)
    DO_LOAD(uint64_t, uint64_t, "ld", "ld");
  else if ((fetch.insn & MASK_LWU) == MATCH_LWU)
    DO_LOAD(uint32_t, uint32_t, "lwu", "lwu");
#endif
  else if ((fetch.insn & MASK_FLD) == MATCH_FLD) {
#ifdef __riscv64
    DO_LOAD(uint64_t, uint64_t, "ld", "ld");
    if (res == 0) {
      SET_F64_RD(fetch.insn, regs, val);
      goto success;
    }
#else
    DO_LOAD(uint32_t, int32_t, "lw", "lw");
    if (res == 0) {
      uint64_t double_val = val;
      addr += 4;
      DO_LOAD(uint32_t, int32_t, "lw", "lw");
      double_val |= (uint64_t)val << 32;
      if (res == 0) {
        SET_F64_RD(fetch.insn, regs, val);
        goto success;
      }
    }
#endif
  } else if ((fetch.insn & MASK_FLW) == MATCH_FLW) {
    DO_LOAD(uint32_t, uint32_t, "lw", "lw");
    if (res == 0) {
      SET_F32_RD(fetch.insn, regs, val);
      goto success;
    }
  } else if ((fetch.insn & MASK_LH) == MATCH_LH) {
    // equivalent to DO_LOAD(uint32_t, int16_t, "lhu", "lh")
    res = unpriv_mem_access(mstatus, mepc,
                            "lbu %[val], 0(%[addr]);"
                            "lb %[tmp], 1(%[addr])",
                            val, tmp, addr, mstatus/*X*/);
    val |= tmp << 8;
  } else if ((fetch.insn & MASK_LHU) == MATCH_LHU) {
    // equivalent to DO_LOAD(uint32_t, uint16_t, "lhu", "lhu")
    res = unpriv_mem_access(mstatus, mepc,
                            "lbu %[val], 0(%[addr]);"
                            "lbu %[tmp], 1(%[addr])",
                            val, tmp, addr, mstatus/*X*/);
    val |= tmp << 8;
  } else {
    return -1;
  }

  if (res) {
    restore_mstatus(mstatus, mepc);
    return -1;
  }

  SET_RD(fetch.insn, regs, val);

success:
  write_csr(mepc, mepc + 4);
  return 0;
}

uintptr_t misaligned_store_trap(uintptr_t mcause, uintptr_t* regs)
{
  uintptr_t mstatus = read_csr(mstatus);
  uintptr_t mepc = read_csr(mepc);
  insn_fetch_t fetch = get_insn(mcause, mstatus, mepc);
  if (fetch.error)
    return -1;

  uintptr_t addr = GET_RS1(fetch.insn, regs) + IMM_S(fetch.insn);
  uintptr_t val = GET_RS2(fetch.insn, regs), error;

  if ((fetch.insn & MASK_SW) == MATCH_SW) {
SW:
    error = unpriv_mem_access(mstatus, mepc,
                              "sb %[val], 0(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 1(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 2(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 3(%[addr]);",
                              unused1, unused2, val, addr);
#ifdef __riscv64
  } else if ((fetch.insn & MASK_SD) == MATCH_SD) {
SD:
    error = unpriv_mem_access(mstatus, mepc,
                              "sb %[val], 0(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 1(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 2(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 3(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 4(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 5(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 6(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 7(%[addr]);",
                              unused1, unused2, val, addr);
#endif
  } else if ((fetch.insn & MASK_SH) == MATCH_SH) {
    error = unpriv_mem_access(mstatus, mepc,
                              "sb %[val], 0(%[addr]);"
                              "srl %[val], %[val], 8; sb %[val], 1(%[addr]);",
                              unused1, unused2, val, addr);
  } else if ((fetch.insn & MASK_FSD) == MATCH_FSD) {
#ifdef __riscv64
    val = GET_F64_RS2(fetch.insn, regs);
    goto SD;
#else
    uint64_t double_val = GET_F64_RS2(fetch.insn, regs);
    uint32_t val_lo = double_val, val_hi = double_val >> 32;
    error = unpriv_mem_access(mstatus, mepc,
                              "sb %[val_lo], 0(%[addr]);"
                              "srl %[val_lo], %[val_lo], 8; sb %[val_lo], 1(%[addr]);"
                              "srl %[val_lo], %[val_lo], 8; sb %[val_lo], 2(%[addr]);"
                              "srl %[val_lo], %[val_lo], 8; sb %[val_lo], 3(%[addr]);"
                              "sb %[val_hi], 4(%[addr]);"
                              "srl %[val_hi], %[val_hi], 8; sb %[val_hi], 5(%[addr]);"
                              "srl %[val_hi], %[val_hi], 8; sb %[val_hi], 6(%[addr]);"
                              "srl %[val_hi], %[val_hi], 8; sb %[val_hi], 7(%[addr]);",
                              unused1, unused2, val_lo, val_hi, addr);
#endif
  } else if ((fetch.insn & MASK_FSW) == MATCH_FSW) {
    val = GET_F32_RS2(fetch.insn, regs);
    goto SW;
  } else
    return -1;

  if (error) {
    restore_mstatus(mstatus, mepc);
    return -1;
  }

  write_csr(mepc, mepc + 4);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_float_load)
{
  uintptr_t val_lo, val_hi, error;
  uint64_t val;
  uintptr_t addr = GET_RS1(insn, regs) + IMM_I(insn);
  
  switch (insn & MASK_FUNCT3)
  {
    case MATCH_FLW & MASK_FUNCT3:
      if (addr % 4 != 0)
        return misaligned_load_trap(mcause, regs);

      error = unpriv_mem_access(mstatus, mepc,
                                "lw %[val_lo], (%[addr])",
                                val_lo, val_hi/*X*/, addr, mstatus/*X*/);

      if (error == 0) {
        SET_F32_RD(insn, regs, val_lo);
        goto success;
      }
      break;

    case MATCH_FLD & MASK_FUNCT3:
      if (addr % sizeof(uintptr_t) != 0)
        return misaligned_load_trap(mcause, regs);
#ifdef __riscv64
      error = unpriv_mem_access(mstatus, mepc,
                                "ld %[val], (%[addr])",
                                val, val_hi/*X*/, addr, mstatus/*X*/);
#else
      error = unpriv_mem_access(mstatus, mepc,
                                "lw %[val_lo], (%[addr]);"
                                "lw %[val_hi], 4(%[addr])",
                                val_lo, val_hi, addr, mstatus/*X*/);
      val = val_lo | ((uint64_t)val_hi << 32);
#endif

      if (error == 0) {
        SET_F64_RD(insn, regs, val);
        goto success;
      }
      break;
  }

  restore_mstatus(mstatus, mepc);
  return -1;

success:
  write_csr(mepc, mepc + 4);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_float_store)
{
  uintptr_t val_lo, val_hi, error;
  uint64_t val;
  uintptr_t addr = GET_RS1(insn, regs) + IMM_I(insn);
  
  switch (insn & MASK_FUNCT3)
  {
    case MATCH_FSW & MASK_FUNCT3:
      if (addr % 4 != 0)
        return misaligned_store_trap(mcause, regs);

      val_lo = GET_F32_RS2(insn, regs);
      error = unpriv_mem_access(mstatus, mepc,
                                "sw %[val_lo], (%[addr])",
                                unused1, unused2, val_lo, addr);
      break;

    case MATCH_FSD & MASK_FUNCT3:
      if (addr % sizeof(uintptr_t) != 0)
        return misaligned_store_trap(mcause, regs);

      val = GET_F64_RS2(insn, regs);
#ifdef __riscv64
      error = unpriv_mem_access(mstatus, mepc,
                                "sd %[val], (%[addr])",
                                unused1, unused2, val, addr);
#else
      val_lo = val;
      val_hi = val >> 32;
      error = unpriv_mem_access(mstatus, mepc,
                                "sw %[val_lo], (%[addr]);"
                                "sw %[val_hi], 4(%[addr])",
                                unused1, unused2, val_lo, val_hi, addr);
#endif
      break;

    default:
      error = 1;
  }

  if (error) {
    restore_mstatus(mstatus, mepc);
    return -1;
  }

  write_csr(mepc, mepc + 4);
  return 0;
}

#ifdef __riscv64
typedef int double_int __attribute__((mode(TI)));
typedef unsigned int double_uint __attribute__((mode(TI)));
#else
typedef int64_t double_int;
typedef uint64_t double_int;
#endif

DECLARE_EMULATION_FUNC(emulate_mul_div)
{
  uintptr_t rs1 = GET_RS1(insn, regs), rs2 = GET_RS2(insn, regs), val;

  // If compiled with -mno-multiply, GCC will expand these out
  if ((insn & MASK_MUL) == MATCH_MUL)
    val = rs1 * rs2;
  else if ((insn & MASK_DIV) == MATCH_DIV)
    val = (intptr_t)rs1 / (intptr_t)rs2;
  else if ((insn & MASK_DIVU) == MATCH_DIVU)
    val = rs1 / rs2;
  else if ((insn & MASK_REM) == MATCH_REM)
    val = (intptr_t)rs1 % (intptr_t)rs2;
  else if ((insn & MASK_REMU) == MATCH_REMU)
    val = rs1 % rs2;
  else if ((insn & MASK_MULH) == MATCH_MULH)
    val = ((double_int)(intptr_t)rs1 * (double_int)(intptr_t)rs2) >> (8 * sizeof(rs1));
  else if ((insn & MASK_MULHU) == MATCH_MULHU)
    val = ((double_int)rs1 * (double_int)rs2) >> (8 * sizeof(rs1));
  else if ((insn & MASK_MULHSU) == MATCH_MULHSU)
    val = ((double_int)(intptr_t)rs1 * (double_int)rs2) >> (8 * sizeof(rs1));
  else
    return -1;

  SET_RD(insn, regs, val);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_mul_div32)
{
#ifndef __riscv64
  return truly_illegal_insn(mcause, regs, insn, mstatus, mepc);
#endif

  uint32_t rs1 = GET_RS1(insn, regs), rs2 = GET_RS2(insn, regs);
  int32_t val;

  // If compiled with -mno-multiply, GCC will expand these out
  if ((insn & MASK_MUL) == MATCH_MULW)
    val = rs1 * rs2;
  else if ((insn & MASK_DIV) == MATCH_DIV)
    val = (int32_t)rs1 / (int32_t)rs2;
  else if ((insn & MASK_DIVU) == MATCH_DIVU)
    val = rs1 / rs2;
  else if ((insn & MASK_REM) == MATCH_REM)
    val = (int32_t)rs1 % (int32_t)rs2;
  else if ((insn & MASK_REMU) == MATCH_REMU)
    val = rs1 % rs2;
  else
    return -1;

  SET_RD(insn, regs, val);
  return 0;
}

static inline int emulate_read_csr(int num, uintptr_t* result, uintptr_t mstatus)
{
  switch (num)
  {
    case CSR_FRM:
      if ((mstatus & MSTATUS_FS) == 0) break;
      *result = GET_FRM();
      return 0;
    case CSR_FFLAGS:
      if ((mstatus & MSTATUS_FS) == 0) break;
      *result = GET_FFLAGS();
      return 0;
    case CSR_FCSR:
      if ((mstatus & MSTATUS_FS) == 0) break;
      *result = GET_FCSR();
      return 0;
  }
  return -1;
}

static inline int emulate_write_csr(int num, uintptr_t value, uintptr_t mstatus)
{
  switch (num)
  {
    case CSR_FRM: SET_FRM(value); return 0;
    case CSR_FFLAGS: SET_FFLAGS(value); return 0;
    case CSR_FCSR: SET_FCSR(value); return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_system)
{
  int rs1_num = (insn >> 15) & 0x1f;
  uintptr_t rs1_val = GET_RS1(insn, regs);
  int csr_num = (uint32_t)insn >> 20;
  uintptr_t csr_val, new_csr_val;

  if (emulate_read_csr(csr_num, &csr_val, mstatus) != 0)
    return -1;

  int do_write = rs1_num;
  switch (GET_RM(insn))
  {
    case 0: return -1;
    case 1: new_csr_val = rs1_val; do_write = 1; break;
    case 2: new_csr_val = csr_val | rs1_val; break;
    case 3: new_csr_val = csr_val & ~rs1_val; break;
    case 4: return -1;
    case 5: new_csr_val = rs1_num; do_write = 1; break;
    case 6: new_csr_val = csr_val | rs1_num; break;
    case 7: new_csr_val = csr_val & ~rs1_num; break;
  }

  if (do_write && emulate_write_csr(csr_num, new_csr_val, mstatus) != 0)
    return -1;

  SET_RD(insn, regs, csr_val);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_fp)
{
  asm (".pushsection .rodata\n"
       "fp_emulation_table:\n"
       "  .word emulate_fadd\n"
       "  .word emulate_fsub\n"
       "  .word emulate_fmul\n"
       "  .word emulate_fdiv\n"
       "  .word emulate_fsgnj\n"
       "  .word emulate_fmin\n"
       "  .word truly_illegal_insn\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fcvt_ff\n"
       "  .word truly_illegal_insn\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fsqrt\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"
       "  .word truly_illegal_insn\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fcmp\n"
       "  .word truly_illegal_insn\n"
       "  .word truly_illegal_insn\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fcvt_if\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fcvt_fi\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fmv_if\n"
       "  .word truly_illegal_insn\n"
       "  .word emulate_fmv_fi\n"
       "  .word truly_illegal_insn\n"
       "  .popsection");

  // if FPU is disabled, punt back to the OS
  if (unlikely((mstatus & MSTATUS_FS) == 0))
    return -1;

  extern int32_t fp_emulation_table[];
  int32_t* pf = (void*)fp_emulation_table + ((insn >> 25) & 0x7c);
  emulation_func f = (emulation_func)(uintptr_t)*pf;

  SETUP_STATIC_ROUNDING(insn);
  return f(mcause, regs, insn, mstatus, mepc);
}

uintptr_t emulate_any_fadd(uintptr_t mcause, uintptr_t* regs, insn_t insn, uintptr_t neg_b)
{
  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs) ^ neg_b;
    SET_F32_RD(insn, regs, f32_add(rs1, rs2));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs) ^ ((uint64_t)neg_b << 32);
    SET_F64_RD(insn, regs, f64_add(rs1, rs2));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fadd)
{
  return emulate_any_fadd(mcause, regs, insn, 0);
}

DECLARE_EMULATION_FUNC(emulate_fsub)
{
  return emulate_any_fadd(mcause, regs, insn, INT32_MIN);
}

DECLARE_EMULATION_FUNC(emulate_fmul)
{
  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    SET_F32_RD(insn, regs, f32_mul(rs1, rs2));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    SET_F64_RD(insn, regs, f64_mul(rs1, rs2));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fdiv)
{
  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    SET_F32_RD(insn, regs, f32_div(rs1, rs2));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    SET_F64_RD(insn, regs, f64_div(rs1, rs2));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fsqrt)
{
  if ((insn >> 20) & 0x1f)
    return -1;

  if (GET_PRECISION(insn) == PRECISION_S) {
    SET_F32_RD(insn, regs, f32_sqrt(GET_F32_RS1(insn, regs)));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    SET_F64_RD(insn, regs, f64_sqrt(GET_F64_RS1(insn, regs)));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fsgnj)
{
  int rm = GET_RM(insn);
  if (rm >= 3)
    return -1;

  #define DO_FSGNJ(rs1, rs2, rm) ({ \
    typeof(rs1) rs1_sign = (rs1) >> (8*sizeof(rs1)-1); \
    typeof(rs1) rs2_sign = (rs2) >> (8*sizeof(rs1)-1); \
    rs1_sign &= (rm) >> 1; \
    rs1_sign ^= (rm) ^ rs2_sign; \
    ((rs1) << 1 >> 1) | (rs1_sign << (8*sizeof(rs1)-1)); })

  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    SET_F32_RD(insn, regs, DO_FSGNJ(rs1, rs2, rm));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    SET_F64_RD(insn, regs, DO_FSGNJ(rs1, rs2, rm));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fmin)
{
  int rm = GET_RM(insn);
  if (rm >= 2)
    return -1;

  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    uint32_t arg1 = rm ? rs2 : rs1;
    uint32_t arg2 = rm ? rs1 : rs2;
    int use_rs1 = f32_lt_quiet(arg1, arg2) || isNaNF32UI(rs2);
    SET_F32_RD(insn, regs, use_rs1 ? rs1 : rs2);
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    uint64_t arg1 = rm ? rs2 : rs1;
    uint64_t arg2 = rm ? rs1 : rs2;
    int use_rs1 = f64_lt_quiet(arg1, arg2) || isNaNF64UI(rs2);
    SET_F64_RD(insn, regs, use_rs1 ? rs1 : rs2);
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fcvt_ff)
{
  int rs2_num = (insn >> 20) & 0x1f;
  if (GET_PRECISION(insn) == PRECISION_S) {
    if (rs2_num != 1)
      return -1;
    SET_F32_RD(insn, regs, f64_to_f32(GET_F64_RS1(insn, regs)));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    if (rs2_num != 0)
      return -1;
    SET_F64_RD(insn, regs, f32_to_f64(GET_F32_RS1(insn, regs)));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fcvt_fi)
{
  if (GET_PRECISION(insn) != PRECISION_S && GET_PRECISION(insn) != PRECISION_D)
    return -1;

  int negative = 0;
  uint64_t uint_val = GET_RS1(insn, regs);

  switch ((insn >> 20) & 0x1f)
  {
    case 0: // int32
      negative = (int32_t)uint_val < 0;
      uint_val = negative ? -(int32_t)uint_val : (int32_t)uint_val;
      break;
    case 1: // uint32
      uint_val = (uint32_t)uint_val;
      break;
#ifdef __riscv64
    case 2: // int64
      negative = (int64_t)uint_val < 0;
      uint_val = negative ? -uint_val : uint_val;
    case 3: // uint64
      break;
#endif
    default:
      return -1;
  }

  uint64_t float64 = ui64_to_f64(uint_val);
  if (negative)
    float64 ^= INT64_MIN;

  if (GET_PRECISION(insn) == PRECISION_S)
    SET_F32_RD(insn, regs, f64_to_f32(float64));
  else
    SET_F64_RD(insn, regs, float64);

  return 0;
}

DECLARE_EMULATION_FUNC(emulate_fcvt_if)
{
  int rs2_num = (insn >> 20) & 0x1f;
#ifdef __riscv64
  if (rs2_num >= 4)
    return -1;
#else
  if (rs2_num >= 2)
    return -1;
#endif

  int64_t float64;
  if (GET_PRECISION(insn) == PRECISION_S)
    float64 = f32_to_f64(GET_F32_RS1(insn, regs));
  else if (GET_PRECISION(insn) == PRECISION_D)
    float64 = GET_F64_RS1(insn, regs);
  else
    return -1;

  int negative = 0;
  if (float64 < 0) {
    negative = 1;
    float64 ^= INT64_MIN;
  }
  uint64_t uint_val = f64_to_ui64(float64, softfloat_roundingMode, true);
  uint64_t result, limit, limit_result;

  switch (rs2_num)
  {
    case 0: // int32
      if (negative) {
        result = (int32_t)-uint_val;
        limit_result = limit = (uint32_t)INT32_MIN;
      } else {
        result = (int32_t)uint_val;
        limit_result = limit = INT32_MAX;
      }
      break;

    case 1: // uint32
      limit = limit_result = UINT32_MAX;
      if (negative)
        result = limit = 0;
      else
        result = (uint32_t)uint_val;
      break;

    case 2: // int32
      if (negative) {
        result = (int64_t)-uint_val;
        limit_result = limit = (uint64_t)INT64_MIN;
      } else {
        result = (int64_t)uint_val;
        limit_result = limit = INT64_MAX;
      }
      break;

    case 3: // uint64
      limit = limit_result = UINT64_MAX;
      if (negative)
        result = limit = 0;
      else
        result = (uint64_t)uint_val;
      break;
  }

  if (uint_val > limit) {
    result = limit_result;
    softfloat_raiseFlags(softfloat_flag_invalid);
  }

  SET_FS_DIRTY();
  SET_RD(insn, regs, result);

  return 0;
}

DECLARE_EMULATION_FUNC(emulate_fcmp)
{
  int rm = GET_RM(insn);
  if (rm >= 3)
    return -1;

  uintptr_t result;
  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    if (rm != 1)
      result = f32_eq(rs1, rs2);
    if (rm == 1 || (rm == 0 && !result))
      result = f32_lt(rs1, rs2);
    goto success;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    if (rm != 1)
      result = f64_eq(rs1, rs2);
    if (rm == 1 || (rm == 0 && !result))
      result = f64_lt(rs1, rs2);
    goto success;
  }
  return -1;
success:
  SET_RD(insn, regs, result);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_fmv_if)
{
  uintptr_t result;
  if ((insn & MASK_FMV_X_S) == MATCH_FMV_X_S)
    result = GET_F32_RS1(insn, regs);
#ifdef __riscv64
  else if ((insn & MASK_FMV_X_D) == MATCH_FMV_X_D)
    result = GET_F64_RS1(insn, regs);
#endif
  else
    return -1;

  SET_RD(insn, regs, result);
  return 0;
}

DECLARE_EMULATION_FUNC(emulate_fmv_fi)
{
  uintptr_t rs1 = GET_RS1(insn, regs);

  if ((insn & MASK_FMV_S_X) == MATCH_FMV_S_X)
    SET_F32_RD(insn, regs, rs1);
  else if ((insn & MASK_FMV_D_X) == MATCH_FMV_D_X)
    SET_F64_RD(insn, regs, rs1);
  else
    return -1;

  return 0;
}

uintptr_t emulate_any_fmadd(int op, uintptr_t* regs, insn_t insn, uintptr_t mstatus)
{
  // if FPU is disabled, punt back to the OS
  if (unlikely((mstatus & MSTATUS_FS) == 0))
    return -1;

  SETUP_STATIC_ROUNDING(insn);
  if (GET_PRECISION(insn) == PRECISION_S) {
    uint32_t rs1 = GET_F32_RS1(insn, regs);
    uint32_t rs2 = GET_F32_RS2(insn, regs);
    uint32_t rs3 = GET_F32_RS3(insn, regs);
    SET_F32_RD(insn, regs, softfloat_mulAddF32(op, rs1, rs2, rs3));
    return 0;
  } else if (GET_PRECISION(insn) == PRECISION_D) {
    uint64_t rs1 = GET_F64_RS1(insn, regs);
    uint64_t rs2 = GET_F64_RS2(insn, regs);
    uint64_t rs3 = GET_F64_RS3(insn, regs);
    SET_F64_RD(insn, regs, softfloat_mulAddF64(op, rs1, rs2, rs3));
    return 0;
  }
  return -1;
}

DECLARE_EMULATION_FUNC(emulate_fmadd)
{
  int op = 0;
  return emulate_any_fmadd(op, regs, insn, mstatus);
}

DECLARE_EMULATION_FUNC(emulate_fmsub)
{
  int op = softfloat_mulAdd_subC;
  return emulate_any_fmadd(op, regs, insn, mstatus);
}

DECLARE_EMULATION_FUNC(emulate_fnmadd)
{
  int op = softfloat_mulAdd_subC | softfloat_mulAdd_subProd;
  return emulate_any_fmadd(op, regs, insn, mstatus);
}

DECLARE_EMULATION_FUNC(emulate_fnmsub)
{
  int op = softfloat_mulAdd_subProd;
  return emulate_any_fmadd(op, regs, insn, mstatus);
}