From d7a404f7c23aa3f71314640e1d6875148e49e311 Mon Sep 17 00:00:00 2001 From: chiangkd Date: Sun, 23 Jul 2023 04:28:28 +0800 Subject: [PATCH] Handle misalignment properly in jump-link instruction As per the specifications, if a branch or jump instruction is found to have an instruction-address-misaligned exception, it indicates that the `rd` would not be updated. To maintain simplicity, the `do_jump` function will continue to handle branch instructions, which modifications will be made exclusively to the `op_jump_link` function. Reference: https://riscv.org/technical/specifications/ --- riscv.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/riscv.c b/riscv.c index cb5438f..83916ac 100644 --- a/riscv.c +++ b/riscv.c @@ -677,9 +677,12 @@ static void do_jump(vm_t *vm, uint32_t addr) static void op_jump_link(vm_t *vm, uint32_t insn, uint32_t addr) { - /* TODO: If the jump fails (misalign), shall rd have to be updated? */ - set_dest(vm, insn, vm->pc); - do_jump(vm, addr); + if (unlikely(addr & 0b11)) { + vm_set_exception(vm, RV_EXC_PC_MISALIGN, addr); + } else { + set_dest(vm, insn, vm->pc); + vm->pc = addr; + } } #define AMO_OP(STORED_EXPR) \