RISC-V priv spec says: "If S-mode is implemented, the set of legal values that the uxl field may assume excludes those that would cause UXLEN to be greater than SXLEN." At this moment there's no relation between SXL and UXL writes, i.e. we're happy to accept SXL < UXL.
We'll start by renaming riscv_write_uxl() to riscv_update_xl_field(), making it closer to what the function actually does. Then, in the same function: - 'xl' is renamed to 'mlx' since it stores riscv_cpu_mxl(); - 'uxl' is renamed to 'xl', indicating that the function is dealing with multiple XL files (SXL, UXL, VSSTATUS.UXL ...); - add a new 'min_field_val' field. With this improved helper what's left is to change SXL/UXL update order in write_mstatus() and, when updating SXL, we'll use the written UXL value as a minimal value for SXL. This means that if we're in a SXL < UXL case, e.g. SXL = 1 and UXL = 2, we'll write SXL = 2/UXL = 2. Another possibility would be to decrement UXL and write SXL = 1/UXL = 1, but the bug report in [1] mentions that bumping SXL is the default behavior of other emulators. For the sake of compatibility we're doing the same. [1] https://gitlab.com/qemu-project/qemu/-/work_items/4821 Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4821 Signed-off-by: Daniel Henrique Barboza <[email protected]> --- target/riscv/tcg/csr.c | 40 +++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59 ++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c index 24eda54bac..c54fa026a2 100644 --- a/target/riscv/tcg/csr.c +++ b/target/riscv/tcg/csr.c @@ -2049,15 +2049,25 @@ static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp, return val; } -static uint64_t riscv_write_uxl(CPURISCVState *env, uint64_t val, - uint64_t field) +static uint64_t riscv_update_xl_field(CPURISCVState *env, uint64_t val, + uint64_t field, uint32_t min_field_val) { - RISCVMXL xl = riscv_cpu_mxl(env); - uint64_t uxl = get_field(val, field); + RISCVMXL mxl = riscv_cpu_mxl(env); + uint64_t xl = get_field(val, field); + bool field_updated = false; + + if (xl < min_field_val) { + xl = min_field_val; + field_updated = true; + } - if (xl != MXL_RV128 && uxl == MXL_RV128) { - uxl = xl; - val = set_field(val, field, uxl); + if (mxl != MXL_RV128 && xl == MXL_RV128) { + xl = mxl; + field_updated = true; + } + + if (field_updated) { + val = set_field(val, field, xl); } return val; @@ -2118,14 +2128,18 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno, if (riscv_has_ext(env, RVH)) { mask |= MSTATUS_MPV | MSTATUS_GVA; } - if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) { - mask |= MSTATUS64_SXL; - val = riscv_write_uxl(env, val, MSTATUS64_SXL); - } if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) { mask |= MSTATUS64_UXL; - val = riscv_write_uxl(env, val, MSTATUS64_UXL); + val = riscv_update_xl_field(env, val, MSTATUS64_UXL, 0); + } + + if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) { + uint32_t min_val = mask & MSTATUS64_UXL + ? get_field(val, MSTATUS64_UXL) : 0; + + mask |= MSTATUS64_SXL; + val = riscv_update_xl_field(env, val, MSTATUS64_SXL, min_val); } } @@ -5474,7 +5488,7 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno, if ((val & VSSTATUS64_UXL) == 0) { mask &= ~VSSTATUS64_UXL; } else { - val = riscv_write_uxl(env, val, VSSTATUS64_UXL); + val = riscv_update_xl_field(env, val, VSSTATUS64_UXL, 0); } if ((env->henvcfg & HENVCFG_DTE)) { if ((val & SSTATUS_SDT) != 0) { diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/system/meson.build index 3c8b018d9e..65bcc2bdbc 100644 --- a/tests/tcg/riscv64/system/meson.build +++ b/tests/tcg/riscv64/system/meson.build @@ -89,6 +89,13 @@ tests += { } } +tests += { + 'test-uxl-sxl.S': { + 'cflags': cflags, + 'qemu_args': ['-cpu', 'max', qemu_args], + } +} + if 'qemu-system-riscv64' in emulators tcg_tests += { 'riscv64-softmmu': { diff --git a/tests/tcg/riscv64/test-uxl-sxl.S b/tests/tcg/riscv64/test-uxl-sxl.S new file mode 100644 index 0000000000..54dc0832b3 --- /dev/null +++ b/tests/tcg/riscv64/test-uxl-sxl.S @@ -0,0 +1,59 @@ +/* + * Test that UXL will never be read as greater than SXL. + * + * Based on Gitlab 4821 testcase, which is derived from riscv-arch-test. + * + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +.option norvc + .text + .global _start + +_start: + lla t0, trap + csrw mtvec, t0 + + csrr s0, mstatus + li t0, 0xf + slli t0, t0, 32 + not t0, t0 + and t1, s0, t0 + li t2, 0x6 // SXL = 01, UXL = 10 + slli t2, t2, 32 + or t1, t1, t2 + csrw mstatus, t1 + csrr s1, mstatus + csrw mstatus, s0 // restore + + srli t1, s1, 34 + andi t1, t1, 3 // SXL + srli t2, s1, 32 + andi t2, t2, 3 // UXL + sltu a0, t1, t2 // fail if SXL < UXL + j _exit + +trap: + # Any trap is a fail code 1 + li a0, 1 + +# Exit code in a0 +_exit: + lla a1, semiargs + li t0, 0x20026 # ADP_Stopped_ApplicationExit + sd t0, 0(a1) + sd a0, 8(a1) + li a0, 0x20 # TARGET_SYS_EXIT_EXTENDED + + # Semihosting call sequence + .balign 16 + slli zero, zero, 0x1f + ebreak + srai zero, zero, 0x7 + j . + + .data + .balign 16 +semiargs: + .space 16 -- 2.43.0
