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 /* from the Linux kernel - /arch/x86/include/uapi/asm/sigcontext.h */ 26 27 struct target_fpreg { 28 uint16_t significand[4]; 29 uint16_t exponent; 30 }; 31 32 struct target_fpxreg { 33 uint16_t significand[4]; 34 uint16_t exponent; 35 uint16_t padding[3]; 36 }; 37 38 struct target_xmmreg { 39 uint32_t element[4]; 40 }; 41 42 struct target_fpstate_32 { 43 /* Regular FPU environment */ 44 uint32_t cw; 45 uint32_t sw; 46 uint32_t tag; 47 uint32_t ipoff; 48 uint32_t cssel; 49 uint32_t dataoff; 50 uint32_t datasel; 51 struct target_fpreg st[8]; 52 uint16_t status; 53 uint16_t magic; /* 0xffff = regular FPU data only */ 54 55 /* FXSR FPU environment */ 56 uint32_t _fxsr_env[6]; /* FXSR FPU env is ignored */ 57 uint32_t mxcsr; 58 uint32_t reserved; 59 struct target_fpxreg fxsr_st[8]; /* FXSR FPU reg data is ignored */ 60 struct target_xmmreg xmm[8]; 61 uint32_t padding[56]; 62 }; 63 64 struct target_fpstate_64 { 65 /* FXSAVE format */ 66 uint16_t cw; 67 uint16_t sw; 68 uint16_t twd; 69 uint16_t fop; 70 uint64_t rip; 71 uint64_t rdp; 72 uint32_t mxcsr; 73 uint32_t mxcsr_mask; 74 uint32_t st_space[32]; 75 uint32_t xmm_space[64]; 76 uint32_t reserved[24]; 77 }; 78 79 #ifndef TARGET_X86_64 80 # define target_fpstate target_fpstate_32 81 #else 82 # define target_fpstate target_fpstate_64 83 #endif 84 85 struct target_sigcontext_32 { 86 uint16_t gs, __gsh; 87 uint16_t fs, __fsh; 88 uint16_t es, __esh; 89 uint16_t ds, __dsh; 90 uint32_t edi; 91 uint32_t esi; 92 uint32_t ebp; 93 uint32_t esp; 94 uint32_t ebx; 95 uint32_t edx; 96 uint32_t ecx; 97 uint32_t eax; 98 uint32_t trapno; 99 uint32_t err; 100 uint32_t eip; 101 uint16_t cs, __csh; 102 uint32_t eflags; 103 uint32_t esp_at_signal; 104 uint16_t ss, __ssh; 105 uint32_t fpstate; /* pointer */ 106 uint32_t oldmask; 107 uint32_t cr2; 108 }; 109 110 struct target_sigcontext_64 { 111 uint64_t r8; 112 uint64_t r9; 113 uint64_t r10; 114 uint64_t r11; 115 uint64_t r12; 116 uint64_t r13; 117 uint64_t r14; 118 uint64_t r15; 119 120 uint64_t rdi; 121 uint64_t rsi; 122 uint64_t rbp; 123 uint64_t rbx; 124 uint64_t rdx; 125 uint64_t rax; 126 uint64_t rcx; 127 uint64_t rsp; 128 uint64_t rip; 129 130 uint64_t eflags; 131 132 uint16_t cs; 133 uint16_t gs; 134 uint16_t fs; 135 uint16_t ss; 136 137 uint64_t err; 138 uint64_t trapno; 139 uint64_t oldmask; 140 uint64_t cr2; 141 142 uint64_t fpstate; /* pointer */ 143 uint64_t padding[8]; 144 }; 145 146 #ifndef TARGET_X86_64 147 # define target_sigcontext target_sigcontext_32 148 #else 149 # define target_sigcontext target_sigcontext_64 150 #endif 151 152 /* see Linux/include/uapi/asm-generic/ucontext.h */ 153 struct target_ucontext { 154 abi_ulong tuc_flags; 155 abi_ulong tuc_link; 156 target_stack_t tuc_stack; 157 struct target_sigcontext tuc_mcontext; 158 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 159 }; 160 161 #ifndef TARGET_X86_64 162 struct sigframe { 163 abi_ulong pretcode; 164 int sig; 165 struct target_sigcontext sc; 166 struct target_fpstate fpstate; 167 abi_ulong extramask[TARGET_NSIG_WORDS-1]; 168 char retcode[8]; 169 }; 170 171 struct rt_sigframe { 172 abi_ulong pretcode; 173 int sig; 174 abi_ulong pinfo; 175 abi_ulong puc; 176 struct target_siginfo info; 177 struct target_ucontext uc; 178 struct target_fpstate fpstate; 179 char retcode[8]; 180 }; 181 182 #else 183 184 struct rt_sigframe { 185 abi_ulong pretcode; 186 struct target_ucontext uc; 187 struct target_siginfo info; 188 struct target_fpstate fpstate; 189 }; 190 191 #endif 192 193 /* 194 * Set up a signal frame. 195 */ 196 197 /* XXX: save x87 state */ 198 static void setup_sigcontext(struct target_sigcontext *sc, 199 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask, 200 abi_ulong fpstate_addr) 201 { 202 CPUState *cs = env_cpu(env); 203 #ifndef TARGET_X86_64 204 uint16_t magic; 205 206 /* already locked in setup_frame() */ 207 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs); 208 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs); 209 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es); 210 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds); 211 __put_user(env->regs[R_EDI], &sc->edi); 212 __put_user(env->regs[R_ESI], &sc->esi); 213 __put_user(env->regs[R_EBP], &sc->ebp); 214 __put_user(env->regs[R_ESP], &sc->esp); 215 __put_user(env->regs[R_EBX], &sc->ebx); 216 __put_user(env->regs[R_EDX], &sc->edx); 217 __put_user(env->regs[R_ECX], &sc->ecx); 218 __put_user(env->regs[R_EAX], &sc->eax); 219 __put_user(cs->exception_index, &sc->trapno); 220 __put_user(env->error_code, &sc->err); 221 __put_user(env->eip, &sc->eip); 222 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs); 223 __put_user(env->eflags, &sc->eflags); 224 __put_user(env->regs[R_ESP], &sc->esp_at_signal); 225 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss); 226 227 cpu_x86_fsave(env, fpstate_addr, 1); 228 fpstate->status = fpstate->sw; 229 magic = 0xffff; 230 __put_user(magic, &fpstate->magic); 231 __put_user(fpstate_addr, &sc->fpstate); 232 233 /* non-iBCS2 extensions.. */ 234 __put_user(mask, &sc->oldmask); 235 __put_user(env->cr[2], &sc->cr2); 236 #else 237 __put_user(env->regs[R_EDI], &sc->rdi); 238 __put_user(env->regs[R_ESI], &sc->rsi); 239 __put_user(env->regs[R_EBP], &sc->rbp); 240 __put_user(env->regs[R_ESP], &sc->rsp); 241 __put_user(env->regs[R_EBX], &sc->rbx); 242 __put_user(env->regs[R_EDX], &sc->rdx); 243 __put_user(env->regs[R_ECX], &sc->rcx); 244 __put_user(env->regs[R_EAX], &sc->rax); 245 246 __put_user(env->regs[8], &sc->r8); 247 __put_user(env->regs[9], &sc->r9); 248 __put_user(env->regs[10], &sc->r10); 249 __put_user(env->regs[11], &sc->r11); 250 __put_user(env->regs[12], &sc->r12); 251 __put_user(env->regs[13], &sc->r13); 252 __put_user(env->regs[14], &sc->r14); 253 __put_user(env->regs[15], &sc->r15); 254 255 __put_user(cs->exception_index, &sc->trapno); 256 __put_user(env->error_code, &sc->err); 257 __put_user(env->eip, &sc->rip); 258 259 __put_user(env->eflags, &sc->eflags); 260 __put_user(env->segs[R_CS].selector, &sc->cs); 261 __put_user((uint16_t)0, &sc->gs); 262 __put_user((uint16_t)0, &sc->fs); 263 __put_user(env->segs[R_SS].selector, &sc->ss); 264 265 __put_user(mask, &sc->oldmask); 266 __put_user(env->cr[2], &sc->cr2); 267 268 /* fpstate_addr must be 16 byte aligned for fxsave */ 269 assert(!(fpstate_addr & 0xf)); 270 271 cpu_x86_fxsave(env, fpstate_addr); 272 __put_user(fpstate_addr, &sc->fpstate); 273 #endif 274 } 275 276 /* 277 * Determine which stack to use.. 278 */ 279 280 static inline abi_ulong 281 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size) 282 { 283 unsigned long esp; 284 285 /* Default to using normal stack */ 286 esp = get_sp_from_cpustate(env); 287 #ifdef TARGET_X86_64 288 esp -= 128; /* this is the redzone */ 289 #endif 290 291 /* This is the X/Open sanctioned signal stack switching. */ 292 if (ka->sa_flags & TARGET_SA_ONSTACK) { 293 esp = target_sigsp(esp, ka); 294 } else { 295 #ifndef TARGET_X86_64 296 /* This is the legacy signal stack switching. */ 297 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS && 298 !(ka->sa_flags & TARGET_SA_RESTORER) && 299 ka->sa_restorer) { 300 esp = (unsigned long) ka->sa_restorer; 301 } 302 #endif 303 } 304 305 #ifndef TARGET_X86_64 306 return (esp - frame_size) & -8ul; 307 #else 308 return ((esp - frame_size) & (~15ul)) - 8; 309 #endif 310 } 311 312 #ifndef TARGET_X86_64 313 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */ 314 void setup_frame(int sig, struct target_sigaction *ka, 315 target_sigset_t *set, CPUX86State *env) 316 { 317 abi_ulong frame_addr; 318 struct sigframe *frame; 319 int i; 320 321 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 322 trace_user_setup_frame(env, frame_addr); 323 324 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 325 goto give_sigsegv; 326 327 __put_user(sig, &frame->sig); 328 329 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0], 330 frame_addr + offsetof(struct sigframe, fpstate)); 331 332 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 333 __put_user(set->sig[i], &frame->extramask[i - 1]); 334 } 335 336 /* Set up to return from userspace. If provided, use a stub 337 already in userspace. */ 338 if (ka->sa_flags & TARGET_SA_RESTORER) { 339 __put_user(ka->sa_restorer, &frame->pretcode); 340 } else { 341 uint16_t val16; 342 abi_ulong retcode_addr; 343 retcode_addr = frame_addr + offsetof(struct sigframe, retcode); 344 __put_user(retcode_addr, &frame->pretcode); 345 /* This is popl %eax ; movl $,%eax ; int $0x80 */ 346 val16 = 0xb858; 347 __put_user(val16, (uint16_t *)(frame->retcode+0)); 348 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2)); 349 val16 = 0x80cd; 350 __put_user(val16, (uint16_t *)(frame->retcode+6)); 351 } 352 353 /* Set up registers for signal handler */ 354 env->regs[R_ESP] = frame_addr; 355 env->eip = ka->_sa_handler; 356 357 cpu_x86_load_seg(env, R_DS, __USER_DS); 358 cpu_x86_load_seg(env, R_ES, __USER_DS); 359 cpu_x86_load_seg(env, R_SS, __USER_DS); 360 cpu_x86_load_seg(env, R_CS, __USER_CS); 361 env->eflags &= ~TF_MASK; 362 363 unlock_user_struct(frame, frame_addr, 1); 364 365 return; 366 367 give_sigsegv: 368 force_sigsegv(sig); 369 } 370 #endif 371 372 /* compare linux/arch/x86/kernel/signal.c:setup_rt_frame() */ 373 void setup_rt_frame(int sig, struct target_sigaction *ka, 374 target_siginfo_t *info, 375 target_sigset_t *set, CPUX86State *env) 376 { 377 abi_ulong frame_addr; 378 #ifndef TARGET_X86_64 379 abi_ulong addr; 380 #endif 381 struct rt_sigframe *frame; 382 int i; 383 384 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 385 trace_user_setup_rt_frame(env, frame_addr); 386 387 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 388 goto give_sigsegv; 389 390 /* These fields are only in rt_sigframe on 32 bit */ 391 #ifndef TARGET_X86_64 392 __put_user(sig, &frame->sig); 393 addr = frame_addr + offsetof(struct rt_sigframe, info); 394 __put_user(addr, &frame->pinfo); 395 addr = frame_addr + offsetof(struct rt_sigframe, uc); 396 __put_user(addr, &frame->puc); 397 #endif 398 if (ka->sa_flags & TARGET_SA_SIGINFO) { 399 tswap_siginfo(&frame->info, info); 400 } 401 402 /* Create the ucontext. */ 403 __put_user(0, &frame->uc.tuc_flags); 404 __put_user(0, &frame->uc.tuc_link); 405 target_save_altstack(&frame->uc.tuc_stack, env); 406 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env, 407 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate)); 408 409 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 410 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]); 411 } 412 413 /* Set up to return from userspace. If provided, use a stub 414 already in userspace. */ 415 #ifndef TARGET_X86_64 416 if (ka->sa_flags & TARGET_SA_RESTORER) { 417 __put_user(ka->sa_restorer, &frame->pretcode); 418 } else { 419 uint16_t val16; 420 addr = frame_addr + offsetof(struct rt_sigframe, retcode); 421 __put_user(addr, &frame->pretcode); 422 /* This is movl $,%eax ; int $0x80 */ 423 __put_user(0xb8, (char *)(frame->retcode+0)); 424 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1)); 425 val16 = 0x80cd; 426 __put_user(val16, (uint16_t *)(frame->retcode+5)); 427 } 428 #else 429 /* XXX: Would be slightly better to return -EFAULT here if test fails 430 assert(ka->sa_flags & TARGET_SA_RESTORER); */ 431 __put_user(ka->sa_restorer, &frame->pretcode); 432 #endif 433 434 /* Set up registers for signal handler */ 435 env->regs[R_ESP] = frame_addr; 436 env->eip = ka->_sa_handler; 437 438 #ifndef TARGET_X86_64 439 env->regs[R_EAX] = sig; 440 env->regs[R_EDX] = frame_addr + offsetof(struct rt_sigframe, info); 441 env->regs[R_ECX] = frame_addr + offsetof(struct rt_sigframe, uc); 442 #else 443 env->regs[R_EAX] = 0; 444 env->regs[R_EDI] = sig; 445 env->regs[R_ESI] = frame_addr + offsetof(struct rt_sigframe, info); 446 env->regs[R_EDX] = frame_addr + offsetof(struct rt_sigframe, uc); 447 #endif 448 449 cpu_x86_load_seg(env, R_DS, __USER_DS); 450 cpu_x86_load_seg(env, R_ES, __USER_DS); 451 cpu_x86_load_seg(env, R_CS, __USER_CS); 452 cpu_x86_load_seg(env, R_SS, __USER_DS); 453 env->eflags &= ~TF_MASK; 454 455 unlock_user_struct(frame, frame_addr, 1); 456 457 return; 458 459 give_sigsegv: 460 force_sigsegv(sig); 461 } 462 463 static int 464 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc) 465 { 466 unsigned int err = 0; 467 abi_ulong fpstate_addr; 468 unsigned int tmpflags; 469 470 #ifndef TARGET_X86_64 471 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs)); 472 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs)); 473 cpu_x86_load_seg(env, R_ES, tswap16(sc->es)); 474 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds)); 475 476 env->regs[R_EDI] = tswapl(sc->edi); 477 env->regs[R_ESI] = tswapl(sc->esi); 478 env->regs[R_EBP] = tswapl(sc->ebp); 479 env->regs[R_ESP] = tswapl(sc->esp); 480 env->regs[R_EBX] = tswapl(sc->ebx); 481 env->regs[R_EDX] = tswapl(sc->edx); 482 env->regs[R_ECX] = tswapl(sc->ecx); 483 env->regs[R_EAX] = tswapl(sc->eax); 484 485 env->eip = tswapl(sc->eip); 486 #else 487 env->regs[8] = tswapl(sc->r8); 488 env->regs[9] = tswapl(sc->r9); 489 env->regs[10] = tswapl(sc->r10); 490 env->regs[11] = tswapl(sc->r11); 491 env->regs[12] = tswapl(sc->r12); 492 env->regs[13] = tswapl(sc->r13); 493 env->regs[14] = tswapl(sc->r14); 494 env->regs[15] = tswapl(sc->r15); 495 496 env->regs[R_EDI] = tswapl(sc->rdi); 497 env->regs[R_ESI] = tswapl(sc->rsi); 498 env->regs[R_EBP] = tswapl(sc->rbp); 499 env->regs[R_EBX] = tswapl(sc->rbx); 500 env->regs[R_EDX] = tswapl(sc->rdx); 501 env->regs[R_EAX] = tswapl(sc->rax); 502 env->regs[R_ECX] = tswapl(sc->rcx); 503 env->regs[R_ESP] = tswapl(sc->rsp); 504 505 env->eip = tswapl(sc->rip); 506 #endif 507 508 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3); 509 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3); 510 511 tmpflags = tswapl(sc->eflags); 512 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); 513 // regs->orig_eax = -1; /* disable syscall checks */ 514 515 fpstate_addr = tswapl(sc->fpstate); 516 if (fpstate_addr != 0) { 517 if (!access_ok(env_cpu(env), VERIFY_READ, fpstate_addr, 518 sizeof(struct target_fpstate))) { 519 goto badframe; 520 } 521 #ifndef TARGET_X86_64 522 cpu_x86_frstor(env, fpstate_addr, 1); 523 #else 524 cpu_x86_fxrstor(env, fpstate_addr); 525 #endif 526 } 527 528 return err; 529 badframe: 530 return 1; 531 } 532 533 /* Note: there is no sigreturn on x86_64, there is only rt_sigreturn */ 534 #ifndef TARGET_X86_64 535 long do_sigreturn(CPUX86State *env) 536 { 537 struct sigframe *frame; 538 abi_ulong frame_addr = env->regs[R_ESP] - 8; 539 target_sigset_t target_set; 540 sigset_t set; 541 int i; 542 543 trace_user_do_sigreturn(env, frame_addr); 544 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 545 goto badframe; 546 /* set blocked signals */ 547 __get_user(target_set.sig[0], &frame->sc.oldmask); 548 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 549 __get_user(target_set.sig[i], &frame->extramask[i - 1]); 550 } 551 552 target_to_host_sigset_internal(&set, &target_set); 553 set_sigmask(&set); 554 555 /* restore registers */ 556 if (restore_sigcontext(env, &frame->sc)) 557 goto badframe; 558 unlock_user_struct(frame, frame_addr, 0); 559 return -TARGET_QEMU_ESIGRETURN; 560 561 badframe: 562 unlock_user_struct(frame, frame_addr, 0); 563 force_sig(TARGET_SIGSEGV); 564 return -TARGET_QEMU_ESIGRETURN; 565 } 566 #endif 567 568 long do_rt_sigreturn(CPUX86State *env) 569 { 570 abi_ulong frame_addr; 571 struct rt_sigframe *frame; 572 sigset_t set; 573 574 frame_addr = env->regs[R_ESP] - sizeof(abi_ulong); 575 trace_user_do_rt_sigreturn(env, frame_addr); 576 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 577 goto badframe; 578 target_to_host_sigset(&set, &frame->uc.tuc_sigmask); 579 set_sigmask(&set); 580 581 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) { 582 goto badframe; 583 } 584 585 target_restore_altstack(&frame->uc.tuc_stack, env); 586 587 unlock_user_struct(frame, frame_addr, 0); 588 return -TARGET_QEMU_ESIGRETURN; 589 590 badframe: 591 unlock_user_struct(frame, frame_addr, 0); 592 force_sig(TARGET_SIGSEGV); 593 return -TARGET_QEMU_ESIGRETURN; 594 } 595