linux-user/aarch64: Fix SME/SME2 signal frame handling
v2 cover linux-user/aarch64: Fix SME/SME2 signal frame handling
This patchset fixes some bugs in our SME-related signal frame handling for linux-user:
* we need to make the equivalent change to a recent kernel bugfix/ABI change: TPIDR2_EL0 should be cleared when delivering a signal
* we forgot the TPIDR2_MAGIC signal frame record (which is necessary for SME v1)
* we forgot the ZT_MAGIC signal frame record when implementing SME2
These bugs generally only surface when guest code attempts to unwind an exception from inside a signal handler and SME is involved. Discovered (and the fixes tested) by some new gcc test cases which implement their part of the bugfix/ABI change
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b5ffc8e75a8
v2: quick respin to address RTH's code review comments. I know I only sent v1 earlier this evening but I wanted to get this out of the door as I'm not going to be working the first half of next week (back Thurs).
v1->v2:
* patch 1: drop unnecessary if(), update comment
* patch 2: do "has sme" check in better place, drop unneeded argument, return type
* patch 3: do "has sme" check in better place, check the incoming SVCR value, not the old one
thanks
-- PMM
Peter Maydell (3):
linux-user/aarch64: Clear TPIDR2_EL0 when delivering signals linux-user/aarch64: Support TPIDR2_MAGIC signal frame record linux-user/aarch64: Support ZT_MAGIC signal frame record
linux-user/aarch64/signal.c | 139 +++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 linux-user/aarch64: Clear TPIDR2_EL0 when delivering signals
A recent change to the kernel (Linux commit b376108e1f88 "arm64/fpsimd: signal: Clear TPIDR2 when delivering signals") updated the signal-handler entry code to always clear TPIDR2_EL0.
This is necessary for the userspace ZA lazy saving scheme to work correctly when unwinding exceptions across a signal boundary. (For the essay-length description of the incorrect behaviour and why this is the correct fix, see the commit message for the kernel commit.)
Make QEMU also clear TPIDR2_EL0 on signal entry, applying the equivalent bugfix to our implementation.
Note that getting this unwinding to work correctly also requires changes to the userspace code, e.g. as implemented in gcc in
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b5ffc8e75a8
This change is technically an ABI change; from the kernel's point of view SME was never enabled (it was hidden behind CONFIG_BROKEN) before the change. From QEMU's point of view our SME-related signal handling was broken anyway as we weren't saving and restoring TPIDR2_EL0.
--- linux-user/aarch64/signal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
linux-user/aarch64/signal.c +5 -1
@@ -666,8 +666,12 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, env->btype =2 ; } -/* Invoke the signal handler with both SM and ZA disabled. */ +/* +* Invoke the signal handler with a clean SME state: both SM and ZA +* disabled and TPIDR2_EL0 cleared. +*/ aarch64_set_svcr (env,0 ,R_SVCR_SM_MASK |R_SVCR_ZA_MASK ); + env->cp15.tpidr2_el0 =0 ;if (info) { frame->info = *info;
Signature · 1 line
2.43.0
Re: [PATCH v2 for-10.1 1/3] linux-user/aarch64: Clear TPIDR2_EL0 when delivering signals
Quoted 23 lines · Peter Maydell
A recent change to the kernel (Linux commit
b376108e1f88"arm64/fpsimd: signal: Clear TPIDR2 when delivering signals") updated the signal-handler entry code to always clearTPIDR2_EL0.This is necessary for the userspace ZA lazy saving scheme to work correctly when unwinding exceptions across a signal boundary. (For the essay-length description of the incorrect behaviour and why this is the correct fix, see the commit message for the kernel commit.)
Make QEMU also clear
TPIDR2_EL0on signal entry, applying the equivalent bugfix to our implementation.Note that getting this unwinding to work correctly also requires changes to the userspace code, e.g. as implemented in gcc in
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b5ffc8e75a8This change is technically an ABI change; from the kernel's point of view SME was never enabled (it was hidden behind
CONFIG_BROKEN) before the change. From QEMU's point of view our SME-related signal handling was broken anyway as we weren't saving and restoringTPIDR2_EL0.
Quoted 7 lines
Fixes 78011586b90d1 ("target/arm: Enable SME for user-only")Signed-off-by Peter Maydell <[email protected]>Reviewed-by Richard Henderson <[email protected]>--- linux-user/aarch64/signal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
Re: [PATCH v2 for-10.1 1/3] linux-user/aarch64: Clear TPIDR2_EL0 when delivering signals
In reply to Peter MaydellQuoted 17 lines · Peter Maydell
A recent change to the kernel (Linux commit
b376108e1f88"arm64/fpsimd: signal: Clear TPIDR2 when delivering signals") updated the signal-handler entry code to always clearTPIDR2_EL0.This is necessary for the userspace ZA lazy saving scheme to work correctly when unwinding exceptions across a signal boundary. (For the essay-length description of the incorrect behaviour and why this is the correct fix, see the commit message for the kernel commit.)
Make QEMU also clear
TPIDR2_EL0on signal entry, applying the equivalent bugfix to our implementation.Note that getting this unwinding to work correctly also requires changes to the userspace code, e.g. as implemented in gcc in
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b5ffc8e75a8
Quoted 8 lines
This change is technically an ABI change; from the kernel's point of view SME was never enabled (it was hidden behind
CONFIG_BROKEN) before the change. From QEMU's point of view our SME-related signal handling was broken anyway as we weren't saving and restoringTPIDR2_EL0.Fixes 78011586b90d1 ("target/arm: Enable SME for user-only")
Is it worth the efforts to apply this one to qemu 7.2.x branch? If yes, I'd love to have it back-ported to that one :)
Thanks,
/mjt
Re: [PATCH v2 for-10.1 1/3] linux-user/aarch64: Clear TPIDR2_EL0 when delivering signals
Quoted 25 lines · Michael Tokarev
Quoted 23 lines · Peter Maydell
A recent change to the kernel (Linux commit
b376108e1f88"arm64/fpsimd: signal: Clear TPIDR2 when delivering signals") updated the signal-handler entry code to always clearTPIDR2_EL0.This is necessary for the userspace ZA lazy saving scheme to work correctly when unwinding exceptions across a signal boundary. (For the essay-length description of the incorrect behaviour and why this is the correct fix, see the commit message for the kernel commit.)
Make QEMU also clear
TPIDR2_EL0on signal entry, applying the equivalent bugfix to our implementation.Note that getting this unwinding to work correctly also requires changes to the userspace code, e.g. as implemented in gcc in
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b5ffc8e75a8This change is technically an ABI change; from the kernel's point of view SME was never enabled (it was hidden behind
CONFIG_BROKEN) before the change. From QEMU's point of view our SME-related signal handling was broken anyway as we weren't saving and restoringTPIDR2_EL0.
Quoted 4 lines
Quoted 2 lines
Fixes 78011586b90d1 ("target/arm: Enable SME for user-only")Is it worth the efforts to apply this one to qemu 7.2.x branch?
Well, it's an easy backport since it's a one-liner, and it's not a complicated change so it's pretty safe. There's no point unless you're also backporting patch 2 of this series, though.
In the 7.2.x version of target_setup_frame() the "clear SVCR bits" code is a little different, but it's still there; the change should go after this part:
if (env->svcr) {
env->svcr = 0;
arm_rebuild_hflags(env);
}(there isn't actually an ordering requirement, so it's just neater to put it in the same logical place)
-- PMM
PATCH v2 2/3 linux-user/aarch64: Support TPIDR2_MAGIC signal frame record
In reply to Peter MaydellFEAT_SME adds the TPIDR2 userspace-accessible system register, which is used as part of the procedure calling standard's lazy saving scheme for the ZA registers:
https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#66the-za-lazy-saving-scheme
The Linux kernel has a signal frame record for saving and restoring this value when calling signal handlers, but we forgot to implement this. The result is that code which tries to unwind an exception out of a signal handler will not work correctly.
Add support for the missing record.
--- linux-user/aarch64/signal.c | 42 +++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
linux-user/aarch64/signal.c +40 -2
@@ -121,6 +121,13 @@ struct target_za_context {#define TARGET_ZA_SIG_CONTEXT_SIZE (VQ ) \ TARGET_ZA_SIG_ZAV_OFFSET(VQ, VQ * TARGET_SVE_VQ_BYTES) +#define TARGET_TPIDR2_MAGIC 0x54504902 + +struct target_tpidr2_context { +struct target_aarch64_ctx head; +uint64_t tpidr2; +}; +struct target_rt_sigframe {struct target_siginfo info;struct target_ucontext uc;
@@ -253,6 +260,14 @@ static void target_setup_za_record(struct target_za_context *za, } } +static void target_setup_tpidr2_record (struct target_tpidr2_context *tpidr2, +CPUARMState *env) +{ +__put_user (TARGET_TPIDR2_MAGIC , &tpidr2->head.magic); +__put_user (sizeof (struct target_tpidr2_context ), &tpidr2->head.size); +__put_user (env->cp15.tpidr2_el0, &tpidr2->tpidr2); +} +static void target_restore_general_frame (CPUARMState *env,struct target_rt_sigframe *sf) {
@@ -403,6 +418,12 @@ static bool target_restore_za_record(CPUARMState *env,return true; } +static void target_restore_tpidr2_record (CPUARMState *env, +struct target_tpidr2_context *tpidr2) +{ +__get_user (env->cp15.tpidr2_el0, &tpidr2->tpidr2); +} +static int target_restore_sigframe (CPUARMState *env,struct target_rt_sigframe *sf) {
@@ -410,6 +431,7 @@ static int target_restore_sigframe(CPUARMState *env,struct target_fpsimd_context *fpsimd =NULL ;struct target_sve_context *sve =NULL ;struct target_za_context *za =NULL ; +struct target_tpidr2_context *tpidr2 =NULL ;uint64_t extra_datap =0 ;bool used_extra = false;int sve_size =0 ;
@@ -460,6 +482,14 @@ static int target_restore_sigframe(CPUARMState *env, za_size = size;break ; +case TARGET_TPIDR2_MAGIC : +if (tpidr2 || size !=sizeof (struct target_tpidr2_context ) || + !cpu_isar_feature (aa64_sme,env_archcpu (env))) { + goto err; + } + tpidr2 = (struct target_tpidr2_context *)ctx; +break ; +case TARGET_EXTRA_MAGIC :if (extra || size !=sizeof (struct target_extra_context )) { goto err;
@@ -497,6 +527,9 @@ static int target_restore_sigframe(CPUARMState *env,if (za && !target_restore_za_record (env, za, za_size, &svcr)) { goto err; } +if (tpidr2) { +target_restore_tpidr2_record (env, tpidr2); + }if (env->svcr != svcr) { env->svcr = svcr;arm_rebuild_hflags (env);
@@ -568,8 +601,8 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, .total_size = offsetof(struct target_rt_sigframe, uc.tuc_mcontext.__reserved), }; -int fpsimd_ofs, fr_ofs, sve_ofs =0 , za_ofs =0 ; -int sve_size =0 , za_size =0 ; +int fpsimd_ofs, fr_ofs, sve_ofs =0 , za_ofs =0 , tpidr2_ofs =0 ; +int sve_size =0 , za_size =0 , tpidr2_size =0 ;struct target_rt_sigframe *frame;struct target_rt_frame_record *fr;abi_ulong frame_addr, return_addr;
@@ -585,6 +618,8 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, sve_ofs =alloc_sigframe_space (sve_size, &layout); }if (cpu_isar_feature (aa64_sme,env_archcpu (env))) { + tpidr2_size =sizeof (struct target_tpidr2_context ); + tpidr2_ofs =alloc_sigframe_space (tpidr2_size, &layout);/* ZA state needs saving only if it is enabled. */ if (FIELD_EX64 (env->svcr,SVCR ,ZA )) { za_size =TARGET_ZA_SIG_CONTEXT_SIZE (sme_vq (env));
@@ -644,6 +679,9 @@ static void target_setup_frame(int usig, struct target_sigaction *ka,if (za_ofs) {target_setup_za_record ((void *)frame + za_ofs, env, za_size); } +if (tpidr2_ofs) { +target_setup_tpidr2_record ((void *)frame + tpidr2_ofs, env); + }/* Set up the stack frame for unwinding. */ fr = (void *)frame + fr_ofs;
Signature · 1 line
2.43.0
Re: [PATCH v2 for-10.1 2/3] linux-user/aarch64: Support TPIDR2_MAGIC signal frame record
Quoted 11 lines · Peter Maydell
FEAT_SMEadds the TPIDR2 userspace-accessible system register, which is used as part of the procedure calling standard's lazy saving scheme for the ZA registers:
https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#66the-za-lazy-saving-schemeThe Linux kernel has a signal frame record for saving and restoring this value when calling signal handlers, but we forgot to implement this. The result is that code which tries to unwind an exception out of a signal handler will not work correctly.
Quoted 8 lines
Add support for the missing record.
Fixes 78011586b90d1 ("target/arm: Enable SME for user-only")Signed-off-by Peter Maydell <[email protected]>--- linux-user/aarch64/signal.c | 42 +++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
Re: [PATCH v2 for-10.1 2/3] linux-user/aarch64: Support TPIDR2_MAGIC signal frame record
In reply to Peter MaydellQuoted 12 lines · Peter Maydell
FEAT_SMEadds the TPIDR2 userspace-accessible system register, which is used as part of the procedure calling standard's lazy saving scheme for the ZA registers:
https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#66the-za-lazy-saving-
schemeThe Linux kernel has a signal frame record for saving and restoring this value when calling signal handlers, but we forgot to implement this. The result is that code which tries to unwind an exception out of a signal handler will not work correctly.
Quoted 8 lines
Add support for the missing record.
Fixes 78011586b90d1 ("target/arm: Enable SME for user-only")Signed-off-by Peter Maydell<[email protected]>--- linux-user/aarch64/signal.c | 42 +++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
r~
PATCH v2 3/3 linux-user/aarch64: Support ZT_MAGIC signal frame record
In reply to Peter MaydellFEAT_SME2 adds the ZT0 register, whose contents may need to be preserved and restored on signal handler entry and exit. This is done with a new ZT_MAGIC record. We forgot to implement support for this in our linux-user code before enabling the SME2p1 emulation, which meant that a signal handler using SME would corrupt the ZT0 register value, and code that attempted to unwind an exception from inside a signal handler would not work.
Add the missing record handling.
--- linux-user/aarch64/signal.c | 93 ++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-)
linux-user/aarch64/signal.c +92 -1
@@ -128,6 +128,23 @@ struct target_tpidr2_context {uint64_t tpidr2; }; +#define TARGET_ZT_MAGIC 0x5a544e01 + +struct target_zt_context { +struct target_aarch64_ctx head; +uint16_t nregs; +uint16_t reserved[3 ]; +/* ZTn register data immediately follows */ +}; + +#define TARGET_ZT_SIG_REG_BYTES (512 / 8) +#define TARGET_ZT_SIG_REGS_SIZE (n) (TARGET_ZT_SIG_REG_BYTES * (n)) +#define TARGET_ZT_SIG_CONTEXT_SIZE (n) (sizeof(struct target_zt_context) + \ + TARGET_ZT_SIG_REGS_SIZE(n)) +#define TARGET_ZT_SIG_REGS_OFFSET sizeof(struct target_zt_context) +QEMU_BUILD_BUG_ON (TARGET_ZT_SIG_REG_BYTES != \ +sizeof_field (CPUARMState, za_state.zt0)); +struct target_rt_sigframe {struct target_siginfo info;struct target_ucontext uc;
@@ -268,6 +285,28 @@ static void target_setup_tpidr2_record(struct target_tpidr2_context *tpidr2,__put_user (env->cp15.tpidr2_el0, &tpidr2->tpidr2); } +static void target_setup_zt_record (struct target_zt_context *zt, +CPUARMState *env,int size) +{ +uint64_t *z; + +memset (zt,0 ,sizeof (*zt)); +__put_user (TARGET_ZT_MAGIC , &zt->head.magic); +__put_user (size, &zt->head.size); +/* +* The record format allows for multiple ZT regs, but +* currently there is only one, ZT0. +*/ +__put_user (1 , &zt->nregs); +assert (size ==TARGET_ZT_SIG_CONTEXT_SIZE (1 )); + +/* ZT0 is the same byte-stream format as SVE regs and ZA */ + z = (void *)zt +TARGET_ZT_SIG_REGS_OFFSET ; +for (int i =0 ; i <ARRAY_SIZE (env->za_state.zt0); i++) { +__put_user_e (env->za_state.zt0[i], z + i, le); + } +} +static void target_restore_general_frame (CPUARMState *env,struct target_rt_sigframe *sf) {
@@ -424,6 +463,30 @@ static void target_restore_tpidr2_record(CPUARMState *env,__get_user (env->cp15.tpidr2_el0, &tpidr2->tpidr2); } +static bool target_restore_zt_record (CPUARMState *env, +struct target_zt_context *zt,int size, +int svcr) +{ +uint16_t nregs; +uint64_t *z; + +if (!(FIELD_EX64 (svcr,SVCR ,ZA ))) { +return false; + } + +__get_user (nregs, &zt->nregs); + +if (nregs !=1 ) { +return false; + } + + z = (void *)zt +TARGET_ZT_SIG_REGS_OFFSET ; +for (int i =0 ; i <ARRAY_SIZE (env->za_state.zt0); i++) { +__get_user_e (env->za_state.zt0[i], z + i, le); + } +return true; +} +static int target_restore_sigframe (CPUARMState *env,struct target_rt_sigframe *sf) {
@@ -432,10 +495,12 @@ static int target_restore_sigframe(CPUARMState *env,struct target_sve_context *sve =NULL ;struct target_za_context *za =NULL ;struct target_tpidr2_context *tpidr2 =NULL ; +struct target_zt_context *zt =NULL ;uint64_t extra_datap =0 ;bool used_extra = false;int sve_size =0 ;int za_size =0 ; +int zt_size =0 ;int svcr =0 ;target_restore_general_frame (env, sf);
@@ -490,6 +555,15 @@ static int target_restore_sigframe(CPUARMState *env, tpidr2 = (struct target_tpidr2_context *)ctx;break ; +case TARGET_ZT_MAGIC : +if (zt || size !=TARGET_ZT_SIG_CONTEXT_SIZE (1 ) || + !cpu_isar_feature (aa64_sme2,env_archcpu (env))) { + goto err; + } + zt = (struct target_zt_context *)ctx; + zt_size = size; +break ; +case TARGET_EXTRA_MAGIC :if (extra || size !=sizeof (struct target_extra_context )) { goto err;
@@ -530,6 +604,13 @@ static int target_restore_sigframe(CPUARMState *env,if (tpidr2) {target_restore_tpidr2_record (env, tpidr2); } +/* +* NB that we must restore ZT after ZA so the check that there's +* no ZT record if SVCR.ZA is 0 gets the right value of SVCR. +*/ +if (zt && !target_restore_zt_record (env, zt, zt_size, svcr)) { + goto err; + }if (env->svcr != svcr) { env->svcr = svcr;arm_rebuild_hflags (env);
@@ -602,7 +683,8 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, uc.tuc_mcontext.__reserved), };int fpsimd_ofs, fr_ofs, sve_ofs =0 , za_ofs =0 , tpidr2_ofs =0 ; -int sve_size =0 , za_size =0 , tpidr2_size =0 ; +int zt_ofs =0 ; +int sve_size =0 , za_size =0 , tpidr2_size =0 , zt_size =0 ;struct target_rt_sigframe *frame;struct target_rt_frame_record *fr;abi_ulong frame_addr, return_addr;
@@ -628,6 +710,12 @@ static void target_setup_frame(int usig, struct target_sigaction *ka, } za_ofs =alloc_sigframe_space (za_size, &layout); } +if (cpu_isar_feature (aa64_sme2,env_archcpu (env)) && +FIELD_EX64 (env->svcr,SVCR ,ZA )) { +/* If SME ZA storage is enabled, we must also save SME2 ZT0 */ + zt_size =TARGET_ZT_SIG_CONTEXT_SIZE (1 ); + zt_ofs =alloc_sigframe_space (zt_size, &layout); + }if (layout.extra_ofs) {/* Reserve space for the extra end marker. The standard end marker
@@ -682,6 +770,9 @@ static void target_setup_frame(int usig, struct target_sigaction *ka,if (tpidr2_ofs) {target_setup_tpidr2_record ((void *)frame + tpidr2_ofs, env); } +if (zt_ofs) { +target_setup_zt_record ((void *)frame + zt_ofs, env, zt_size); + }/* Set up the stack frame for unwinding. */ fr = (void *)frame + fr_ofs;
Signature · 1 line
2.43.0
Re: [PATCH v2 for-10.1 3/3] linux-user/aarch64: Support ZT_MAGIC signal frame record
Quoted 8 lines · Peter Maydell
FEAT_SME2adds the ZT0 register, whose contents may need to be preserved and restored on signal handler entry and exit. This is done with a newZT_MAGICrecord. We forgot to implement support for this in our linux-user code before enabling the SME2p1 emulation, which meant that a signal handler using SME would corrupt the ZT0 register value, and code that attempted to unwind an exception from inside a signal handler would not work.
Quoted 7 lines
Add the missing record handling.
Fixes 7b1613a1020d2942 ("target/arm: Enable FEAT_SME2p1 on -cpu max")Signed-off-by Peter Maydell <[email protected]>--- linux-user/aarch64/signal.c | 93 ++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-)
Re: [PATCH v2 for-10.1 3/3] linux-user/aarch64: Support ZT_MAGIC signal frame record
In reply to Peter MaydellQuoted 8 lines · Peter Maydell
FEAT_SME2adds the ZT0 register, whose contents may need to be preserved and restored on signal handler entry and exit. This is done with a newZT_MAGICrecord. We forgot to implement support for this in our linux-user code before enabling the SME2p1 emulation, which meant that a signal handler using SME would corrupt the ZT0 register value, and code that attempted to unwind an exception from inside a signal handler would not work.
Quoted 7 lines
Add the missing record handling.
Fixes 7b1613a1020d2942 ("target/arm: Enable FEAT_SME2p1 on -cpu max")Signed-off-by Peter Maydell<[email protected]>--- linux-user/aarch64/signal.c | 93 ++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-)
r~