linux-user: Split out target_restore_altstack

Create a function to match target_save_altstack.
Fix some style and unlock issues in do_sigaltstack.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20210426025334.1168495-2-richard.henderson@linaro.org>
Signed-off-by: Laurent Vivier <laurent@vivier.eu>
This commit is contained in:
Richard Henderson 2021-04-25 19:53:10 -07:00 committed by Laurent Vivier
parent 9f771ad839
commit 92bad94836
2 changed files with 66 additions and 50 deletions

View File

@ -24,6 +24,7 @@ int on_sig_stack(unsigned long sp);
int sas_ss_flags(unsigned long sp); int sas_ss_flags(unsigned long sp);
abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka); abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka);
void target_save_altstack(target_stack_t *uss, CPUArchState *env); void target_save_altstack(target_stack_t *uss, CPUArchState *env);
abi_long target_restore_altstack(target_stack_t *uss, abi_ulong sp);
static inline void target_sigemptyset(target_sigset_t *set) static inline void target_sigemptyset(target_sigset_t *set)
{ {

View File

@ -297,6 +297,50 @@ void target_save_altstack(target_stack_t *uss, CPUArchState *env)
__put_user(ts->sigaltstack_used.ss_size, &uss->ss_size); __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
} }
abi_long target_restore_altstack(target_stack_t *uss, abi_ulong sp)
{
TaskState *ts = (TaskState *)thread_cpu->opaque;
size_t minstacksize = TARGET_MINSIGSTKSZ;
target_stack_t ss;
#if defined(TARGET_PPC64)
/* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
struct image_info *image = ts->info;
if (get_ppc64_abi(image) > 1) {
minstacksize = 4096;
}
#endif
__get_user(ss.ss_sp, &uss->ss_sp);
__get_user(ss.ss_size, &uss->ss_size);
__get_user(ss.ss_flags, &uss->ss_flags);
if (on_sig_stack(sp)) {
return -TARGET_EPERM;
}
switch (ss.ss_flags) {
default:
return -TARGET_EINVAL;
case TARGET_SS_DISABLE:
ss.ss_size = 0;
ss.ss_sp = 0;
break;
case TARGET_SS_ONSTACK:
case 0:
if (ss.ss_size < minstacksize) {
return -TARGET_ENOMEM;
}
break;
}
ts->sigaltstack_used.ss_sp = ss.ss_sp;
ts->sigaltstack_used.ss_size = ss.ss_size;
return 0;
}
/* siginfo conversion */ /* siginfo conversion */
static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
@ -758,73 +802,44 @@ static void host_signal_handler(int host_signum, siginfo_t *info,
/* compare linux/kernel/signal.c:do_sigaltstack() */ /* compare linux/kernel/signal.c:do_sigaltstack() */
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp) abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
{ {
int ret; target_stack_t oss, *uoss = NULL;
struct target_sigaltstack oss; abi_long ret = -TARGET_EFAULT;
if (uoss_addr) {
TaskState *ts = (TaskState *)thread_cpu->opaque; TaskState *ts = (TaskState *)thread_cpu->opaque;
/* XXX: test errors */ /* Verify writability now, but do not alter user memory yet. */
if(uoss_addr) if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
{ goto out;
}
__put_user(ts->sigaltstack_used.ss_sp, &oss.ss_sp); __put_user(ts->sigaltstack_used.ss_sp, &oss.ss_sp);
__put_user(ts->sigaltstack_used.ss_size, &oss.ss_size); __put_user(ts->sigaltstack_used.ss_size, &oss.ss_size);
__put_user(sas_ss_flags(sp), &oss.ss_flags); __put_user(sas_ss_flags(sp), &oss.ss_flags);
} }
if(uss_addr) if (uss_addr) {
{ target_stack_t *uss;
struct target_sigaltstack *uss;
struct target_sigaltstack ss;
size_t minstacksize = TARGET_MINSIGSTKSZ;
#if defined(TARGET_PPC64)
/* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
if (get_ppc64_abi(image) > 1) {
minstacksize = 4096;
}
#endif
ret = -TARGET_EFAULT;
if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) { if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
goto out; goto out;
} }
__get_user(ss.ss_sp, &uss->ss_sp); ret = target_restore_altstack(uss, sp);
__get_user(ss.ss_size, &uss->ss_size); if (ret) {
__get_user(ss.ss_flags, &uss->ss_flags);
unlock_user_struct(uss, uss_addr, 0);
ret = -TARGET_EPERM;
if (on_sig_stack(sp))
goto out;
ret = -TARGET_EINVAL;
if (ss.ss_flags != TARGET_SS_DISABLE
&& ss.ss_flags != TARGET_SS_ONSTACK
&& ss.ss_flags != 0)
goto out;
if (ss.ss_flags == TARGET_SS_DISABLE) {
ss.ss_size = 0;
ss.ss_sp = 0;
} else {
ret = -TARGET_ENOMEM;
if (ss.ss_size < minstacksize) {
goto out; goto out;
} }
} }
ts->sigaltstack_used.ss_sp = ss.ss_sp;
ts->sigaltstack_used.ss_size = ss.ss_size;
}
if (uoss_addr) { if (uoss_addr) {
ret = -TARGET_EFAULT; memcpy(uoss, &oss, sizeof(oss));
if (copy_to_user(uoss_addr, &oss, sizeof(oss))) unlock_user_struct(uoss, uoss_addr, 1);
goto out; uoss = NULL;
} }
ret = 0; ret = 0;
out:
out:
if (uoss) {
unlock_user_struct(uoss, uoss_addr, 0);
}
return ret; return ret;
} }