From patchwork Mon Jul 1 17:35:12 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 18210 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qa0-f72.google.com (mail-qa0-f72.google.com [209.85.216.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A89B925E04 for ; Mon, 1 Jul 2013 17:35:29 +0000 (UTC) Received: by mail-qa0-f72.google.com with SMTP id f14sf5400457qak.11 for ; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-beenthere:x-forwarded-to:x-forwarded-for :delivered-to:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references:x-gm-message-state:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-google-group-id:list-post:list-help:list-archive:list-unsubscribe; bh=/irBarP6ArEhmflbNkNnCkq2o5m68ViR1ZZYuT+Soy8=; b=YjF2SFE1CcIwFZ7uFy+rpwe5tv9f5/ESOp8Xs+bbwgx9aA8YiE7wElCZ+tHfQl6P6t G/GTo2E5YwZucrtnY9uK3xUMO5sIJT0vghAMCBA3NVOfWTLkykHQZ6hi03pH4ZZsmxeI vPIIUu1J3gnJ1JUdotNtbT05FymGvaa8ymWsL81BA6jj6q1066eQn5lA2Kfn5yvDAULx FrH/2x7w+6bs2F4fD53h2DP/BiJXdULWhWgxAZn3gT9dZi0hHyfJ+JxhVytIx8x7cxkB RnVG0u53cTMuOmUVd6GIB+FIZXtd/P9ew8WoxgOjJwkR3FPddEsHTn/d/XePg6otNvJv e9Kg== X-Received: by 10.236.32.229 with SMTP id o65mr15509507yha.19.1372700129486; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.49.109.36 with SMTP id hp4ls2244718qeb.33.gmail; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) X-Received: by 10.58.96.76 with SMTP id dq12mr9859265veb.91.1372700129342; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) Received: from mail-ve0-f171.google.com (mail-ve0-f171.google.com [209.85.128.171]) by mx.google.com with ESMTPS id zw14si4989148veb.64.2013.07.01.10.35.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 01 Jul 2013 10:35:29 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.171; Received: by mail-ve0-f171.google.com with SMTP id b10so4020251vea.30 for ; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) X-Received: by 10.220.83.69 with SMTP id e5mr7365548vcl.53.1372700129210; Mon, 01 Jul 2013 10:35:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.58.165.8 with SMTP id yu8csp289802veb; Mon, 1 Jul 2013 10:35:28 -0700 (PDT) X-Received: by 10.180.37.48 with SMTP id v16mr12763911wij.35.1372700124433; Mon, 01 Jul 2013 10:35:24 -0700 (PDT) Received: from mnementh.archaic.org.uk (1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.d.1.0.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:1d0::1]) by mx.google.com with ESMTPS id gc5si6815970wjb.144.2013.07.01.10.35.23 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Mon, 01 Jul 2013 10:35:24 -0700 (PDT) Received-SPF: neutral (google.com: 2001:8b0:1d0::1 is neither permitted nor denied by best guess record for domain of pm215@archaic.org.uk) client-ip=2001:8b0:1d0::1; Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1Uti0Z-0002LQ-9b; Mon, 01 Jul 2013 18:35:23 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Alexander Graf , Andreas Schwab , "Mian M. Hamayun" Subject: [PATCH v5 13/21] linux-user: Add signal handling for AArch64 Date: Mon, 1 Jul 2013 18:35:12 +0100 Message-Id: <1372700120-8896-14-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1372700120-8896-1-git-send-email-peter.maydell@linaro.org> References: <1372700120-8896-1-git-send-email-peter.maydell@linaro.org> X-Gm-Message-State: ALoCoQmFRYFWW1WOvbgPbflifJhYgmm4nlmFO3z8LsRWg2zS60zorCVFAx/dNIxTXtXns7LaZRcT X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Andreas Schwab This patch adds signal handling for AArch64. The code is based on the respective source in the Linux kernel. Signed-off-by: Andreas Schwab Signed-off-by: Alexander Graf Signed-off-by: John Rigby Message-id: 1368505980-17151-10-git-send-email-john.rigby@linaro.org [PMM: fixed style nits: tabs, long lines; pulled target_signal.h in from a later patch; it fits better here] Signed-off-by: Peter Maydell --- linux-user/aarch64/target_signal.h | 29 ++++ linux-user/signal.c | 254 ++++++++++++++++++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 linux-user/aarch64/target_signal.h diff --git a/linux-user/aarch64/target_signal.h b/linux-user/aarch64/target_signal.h new file mode 100644 index 0000000..d69a2d2 --- /dev/null +++ b/linux-user/aarch64/target_signal.h @@ -0,0 +1,29 @@ +#ifndef TARGET_SIGNAL_H +#define TARGET_SIGNAL_H + +#include "cpu.h" + +/* this struct defines a stack used during syscall handling */ + +typedef struct target_sigaltstack { + abi_ulong ss_sp; + abi_long ss_flags; + abi_ulong ss_size; +} target_stack_t; + + +/* + * sigaltstack controls + */ +#define TARGET_SS_ONSTACK 1 +#define TARGET_SS_DISABLE 2 + +#define TARGET_MINSIGSTKSZ 2048 +#define TARGET_SIGSTKSZ 8192 + +static inline abi_ulong get_sp_from_cpustate(CPUARMState *state) +{ + return state->xregs[31]; +} + +#endif /* TARGET_SIGNAL_H */ diff --git a/linux-user/signal.c b/linux-user/signal.c index c4e20dc..076a9a2 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -1090,6 +1090,260 @@ badframe: return 0; } +#elif defined(TARGET_AARCH64) + +struct target_sigcontext { + uint64_t fault_address; + /* AArch64 registers */ + uint64_t regs[31]; + uint64_t sp; + uint64_t pc; + uint64_t pstate; + /* 4K reserved for FP/SIMD state and future expansion */ + char __reserved[4096] __attribute__((__aligned__(16))); +}; + +struct target_ucontext { + abi_ulong tuc_flags; + abi_ulong tuc_link; + target_stack_t tuc_stack; + target_sigset_t tuc_sigmask; + /* glibc uses a 1024-bit sigset_t */ + char __unused[1024 / 8 - sizeof(target_sigset_t)]; + /* last for future expansion */ + struct target_sigcontext tuc_mcontext; +}; + +/* + * Header to be used at the beginning of structures extending the user + * context. Such structures must be placed after the rt_sigframe on the stack + * and be 16-byte aligned. The last structure must be a dummy one with the + * magic and size set to 0. + */ +struct target_aarch64_ctx { + uint32_t magic; + uint32_t size; +}; + +#define TARGET_FPSIMD_MAGIC 0x46508001 + +struct target_fpsimd_context { + struct target_aarch64_ctx head; + uint32_t fpsr; + uint32_t fpcr; + uint64_t vregs[32 * 2]; +}; + +/* + * Auxiliary context saved in the sigcontext.__reserved array. Not exported to + * user space as it will change with the addition of new context. User space + * should check the magic/size information. + */ +struct target_aux_context { + struct target_fpsimd_context fpsimd; + /* additional context to be added before "end" */ + struct target_aarch64_ctx end; +}; + +struct target_rt_sigframe { + struct target_siginfo info; + struct target_ucontext uc; + uint64_t fp; + uint64_t lr; + uint32_t tramp[2]; +}; + +static int target_setup_sigframe(struct target_rt_sigframe *sf, + CPUARMState *env, target_sigset_t *set) +{ + int i; + struct target_aux_context *aux = + (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved; + + /* set up the stack frame for unwinding */ + __put_user(env->xregs[29], &sf->fp); + __put_user(env->xregs[30], &sf->lr); + + for (i = 0; i < 31; i++) { + __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); + } + __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp); + __put_user(env->pc, &sf->uc.tuc_mcontext.pc); + __put_user(env->pstate, &sf->uc.tuc_mcontext.pstate); + + __put_user(/*current->thread.fault_address*/ 0, + &sf->uc.tuc_mcontext.fault_address); + + for (i = 0; i < TARGET_NSIG_WORDS; i++) { + __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]); + } + + for (i = 0; i < 32 * 2; i++) { + __put_user(env->vfp.regs[i], &aux->fpsimd.vregs[i]); + } + __put_user(/*env->fpsr*/0, &aux->fpsimd.fpsr); + __put_user(/*env->fpcr*/0, &aux->fpsimd.fpcr); + __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic); + __put_user(sizeof(struct target_fpsimd_context), + &aux->fpsimd.head.size); + + /* set the "end" magic */ + __put_user(0, &aux->end.magic); + __put_user(0, &aux->end.size); + + return 0; +} + +static int target_restore_sigframe(CPUARMState *env, + struct target_rt_sigframe *sf) +{ + sigset_t set; + int i; + struct target_aux_context *aux = + (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved; + uint32_t magic, size; + + target_to_host_sigset(&set, &sf->uc.tuc_sigmask); + sigprocmask(SIG_SETMASK, &set, NULL); + + for (i = 0; i < 31; i++) { + __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); + } + + __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp); + __get_user(env->pc, &sf->uc.tuc_mcontext.pc); + __get_user(env->pstate, &sf->uc.tuc_mcontext.pstate); + + __get_user(magic, &aux->fpsimd.head.magic); + __get_user(size, &aux->fpsimd.head.size); + + if (magic != TARGET_FPSIMD_MAGIC + || size != sizeof(struct target_fpsimd_context)) { + return 1; + } + + for (i = 0; i < 32 * 2; i++) { + __get_user(env->vfp.regs[i], &aux->fpsimd.vregs[i]); + } + + return 0; +} + +static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env) +{ + abi_ulong sp; + + sp = env->xregs[31]; + + /* + * This is the X/Open sanctioned signal stack switching. + */ + if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) { + sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; + } + + sp = (sp - sizeof(struct target_rt_sigframe)) & ~15; + + return sp; +} + +static void target_setup_frame(int usig, struct target_sigaction *ka, + target_siginfo_t *info, target_sigset_t *set, + CPUARMState *env) +{ + struct target_rt_sigframe *frame; + abi_ulong frame_addr; + + frame_addr = get_sigframe(ka, env); + if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { + goto give_sigsegv; + } + + __put_user(0, &frame->uc.tuc_flags); + __put_user(0, &frame->uc.tuc_link); + + __put_user(target_sigaltstack_used.ss_sp, + &frame->uc.tuc_stack.ss_sp); + __put_user(sas_ss_flags(env->xregs[31]), + &frame->uc.tuc_stack.ss_flags); + __put_user(target_sigaltstack_used.ss_size, + &frame->uc.tuc_stack.ss_size); + target_setup_sigframe(frame, env, set); + /* mov x8,#__NR_rt_sigreturn; svc #0 */ + __put_user(0xd2801168, &frame->tramp[0]); + __put_user(0xd4000001, &frame->tramp[1]); + env->xregs[0] = usig; + env->xregs[31] = frame_addr; + env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp); + env->pc = ka->_sa_handler; + env->xregs[30] = env->xregs[31] + + offsetof(struct target_rt_sigframe, tramp); + if (info) { + if (copy_siginfo_to_user(&frame->info, info)) { + goto give_sigsegv; + } + env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info); + env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc); + } + + unlock_user_struct(frame, frame_addr, 1); + return; + + give_sigsegv: + unlock_user_struct(frame, frame_addr, 1); + force_sig(TARGET_SIGSEGV); +} + +static void setup_rt_frame(int sig, struct target_sigaction *ka, + target_siginfo_t *info, target_sigset_t *set, + CPUARMState *env) +{ + target_setup_frame(sig, ka, info, set, env); +} + +static void setup_frame(int sig, struct target_sigaction *ka, + target_sigset_t *set, CPUARMState *env) +{ + target_setup_frame(sig, ka, 0, set, env); +} + +long do_rt_sigreturn(CPUARMState *env) +{ + struct target_rt_sigframe *frame; + abi_ulong frame_addr = env->xregs[31]; + + if (frame_addr & 15) { + goto badframe; + } + + if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { + goto badframe; + } + + if (target_restore_sigframe(env, frame)) { + goto badframe; + } + + if (do_sigaltstack(frame_addr + + offsetof(struct target_rt_sigframe, uc.tuc_stack), + 0, get_sp_from_cpustate(env)) == -EFAULT) { + goto badframe; + } + + unlock_user_struct(frame, frame_addr, 0); + return env->xregs[0]; + + badframe: + unlock_user_struct(frame, frame_addr, 0); + force_sig(TARGET_SIGSEGV); + return 0; +} + +long do_sigreturn(CPUARMState *env) +{ + return do_rt_sigreturn(env); +} + #elif defined(TARGET_ARM) struct target_sigcontext {