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 gp; 43 target_ulong ugp; 44 target_ulong pc; 45 target_ulong cause; 46 target_ulong badva; 47 target_ulong pred[NUM_PREGS]; 48 }; 49 50 struct target_ucontext { 51 unsigned long uc_flags; 52 target_ulong uc_link; /* target pointer */ 53 target_stack_t uc_stack; 54 struct target_sigcontext uc_mcontext; 55 target_sigset_t uc_sigmask; 56 }; 57 58 struct target_rt_sigframe { 59 uint32_t tramp[2]; 60 struct target_siginfo info; 61 struct target_ucontext uc; 62 }; 63 64 static abi_ulong get_sigframe(struct target_sigaction *ka, 65 CPUHexagonState *regs, size_t framesize) 66 { 67 abi_ulong sp = get_sp_from_cpustate(regs); 68 69 /* This is the X/Open sanctioned signal stack switching. */ 70 sp = target_sigsp(sp, ka) - framesize; 71 72 sp = QEMU_ALIGN_DOWN(sp, 8); 73 74 return sp; 75 } 76 77 static void setup_sigcontext(struct target_sigcontext *sc, CPUHexagonState *env) 78 { 79 __put_user(env->gpr[HEX_REG_R00], &sc->r0); 80 __put_user(env->gpr[HEX_REG_R01], &sc->r1); 81 __put_user(env->gpr[HEX_REG_R02], &sc->r2); 82 __put_user(env->gpr[HEX_REG_R03], &sc->r3); 83 __put_user(env->gpr[HEX_REG_R04], &sc->r4); 84 __put_user(env->gpr[HEX_REG_R05], &sc->r5); 85 __put_user(env->gpr[HEX_REG_R06], &sc->r6); 86 __put_user(env->gpr[HEX_REG_R07], &sc->r7); 87 __put_user(env->gpr[HEX_REG_R08], &sc->r8); 88 __put_user(env->gpr[HEX_REG_R09], &sc->r9); 89 __put_user(env->gpr[HEX_REG_R10], &sc->r10); 90 __put_user(env->gpr[HEX_REG_R11], &sc->r11); 91 __put_user(env->gpr[HEX_REG_R12], &sc->r12); 92 __put_user(env->gpr[HEX_REG_R13], &sc->r13); 93 __put_user(env->gpr[HEX_REG_R14], &sc->r14); 94 __put_user(env->gpr[HEX_REG_R15], &sc->r15); 95 __put_user(env->gpr[HEX_REG_R16], &sc->r16); 96 __put_user(env->gpr[HEX_REG_R17], &sc->r17); 97 __put_user(env->gpr[HEX_REG_R18], &sc->r18); 98 __put_user(env->gpr[HEX_REG_R19], &sc->r19); 99 __put_user(env->gpr[HEX_REG_R20], &sc->r20); 100 __put_user(env->gpr[HEX_REG_R21], &sc->r21); 101 __put_user(env->gpr[HEX_REG_R22], &sc->r22); 102 __put_user(env->gpr[HEX_REG_R23], &sc->r23); 103 __put_user(env->gpr[HEX_REG_R24], &sc->r24); 104 __put_user(env->gpr[HEX_REG_R25], &sc->r25); 105 __put_user(env->gpr[HEX_REG_R26], &sc->r26); 106 __put_user(env->gpr[HEX_REG_R27], &sc->r27); 107 __put_user(env->gpr[HEX_REG_R28], &sc->r28); 108 __put_user(env->gpr[HEX_REG_R29], &sc->r29); 109 __put_user(env->gpr[HEX_REG_R30], &sc->r30); 110 __put_user(env->gpr[HEX_REG_R31], &sc->r31); 111 __put_user(env->gpr[HEX_REG_SA0], &sc->sa0); 112 __put_user(env->gpr[HEX_REG_LC0], &sc->lc0); 113 __put_user(env->gpr[HEX_REG_SA1], &sc->sa1); 114 __put_user(env->gpr[HEX_REG_LC1], &sc->lc1); 115 __put_user(env->gpr[HEX_REG_M0], &sc->m0); 116 __put_user(env->gpr[HEX_REG_M1], &sc->m1); 117 __put_user(env->gpr[HEX_REG_USR], &sc->usr); 118 __put_user(env->gpr[HEX_REG_GP], &sc->gp); 119 __put_user(env->gpr[HEX_REG_UGP], &sc->ugp); 120 __put_user(env->gpr[HEX_REG_PC], &sc->pc); 121 122 int i; 123 for (i = 0; i < NUM_PREGS; i++) { 124 __put_user(env->pred[i], &(sc->pred[i])); 125 } 126 } 127 128 static void setup_ucontext(struct target_ucontext *uc, 129 CPUHexagonState *env, target_sigset_t *set) 130 { 131 __put_user(0, &(uc->uc_flags)); 132 __put_user(0, &(uc->uc_link)); 133 134 target_save_altstack(&uc->uc_stack, env); 135 136 int i; 137 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 138 __put_user(set->sig[i], &(uc->uc_sigmask.sig[i])); 139 } 140 141 setup_sigcontext(&uc->uc_mcontext, env); 142 } 143 144 static inline void install_sigtramp(uint32_t *tramp) 145 { 146 __put_user(0x7800d166, tramp + 0); /* { r6=#__NR_rt_sigreturn } */ 147 __put_user(0x5400c004, tramp + 1); /* { trap0(#1) } */ 148 } 149 150 void setup_rt_frame(int sig, struct target_sigaction *ka, 151 target_siginfo_t *info, 152 target_sigset_t *set, CPUHexagonState *env) 153 { 154 abi_ulong frame_addr; 155 struct target_rt_sigframe *frame; 156 157 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 158 trace_user_setup_rt_frame(env, frame_addr); 159 160 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 161 goto badframe; 162 } 163 164 setup_ucontext(&frame->uc, env, set); 165 tswap_siginfo(&frame->info, info); 166 /* 167 * The on-stack signal trampoline is no longer executed; 168 * however, the libgcc signal frame unwinding code checks 169 * for the presence of these two numeric magic values. 170 */ 171 install_sigtramp(frame->tramp); 172 173 env->gpr[HEX_REG_PC] = ka->_sa_handler; 174 env->gpr[HEX_REG_SP] = frame_addr; 175 env->gpr[HEX_REG_R00] = sig; 176 env->gpr[HEX_REG_R01] = 177 frame_addr + offsetof(struct target_rt_sigframe, info); 178 env->gpr[HEX_REG_R02] = 179 frame_addr + offsetof(struct target_rt_sigframe, uc); 180 env->gpr[HEX_REG_LR] = default_rt_sigreturn; 181 182 return; 183 184 badframe: 185 unlock_user_struct(frame, frame_addr, 1); 186 if (sig == TARGET_SIGSEGV) { 187 ka->_sa_handler = TARGET_SIG_DFL; 188 } 189 force_sig(TARGET_SIGSEGV); 190 } 191 192 static void restore_sigcontext(CPUHexagonState *env, 193 struct target_sigcontext *sc) 194 { 195 __get_user(env->gpr[HEX_REG_R00], &sc->r0); 196 __get_user(env->gpr[HEX_REG_R01], &sc->r1); 197 __get_user(env->gpr[HEX_REG_R02], &sc->r2); 198 __get_user(env->gpr[HEX_REG_R03], &sc->r3); 199 __get_user(env->gpr[HEX_REG_R04], &sc->r4); 200 __get_user(env->gpr[HEX_REG_R05], &sc->r5); 201 __get_user(env->gpr[HEX_REG_R06], &sc->r6); 202 __get_user(env->gpr[HEX_REG_R07], &sc->r7); 203 __get_user(env->gpr[HEX_REG_R08], &sc->r8); 204 __get_user(env->gpr[HEX_REG_R09], &sc->r9); 205 __get_user(env->gpr[HEX_REG_R10], &sc->r10); 206 __get_user(env->gpr[HEX_REG_R11], &sc->r11); 207 __get_user(env->gpr[HEX_REG_R12], &sc->r12); 208 __get_user(env->gpr[HEX_REG_R13], &sc->r13); 209 __get_user(env->gpr[HEX_REG_R14], &sc->r14); 210 __get_user(env->gpr[HEX_REG_R15], &sc->r15); 211 __get_user(env->gpr[HEX_REG_R16], &sc->r16); 212 __get_user(env->gpr[HEX_REG_R17], &sc->r17); 213 __get_user(env->gpr[HEX_REG_R18], &sc->r18); 214 __get_user(env->gpr[HEX_REG_R19], &sc->r19); 215 __get_user(env->gpr[HEX_REG_R20], &sc->r20); 216 __get_user(env->gpr[HEX_REG_R21], &sc->r21); 217 __get_user(env->gpr[HEX_REG_R22], &sc->r22); 218 __get_user(env->gpr[HEX_REG_R23], &sc->r23); 219 __get_user(env->gpr[HEX_REG_R24], &sc->r24); 220 __get_user(env->gpr[HEX_REG_R25], &sc->r25); 221 __get_user(env->gpr[HEX_REG_R26], &sc->r26); 222 __get_user(env->gpr[HEX_REG_R27], &sc->r27); 223 __get_user(env->gpr[HEX_REG_R28], &sc->r28); 224 __get_user(env->gpr[HEX_REG_R29], &sc->r29); 225 __get_user(env->gpr[HEX_REG_R30], &sc->r30); 226 __get_user(env->gpr[HEX_REG_R31], &sc->r31); 227 __get_user(env->gpr[HEX_REG_SA0], &sc->sa0); 228 __get_user(env->gpr[HEX_REG_LC0], &sc->lc0); 229 __get_user(env->gpr[HEX_REG_SA1], &sc->sa1); 230 __get_user(env->gpr[HEX_REG_LC1], &sc->lc1); 231 __get_user(env->gpr[HEX_REG_M0], &sc->m0); 232 __get_user(env->gpr[HEX_REG_M1], &sc->m1); 233 __get_user(env->gpr[HEX_REG_USR], &sc->usr); 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 int i; 239 for (i = 0; i < NUM_PREGS; i++) { 240 __get_user(env->pred[i], &(sc->pred[i])); 241 } 242 } 243 244 static void restore_ucontext(CPUHexagonState *env, struct target_ucontext *uc) 245 { 246 sigset_t blocked; 247 target_sigset_t target_set; 248 int i; 249 250 target_sigemptyset(&target_set); 251 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 252 __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i])); 253 } 254 255 target_to_host_sigset_internal(&blocked, &target_set); 256 set_sigmask(&blocked); 257 258 restore_sigcontext(env, &uc->uc_mcontext); 259 } 260 261 long do_rt_sigreturn(CPUHexagonState *env) 262 { 263 struct target_rt_sigframe *frame; 264 abi_ulong frame_addr; 265 266 frame_addr = env->gpr[HEX_REG_SP]; 267 trace_user_do_sigreturn(env, frame_addr); 268 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 269 goto badframe; 270 } 271 272 restore_ucontext(env, &frame->uc); 273 target_restore_altstack(&frame->uc.uc_stack, env); 274 275 unlock_user_struct(frame, frame_addr, 0); 276 return -QEMU_ESIGRETURN; 277 278 badframe: 279 unlock_user_struct(frame, frame_addr, 0); 280 force_sig(TARGET_SIGSEGV); 281 return 0; 282 } 283 284 void setup_sigtramp(abi_ulong sigtramp_page) 285 { 286 uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 4 * 2, 0); 287 assert(tramp != NULL); 288 289 default_rt_sigreturn = sigtramp_page; 290 install_sigtramp(tramp); 291 292 unlock_user(tramp, sigtramp_page, 4 * 2); 293 } 294