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, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 #include <stdlib.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include <stdarg.h> 24 #include <unistd.h> 25 #include <signal.h> 26 #include <errno.h> 27 #include <sys/ucontext.h> 28 29 #ifdef __ia64__ 30 #undef uc_mcontext 31 #undef uc_sigmask 32 #undef uc_stack 33 #undef uc_link 34 #endif 35 36 #include "qemu.h" 37 38 //#define DEBUG_SIGNAL 39 40 #define MAX_SIGQUEUE_SIZE 1024 41 42 struct sigqueue { 43 struct sigqueue *next; 44 target_siginfo_t info; 45 }; 46 47 struct emulated_sigaction { 48 struct target_sigaction sa; 49 int pending; /* true if signal is pending */ 50 struct sigqueue *first; 51 struct sigqueue info; /* in order to always have memory for the 52 first signal, we put it here */ 53 }; 54 55 static struct emulated_sigaction sigact_table[TARGET_NSIG]; 56 static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */ 57 static struct sigqueue *first_free; /* first free siginfo queue entry */ 58 static int signal_pending; /* non zero if a signal may be pending */ 59 60 static void host_signal_handler(int host_signum, siginfo_t *info, 61 void *puc); 62 63 static uint8_t host_to_target_signal_table[65] = { 64 [SIGHUP] = TARGET_SIGHUP, 65 [SIGINT] = TARGET_SIGINT, 66 [SIGQUIT] = TARGET_SIGQUIT, 67 [SIGILL] = TARGET_SIGILL, 68 [SIGTRAP] = TARGET_SIGTRAP, 69 [SIGABRT] = TARGET_SIGABRT, 70 /* [SIGIOT] = TARGET_SIGIOT,*/ 71 [SIGBUS] = TARGET_SIGBUS, 72 [SIGFPE] = TARGET_SIGFPE, 73 [SIGKILL] = TARGET_SIGKILL, 74 [SIGUSR1] = TARGET_SIGUSR1, 75 [SIGSEGV] = TARGET_SIGSEGV, 76 [SIGUSR2] = TARGET_SIGUSR2, 77 [SIGPIPE] = TARGET_SIGPIPE, 78 [SIGALRM] = TARGET_SIGALRM, 79 [SIGTERM] = TARGET_SIGTERM, 80 #ifdef SIGSTKFLT 81 [SIGSTKFLT] = TARGET_SIGSTKFLT, 82 #endif 83 [SIGCHLD] = TARGET_SIGCHLD, 84 [SIGCONT] = TARGET_SIGCONT, 85 [SIGSTOP] = TARGET_SIGSTOP, 86 [SIGTSTP] = TARGET_SIGTSTP, 87 [SIGTTIN] = TARGET_SIGTTIN, 88 [SIGTTOU] = TARGET_SIGTTOU, 89 [SIGURG] = TARGET_SIGURG, 90 [SIGXCPU] = TARGET_SIGXCPU, 91 [SIGXFSZ] = TARGET_SIGXFSZ, 92 [SIGVTALRM] = TARGET_SIGVTALRM, 93 [SIGPROF] = TARGET_SIGPROF, 94 [SIGWINCH] = TARGET_SIGWINCH, 95 [SIGIO] = TARGET_SIGIO, 96 [SIGPWR] = TARGET_SIGPWR, 97 [SIGSYS] = TARGET_SIGSYS, 98 /* next signals stay the same */ 99 }; 100 static uint8_t target_to_host_signal_table[65]; 101 102 static inline int host_to_target_signal(int sig) 103 { 104 return host_to_target_signal_table[sig]; 105 } 106 107 static inline int target_to_host_signal(int sig) 108 { 109 return target_to_host_signal_table[sig]; 110 } 111 112 static void host_to_target_sigset_internal(target_sigset_t *d, 113 const sigset_t *s) 114 { 115 int i; 116 unsigned long sigmask; 117 uint32_t target_sigmask; 118 119 sigmask = ((unsigned long *)s)[0]; 120 target_sigmask = 0; 121 for(i = 0; i < 32; i++) { 122 if (sigmask & (1 << i)) 123 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1); 124 } 125 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32 126 d->sig[0] = target_sigmask; 127 for(i = 1;i < TARGET_NSIG_WORDS; i++) { 128 d->sig[i] = ((unsigned long *)s)[i]; 129 } 130 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2 131 d->sig[0] = target_sigmask; 132 d->sig[1] = sigmask >> 32; 133 #else 134 #error host_to_target_sigset 135 #endif 136 } 137 138 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) 139 { 140 target_sigset_t d1; 141 int i; 142 143 host_to_target_sigset_internal(&d1, s); 144 for(i = 0;i < TARGET_NSIG_WORDS; i++) 145 __put_user(d1.sig[i], &d->sig[i]); 146 } 147 148 void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s) 149 { 150 int i; 151 unsigned long sigmask; 152 target_ulong target_sigmask; 153 154 target_sigmask = s->sig[0]; 155 sigmask = 0; 156 for(i = 0; i < 32; i++) { 157 if (target_sigmask & (1 << i)) 158 sigmask |= 1 << (target_to_host_signal(i + 1) - 1); 159 } 160 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32 161 ((unsigned long *)d)[0] = sigmask; 162 for(i = 1;i < TARGET_NSIG_WORDS; i++) { 163 ((unsigned long *)d)[i] = s->sig[i]; 164 } 165 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2 166 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32); 167 #else 168 #error target_to_host_sigset 169 #endif /* TARGET_LONG_BITS */ 170 } 171 172 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) 173 { 174 target_sigset_t s1; 175 int i; 176 177 for(i = 0;i < TARGET_NSIG_WORDS; i++) 178 __get_user(s1.sig[i], &s->sig[i]); 179 target_to_host_sigset_internal(d, &s1); 180 } 181 182 void host_to_target_old_sigset(target_ulong *old_sigset, 183 const sigset_t *sigset) 184 { 185 target_sigset_t d; 186 host_to_target_sigset(&d, sigset); 187 *old_sigset = d.sig[0]; 188 } 189 190 void target_to_host_old_sigset(sigset_t *sigset, 191 const target_ulong *old_sigset) 192 { 193 target_sigset_t d; 194 int i; 195 196 d.sig[0] = *old_sigset; 197 for(i = 1;i < TARGET_NSIG_WORDS; i++) 198 d.sig[i] = 0; 199 target_to_host_sigset(sigset, &d); 200 } 201 202 /* siginfo conversion */ 203 204 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 205 const siginfo_t *info) 206 { 207 int sig; 208 sig = host_to_target_signal(info->si_signo); 209 tinfo->si_signo = sig; 210 tinfo->si_errno = 0; 211 tinfo->si_code = 0; 212 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || 213 sig == SIGBUS || sig == SIGTRAP) { 214 /* should never come here, but who knows. The information for 215 the target is irrelevant */ 216 tinfo->_sifields._sigfault._addr = 0; 217 } else if (sig >= TARGET_SIGRTMIN) { 218 tinfo->_sifields._rt._pid = info->si_pid; 219 tinfo->_sifields._rt._uid = info->si_uid; 220 /* XXX: potential problem if 64 bit */ 221 tinfo->_sifields._rt._sigval.sival_ptr = 222 (target_ulong)info->si_value.sival_ptr; 223 } 224 } 225 226 static void tswap_siginfo(target_siginfo_t *tinfo, 227 const target_siginfo_t *info) 228 { 229 int sig; 230 sig = info->si_signo; 231 tinfo->si_signo = tswap32(sig); 232 tinfo->si_errno = tswap32(info->si_errno); 233 tinfo->si_code = tswap32(info->si_code); 234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV || 235 sig == SIGBUS || sig == SIGTRAP) { 236 tinfo->_sifields._sigfault._addr = 237 tswapl(info->_sifields._sigfault._addr); 238 } else if (sig >= TARGET_SIGRTMIN) { 239 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid); 240 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid); 241 tinfo->_sifields._rt._sigval.sival_ptr = 242 tswapl(info->_sifields._rt._sigval.sival_ptr); 243 } 244 } 245 246 247 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) 248 { 249 host_to_target_siginfo_noswap(tinfo, info); 250 tswap_siginfo(tinfo, tinfo); 251 } 252 253 /* XXX: we support only POSIX RT signals are used. */ 254 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */ 255 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) 256 { 257 info->si_signo = tswap32(tinfo->si_signo); 258 info->si_errno = tswap32(tinfo->si_errno); 259 info->si_code = tswap32(tinfo->si_code); 260 info->si_pid = tswap32(tinfo->_sifields._rt._pid); 261 info->si_uid = tswap32(tinfo->_sifields._rt._uid); 262 info->si_value.sival_ptr = 263 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr); 264 } 265 266 void signal_init(void) 267 { 268 struct sigaction act; 269 int i, j; 270 271 /* generate signal conversion tables */ 272 for(i = 1; i <= 64; i++) { 273 if (host_to_target_signal_table[i] == 0) 274 host_to_target_signal_table[i] = i; 275 } 276 for(i = 1; i <= 64; i++) { 277 j = host_to_target_signal_table[i]; 278 target_to_host_signal_table[j] = i; 279 } 280 281 /* set all host signal handlers. ALL signals are blocked during 282 the handlers to serialize them. */ 283 sigfillset(&act.sa_mask); 284 act.sa_flags = SA_SIGINFO; 285 act.sa_sigaction = host_signal_handler; 286 for(i = 1; i < NSIG; i++) { 287 sigaction(i, &act, NULL); 288 } 289 290 memset(sigact_table, 0, sizeof(sigact_table)); 291 292 first_free = &sigqueue_table[0]; 293 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) 294 sigqueue_table[i].next = &sigqueue_table[i + 1]; 295 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL; 296 } 297 298 /* signal queue handling */ 299 300 static inline struct sigqueue *alloc_sigqueue(void) 301 { 302 struct sigqueue *q = first_free; 303 if (!q) 304 return NULL; 305 first_free = q->next; 306 return q; 307 } 308 309 static inline void free_sigqueue(struct sigqueue *q) 310 { 311 q->next = first_free; 312 first_free = q; 313 } 314 315 /* abort execution with signal */ 316 void __attribute((noreturn)) force_sig(int sig) 317 { 318 int host_sig; 319 host_sig = target_to_host_signal(sig); 320 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n", 321 sig, strsignal(host_sig)); 322 #if 1 323 _exit(-host_sig); 324 #else 325 { 326 struct sigaction act; 327 sigemptyset(&act.sa_mask); 328 act.sa_flags = SA_SIGINFO; 329 act.sa_sigaction = SIG_DFL; 330 sigaction(SIGABRT, &act, NULL); 331 abort(); 332 } 333 #endif 334 } 335 336 /* queue a signal so that it will be send to the virtual CPU as soon 337 as possible */ 338 int queue_signal(int sig, target_siginfo_t *info) 339 { 340 struct emulated_sigaction *k; 341 struct sigqueue *q, **pq; 342 target_ulong handler; 343 344 #if defined(DEBUG_SIGNAL) 345 fprintf(stderr, "queue_signal: sig=%d\n", 346 sig); 347 #endif 348 k = &sigact_table[sig - 1]; 349 handler = k->sa._sa_handler; 350 if (handler == TARGET_SIG_DFL) { 351 /* default handler : ignore some signal. The other are fatal */ 352 if (sig != TARGET_SIGCHLD && 353 sig != TARGET_SIGURG && 354 sig != TARGET_SIGWINCH) { 355 force_sig(sig); 356 } else { 357 return 0; /* indicate ignored */ 358 } 359 } else if (handler == TARGET_SIG_IGN) { 360 /* ignore signal */ 361 return 0; 362 } else if (handler == TARGET_SIG_ERR) { 363 force_sig(sig); 364 } else { 365 pq = &k->first; 366 if (sig < TARGET_SIGRTMIN) { 367 /* if non real time signal, we queue exactly one signal */ 368 if (!k->pending) 369 q = &k->info; 370 else 371 return 0; 372 } else { 373 if (!k->pending) { 374 /* first signal */ 375 q = &k->info; 376 } else { 377 q = alloc_sigqueue(); 378 if (!q) 379 return -EAGAIN; 380 while (*pq != NULL) 381 pq = &(*pq)->next; 382 } 383 } 384 *pq = q; 385 q->info = *info; 386 q->next = NULL; 387 k->pending = 1; 388 /* signal that a new signal is pending */ 389 signal_pending = 1; 390 return 1; /* indicates that the signal was queued */ 391 } 392 } 393 394 static void host_signal_handler(int host_signum, siginfo_t *info, 395 void *puc) 396 { 397 int sig; 398 target_siginfo_t tinfo; 399 400 /* the CPU emulator uses some host signals to detect exceptions, 401 we we forward to it some signals */ 402 if (host_signum == SIGSEGV || host_signum == SIGBUS 403 #if defined(TARGET_I386) && defined(USE_CODE_COPY) 404 || host_signum == SIGFPE 405 #endif 406 ) { 407 if (cpu_signal_handler(host_signum, info, puc)) 408 return; 409 } 410 411 /* get target signal number */ 412 sig = host_to_target_signal(host_signum); 413 if (sig < 1 || sig > TARGET_NSIG) 414 return; 415 #if defined(DEBUG_SIGNAL) 416 fprintf(stderr, "qemu: got signal %d\n", sig); 417 #endif 418 host_to_target_siginfo_noswap(&tinfo, info); 419 if (queue_signal(sig, &tinfo) == 1) { 420 /* interrupt the virtual CPU as soon as possible */ 421 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); 422 } 423 } 424 425 int do_sigaction(int sig, const struct target_sigaction *act, 426 struct target_sigaction *oact) 427 { 428 struct emulated_sigaction *k; 429 struct sigaction act1; 430 int host_sig; 431 432 if (sig < 1 || sig > TARGET_NSIG) 433 return -EINVAL; 434 k = &sigact_table[sig - 1]; 435 #if defined(DEBUG_SIGNAL) 436 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n", 437 sig, (int)act, (int)oact); 438 #endif 439 if (oact) { 440 oact->_sa_handler = tswapl(k->sa._sa_handler); 441 oact->sa_flags = tswapl(k->sa.sa_flags); 442 oact->sa_restorer = tswapl(k->sa.sa_restorer); 443 oact->sa_mask = k->sa.sa_mask; 444 } 445 if (act) { 446 k->sa._sa_handler = tswapl(act->_sa_handler); 447 k->sa.sa_flags = tswapl(act->sa_flags); 448 k->sa.sa_restorer = tswapl(act->sa_restorer); 449 k->sa.sa_mask = act->sa_mask; 450 451 /* we update the host linux signal state */ 452 host_sig = target_to_host_signal(sig); 453 if (host_sig != SIGSEGV && host_sig != SIGBUS) { 454 sigfillset(&act1.sa_mask); 455 act1.sa_flags = SA_SIGINFO; 456 if (k->sa.sa_flags & TARGET_SA_RESTART) 457 act1.sa_flags |= SA_RESTART; 458 /* NOTE: it is important to update the host kernel signal 459 ignore state to avoid getting unexpected interrupted 460 syscalls */ 461 if (k->sa._sa_handler == TARGET_SIG_IGN) { 462 act1.sa_sigaction = (void *)SIG_IGN; 463 } else if (k->sa._sa_handler == TARGET_SIG_DFL) { 464 act1.sa_sigaction = (void *)SIG_DFL; 465 } else { 466 act1.sa_sigaction = host_signal_handler; 467 } 468 sigaction(host_sig, &act1, NULL); 469 } 470 } 471 return 0; 472 } 473 474 #ifndef offsetof 475 #define offsetof(type, field) ((size_t) &((type *)0)->field) 476 #endif 477 478 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo, 479 const target_siginfo_t *info) 480 { 481 tswap_siginfo(tinfo, info); 482 return 0; 483 } 484 485 #ifdef TARGET_I386 486 487 /* from the Linux kernel */ 488 489 struct target_fpreg { 490 uint16_t significand[4]; 491 uint16_t exponent; 492 }; 493 494 struct target_fpxreg { 495 uint16_t significand[4]; 496 uint16_t exponent; 497 uint16_t padding[3]; 498 }; 499 500 struct target_xmmreg { 501 target_ulong element[4]; 502 }; 503 504 struct target_fpstate { 505 /* Regular FPU environment */ 506 target_ulong cw; 507 target_ulong sw; 508 target_ulong tag; 509 target_ulong ipoff; 510 target_ulong cssel; 511 target_ulong dataoff; 512 target_ulong datasel; 513 struct target_fpreg _st[8]; 514 uint16_t status; 515 uint16_t magic; /* 0xffff = regular FPU data only */ 516 517 /* FXSR FPU environment */ 518 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */ 519 target_ulong mxcsr; 520 target_ulong reserved; 521 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */ 522 struct target_xmmreg _xmm[8]; 523 target_ulong padding[56]; 524 }; 525 526 #define X86_FXSR_MAGIC 0x0000 527 528 struct target_sigcontext { 529 uint16_t gs, __gsh; 530 uint16_t fs, __fsh; 531 uint16_t es, __esh; 532 uint16_t ds, __dsh; 533 target_ulong edi; 534 target_ulong esi; 535 target_ulong ebp; 536 target_ulong esp; 537 target_ulong ebx; 538 target_ulong edx; 539 target_ulong ecx; 540 target_ulong eax; 541 target_ulong trapno; 542 target_ulong err; 543 target_ulong eip; 544 uint16_t cs, __csh; 545 target_ulong eflags; 546 target_ulong esp_at_signal; 547 uint16_t ss, __ssh; 548 target_ulong fpstate; /* pointer */ 549 target_ulong oldmask; 550 target_ulong cr2; 551 }; 552 553 typedef struct target_sigaltstack { 554 target_ulong ss_sp; 555 int ss_flags; 556 target_ulong ss_size; 557 } target_stack_t; 558 559 struct target_ucontext { 560 target_ulong uc_flags; 561 target_ulong uc_link; 562 target_stack_t uc_stack; 563 struct target_sigcontext uc_mcontext; 564 target_sigset_t uc_sigmask; /* mask last for extensibility */ 565 }; 566 567 struct sigframe 568 { 569 target_ulong pretcode; 570 int sig; 571 struct target_sigcontext sc; 572 struct target_fpstate fpstate; 573 target_ulong extramask[TARGET_NSIG_WORDS-1]; 574 char retcode[8]; 575 }; 576 577 struct rt_sigframe 578 { 579 target_ulong pretcode; 580 int sig; 581 target_ulong pinfo; 582 target_ulong puc; 583 struct target_siginfo info; 584 struct target_ucontext uc; 585 struct target_fpstate fpstate; 586 char retcode[8]; 587 }; 588 589 /* 590 * Set up a signal frame. 591 */ 592 593 /* XXX: save x87 state */ 594 static int 595 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate, 596 CPUX86State *env, unsigned long mask) 597 { 598 int err = 0; 599 600 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs); 601 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs); 602 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es); 603 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds); 604 err |= __put_user(env->regs[R_EDI], &sc->edi); 605 err |= __put_user(env->regs[R_ESI], &sc->esi); 606 err |= __put_user(env->regs[R_EBP], &sc->ebp); 607 err |= __put_user(env->regs[R_ESP], &sc->esp); 608 err |= __put_user(env->regs[R_EBX], &sc->ebx); 609 err |= __put_user(env->regs[R_EDX], &sc->edx); 610 err |= __put_user(env->regs[R_ECX], &sc->ecx); 611 err |= __put_user(env->regs[R_EAX], &sc->eax); 612 err |= __put_user(env->exception_index, &sc->trapno); 613 err |= __put_user(env->error_code, &sc->err); 614 err |= __put_user(env->eip, &sc->eip); 615 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs); 616 err |= __put_user(env->eflags, &sc->eflags); 617 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal); 618 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss); 619 620 cpu_x86_fsave(env, (void *)fpstate, 1); 621 fpstate->status = fpstate->sw; 622 err |= __put_user(0xffff, &fpstate->magic); 623 err |= __put_user(fpstate, &sc->fpstate); 624 625 /* non-iBCS2 extensions.. */ 626 err |= __put_user(mask, &sc->oldmask); 627 err |= __put_user(env->cr[2], &sc->cr2); 628 return err; 629 } 630 631 /* 632 * Determine which stack to use.. 633 */ 634 635 static inline void * 636 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size) 637 { 638 unsigned long esp; 639 640 /* Default to using normal stack */ 641 esp = env->regs[R_ESP]; 642 #if 0 643 /* This is the X/Open sanctioned signal stack switching. */ 644 if (ka->sa.sa_flags & SA_ONSTACK) { 645 if (sas_ss_flags(esp) == 0) 646 esp = current->sas_ss_sp + current->sas_ss_size; 647 } 648 649 /* This is the legacy signal stack switching. */ 650 else 651 #endif 652 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS && 653 !(ka->sa.sa_flags & TARGET_SA_RESTORER) && 654 ka->sa.sa_restorer) { 655 esp = (unsigned long) ka->sa.sa_restorer; 656 } 657 return (void *)((esp - frame_size) & -8ul); 658 } 659 660 static void setup_frame(int sig, struct emulated_sigaction *ka, 661 target_sigset_t *set, CPUX86State *env) 662 { 663 struct sigframe *frame; 664 int i, err = 0; 665 666 frame = get_sigframe(ka, env, sizeof(*frame)); 667 668 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 669 goto give_sigsegv; 670 err |= __put_user((/*current->exec_domain 671 && current->exec_domain->signal_invmap 672 && sig < 32 673 ? current->exec_domain->signal_invmap[sig] 674 : */ sig), 675 &frame->sig); 676 if (err) 677 goto give_sigsegv; 678 679 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]); 680 if (err) 681 goto give_sigsegv; 682 683 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 684 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 685 goto give_sigsegv; 686 } 687 688 /* Set up to return from userspace. If provided, use a stub 689 already in userspace. */ 690 if (ka->sa.sa_flags & TARGET_SA_RESTORER) { 691 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); 692 } else { 693 err |= __put_user(frame->retcode, &frame->pretcode); 694 /* This is popl %eax ; movl $,%eax ; int $0x80 */ 695 err |= __put_user(0xb858, (short *)(frame->retcode+0)); 696 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2)); 697 err |= __put_user(0x80cd, (short *)(frame->retcode+6)); 698 } 699 700 if (err) 701 goto give_sigsegv; 702 703 /* Set up registers for signal handler */ 704 env->regs[R_ESP] = (unsigned long) frame; 705 env->eip = (unsigned long) ka->sa._sa_handler; 706 707 cpu_x86_load_seg(env, R_DS, __USER_DS); 708 cpu_x86_load_seg(env, R_ES, __USER_DS); 709 cpu_x86_load_seg(env, R_SS, __USER_DS); 710 cpu_x86_load_seg(env, R_CS, __USER_CS); 711 env->eflags &= ~TF_MASK; 712 713 return; 714 715 give_sigsegv: 716 if (sig == TARGET_SIGSEGV) 717 ka->sa._sa_handler = TARGET_SIG_DFL; 718 force_sig(TARGET_SIGSEGV /* , current */); 719 } 720 721 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 722 target_siginfo_t *info, 723 target_sigset_t *set, CPUX86State *env) 724 { 725 struct rt_sigframe *frame; 726 int i, err = 0; 727 728 frame = get_sigframe(ka, env, sizeof(*frame)); 729 730 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) 731 goto give_sigsegv; 732 733 err |= __put_user((/*current->exec_domain 734 && current->exec_domain->signal_invmap 735 && sig < 32 736 ? current->exec_domain->signal_invmap[sig] 737 : */sig), 738 &frame->sig); 739 err |= __put_user((target_ulong)&frame->info, &frame->pinfo); 740 err |= __put_user((target_ulong)&frame->uc, &frame->puc); 741 err |= copy_siginfo_to_user(&frame->info, info); 742 if (err) 743 goto give_sigsegv; 744 745 /* Create the ucontext. */ 746 err |= __put_user(0, &frame->uc.uc_flags); 747 err |= __put_user(0, &frame->uc.uc_link); 748 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp); 749 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0, 750 &frame->uc.uc_stack.ss_flags); 751 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size); 752 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate, 753 env, set->sig[0]); 754 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 755 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i])) 756 goto give_sigsegv; 757 } 758 759 /* Set up to return from userspace. If provided, use a stub 760 already in userspace. */ 761 if (ka->sa.sa_flags & TARGET_SA_RESTORER) { 762 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); 763 } else { 764 err |= __put_user(frame->retcode, &frame->pretcode); 765 /* This is movl $,%eax ; int $0x80 */ 766 err |= __put_user(0xb8, (char *)(frame->retcode+0)); 767 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1)); 768 err |= __put_user(0x80cd, (short *)(frame->retcode+5)); 769 } 770 771 if (err) 772 goto give_sigsegv; 773 774 /* Set up registers for signal handler */ 775 env->regs[R_ESP] = (unsigned long) frame; 776 env->eip = (unsigned long) ka->sa._sa_handler; 777 778 cpu_x86_load_seg(env, R_DS, __USER_DS); 779 cpu_x86_load_seg(env, R_ES, __USER_DS); 780 cpu_x86_load_seg(env, R_SS, __USER_DS); 781 cpu_x86_load_seg(env, R_CS, __USER_CS); 782 env->eflags &= ~TF_MASK; 783 784 return; 785 786 give_sigsegv: 787 if (sig == TARGET_SIGSEGV) 788 ka->sa._sa_handler = TARGET_SIG_DFL; 789 force_sig(TARGET_SIGSEGV /* , current */); 790 } 791 792 static int 793 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax) 794 { 795 unsigned int err = 0; 796 797 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs)); 798 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs)); 799 cpu_x86_load_seg(env, R_ES, lduw(&sc->es)); 800 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds)); 801 802 env->regs[R_EDI] = ldl(&sc->edi); 803 env->regs[R_ESI] = ldl(&sc->esi); 804 env->regs[R_EBP] = ldl(&sc->ebp); 805 env->regs[R_ESP] = ldl(&sc->esp); 806 env->regs[R_EBX] = ldl(&sc->ebx); 807 env->regs[R_EDX] = ldl(&sc->edx); 808 env->regs[R_ECX] = ldl(&sc->ecx); 809 env->eip = ldl(&sc->eip); 810 811 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3); 812 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3); 813 814 { 815 unsigned int tmpflags; 816 tmpflags = ldl(&sc->eflags); 817 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); 818 // regs->orig_eax = -1; /* disable syscall checks */ 819 } 820 821 { 822 struct _fpstate * buf; 823 buf = (void *)ldl(&sc->fpstate); 824 if (buf) { 825 #if 0 826 if (verify_area(VERIFY_READ, buf, sizeof(*buf))) 827 goto badframe; 828 #endif 829 cpu_x86_frstor(env, (void *)buf, 1); 830 } 831 } 832 833 *peax = ldl(&sc->eax); 834 return err; 835 #if 0 836 badframe: 837 return 1; 838 #endif 839 } 840 841 long do_sigreturn(CPUX86State *env) 842 { 843 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8); 844 target_sigset_t target_set; 845 sigset_t set; 846 int eax, i; 847 848 #if defined(DEBUG_SIGNAL) 849 fprintf(stderr, "do_sigreturn\n"); 850 #endif 851 /* set blocked signals */ 852 if (__get_user(target_set.sig[0], &frame->sc.oldmask)) 853 goto badframe; 854 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 855 if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) 856 goto badframe; 857 } 858 859 target_to_host_sigset_internal(&set, &target_set); 860 sigprocmask(SIG_SETMASK, &set, NULL); 861 862 /* restore registers */ 863 if (restore_sigcontext(env, &frame->sc, &eax)) 864 goto badframe; 865 return eax; 866 867 badframe: 868 force_sig(TARGET_SIGSEGV); 869 return 0; 870 } 871 872 long do_rt_sigreturn(CPUX86State *env) 873 { 874 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4); 875 sigset_t set; 876 // stack_t st; 877 int eax; 878 879 #if 0 880 if (verify_area(VERIFY_READ, frame, sizeof(*frame))) 881 goto badframe; 882 #endif 883 target_to_host_sigset(&set, &frame->uc.uc_sigmask); 884 sigprocmask(SIG_SETMASK, &set, NULL); 885 886 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax)) 887 goto badframe; 888 889 #if 0 890 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st))) 891 goto badframe; 892 /* It is more difficult to avoid calling this function than to 893 call it and ignore errors. */ 894 do_sigaltstack(&st, NULL, regs->esp); 895 #endif 896 return eax; 897 898 badframe: 899 force_sig(TARGET_SIGSEGV); 900 return 0; 901 } 902 903 #elif defined(TARGET_ARM) 904 905 struct target_sigcontext { 906 target_ulong trap_no; 907 target_ulong error_code; 908 target_ulong oldmask; 909 target_ulong arm_r0; 910 target_ulong arm_r1; 911 target_ulong arm_r2; 912 target_ulong arm_r3; 913 target_ulong arm_r4; 914 target_ulong arm_r5; 915 target_ulong arm_r6; 916 target_ulong arm_r7; 917 target_ulong arm_r8; 918 target_ulong arm_r9; 919 target_ulong arm_r10; 920 target_ulong arm_fp; 921 target_ulong arm_ip; 922 target_ulong arm_sp; 923 target_ulong arm_lr; 924 target_ulong arm_pc; 925 target_ulong arm_cpsr; 926 target_ulong fault_address; 927 }; 928 929 typedef struct target_sigaltstack { 930 target_ulong ss_sp; 931 int ss_flags; 932 target_ulong ss_size; 933 } target_stack_t; 934 935 struct target_ucontext { 936 target_ulong uc_flags; 937 target_ulong uc_link; 938 target_stack_t uc_stack; 939 struct target_sigcontext uc_mcontext; 940 target_sigset_t uc_sigmask; /* mask last for extensibility */ 941 }; 942 943 struct sigframe 944 { 945 struct target_sigcontext sc; 946 target_ulong extramask[TARGET_NSIG_WORDS-1]; 947 target_ulong retcode; 948 }; 949 950 struct rt_sigframe 951 { 952 struct target_siginfo *pinfo; 953 void *puc; 954 struct target_siginfo info; 955 struct target_ucontext uc; 956 target_ulong retcode; 957 }; 958 959 #define TARGET_CONFIG_CPU_32 1 960 961 /* 962 * For ARM syscalls, we encode the syscall number into the instruction. 963 */ 964 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE)) 965 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE)) 966 967 /* 968 * For Thumb syscalls, we pass the syscall number via r7. We therefore 969 * need two 16-bit instructions. 970 */ 971 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn)) 972 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn)) 973 974 static const target_ulong retcodes[4] = { 975 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN, 976 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN 977 }; 978 979 980 #define __put_user_error(x,p,e) __put_user(x, p) 981 #define __get_user_error(x,p,e) __get_user(x, p) 982 983 static inline int valid_user_regs(CPUState *regs) 984 { 985 return 1; 986 } 987 988 static int 989 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ 990 CPUState *env, unsigned long mask) 991 { 992 int err = 0; 993 994 __put_user_error(env->regs[0], &sc->arm_r0, err); 995 __put_user_error(env->regs[1], &sc->arm_r1, err); 996 __put_user_error(env->regs[2], &sc->arm_r2, err); 997 __put_user_error(env->regs[3], &sc->arm_r3, err); 998 __put_user_error(env->regs[4], &sc->arm_r4, err); 999 __put_user_error(env->regs[5], &sc->arm_r5, err); 1000 __put_user_error(env->regs[6], &sc->arm_r6, err); 1001 __put_user_error(env->regs[7], &sc->arm_r7, err); 1002 __put_user_error(env->regs[8], &sc->arm_r8, err); 1003 __put_user_error(env->regs[9], &sc->arm_r9, err); 1004 __put_user_error(env->regs[10], &sc->arm_r10, err); 1005 __put_user_error(env->regs[11], &sc->arm_fp, err); 1006 __put_user_error(env->regs[12], &sc->arm_ip, err); 1007 __put_user_error(env->regs[13], &sc->arm_sp, err); 1008 __put_user_error(env->regs[14], &sc->arm_lr, err); 1009 __put_user_error(env->regs[15], &sc->arm_pc, err); 1010 #ifdef TARGET_CONFIG_CPU_32 1011 __put_user_error(env->cpsr, &sc->arm_cpsr, err); 1012 #endif 1013 1014 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err); 1015 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err); 1016 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err); 1017 __put_user_error(mask, &sc->oldmask, err); 1018 1019 return err; 1020 } 1021 1022 static inline void * 1023 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize) 1024 { 1025 unsigned long sp = regs->regs[13]; 1026 1027 #if 0 1028 /* 1029 * This is the X/Open sanctioned signal stack switching. 1030 */ 1031 if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) 1032 sp = current->sas_ss_sp + current->sas_ss_size; 1033 #endif 1034 /* 1035 * ATPCS B01 mandates 8-byte alignment 1036 */ 1037 return (void *)((sp - framesize) & ~7); 1038 } 1039 1040 static int 1041 setup_return(CPUState *env, struct emulated_sigaction *ka, 1042 target_ulong *rc, void *frame, int usig) 1043 { 1044 target_ulong handler = (target_ulong)ka->sa._sa_handler; 1045 target_ulong retcode; 1046 int thumb = 0; 1047 #if defined(TARGET_CONFIG_CPU_32) 1048 target_ulong cpsr = env->cpsr; 1049 1050 #if 0 1051 /* 1052 * Maybe we need to deliver a 32-bit signal to a 26-bit task. 1053 */ 1054 if (ka->sa.sa_flags & SA_THIRTYTWO) 1055 cpsr = (cpsr & ~MODE_MASK) | USR_MODE; 1056 1057 #ifdef CONFIG_ARM_THUMB 1058 if (elf_hwcap & HWCAP_THUMB) { 1059 /* 1060 * The LSB of the handler determines if we're going to 1061 * be using THUMB or ARM mode for this signal handler. 1062 */ 1063 thumb = handler & 1; 1064 1065 if (thumb) 1066 cpsr |= T_BIT; 1067 else 1068 cpsr &= ~T_BIT; 1069 } 1070 #endif 1071 #endif 1072 #endif /* TARGET_CONFIG_CPU_32 */ 1073 1074 if (ka->sa.sa_flags & TARGET_SA_RESTORER) { 1075 retcode = (target_ulong)ka->sa.sa_restorer; 1076 } else { 1077 unsigned int idx = thumb; 1078 1079 if (ka->sa.sa_flags & TARGET_SA_SIGINFO) 1080 idx += 2; 1081 1082 if (__put_user(retcodes[idx], rc)) 1083 return 1; 1084 #if 0 1085 flush_icache_range((target_ulong)rc, 1086 (target_ulong)(rc + 1)); 1087 #endif 1088 retcode = ((target_ulong)rc) + thumb; 1089 } 1090 1091 env->regs[0] = usig; 1092 env->regs[13] = (target_ulong)frame; 1093 env->regs[14] = retcode; 1094 env->regs[15] = handler & (thumb ? ~1 : ~3); 1095 1096 #ifdef TARGET_CONFIG_CPU_32 1097 env->cpsr = cpsr; 1098 #endif 1099 1100 return 0; 1101 } 1102 1103 static void setup_frame(int usig, struct emulated_sigaction *ka, 1104 target_sigset_t *set, CPUState *regs) 1105 { 1106 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame)); 1107 int i, err = 0; 1108 1109 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]); 1110 1111 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 1112 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 1113 return; 1114 } 1115 1116 if (err == 0) 1117 err = setup_return(regs, ka, &frame->retcode, frame, usig); 1118 // return err; 1119 } 1120 1121 static void setup_rt_frame(int usig, struct emulated_sigaction *ka, 1122 target_siginfo_t *info, 1123 target_sigset_t *set, CPUState *env) 1124 { 1125 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame)); 1126 int i, err = 0; 1127 1128 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) 1129 return /* 1 */; 1130 1131 __put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err); 1132 __put_user_error(&frame->uc, (target_ulong *)&frame->puc, err); 1133 err |= copy_siginfo_to_user(&frame->info, info); 1134 1135 /* Clear all the bits of the ucontext we don't use. */ 1136 err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext)); 1137 1138 err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/ 1139 env, set->sig[0]); 1140 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 1141 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i])) 1142 return; 1143 } 1144 1145 if (err == 0) 1146 err = setup_return(env, ka, &frame->retcode, frame, usig); 1147 1148 if (err == 0) { 1149 /* 1150 * For realtime signals we must also set the second and third 1151 * arguments for the signal handler. 1152 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06 1153 */ 1154 env->regs[1] = (target_ulong)frame->pinfo; 1155 env->regs[2] = (target_ulong)frame->puc; 1156 } 1157 1158 // return err; 1159 } 1160 1161 static int 1162 restore_sigcontext(CPUState *env, struct target_sigcontext *sc) 1163 { 1164 int err = 0; 1165 1166 __get_user_error(env->regs[0], &sc->arm_r0, err); 1167 __get_user_error(env->regs[1], &sc->arm_r1, err); 1168 __get_user_error(env->regs[2], &sc->arm_r2, err); 1169 __get_user_error(env->regs[3], &sc->arm_r3, err); 1170 __get_user_error(env->regs[4], &sc->arm_r4, err); 1171 __get_user_error(env->regs[5], &sc->arm_r5, err); 1172 __get_user_error(env->regs[6], &sc->arm_r6, err); 1173 __get_user_error(env->regs[7], &sc->arm_r7, err); 1174 __get_user_error(env->regs[8], &sc->arm_r8, err); 1175 __get_user_error(env->regs[9], &sc->arm_r9, err); 1176 __get_user_error(env->regs[10], &sc->arm_r10, err); 1177 __get_user_error(env->regs[11], &sc->arm_fp, err); 1178 __get_user_error(env->regs[12], &sc->arm_ip, err); 1179 __get_user_error(env->regs[13], &sc->arm_sp, err); 1180 __get_user_error(env->regs[14], &sc->arm_lr, err); 1181 __get_user_error(env->regs[15], &sc->arm_pc, err); 1182 #ifdef TARGET_CONFIG_CPU_32 1183 __get_user_error(env->cpsr, &sc->arm_cpsr, err); 1184 #endif 1185 1186 err |= !valid_user_regs(env); 1187 1188 return err; 1189 } 1190 1191 long do_sigreturn(CPUState *env) 1192 { 1193 struct sigframe *frame; 1194 target_sigset_t set; 1195 sigset_t host_set; 1196 int i; 1197 1198 /* 1199 * Since we stacked the signal on a 64-bit boundary, 1200 * then 'sp' should be word aligned here. If it's 1201 * not, then the user is trying to mess with us. 1202 */ 1203 if (env->regs[13] & 7) 1204 goto badframe; 1205 1206 frame = (struct sigframe *)env->regs[13]; 1207 1208 #if 0 1209 if (verify_area(VERIFY_READ, frame, sizeof (*frame))) 1210 goto badframe; 1211 #endif 1212 if (__get_user(set.sig[0], &frame->sc.oldmask)) 1213 goto badframe; 1214 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 1215 if (__get_user(set.sig[i], &frame->extramask[i - 1])) 1216 goto badframe; 1217 } 1218 1219 target_to_host_sigset_internal(&host_set, &set); 1220 sigprocmask(SIG_SETMASK, &host_set, NULL); 1221 1222 if (restore_sigcontext(env, &frame->sc)) 1223 goto badframe; 1224 1225 #if 0 1226 /* Send SIGTRAP if we're single-stepping */ 1227 if (ptrace_cancel_bpt(current)) 1228 send_sig(SIGTRAP, current, 1); 1229 #endif 1230 return env->regs[0]; 1231 1232 badframe: 1233 force_sig(SIGSEGV /* , current */); 1234 return 0; 1235 } 1236 1237 long do_rt_sigreturn(CPUState *env) 1238 { 1239 struct rt_sigframe *frame; 1240 sigset_t host_set; 1241 1242 /* 1243 * Since we stacked the signal on a 64-bit boundary, 1244 * then 'sp' should be word aligned here. If it's 1245 * not, then the user is trying to mess with us. 1246 */ 1247 if (env->regs[13] & 7) 1248 goto badframe; 1249 1250 frame = (struct rt_sigframe *)env->regs[13]; 1251 1252 #if 0 1253 if (verify_area(VERIFY_READ, frame, sizeof (*frame))) 1254 goto badframe; 1255 #endif 1256 target_to_host_sigset(&host_set, &frame->uc.uc_sigmask); 1257 sigprocmask(SIG_SETMASK, &host_set, NULL); 1258 1259 if (restore_sigcontext(env, &frame->uc.uc_mcontext)) 1260 goto badframe; 1261 1262 #if 0 1263 /* Send SIGTRAP if we're single-stepping */ 1264 if (ptrace_cancel_bpt(current)) 1265 send_sig(SIGTRAP, current, 1); 1266 #endif 1267 return env->regs[0]; 1268 1269 badframe: 1270 force_sig(SIGSEGV /* , current */); 1271 return 0; 1272 } 1273 1274 #elif defined(TARGET_SPARC) 1275 #define __SUNOS_MAXWIN 31 1276 1277 /* This is what SunOS does, so shall I. */ 1278 struct target_sigcontext { 1279 target_ulong sigc_onstack; /* state to restore */ 1280 1281 target_ulong sigc_mask; /* sigmask to restore */ 1282 target_ulong sigc_sp; /* stack pointer */ 1283 target_ulong sigc_pc; /* program counter */ 1284 target_ulong sigc_npc; /* next program counter */ 1285 target_ulong sigc_psr; /* for condition codes etc */ 1286 target_ulong sigc_g1; /* User uses these two registers */ 1287 target_ulong sigc_o0; /* within the trampoline code. */ 1288 1289 /* Now comes information regarding the users window set 1290 * at the time of the signal. 1291 */ 1292 target_ulong sigc_oswins; /* outstanding windows */ 1293 1294 /* stack ptrs for each regwin buf */ 1295 char *sigc_spbuf[__SUNOS_MAXWIN]; 1296 1297 /* Windows to restore after signal */ 1298 struct { 1299 target_ulong locals[8]; 1300 target_ulong ins[8]; 1301 } sigc_wbuf[__SUNOS_MAXWIN]; 1302 }; 1303 /* A Sparc stack frame */ 1304 struct sparc_stackf { 1305 target_ulong locals[8]; 1306 target_ulong ins[6]; 1307 struct sparc_stackf *fp; 1308 target_ulong callers_pc; 1309 char *structptr; 1310 target_ulong xargs[6]; 1311 target_ulong xxargs[1]; 1312 }; 1313 1314 typedef struct { 1315 struct { 1316 target_ulong psr; 1317 target_ulong pc; 1318 target_ulong npc; 1319 target_ulong y; 1320 target_ulong u_regs[16]; /* globals and ins */ 1321 } si_regs; 1322 int si_mask; 1323 } __siginfo_t; 1324 1325 typedef struct { 1326 unsigned long si_float_regs [32]; 1327 unsigned long si_fsr; 1328 unsigned long si_fpqdepth; 1329 struct { 1330 unsigned long *insn_addr; 1331 unsigned long insn; 1332 } si_fpqueue [16]; 1333 } __siginfo_fpu_t; 1334 1335 1336 struct target_signal_frame { 1337 struct sparc_stackf ss; 1338 __siginfo_t info; 1339 __siginfo_fpu_t *fpu_save; 1340 target_ulong insns[2] __attribute__ ((aligned (8))); 1341 target_ulong extramask[TARGET_NSIG_WORDS - 1]; 1342 target_ulong extra_size; /* Should be 0 */ 1343 __siginfo_fpu_t fpu_state; 1344 }; 1345 struct target_rt_signal_frame { 1346 struct sparc_stackf ss; 1347 siginfo_t info; 1348 target_ulong regs[20]; 1349 sigset_t mask; 1350 __siginfo_fpu_t *fpu_save; 1351 unsigned int insns[2]; 1352 stack_t stack; 1353 unsigned int extra_size; /* Should be 0 */ 1354 __siginfo_fpu_t fpu_state; 1355 }; 1356 1357 #define UREG_O0 0 1358 #define UREG_O6 6 1359 #define UREG_I0 16 1360 #define UREG_I1 17 1361 #define UREG_I2 18 1362 #define UREG_I6 22 1363 #define UREG_I7 23 1364 #define UREG_FP UREG_I6 1365 #define UREG_SP UREG_O6 1366 1367 static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize) 1368 { 1369 unsigned long sp; 1370 1371 sp = env->regwptr[UREG_FP]; 1372 #if 0 1373 1374 /* This is the X/Open sanctioned signal stack switching. */ 1375 if (sa->sa_flags & TARGET_SA_ONSTACK) { 1376 if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7)) 1377 sp = current->sas_ss_sp + current->sas_ss_size; 1378 } 1379 #endif 1380 return (void *)(sp - framesize); 1381 } 1382 1383 static int 1384 setup___siginfo(__siginfo_t *si, CPUState *env, target_ulong mask) 1385 { 1386 int err = 0, i; 1387 1388 fprintf(stderr, "2.a %lx psr: %lx regs: %lx\n", si, env->psr, si->si_regs.psr); 1389 err |= __put_user(env->psr, &si->si_regs.psr); 1390 fprintf(stderr, "2.a1 pc:%lx\n", si->si_regs.pc); 1391 err |= __put_user(env->pc, &si->si_regs.pc); 1392 err |= __put_user(env->npc, &si->si_regs.npc); 1393 err |= __put_user(env->y, &si->si_regs.y); 1394 fprintf(stderr, "2.b\n"); 1395 for (i=0; i < 7; i++) { 1396 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]); 1397 } 1398 for (i=0; i < 7; i++) { 1399 err |= __put_user(env->regwptr[i+16], &si->si_regs.u_regs[i+8]); 1400 } 1401 fprintf(stderr, "2.c\n"); 1402 err |= __put_user(mask, &si->si_mask); 1403 return err; 1404 } 1405 static int 1406 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ 1407 CPUState *env, unsigned long mask) 1408 { 1409 int err = 0; 1410 1411 err |= __put_user(mask, &sc->sigc_mask); 1412 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp); 1413 err |= __put_user(env->pc, &sc->sigc_pc); 1414 err |= __put_user(env->npc, &sc->sigc_npc); 1415 err |= __put_user(env->psr, &sc->sigc_psr); 1416 err |= __put_user(env->gregs[1], &sc->sigc_g1); 1417 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0); 1418 1419 return err; 1420 } 1421 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7))) 1422 1423 static void setup_frame(int sig, struct emulated_sigaction *ka, 1424 target_sigset_t *set, CPUState *env) 1425 { 1426 struct target_signal_frame *sf; 1427 int sigframe_size, err, i; 1428 1429 /* 1. Make sure everything is clean */ 1430 //synchronize_user_stack(); 1431 1432 sigframe_size = NF_ALIGNEDSZ; 1433 1434 sf = (struct target_signal_frame *) 1435 get_sigframe(ka, env, sigframe_size); 1436 1437 #if 0 1438 if (invalid_frame_pointer(sf, sigframe_size)) 1439 goto sigill_and_return; 1440 #endif 1441 /* 2. Save the current process state */ 1442 err = setup___siginfo(&sf->info, env, set->sig[0]); 1443 err |= __put_user(0, &sf->extra_size); 1444 1445 //err |= save_fpu_state(regs, &sf->fpu_state); 1446 //err |= __put_user(&sf->fpu_state, &sf->fpu_save); 1447 1448 err |= __put_user(set->sig[0], &sf->info.si_mask); 1449 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { 1450 err |= __put_user(set->sig[i + 1], &sf->extramask[i]); 1451 } 1452 1453 for (i = 0; i < 7; i++) { 1454 err |= __put_user(env->regwptr[i + 8], &sf->ss.locals[i]); 1455 } 1456 for (i = 0; i < 7; i++) { 1457 err |= __put_user(env->regwptr[i + 16], &sf->ss.ins[i]); 1458 } 1459 //err |= __copy_to_user(sf, (char *) regs->u_regs[UREG_FP], 1460 // sizeof(struct reg_window)); 1461 if (err) 1462 goto sigsegv; 1463 1464 /* 3. signal handler back-trampoline and parameters */ 1465 env->regwptr[UREG_FP] = (target_ulong) sf; 1466 env->regwptr[UREG_I0] = sig; 1467 env->regwptr[UREG_I1] = (target_ulong) &sf->info; 1468 env->regwptr[UREG_I2] = (target_ulong) &sf->info; 1469 1470 /* 4. signal handler */ 1471 env->pc = (unsigned long) ka->sa._sa_handler; 1472 env->npc = (env->pc + 4); 1473 /* 5. return to kernel instructions */ 1474 if (ka->sa.sa_restorer) 1475 env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer; 1476 else { 1477 env->regwptr[UREG_I7] = (unsigned long)(&(sf->insns[0]) - 2); 1478 1479 /* mov __NR_sigreturn, %g1 */ 1480 err |= __put_user(0x821020d8, &sf->insns[0]); 1481 1482 /* t 0x10 */ 1483 err |= __put_user(0x91d02010, &sf->insns[1]); 1484 if (err) 1485 goto sigsegv; 1486 1487 /* Flush instruction space. */ 1488 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); 1489 //tb_flush(env); 1490 } 1491 return; 1492 1493 sigill_and_return: 1494 force_sig(TARGET_SIGILL); 1495 sigsegv: 1496 force_sig(TARGET_SIGSEGV); 1497 } 1498 static inline int 1499 restore_fpu_state(CPUState *env, __siginfo_fpu_t *fpu) 1500 { 1501 int err; 1502 #if 0 1503 #ifdef CONFIG_SMP 1504 if (current->flags & PF_USEDFPU) 1505 regs->psr &= ~PSR_EF; 1506 #else 1507 if (current == last_task_used_math) { 1508 last_task_used_math = 0; 1509 regs->psr &= ~PSR_EF; 1510 } 1511 #endif 1512 current->used_math = 1; 1513 current->flags &= ~PF_USEDFPU; 1514 #endif 1515 #if 0 1516 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu))) 1517 return -EFAULT; 1518 #endif 1519 1520 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0], 1521 (sizeof(unsigned long) * 32)); 1522 err |= __get_user(env->fsr, &fpu->si_fsr); 1523 #if 0 1524 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth); 1525 if (current->thread.fpqdepth != 0) 1526 err |= __copy_from_user(¤t->thread.fpqueue[0], 1527 &fpu->si_fpqueue[0], 1528 ((sizeof(unsigned long) + 1529 (sizeof(unsigned long *)))*16)); 1530 #endif 1531 return err; 1532 } 1533 1534 1535 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 1536 target_siginfo_t *info, 1537 target_sigset_t *set, CPUState *env) 1538 { 1539 fprintf(stderr, "setup_rt_frame: not implemented\n"); 1540 } 1541 1542 long do_sigreturn(CPUState *env) 1543 { 1544 struct target_signal_frame *sf; 1545 unsigned long up_psr, pc, npc; 1546 target_sigset_t set; 1547 __siginfo_fpu_t *fpu_save; 1548 int err; 1549 1550 sf = (struct new_signal_frame *) env->regwptr[UREG_FP]; 1551 fprintf(stderr, "sigreturn sf: %lx\n", &sf); 1552 1553 /* 1. Make sure we are not getting garbage from the user */ 1554 #if 0 1555 if (verify_area (VERIFY_READ, sf, sizeof (*sf))) 1556 goto segv_and_exit; 1557 #endif 1558 1559 if (((uint) sf) & 3) 1560 goto segv_and_exit; 1561 1562 err = __get_user(pc, &sf->info.si_regs.pc); 1563 err |= __get_user(npc, &sf->info.si_regs.npc); 1564 1565 fprintf(stderr, "pc: %lx npc %lx\n", pc, npc); 1566 if ((pc | npc) & 3) 1567 goto segv_and_exit; 1568 1569 /* 2. Restore the state */ 1570 up_psr = env->psr; 1571 //err |= __copy_from_user(regs, &sf->info.si_regs, sizeof (struct pt_regs) 1572 //); 1573 /* User can only change condition codes and FPU enabling in %psr. */ 1574 env->psr = (up_psr & ~(PSR_ICC /* | PSR_EF */)) 1575 | (env->psr & (PSR_ICC /* | PSR_EF */)); 1576 fprintf(stderr, "psr: %lx\n", env->psr); 1577 1578 err |= __get_user(fpu_save, &sf->fpu_save); 1579 1580 if (fpu_save) 1581 err |= restore_fpu_state(env, fpu_save); 1582 1583 /* This is pretty much atomic, no amount locking would prevent 1584 * the races which exist anyways. 1585 */ 1586 err |= __get_user(set.sig[0], &sf->info.si_mask); 1587 //err |= __copy_from_user(&set.sig[1], &sf->extramask, 1588 // (_NSIG_WORDS-1) * sizeof(unsigned int)); 1589 1590 if (err) 1591 goto segv_and_exit; 1592 1593 #if 0 1594 sigdelsetmask(&set, ~_BLOCKABLE); 1595 spin_lock_irq(¤t->sigmask_lock); 1596 current->blocked = set; 1597 recalc_sigpending(current); 1598 spin_unlock_irq(¤t->sigmask_lock); 1599 #endif 1600 fprintf(stderr, "returning %lx\n", env->regwptr[0]); 1601 return env->regwptr[0]; 1602 1603 segv_and_exit: 1604 force_sig(TARGET_SIGSEGV); 1605 } 1606 1607 long do_rt_sigreturn(CPUState *env) 1608 { 1609 fprintf(stderr, "do_rt_sigreturn: not implemented\n"); 1610 return -ENOSYS; 1611 } 1612 1613 1614 #else 1615 1616 static void setup_frame(int sig, struct emulated_sigaction *ka, 1617 target_sigset_t *set, CPUState *env) 1618 { 1619 fprintf(stderr, "setup_frame: not implemented\n"); 1620 } 1621 1622 static void setup_rt_frame(int sig, struct emulated_sigaction *ka, 1623 target_siginfo_t *info, 1624 target_sigset_t *set, CPUState *env) 1625 { 1626 fprintf(stderr, "setup_rt_frame: not implemented\n"); 1627 } 1628 1629 long do_sigreturn(CPUState *env) 1630 { 1631 fprintf(stderr, "do_sigreturn: not implemented\n"); 1632 return -ENOSYS; 1633 } 1634 1635 long do_rt_sigreturn(CPUState *env) 1636 { 1637 fprintf(stderr, "do_rt_sigreturn: not implemented\n"); 1638 return -ENOSYS; 1639 } 1640 1641 #endif 1642 1643 void process_pending_signals(void *cpu_env) 1644 { 1645 int sig; 1646 target_ulong handler; 1647 sigset_t set, old_set; 1648 target_sigset_t target_old_set; 1649 struct emulated_sigaction *k; 1650 struct sigqueue *q; 1651 1652 if (!signal_pending) 1653 return; 1654 1655 k = sigact_table; 1656 for(sig = 1; sig <= TARGET_NSIG; sig++) { 1657 if (k->pending) 1658 goto handle_signal; 1659 k++; 1660 } 1661 /* if no signal is pending, just return */ 1662 signal_pending = 0; 1663 return; 1664 1665 handle_signal: 1666 #ifdef DEBUG_SIGNAL 1667 fprintf(stderr, "qemu: process signal %d\n", sig); 1668 #endif 1669 /* dequeue signal */ 1670 q = k->first; 1671 k->first = q->next; 1672 if (!k->first) 1673 k->pending = 0; 1674 1675 handler = k->sa._sa_handler; 1676 if (handler == TARGET_SIG_DFL) { 1677 /* default handler : ignore some signal. The other are fatal */ 1678 if (sig != TARGET_SIGCHLD && 1679 sig != TARGET_SIGURG && 1680 sig != TARGET_SIGWINCH) { 1681 force_sig(sig); 1682 } 1683 } else if (handler == TARGET_SIG_IGN) { 1684 /* ignore sig */ 1685 } else if (handler == TARGET_SIG_ERR) { 1686 force_sig(sig); 1687 } else { 1688 /* compute the blocked signals during the handler execution */ 1689 target_to_host_sigset(&set, &k->sa.sa_mask); 1690 /* SA_NODEFER indicates that the current signal should not be 1691 blocked during the handler */ 1692 if (!(k->sa.sa_flags & TARGET_SA_NODEFER)) 1693 sigaddset(&set, target_to_host_signal(sig)); 1694 1695 /* block signals in the handler using Linux */ 1696 sigprocmask(SIG_BLOCK, &set, &old_set); 1697 /* save the previous blocked signal state to restore it at the 1698 end of the signal execution (see do_sigreturn) */ 1699 host_to_target_sigset_internal(&target_old_set, &old_set); 1700 1701 /* if the CPU is in VM86 mode, we restore the 32 bit values */ 1702 #ifdef TARGET_I386 1703 { 1704 CPUX86State *env = cpu_env; 1705 if (env->eflags & VM_MASK) 1706 save_v86_state(env); 1707 } 1708 #endif 1709 /* prepare the stack frame of the virtual CPU */ 1710 if (k->sa.sa_flags & TARGET_SA_SIGINFO) 1711 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env); 1712 else 1713 setup_frame(sig, k, &target_old_set, cpu_env); 1714 if (k->sa.sa_flags & TARGET_SA_RESETHAND) 1715 k->sa._sa_handler = TARGET_SIG_DFL; 1716 } 1717 if (q != &k->info) 1718 free_sigqueue(q); 1719 } 1720 1721 1722