From 9b6a3b974db389cdd885aea967cbe7bfa369d17a Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Mon, 11 Nov 2019 10:35:07 +0800 Subject: [PATCH] riscv64,exec,amo: fix bug of atomicity violation * An AMO instruction may trigger page fault when storing the result back to memory. In such a case, we should not update the dest register. To fix this issue, we delay the register writeback after the store operation. --- src/isa/riscv64/exec/amo.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/isa/riscv64/exec/amo.c b/src/isa/riscv64/exec/amo.c index 8ecbc267..cef0744e 100644 --- a/src/isa/riscv64/exec/amo.c +++ b/src/isa/riscv64/exec/amo.c @@ -28,33 +28,37 @@ make_EHelper(sc) { static void inline amo_load() { rtl_lm(&s0, &id_src->val, decinfo.width); rtl_sext(&s0, &s0, decinfo.width); +} + +static void inline amo_update() { + rtl_sm(&id_src->val, &s1, decinfo.width); rtl_sr(id_dest->reg, &s0, 0); } make_EHelper(amoswap) { amo_load(); - //swap - rtl_sm(&id_src->val, &id_src2->val, decinfo.width); + rtl_mv(&s1, &id_src2->val); // swap + amo_update(); print_asm_template3(amoswap); } make_EHelper(amoadd) { amo_load(); - rtl_add(&s0, &s0, &id_src2->val); - rtl_sm(&id_src->val, &s0, decinfo.width); + rtl_add(&s1, &s0, &id_src2->val); + amo_update(); print_asm_template3(amoor); } make_EHelper(amoor) { amo_load(); - rtl_or(&s0, &s0, &id_src2->val); - rtl_sm(&id_src->val, &s0, decinfo.width); + rtl_or(&s1, &s0, &id_src2->val); + amo_update(); print_asm_template3(amoor); } make_EHelper(amoand) { amo_load(); - rtl_and(&s0, &s0, &id_src2->val); - rtl_sm(&id_src->val, &s0, decinfo.width); + rtl_and(&s1, &s0, &id_src2->val); + amo_update(); print_asm_template3(amoand); } -- GitLab