Message ID | 20211015041053.2769193-65-richard.henderson@linaro.org |
---|---|
State | Superseded |
Headers | show |
Series | user-only: Cleanup SIGSEGV and SIGBUS handling | expand |
On Thu, Oct 14, 2021 at 10:14 PM Richard Henderson < richard.henderson@linaro.org> wrote: > This requires extra work for each target, but adds the > common syscall code, and the necessary flag in CPUState. > > Signed-off-by: Richard Henderson <richard.henderson@linaro.org> > --- > include/hw/core/cpu.h | 3 +++ > linux-user/generic/target_prctl_unalign.h | 27 +++++++++++++++++++++++ > cpu.c | 20 ++++++++++++----- > linux-user/syscall.c | 13 +++++++++-- > 4 files changed, 56 insertions(+), 7 deletions(-) > create mode 100644 linux-user/generic/target_prctl_unalign.h > Reviewed-by: Warner Losh <imp@bsdimp.com> > diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h > index 1a10497af3..6202bbf9c3 100644 > --- a/include/hw/core/cpu.h > +++ b/include/hw/core/cpu.h > @@ -412,6 +412,9 @@ struct CPUState { > > bool ignore_memory_transaction_failures; > > + /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */ > + bool prctl_unalign_sigbus; > + > struct hax_vcpu_state *hax_vcpu; > > struct hvf_vcpu_state *hvf; > diff --git a/linux-user/generic/target_prctl_unalign.h > b/linux-user/generic/target_prctl_unalign.h > new file mode 100644 > index 0000000000..bc3b83af2a > --- /dev/null > +++ b/linux-user/generic/target_prctl_unalign.h > @@ -0,0 +1,27 @@ > +/* > + * Generic prctl unalign functions for linux-user > + * > + * SPDX-License-Identifier: GPL-2.0-or-later > + */ > +#ifndef GENERIC_TARGET_PRCTL_UNALIGN_H > +#define GENERIC_TARGET_PRCTL_UNALIGN_H > + > +static abi_long do_prctl_get_unalign(CPUArchState *env, target_long arg2) > +{ > + CPUState *cs = env_cpu(env); > + uint32_t res = PR_UNALIGN_NOPRINT; > + if (cs->prctl_unalign_sigbus) { > + res |= PR_UNALIGN_SIGBUS; > + } > + return put_user_u32(res, arg2); > +} > +#define do_prctl_get_unalign do_prctl_get_unalign > + > +static abi_long do_prctl_set_unalign(CPUArchState *env, target_long arg2) > +{ > + env_cpu(env)->prctl_unalign_sigbus = arg2 & PR_UNALIGN_SIGBUS; > + return 0; > +} > +#define do_prctl_set_unalign do_prctl_set_unalign > + > +#endif /* GENERIC_TARGET_PRCTL_UNALIGN_H */ > diff --git a/cpu.c b/cpu.c > index 9bce67ef55..9e388d9cd3 100644 > --- a/cpu.c > +++ b/cpu.c > @@ -179,13 +179,23 @@ void cpu_exec_unrealizefn(CPUState *cpu) > cpu_list_remove(cpu); > } > > +/* > + * This can't go in hw/core/cpu.c because that file is compiled only > + * once for both user-mode and system builds. > + */ > static Property cpu_common_props[] = { > -#ifndef CONFIG_USER_ONLY > +#ifdef CONFIG_USER_ONLY > /* > - * Create a memory property for softmmu CPU object, > - * so users can wire up its memory. (This can't go in hw/core/cpu.c > - * because that file is compiled only once for both user-mode > - * and system builds.) The default if no link is set up is to use > + * Create a property for the user-only object, so users can > + * adjust prctl(PR_SET_UNALIGN) from the command-line. > + * Has no effect if the target does not support the feature. > + */ > + DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState, > + prctl_unalign_sigbus, false), > +#else > + /* > + * Create a memory property for softmmu CPU object, so users can > + * wire up its memory. The default if no link is set up is to use > * the system address space. > */ > DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION, > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 7635c2397a..ac3bc8a330 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -6375,6 +6375,12 @@ static abi_long do_prctl_inval1(CPUArchState *env, > abi_long arg2) > #ifndef do_prctl_get_tagged_addr_ctrl > #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0 > #endif > +#ifndef do_prctl_get_unalign > +#define do_prctl_get_unalign do_prctl_inval1 > +#endif > +#ifndef do_prctl_set_unalign > +#define do_prctl_set_unalign do_prctl_inval1 > +#endif > > static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long > arg2, > abi_long arg3, abi_long arg4, abi_long arg5) > @@ -6438,6 +6444,11 @@ static abi_long do_prctl(CPUArchState *env, > abi_long option, abi_long arg2, > } > return do_prctl_get_tagged_addr_ctrl(env); > > + case PR_GET_UNALIGN: > + return do_prctl_get_unalign(env, arg2); > + case PR_SET_UNALIGN: > + return do_prctl_set_unalign(env, arg2); > + > case PR_GET_DUMPABLE: > case PR_SET_DUMPABLE: > case PR_GET_KEEPCAPS: > @@ -6480,8 +6491,6 @@ static abi_long do_prctl(CPUArchState *env, abi_long > option, abi_long arg2, > case PR_SET_THP_DISABLE: > case PR_GET_TSC: > case PR_SET_TSC: > - case PR_GET_UNALIGN: > - case PR_SET_UNALIGN: > default: > /* Disable to prevent the target disabling stuff we need. */ > return -TARGET_EINVAL; > -- > 2.25.1 > > <div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 14, 2021 at 10:14 PM Richard Henderson <<a href="mailto:richard.henderson@linaro.org">richard.henderson@linaro.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This requires extra work for each target, but adds the<br> common syscall code, and the necessary flag in CPUState.<br> <br> Signed-off-by: Richard Henderson <<a href="mailto:richard.henderson@linaro.org" target="_blank">richard.henderson@linaro.org</a>><br> ---<br> include/hw/core/cpu.h | 3 +++<br> linux-user/generic/target_prctl_unalign.h | 27 +++++++++++++++++++++++<br> cpu.c | 20 ++++++++++++-----<br> linux-user/syscall.c | 13 +++++++++--<br> 4 files changed, 56 insertions(+), 7 deletions(-)<br> create mode 100644 linux-user/generic/target_prctl_unalign.h<br></blockquote><div><br></div><div>Reviewed-by: Warner Losh <<a href="mailto:imp@bsdimp.com">imp@bsdimp.com</a>></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h<br> index 1a10497af3..6202bbf9c3 100644<br> --- a/include/hw/core/cpu.h<br> +++ b/include/hw/core/cpu.h<br> @@ -412,6 +412,9 @@ struct CPUState {<br> <br> bool ignore_memory_transaction_failures;<br> <br> + /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */<br> + bool prctl_unalign_sigbus;<br> +<br> struct hax_vcpu_state *hax_vcpu;<br> <br> struct hvf_vcpu_state *hvf;<br> diff --git a/linux-user/generic/target_prctl_unalign.h b/linux-user/generic/target_prctl_unalign.h<br> new file mode 100644<br> index 0000000000..bc3b83af2a<br> --- /dev/null<br> +++ b/linux-user/generic/target_prctl_unalign.h<br> @@ -0,0 +1,27 @@<br> +/*<br> + * Generic prctl unalign functions for linux-user<br> + *<br> + * SPDX-License-Identifier: GPL-2.0-or-later<br> + */<br> +#ifndef GENERIC_TARGET_PRCTL_UNALIGN_H<br> +#define GENERIC_TARGET_PRCTL_UNALIGN_H<br> +<br> +static abi_long do_prctl_get_unalign(CPUArchState *env, target_long arg2)<br> +{<br> + CPUState *cs = env_cpu(env);<br> + uint32_t res = PR_UNALIGN_NOPRINT;<br> + if (cs->prctl_unalign_sigbus) {<br> + res |= PR_UNALIGN_SIGBUS;<br> + }<br> + return put_user_u32(res, arg2);<br> +}<br> +#define do_prctl_get_unalign do_prctl_get_unalign<br> +<br> +static abi_long do_prctl_set_unalign(CPUArchState *env, target_long arg2)<br> +{<br> + env_cpu(env)->prctl_unalign_sigbus = arg2 & PR_UNALIGN_SIGBUS;<br> + return 0;<br> +}<br> +#define do_prctl_set_unalign do_prctl_set_unalign<br> +<br> +#endif /* GENERIC_TARGET_PRCTL_UNALIGN_H */<br> diff --git a/cpu.c b/cpu.c<br> index 9bce67ef55..9e388d9cd3 100644<br> --- a/cpu.c<br> +++ b/cpu.c<br> @@ -179,13 +179,23 @@ void cpu_exec_unrealizefn(CPUState *cpu)<br> cpu_list_remove(cpu);<br> }<br> <br> +/*<br> + * This can't go in hw/core/cpu.c because that file is compiled only<br> + * once for both user-mode and system builds.<br> + */<br> static Property cpu_common_props[] = {<br> -#ifndef CONFIG_USER_ONLY<br> +#ifdef CONFIG_USER_ONLY<br> /*<br> - * Create a memory property for softmmu CPU object,<br> - * so users can wire up its memory. (This can't go in hw/core/cpu.c<br> - * because that file is compiled only once for both user-mode<br> - * and system builds.) The default if no link is set up is to use<br> + * Create a property for the user-only object, so users can<br> + * adjust prctl(PR_SET_UNALIGN) from the command-line.<br> + * Has no effect if the target does not support the feature.<br> + */<br> + DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState,<br> + prctl_unalign_sigbus, false),<br> +#else<br> + /*<br> + * Create a memory property for softmmu CPU object, so users can<br> + * wire up its memory. The default if no link is set up is to use<br> * the system address space.<br> */<br> DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,<br> diff --git a/linux-user/syscall.c b/linux-user/syscall.c<br> index 7635c2397a..ac3bc8a330 100644<br> --- a/linux-user/syscall.c<br> +++ b/linux-user/syscall.c<br> @@ -6375,6 +6375,12 @@ static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2)<br> #ifndef do_prctl_get_tagged_addr_ctrl<br> #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0<br> #endif<br> +#ifndef do_prctl_get_unalign<br> +#define do_prctl_get_unalign do_prctl_inval1<br> +#endif<br> +#ifndef do_prctl_set_unalign<br> +#define do_prctl_set_unalign do_prctl_inval1<br> +#endif<br> <br> static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,<br> abi_long arg3, abi_long arg4, abi_long arg5)<br> @@ -6438,6 +6444,11 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,<br> }<br> return do_prctl_get_tagged_addr_ctrl(env);<br> <br> + case PR_GET_UNALIGN:<br> + return do_prctl_get_unalign(env, arg2);<br> + case PR_SET_UNALIGN:<br> + return do_prctl_set_unalign(env, arg2);<br> +<br> case PR_GET_DUMPABLE:<br> case PR_SET_DUMPABLE:<br> case PR_GET_KEEPCAPS:<br> @@ -6480,8 +6491,6 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2,<br> case PR_SET_THP_DISABLE:<br> case PR_GET_TSC:<br> case PR_SET_TSC:<br> - case PR_GET_UNALIGN:<br> - case PR_SET_UNALIGN:<br> default:<br> /* Disable to prevent the target disabling stuff we need. */<br> return -TARGET_EINVAL;<br> -- <br> 2.25.1<br> <br> </blockquote></div></div>
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 1a10497af3..6202bbf9c3 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -412,6 +412,9 @@ struct CPUState { bool ignore_memory_transaction_failures; + /* Used for user-only emulation of prctl(PR_SET_UNALIGN). */ + bool prctl_unalign_sigbus; + struct hax_vcpu_state *hax_vcpu; struct hvf_vcpu_state *hvf; diff --git a/linux-user/generic/target_prctl_unalign.h b/linux-user/generic/target_prctl_unalign.h new file mode 100644 index 0000000000..bc3b83af2a --- /dev/null +++ b/linux-user/generic/target_prctl_unalign.h @@ -0,0 +1,27 @@ +/* + * Generic prctl unalign functions for linux-user + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef GENERIC_TARGET_PRCTL_UNALIGN_H +#define GENERIC_TARGET_PRCTL_UNALIGN_H + +static abi_long do_prctl_get_unalign(CPUArchState *env, target_long arg2) +{ + CPUState *cs = env_cpu(env); + uint32_t res = PR_UNALIGN_NOPRINT; + if (cs->prctl_unalign_sigbus) { + res |= PR_UNALIGN_SIGBUS; + } + return put_user_u32(res, arg2); +} +#define do_prctl_get_unalign do_prctl_get_unalign + +static abi_long do_prctl_set_unalign(CPUArchState *env, target_long arg2) +{ + env_cpu(env)->prctl_unalign_sigbus = arg2 & PR_UNALIGN_SIGBUS; + return 0; +} +#define do_prctl_set_unalign do_prctl_set_unalign + +#endif /* GENERIC_TARGET_PRCTL_UNALIGN_H */ diff --git a/cpu.c b/cpu.c index 9bce67ef55..9e388d9cd3 100644 --- a/cpu.c +++ b/cpu.c @@ -179,13 +179,23 @@ void cpu_exec_unrealizefn(CPUState *cpu) cpu_list_remove(cpu); } +/* + * This can't go in hw/core/cpu.c because that file is compiled only + * once for both user-mode and system builds. + */ static Property cpu_common_props[] = { -#ifndef CONFIG_USER_ONLY +#ifdef CONFIG_USER_ONLY /* - * Create a memory property for softmmu CPU object, - * so users can wire up its memory. (This can't go in hw/core/cpu.c - * because that file is compiled only once for both user-mode - * and system builds.) The default if no link is set up is to use + * Create a property for the user-only object, so users can + * adjust prctl(PR_SET_UNALIGN) from the command-line. + * Has no effect if the target does not support the feature. + */ + DEFINE_PROP_BOOL("prctl-unalign-sigbus", CPUState, + prctl_unalign_sigbus, false), +#else + /* + * Create a memory property for softmmu CPU object, so users can + * wire up its memory. The default if no link is set up is to use * the system address space. */ DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION, diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 7635c2397a..ac3bc8a330 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -6375,6 +6375,12 @@ static abi_long do_prctl_inval1(CPUArchState *env, abi_long arg2) #ifndef do_prctl_get_tagged_addr_ctrl #define do_prctl_get_tagged_addr_ctrl do_prctl_inval0 #endif +#ifndef do_prctl_get_unalign +#define do_prctl_get_unalign do_prctl_inval1 +#endif +#ifndef do_prctl_set_unalign +#define do_prctl_set_unalign do_prctl_inval1 +#endif static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, abi_long arg3, abi_long arg4, abi_long arg5) @@ -6438,6 +6444,11 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, } return do_prctl_get_tagged_addr_ctrl(env); + case PR_GET_UNALIGN: + return do_prctl_get_unalign(env, arg2); + case PR_SET_UNALIGN: + return do_prctl_set_unalign(env, arg2); + case PR_GET_DUMPABLE: case PR_SET_DUMPABLE: case PR_GET_KEEPCAPS: @@ -6480,8 +6491,6 @@ static abi_long do_prctl(CPUArchState *env, abi_long option, abi_long arg2, case PR_SET_THP_DISABLE: case PR_GET_TSC: case PR_SET_TSC: - case PR_GET_UNALIGN: - case PR_SET_UNALIGN: default: /* Disable to prevent the target disabling stuff we need. */ return -TARGET_EINVAL;
This requires extra work for each target, but adds the common syscall code, and the necessary flag in CPUState. Signed-off-by: Richard Henderson <richard.henderson@linaro.org> --- include/hw/core/cpu.h | 3 +++ linux-user/generic/target_prctl_unalign.h | 27 +++++++++++++++++++++++ cpu.c | 20 ++++++++++++----- linux-user/syscall.c | 13 +++++++++-- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 linux-user/generic/target_prctl_unalign.h -- 2.25.1