[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] riscv: Fix _movi_p loading incorrect value
From: |
Kim Kuparinen |
Subject: |
[PATCH] riscv: Fix _movi_p loading incorrect value |
Date: |
Mon, 9 May 2022 22:41:38 +0300 |
Signed-off-by: Kim Kuparinen <kimi.h.kuparinen@gmail.com>
---
Hi, I recently sent out a misleading version of this patch. There is an
issue with how _movi_p handles 64bit addresses whose low 32bits would be
sign-extended, essentially following the logic in the previous email:
https://lists.gnu.org/archive/html/lightning/2021-11/msg00005.html
> movi_p a0, 0b0010 1001
>
> is expanded to
>
> // lo
> lui r0, 0b10 // r0 => 0b1111 1000
> addiw r0, 0b01 // r0 => 0b1111 1001
> // hi
> lui a0, 0b00 // a0 => 0b0000 0000
> addiw a0, b10 // a0 => 0b0000 0010
> slli a0, 4 // a0 => 0b0010 0000
> add a0, a0, r0 // a0 => 0b0001 1001
Previously I incorrectly assumed this also related to movi, which it doesn't,
sorry
about that. The errors I encountered were segmentation faults from incorrect
addresses
on qemu-system-riscv64 v7.0.0 with kernel 5.16.0-6-riscv64, and this was a
minimal
example I could come up with:
> #include <lightning.h>
>
> static jit_state_t *_jit;
>
> typedef int (*f_t)();
>
> int main()
> {
> init_jit(0);
> _jit = jit_new_state();
>
> jit_prolog();
> jit_node_t *addr = jit_movi(JIT_R0, 0);
> jit_jmpr(JIT_R0);
> jit_patch(addr); // addr = 0x3ff7fdc02c
>
> jit_reti(0);
>
> f_t f = (f_t)jit_emit();
> jit_clear_state();
>
> f();
> }
In my case, the jit'ed code seems to generally be placed around 0x3ffxxxxxxx,
which
triggers this bug, but I wouldn't be surprised if some other system place the
code somewhere else and don't trigger it.
The testsuite was also failing on almost all tests until I applied this patch,
now
all tests pass on my machine.
If you still consider this patch incorrect, please at least take this as a bug
report :)
lib/jit_riscv-cpu.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/lib/jit_riscv-cpu.c b/lib/jit_riscv-cpu.c
index 388489f..821c922 100644
--- a/lib/jit_riscv-cpu.c
+++ b/lib/jit_riscv-cpu.c
@@ -1363,6 +1363,10 @@ _movi_p(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
LUI(r0, hi >> 12);
ADDIW(r0, r0, lo);
ww = i0 >> 32;
+
+ if(hi < 0)
+ ww++;
+
lo = ww << 20 >> 20;
hi = ww - lo;
LUI(rn(t0), hi >> 12);
@@ -2331,6 +2335,10 @@ _patch_at(jit_state_t *_jit, jit_word_t instr,
jit_word_t label)
i.w = u.i[2];
if (i.U.opcode == 55) { /* LUI */
ww = label >> 32;
+
+ if(hi < 0)
+ ww++;
+
lo = ww << 20 >> 20;
hi = ww - lo;
i.U.imm12_31 = hi >> 12;
--
2.35.1
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] riscv: Fix _movi_p loading incorrect value,
Kim Kuparinen <=