target/riscv: do not write SXL < UXL [GL4821]
Subsystems (2)
v2 cover target/riscv: do not write SXL < UXL [GL4821]
Based-on: [email protected] ("target/riscv: change mstatus bits for read-only if S ou U unsupported")
Hi,
In this v2 there are 2 Sashiko-suggested changes:
- added a "Fixes" tag;
- simplified the 'min_val' calculation when writing SXL. Sashiko said:
"- [MEDIUM] In target/riscv/tcg/csr.c (write_mstatus), SXL can be set less than UXL if RVU is absent. The min_val for SXL is only derived from UXL if RVU is present; otherwise, it defaults to 0. This allows a guest to write SXL < UXL, violating the intended fix and the hardware contract. The check should ensure min_val is derived from UXL regardless of RVU."
This won't happen in practice since we're basing this patch on a commit that will forbid UXL writes unless we have RVU, i.e. if there's no RVU we can assume UXL will be zero. But, by this same logic, this ternary is unneeded:
uint32_t min_val = mask & MSTATUS64_UXL
? get_field(val, MSTATUS64_UXL) : 0;And we should just use 'get_field(...)' directly instead. So we're ended up doing what Sashiko suggested but for other reason.
No other changes made.
Changes in v2:
- added "Fixes" tag;
- removed the ternary when assigning 'min_val' during SXL logic;
- v1 link: https://lore.kernel.org/qemu-devel/[email protected]/
Daniel Henrique Barboza (1):
target/riscv: do not write SXL < UXL [GL4821]
target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
Signature · 1 line
2.43.0
PATCH v2 1/1 target/riscv: do not write SXL < UXL [GL4821]
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
--- target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
target/riscv/tcg/csr.c +26 -13
@@ -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,17 @@ 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 =get_field (val,MSTATUS64_UXL ); + + mask |=MSTATUS64_SXL ; + val =riscv_update_xl_field (env, val,MSTATUS64_SXL , min_val); } }
@@ -5474,7 +5487,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 ) {
tests/tcg/riscv64/system/meson.build +7 -0
@@ -88,6 +88,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': {
tests/tcg/riscv64/test-uxl-sxl.S new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 31 lines · Daniel Henrique Barboza
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()toriscv_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
inwrite_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.
Quoted 7 lines
[1] https://gitlab.com/qemu-project/qemu/-/work_items/4821
Fixes ebef6685a1a2 ("target/riscv: use SXL instead of MXL for
read_sstatus")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4821
Signed-off-by: Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 170 lines
---
target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59
++++++++++++++++++++++++++++3 files changed, 92 insertions(+), 13 deletions(-)create mode 100644
tests/tcg/riscv64/test-uxl-sxl.Starget/riscv/tcg/csr.c +0
-0@@ -2049,15 +2049,25 @@ static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp, return val; }target/riscv/tcg/csr.c +1
-2-static uint64_t riscv_write_uxl (CPURISCVState *env,uint64_t val, - uint64_t field) +static uint64_t riscv_update_xl_field(CPURISCVState *env, uint64_tval,
+uint64_tfield,uint32_tmin_field_val)
{
- RISCVMXL xl =riscv_cpu_mxl(env);
-uint64_tuxl =get_field(val, field);
+ RISCVMXL mxl =riscv_cpu_mxl(env);
+uint64_txl =get_field(val, field);
+ boolfield_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;target/riscv/tcg/csr.c +0
-4@@ -2118,14 +2128,17 @@ 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); + }target/riscv/tcg/csr.c +6
-0+ + if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) { + uint32_t min_val = get_field(val, MSTATUS64_UXL); + + mask |= MSTATUS64_SXL; + val = riscv_update_xl_field(env, val, MSTATUS64_SXL,
min_val);
}
}target/riscv/tcg/csr.c +1
-1@@ -5474,7 +5487,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) {tests/tcg/riscv64/system/meson.build +0
-0@@ -88,6 +88,13 @@ tests += { } }tests/tcg/riscv64/system/meson.build +7
-0+tests += { + 'test-uxl-sxl.S': { + 'cflags': cflags, + 'qemu_args': ['-cpu', 'max', qemu_args], + } +} +if 'qemu-system-riscv64' in emulators
tcg_tests+= {
'riscv64-softmmu': {tests/tcg/riscv64/test-uxl-sxl.S new file +4
-0@@ -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
+ */tests/tcg/riscv64/test-uxl-sxl.S new file +51
-0+ +.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
Quoted 47 lines · Daniel Henrique Barboza
Based-on: [email protected] ("target/riscv: change mstatus bits for read-only if S ou U unsupported")
Hi,
In this v2 there are 2 Sashiko-suggested changes:
- added a "Fixes" tag;
- simplified the '
min_val' calculation when writing SXL. Sashiko said:"- [MEDIUM] In
target/riscv/tcg/csr.c(write_mstatus), SXL can be set less
than UXL if RVU is absent. Themin_valfor SXL is only derived from UXL
if RVU is present; otherwise, it defaults to 0. This allows a guest to
write SXL < UXL, violating the intended fix and the hardware contract.
The check should ensuremin_valis derived from UXL regardless of RVU."This won't happen in practice since we're basing this patch on a commit
that will forbid UXL writes unless we have RVU, i.e. if there's no RVU
we can assume UXL will be zero. But, by this same logic, this ternary
is unneeded:
uint32_tmin_val= mask &MSTATUS64_UXL
?get_field(val, MSTATUS64_UXL): 0;And we should just use '
get_field(...)' directly instead. So we're ended up doing what Sashiko suggested but for other reason.No other changes made.
Changes in v2:
- added "Fixes" tag;
- removed the ternary when assigning 'min_val' during SXL logic;
- v1 link:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 2 lines
Daniel Henrique Barboza (1):
target/riscv: do not write SXL < UXL [GL4821]
Why is there a GL in the commit message?
Alistair
Quoted 7 lines
target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59
++++++++++++++++++++++++++++3 files changed, 92 insertions(+), 13 deletions(-)create mode 100644
tests/tcg/riscv64/test-uxl-sxl.S
Quoted 48 lines · Alistair Francis
Quoted 47 lines · Daniel Henrique Barboza
Based-on: [email protected] ("target/riscv: change mstatus bits for read-only if S ou U unsupported")
Hi,
In this v2 there are 2 Sashiko-suggested changes:
- added a "Fixes" tag;
- simplified the '
min_val' calculation when writing SXL. Sashiko said:"- [MEDIUM] In
target/riscv/tcg/csr.c(write_mstatus), SXL can be
set less
than UXL if RVU is absent. Themin_valfor SXL is only derived from
UXL
if RVU is present; otherwise, it defaults to 0. This allows a guest
to
write SXL < UXL, violating the intended fix and the hardware
contract.
The check should ensuremin_valis derived from UXL regardless of
RVU."This won't happen in practice since we're basing this patch on a commit
that will forbid UXL writes unless we have RVU, i.e. if there's no RVU
we can assume UXL will be zero. But, by this same logic, this ternary
is unneeded:
uint32_tmin_val= mask &MSTATUS64_UXL
?get_field(val, MSTATUS64_UXL): 0;And we should just use '
get_field(...)' directly instead. So we're ended up doing what Sashiko suggested but for other reason.No other changes made.
Changes in v2:
- added "Fixes" tag;
- removed the ternary when assigning 'min_val' during SXL logic;
- v1 link:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 4 lines
Quoted 2 lines
Daniel Henrique Barboza (1):
target/riscv: do not write SXL < UXL [GL4821]Why is there a GL in the commit message?
Shorthand for "Gitlab". Makes it a bit easier to find patches that are addressing bugs in "git log --oneline" and friends instead of reading the "Resolves" tag.
Let me know if this is annoying and I'll stop :D
Daniel
Quoted 10 lines
Alistair
Quoted 7 lines
target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59 ++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 13 deletions(-)create mode 100644
tests/tcg/riscv64/test-uxl-sxl.S
Quoted 60 lines · Daniel Henrique Barboza
Quoted 57 lines · Alistair Francis
Quoted 55 lines · Daniel Henrique Barboza
Based-on: [email protected] ("target/riscv: change mstatus bits for read-only if S ou U unsupported")
Hi,
In this v2 there are 2 Sashiko-suggested changes:
- added a "Fixes" tag;
- simplified the '
min_val' calculation when writing SXL. Sashiko said:"- [MEDIUM] In
target/riscv/tcg/csr.c(write_mstatus), SXL can be
set less
than UXL if RVU is absent. Themin_valfor SXL is only derived from
UXL
if RVU is present; otherwise, it defaults to 0. This allows a guest
to
write SXL < UXL, violating the intended fix and the hardware contract.
The check should ensuremin_valis derived from UXL regardless of
RVU."This won't happen in practice since we're basing this patch on a commit
that will forbid UXL writes unless we have RVU, i.e. if there's no
RVU
we can assume UXL will be zero. But, by this same logic, this ternary
is unneeded:
uint32_tmin_val= mask &MSTATUS64_UXL
?get_field(val, MSTATUS64_UXL): 0;And we should just use '
get_field(...)' directly instead. So we're
ended up doing what Sashiko suggested but for other reason.No other changes made.
Changes in v2:
- added "Fixes" tag;
- removed the ternary when assigning 'min_val' during SXL logic;
- v1 link:
https://lore.kernel.org/qemu-devel/[email protected]/Daniel Henrique Barboza (1):
target/riscv: do not write SXL < UXL [GL4821]
Quoted 6 lines
Quoted 1 line
Why is there a GL in the commit message?
Shorthand for "Gitlab". Makes it a bit easier to find patches that are addressing
bugs in "git log --oneline" and friends instead of reading the "Resolves" tag.
Ah! Now I see
Quoted 2 lines
Let me know if this is annoying and I'll stop :D
I don't see anyone else doing this, so maybe we shouldn't stand out in RISC-V land :)
Alistair
Quoted 15 lines
Daniel
Quoted 10 lines
Alistair
Quoted 7 lines
target/riscv/tcg/csr.c | 39 ++++++++++++------ tests/tcg/riscv64/system/meson.build | 7 ++++ tests/tcg/riscv64/test-uxl-sxl.S | 59
++++++++++++++++++++++++++++3 files changed, 92 insertions(+), 13 deletions(-)create mode 100644
tests/tcg/riscv64/test-uxl-sxl.S