box64: RISC-V Vector Extension Seems Not Working Well

Description

I encountered a weird bug when running lstopo (https://archlinux.org/packages/extra/x86_64/hwloc/) with box64 for testing. It raised SIGILL like this:

86920|SIGILL @0x7040b8b85618 (???(0x7040b8b85618)) (x64pc=0x3f020a7073/"/lib/libcairo.so.2 + 0xa7073", rsp=0x7040bb5fa390, stack=0x7040bae00000:0x7040bb600000 own=(nil) fp=0x7040bb5fae30), for accessing 0x7040b8b85618 (code=1/prot=7), db=0x7040b9dfef40(0x7040b8b84f80:0x7040b8b85b48/0x3f020a6f06:0x3f020a7103//lib/libcairo.so.2 + 0xa6f06:clean, hash:89047ac9/89047ac9) handler=(nil)
RAX:0x000000003744f690 RCX:0x0000000000000000 RDX:0x000000003732fa20 RBX:0x0000000037315650 
RSP:0x00007040bb5fa390 RBP:0x00007040bb5fae48 RSI:0x0000000000000042 RDI:0x00007040bb5fa340 
 R8:0x00007040bb5fa440  R9:0x000000003732f830 R10:0x000000003744f6d0 R11:0x0000000000000000 
R12:0x0000000000000001 R13:0x00000000374498d0 R14:0x000000003732fa20 R15:0x00007040bb5fa600 
ES:0x002b CS:0x0033 SS:0x002b DS:0x002b FS:0x0043 GS:0x0053 
RSP-0x20:0x00000000374497e0 RSP-0x18:0x000000003744ad90 RSP-0x10:0x00007040bb5fa600 RSP-0x08:0x0000003f020a6f06
RSP+0x00:0x0000000100000f00 RSP+0x08:0x000000003744f6d0 RSP+0x10:0x000000000000000d RSP+0x18:0x00007040bb5fae30 opcode=27 F5 05 02 27 38 05 0F (0F 84 B7 07 00)
[1]    86920 illegal hardware instruction (core dumped)  ./box64 lstopo

I found that, if I turned off rv64_vector manually, this SIGILL would not occur. So it must be something wrong in box64 with rv64_vector enabled.

Investigation

I did some investigation about this issue, and found that, if I disabled this translation via inserting return 0 after case 0x6C, this bug would not occur.

https://github.com/ptitSeb/box64/blob/fc7f83c60b82e77712165dd600501af2c59b8d3b/src/dynarec/rv64/dynarec_rv64_660f_vector.c#L180-L203

Therefore, I think this instruction (PUNPCKLQDQ) is not processed correctly. However, GDB told me that illegal instruction did not come from this process, but from

https://github.com/ptitSeb/box64/blob/fc7f83c60b82e77712165dd600501af2c59b8d3b/src/dynarec/rv64/dynarec_rv64_helper.c#L1837

Weird…

Want Help

I am not familiar with both x64 mmx and riscv vector extension, and failed to find out where the bug is in the past 2 days, so I am sending this issue and waiting for help/fix.

I checked the history of these code lines, and found PR #1755, #1811 & #1816 are related.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25 (13 by maintainers)

Most upvoted comments

BTW, I would suggest that when fixing something, related tests should be added (to avoid regression), though I don’t know whether it is hard to do so (i.e. construct a MRE and test it in CI).

Indeed, but the issues are from vector infrastructure, and they are not the same before and after the refactor, so it’s not an easy task to come up with such tests… (btw this is just me making excuses for not writing tests).

Okay, seems there is one thing I didn’t consider when refactoring, can you try this?

diff --git a/src/dynarec/rv64/dynarec_rv64_helper.c b/src/dynarec/rv64/dynarec_rv64_helper.c
index fb1ad81d..4be3f8ae 100644
--- a/src/dynarec/rv64/dynarec_rv64_helper.c
+++ b/src/dynarec/rv64/dynarec_rv64_helper.c
@@ -2528,7 +2528,7 @@ void fpu_reset_cache(dynarec_rv64_t* dyn, int ninst, int reset_n)
     #if STEP > 1
     // for STEP 2 & 3, just need to refresh with current, and undo the changes (push & swap)
     dyn->e = dyn->insts[ninst].e;
-    dyn->vector_sew = dyn->insts[ninst].vector_sew_exit;
+    dyn->vector_sew = dyn->insts[ninst].vector_sew_entry;
     #else
     dyn->e = dyn->insts[reset_n].e;
     dyn->vector_sew = dyn->insts[reset_n].vector_sew_exit;
diff --git a/src/dynarec/rv64/dynarec_rv64_pass2.h b/src/dynarec/rv64/dynarec_rv64_pass2.h
index 909522e3..4f41f628 100644
--- a/src/dynarec/rv64/dynarec_rv64_pass2.h
+++ b/src/dynarec/rv64/dynarec_rv64_pass2.h
@@ -8,6 +8,7 @@
 #define MESSAGE(A, ...) do {} while (0)
 #define EMIT(A)     do {dyn->insts[ninst].size+=4; dyn->native_size+=4;}while(0)
 #define NEW_INST                                                                                                                                                               \
+    dyn->vector_sew = dyn->insts[ninst].vector_sew_entry;                                                                                                                      \
     if (ninst) {                                                                                                                                                               \
         dyn->insts[ninst].address = (dyn->insts[ninst - 1].address + dyn->insts[ninst - 1].size);                                                                              \
         dyn->insts_size += 1 + ((dyn->insts[ninst - 1].x64.size > (dyn->insts[ninst - 1].size / 4)) ? dyn->insts[ninst - 1].x64.size : (dyn->insts[ninst - 1].size / 4)) / 15; \
diff --git a/src/dynarec/rv64/dynarec_rv64_pass3.h b/src/dynarec/rv64/dynarec_rv64_pass3.h
index 1254dc4a..556586f2 100644
--- a/src/dynarec/rv64/dynarec_rv64_pass3.h
+++ b/src/dynarec/rv64/dynarec_rv64_pass3.h
@@ -13,6 +13,7 @@
 
 #define MESSAGE(A, ...)  if(box64_dynarec_dump) dynarec_log(LOG_NONE, __VA_ARGS__)
 #define NEW_INST                                                                                                  \
+    dyn->vector_sew = dyn->insts[ninst].vector_sew_entry;                                                         \
     if (box64_dynarec_dump) print_newinst(dyn, ninst);                                                            \
     if (ninst) {                                                                                                  \
         addInst(dyn->instsize, &dyn->insts_size, dyn->insts[ninst - 1].x64.size, dyn->insts[ninst - 1].size / 4); \

can you upload the dump.txt again? I just want to make sure a few other things works as expected on the SEW transformation, thanks.

dump.tar.gz

Discard that, and try with this patch… (sorry, it’s so annoying I cannot reproduce this on my side.)

diff --git a/src/dynarec/dynarec_native_pass.c b/src/dynarec/dynarec_native_pass.c
index 24528fa1..6c4ba1f5 100644
--- a/src/dynarec/dynarec_native_pass.c
+++ b/src/dynarec/dynarec_native_pass.c
@@ -55,7 +55,7 @@ uintptr_t native_pass(dynarec_native_t* dyn, uintptr_t addr, int alternate, int
     #endif
     fpu_reset(dyn);
     ARCH_INIT();
-    int reset_n = -1;
+    int reset_n = -1; // -1 no reset; -2 reset to 0; else reset to the state of reset_n
     dyn->last_ip = (alternate || (dyn->insts && dyn->insts[0].pred_sz))?0:ip;  // RIP is always set at start of block unless there is a predecessor!
     int stopblock = 2+(FindElfAddress(my_context, addr)?0:1); // if block is in elf_memory, it can be extended with box64_dynarec_bigblock==2, else it needs 3
     // ok, go now
diff --git a/src/dynarec/rv64/dynarec_rv64_pass0.h b/src/dynarec/rv64/dynarec_rv64_pass0.h
index 782dae0b..9a84ab5b 100644
--- a/src/dynarec/rv64/dynarec_rv64_pass0.h
+++ b/src/dynarec/rv64/dynarec_rv64_pass0.h
@@ -29,7 +29,7 @@
     for (int i = 0; i < 16; ++i)                                                   \
         dyn->e.olds[i].v = 0;                                                      \
     dyn->insts[ninst].f_entry = dyn->f;                                            \
-    if (reset_n != -1)                                                             \
+    if (reset_n == -1)                                                             \
         dyn->vector_sew = ninst ? dyn->insts[ninst - 1].vector_sew : VECTOR_SEWNA; \
     if (ninst)                                                                     \
         dyn->insts[ninst - 1].x64.size = dyn->insts[ninst].x64.addr - dyn->insts[ninst - 1].x64.addr;
image

So I set up X11 forward to test again, and it’s still working (without any wrapping).