| 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 | */ |
| 19 | #include "qemu/osdep.h" |
| 20 | #include "qemu.h" |
| 21 | #include "user-internals.h" |
| 22 | #include "signal-common.h" |
| 23 | #include "linux-user/trace.h" |
| 24 | #include "target/arm/cpu-features.h" |
| 25 | #include "vdso-asmoffset.h" |
| 26 | |
| 27 | struct target_sigcontext { |
| 28 | abi_ulong trap_no; |
| 29 | abi_ulong error_code; |
| 30 | abi_ulong oldmask; |
| 31 | abi_ulong arm_r0; |
| 32 | abi_ulong arm_r1; |
| 33 | abi_ulong arm_r2; |
| 34 | abi_ulong arm_r3; |
| 35 | abi_ulong arm_r4; |
| 36 | abi_ulong arm_r5; |
| 37 | abi_ulong arm_r6; |
| 38 | abi_ulong arm_r7; |
| 39 | abi_ulong arm_r8; |
| 40 | abi_ulong arm_r9; |
| 41 | abi_ulong arm_r10; |
| 42 | abi_ulong arm_fp; |
| 43 | abi_ulong arm_ip; |
| 44 | abi_ulong arm_sp; |
| 45 | abi_ulong arm_lr; |
| 46 | abi_ulong arm_pc; |
| 47 | abi_ulong arm_cpsr; |
| 48 | abi_ulong fault_address; |
| 49 | }; |
| 50 | |
| 51 | struct target_ucontext { |
| 52 | abi_ulong tuc_flags; |
| 53 | abi_ulong tuc_link; |
| 54 | target_stack_t tuc_stack; |
| 55 | struct target_sigcontext tuc_mcontext; |
| 56 | target_sigset_t tuc_sigmask; /* mask last for extensibility */ |
| 57 | char __unused[128 - sizeof(target_sigset_t)]; |
| 58 | abi_ulong tuc_regspace[128] __attribute__((__aligned__(8))); |
| 59 | }; |
| 60 | |
| 61 | struct target_user_vfp { |
| 62 | uint64_t fpregs[32]; |
| 63 | abi_ulong fpscr; |
| 64 | }; |
| 65 | |
| 66 | struct target_user_vfp_exc { |
| 67 | abi_ulong fpexc; |
| 68 | abi_ulong fpinst; |
| 69 | abi_ulong fpinst2; |
| 70 | }; |
| 71 | |
| 72 | struct target_vfp_sigframe { |
| 73 | abi_ulong magic; |
| 74 | abi_ulong size; |
| 75 | struct target_user_vfp ufp; |
| 76 | struct target_user_vfp_exc ufp_exc; |
| 77 | } __attribute__((__aligned__(8))); |
| 78 | |
| 79 | #define TARGET_VFP_MAGIC 0x56465001 |
| 80 | |
| 81 | struct sigframe |
| 82 | { |
| 83 | struct target_ucontext uc; |
| 84 | abi_ulong retcode[4]; |
| 85 | }; |
| 86 | |
| 87 | struct rt_sigframe |
| 88 | { |
| 89 | struct target_siginfo info; |
| 90 | struct sigframe sig; |
| 91 | }; |
| 92 | |
| 93 | QEMU_BUILD_BUG_ON(offsetof(struct sigframe, retcode[3]) |
| 94 | != SIGFRAME_RC3_OFFSET); |
| 95 | QEMU_BUILD_BUG_ON(offsetof(struct rt_sigframe, sig.retcode[3]) |
| 96 | != RT_SIGFRAME_RC3_OFFSET); |
| 97 | |
| 98 | static abi_ptr sigreturn_fdpic_tramp; |
| 99 | |
| 100 | /* |
| 101 | * Up to 3 words of 'retcode' in the sigframe are code, |
| 102 | * with retcode[3] being used by fdpic for the function descriptor. |
| 103 | * This code is not actually executed, but is retained for ABI compat. |
| 104 | * |
| 105 | * We will create a table of 8 retcode variants in the sigtramp page. |
| 106 | * Let each table entry use 3 words. |
| 107 | */ |
| 108 | #define RETCODE_WORDS 3 |
| 109 | #define RETCODE_BYTES (RETCODE_WORDS * 4) |
| 110 | |
| 111 | static inline int valid_user_regs(CPUARMState *regs) |
| 112 | { |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ |
| 118 | CPUARMState *env, abi_ulong mask) |
| 119 | { |
| 120 | __put_user(env->regs[0], &sc->arm_r0); |
| 121 | __put_user(env->regs[1], &sc->arm_r1); |
| 122 | __put_user(env->regs[2], &sc->arm_r2); |
| 123 | __put_user(env->regs[3], &sc->arm_r3); |
| 124 | __put_user(env->regs[4], &sc->arm_r4); |
| 125 | __put_user(env->regs[5], &sc->arm_r5); |
| 126 | __put_user(env->regs[6], &sc->arm_r6); |
| 127 | __put_user(env->regs[7], &sc->arm_r7); |
| 128 | __put_user(env->regs[8], &sc->arm_r8); |
| 129 | __put_user(env->regs[9], &sc->arm_r9); |
| 130 | __put_user(env->regs[10], &sc->arm_r10); |
| 131 | __put_user(env->regs[11], &sc->arm_fp); |
| 132 | __put_user(env->regs[12], &sc->arm_ip); |
| 133 | __put_user(env->regs[13], &sc->arm_sp); |
| 134 | __put_user(env->regs[14], &sc->arm_lr); |
| 135 | __put_user(env->regs[15], &sc->arm_pc); |
| 136 | __put_user(cpsr_read(env), &sc->arm_cpsr); |
| 137 | |
| 138 | __put_user(/* current->thread.trap_no */ 0, &sc->trap_no); |
| 139 | __put_user(/* current->thread.error_code */ 0, &sc->error_code); |
| 140 | __put_user(/* current->thread.address */ 0, &sc->fault_address); |
| 141 | __put_user(mask, &sc->oldmask); |
| 142 | } |
| 143 | |
| 144 | static inline abi_ulong |
| 145 | get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize) |
| 146 | { |
| 147 | unsigned long sp; |
| 148 | |
| 149 | sp = target_sigsp(get_sp_from_cpustate(regs), ka); |
| 150 | /* |
| 151 | * ATPCS B01 mandates 8-byte alignment |
| 152 | */ |
| 153 | return (sp - framesize) & ~7; |
| 154 | } |
| 155 | |
| 156 | static void write_arm_sigreturn(uint32_t *rc, int syscall); |
| 157 | static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs); |
| 158 | |
| 159 | static int |
| 160 | setup_return(CPUARMState *env, struct target_sigaction *ka, int usig, |
| 161 | struct sigframe *frame, abi_ulong sp_addr) |
| 162 | { |
| 163 | abi_ulong handler = 0; |
| 164 | abi_ulong handler_fdpic_GOT = 0; |
| 165 | abi_ulong retcode; |
| 166 | bool is_fdpic = info_is_fdpic(get_task_state(thread_cpu)->info); |
| 167 | bool is_rt = ka->sa_flags & TARGET_SA_SIGINFO; |
| 168 | bool thumb; |
| 169 | |
| 170 | if (is_fdpic) { |
| 171 | /* In FDPIC mode, ka->_sa_handler points to a function |
| 172 | * descriptor (FD). The first word contains the address of the |
| 173 | * handler. The second word contains the value of the PIC |
| 174 | * register (r9). */ |
| 175 | abi_ulong funcdesc_ptr = ka->_sa_handler; |
| 176 | if (get_user_ual(handler, funcdesc_ptr) |
| 177 | || get_user_ual(handler_fdpic_GOT, funcdesc_ptr + 4)) { |
| 178 | return 1; |
| 179 | } |
| 180 | } else { |
| 181 | handler = ka->_sa_handler; |
| 182 | } |
| 183 | thumb = handler & 1; |
| 184 | |
| 185 | uint32_t cpsr = cpsr_read(env); |
| 186 | |
| 187 | cpsr &= ~CPSR_IT; |
| 188 | if (thumb) { |
| 189 | cpsr |= CPSR_T; |
| 190 | } else { |
| 191 | cpsr &= ~CPSR_T; |
| 192 | } |
| 193 | if (env->cp15.sctlr_el[1] & SCTLR_E0E) { |
| 194 | cpsr |= CPSR_E; |
| 195 | } else { |
| 196 | cpsr &= ~CPSR_E; |
| 197 | } |
| 198 | |
| 199 | /* Our vdso default_sigreturn label is a table of entry points. */ |
| 200 | retcode = default_sigreturn + (is_fdpic * 2 + is_rt) * 8; |
| 201 | |
| 202 | /* |
| 203 | * Put the sigreturn code on the stack no matter which return |
| 204 | * mechanism we use in order to remain ABI compliant. |
| 205 | * Because this is about ABI, always use the A32 instructions, |
| 206 | * despite the fact that our actual vdso trampoline is T16. |
| 207 | */ |
| 208 | if (is_fdpic) { |
| 209 | write_arm_fdpic_sigreturn(frame->retcode, |
| 210 | is_rt ? RT_SIGFRAME_RC3_OFFSET |
| 211 | : SIGFRAME_RC3_OFFSET); |
| 212 | } else { |
| 213 | write_arm_sigreturn(frame->retcode, |
| 214 | is_rt ? TARGET_NR_rt_sigreturn |
| 215 | : TARGET_NR_sigreturn); |
| 216 | } |
| 217 | |
| 218 | if (ka->sa_flags & TARGET_SA_RESTORER) { |
| 219 | if (is_fdpic) { |
| 220 | /* Place the function descriptor in slot 3. */ |
| 221 | __put_user((abi_ulong)ka->sa_restorer, &frame->retcode[3]); |
| 222 | } else { |
| 223 | retcode = ka->sa_restorer; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | env->regs[0] = usig; |
| 228 | if (is_fdpic) { |
| 229 | env->regs[9] = handler_fdpic_GOT; |
| 230 | } |
| 231 | env->regs[13] = sp_addr; |
| 232 | env->regs[14] = retcode; |
| 233 | env->regs[15] = handler & (thumb ? ~1 : ~3); |
| 234 | cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static abi_ulong *setup_sigframe_vfp(abi_ulong *regspace, CPUARMState *env) |
| 240 | { |
| 241 | int i; |
| 242 | struct target_vfp_sigframe *vfpframe; |
| 243 | vfpframe = (struct target_vfp_sigframe *)regspace; |
| 244 | __put_user(TARGET_VFP_MAGIC, &vfpframe->magic); |
| 245 | __put_user(sizeof(*vfpframe), &vfpframe->size); |
| 246 | for (i = 0; i < 32; i++) { |
| 247 | __put_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]); |
| 248 | } |
| 249 | __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr); |
| 250 | __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc); |
| 251 | __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst); |
| 252 | __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2); |
| 253 | return (abi_ulong*)(vfpframe+1); |
| 254 | } |
| 255 | |
| 256 | static void setup_sigframe(struct target_ucontext *uc, |
| 257 | target_sigset_t *set, CPUARMState *env) |
| 258 | { |
| 259 | struct target_sigaltstack stack; |
| 260 | int i; |
| 261 | abi_ulong *regspace; |
| 262 | |
| 263 | /* Clear all the bits of the ucontext we don't use. */ |
| 264 | memset(uc, 0, offsetof(struct target_ucontext, tuc_mcontext)); |
| 265 | |
| 266 | memset(&stack, 0, sizeof(stack)); |
| 267 | target_save_altstack(&stack, env); |
| 268 | memcpy(&uc->tuc_stack, &stack, sizeof(stack)); |
| 269 | |
| 270 | setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]); |
| 271 | /* Save coprocessor signal frame. */ |
| 272 | regspace = uc->tuc_regspace; |
| 273 | if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { |
| 274 | regspace = setup_sigframe_vfp(regspace, env); |
| 275 | } |
| 276 | |
| 277 | /* Write terminating magic word */ |
| 278 | __put_user(0, regspace); |
| 279 | |
| 280 | for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
| 281 | __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void setup_frame(int usig, struct target_sigaction *ka, |
| 286 | target_sigset_t *set, CPUARMState *regs) |
| 287 | { |
| 288 | struct sigframe *frame; |
| 289 | abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame)); |
| 290 | |
| 291 | trace_user_setup_frame(regs, frame_addr); |
| 292 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { |
| 293 | goto sigsegv; |
| 294 | } |
| 295 | |
| 296 | setup_sigframe(&frame->uc, set, regs); |
| 297 | |
| 298 | if (setup_return(regs, ka, usig, frame, frame_addr)) { |
| 299 | goto sigsegv; |
| 300 | } |
| 301 | |
| 302 | unlock_user_struct(frame, frame_addr, 1); |
| 303 | return; |
| 304 | sigsegv: |
| 305 | unlock_user_struct(frame, frame_addr, 1); |
| 306 | force_sigsegv(usig); |
| 307 | } |
| 308 | |
| 309 | void setup_rt_frame(int usig, struct target_sigaction *ka, |
| 310 | target_siginfo_t *info, |
| 311 | target_sigset_t *set, CPUARMState *env) |
| 312 | { |
| 313 | struct rt_sigframe *frame; |
| 314 | abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame)); |
| 315 | abi_ulong info_addr, uc_addr; |
| 316 | |
| 317 | trace_user_setup_rt_frame(env, frame_addr); |
| 318 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { |
| 319 | goto sigsegv; |
| 320 | } |
| 321 | |
| 322 | info_addr = frame_addr + offsetof(struct rt_sigframe, info); |
| 323 | uc_addr = frame_addr + offsetof(struct rt_sigframe, sig.uc); |
| 324 | frame->info = *info; |
| 325 | |
| 326 | setup_sigframe(&frame->sig.uc, set, env); |
| 327 | |
| 328 | if (setup_return(env, ka, usig, &frame->sig, frame_addr)) { |
| 329 | goto sigsegv; |
| 330 | } |
| 331 | |
| 332 | env->regs[1] = info_addr; |
| 333 | env->regs[2] = uc_addr; |
| 334 | |
| 335 | unlock_user_struct(frame, frame_addr, 1); |
| 336 | return; |
| 337 | sigsegv: |
| 338 | unlock_user_struct(frame, frame_addr, 1); |
| 339 | force_sigsegv(usig); |
| 340 | } |
| 341 | |
| 342 | static int |
| 343 | restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc) |
| 344 | { |
| 345 | int err = 0; |
| 346 | uint32_t cpsr; |
| 347 | |
| 348 | __get_user(env->regs[0], &sc->arm_r0); |
| 349 | __get_user(env->regs[1], &sc->arm_r1); |
| 350 | __get_user(env->regs[2], &sc->arm_r2); |
| 351 | __get_user(env->regs[3], &sc->arm_r3); |
| 352 | __get_user(env->regs[4], &sc->arm_r4); |
| 353 | __get_user(env->regs[5], &sc->arm_r5); |
| 354 | __get_user(env->regs[6], &sc->arm_r6); |
| 355 | __get_user(env->regs[7], &sc->arm_r7); |
| 356 | __get_user(env->regs[8], &sc->arm_r8); |
| 357 | __get_user(env->regs[9], &sc->arm_r9); |
| 358 | __get_user(env->regs[10], &sc->arm_r10); |
| 359 | __get_user(env->regs[11], &sc->arm_fp); |
| 360 | __get_user(env->regs[12], &sc->arm_ip); |
| 361 | __get_user(env->regs[13], &sc->arm_sp); |
| 362 | __get_user(env->regs[14], &sc->arm_lr); |
| 363 | __get_user(env->regs[15], &sc->arm_pc); |
| 364 | __get_user(cpsr, &sc->arm_cpsr); |
| 365 | cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr); |
| 366 | |
| 367 | err |= !valid_user_regs(env); |
| 368 | |
| 369 | return err; |
| 370 | } |
| 371 | |
| 372 | static abi_ulong *restore_sigframe_vfp(CPUARMState *env, abi_ulong *regspace) |
| 373 | { |
| 374 | int i; |
| 375 | abi_ulong magic, sz; |
| 376 | uint32_t fpscr, fpexc; |
| 377 | struct target_vfp_sigframe *vfpframe; |
| 378 | vfpframe = (struct target_vfp_sigframe *)regspace; |
| 379 | |
| 380 | __get_user(magic, &vfpframe->magic); |
| 381 | __get_user(sz, &vfpframe->size); |
| 382 | if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) { |
| 383 | return 0; |
| 384 | } |
| 385 | for (i = 0; i < 32; i++) { |
| 386 | __get_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]); |
| 387 | } |
| 388 | __get_user(fpscr, &vfpframe->ufp.fpscr); |
| 389 | vfp_set_fpscr(env, fpscr); |
| 390 | __get_user(fpexc, &vfpframe->ufp_exc.fpexc); |
| 391 | /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid |
| 392 | * and the exception flag is cleared |
| 393 | */ |
| 394 | fpexc |= (1 << 30); |
| 395 | fpexc &= ~((1 << 31) | (1 << 28)); |
| 396 | env->vfp.xregs[ARM_VFP_FPEXC] = fpexc; |
| 397 | __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst); |
| 398 | __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2); |
| 399 | return (abi_ulong*)(vfpframe + 1); |
| 400 | } |
| 401 | |
| 402 | static int do_sigframe_return(CPUARMState *env, |
| 403 | target_ulong context_addr, |
| 404 | struct target_ucontext *uc) |
| 405 | { |
| 406 | sigset_t host_set; |
| 407 | abi_ulong *regspace; |
| 408 | |
| 409 | target_to_host_sigset(&host_set, &uc->tuc_sigmask); |
| 410 | set_sigmask(&host_set); |
| 411 | |
| 412 | if (restore_sigcontext(env, &uc->tuc_mcontext)) { |
| 413 | return 1; |
| 414 | } |
| 415 | |
| 416 | /* Restore coprocessor signal frame */ |
| 417 | regspace = uc->tuc_regspace; |
| 418 | if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { |
| 419 | regspace = restore_sigframe_vfp(env, regspace); |
| 420 | if (!regspace) { |
| 421 | return 1; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | target_restore_altstack(&uc->tuc_stack, env); |
| 426 | |
| 427 | #if 0 |
| 428 | /* Send SIGTRAP if we're single-stepping */ |
| 429 | if (ptrace_cancel_bpt(current)) |
| 430 | send_sig(SIGTRAP, current, 1); |
| 431 | #endif |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | long do_sigreturn(CPUARMState *env) |
| 437 | { |
| 438 | abi_ulong frame_addr; |
| 439 | struct sigframe *frame = NULL; |
| 440 | |
| 441 | /* |
| 442 | * Since we stacked the signal on a 64-bit boundary, |
| 443 | * then 'sp' should be word aligned here. If it's |
| 444 | * not, then the user is trying to mess with us. |
| 445 | */ |
| 446 | frame_addr = env->regs[13]; |
| 447 | trace_user_do_sigreturn(env, frame_addr); |
| 448 | if (frame_addr & 7) { |
| 449 | goto badframe; |
| 450 | } |
| 451 | |
| 452 | if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { |
| 453 | goto badframe; |
| 454 | } |
| 455 | |
| 456 | if (do_sigframe_return(env, |
| 457 | frame_addr + offsetof(struct sigframe, uc), |
| 458 | &frame->uc)) { |
| 459 | goto badframe; |
| 460 | } |
| 461 | |
| 462 | unlock_user_struct(frame, frame_addr, 0); |
| 463 | return -QEMU_ESIGRETURN; |
| 464 | |
| 465 | badframe: |
| 466 | unlock_user_struct(frame, frame_addr, 0); |
| 467 | force_sig(TARGET_SIGSEGV); |
| 468 | return -QEMU_ESIGRETURN; |
| 469 | } |
| 470 | |
| 471 | long do_rt_sigreturn(CPUARMState *env) |
| 472 | { |
| 473 | abi_ulong frame_addr; |
| 474 | struct rt_sigframe *frame = NULL; |
| 475 | |
| 476 | /* |
| 477 | * Since we stacked the signal on a 64-bit boundary, |
| 478 | * then 'sp' should be word aligned here. If it's |
| 479 | * not, then the user is trying to mess with us. |
| 480 | */ |
| 481 | frame_addr = env->regs[13]; |
| 482 | trace_user_do_rt_sigreturn(env, frame_addr); |
| 483 | if (frame_addr & 7) { |
| 484 | goto badframe; |
| 485 | } |
| 486 | |
| 487 | if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { |
| 488 | goto badframe; |
| 489 | } |
| 490 | |
| 491 | if (do_sigframe_return(env, |
| 492 | frame_addr + offsetof(struct rt_sigframe, sig.uc), |
| 493 | &frame->sig.uc)) { |
| 494 | goto badframe; |
| 495 | } |
| 496 | |
| 497 | unlock_user_struct(frame, frame_addr, 0); |
| 498 | return -QEMU_ESIGRETURN; |
| 499 | |
| 500 | badframe: |
| 501 | unlock_user_struct(frame, frame_addr, 0); |
| 502 | force_sig(TARGET_SIGSEGV); |
| 503 | return -QEMU_ESIGRETURN; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * EABI syscalls pass the number via r7. |
| 508 | * Note that the kernel still adds the OABI syscall number to the trap, |
| 509 | * presumably for backward ABI compatibility with unwinders. |
| 510 | */ |
| 511 | #define ARM_MOV_R7_IMM(X) (0xe3a07000 | (X)) |
| 512 | #define ARM_SWI_SYS(X) (0xef000000 | (X) | ARM_SYSCALL_BASE) |
| 513 | |
| 514 | #define THUMB_MOVS_R7_IMM(X) (0x2700 | (X)) |
| 515 | #define THUMB_SWI_SYS 0xdf00 |
| 516 | |
| 517 | static void write_arm_sigreturn(uint32_t *rc, int syscall) |
| 518 | { |
| 519 | __put_user(ARM_MOV_R7_IMM(syscall), rc); |
| 520 | __put_user(ARM_SWI_SYS(syscall), rc + 1); |
| 521 | /* Wrote 8 of 12 bytes */ |
| 522 | } |
| 523 | |
| 524 | static void write_thm_sigreturn(uint32_t *rc, int syscall) |
| 525 | { |
| 526 | __put_user(THUMB_SWI_SYS << 16 | THUMB_MOVS_R7_IMM(syscall), rc); |
| 527 | /* Wrote 4 of 12 bytes */ |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Stub needed to make sure the FD register (r9) contains the right value. |
| 532 | * Use the same instruction sequence as the kernel. |
| 533 | */ |
| 534 | static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs) |
| 535 | { |
| 536 | assert(ofs <= 0xfff); |
| 537 | __put_user(0xe59d3000 | ofs, rc + 0); /* ldr r3, [sp, #ofs] */ |
| 538 | __put_user(0xe8930908, rc + 1); /* ldm r3, { r3, r9 } */ |
| 539 | __put_user(0xe12fff13, rc + 2); /* bx r3 */ |
| 540 | /* Wrote 12 of 12 bytes */ |
| 541 | } |
| 542 | |
| 543 | static void write_thm_fdpic_sigreturn(void *vrc, int ofs) |
| 544 | { |
| 545 | uint16_t *rc = vrc; |
| 546 | |
| 547 | assert((ofs & ~0x3fc) == 0); |
| 548 | __put_user(0x9b00 | (ofs >> 2), rc + 0); /* ldr r3, [sp, #ofs] */ |
| 549 | __put_user(0xcb0c, rc + 1); /* ldm r3, { r2, r3 } */ |
| 550 | __put_user(0x4699, rc + 2); /* mov r9, r3 */ |
| 551 | __put_user(0x4710, rc + 3); /* bx r2 */ |
| 552 | /* Wrote 8 of 12 bytes */ |
| 553 | } |
| 554 | |
| 555 | void setup_sigtramp(abi_ulong sigtramp_page) |
| 556 | { |
| 557 | uint32_t total_size = 8 * RETCODE_BYTES; |
| 558 | uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, total_size, 0); |
| 559 | |
| 560 | assert(tramp != NULL); |
| 561 | |
| 562 | default_sigreturn = sigtramp_page; |
| 563 | write_arm_sigreturn(&tramp[0 * RETCODE_WORDS], TARGET_NR_sigreturn); |
| 564 | write_thm_sigreturn(&tramp[1 * RETCODE_WORDS], TARGET_NR_sigreturn); |
| 565 | write_arm_sigreturn(&tramp[2 * RETCODE_WORDS], TARGET_NR_rt_sigreturn); |
| 566 | write_thm_sigreturn(&tramp[3 * RETCODE_WORDS], TARGET_NR_rt_sigreturn); |
| 567 | |
| 568 | sigreturn_fdpic_tramp = sigtramp_page + 4 * RETCODE_BYTES; |
| 569 | write_arm_fdpic_sigreturn(tramp + 4 * RETCODE_WORDS, |
| 570 | offsetof(struct sigframe, retcode[3])); |
| 571 | write_thm_fdpic_sigreturn(tramp + 5 * RETCODE_WORDS, |
| 572 | offsetof(struct sigframe, retcode[3])); |
| 573 | write_arm_fdpic_sigreturn(tramp + 6 * RETCODE_WORDS, |
| 574 | offsetof(struct rt_sigframe, sig.retcode[3])); |
| 575 | write_thm_fdpic_sigreturn(tramp + 7 * RETCODE_WORDS, |
| 576 | offsetof(struct rt_sigframe, sig.retcode[3])); |
| 577 | |
| 578 | unlock_user(tramp, sigtramp_page, total_size); |
| 579 | } |