From patchwork Tue Apr 30 06:38:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Rigby X-Patchwork-Id: 240564 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B90362C00A4 for ; Tue, 30 Apr 2013 16:41:25 +1000 (EST) Received: from localhost ([::1]:42801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UX4Ff-0008GF-Ep for incoming@patchwork.ozlabs.org; Tue, 30 Apr 2013 02:41:23 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54737) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UX4Ds-0005hJ-KI for qemu-devel@nongnu.org; Tue, 30 Apr 2013 02:39:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UX4Do-0004Rz-QD for qemu-devel@nongnu.org; Tue, 30 Apr 2013 02:39:32 -0400 Received: from mail-pa0-f44.google.com ([209.85.220.44]:61346) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UX4Do-0004Ro-Gy for qemu-devel@nongnu.org; Tue, 30 Apr 2013 02:39:28 -0400 Received: by mail-pa0-f44.google.com with SMTP id rl6so178752pac.31 for ; Mon, 29 Apr 2013 23:39:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state; bh=rywg7a+XSrxvnoVga7NrNNn6t9gq/4153tCgJwZP4Zg=; b=XXNv1ifM3SoLAVgOqRcjWgQWHNTH5468JVhmuT78Al9raXDQSK5uiKSxzEMbQz/Ixf BdE7cKtVRoFQmBZJ1cgY2ie9CLYhWACfwYtV7ayhqxp50xwmi5ioIdJknOnmAkHcS/4v wGjAavP9PZPxvl4Y50jBjdpKTaHQ/rfvzTrcfvnAeDU1sHtcA9uGANjx3kSGIoon/4wW iM+C0fhqhtGoqIhRLKNe7sZQVdfjQKa8tKbpTD/Sj53xdJk6Rznff7M1Z0KWLR8XyZ3X CCe60rnFTFEhQ8GWye0U8aUurZ/Xe3kWAXpw0/3wGK46B9Yf5OO45O1sti2+wpBAP43N EaMw== X-Received: by 10.66.172.233 with SMTP id bf9mr8804535pac.139.1367303967810; Mon, 29 Apr 2013 23:39:27 -0700 (PDT) Received: from localhost.localdomain (c-76-23-54-220.hsd1.ut.comcast.net. [76.23.54.220]) by mx.google.com with ESMTPSA id ba10sm27229917pbd.21.2013.04.29.23.39.25 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 29 Apr 2013 23:39:26 -0700 (PDT) From: John Rigby To: qemu-devel Date: Tue, 30 Apr 2013 00:38:38 -0600 Message-Id: <1367303918-9808-1-git-send-email-john.rigby@linaro.org> X-Mailer: git-send-email 1.7.3.3.398.g0b0cd X-Gm-Message-State: ALoCoQmGYRMwgPakPQwH2oRr8KEVpWpKCclhOewXb723dmui6xY1dlrYCWhiUW0KjjpqIK3ro7kZ X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.220.44 Cc: Peter Maydell , Andreas Schwab , Alexander Graf , Riku Voipio Subject: [Qemu-devel] [PATCH v2 09/12] linux-user: Add signal handling for AArch64 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- linux-user/arm/target_signal.h | 4 + linux-user/signal.c | 263 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) diff --git a/linux-user/arm/target_signal.h b/linux-user/arm/target_signal.h index 2b32813..afc8698 100644 --- a/linux-user/arm/target_signal.h +++ b/linux-user/arm/target_signal.h @@ -23,7 +23,11 @@ typedef struct target_sigaltstack { static inline abi_ulong get_sp_from_cpustate(CPUARMState *state) { +#ifdef TARGET_AARCH64 + return state->sp; +#else return state->regs[13]; +#endif } #endif /* TARGET_SIGNAL_H */ diff --git a/linux-user/signal.c b/linux-user/signal.c index 1055507..cf9ee61 100644 --- a/linux-user/signal.c +++ b/linux-user/signal.c @@ -1090,6 +1090,269 @@ 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, err = 0; + struct target_aux_context *aux = + (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved; + + /* set up the stack frame for unwinding */ + err |= __put_user(env->xregs[29], &sf->fp); + err |= __put_user(env->xregs[30], &sf->lr); + + for (i = 0; i < 31; i++) { + err |= __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); + } + err |= __put_user(env->sp, &sf->uc.tuc_mcontext.sp); + err |= __put_user(env->pc, &sf->uc.tuc_mcontext.pc); + err |= __put_user(env->pstate, &sf->uc.tuc_mcontext.pstate); + + err |= __put_user(/*current->thread.fault_address*/ 0, + &sf->uc.tuc_mcontext.fault_address); + + for (i = 0; i < TARGET_NSIG_WORDS; i++) { + err |= __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]); + } + + for (i = 0; i < 32 * 2; i++) { + err |= __put_user(env->vfp.regs[i], &aux->fpsimd.vregs[i]); + } + err |= __put_user(/*env->fpsr*/0, &aux->fpsimd.fpsr); + err |= __put_user(/*env->fpcr*/0, &aux->fpsimd.fpcr); + err |= __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic); + err |= __put_user(sizeof(struct target_fpsimd_context), + &aux->fpsimd.head.size); + + /* set the "end" magic */ + err |= __put_user(0, &aux->end.magic); + err |= __put_user(0, &aux->end.size); + + return err; +} + +static int target_restore_sigframe(CPUARMState *env, + struct target_rt_sigframe *sf) +{ + sigset_t set; + int i, err = 0; + 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++) { + err |= __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); + } + + err |= __get_user(env->sp, &sf->uc.tuc_mcontext.sp); + err |= __get_user(env->pc, &sf->uc.tuc_mcontext.pc); + err |= __get_user(env->pstate, &sf->uc.tuc_mcontext.pstate); + + err |= __get_user(magic, &aux->fpsimd.head.magic); + err |= __get_user(size, &aux->fpsimd.head.size); + if (err) { + return err; + } + if (magic != TARGET_FPSIMD_MAGIC || size != sizeof(struct target_fpsimd_context)) { + return 1; + } + + for (i = 0; i < 32 * 2; i++) { + err |= __get_user(env->vfp.regs[i], &aux->fpsimd.vregs[i]); + } + +#if 0 + err |= __get_user(env->fpsr, &aux->fpsimd.fpsr); + err |= __get_user(env->fpcr, &aux->fpsimd.fpcr); +#endif + + return err; +} + +static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env) +{ + abi_ulong sp; + + sp = env->sp; + + /* + * 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; + int err = 0; + + frame_addr = get_sigframe(ka, env); + if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { + goto give_sigsegv; + } + + err |= __put_user(0, &frame->uc.tuc_flags); + err |= __put_user(0, &frame->uc.tuc_link); + + err |= __put_user(target_sigaltstack_used.ss_sp, + &frame->uc.tuc_stack.ss_sp); + err |= __put_user(sas_ss_flags(env->sp), + &frame->uc.tuc_stack.ss_flags); + err |= __put_user(target_sigaltstack_used.ss_size, + &frame->uc.tuc_stack.ss_size); + err |= target_setup_sigframe(frame, env, set); + /* mov x8,#__NR_rt_sigreturn; svc #0 */ + err |= __put_user(0xd2801168, &frame->tramp[0]); + err |= __put_user(0xd4000001, &frame->tramp[1]); + if (err == 0) { + env->xregs[0] = usig; + env->sp = frame_addr; + env->xregs[29] = env->sp + offsetof(struct target_rt_sigframe, fp); + env->pc = ka->_sa_handler; + env->xregs[30] = env->sp + offsetof(struct target_rt_sigframe, tramp); + if (info) { + err |= copy_siginfo_to_user(&frame->info, info); + env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info); + env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc); + } + } + + if (!err) { + 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->sp; + + 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 {