| 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 | |
| 25 | /* |
| 26 | * code and data structures from linux kernel: |
| 27 | * include/asm-sh/sigcontext.h |
| 28 | * arch/sh/kernel/signal.c |
| 29 | */ |
| 30 | |
| 31 | struct target_sigcontext { |
| 32 | target_ulong oldmask; |
| 33 | |
| 34 | /* CPU registers */ |
| 35 | target_ulong sc_gregs[16]; |
| 36 | target_ulong sc_pc; |
| 37 | target_ulong sc_pr; |
| 38 | target_ulong sc_sr; |
| 39 | target_ulong sc_gbr; |
| 40 | target_ulong sc_mach; |
| 41 | target_ulong sc_macl; |
| 42 | |
| 43 | /* FPU registers */ |
| 44 | target_ulong sc_fpregs[16]; |
| 45 | target_ulong sc_xfpregs[16]; |
| 46 | unsigned int sc_fpscr; |
| 47 | unsigned int sc_fpul; |
| 48 | unsigned int sc_ownedfp; |
| 49 | }; |
| 50 | |
| 51 | struct target_sigframe |
| 52 | { |
| 53 | struct target_sigcontext sc; |
| 54 | target_ulong extramask[TARGET_NSIG_WORDS-1]; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | struct target_ucontext { |
| 59 | target_ulong tuc_flags; |
| 60 | abi_ulong tuc_link; |
| 61 | target_stack_t tuc_stack; |
| 62 | struct target_sigcontext tuc_mcontext; |
| 63 | target_sigset_t tuc_sigmask; /* mask last for extensibility */ |
| 64 | }; |
| 65 | |
| 66 | struct target_rt_sigframe |
| 67 | { |
| 68 | struct target_siginfo info; |
| 69 | struct target_ucontext uc; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */ |
| 74 | #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */ |
| 75 | |
| 76 | static abi_ulong get_sigframe(struct target_sigaction *ka, |
| 77 | unsigned long sp, size_t frame_size) |
| 78 | { |
| 79 | sp = target_sigsp(sp, ka); |
| 80 | |
| 81 | return (sp - frame_size) & -8ul; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Notice when we're in the middle of a gUSA region and reset. |
| 86 | * Note that this will only occur when #CF_PARALLEL is unset, as we |
| 87 | * will translate such sequences differently in a parallel context. |
| 88 | */ |
| 89 | static void unwind_gusa(CPUSH4State *regs) |
| 90 | { |
| 91 | /* If the stack pointer is sufficiently negative, and we haven't |
| 92 | completed the sequence, then reset to the entry to the region. */ |
| 93 | /* ??? The SH4 kernel checks for and address above 0xC0000000. |
| 94 | However, the page mappings in qemu linux-user aren't as restricted |
| 95 | and we wind up with the normal stack mapped above 0xF0000000. |
| 96 | That said, there is no reason why the kernel should be allowing |
| 97 | a gUSA region that spans 1GB. Use a tighter check here, for what |
| 98 | can actually be enabled by the immediate move. */ |
| 99 | if (regs->gregs[15] >= -128u && regs->pc < regs->gregs[0]) { |
| 100 | /* Reset the PC to before the gUSA region, as computed from |
| 101 | R0 = region end, SP = -(region size), plus one more for the |
| 102 | insn that actually initializes SP to the region size. */ |
| 103 | regs->pc = regs->gregs[0] + regs->gregs[15] - 2; |
| 104 | |
| 105 | /* Reset the SP to the saved version in R1. */ |
| 106 | regs->gregs[15] = regs->gregs[1]; |
| 107 | } else if (regs->gregs[15] >= -128u && regs->pc == regs->gregs[0]) { |
| 108 | /* If we are on the last instruction of a gUSA region, we must reset |
| 109 | the SP, otherwise we would be pushing the signal context to |
| 110 | invalid memory. */ |
| 111 | regs->gregs[15] = regs->gregs[1]; |
| 112 | } else if (regs->flags & (TB_FLAG_DELAY_SLOT | TB_FLAG_DELAY_SLOT_COND)) { |
| 113 | /* If we are in a delay slot, push the previous instruction. */ |
| 114 | regs->pc -= 2; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void setup_sigcontext(struct target_sigcontext *sc, |
| 119 | CPUSH4State *regs, unsigned long mask) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | #define COPY(x) __put_user(regs->x, &sc->sc_##x) |
| 124 | COPY(gregs[0]); COPY(gregs[1]); |
| 125 | COPY(gregs[2]); COPY(gregs[3]); |
| 126 | COPY(gregs[4]); COPY(gregs[5]); |
| 127 | COPY(gregs[6]); COPY(gregs[7]); |
| 128 | COPY(gregs[8]); COPY(gregs[9]); |
| 129 | COPY(gregs[10]); COPY(gregs[11]); |
| 130 | COPY(gregs[12]); COPY(gregs[13]); |
| 131 | COPY(gregs[14]); COPY(gregs[15]); |
| 132 | COPY(gbr); COPY(mach); |
| 133 | COPY(macl); COPY(pr); |
| 134 | COPY(pc); |
| 135 | #undef COPY |
| 136 | /* The T, M and Q bits live outside env->sr; fold them back in. */ |
| 137 | __put_user(cpu_read_sr(regs), &sc->sc_sr); |
| 138 | |
| 139 | for (i=0; i<16; i++) { |
| 140 | __put_user(regs->fregs[i], &sc->sc_fpregs[i]); |
| 141 | } |
| 142 | __put_user(regs->fpscr, &sc->sc_fpscr); |
| 143 | __put_user(regs->fpul, &sc->sc_fpul); |
| 144 | |
| 145 | /* non-iBCS2 extensions.. */ |
| 146 | __put_user(mask, &sc->oldmask); |
| 147 | } |
| 148 | |
| 149 | static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | #define COPY(x) __get_user(regs->x, &sc->sc_##x) |
| 154 | COPY(gregs[0]); COPY(gregs[1]); |
| 155 | COPY(gregs[2]); COPY(gregs[3]); |
| 156 | COPY(gregs[4]); COPY(gregs[5]); |
| 157 | COPY(gregs[6]); COPY(gregs[7]); |
| 158 | COPY(gregs[8]); COPY(gregs[9]); |
| 159 | COPY(gregs[10]); COPY(gregs[11]); |
| 160 | COPY(gregs[12]); COPY(gregs[13]); |
| 161 | COPY(gregs[14]); COPY(gregs[15]); |
| 162 | COPY(gbr); COPY(mach); |
| 163 | COPY(macl); COPY(pr); |
| 164 | COPY(pc); |
| 165 | #undef COPY |
| 166 | /* The T, M and Q bits live outside env->sr; unfold them. */ |
| 167 | { |
| 168 | uint32_t sr; |
| 169 | __get_user(sr, &sc->sc_sr); |
| 170 | cpu_write_sr(regs, sr); |
| 171 | } |
| 172 | |
| 173 | for (i=0; i<16; i++) { |
| 174 | __get_user(regs->fregs[i], &sc->sc_fpregs[i]); |
| 175 | } |
| 176 | /* Resync the derived float_status state, not just env->fpscr. */ |
| 177 | { |
| 178 | uint32_t fpscr; |
| 179 | __get_user(fpscr, &sc->sc_fpscr); |
| 180 | cpu_load_fpscr(regs, fpscr); |
| 181 | } |
| 182 | __get_user(regs->fpul, &sc->sc_fpul); |
| 183 | |
| 184 | regs->tra = -1; /* disable syscall checks */ |
| 185 | regs->flags = 0; |
| 186 | } |
| 187 | |
| 188 | void setup_frame(int sig, struct target_sigaction *ka, |
| 189 | target_sigset_t *set, CPUSH4State *regs) |
| 190 | { |
| 191 | struct target_sigframe *frame; |
| 192 | abi_ulong frame_addr; |
| 193 | int i; |
| 194 | |
| 195 | unwind_gusa(regs); |
| 196 | |
| 197 | frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame)); |
| 198 | trace_user_setup_frame(regs, frame_addr); |
| 199 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { |
| 200 | goto give_sigsegv; |
| 201 | } |
| 202 | |
| 203 | setup_sigcontext(&frame->sc, regs, set->sig[0]); |
| 204 | |
| 205 | for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { |
| 206 | __put_user(set->sig[i + 1], &frame->extramask[i]); |
| 207 | } |
| 208 | |
| 209 | regs->fpscr = FPSCR_PR; |
| 210 | |
| 211 | /* Set up to return from userspace. If provided, use a stub |
| 212 | already in userspace. */ |
| 213 | if (ka->sa_flags & TARGET_SA_RESTORER) { |
| 214 | regs->pr = ka->sa_restorer; |
| 215 | } else { |
| 216 | regs->pr = default_sigreturn; |
| 217 | } |
| 218 | |
| 219 | /* Set up registers for signal handler */ |
| 220 | regs->gregs[15] = frame_addr; |
| 221 | regs->gregs[4] = sig; /* Arg for signal handler */ |
| 222 | regs->gregs[5] = 0; |
| 223 | regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc); |
| 224 | regs->pc = (unsigned long) ka->_sa_handler; |
| 225 | regs->flags &= ~(TB_FLAG_DELAY_SLOT_MASK | TB_FLAG_GUSA_MASK); |
| 226 | |
| 227 | unlock_user_struct(frame, frame_addr, 1); |
| 228 | return; |
| 229 | |
| 230 | give_sigsegv: |
| 231 | unlock_user_struct(frame, frame_addr, 1); |
| 232 | force_sigsegv(sig); |
| 233 | } |
| 234 | |
| 235 | void setup_rt_frame(int sig, struct target_sigaction *ka, |
| 236 | target_siginfo_t *info, |
| 237 | target_sigset_t *set, CPUSH4State *regs) |
| 238 | { |
| 239 | struct target_rt_sigframe *frame; |
| 240 | abi_ulong frame_addr; |
| 241 | int i; |
| 242 | |
| 243 | unwind_gusa(regs); |
| 244 | |
| 245 | frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame)); |
| 246 | trace_user_setup_rt_frame(regs, frame_addr); |
| 247 | if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { |
| 248 | goto give_sigsegv; |
| 249 | } |
| 250 | |
| 251 | frame->info = *info; |
| 252 | |
| 253 | /* Create the ucontext. */ |
| 254 | __put_user(0, &frame->uc.tuc_flags); |
| 255 | __put_user(0, &frame->uc.tuc_link); |
| 256 | target_save_altstack(&frame->uc.tuc_stack, regs); |
| 257 | setup_sigcontext(&frame->uc.tuc_mcontext, |
| 258 | regs, set->sig[0]); |
| 259 | for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
| 260 | __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]); |
| 261 | } |
| 262 | |
| 263 | regs->fpscr = FPSCR_PR; |
| 264 | |
| 265 | /* Set up to return from userspace. If provided, use a stub |
| 266 | already in userspace. */ |
| 267 | if (ka->sa_flags & TARGET_SA_RESTORER) { |
| 268 | regs->pr = ka->sa_restorer; |
| 269 | } else { |
| 270 | regs->pr = default_rt_sigreturn; |
| 271 | } |
| 272 | |
| 273 | /* Set up registers for signal handler */ |
| 274 | regs->gregs[15] = frame_addr; |
| 275 | regs->gregs[4] = sig; /* Arg for signal handler */ |
| 276 | regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info); |
| 277 | regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc); |
| 278 | regs->pc = (unsigned long) ka->_sa_handler; |
| 279 | regs->flags &= ~(TB_FLAG_DELAY_SLOT_MASK | TB_FLAG_GUSA_MASK); |
| 280 | |
| 281 | unlock_user_struct(frame, frame_addr, 1); |
| 282 | return; |
| 283 | |
| 284 | give_sigsegv: |
| 285 | unlock_user_struct(frame, frame_addr, 1); |
| 286 | force_sigsegv(sig); |
| 287 | } |
| 288 | |
| 289 | long do_sigreturn(CPUSH4State *regs) |
| 290 | { |
| 291 | struct target_sigframe *frame; |
| 292 | abi_ulong frame_addr; |
| 293 | sigset_t blocked; |
| 294 | target_sigset_t target_set; |
| 295 | int i; |
| 296 | |
| 297 | frame_addr = regs->gregs[15]; |
| 298 | trace_user_do_sigreturn(regs, frame_addr); |
| 299 | if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { |
| 300 | goto badframe; |
| 301 | } |
| 302 | |
| 303 | __get_user(target_set.sig[0], &frame->sc.oldmask); |
| 304 | for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
| 305 | __get_user(target_set.sig[i], &frame->extramask[i - 1]); |
| 306 | } |
| 307 | |
| 308 | target_to_host_sigset_internal(&blocked, &target_set); |
| 309 | set_sigmask(&blocked); |
| 310 | |
| 311 | restore_sigcontext(regs, &frame->sc); |
| 312 | |
| 313 | unlock_user_struct(frame, frame_addr, 0); |
| 314 | return -QEMU_ESIGRETURN; |
| 315 | |
| 316 | badframe: |
| 317 | unlock_user_struct(frame, frame_addr, 0); |
| 318 | force_sig(TARGET_SIGSEGV); |
| 319 | return -QEMU_ESIGRETURN; |
| 320 | } |
| 321 | |
| 322 | long do_rt_sigreturn(CPUSH4State *regs) |
| 323 | { |
| 324 | struct target_rt_sigframe *frame; |
| 325 | abi_ulong frame_addr; |
| 326 | sigset_t blocked; |
| 327 | |
| 328 | frame_addr = regs->gregs[15]; |
| 329 | trace_user_do_rt_sigreturn(regs, frame_addr); |
| 330 | if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { |
| 331 | goto badframe; |
| 332 | } |
| 333 | |
| 334 | target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask); |
| 335 | set_sigmask(&blocked); |
| 336 | |
| 337 | restore_sigcontext(regs, &frame->uc.tuc_mcontext); |
| 338 | target_restore_altstack(&frame->uc.tuc_stack, regs); |
| 339 | |
| 340 | unlock_user_struct(frame, frame_addr, 0); |
| 341 | return -QEMU_ESIGRETURN; |
| 342 | |
| 343 | badframe: |
| 344 | unlock_user_struct(frame, frame_addr, 0); |
| 345 | force_sig(TARGET_SIGSEGV); |
| 346 | return -QEMU_ESIGRETURN; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * "or r0,r0" nop used by the Linux kernel inline sigreturn trampolines to |
| 351 | * avoid a hardware bug (OR_R0_R0 in arch/sh/kernel/signal_32.c). Five of |
| 352 | * these nops follow TRAP_NOARG, placing the syscall number word 14 bytes |
| 353 | * past the MOVW(7) instruction (at MOVW(7)'s load offset). This yields the |
| 354 | * fixed 16-byte layout that libunwind's unw_is_signal_frame detects: |
| 355 | * [MOVW(7), TRAP_NOARG, 5x NOP_OR, .word syscall_nr] |
| 356 | */ |
| 357 | #define NOP_OR 0x200b |
| 358 | |
| 359 | void setup_sigtramp(abi_ulong sigtramp_page) |
| 360 | { |
| 361 | uint16_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 * 16, 0); |
| 362 | assert(tramp != NULL); |
| 363 | |
| 364 | /* sigreturn trampoline (non-RT) at offset 0 */ |
| 365 | default_sigreturn = sigtramp_page; |
| 366 | __put_user(MOVW(7), &tramp[0]); |
| 367 | __put_user(TRAP_NOARG, &tramp[1]); |
| 368 | __put_user(NOP_OR, &tramp[2]); |
| 369 | __put_user(NOP_OR, &tramp[3]); |
| 370 | __put_user(NOP_OR, &tramp[4]); |
| 371 | __put_user(NOP_OR, &tramp[5]); |
| 372 | __put_user(NOP_OR, &tramp[6]); |
| 373 | __put_user(TARGET_NR_sigreturn, &tramp[7]); |
| 374 | |
| 375 | /* rt_sigreturn trampoline at offset 16 */ |
| 376 | default_rt_sigreturn = sigtramp_page + 16; |
| 377 | __put_user(MOVW(7), &tramp[8]); |
| 378 | __put_user(TRAP_NOARG, &tramp[9]); |
| 379 | __put_user(NOP_OR, &tramp[10]); |
| 380 | __put_user(NOP_OR, &tramp[11]); |
| 381 | __put_user(NOP_OR, &tramp[12]); |
| 382 | __put_user(NOP_OR, &tramp[13]); |
| 383 | __put_user(NOP_OR, &tramp[14]); |
| 384 | __put_user(TARGET_NR_rt_sigreturn, &tramp[15]); |
| 385 | |
| 386 | unlock_user(tramp, sigtramp_page, 2 * 16); |
| 387 | } |