Laurent Vivier | befb744 | 2018-04-24 21:26:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Emulation of Linux signals |
| 3 | * |
| 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
Laurent Vivier | ea14059 | 2018-04-24 21:26:27 +0200 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
| 20 | #include "qemu.h" |
Laurent Vivier | ea14059 | 2018-04-24 21:26:27 +0200 | [diff] [blame] | 21 | #include "signal-common.h" |
| 22 | #include "linux-user/trace.h" |
| 23 | |
| 24 | struct target_sigcontext { |
| 25 | union { |
| 26 | /* General-purpose registers. */ |
| 27 | abi_ulong gregs[56]; |
| 28 | struct { |
| 29 | abi_ulong __gregs[53]; |
| 30 | abi_ulong tp; /* Aliases gregs[TREG_TP]. */ |
| 31 | abi_ulong sp; /* Aliases gregs[TREG_SP]. */ |
| 32 | abi_ulong lr; /* Aliases gregs[TREG_LR]. */ |
| 33 | }; |
| 34 | }; |
| 35 | abi_ulong pc; /* Program counter. */ |
| 36 | abi_ulong ics; /* In Interrupt Critical Section? */ |
| 37 | abi_ulong faultnum; /* Fault number. */ |
| 38 | abi_ulong pad[5]; |
| 39 | }; |
| 40 | |
| 41 | struct target_ucontext { |
| 42 | abi_ulong tuc_flags; |
| 43 | abi_ulong tuc_link; |
| 44 | target_stack_t tuc_stack; |
| 45 | struct target_sigcontext tuc_mcontext; |
| 46 | target_sigset_t tuc_sigmask; /* mask last for extensibility */ |
| 47 | }; |
| 48 | |
| 49 | struct target_rt_sigframe { |
| 50 | unsigned char save_area[16]; /* caller save area */ |
| 51 | struct target_siginfo info; |
| 52 | struct target_ucontext uc; |
| 53 | abi_ulong retcode[2]; |
| 54 | }; |
| 55 | |
| 56 | #define INSN_MOVELI_R10_139 0x00045fe551483000ULL /* { moveli r10, 139 } */ |
| 57 | #define INSN_SWINT1 0x286b180051485000ULL /* { swint1 } */ |
| 58 | |
| 59 | |
| 60 | static void setup_sigcontext(struct target_sigcontext *sc, |
| 61 | CPUArchState *env, int signo) |
| 62 | { |
| 63 | int i; |
| 64 | |
| 65 | for (i = 0; i < TILEGX_R_COUNT; ++i) { |
| 66 | __put_user(env->regs[i], &sc->gregs[i]); |
| 67 | } |
| 68 | |
| 69 | __put_user(env->pc, &sc->pc); |
| 70 | __put_user(0, &sc->ics); |
| 71 | __put_user(signo, &sc->faultnum); |
| 72 | } |
| 73 | |
| 74 | static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < TILEGX_R_COUNT; ++i) { |
| 79 | __get_user(env->regs[i], &sc->gregs[i]); |
| 80 | } |
| 81 | |
| 82 | __get_user(env->pc, &sc->pc); |
| 83 | } |
| 84 | |
| 85 | static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env, |
| 86 | size_t frame_size) |
| 87 | { |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 88 | unsigned long sp = get_sp_from_cpustate(env); |
Laurent Vivier | ea14059 | 2018-04-24 21:26:27 +0200 | [diff] [blame] | 89 | |
| 90 | if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) { |
| 91 | return -1UL; |
| 92 | } |
| 93 | |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 94 | sp = target_sigsp(sp, ka) - frame_size; |
Laurent Vivier | ea14059 | 2018-04-24 21:26:27 +0200 | [diff] [blame] | 95 | sp &= -16UL; |
| 96 | return sp; |
| 97 | } |
| 98 | |
| 99 | void setup_rt_frame(int sig, struct target_sigaction *ka, |
| 100 | target_siginfo_t *info, |
| 101 | target_sigset_t *set, CPUArchState *env) |
| 102 | { |
| 103 | abi_ulong frame_addr; |
| 104 | struct target_rt_sigframe *frame; |
| 105 | unsigned long restorer; |
| 106 | |
| 107 | frame_addr = get_sigframe(ka, env, sizeof(*frame)); |
| 108 | trace_user_setup_rt_frame(env, frame_addr); |
| 109 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { |
| 110 | goto give_sigsegv; |
| 111 | } |
| 112 | |
| 113 | /* Always write at least the signal number for the stack backtracer. */ |
| 114 | if (ka->sa_flags & TARGET_SA_SIGINFO) { |
| 115 | /* At sigreturn time, restore the callee-save registers too. */ |
| 116 | tswap_siginfo(&frame->info, info); |
| 117 | /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */ |
| 118 | } else { |
| 119 | __put_user(info->si_signo, &frame->info.si_signo); |
| 120 | } |
| 121 | |
| 122 | /* Create the ucontext. */ |
| 123 | __put_user(0, &frame->uc.tuc_flags); |
| 124 | __put_user(0, &frame->uc.tuc_link); |
Laurent Vivier | 465e237 | 2018-04-11 21:23:47 +0200 | [diff] [blame] | 125 | target_save_altstack(&frame->uc.tuc_stack, env); |
Laurent Vivier | ea14059 | 2018-04-24 21:26:27 +0200 | [diff] [blame] | 126 | setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo); |
| 127 | |
| 128 | if (ka->sa_flags & TARGET_SA_RESTORER) { |
| 129 | restorer = (unsigned long) ka->sa_restorer; |
| 130 | } else { |
| 131 | __put_user(INSN_MOVELI_R10_139, &frame->retcode[0]); |
| 132 | __put_user(INSN_SWINT1, &frame->retcode[1]); |
| 133 | restorer = frame_addr + offsetof(struct target_rt_sigframe, retcode); |
| 134 | } |
| 135 | env->pc = (unsigned long) ka->_sa_handler; |
| 136 | env->regs[TILEGX_R_SP] = (unsigned long) frame; |
| 137 | env->regs[TILEGX_R_LR] = restorer; |
| 138 | env->regs[0] = (unsigned long) sig; |
| 139 | env->regs[1] = (unsigned long) &frame->info; |
| 140 | env->regs[2] = (unsigned long) &frame->uc; |
| 141 | /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */ |
| 142 | |
| 143 | unlock_user_struct(frame, frame_addr, 1); |
| 144 | return; |
| 145 | |
| 146 | give_sigsegv: |
| 147 | force_sigsegv(sig); |
| 148 | } |
| 149 | |
| 150 | long do_rt_sigreturn(CPUTLGState *env) |
| 151 | { |
| 152 | abi_ulong frame_addr = env->regs[TILEGX_R_SP]; |
| 153 | struct target_rt_sigframe *frame; |
| 154 | sigset_t set; |
| 155 | |
| 156 | trace_user_do_rt_sigreturn(env, frame_addr); |
| 157 | if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { |
| 158 | goto badframe; |
| 159 | } |
| 160 | target_to_host_sigset(&set, &frame->uc.tuc_sigmask); |
| 161 | set_sigmask(&set); |
| 162 | |
| 163 | restore_sigcontext(env, &frame->uc.tuc_mcontext); |
| 164 | if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe, |
| 165 | uc.tuc_stack), |
| 166 | 0, env->regs[TILEGX_R_SP]) == -EFAULT) { |
| 167 | goto badframe; |
| 168 | } |
| 169 | |
| 170 | unlock_user_struct(frame, frame_addr, 0); |
| 171 | return -TARGET_QEMU_ESIGRETURN; |
| 172 | |
| 173 | |
| 174 | badframe: |
| 175 | unlock_user_struct(frame, frame_addr, 0); |
| 176 | force_sig(TARGET_SIGSEGV); |
| 177 | return -TARGET_QEMU_ESIGRETURN; |
| 178 | } |