1 /* 2 * Emulation of Linux signals 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 #include "qemu/osdep.h" 21 #include "qemu.h" 22 #include "user-internals.h" 23 #include "signal-common.h" 24 #include "linux-user/trace.h" 25 26 struct target_sigcontext { 27 target_ulong r0, r1, r2, r3; 28 target_ulong r4, r5, r6, r7; 29 target_ulong r8, r9, r10, r11; 30 target_ulong r12, r13, r14, r15; 31 target_ulong r16, r17, r18, r19; 32 target_ulong r20, r21, r22, r23; 33 target_ulong r24, r25, r26, r27; 34 target_ulong r28, r29, r30, r31; 35 target_ulong sa0; 36 target_ulong lc0; 37 target_ulong sa1; 38 target_ulong lc1; 39 target_ulong m0; 40 target_ulong m1; 41 target_ulong usr; 42 target_ulong p3_0; 43 target_ulong gp; 44 target_ulong ugp; 45 target_ulong pc; 46 target_ulong cause; 47 target_ulong badva; 48 target_ulong pad1; 49 target_ulong pad2; 50 target_ulong pad3; 51 }; 52 53 struct target_ucontext { 54 unsigned long uc_flags; 55 target_ulong uc_link; /* target pointer */ 56 target_stack_t uc_stack; 57 struct target_sigcontext uc_mcontext; 58 target_sigset_t uc_sigmask; 59 }; 60 61 struct target_rt_sigframe { 62 uint32_t tramp[2]; 63 struct target_siginfo info; 64 struct target_ucontext uc; 65 }; 66 67 static abi_ulong get_sigframe(struct target_sigaction *ka, 68 CPUHexagonState *regs, size_t framesize) 69 { 70 abi_ulong sp = get_sp_from_cpustate(regs); 71 72 /* This is the X/Open sanctioned signal stack switching. */ 73 sp = target_sigsp(sp, ka) - framesize; 74 75 sp = QEMU_ALIGN_DOWN(sp, 8); 76 77 return sp; 78 } 79 80 static void setup_sigcontext(struct target_sigcontext *sc, CPUHexagonState *env) 81 { 82 __put_user(env->gpr[HEX_REG_R00], &sc->r0); 83 __put_user(env->gpr[HEX_REG_R01], &sc->r1); 84 __put_user(env->gpr[HEX_REG_R02], &sc->r2); 85 __put_user(env->gpr[HEX_REG_R03], &sc->r3); 86 __put_user(env->gpr[HEX_REG_R04], &sc->r4); 87 __put_user(env->gpr[HEX_REG_R05], &sc->r5); 88 __put_user(env->gpr[HEX_REG_R06], &sc->r6); 89 __put_user(env->gpr[HEX_REG_R07], &sc->r7); 90 __put_user(env->gpr[HEX_REG_R08], &sc->r8); 91 __put_user(env->gpr[HEX_REG_R09], &sc->r9); 92 __put_user(env->gpr[HEX_REG_R10], &sc->r10); 93 __put_user(env->gpr[HEX_REG_R11], &sc->r11); 94 __put_user(env->gpr[HEX_REG_R12], &sc->r12); 95 __put_user(env->gpr[HEX_REG_R13], &sc->r13); 96 __put_user(env->gpr[HEX_REG_R14], &sc->r14); 97 __put_user(env->gpr[HEX_REG_R15], &sc->r15); 98 __put_user(env->gpr[HEX_REG_R16], &sc->r16); 99 __put_user(env->gpr[HEX_REG_R17], &sc->r17); 100 __put_user(env->gpr[HEX_REG_R18], &sc->r18); 101 __put_user(env->gpr[HEX_REG_R19], &sc->r19); 102 __put_user(env->gpr[HEX_REG_R20], &sc->r20); 103 __put_user(env->gpr[HEX_REG_R21], &sc->r21); 104 __put_user(env->gpr[HEX_REG_R22], &sc->r22); 105 __put_user(env->gpr[HEX_REG_R23], &sc->r23); 106 __put_user(env->gpr[HEX_REG_R24], &sc->r24); 107 __put_user(env->gpr[HEX_REG_R25], &sc->r25); 108 __put_user(env->gpr[HEX_REG_R26], &sc->r26); 109 __put_user(env->gpr[HEX_REG_R27], &sc->r27); 110 __put_user(env->gpr[HEX_REG_R28], &sc->r28); 111 __put_user(env->gpr[HEX_REG_R29], &sc->r29); 112 __put_user(env->gpr[HEX_REG_R30], &sc->r30); 113 __put_user(env->gpr[HEX_REG_R31], &sc->r31); 114 __put_user(env->gpr[HEX_REG_SA0], &sc->sa0); 115 __put_user(env->gpr[HEX_REG_LC0], &sc->lc0); 116 __put_user(env->gpr[HEX_REG_SA1], &sc->sa1); 117 __put_user(env->gpr[HEX_REG_LC1], &sc->lc1); 118 __put_user(env->gpr[HEX_REG_M0], &sc->m0); 119 __put_user(env->gpr[HEX_REG_M1], &sc->m1); 120 __put_user(env->gpr[HEX_REG_USR], &sc->usr); 121 __put_user(env->gpr[HEX_REG_P3_0], &sc->p3_0); 122 __put_user(env->gpr[HEX_REG_GP], &sc->gp); 123 __put_user(env->gpr[HEX_REG_UGP], &sc->ugp); 124 __put_user(env->gpr[HEX_REG_PC], &sc->pc); 125 } 126 127 static void setup_ucontext(struct target_ucontext *uc, 128 CPUHexagonState *env, target_sigset_t *set) 129 { 130 __put_user(0, &(uc->uc_flags)); 131 __put_user(0, &(uc->uc_link)); 132 133 target_save_altstack(&uc->uc_stack, env); 134 135 int i; 136 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 137 __put_user(set->sig[i], &(uc->uc_sigmask.sig[i])); 138 } 139 140 setup_sigcontext(&uc->uc_mcontext, env); 141 } 142 143 static inline void install_sigtramp(uint32_t *tramp) 144 { 145 __put_user(0x7800d166, tramp + 0); /* { r6=#__NR_rt_sigreturn } */ 146 __put_user(0x5400c004, tramp + 1); /* { trap0(#1) } */ 147 } 148 149 void setup_rt_frame(int sig, struct target_sigaction *ka, 150 target_siginfo_t *info, 151 target_sigset_t *set, CPUHexagonState *env) 152 { 153 abi_ulong frame_addr; 154 struct target_rt_sigframe *frame; 155 156 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 157 trace_user_setup_rt_frame(env, frame_addr); 158 159 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 160 goto badframe; 161 } 162 163 setup_ucontext(&frame->uc, env, set); 164 tswap_siginfo(&frame->info, info); 165 /* 166 * The on-stack signal trampoline is no longer executed; 167 * however, the libgcc signal frame unwinding code checks 168 * for the presence of these two numeric magic values. 169 */ 170 install_sigtramp(frame->tramp); 171 172 env->gpr[HEX_REG_PC] = ka->_sa_handler; 173 env->gpr[HEX_REG_SP] = frame_addr; 174 env->gpr[HEX_REG_R00] = sig; 175 env->gpr[HEX_REG_R01] = 176 frame_addr + offsetof(struct target_rt_sigframe, info); 177 env->gpr[HEX_REG_R02] = 178 frame_addr + offsetof(struct target_rt_sigframe, uc); 179 env->gpr[HEX_REG_LR] = default_rt_sigreturn; 180 181 return; 182 183 badframe: 184 unlock_user_struct(frame, frame_addr, 1); 185 if (sig == TARGET_SIGSEGV) { 186 ka->_sa_handler = TARGET_SIG_DFL; 187 } 188 force_sig(TARGET_SIGSEGV); 189 } 190 191 static void restore_sigcontext(CPUHexagonState *env, 192 struct target_sigcontext *sc) 193 { 194 __get_user(env->gpr[HEX_REG_R00], &sc->r0); 195 __get_user(env->gpr[HEX_REG_R01], &sc->r1); 196 __get_user(env->gpr[HEX_REG_R02], &sc->r2); 197 __get_user(env->gpr[HEX_REG_R03], &sc->r3); 198 __get_user(env->gpr[HEX_REG_R04], &sc->r4); 199 __get_user(env->gpr[HEX_REG_R05], &sc->r5); 200 __get_user(env->gpr[HEX_REG_R06], &sc->r6); 201 __get_user(env->gpr[HEX_REG_R07], &sc->r7); 202 __get_user(env->gpr[HEX_REG_R08], &sc->r8); 203 __get_user(env->gpr[HEX_REG_R09], &sc->r9); 204 __get_user(env->gpr[HEX_REG_R10], &sc->r10); 205 __get_user(env->gpr[HEX_REG_R11], &sc->r11); 206 __get_user(env->gpr[HEX_REG_R12], &sc->r12); 207 __get_user(env->gpr[HEX_REG_R13], &sc->r13); 208 __get_user(env->gpr[HEX_REG_R14], &sc->r14); 209 __get_user(env->gpr[HEX_REG_R15], &sc->r15); 210 __get_user(env->gpr[HEX_REG_R16], &sc->r16); 211 __get_user(env->gpr[HEX_REG_R17], &sc->r17); 212 __get_user(env->gpr[HEX_REG_R18], &sc->r18); 213 __get_user(env->gpr[HEX_REG_R19], &sc->r19); 214 __get_user(env->gpr[HEX_REG_R20], &sc->r20); 215 __get_user(env->gpr[HEX_REG_R21], &sc->r21); 216 __get_user(env->gpr[HEX_REG_R22], &sc->r22); 217 __get_user(env->gpr[HEX_REG_R23], &sc->r23); 218 __get_user(env->gpr[HEX_REG_R24], &sc->r24); 219 __get_user(env->gpr[HEX_REG_R25], &sc->r25); 220 __get_user(env->gpr[HEX_REG_R26], &sc->r26); 221 __get_user(env->gpr[HEX_REG_R27], &sc->r27); 222 __get_user(env->gpr[HEX_REG_R28], &sc->r28); 223 __get_user(env->gpr[HEX_REG_R29], &sc->r29); 224 __get_user(env->gpr[HEX_REG_R30], &sc->r30); 225 __get_user(env->gpr[HEX_REG_R31], &sc->r31); 226 __get_user(env->gpr[HEX_REG_SA0], &sc->sa0); 227 __get_user(env->gpr[HEX_REG_LC0], &sc->lc0); 228 __get_user(env->gpr[HEX_REG_SA1], &sc->sa1); 229 __get_user(env->gpr[HEX_REG_LC1], &sc->lc1); 230 __get_user(env->gpr[HEX_REG_M0], &sc->m0); 231 __get_user(env->gpr[HEX_REG_M1], &sc->m1); 232 __get_user(env->gpr[HEX_REG_USR], &sc->usr); 233 __get_user(env->gpr[HEX_REG_P3_0], &sc->p3_0); 234 __get_user(env->gpr[HEX_REG_GP], &sc->gp); 235 __get_user(env->gpr[HEX_REG_UGP], &sc->ugp); 236 __get_user(env->gpr[HEX_REG_PC], &sc->pc); 237 } 238 239 static void restore_ucontext(CPUHexagonState *env, struct target_ucontext *uc) 240 { 241 sigset_t blocked; 242 target_sigset_t target_set; 243 int i; 244 245 target_sigemptyset(&target_set); 246 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 247 __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i])); 248 } 249 250 target_to_host_sigset_internal(&blocked, &target_set); 251 set_sigmask(&blocked); 252 253 restore_sigcontext(env, &uc->uc_mcontext); 254 } 255 256 long do_rt_sigreturn(CPUHexagonState *env) 257 { 258 struct target_rt_sigframe *frame; 259 abi_ulong frame_addr; 260 261 frame_addr = env->gpr[HEX_REG_SP]; 262 trace_user_do_sigreturn(env, frame_addr); 263 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 264 goto badframe; 265 } 266 267 restore_ucontext(env, &frame->uc); 268 target_restore_altstack(&frame->uc.uc_stack, env); 269 270 unlock_user_struct(frame, frame_addr, 0); 271 return -TARGET_QEMU_ESIGRETURN; 272 273 badframe: 274 unlock_user_struct(frame, frame_addr, 0); 275 force_sig(TARGET_SIGSEGV); 276 return 0; 277 } 278 279 void setup_sigtramp(abi_ulong sigtramp_page) 280 { 281 uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 4 * 2, 0); 282 assert(tramp != NULL); 283 284 default_rt_sigreturn = sigtramp_page; 285 install_sigtramp(tramp); 286 287 unlock_user(tramp, sigtramp_page, 4 * 2); 288 } 289