target/arm: Fix SEV/WFE handling for v6K and earlier
cover target/arm: Fix SEV/WFE handling for v6K and earlier
This is an extension of my previous patch
https://patchew.org/QEMU/[email protected]/
("target/arm: Don't NOP the SEV insn on v6K CPUs") to also get the architecture version checks right on SEV, WFE and YIELD. (That then means all the insns in the NOP-hint space have checks in them). RTH suggested that a mismatch in the condition check between WFE and SEV was more important than I had thought, and I agree. A guest might choose to have spinlocks using WFE/SEV regardless of CPU architecture version, and rely on them degrading to busy-wait loops on older CPUs because both WFE/SEV are NOPs there; if we make WFE actually wait but SEV a NOP then that kind of code would deadlock.
Patch 1: same as previous, already reviewed.
Patch 2: add checks to WFE, WFI and YIELD.
Patch 3: trivial comment cleanup.
thanks
-- PMM
Peter Maydell (3):
target/arm: Don't NOP the SEV insn on v6K CPUs target/arm: Make YIELD, WFI and WFE be NOPs on pre-v6K target/arm: Remove stale comment about WFE/SEV implementation
target/arm/cpu.h | 1 - target/arm/tcg/translate.c | 48 ++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 target/arm: Don't NOP the SEV insn on v6K CPUs
When we implememented SEV to do something on A-profile rather than being a nop, we got the condition slightly wrong, and made it only effective from v7. In fact the instruction's Arm encoding has non-NOP behaviour from ARMv6K.
The effect is that a kernel boot may hang on a v6K CPU like the ARM11MPCore.
(This wouldn't have been so noticeable if we feature checked the WFE instruction, and had made the same mistake for the condition on both instructions. But we never have done the feature checks that we ought on WFE, so the mistake on SEV meant that we showed the 11mpcore guest a WFE that did something and a SEV that was a NOP.)
The v7A Arm ARM is not entirely clear about whether v6K has the Thumb SEV encoding or not: it says "ARMv7 (executes as NOP in ARMv6T2)", leaving v6K not stated. The 11MPCore TRM says it has at least WFI in both Arm and Thumb, and the v7A Arm ARM uses the same condition text for WFI, so I make the assumption that WFI, WFE, and SEV all get their functionality for both Thumb and Arm in v6K. It's possible that this differed between v6K CPUs -- the 1176 TRM says it has the v6K STREXD/STREXH/STREXB etc, but the WFI is the old-style cp15 one.
Keeping the condition check the same for both Thumb and Arm encodings is the conservative choice: if guests try to execute the Thumb SEV insn it will be because they want SEV, not because they want a NOP.
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4044
--- target/arm/tcg/translate.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
target/arm/tcg/translate.c +10 -3
@@ -3246,12 +3246,19 @@ static bool trans_YIELD(DisasContext *s, arg_YIELD *a)static bool trans_SEV (DisasContext *s,arg_SEV *a) {/* -* SEV is a NOP for user-mode emulation. For v6T2 and earlier -* non-M-profile cores this encoding is a NOP hint. +* SEV is a NOP for user-mode emulation. The instruction is +* also a NOP hint on cores that pre-date the architectural +* feature that adds it: +* - M-profile always has SEV +* - for A/R profile, it exists from v6K onward +* The v7A Arm ARM is not entirely clear about whether v6K has the +* Thumb SEV or not; we make the condition the same, to be +* conservative. (If guests try to execute the Thumb SEV insn it +* will be because they want SEV, not because they want a NOP.) */ #ifndef CONFIG_USER_ONLY if (arm_dc_feature (s,ARM_FEATURE_M ) || -arm_dc_feature (s,ARM_FEATURE_V7 )) { +arm_dc_feature (s,ARM_FEATURE_V6K )) {gen_helper_sev (tcg_env); }#endif
Signature · 1 line
2.43.0
PATCH 2/3 target/arm: Make YIELD, WFI and WFE be NOPs on pre-v6K
In reply to Peter MaydellThe YIELD, WFI and WFE instructions are in the NOP hint space, and were only defined to actual non-NOP instructions starting in the v6K architecture; they are also present for all M-profile architecture versions.
We never did check the architecture version before making these instructions have their special behaviour. Mostly this has not been a problem because a guest won't execute one of these insns unless it is prepared for it to have its usual effect, and because we implemented SEV and WFE as NOPs anyway.
Now we have implemented SEV and WFE to be more than just NOPs, it's important that we have the same condition on the SEV as the WFE, so that we either NOP both or else implement both. A guest probably won't try to use SEV/WFE on CPUs that don't implement them, but it is valid for it to do that and rely on them both being NOPs (and so a WFE-loop falls back to a pure busy-wait loop).
Add the "only if M profile or v6K or better" check to YIELD, WFE and WFI. This means that all the insns in the NOP-hint space for A32, T32 and T16 have a correct feature check.
--- target/arm/tcg/translate.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
target/arm/tcg/translate.c +26 -9
@@ -3235,10 +3235,14 @@ static bool trans_YIELD(DisasContext *s, arg_YIELD *a)* the next round-robin scheduled vCPU gets a crack. When running in * MTTCG we don't generate jumps to the helper as it won't affect the * scheduling of other vCPUs. +* This is a NOP hint on older architectures. */ -if (!(tb_cflags (s->base.tb) &CF_PARALLEL )) { -gen_update_pc (s,curr_insn_len (s)); - s->base.is_jmp =DISAS_YIELD ; +if (arm_dc_feature (s,ARM_FEATURE_M ) || +arm_dc_feature (s,ARM_FEATURE_V6K )) { +if (!(tb_cflags (s->base.tb) &CF_PARALLEL )) { +gen_update_pc (s,curr_insn_len (s)); + s->base.is_jmp =DISAS_YIELD ; + } }return true; }
@@ -3280,17 +3284,30 @@ static bool trans_SEVL(DisasContext *s, arg_SEV *a)static bool trans_WFE (DisasContext *s,arg_WFE *a) { -/* For WFE, halt the vCPU until an event. */ -gen_update_pc (s,curr_insn_len (s)); - s->base.is_jmp =DISAS_WFE ; +/* +* For WFE, halt the vCPU until an event. This is a NOP +* hint on older architectures, with the same conditions +* as SEV. +*/ +if (arm_dc_feature (s,ARM_FEATURE_M ) || +arm_dc_feature (s,ARM_FEATURE_V6K )) { +gen_update_pc (s,curr_insn_len (s)); + s->base.is_jmp =DISAS_WFE ; + }return true; }static bool trans_WFI (DisasContext *s,arg_WFI *a) { -/* For WFI, halt the vCPU until an IRQ. */ -gen_update_pc (s,curr_insn_len (s)); - s->base.is_jmp =DISAS_WFI ; +/* +* For WFI, halt the vCPU until an IRQ. This is a NOP +* hint on older architectures. +*/ +if (arm_dc_feature (s,ARM_FEATURE_M ) || +arm_dc_feature (s,ARM_FEATURE_V6K )) { +gen_update_pc (s,curr_insn_len (s)); + s->base.is_jmp =DISAS_WFI ; + }return true; }
Signature · 1 line
2.43.0
Re: [PATCH for-11.1 2/3] target/arm: Make YIELD, WFI and WFE be NOPs on pre-v6K
Quoted 22 lines · Peter Maydell
The YIELD, WFI and WFE instructions are in the NOP hint space, and were only defined to actual non-NOP instructions starting in the v6K architecture; they are also present for all M-profile architecture versions.
We never did check the architecture version before making these instructions have their special behaviour. Mostly this has not been a problem because a guest won't execute one of these insns unless it is prepared for it to have its usual effect, and because we implemented SEV and WFE as NOPs anyway.
Now we have implemented SEV and WFE to be more than just NOPs, it's important that we have the same condition on the SEV as the WFE, so that we either NOP both or else implement both. A guest probably won't try to use SEV/WFE on CPUs that don't implement them, but it is valid for it to do that and rely on them both being NOPs (and so a WFE-loop falls back to a pure busy-wait loop).
Add the "only if M profile or v6K or better" check to YIELD, WFE and WFI. This means that all the insns in the NOP-hint space for A32, T32 and T16 have a correct feature check.
Quoted 5 lines
Fixes 60e7ee5bb7cd ("target/arm: implements SEV/SEVL for all modes")Signed-off-by Peter Maydell<[email protected]>--- target/arm/tcg/translate.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-)
r~
PATCH 3/3 target/arm: Remove stale comment about WFE/SEV implementation
In reply to Peter MaydellWe forgot to remove a comment about WFE/SEV only being implemented for M-profile when we added the A-profile support for this; delete the stale text.
--- target/arm/cpu.h | 1 - 1 file changed, 1 deletion(-)
target/arm/cpu.h +0 -1
@@ -766,7 +766,6 @@ typedef struct CPUArchState {/* * The event register is shared by all ARM profiles (A/R/M), * so it is stored in the top-level CPU state. -* WFE/SEV handling is currently implemented only for M-profile. */ bool event_register;
Signature · 1 line
2.43.0
Re: [PATCH for-11.1 3/3] target/arm: Remove stale comment about WFE/SEV implementation
Quoted 8 lines · Peter Maydell
We forgot to remove a comment about WFE/SEV only being implemented for M-profile when we added the A-profile support for this; delete the stale text.
Signed-off-by Peter Maydell<[email protected]>--- target/arm/cpu.h | 1 - 1 file changed, 1 deletion(-)
r~