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 <stdlib.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include <stdarg.h> 23 #include <unistd.h> 24 #include <errno.h> 25 #include <assert.h> 26 #include <sys/ucontext.h> 27 #include <sys/resource.h> 28 29 #include "qemu.h" 30 #include "qemu-common.h" 31 #include "target_signal.h" 32 33 //#define DEBUG_SIGNAL 34 35 static struct target_sigaltstack target_sigaltstack_used = { 36 .ss_sp = 0, 37 .ss_size = 0, 38 .ss_flags = TARGET_SS_DISABLE, 39 }; 40 41 static struct target_sigaction sigact_table[TARGET_NSIG]; 42 43 static void host_signal_handler(int host_signum, siginfo_t *info, 44 void *puc); 45 46 static uint8_t host_to_target_signal_table[_NSIG] = { 47 [SIGHUP] = TARGET_SIGHUP, 48 [SIGINT] = TARGET_SIGINT, 49 [SIGQUIT] = TARGET_SIGQUIT, 50 [SIGILL] = TARGET_SIGILL, 51 [SIGTRAP] = TARGET_SIGTRAP, 52 [SIGABRT] = TARGET_SIGABRT, 53 /* [SIGIOT] = TARGET_SIGIOT,*/ 54 [SIGBUS] = TARGET_SIGBUS, 55 [SIGFPE] = TARGET_SIGFPE, 56 [SIGKILL] = TARGET_SIGKILL, 57 [SIGUSR1] = TARGET_SIGUSR1, 58 [SIGSEGV] = TARGET_SIGSEGV, 59 [SIGUSR2] = TARGET_SIGUSR2, 60 [SIGPIPE] = TARGET_SIGPIPE, 61 [SIGALRM] = TARGET_SIGALRM, 62 [SIGTERM] = TARGET_SIGTERM, 63 #ifdef SIGSTKFLT 64 [SIGSTKFLT] = TARGET_SIGSTKFLT, 65 #endif 66 [SIGCHLD] = TARGET_SIGCHLD, 67 [SIGCONT] = TARGET_SIGCONT, 68 [SIGSTOP] = TARGET_SIGSTOP, 69 [SIGTSTP] = TARGET_SIGTSTP, 70 [SIGTTIN] = TARGET_SIGTTIN, 71 [SIGTTOU] = TARGET_SIGTTOU, 72 [SIGURG] = TARGET_SIGURG, 73 [SIGXCPU] = TARGET_SIGXCPU, 74 [SIGXFSZ] = TARGET_SIGXFSZ, 75 [SIGVTALRM] = TARGET_SIGVTALRM, 76 [SIGPROF] = TARGET_SIGPROF, 77 [SIGWINCH] = TARGET_SIGWINCH, 78 [SIGIO] = TARGET_SIGIO, 79 [SIGPWR] = TARGET_SIGPWR, 80 [SIGSYS] = TARGET_SIGSYS, 81 /* next signals stay the same */ 82 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with 83 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/ 84 To fix this properly we need to do manual signal delivery multiplexed 85 over a single host signal. */ 86 [__SIGRTMIN] = __SIGRTMAX, 87 [__SIGRTMAX] = __SIGRTMIN, 88 }; 89 static uint8_t target_to_host_signal_table[_NSIG]; 90 91 static inline int on_sig_stack(unsigned long sp) 92 { 93 return (sp - target_sigaltstack_used.ss_sp 94 < target_sigaltstack_used.ss_size); 95 } 96 97 static inline int sas_ss_flags(unsigned long sp) 98 { 99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE 100 : on_sig_stack(sp) ? SS_ONSTACK : 0); 101 } 102 103 int host_to_target_signal(int sig) 104 { 105 if (sig < 0 || sig >= _NSIG) 106 return sig; 107 return host_to_target_signal_table[sig]; 108 } 109 110 int target_to_host_signal(int sig) 111 { 112 if (sig < 0 || sig >= _NSIG) 113 return sig; 114 return target_to_host_signal_table[sig]; 115 } 116 117 static inline void target_sigemptyset(target_sigset_t *set) 118 { 119 memset(set, 0, sizeof(*set)); 120 } 121 122 static inline void target_sigaddset(target_sigset_t *set, int signum) 123 { 124 signum--; 125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 126 set->sig[signum / TARGET_NSIG_BPW] |= mask; 127 } 128 129 static inline int target_sigismember(const target_sigset_t *set, int signum) 130 { 131 signum--; 132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW); 133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0); 134 } 135 136 static void host_to_target_sigset_internal(target_sigset_t *d, 137 const sigset_t *s) 138 { 139 int i; 140 target_sigemptyset(d); 141 for (i = 1; i <= TARGET_NSIG; i++) { 142 if (sigismember(s, i)) { 143 target_sigaddset(d, host_to_target_signal(i)); 144 } 145 } 146 } 147 148 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) 149 { 150 target_sigset_t d1; 151 int i; 152 153 host_to_target_sigset_internal(&d1, s); 154 for(i = 0;i < TARGET_NSIG_WORDS; i++) 155 d->sig[i] = tswapal(d1.sig[i]); 156 } 157 158 static void target_to_host_sigset_internal(sigset_t *d, 159 const target_sigset_t *s) 160 { 161 int i; 162 sigemptyset(d); 163 for (i = 1; i <= TARGET_NSIG; i++) { 164 if (target_sigismember(s, i)) { 165 sigaddset(d, target_to_host_signal(i)); 166 } 167 } 168 } 169 170 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) 171 { 172 target_sigset_t s1; 173 int i; 174 175 for(i = 0;i < TARGET_NSIG_WORDS; i++) 176 s1.sig[i] = tswapal(s->sig[i]); 177 target_to_host_sigset_internal(d, &s1); 178 } 179 180 void host_to_target_old_sigset(abi_ulong *old_sigset, 181 const sigset_t *sigset) 182 { 183 target_sigset_t d; 184 host_to_target_sigset(&d, sigset); 185 *old_sigset = d.sig[0]; 186 } 187 188 void target_to_host_old_sigset(sigset_t *sigset, 189 const abi_ulong *old_sigset) 190 { 191 target_sigset_t d; 192 int i; 193 194 d.sig[0] = *old_sigset; 195 for(i = 1;i < TARGET_NSIG_WORDS; i++) 196 d.sig[i] = 0; 197 target_to_host_sigset(sigset, &d); 198 } 199 200 /* Wrapper for sigprocmask function 201 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset 202 * are host signal set, not guest ones. This wraps the sigprocmask host calls 203 * that should be protected (calls originated from guest) 204 */ 205 int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset) 206 { 207 int ret; 208 sigset_t val; 209 sigset_t *temp = NULL; 210 CPUState *cpu = thread_cpu; 211 TaskState *ts = (TaskState *)cpu->opaque; 212 bool segv_was_blocked = ts->sigsegv_blocked; 213 214 if (set) { 215 bool has_sigsegv = sigismember(set, SIGSEGV); 216 val = *set; 217 temp = &val; 218 219 sigdelset(temp, SIGSEGV); 220 221 switch (how) { 222 case SIG_BLOCK: 223 if (has_sigsegv) { 224 ts->sigsegv_blocked = true; 225 } 226 break; 227 case SIG_UNBLOCK: 228 if (has_sigsegv) { 229 ts->sigsegv_blocked = false; 230 } 231 break; 232 case SIG_SETMASK: 233 ts->sigsegv_blocked = has_sigsegv; 234 break; 235 default: 236 g_assert_not_reached(); 237 } 238 } 239 240 ret = sigprocmask(how, temp, oldset); 241 242 if (oldset && segv_was_blocked) { 243 sigaddset(oldset, SIGSEGV); 244 } 245 246 return ret; 247 } 248 249 /* siginfo conversion */ 250 251 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, 252 const siginfo_t *info) 253 { 254 int sig = host_to_target_signal(info->si_signo); 255 tinfo->si_signo = sig; 256 tinfo->si_errno = 0; 257 tinfo->si_code = info->si_code; 258 259 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV 260 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) { 261 /* Should never come here, but who knows. The information for 262 the target is irrelevant. */ 263 tinfo->_sifields._sigfault._addr = 0; 264 } else if (sig == TARGET_SIGIO) { 265 tinfo->_sifields._sigpoll._band = info->si_band; 266 tinfo->_sifields._sigpoll._fd = info->si_fd; 267 } else if (sig == TARGET_SIGCHLD) { 268 tinfo->_sifields._sigchld._pid = info->si_pid; 269 tinfo->_sifields._sigchld._uid = info->si_uid; 270 tinfo->_sifields._sigchld._status 271 = host_to_target_waitstatus(info->si_status); 272 tinfo->_sifields._sigchld._utime = info->si_utime; 273 tinfo->_sifields._sigchld._stime = info->si_stime; 274 } else if (sig >= TARGET_SIGRTMIN) { 275 tinfo->_sifields._rt._pid = info->si_pid; 276 tinfo->_sifields._rt._uid = info->si_uid; 277 /* XXX: potential problem if 64 bit */ 278 tinfo->_sifields._rt._sigval.sival_ptr 279 = (abi_ulong)(unsigned long)info->si_value.sival_ptr; 280 } 281 } 282 283 static void tswap_siginfo(target_siginfo_t *tinfo, 284 const target_siginfo_t *info) 285 { 286 int sig = info->si_signo; 287 tinfo->si_signo = tswap32(sig); 288 tinfo->si_errno = tswap32(info->si_errno); 289 tinfo->si_code = tswap32(info->si_code); 290 291 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV 292 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) { 293 tinfo->_sifields._sigfault._addr 294 = tswapal(info->_sifields._sigfault._addr); 295 } else if (sig == TARGET_SIGIO) { 296 tinfo->_sifields._sigpoll._band 297 = tswap32(info->_sifields._sigpoll._band); 298 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd); 299 } else if (sig == TARGET_SIGCHLD) { 300 tinfo->_sifields._sigchld._pid 301 = tswap32(info->_sifields._sigchld._pid); 302 tinfo->_sifields._sigchld._uid 303 = tswap32(info->_sifields._sigchld._uid); 304 tinfo->_sifields._sigchld._status 305 = tswap32(info->_sifields._sigchld._status); 306 tinfo->_sifields._sigchld._utime 307 = tswapal(info->_sifields._sigchld._utime); 308 tinfo->_sifields._sigchld._stime 309 = tswapal(info->_sifields._sigchld._stime); 310 } else if (sig >= TARGET_SIGRTMIN) { 311 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid); 312 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid); 313 tinfo->_sifields._rt._sigval.sival_ptr 314 = tswapal(info->_sifields._rt._sigval.sival_ptr); 315 } 316 } 317 318 319 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) 320 { 321 host_to_target_siginfo_noswap(tinfo, info); 322 tswap_siginfo(tinfo, tinfo); 323 } 324 325 /* XXX: we support only POSIX RT signals are used. */ 326 /* XXX: find a solution for 64 bit (additional malloced data is needed) */ 327 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) 328 { 329 info->si_signo = tswap32(tinfo->si_signo); 330 info->si_errno = tswap32(tinfo->si_errno); 331 info->si_code = tswap32(tinfo->si_code); 332 info->si_pid = tswap32(tinfo->_sifields._rt._pid); 333 info->si_uid = tswap32(tinfo->_sifields._rt._uid); 334 info->si_value.sival_ptr = 335 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr); 336 } 337 338 static int fatal_signal (int sig) 339 { 340 switch (sig) { 341 case TARGET_SIGCHLD: 342 case TARGET_SIGURG: 343 case TARGET_SIGWINCH: 344 /* Ignored by default. */ 345 return 0; 346 case TARGET_SIGCONT: 347 case TARGET_SIGSTOP: 348 case TARGET_SIGTSTP: 349 case TARGET_SIGTTIN: 350 case TARGET_SIGTTOU: 351 /* Job control signals. */ 352 return 0; 353 default: 354 return 1; 355 } 356 } 357 358 /* returns 1 if given signal should dump core if not handled */ 359 static int core_dump_signal(int sig) 360 { 361 switch (sig) { 362 case TARGET_SIGABRT: 363 case TARGET_SIGFPE: 364 case TARGET_SIGILL: 365 case TARGET_SIGQUIT: 366 case TARGET_SIGSEGV: 367 case TARGET_SIGTRAP: 368 case TARGET_SIGBUS: 369 return (1); 370 default: 371 return (0); 372 } 373 } 374 375 void signal_init(void) 376 { 377 struct sigaction act; 378 struct sigaction oact; 379 int i, j; 380 int host_sig; 381 382 /* generate signal conversion tables */ 383 for(i = 1; i < _NSIG; i++) { 384 if (host_to_target_signal_table[i] == 0) 385 host_to_target_signal_table[i] = i; 386 } 387 for(i = 1; i < _NSIG; i++) { 388 j = host_to_target_signal_table[i]; 389 target_to_host_signal_table[j] = i; 390 } 391 392 /* set all host signal handlers. ALL signals are blocked during 393 the handlers to serialize them. */ 394 memset(sigact_table, 0, sizeof(sigact_table)); 395 396 sigfillset(&act.sa_mask); 397 act.sa_flags = SA_SIGINFO; 398 act.sa_sigaction = host_signal_handler; 399 for(i = 1; i <= TARGET_NSIG; i++) { 400 host_sig = target_to_host_signal(i); 401 sigaction(host_sig, NULL, &oact); 402 if (oact.sa_sigaction == (void *)SIG_IGN) { 403 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN; 404 } else if (oact.sa_sigaction == (void *)SIG_DFL) { 405 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL; 406 } 407 /* If there's already a handler installed then something has 408 gone horribly wrong, so don't even try to handle that case. */ 409 /* Install some handlers for our own use. We need at least 410 SIGSEGV and SIGBUS, to detect exceptions. We can not just 411 trap all signals because it affects syscall interrupt 412 behavior. But do trap all default-fatal signals. */ 413 if (fatal_signal (i)) 414 sigaction(host_sig, &act, NULL); 415 } 416 } 417 418 /* signal queue handling */ 419 420 static inline struct sigqueue *alloc_sigqueue(CPUArchState *env) 421 { 422 CPUState *cpu = ENV_GET_CPU(env); 423 TaskState *ts = cpu->opaque; 424 struct sigqueue *q = ts->first_free; 425 if (!q) 426 return NULL; 427 ts->first_free = q->next; 428 return q; 429 } 430 431 static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q) 432 { 433 CPUState *cpu = ENV_GET_CPU(env); 434 TaskState *ts = cpu->opaque; 435 436 q->next = ts->first_free; 437 ts->first_free = q; 438 } 439 440 /* abort execution with signal */ 441 static void QEMU_NORETURN force_sig(int target_sig) 442 { 443 CPUState *cpu = thread_cpu; 444 CPUArchState *env = cpu->env_ptr; 445 TaskState *ts = (TaskState *)cpu->opaque; 446 int host_sig, core_dumped = 0; 447 struct sigaction act; 448 host_sig = target_to_host_signal(target_sig); 449 gdb_signalled(env, target_sig); 450 451 /* dump core if supported by target binary format */ 452 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) { 453 stop_all_tasks(); 454 core_dumped = 455 ((*ts->bprm->core_dump)(target_sig, env) == 0); 456 } 457 if (core_dumped) { 458 /* we already dumped the core of target process, we don't want 459 * a coredump of qemu itself */ 460 struct rlimit nodump; 461 getrlimit(RLIMIT_CORE, &nodump); 462 nodump.rlim_cur=0; 463 setrlimit(RLIMIT_CORE, &nodump); 464 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n", 465 target_sig, strsignal(host_sig), "core dumped" ); 466 } 467 468 /* The proper exit code for dying from an uncaught signal is 469 * -<signal>. The kernel doesn't allow exit() or _exit() to pass 470 * a negative value. To get the proper exit code we need to 471 * actually die from an uncaught signal. Here the default signal 472 * handler is installed, we send ourself a signal and we wait for 473 * it to arrive. */ 474 sigfillset(&act.sa_mask); 475 act.sa_handler = SIG_DFL; 476 act.sa_flags = 0; 477 sigaction(host_sig, &act, NULL); 478 479 /* For some reason raise(host_sig) doesn't send the signal when 480 * statically linked on x86-64. */ 481 kill(getpid(), host_sig); 482 483 /* Make sure the signal isn't masked (just reuse the mask inside 484 of act) */ 485 sigdelset(&act.sa_mask, host_sig); 486 sigsuspend(&act.sa_mask); 487 488 /* unreachable */ 489 abort(); 490 } 491 492 /* queue a signal so that it will be send to the virtual CPU as soon 493 as possible */ 494 int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info) 495 { 496 CPUState *cpu = ENV_GET_CPU(env); 497 TaskState *ts = cpu->opaque; 498 struct emulated_sigtable *k; 499 struct sigqueue *q, **pq; 500 abi_ulong handler; 501 int queue; 502 503 #if defined(DEBUG_SIGNAL) 504 fprintf(stderr, "queue_signal: sig=%d\n", 505 sig); 506 #endif 507 k = &ts->sigtab[sig - 1]; 508 queue = gdb_queuesig (); 509 handler = sigact_table[sig - 1]._sa_handler; 510 511 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) { 512 /* Guest has blocked SIGSEGV but we got one anyway. Assume this 513 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info 514 * because it got a real MMU fault). A blocked SIGSEGV in that 515 * situation is treated as if using the default handler. This is 516 * not correct if some other process has randomly sent us a SIGSEGV 517 * via kill(), but that is not easy to distinguish at this point, 518 * so we assume it doesn't happen. 519 */ 520 handler = TARGET_SIG_DFL; 521 } 522 523 if (!queue && handler == TARGET_SIG_DFL) { 524 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) { 525 kill(getpid(),SIGSTOP); 526 return 0; 527 } else 528 /* default handler : ignore some signal. The other are fatal */ 529 if (sig != TARGET_SIGCHLD && 530 sig != TARGET_SIGURG && 531 sig != TARGET_SIGWINCH && 532 sig != TARGET_SIGCONT) { 533 force_sig(sig); 534 } else { 535 return 0; /* indicate ignored */ 536 } 537 } else if (!queue && handler == TARGET_SIG_IGN) { 538 /* ignore signal */ 539 return 0; 540 } else if (!queue && handler == TARGET_SIG_ERR) { 541 force_sig(sig); 542 } else { 543 pq = &k->first; 544 if (sig < TARGET_SIGRTMIN) { 545 /* if non real time signal, we queue exactly one signal */ 546 if (!k->pending) 547 q = &k->info; 548 else 549 return 0; 550 } else { 551 if (!k->pending) { 552 /* first signal */ 553 q = &k->info; 554 } else { 555 q = alloc_sigqueue(env); 556 if (!q) 557 return -EAGAIN; 558 while (*pq != NULL) 559 pq = &(*pq)->next; 560 } 561 } 562 *pq = q; 563 q->info = *info; 564 q->next = NULL; 565 k->pending = 1; 566 /* signal that a new signal is pending */ 567 ts->signal_pending = 1; 568 return 1; /* indicates that the signal was queued */ 569 } 570 } 571 572 static void host_signal_handler(int host_signum, siginfo_t *info, 573 void *puc) 574 { 575 CPUArchState *env = thread_cpu->env_ptr; 576 int sig; 577 target_siginfo_t tinfo; 578 579 /* the CPU emulator uses some host signals to detect exceptions, 580 we forward to it some signals */ 581 if ((host_signum == SIGSEGV || host_signum == SIGBUS) 582 && info->si_code > 0) { 583 if (cpu_signal_handler(host_signum, info, puc)) 584 return; 585 } 586 587 /* get target signal number */ 588 sig = host_to_target_signal(host_signum); 589 if (sig < 1 || sig > TARGET_NSIG) 590 return; 591 #if defined(DEBUG_SIGNAL) 592 fprintf(stderr, "qemu: got signal %d\n", sig); 593 #endif 594 host_to_target_siginfo_noswap(&tinfo, info); 595 if (queue_signal(env, sig, &tinfo) == 1) { 596 /* interrupt the virtual CPU as soon as possible */ 597 cpu_exit(thread_cpu); 598 } 599 } 600 601 /* do_sigaltstack() returns target values and errnos. */ 602 /* compare linux/kernel/signal.c:do_sigaltstack() */ 603 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp) 604 { 605 int ret; 606 struct target_sigaltstack oss; 607 608 /* XXX: test errors */ 609 if(uoss_addr) 610 { 611 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp); 612 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size); 613 __put_user(sas_ss_flags(sp), &oss.ss_flags); 614 } 615 616 if(uss_addr) 617 { 618 struct target_sigaltstack *uss; 619 struct target_sigaltstack ss; 620 621 ret = -TARGET_EFAULT; 622 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1) 623 || __get_user(ss.ss_sp, &uss->ss_sp) 624 || __get_user(ss.ss_size, &uss->ss_size) 625 || __get_user(ss.ss_flags, &uss->ss_flags)) 626 goto out; 627 unlock_user_struct(uss, uss_addr, 0); 628 629 ret = -TARGET_EPERM; 630 if (on_sig_stack(sp)) 631 goto out; 632 633 ret = -TARGET_EINVAL; 634 if (ss.ss_flags != TARGET_SS_DISABLE 635 && ss.ss_flags != TARGET_SS_ONSTACK 636 && ss.ss_flags != 0) 637 goto out; 638 639 if (ss.ss_flags == TARGET_SS_DISABLE) { 640 ss.ss_size = 0; 641 ss.ss_sp = 0; 642 } else { 643 ret = -TARGET_ENOMEM; 644 if (ss.ss_size < MINSIGSTKSZ) 645 goto out; 646 } 647 648 target_sigaltstack_used.ss_sp = ss.ss_sp; 649 target_sigaltstack_used.ss_size = ss.ss_size; 650 } 651 652 if (uoss_addr) { 653 ret = -TARGET_EFAULT; 654 if (copy_to_user(uoss_addr, &oss, sizeof(oss))) 655 goto out; 656 } 657 658 ret = 0; 659 out: 660 return ret; 661 } 662 663 /* do_sigaction() return host values and errnos */ 664 int do_sigaction(int sig, const struct target_sigaction *act, 665 struct target_sigaction *oact) 666 { 667 struct target_sigaction *k; 668 struct sigaction act1; 669 int host_sig; 670 int ret = 0; 671 672 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) 673 return -EINVAL; 674 k = &sigact_table[sig - 1]; 675 #if defined(DEBUG_SIGNAL) 676 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n", 677 sig, act, oact); 678 #endif 679 if (oact) { 680 __put_user(k->_sa_handler, &oact->_sa_handler); 681 __put_user(k->sa_flags, &oact->sa_flags); 682 #if !defined(TARGET_MIPS) 683 __put_user(k->sa_restorer, &oact->sa_restorer); 684 #endif 685 /* Not swapped. */ 686 oact->sa_mask = k->sa_mask; 687 } 688 if (act) { 689 /* FIXME: This is not threadsafe. */ 690 __get_user(k->_sa_handler, &act->_sa_handler); 691 __get_user(k->sa_flags, &act->sa_flags); 692 #if !defined(TARGET_MIPS) 693 __get_user(k->sa_restorer, &act->sa_restorer); 694 #endif 695 /* To be swapped in target_to_host_sigset. */ 696 k->sa_mask = act->sa_mask; 697 698 /* we update the host linux signal state */ 699 host_sig = target_to_host_signal(sig); 700 if (host_sig != SIGSEGV && host_sig != SIGBUS) { 701 sigfillset(&act1.sa_mask); 702 act1.sa_flags = SA_SIGINFO; 703 if (k->sa_flags & TARGET_SA_RESTART) 704 act1.sa_flags |= SA_RESTART; 705 /* NOTE: it is important to update the host kernel signal 706 ignore state to avoid getting unexpected interrupted 707 syscalls */ 708 if (k->_sa_handler == TARGET_SIG_IGN) { 709 act1.sa_sigaction = (void *)SIG_IGN; 710 } else if (k->_sa_handler == TARGET_SIG_DFL) { 711 if (fatal_signal (sig)) 712 act1.sa_sigaction = host_signal_handler; 713 else 714 act1.sa_sigaction = (void *)SIG_DFL; 715 } else { 716 act1.sa_sigaction = host_signal_handler; 717 } 718 ret = sigaction(host_sig, &act1, NULL); 719 } 720 } 721 return ret; 722 } 723 724 static inline void copy_siginfo_to_user(target_siginfo_t *tinfo, 725 const target_siginfo_t *info) 726 { 727 tswap_siginfo(tinfo, info); 728 } 729 730 static inline int current_exec_domain_sig(int sig) 731 { 732 return /* current->exec_domain && current->exec_domain->signal_invmap 733 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig; 734 } 735 736 #if defined(TARGET_I386) && TARGET_ABI_BITS == 32 737 738 /* from the Linux kernel */ 739 740 struct target_fpreg { 741 uint16_t significand[4]; 742 uint16_t exponent; 743 }; 744 745 struct target_fpxreg { 746 uint16_t significand[4]; 747 uint16_t exponent; 748 uint16_t padding[3]; 749 }; 750 751 struct target_xmmreg { 752 abi_ulong element[4]; 753 }; 754 755 struct target_fpstate { 756 /* Regular FPU environment */ 757 abi_ulong cw; 758 abi_ulong sw; 759 abi_ulong tag; 760 abi_ulong ipoff; 761 abi_ulong cssel; 762 abi_ulong dataoff; 763 abi_ulong datasel; 764 struct target_fpreg _st[8]; 765 uint16_t status; 766 uint16_t magic; /* 0xffff = regular FPU data only */ 767 768 /* FXSR FPU environment */ 769 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */ 770 abi_ulong mxcsr; 771 abi_ulong reserved; 772 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */ 773 struct target_xmmreg _xmm[8]; 774 abi_ulong padding[56]; 775 }; 776 777 #define X86_FXSR_MAGIC 0x0000 778 779 struct target_sigcontext { 780 uint16_t gs, __gsh; 781 uint16_t fs, __fsh; 782 uint16_t es, __esh; 783 uint16_t ds, __dsh; 784 abi_ulong edi; 785 abi_ulong esi; 786 abi_ulong ebp; 787 abi_ulong esp; 788 abi_ulong ebx; 789 abi_ulong edx; 790 abi_ulong ecx; 791 abi_ulong eax; 792 abi_ulong trapno; 793 abi_ulong err; 794 abi_ulong eip; 795 uint16_t cs, __csh; 796 abi_ulong eflags; 797 abi_ulong esp_at_signal; 798 uint16_t ss, __ssh; 799 abi_ulong fpstate; /* pointer */ 800 abi_ulong oldmask; 801 abi_ulong cr2; 802 }; 803 804 struct target_ucontext { 805 abi_ulong tuc_flags; 806 abi_ulong tuc_link; 807 target_stack_t tuc_stack; 808 struct target_sigcontext tuc_mcontext; 809 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 810 }; 811 812 struct sigframe 813 { 814 abi_ulong pretcode; 815 int sig; 816 struct target_sigcontext sc; 817 struct target_fpstate fpstate; 818 abi_ulong extramask[TARGET_NSIG_WORDS-1]; 819 char retcode[8]; 820 }; 821 822 struct rt_sigframe 823 { 824 abi_ulong pretcode; 825 int sig; 826 abi_ulong pinfo; 827 abi_ulong puc; 828 struct target_siginfo info; 829 struct target_ucontext uc; 830 struct target_fpstate fpstate; 831 char retcode[8]; 832 }; 833 834 /* 835 * Set up a signal frame. 836 */ 837 838 /* XXX: save x87 state */ 839 static void setup_sigcontext(struct target_sigcontext *sc, 840 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask, 841 abi_ulong fpstate_addr) 842 { 843 CPUState *cs = CPU(x86_env_get_cpu(env)); 844 uint16_t magic; 845 846 /* already locked in setup_frame() */ 847 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs); 848 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs); 849 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es); 850 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds); 851 __put_user(env->regs[R_EDI], &sc->edi); 852 __put_user(env->regs[R_ESI], &sc->esi); 853 __put_user(env->regs[R_EBP], &sc->ebp); 854 __put_user(env->regs[R_ESP], &sc->esp); 855 __put_user(env->regs[R_EBX], &sc->ebx); 856 __put_user(env->regs[R_EDX], &sc->edx); 857 __put_user(env->regs[R_ECX], &sc->ecx); 858 __put_user(env->regs[R_EAX], &sc->eax); 859 __put_user(cs->exception_index, &sc->trapno); 860 __put_user(env->error_code, &sc->err); 861 __put_user(env->eip, &sc->eip); 862 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs); 863 __put_user(env->eflags, &sc->eflags); 864 __put_user(env->regs[R_ESP], &sc->esp_at_signal); 865 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss); 866 867 cpu_x86_fsave(env, fpstate_addr, 1); 868 fpstate->status = fpstate->sw; 869 magic = 0xffff; 870 __put_user(magic, &fpstate->magic); 871 __put_user(fpstate_addr, &sc->fpstate); 872 873 /* non-iBCS2 extensions.. */ 874 __put_user(mask, &sc->oldmask); 875 __put_user(env->cr[2], &sc->cr2); 876 } 877 878 /* 879 * Determine which stack to use.. 880 */ 881 882 static inline abi_ulong 883 get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size) 884 { 885 unsigned long esp; 886 887 /* Default to using normal stack */ 888 esp = env->regs[R_ESP]; 889 /* This is the X/Open sanctioned signal stack switching. */ 890 if (ka->sa_flags & TARGET_SA_ONSTACK) { 891 if (sas_ss_flags(esp) == 0) 892 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 893 } 894 895 /* This is the legacy signal stack switching. */ 896 else 897 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS && 898 !(ka->sa_flags & TARGET_SA_RESTORER) && 899 ka->sa_restorer) { 900 esp = (unsigned long) ka->sa_restorer; 901 } 902 return (esp - frame_size) & -8ul; 903 } 904 905 /* compare linux/arch/i386/kernel/signal.c:setup_frame() */ 906 static void setup_frame(int sig, struct target_sigaction *ka, 907 target_sigset_t *set, CPUX86State *env) 908 { 909 abi_ulong frame_addr; 910 struct sigframe *frame; 911 int i; 912 913 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 914 915 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 916 goto give_sigsegv; 917 918 __put_user(current_exec_domain_sig(sig), 919 &frame->sig); 920 921 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0], 922 frame_addr + offsetof(struct sigframe, fpstate)); 923 924 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 925 __put_user(set->sig[i], &frame->extramask[i - 1]); 926 } 927 928 /* Set up to return from userspace. If provided, use a stub 929 already in userspace. */ 930 if (ka->sa_flags & TARGET_SA_RESTORER) { 931 __put_user(ka->sa_restorer, &frame->pretcode); 932 } else { 933 uint16_t val16; 934 abi_ulong retcode_addr; 935 retcode_addr = frame_addr + offsetof(struct sigframe, retcode); 936 __put_user(retcode_addr, &frame->pretcode); 937 /* This is popl %eax ; movl $,%eax ; int $0x80 */ 938 val16 = 0xb858; 939 __put_user(val16, (uint16_t *)(frame->retcode+0)); 940 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2)); 941 val16 = 0x80cd; 942 __put_user(val16, (uint16_t *)(frame->retcode+6)); 943 } 944 945 946 /* Set up registers for signal handler */ 947 env->regs[R_ESP] = frame_addr; 948 env->eip = ka->_sa_handler; 949 950 cpu_x86_load_seg(env, R_DS, __USER_DS); 951 cpu_x86_load_seg(env, R_ES, __USER_DS); 952 cpu_x86_load_seg(env, R_SS, __USER_DS); 953 cpu_x86_load_seg(env, R_CS, __USER_CS); 954 env->eflags &= ~TF_MASK; 955 956 unlock_user_struct(frame, frame_addr, 1); 957 958 return; 959 960 give_sigsegv: 961 if (sig == TARGET_SIGSEGV) 962 ka->_sa_handler = TARGET_SIG_DFL; 963 force_sig(TARGET_SIGSEGV /* , current */); 964 } 965 966 /* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */ 967 static void setup_rt_frame(int sig, struct target_sigaction *ka, 968 target_siginfo_t *info, 969 target_sigset_t *set, CPUX86State *env) 970 { 971 abi_ulong frame_addr, addr; 972 struct rt_sigframe *frame; 973 int i, err = 0; 974 975 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 976 977 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 978 goto give_sigsegv; 979 980 __put_user(current_exec_domain_sig(sig), &frame->sig); 981 addr = frame_addr + offsetof(struct rt_sigframe, info); 982 __put_user(addr, &frame->pinfo); 983 addr = frame_addr + offsetof(struct rt_sigframe, uc); 984 __put_user(addr, &frame->puc); 985 copy_siginfo_to_user(&frame->info, info); 986 987 /* Create the ucontext. */ 988 __put_user(0, &frame->uc.tuc_flags); 989 __put_user(0, &frame->uc.tuc_link); 990 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp); 991 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), 992 &frame->uc.tuc_stack.ss_flags); 993 __put_user(target_sigaltstack_used.ss_size, 994 &frame->uc.tuc_stack.ss_size); 995 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env, 996 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate)); 997 998 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 999 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i])) 1000 goto give_sigsegv; 1001 } 1002 1003 /* Set up to return from userspace. If provided, use a stub 1004 already in userspace. */ 1005 if (ka->sa_flags & TARGET_SA_RESTORER) { 1006 __put_user(ka->sa_restorer, &frame->pretcode); 1007 } else { 1008 uint16_t val16; 1009 addr = frame_addr + offsetof(struct rt_sigframe, retcode); 1010 __put_user(addr, &frame->pretcode); 1011 /* This is movl $,%eax ; int $0x80 */ 1012 __put_user(0xb8, (char *)(frame->retcode+0)); 1013 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1)); 1014 val16 = 0x80cd; 1015 __put_user(val16, (uint16_t *)(frame->retcode+5)); 1016 } 1017 1018 if (err) 1019 goto give_sigsegv; 1020 1021 /* Set up registers for signal handler */ 1022 env->regs[R_ESP] = frame_addr; 1023 env->eip = ka->_sa_handler; 1024 1025 cpu_x86_load_seg(env, R_DS, __USER_DS); 1026 cpu_x86_load_seg(env, R_ES, __USER_DS); 1027 cpu_x86_load_seg(env, R_SS, __USER_DS); 1028 cpu_x86_load_seg(env, R_CS, __USER_CS); 1029 env->eflags &= ~TF_MASK; 1030 1031 unlock_user_struct(frame, frame_addr, 1); 1032 1033 return; 1034 1035 give_sigsegv: 1036 unlock_user_struct(frame, frame_addr, 1); 1037 if (sig == TARGET_SIGSEGV) 1038 ka->_sa_handler = TARGET_SIG_DFL; 1039 force_sig(TARGET_SIGSEGV /* , current */); 1040 } 1041 1042 static int 1043 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax) 1044 { 1045 unsigned int err = 0; 1046 abi_ulong fpstate_addr; 1047 unsigned int tmpflags; 1048 1049 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs)); 1050 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs)); 1051 cpu_x86_load_seg(env, R_ES, tswap16(sc->es)); 1052 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds)); 1053 1054 env->regs[R_EDI] = tswapl(sc->edi); 1055 env->regs[R_ESI] = tswapl(sc->esi); 1056 env->regs[R_EBP] = tswapl(sc->ebp); 1057 env->regs[R_ESP] = tswapl(sc->esp); 1058 env->regs[R_EBX] = tswapl(sc->ebx); 1059 env->regs[R_EDX] = tswapl(sc->edx); 1060 env->regs[R_ECX] = tswapl(sc->ecx); 1061 env->eip = tswapl(sc->eip); 1062 1063 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3); 1064 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3); 1065 1066 tmpflags = tswapl(sc->eflags); 1067 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); 1068 // regs->orig_eax = -1; /* disable syscall checks */ 1069 1070 fpstate_addr = tswapl(sc->fpstate); 1071 if (fpstate_addr != 0) { 1072 if (!access_ok(VERIFY_READ, fpstate_addr, 1073 sizeof(struct target_fpstate))) 1074 goto badframe; 1075 cpu_x86_frstor(env, fpstate_addr, 1); 1076 } 1077 1078 *peax = tswapl(sc->eax); 1079 return err; 1080 badframe: 1081 return 1; 1082 } 1083 1084 long do_sigreturn(CPUX86State *env) 1085 { 1086 struct sigframe *frame; 1087 abi_ulong frame_addr = env->regs[R_ESP] - 8; 1088 target_sigset_t target_set; 1089 sigset_t set; 1090 int eax, i; 1091 1092 #if defined(DEBUG_SIGNAL) 1093 fprintf(stderr, "do_sigreturn\n"); 1094 #endif 1095 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 1096 goto badframe; 1097 /* set blocked signals */ 1098 if (__get_user(target_set.sig[0], &frame->sc.oldmask)) 1099 goto badframe; 1100 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 1101 if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) 1102 goto badframe; 1103 } 1104 1105 target_to_host_sigset_internal(&set, &target_set); 1106 do_sigprocmask(SIG_SETMASK, &set, NULL); 1107 1108 /* restore registers */ 1109 if (restore_sigcontext(env, &frame->sc, &eax)) 1110 goto badframe; 1111 unlock_user_struct(frame, frame_addr, 0); 1112 return eax; 1113 1114 badframe: 1115 unlock_user_struct(frame, frame_addr, 0); 1116 force_sig(TARGET_SIGSEGV); 1117 return 0; 1118 } 1119 1120 long do_rt_sigreturn(CPUX86State *env) 1121 { 1122 abi_ulong frame_addr; 1123 struct rt_sigframe *frame; 1124 sigset_t set; 1125 int eax; 1126 1127 frame_addr = env->regs[R_ESP] - 4; 1128 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 1129 goto badframe; 1130 target_to_host_sigset(&set, &frame->uc.tuc_sigmask); 1131 do_sigprocmask(SIG_SETMASK, &set, NULL); 1132 1133 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax)) 1134 goto badframe; 1135 1136 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0, 1137 get_sp_from_cpustate(env)) == -EFAULT) 1138 goto badframe; 1139 1140 unlock_user_struct(frame, frame_addr, 0); 1141 return eax; 1142 1143 badframe: 1144 unlock_user_struct(frame, frame_addr, 0); 1145 force_sig(TARGET_SIGSEGV); 1146 return 0; 1147 } 1148 1149 #elif defined(TARGET_AARCH64) 1150 1151 struct target_sigcontext { 1152 uint64_t fault_address; 1153 /* AArch64 registers */ 1154 uint64_t regs[31]; 1155 uint64_t sp; 1156 uint64_t pc; 1157 uint64_t pstate; 1158 /* 4K reserved for FP/SIMD state and future expansion */ 1159 char __reserved[4096] __attribute__((__aligned__(16))); 1160 }; 1161 1162 struct target_ucontext { 1163 abi_ulong tuc_flags; 1164 abi_ulong tuc_link; 1165 target_stack_t tuc_stack; 1166 target_sigset_t tuc_sigmask; 1167 /* glibc uses a 1024-bit sigset_t */ 1168 char __unused[1024 / 8 - sizeof(target_sigset_t)]; 1169 /* last for future expansion */ 1170 struct target_sigcontext tuc_mcontext; 1171 }; 1172 1173 /* 1174 * Header to be used at the beginning of structures extending the user 1175 * context. Such structures must be placed after the rt_sigframe on the stack 1176 * and be 16-byte aligned. The last structure must be a dummy one with the 1177 * magic and size set to 0. 1178 */ 1179 struct target_aarch64_ctx { 1180 uint32_t magic; 1181 uint32_t size; 1182 }; 1183 1184 #define TARGET_FPSIMD_MAGIC 0x46508001 1185 1186 struct target_fpsimd_context { 1187 struct target_aarch64_ctx head; 1188 uint32_t fpsr; 1189 uint32_t fpcr; 1190 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */ 1191 }; 1192 1193 /* 1194 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to 1195 * user space as it will change with the addition of new context. User space 1196 * should check the magic/size information. 1197 */ 1198 struct target_aux_context { 1199 struct target_fpsimd_context fpsimd; 1200 /* additional context to be added before "end" */ 1201 struct target_aarch64_ctx end; 1202 }; 1203 1204 struct target_rt_sigframe { 1205 struct target_siginfo info; 1206 struct target_ucontext uc; 1207 uint64_t fp; 1208 uint64_t lr; 1209 uint32_t tramp[2]; 1210 }; 1211 1212 static int target_setup_sigframe(struct target_rt_sigframe *sf, 1213 CPUARMState *env, target_sigset_t *set) 1214 { 1215 int i; 1216 struct target_aux_context *aux = 1217 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved; 1218 1219 /* set up the stack frame for unwinding */ 1220 __put_user(env->xregs[29], &sf->fp); 1221 __put_user(env->xregs[30], &sf->lr); 1222 1223 for (i = 0; i < 31; i++) { 1224 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); 1225 } 1226 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp); 1227 __put_user(env->pc, &sf->uc.tuc_mcontext.pc); 1228 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate); 1229 1230 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address); 1231 1232 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 1233 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]); 1234 } 1235 1236 for (i = 0; i < 32; i++) { 1237 #ifdef TARGET_WORDS_BIGENDIAN 1238 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]); 1239 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]); 1240 #else 1241 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]); 1242 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]); 1243 #endif 1244 } 1245 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr); 1246 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr); 1247 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic); 1248 __put_user(sizeof(struct target_fpsimd_context), 1249 &aux->fpsimd.head.size); 1250 1251 /* set the "end" magic */ 1252 __put_user(0, &aux->end.magic); 1253 __put_user(0, &aux->end.size); 1254 1255 return 0; 1256 } 1257 1258 static int target_restore_sigframe(CPUARMState *env, 1259 struct target_rt_sigframe *sf) 1260 { 1261 sigset_t set; 1262 int i; 1263 struct target_aux_context *aux = 1264 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved; 1265 uint32_t magic, size, fpsr, fpcr; 1266 uint64_t pstate; 1267 1268 target_to_host_sigset(&set, &sf->uc.tuc_sigmask); 1269 do_sigprocmask(SIG_SETMASK, &set, NULL); 1270 1271 for (i = 0; i < 31; i++) { 1272 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]); 1273 } 1274 1275 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp); 1276 __get_user(env->pc, &sf->uc.tuc_mcontext.pc); 1277 __get_user(pstate, &sf->uc.tuc_mcontext.pstate); 1278 pstate_write(env, pstate); 1279 1280 __get_user(magic, &aux->fpsimd.head.magic); 1281 __get_user(size, &aux->fpsimd.head.size); 1282 1283 if (magic != TARGET_FPSIMD_MAGIC 1284 || size != sizeof(struct target_fpsimd_context)) { 1285 return 1; 1286 } 1287 1288 for (i = 0; i < 32; i++) { 1289 #ifdef TARGET_WORDS_BIGENDIAN 1290 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]); 1291 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]); 1292 #else 1293 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]); 1294 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]); 1295 #endif 1296 } 1297 __get_user(fpsr, &aux->fpsimd.fpsr); 1298 vfp_set_fpsr(env, fpsr); 1299 __get_user(fpcr, &aux->fpsimd.fpcr); 1300 vfp_set_fpcr(env, fpcr); 1301 1302 return 0; 1303 } 1304 1305 static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env) 1306 { 1307 abi_ulong sp; 1308 1309 sp = env->xregs[31]; 1310 1311 /* 1312 * This is the X/Open sanctioned signal stack switching. 1313 */ 1314 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) { 1315 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 1316 } 1317 1318 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15; 1319 1320 return sp; 1321 } 1322 1323 static void target_setup_frame(int usig, struct target_sigaction *ka, 1324 target_siginfo_t *info, target_sigset_t *set, 1325 CPUARMState *env) 1326 { 1327 struct target_rt_sigframe *frame; 1328 abi_ulong frame_addr, return_addr; 1329 1330 frame_addr = get_sigframe(ka, env); 1331 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 1332 goto give_sigsegv; 1333 } 1334 1335 __put_user(0, &frame->uc.tuc_flags); 1336 __put_user(0, &frame->uc.tuc_link); 1337 1338 __put_user(target_sigaltstack_used.ss_sp, 1339 &frame->uc.tuc_stack.ss_sp); 1340 __put_user(sas_ss_flags(env->xregs[31]), 1341 &frame->uc.tuc_stack.ss_flags); 1342 __put_user(target_sigaltstack_used.ss_size, 1343 &frame->uc.tuc_stack.ss_size); 1344 target_setup_sigframe(frame, env, set); 1345 if (ka->sa_flags & TARGET_SA_RESTORER) { 1346 return_addr = ka->sa_restorer; 1347 } else { 1348 /* mov x8,#__NR_rt_sigreturn; svc #0 */ 1349 __put_user(0xd2801168, &frame->tramp[0]); 1350 __put_user(0xd4000001, &frame->tramp[1]); 1351 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp); 1352 } 1353 env->xregs[0] = usig; 1354 env->xregs[31] = frame_addr; 1355 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp); 1356 env->pc = ka->_sa_handler; 1357 env->xregs[30] = return_addr; 1358 if (info) { 1359 copy_siginfo_to_user(&frame->info, info); 1360 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info); 1361 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc); 1362 } 1363 1364 unlock_user_struct(frame, frame_addr, 1); 1365 return; 1366 1367 give_sigsegv: 1368 unlock_user_struct(frame, frame_addr, 1); 1369 force_sig(TARGET_SIGSEGV); 1370 } 1371 1372 static void setup_rt_frame(int sig, struct target_sigaction *ka, 1373 target_siginfo_t *info, target_sigset_t *set, 1374 CPUARMState *env) 1375 { 1376 target_setup_frame(sig, ka, info, set, env); 1377 } 1378 1379 static void setup_frame(int sig, struct target_sigaction *ka, 1380 target_sigset_t *set, CPUARMState *env) 1381 { 1382 target_setup_frame(sig, ka, 0, set, env); 1383 } 1384 1385 long do_rt_sigreturn(CPUARMState *env) 1386 { 1387 struct target_rt_sigframe *frame = NULL; 1388 abi_ulong frame_addr = env->xregs[31]; 1389 1390 if (frame_addr & 15) { 1391 goto badframe; 1392 } 1393 1394 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 1395 goto badframe; 1396 } 1397 1398 if (target_restore_sigframe(env, frame)) { 1399 goto badframe; 1400 } 1401 1402 if (do_sigaltstack(frame_addr + 1403 offsetof(struct target_rt_sigframe, uc.tuc_stack), 1404 0, get_sp_from_cpustate(env)) == -EFAULT) { 1405 goto badframe; 1406 } 1407 1408 unlock_user_struct(frame, frame_addr, 0); 1409 return env->xregs[0]; 1410 1411 badframe: 1412 unlock_user_struct(frame, frame_addr, 0); 1413 force_sig(TARGET_SIGSEGV); 1414 return 0; 1415 } 1416 1417 long do_sigreturn(CPUARMState *env) 1418 { 1419 return do_rt_sigreturn(env); 1420 } 1421 1422 #elif defined(TARGET_ARM) 1423 1424 struct target_sigcontext { 1425 abi_ulong trap_no; 1426 abi_ulong error_code; 1427 abi_ulong oldmask; 1428 abi_ulong arm_r0; 1429 abi_ulong arm_r1; 1430 abi_ulong arm_r2; 1431 abi_ulong arm_r3; 1432 abi_ulong arm_r4; 1433 abi_ulong arm_r5; 1434 abi_ulong arm_r6; 1435 abi_ulong arm_r7; 1436 abi_ulong arm_r8; 1437 abi_ulong arm_r9; 1438 abi_ulong arm_r10; 1439 abi_ulong arm_fp; 1440 abi_ulong arm_ip; 1441 abi_ulong arm_sp; 1442 abi_ulong arm_lr; 1443 abi_ulong arm_pc; 1444 abi_ulong arm_cpsr; 1445 abi_ulong fault_address; 1446 }; 1447 1448 struct target_ucontext_v1 { 1449 abi_ulong tuc_flags; 1450 abi_ulong tuc_link; 1451 target_stack_t tuc_stack; 1452 struct target_sigcontext tuc_mcontext; 1453 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 1454 }; 1455 1456 struct target_ucontext_v2 { 1457 abi_ulong tuc_flags; 1458 abi_ulong tuc_link; 1459 target_stack_t tuc_stack; 1460 struct target_sigcontext tuc_mcontext; 1461 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 1462 char __unused[128 - sizeof(target_sigset_t)]; 1463 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8))); 1464 }; 1465 1466 struct target_user_vfp { 1467 uint64_t fpregs[32]; 1468 abi_ulong fpscr; 1469 }; 1470 1471 struct target_user_vfp_exc { 1472 abi_ulong fpexc; 1473 abi_ulong fpinst; 1474 abi_ulong fpinst2; 1475 }; 1476 1477 struct target_vfp_sigframe { 1478 abi_ulong magic; 1479 abi_ulong size; 1480 struct target_user_vfp ufp; 1481 struct target_user_vfp_exc ufp_exc; 1482 } __attribute__((__aligned__(8))); 1483 1484 struct target_iwmmxt_sigframe { 1485 abi_ulong magic; 1486 abi_ulong size; 1487 uint64_t regs[16]; 1488 /* Note that not all the coprocessor control registers are stored here */ 1489 uint32_t wcssf; 1490 uint32_t wcasf; 1491 uint32_t wcgr0; 1492 uint32_t wcgr1; 1493 uint32_t wcgr2; 1494 uint32_t wcgr3; 1495 } __attribute__((__aligned__(8))); 1496 1497 #define TARGET_VFP_MAGIC 0x56465001 1498 #define TARGET_IWMMXT_MAGIC 0x12ef842a 1499 1500 struct sigframe_v1 1501 { 1502 struct target_sigcontext sc; 1503 abi_ulong extramask[TARGET_NSIG_WORDS-1]; 1504 abi_ulong retcode; 1505 }; 1506 1507 struct sigframe_v2 1508 { 1509 struct target_ucontext_v2 uc; 1510 abi_ulong retcode; 1511 }; 1512 1513 struct rt_sigframe_v1 1514 { 1515 abi_ulong pinfo; 1516 abi_ulong puc; 1517 struct target_siginfo info; 1518 struct target_ucontext_v1 uc; 1519 abi_ulong retcode; 1520 }; 1521 1522 struct rt_sigframe_v2 1523 { 1524 struct target_siginfo info; 1525 struct target_ucontext_v2 uc; 1526 abi_ulong retcode; 1527 }; 1528 1529 #define TARGET_CONFIG_CPU_32 1 1530 1531 /* 1532 * For ARM syscalls, we encode the syscall number into the instruction. 1533 */ 1534 #define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE)) 1535 #define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE)) 1536 1537 /* 1538 * For Thumb syscalls, we pass the syscall number via r7. We therefore 1539 * need two 16-bit instructions. 1540 */ 1541 #define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn)) 1542 #define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn)) 1543 1544 static const abi_ulong retcodes[4] = { 1545 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN, 1546 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN 1547 }; 1548 1549 1550 static inline int valid_user_regs(CPUARMState *regs) 1551 { 1552 return 1; 1553 } 1554 1555 static void 1556 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ 1557 CPUARMState *env, abi_ulong mask) 1558 { 1559 __put_user(env->regs[0], &sc->arm_r0); 1560 __put_user(env->regs[1], &sc->arm_r1); 1561 __put_user(env->regs[2], &sc->arm_r2); 1562 __put_user(env->regs[3], &sc->arm_r3); 1563 __put_user(env->regs[4], &sc->arm_r4); 1564 __put_user(env->regs[5], &sc->arm_r5); 1565 __put_user(env->regs[6], &sc->arm_r6); 1566 __put_user(env->regs[7], &sc->arm_r7); 1567 __put_user(env->regs[8], &sc->arm_r8); 1568 __put_user(env->regs[9], &sc->arm_r9); 1569 __put_user(env->regs[10], &sc->arm_r10); 1570 __put_user(env->regs[11], &sc->arm_fp); 1571 __put_user(env->regs[12], &sc->arm_ip); 1572 __put_user(env->regs[13], &sc->arm_sp); 1573 __put_user(env->regs[14], &sc->arm_lr); 1574 __put_user(env->regs[15], &sc->arm_pc); 1575 #ifdef TARGET_CONFIG_CPU_32 1576 __put_user(cpsr_read(env), &sc->arm_cpsr); 1577 #endif 1578 1579 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no); 1580 __put_user(/* current->thread.error_code */ 0, &sc->error_code); 1581 __put_user(/* current->thread.address */ 0, &sc->fault_address); 1582 __put_user(mask, &sc->oldmask); 1583 } 1584 1585 static inline abi_ulong 1586 get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize) 1587 { 1588 unsigned long sp = regs->regs[13]; 1589 1590 /* 1591 * This is the X/Open sanctioned signal stack switching. 1592 */ 1593 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) 1594 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 1595 /* 1596 * ATPCS B01 mandates 8-byte alignment 1597 */ 1598 return (sp - framesize) & ~7; 1599 } 1600 1601 static int 1602 setup_return(CPUARMState *env, struct target_sigaction *ka, 1603 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr) 1604 { 1605 abi_ulong handler = ka->_sa_handler; 1606 abi_ulong retcode; 1607 int thumb = handler & 1; 1608 uint32_t cpsr = cpsr_read(env); 1609 1610 cpsr &= ~CPSR_IT; 1611 if (thumb) { 1612 cpsr |= CPSR_T; 1613 } else { 1614 cpsr &= ~CPSR_T; 1615 } 1616 1617 if (ka->sa_flags & TARGET_SA_RESTORER) { 1618 retcode = ka->sa_restorer; 1619 } else { 1620 unsigned int idx = thumb; 1621 1622 if (ka->sa_flags & TARGET_SA_SIGINFO) 1623 idx += 2; 1624 1625 if (__put_user(retcodes[idx], rc)) 1626 return 1; 1627 1628 retcode = rc_addr + thumb; 1629 } 1630 1631 env->regs[0] = usig; 1632 env->regs[13] = frame_addr; 1633 env->regs[14] = retcode; 1634 env->regs[15] = handler & (thumb ? ~1 : ~3); 1635 cpsr_write(env, cpsr, 0xffffffff); 1636 1637 return 0; 1638 } 1639 1640 static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env) 1641 { 1642 int i; 1643 struct target_vfp_sigframe *vfpframe; 1644 vfpframe = (struct target_vfp_sigframe *)regspace; 1645 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic); 1646 __put_user(sizeof(*vfpframe), &vfpframe->size); 1647 for (i = 0; i < 32; i++) { 1648 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]); 1649 } 1650 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr); 1651 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc); 1652 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst); 1653 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2); 1654 return (abi_ulong*)(vfpframe+1); 1655 } 1656 1657 static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, 1658 CPUARMState *env) 1659 { 1660 int i; 1661 struct target_iwmmxt_sigframe *iwmmxtframe; 1662 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace; 1663 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic); 1664 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size); 1665 for (i = 0; i < 16; i++) { 1666 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]); 1667 } 1668 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf); 1669 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf); 1670 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0); 1671 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1); 1672 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2); 1673 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3); 1674 return (abi_ulong*)(iwmmxtframe+1); 1675 } 1676 1677 static void setup_sigframe_v2(struct target_ucontext_v2 *uc, 1678 target_sigset_t *set, CPUARMState *env) 1679 { 1680 struct target_sigaltstack stack; 1681 int i; 1682 abi_ulong *regspace; 1683 1684 /* Clear all the bits of the ucontext we don't use. */ 1685 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext)); 1686 1687 memset(&stack, 0, sizeof(stack)); 1688 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp); 1689 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size); 1690 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags); 1691 memcpy(&uc->tuc_stack, &stack, sizeof(stack)); 1692 1693 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]); 1694 /* Save coprocessor signal frame. */ 1695 regspace = uc->tuc_regspace; 1696 if (arm_feature(env, ARM_FEATURE_VFP)) { 1697 regspace = setup_sigframe_v2_vfp(regspace, env); 1698 } 1699 if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 1700 regspace = setup_sigframe_v2_iwmmxt(regspace, env); 1701 } 1702 1703 /* Write terminating magic word */ 1704 __put_user(0, regspace); 1705 1706 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 1707 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]); 1708 } 1709 } 1710 1711 /* compare linux/arch/arm/kernel/signal.c:setup_frame() */ 1712 static void setup_frame_v1(int usig, struct target_sigaction *ka, 1713 target_sigset_t *set, CPUARMState *regs) 1714 { 1715 struct sigframe_v1 *frame; 1716 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame)); 1717 int i; 1718 1719 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 1720 return; 1721 1722 setup_sigcontext(&frame->sc, regs, set->sig[0]); 1723 1724 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 1725 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 1726 goto end; 1727 } 1728 1729 setup_return(regs, ka, &frame->retcode, frame_addr, usig, 1730 frame_addr + offsetof(struct sigframe_v1, retcode)); 1731 1732 end: 1733 unlock_user_struct(frame, frame_addr, 1); 1734 } 1735 1736 static void setup_frame_v2(int usig, struct target_sigaction *ka, 1737 target_sigset_t *set, CPUARMState *regs) 1738 { 1739 struct sigframe_v2 *frame; 1740 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame)); 1741 1742 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 1743 return; 1744 1745 setup_sigframe_v2(&frame->uc, set, regs); 1746 1747 setup_return(regs, ka, &frame->retcode, frame_addr, usig, 1748 frame_addr + offsetof(struct sigframe_v2, retcode)); 1749 1750 unlock_user_struct(frame, frame_addr, 1); 1751 } 1752 1753 static void setup_frame(int usig, struct target_sigaction *ka, 1754 target_sigset_t *set, CPUARMState *regs) 1755 { 1756 if (get_osversion() >= 0x020612) { 1757 setup_frame_v2(usig, ka, set, regs); 1758 } else { 1759 setup_frame_v1(usig, ka, set, regs); 1760 } 1761 } 1762 1763 /* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */ 1764 static void setup_rt_frame_v1(int usig, struct target_sigaction *ka, 1765 target_siginfo_t *info, 1766 target_sigset_t *set, CPUARMState *env) 1767 { 1768 struct rt_sigframe_v1 *frame; 1769 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame)); 1770 struct target_sigaltstack stack; 1771 int i; 1772 abi_ulong info_addr, uc_addr; 1773 1774 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 1775 return /* 1 */; 1776 1777 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info); 1778 __put_user(info_addr, &frame->pinfo); 1779 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc); 1780 __put_user(uc_addr, &frame->puc); 1781 copy_siginfo_to_user(&frame->info, info); 1782 1783 /* Clear all the bits of the ucontext we don't use. */ 1784 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext)); 1785 1786 memset(&stack, 0, sizeof(stack)); 1787 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp); 1788 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size); 1789 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags); 1790 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack)); 1791 1792 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]); 1793 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 1794 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i])) 1795 goto end; 1796 } 1797 1798 setup_return(env, ka, &frame->retcode, frame_addr, usig, 1799 frame_addr + offsetof(struct rt_sigframe_v1, retcode)); 1800 1801 env->regs[1] = info_addr; 1802 env->regs[2] = uc_addr; 1803 1804 end: 1805 unlock_user_struct(frame, frame_addr, 1); 1806 } 1807 1808 static void setup_rt_frame_v2(int usig, struct target_sigaction *ka, 1809 target_siginfo_t *info, 1810 target_sigset_t *set, CPUARMState *env) 1811 { 1812 struct rt_sigframe_v2 *frame; 1813 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame)); 1814 abi_ulong info_addr, uc_addr; 1815 1816 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 1817 return /* 1 */; 1818 1819 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info); 1820 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc); 1821 copy_siginfo_to_user(&frame->info, info); 1822 1823 setup_sigframe_v2(&frame->uc, set, env); 1824 1825 setup_return(env, ka, &frame->retcode, frame_addr, usig, 1826 frame_addr + offsetof(struct rt_sigframe_v2, retcode)); 1827 1828 env->regs[1] = info_addr; 1829 env->regs[2] = uc_addr; 1830 1831 unlock_user_struct(frame, frame_addr, 1); 1832 } 1833 1834 static void setup_rt_frame(int usig, struct target_sigaction *ka, 1835 target_siginfo_t *info, 1836 target_sigset_t *set, CPUARMState *env) 1837 { 1838 if (get_osversion() >= 0x020612) { 1839 setup_rt_frame_v2(usig, ka, info, set, env); 1840 } else { 1841 setup_rt_frame_v1(usig, ka, info, set, env); 1842 } 1843 } 1844 1845 static int 1846 restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc) 1847 { 1848 int err = 0; 1849 uint32_t cpsr; 1850 1851 __get_user(env->regs[0], &sc->arm_r0); 1852 __get_user(env->regs[1], &sc->arm_r1); 1853 __get_user(env->regs[2], &sc->arm_r2); 1854 __get_user(env->regs[3], &sc->arm_r3); 1855 __get_user(env->regs[4], &sc->arm_r4); 1856 __get_user(env->regs[5], &sc->arm_r5); 1857 __get_user(env->regs[6], &sc->arm_r6); 1858 __get_user(env->regs[7], &sc->arm_r7); 1859 __get_user(env->regs[8], &sc->arm_r8); 1860 __get_user(env->regs[9], &sc->arm_r9); 1861 __get_user(env->regs[10], &sc->arm_r10); 1862 __get_user(env->regs[11], &sc->arm_fp); 1863 __get_user(env->regs[12], &sc->arm_ip); 1864 __get_user(env->regs[13], &sc->arm_sp); 1865 __get_user(env->regs[14], &sc->arm_lr); 1866 __get_user(env->regs[15], &sc->arm_pc); 1867 #ifdef TARGET_CONFIG_CPU_32 1868 __get_user(cpsr, &sc->arm_cpsr); 1869 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC); 1870 #endif 1871 1872 err |= !valid_user_regs(env); 1873 1874 return err; 1875 } 1876 1877 static long do_sigreturn_v1(CPUARMState *env) 1878 { 1879 abi_ulong frame_addr; 1880 struct sigframe_v1 *frame = NULL; 1881 target_sigset_t set; 1882 sigset_t host_set; 1883 int i; 1884 1885 /* 1886 * Since we stacked the signal on a 64-bit boundary, 1887 * then 'sp' should be word aligned here. If it's 1888 * not, then the user is trying to mess with us. 1889 */ 1890 frame_addr = env->regs[13]; 1891 if (frame_addr & 7) { 1892 goto badframe; 1893 } 1894 1895 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 1896 goto badframe; 1897 1898 if (__get_user(set.sig[0], &frame->sc.oldmask)) 1899 goto badframe; 1900 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 1901 if (__get_user(set.sig[i], &frame->extramask[i - 1])) 1902 goto badframe; 1903 } 1904 1905 target_to_host_sigset_internal(&host_set, &set); 1906 do_sigprocmask(SIG_SETMASK, &host_set, NULL); 1907 1908 if (restore_sigcontext(env, &frame->sc)) 1909 goto badframe; 1910 1911 #if 0 1912 /* Send SIGTRAP if we're single-stepping */ 1913 if (ptrace_cancel_bpt(current)) 1914 send_sig(SIGTRAP, current, 1); 1915 #endif 1916 unlock_user_struct(frame, frame_addr, 0); 1917 return env->regs[0]; 1918 1919 badframe: 1920 unlock_user_struct(frame, frame_addr, 0); 1921 force_sig(TARGET_SIGSEGV /* , current */); 1922 return 0; 1923 } 1924 1925 static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace) 1926 { 1927 int i; 1928 abi_ulong magic, sz; 1929 uint32_t fpscr, fpexc; 1930 struct target_vfp_sigframe *vfpframe; 1931 vfpframe = (struct target_vfp_sigframe *)regspace; 1932 1933 __get_user(magic, &vfpframe->magic); 1934 __get_user(sz, &vfpframe->size); 1935 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) { 1936 return 0; 1937 } 1938 for (i = 0; i < 32; i++) { 1939 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]); 1940 } 1941 __get_user(fpscr, &vfpframe->ufp.fpscr); 1942 vfp_set_fpscr(env, fpscr); 1943 __get_user(fpexc, &vfpframe->ufp_exc.fpexc); 1944 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid 1945 * and the exception flag is cleared 1946 */ 1947 fpexc |= (1 << 30); 1948 fpexc &= ~((1 << 31) | (1 << 28)); 1949 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc; 1950 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst); 1951 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2); 1952 return (abi_ulong*)(vfpframe + 1); 1953 } 1954 1955 static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env, 1956 abi_ulong *regspace) 1957 { 1958 int i; 1959 abi_ulong magic, sz; 1960 struct target_iwmmxt_sigframe *iwmmxtframe; 1961 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace; 1962 1963 __get_user(magic, &iwmmxtframe->magic); 1964 __get_user(sz, &iwmmxtframe->size); 1965 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) { 1966 return 0; 1967 } 1968 for (i = 0; i < 16; i++) { 1969 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]); 1970 } 1971 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf); 1972 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf); 1973 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0); 1974 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1); 1975 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2); 1976 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3); 1977 return (abi_ulong*)(iwmmxtframe + 1); 1978 } 1979 1980 static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr, 1981 struct target_ucontext_v2 *uc) 1982 { 1983 sigset_t host_set; 1984 abi_ulong *regspace; 1985 1986 target_to_host_sigset(&host_set, &uc->tuc_sigmask); 1987 do_sigprocmask(SIG_SETMASK, &host_set, NULL); 1988 1989 if (restore_sigcontext(env, &uc->tuc_mcontext)) 1990 return 1; 1991 1992 /* Restore coprocessor signal frame */ 1993 regspace = uc->tuc_regspace; 1994 if (arm_feature(env, ARM_FEATURE_VFP)) { 1995 regspace = restore_sigframe_v2_vfp(env, regspace); 1996 if (!regspace) { 1997 return 1; 1998 } 1999 } 2000 if (arm_feature(env, ARM_FEATURE_IWMMXT)) { 2001 regspace = restore_sigframe_v2_iwmmxt(env, regspace); 2002 if (!regspace) { 2003 return 1; 2004 } 2005 } 2006 2007 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT) 2008 return 1; 2009 2010 #if 0 2011 /* Send SIGTRAP if we're single-stepping */ 2012 if (ptrace_cancel_bpt(current)) 2013 send_sig(SIGTRAP, current, 1); 2014 #endif 2015 2016 return 0; 2017 } 2018 2019 static long do_sigreturn_v2(CPUARMState *env) 2020 { 2021 abi_ulong frame_addr; 2022 struct sigframe_v2 *frame = NULL; 2023 2024 /* 2025 * Since we stacked the signal on a 64-bit boundary, 2026 * then 'sp' should be word aligned here. If it's 2027 * not, then the user is trying to mess with us. 2028 */ 2029 frame_addr = env->regs[13]; 2030 if (frame_addr & 7) { 2031 goto badframe; 2032 } 2033 2034 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 2035 goto badframe; 2036 2037 if (do_sigframe_return_v2(env, frame_addr, &frame->uc)) 2038 goto badframe; 2039 2040 unlock_user_struct(frame, frame_addr, 0); 2041 return env->regs[0]; 2042 2043 badframe: 2044 unlock_user_struct(frame, frame_addr, 0); 2045 force_sig(TARGET_SIGSEGV /* , current */); 2046 return 0; 2047 } 2048 2049 long do_sigreturn(CPUARMState *env) 2050 { 2051 if (get_osversion() >= 0x020612) { 2052 return do_sigreturn_v2(env); 2053 } else { 2054 return do_sigreturn_v1(env); 2055 } 2056 } 2057 2058 static long do_rt_sigreturn_v1(CPUARMState *env) 2059 { 2060 abi_ulong frame_addr; 2061 struct rt_sigframe_v1 *frame = NULL; 2062 sigset_t host_set; 2063 2064 /* 2065 * Since we stacked the signal on a 64-bit boundary, 2066 * then 'sp' should be word aligned here. If it's 2067 * not, then the user is trying to mess with us. 2068 */ 2069 frame_addr = env->regs[13]; 2070 if (frame_addr & 7) { 2071 goto badframe; 2072 } 2073 2074 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 2075 goto badframe; 2076 2077 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask); 2078 do_sigprocmask(SIG_SETMASK, &host_set, NULL); 2079 2080 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) 2081 goto badframe; 2082 2083 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT) 2084 goto badframe; 2085 2086 #if 0 2087 /* Send SIGTRAP if we're single-stepping */ 2088 if (ptrace_cancel_bpt(current)) 2089 send_sig(SIGTRAP, current, 1); 2090 #endif 2091 unlock_user_struct(frame, frame_addr, 0); 2092 return env->regs[0]; 2093 2094 badframe: 2095 unlock_user_struct(frame, frame_addr, 0); 2096 force_sig(TARGET_SIGSEGV /* , current */); 2097 return 0; 2098 } 2099 2100 static long do_rt_sigreturn_v2(CPUARMState *env) 2101 { 2102 abi_ulong frame_addr; 2103 struct rt_sigframe_v2 *frame = NULL; 2104 2105 /* 2106 * Since we stacked the signal on a 64-bit boundary, 2107 * then 'sp' should be word aligned here. If it's 2108 * not, then the user is trying to mess with us. 2109 */ 2110 frame_addr = env->regs[13]; 2111 if (frame_addr & 7) { 2112 goto badframe; 2113 } 2114 2115 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 2116 goto badframe; 2117 2118 if (do_sigframe_return_v2(env, frame_addr, &frame->uc)) 2119 goto badframe; 2120 2121 unlock_user_struct(frame, frame_addr, 0); 2122 return env->regs[0]; 2123 2124 badframe: 2125 unlock_user_struct(frame, frame_addr, 0); 2126 force_sig(TARGET_SIGSEGV /* , current */); 2127 return 0; 2128 } 2129 2130 long do_rt_sigreturn(CPUARMState *env) 2131 { 2132 if (get_osversion() >= 0x020612) { 2133 return do_rt_sigreturn_v2(env); 2134 } else { 2135 return do_rt_sigreturn_v1(env); 2136 } 2137 } 2138 2139 #elif defined(TARGET_SPARC) 2140 2141 #define __SUNOS_MAXWIN 31 2142 2143 /* This is what SunOS does, so shall I. */ 2144 struct target_sigcontext { 2145 abi_ulong sigc_onstack; /* state to restore */ 2146 2147 abi_ulong sigc_mask; /* sigmask to restore */ 2148 abi_ulong sigc_sp; /* stack pointer */ 2149 abi_ulong sigc_pc; /* program counter */ 2150 abi_ulong sigc_npc; /* next program counter */ 2151 abi_ulong sigc_psr; /* for condition codes etc */ 2152 abi_ulong sigc_g1; /* User uses these two registers */ 2153 abi_ulong sigc_o0; /* within the trampoline code. */ 2154 2155 /* Now comes information regarding the users window set 2156 * at the time of the signal. 2157 */ 2158 abi_ulong sigc_oswins; /* outstanding windows */ 2159 2160 /* stack ptrs for each regwin buf */ 2161 char *sigc_spbuf[__SUNOS_MAXWIN]; 2162 2163 /* Windows to restore after signal */ 2164 struct { 2165 abi_ulong locals[8]; 2166 abi_ulong ins[8]; 2167 } sigc_wbuf[__SUNOS_MAXWIN]; 2168 }; 2169 /* A Sparc stack frame */ 2170 struct sparc_stackf { 2171 abi_ulong locals[8]; 2172 abi_ulong ins[8]; 2173 /* It's simpler to treat fp and callers_pc as elements of ins[] 2174 * since we never need to access them ourselves. 2175 */ 2176 char *structptr; 2177 abi_ulong xargs[6]; 2178 abi_ulong xxargs[1]; 2179 }; 2180 2181 typedef struct { 2182 struct { 2183 abi_ulong psr; 2184 abi_ulong pc; 2185 abi_ulong npc; 2186 abi_ulong y; 2187 abi_ulong u_regs[16]; /* globals and ins */ 2188 } si_regs; 2189 int si_mask; 2190 } __siginfo_t; 2191 2192 typedef struct { 2193 abi_ulong si_float_regs[32]; 2194 unsigned long si_fsr; 2195 unsigned long si_fpqdepth; 2196 struct { 2197 unsigned long *insn_addr; 2198 unsigned long insn; 2199 } si_fpqueue [16]; 2200 } qemu_siginfo_fpu_t; 2201 2202 2203 struct target_signal_frame { 2204 struct sparc_stackf ss; 2205 __siginfo_t info; 2206 abi_ulong fpu_save; 2207 abi_ulong insns[2] __attribute__ ((aligned (8))); 2208 abi_ulong extramask[TARGET_NSIG_WORDS - 1]; 2209 abi_ulong extra_size; /* Should be 0 */ 2210 qemu_siginfo_fpu_t fpu_state; 2211 }; 2212 struct target_rt_signal_frame { 2213 struct sparc_stackf ss; 2214 siginfo_t info; 2215 abi_ulong regs[20]; 2216 sigset_t mask; 2217 abi_ulong fpu_save; 2218 unsigned int insns[2]; 2219 stack_t stack; 2220 unsigned int extra_size; /* Should be 0 */ 2221 qemu_siginfo_fpu_t fpu_state; 2222 }; 2223 2224 #define UREG_O0 16 2225 #define UREG_O6 22 2226 #define UREG_I0 0 2227 #define UREG_I1 1 2228 #define UREG_I2 2 2229 #define UREG_I3 3 2230 #define UREG_I4 4 2231 #define UREG_I5 5 2232 #define UREG_I6 6 2233 #define UREG_I7 7 2234 #define UREG_L0 8 2235 #define UREG_FP UREG_I6 2236 #define UREG_SP UREG_O6 2237 2238 static inline abi_ulong get_sigframe(struct target_sigaction *sa, 2239 CPUSPARCState *env, 2240 unsigned long framesize) 2241 { 2242 abi_ulong sp; 2243 2244 sp = env->regwptr[UREG_FP]; 2245 2246 /* This is the X/Open sanctioned signal stack switching. */ 2247 if (sa->sa_flags & TARGET_SA_ONSTACK) { 2248 if (!on_sig_stack(sp) 2249 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7)) 2250 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 2251 } 2252 return sp - framesize; 2253 } 2254 2255 static int 2256 setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask) 2257 { 2258 int err = 0, i; 2259 2260 __put_user(env->psr, &si->si_regs.psr); 2261 __put_user(env->pc, &si->si_regs.pc); 2262 __put_user(env->npc, &si->si_regs.npc); 2263 __put_user(env->y, &si->si_regs.y); 2264 for (i=0; i < 8; i++) { 2265 __put_user(env->gregs[i], &si->si_regs.u_regs[i]); 2266 } 2267 for (i=0; i < 8; i++) { 2268 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]); 2269 } 2270 __put_user(mask, &si->si_mask); 2271 return err; 2272 } 2273 2274 #if 0 2275 static int 2276 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ 2277 CPUSPARCState *env, unsigned long mask) 2278 { 2279 int err = 0; 2280 2281 __put_user(mask, &sc->sigc_mask); 2282 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp); 2283 __put_user(env->pc, &sc->sigc_pc); 2284 __put_user(env->npc, &sc->sigc_npc); 2285 __put_user(env->psr, &sc->sigc_psr); 2286 __put_user(env->gregs[1], &sc->sigc_g1); 2287 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0); 2288 2289 return err; 2290 } 2291 #endif 2292 #define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7))) 2293 2294 static void setup_frame(int sig, struct target_sigaction *ka, 2295 target_sigset_t *set, CPUSPARCState *env) 2296 { 2297 abi_ulong sf_addr; 2298 struct target_signal_frame *sf; 2299 int sigframe_size, err, i; 2300 2301 /* 1. Make sure everything is clean */ 2302 //synchronize_user_stack(); 2303 2304 sigframe_size = NF_ALIGNEDSZ; 2305 sf_addr = get_sigframe(ka, env, sigframe_size); 2306 2307 sf = lock_user(VERIFY_WRITE, sf_addr, 2308 sizeof(struct target_signal_frame), 0); 2309 if (!sf) 2310 goto sigsegv; 2311 2312 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]); 2313 #if 0 2314 if (invalid_frame_pointer(sf, sigframe_size)) 2315 goto sigill_and_return; 2316 #endif 2317 /* 2. Save the current process state */ 2318 err = setup___siginfo(&sf->info, env, set->sig[0]); 2319 __put_user(0, &sf->extra_size); 2320 2321 //save_fpu_state(regs, &sf->fpu_state); 2322 //__put_user(&sf->fpu_state, &sf->fpu_save); 2323 2324 __put_user(set->sig[0], &sf->info.si_mask); 2325 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { 2326 __put_user(set->sig[i + 1], &sf->extramask[i]); 2327 } 2328 2329 for (i = 0; i < 8; i++) { 2330 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]); 2331 } 2332 for (i = 0; i < 8; i++) { 2333 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]); 2334 } 2335 if (err) 2336 goto sigsegv; 2337 2338 /* 3. signal handler back-trampoline and parameters */ 2339 env->regwptr[UREG_FP] = sf_addr; 2340 env->regwptr[UREG_I0] = sig; 2341 env->regwptr[UREG_I1] = sf_addr + 2342 offsetof(struct target_signal_frame, info); 2343 env->regwptr[UREG_I2] = sf_addr + 2344 offsetof(struct target_signal_frame, info); 2345 2346 /* 4. signal handler */ 2347 env->pc = ka->_sa_handler; 2348 env->npc = (env->pc + 4); 2349 /* 5. return to kernel instructions */ 2350 if (ka->sa_restorer) 2351 env->regwptr[UREG_I7] = ka->sa_restorer; 2352 else { 2353 uint32_t val32; 2354 2355 env->regwptr[UREG_I7] = sf_addr + 2356 offsetof(struct target_signal_frame, insns) - 2 * 4; 2357 2358 /* mov __NR_sigreturn, %g1 */ 2359 val32 = 0x821020d8; 2360 __put_user(val32, &sf->insns[0]); 2361 2362 /* t 0x10 */ 2363 val32 = 0x91d02010; 2364 __put_user(val32, &sf->insns[1]); 2365 if (err) 2366 goto sigsegv; 2367 2368 /* Flush instruction space. */ 2369 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0])); 2370 // tb_flush(env); 2371 } 2372 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 2373 return; 2374 #if 0 2375 sigill_and_return: 2376 force_sig(TARGET_SIGILL); 2377 #endif 2378 sigsegv: 2379 //fprintf(stderr, "force_sig\n"); 2380 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame)); 2381 force_sig(TARGET_SIGSEGV); 2382 } 2383 static inline int 2384 restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu) 2385 { 2386 int err; 2387 #if 0 2388 #ifdef CONFIG_SMP 2389 if (current->flags & PF_USEDFPU) 2390 regs->psr &= ~PSR_EF; 2391 #else 2392 if (current == last_task_used_math) { 2393 last_task_used_math = 0; 2394 regs->psr &= ~PSR_EF; 2395 } 2396 #endif 2397 current->used_math = 1; 2398 current->flags &= ~PF_USEDFPU; 2399 #endif 2400 #if 0 2401 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu))) 2402 return -EFAULT; 2403 #endif 2404 2405 /* XXX: incorrect */ 2406 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0], 2407 (sizeof(abi_ulong) * 32)); 2408 err |= __get_user(env->fsr, &fpu->si_fsr); 2409 #if 0 2410 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth); 2411 if (current->thread.fpqdepth != 0) 2412 err |= __copy_from_user(¤t->thread.fpqueue[0], 2413 &fpu->si_fpqueue[0], 2414 ((sizeof(unsigned long) + 2415 (sizeof(unsigned long *)))*16)); 2416 #endif 2417 return err; 2418 } 2419 2420 2421 static void setup_rt_frame(int sig, struct target_sigaction *ka, 2422 target_siginfo_t *info, 2423 target_sigset_t *set, CPUSPARCState *env) 2424 { 2425 fprintf(stderr, "setup_rt_frame: not implemented\n"); 2426 } 2427 2428 long do_sigreturn(CPUSPARCState *env) 2429 { 2430 abi_ulong sf_addr; 2431 struct target_signal_frame *sf; 2432 uint32_t up_psr, pc, npc; 2433 target_sigset_t set; 2434 sigset_t host_set; 2435 int err=0, i; 2436 2437 sf_addr = env->regwptr[UREG_FP]; 2438 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) 2439 goto segv_and_exit; 2440 #if 0 2441 fprintf(stderr, "sigreturn\n"); 2442 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]); 2443 #endif 2444 //cpu_dump_state(env, stderr, fprintf, 0); 2445 2446 /* 1. Make sure we are not getting garbage from the user */ 2447 2448 if (sf_addr & 3) 2449 goto segv_and_exit; 2450 2451 __get_user(pc, &sf->info.si_regs.pc); 2452 __get_user(npc, &sf->info.si_regs.npc); 2453 2454 if ((pc | npc) & 3) 2455 goto segv_and_exit; 2456 2457 /* 2. Restore the state */ 2458 __get_user(up_psr, &sf->info.si_regs.psr); 2459 2460 /* User can only change condition codes and FPU enabling in %psr. */ 2461 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */)) 2462 | (env->psr & ~(PSR_ICC /* | PSR_EF */)); 2463 2464 env->pc = pc; 2465 env->npc = npc; 2466 __get_user(env->y, &sf->info.si_regs.y); 2467 for (i=0; i < 8; i++) { 2468 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]); 2469 } 2470 for (i=0; i < 8; i++) { 2471 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]); 2472 } 2473 2474 /* FIXME: implement FPU save/restore: 2475 * __get_user(fpu_save, &sf->fpu_save); 2476 * if (fpu_save) 2477 * err |= restore_fpu_state(env, fpu_save); 2478 */ 2479 2480 /* This is pretty much atomic, no amount locking would prevent 2481 * the races which exist anyways. 2482 */ 2483 __get_user(set.sig[0], &sf->info.si_mask); 2484 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 2485 __get_user(set.sig[i], &sf->extramask[i - 1]); 2486 } 2487 2488 target_to_host_sigset_internal(&host_set, &set); 2489 do_sigprocmask(SIG_SETMASK, &host_set, NULL); 2490 2491 if (err) 2492 goto segv_and_exit; 2493 unlock_user_struct(sf, sf_addr, 0); 2494 return env->regwptr[0]; 2495 2496 segv_and_exit: 2497 unlock_user_struct(sf, sf_addr, 0); 2498 force_sig(TARGET_SIGSEGV); 2499 } 2500 2501 long do_rt_sigreturn(CPUSPARCState *env) 2502 { 2503 fprintf(stderr, "do_rt_sigreturn: not implemented\n"); 2504 return -TARGET_ENOSYS; 2505 } 2506 2507 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) 2508 #define MC_TSTATE 0 2509 #define MC_PC 1 2510 #define MC_NPC 2 2511 #define MC_Y 3 2512 #define MC_G1 4 2513 #define MC_G2 5 2514 #define MC_G3 6 2515 #define MC_G4 7 2516 #define MC_G5 8 2517 #define MC_G6 9 2518 #define MC_G7 10 2519 #define MC_O0 11 2520 #define MC_O1 12 2521 #define MC_O2 13 2522 #define MC_O3 14 2523 #define MC_O4 15 2524 #define MC_O5 16 2525 #define MC_O6 17 2526 #define MC_O7 18 2527 #define MC_NGREG 19 2528 2529 typedef abi_ulong target_mc_greg_t; 2530 typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG]; 2531 2532 struct target_mc_fq { 2533 abi_ulong *mcfq_addr; 2534 uint32_t mcfq_insn; 2535 }; 2536 2537 struct target_mc_fpu { 2538 union { 2539 uint32_t sregs[32]; 2540 uint64_t dregs[32]; 2541 //uint128_t qregs[16]; 2542 } mcfpu_fregs; 2543 abi_ulong mcfpu_fsr; 2544 abi_ulong mcfpu_fprs; 2545 abi_ulong mcfpu_gsr; 2546 struct target_mc_fq *mcfpu_fq; 2547 unsigned char mcfpu_qcnt; 2548 unsigned char mcfpu_qentsz; 2549 unsigned char mcfpu_enab; 2550 }; 2551 typedef struct target_mc_fpu target_mc_fpu_t; 2552 2553 typedef struct { 2554 target_mc_gregset_t mc_gregs; 2555 target_mc_greg_t mc_fp; 2556 target_mc_greg_t mc_i7; 2557 target_mc_fpu_t mc_fpregs; 2558 } target_mcontext_t; 2559 2560 struct target_ucontext { 2561 struct target_ucontext *tuc_link; 2562 abi_ulong tuc_flags; 2563 target_sigset_t tuc_sigmask; 2564 target_mcontext_t tuc_mcontext; 2565 }; 2566 2567 /* A V9 register window */ 2568 struct target_reg_window { 2569 abi_ulong locals[8]; 2570 abi_ulong ins[8]; 2571 }; 2572 2573 #define TARGET_STACK_BIAS 2047 2574 2575 /* {set, get}context() needed for 64-bit SparcLinux userland. */ 2576 void sparc64_set_context(CPUSPARCState *env) 2577 { 2578 abi_ulong ucp_addr; 2579 struct target_ucontext *ucp; 2580 target_mc_gregset_t *grp; 2581 abi_ulong pc, npc, tstate; 2582 abi_ulong fp, i7, w_addr; 2583 int err = 0; 2584 unsigned int i; 2585 2586 ucp_addr = env->regwptr[UREG_I0]; 2587 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) 2588 goto do_sigsegv; 2589 grp = &ucp->tuc_mcontext.mc_gregs; 2590 __get_user(pc, &((*grp)[MC_PC])); 2591 __get_user(npc, &((*grp)[MC_NPC])); 2592 if (err || ((pc | npc) & 3)) 2593 goto do_sigsegv; 2594 if (env->regwptr[UREG_I1]) { 2595 target_sigset_t target_set; 2596 sigset_t set; 2597 2598 if (TARGET_NSIG_WORDS == 1) { 2599 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0])) 2600 goto do_sigsegv; 2601 } else { 2602 abi_ulong *src, *dst; 2603 src = ucp->tuc_sigmask.sig; 2604 dst = target_set.sig; 2605 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 2606 __get_user(*dst, src); 2607 } 2608 if (err) 2609 goto do_sigsegv; 2610 } 2611 target_to_host_sigset_internal(&set, &target_set); 2612 do_sigprocmask(SIG_SETMASK, &set, NULL); 2613 } 2614 env->pc = pc; 2615 env->npc = npc; 2616 __get_user(env->y, &((*grp)[MC_Y])); 2617 __get_user(tstate, &((*grp)[MC_TSTATE])); 2618 env->asi = (tstate >> 24) & 0xff; 2619 cpu_put_ccr(env, tstate >> 32); 2620 cpu_put_cwp64(env, tstate & 0x1f); 2621 __get_user(env->gregs[1], (&(*grp)[MC_G1])); 2622 __get_user(env->gregs[2], (&(*grp)[MC_G2])); 2623 __get_user(env->gregs[3], (&(*grp)[MC_G3])); 2624 __get_user(env->gregs[4], (&(*grp)[MC_G4])); 2625 __get_user(env->gregs[5], (&(*grp)[MC_G5])); 2626 __get_user(env->gregs[6], (&(*grp)[MC_G6])); 2627 __get_user(env->gregs[7], (&(*grp)[MC_G7])); 2628 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0])); 2629 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1])); 2630 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2])); 2631 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3])); 2632 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4])); 2633 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5])); 2634 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6])); 2635 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7])); 2636 2637 __get_user(fp, &(ucp->tuc_mcontext.mc_fp)); 2638 __get_user(i7, &(ucp->tuc_mcontext.mc_i7)); 2639 2640 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6]; 2641 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 2642 abi_ulong) != 0) 2643 goto do_sigsegv; 2644 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 2645 abi_ulong) != 0) 2646 goto do_sigsegv; 2647 /* FIXME this does not match how the kernel handles the FPU in 2648 * its sparc64_set_context implementation. In particular the FPU 2649 * is only restored if fenab is non-zero in: 2650 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab)); 2651 */ 2652 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs)); 2653 { 2654 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs; 2655 for (i = 0; i < 64; i++, src++) { 2656 if (i & 1) { 2657 __get_user(env->fpr[i/2].l.lower, src); 2658 } else { 2659 __get_user(env->fpr[i/2].l.upper, src); 2660 } 2661 } 2662 } 2663 __get_user(env->fsr, 2664 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr)); 2665 __get_user(env->gsr, 2666 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr)); 2667 if (err) 2668 goto do_sigsegv; 2669 unlock_user_struct(ucp, ucp_addr, 0); 2670 return; 2671 do_sigsegv: 2672 unlock_user_struct(ucp, ucp_addr, 0); 2673 force_sig(TARGET_SIGSEGV); 2674 } 2675 2676 void sparc64_get_context(CPUSPARCState *env) 2677 { 2678 abi_ulong ucp_addr; 2679 struct target_ucontext *ucp; 2680 target_mc_gregset_t *grp; 2681 target_mcontext_t *mcp; 2682 abi_ulong fp, i7, w_addr; 2683 int err; 2684 unsigned int i; 2685 target_sigset_t target_set; 2686 sigset_t set; 2687 2688 ucp_addr = env->regwptr[UREG_I0]; 2689 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) 2690 goto do_sigsegv; 2691 2692 mcp = &ucp->tuc_mcontext; 2693 grp = &mcp->mc_gregs; 2694 2695 /* Skip over the trap instruction, first. */ 2696 env->pc = env->npc; 2697 env->npc += 4; 2698 2699 err = 0; 2700 2701 do_sigprocmask(0, NULL, &set); 2702 host_to_target_sigset_internal(&target_set, &set); 2703 if (TARGET_NSIG_WORDS == 1) { 2704 __put_user(target_set.sig[0], 2705 (abi_ulong *)&ucp->tuc_sigmask); 2706 } else { 2707 abi_ulong *src, *dst; 2708 src = target_set.sig; 2709 dst = ucp->tuc_sigmask.sig; 2710 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) { 2711 __put_user(*src, dst); 2712 } 2713 if (err) 2714 goto do_sigsegv; 2715 } 2716 2717 /* XXX: tstate must be saved properly */ 2718 // __put_user(env->tstate, &((*grp)[MC_TSTATE])); 2719 __put_user(env->pc, &((*grp)[MC_PC])); 2720 __put_user(env->npc, &((*grp)[MC_NPC])); 2721 __put_user(env->y, &((*grp)[MC_Y])); 2722 __put_user(env->gregs[1], &((*grp)[MC_G1])); 2723 __put_user(env->gregs[2], &((*grp)[MC_G2])); 2724 __put_user(env->gregs[3], &((*grp)[MC_G3])); 2725 __put_user(env->gregs[4], &((*grp)[MC_G4])); 2726 __put_user(env->gregs[5], &((*grp)[MC_G5])); 2727 __put_user(env->gregs[6], &((*grp)[MC_G6])); 2728 __put_user(env->gregs[7], &((*grp)[MC_G7])); 2729 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0])); 2730 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1])); 2731 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2])); 2732 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3])); 2733 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4])); 2734 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5])); 2735 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6])); 2736 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7])); 2737 2738 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6]; 2739 fp = i7 = 0; 2740 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 2741 abi_ulong) != 0) 2742 goto do_sigsegv; 2743 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 2744 abi_ulong) != 0) 2745 goto do_sigsegv; 2746 __put_user(fp, &(mcp->mc_fp)); 2747 __put_user(i7, &(mcp->mc_i7)); 2748 2749 { 2750 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs; 2751 for (i = 0; i < 64; i++, dst++) { 2752 if (i & 1) { 2753 __put_user(env->fpr[i/2].l.lower, dst); 2754 } else { 2755 __put_user(env->fpr[i/2].l.upper, dst); 2756 } 2757 } 2758 } 2759 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr)); 2760 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr)); 2761 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs)); 2762 2763 if (err) 2764 goto do_sigsegv; 2765 unlock_user_struct(ucp, ucp_addr, 1); 2766 return; 2767 do_sigsegv: 2768 unlock_user_struct(ucp, ucp_addr, 1); 2769 force_sig(TARGET_SIGSEGV); 2770 } 2771 #endif 2772 #elif defined(TARGET_MIPS) || defined(TARGET_MIPS64) 2773 2774 # if defined(TARGET_ABI_MIPSO32) 2775 struct target_sigcontext { 2776 uint32_t sc_regmask; /* Unused */ 2777 uint32_t sc_status; 2778 uint64_t sc_pc; 2779 uint64_t sc_regs[32]; 2780 uint64_t sc_fpregs[32]; 2781 uint32_t sc_ownedfp; /* Unused */ 2782 uint32_t sc_fpc_csr; 2783 uint32_t sc_fpc_eir; /* Unused */ 2784 uint32_t sc_used_math; 2785 uint32_t sc_dsp; /* dsp status, was sc_ssflags */ 2786 uint32_t pad0; 2787 uint64_t sc_mdhi; 2788 uint64_t sc_mdlo; 2789 target_ulong sc_hi1; /* Was sc_cause */ 2790 target_ulong sc_lo1; /* Was sc_badvaddr */ 2791 target_ulong sc_hi2; /* Was sc_sigset[4] */ 2792 target_ulong sc_lo2; 2793 target_ulong sc_hi3; 2794 target_ulong sc_lo3; 2795 }; 2796 # else /* N32 || N64 */ 2797 struct target_sigcontext { 2798 uint64_t sc_regs[32]; 2799 uint64_t sc_fpregs[32]; 2800 uint64_t sc_mdhi; 2801 uint64_t sc_hi1; 2802 uint64_t sc_hi2; 2803 uint64_t sc_hi3; 2804 uint64_t sc_mdlo; 2805 uint64_t sc_lo1; 2806 uint64_t sc_lo2; 2807 uint64_t sc_lo3; 2808 uint64_t sc_pc; 2809 uint32_t sc_fpc_csr; 2810 uint32_t sc_used_math; 2811 uint32_t sc_dsp; 2812 uint32_t sc_reserved; 2813 }; 2814 # endif /* O32 */ 2815 2816 struct sigframe { 2817 uint32_t sf_ass[4]; /* argument save space for o32 */ 2818 uint32_t sf_code[2]; /* signal trampoline */ 2819 struct target_sigcontext sf_sc; 2820 target_sigset_t sf_mask; 2821 }; 2822 2823 struct target_ucontext { 2824 target_ulong tuc_flags; 2825 target_ulong tuc_link; 2826 target_stack_t tuc_stack; 2827 target_ulong pad0; 2828 struct target_sigcontext tuc_mcontext; 2829 target_sigset_t tuc_sigmask; 2830 }; 2831 2832 struct target_rt_sigframe { 2833 uint32_t rs_ass[4]; /* argument save space for o32 */ 2834 uint32_t rs_code[2]; /* signal trampoline */ 2835 struct target_siginfo rs_info; 2836 struct target_ucontext rs_uc; 2837 }; 2838 2839 /* Install trampoline to jump back from signal handler */ 2840 static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall) 2841 { 2842 int err = 0; 2843 2844 /* 2845 * Set up the return code ... 2846 * 2847 * li v0, __NR__foo_sigreturn 2848 * syscall 2849 */ 2850 2851 __put_user(0x24020000 + syscall, tramp + 0); 2852 __put_user(0x0000000c , tramp + 1); 2853 return err; 2854 } 2855 2856 static inline void setup_sigcontext(CPUMIPSState *regs, 2857 struct target_sigcontext *sc) 2858 { 2859 int i; 2860 2861 __put_user(exception_resume_pc(regs), &sc->sc_pc); 2862 regs->hflags &= ~MIPS_HFLAG_BMASK; 2863 2864 __put_user(0, &sc->sc_regs[0]); 2865 for (i = 1; i < 32; ++i) { 2866 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); 2867 } 2868 2869 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi); 2870 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo); 2871 2872 /* Rather than checking for dsp existence, always copy. The storage 2873 would just be garbage otherwise. */ 2874 __put_user(regs->active_tc.HI[1], &sc->sc_hi1); 2875 __put_user(regs->active_tc.HI[2], &sc->sc_hi2); 2876 __put_user(regs->active_tc.HI[3], &sc->sc_hi3); 2877 __put_user(regs->active_tc.LO[1], &sc->sc_lo1); 2878 __put_user(regs->active_tc.LO[2], &sc->sc_lo2); 2879 __put_user(regs->active_tc.LO[3], &sc->sc_lo3); 2880 { 2881 uint32_t dsp = cpu_rddsp(0x3ff, regs); 2882 __put_user(dsp, &sc->sc_dsp); 2883 } 2884 2885 __put_user(1, &sc->sc_used_math); 2886 2887 for (i = 0; i < 32; ++i) { 2888 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]); 2889 } 2890 } 2891 2892 static inline void 2893 restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc) 2894 { 2895 int i; 2896 2897 __get_user(regs->CP0_EPC, &sc->sc_pc); 2898 2899 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi); 2900 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo); 2901 2902 for (i = 1; i < 32; ++i) { 2903 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); 2904 } 2905 2906 __get_user(regs->active_tc.HI[1], &sc->sc_hi1); 2907 __get_user(regs->active_tc.HI[2], &sc->sc_hi2); 2908 __get_user(regs->active_tc.HI[3], &sc->sc_hi3); 2909 __get_user(regs->active_tc.LO[1], &sc->sc_lo1); 2910 __get_user(regs->active_tc.LO[2], &sc->sc_lo2); 2911 __get_user(regs->active_tc.LO[3], &sc->sc_lo3); 2912 { 2913 uint32_t dsp; 2914 __get_user(dsp, &sc->sc_dsp); 2915 cpu_wrdsp(dsp, 0x3ff, regs); 2916 } 2917 2918 for (i = 0; i < 32; ++i) { 2919 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]); 2920 } 2921 } 2922 2923 /* 2924 * Determine which stack to use.. 2925 */ 2926 static inline abi_ulong 2927 get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size) 2928 { 2929 unsigned long sp; 2930 2931 /* Default to using normal stack */ 2932 sp = regs->active_tc.gpr[29]; 2933 2934 /* 2935 * FPU emulator may have its own trampoline active just 2936 * above the user stack, 16-bytes before the next lowest 2937 * 16 byte boundary. Try to avoid trashing it. 2938 */ 2939 sp -= 32; 2940 2941 /* This is the X/Open sanctioned signal stack switching. */ 2942 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) { 2943 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 2944 } 2945 2946 return (sp - frame_size) & ~7; 2947 } 2948 2949 static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env) 2950 { 2951 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) { 2952 env->hflags &= ~MIPS_HFLAG_M16; 2953 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT; 2954 env->active_tc.PC &= ~(target_ulong) 1; 2955 } 2956 } 2957 2958 # if defined(TARGET_ABI_MIPSO32) 2959 /* compare linux/arch/mips/kernel/signal.c:setup_frame() */ 2960 static void setup_frame(int sig, struct target_sigaction * ka, 2961 target_sigset_t *set, CPUMIPSState *regs) 2962 { 2963 struct sigframe *frame; 2964 abi_ulong frame_addr; 2965 int i; 2966 2967 frame_addr = get_sigframe(ka, regs, sizeof(*frame)); 2968 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 2969 goto give_sigsegv; 2970 2971 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn); 2972 2973 setup_sigcontext(regs, &frame->sf_sc); 2974 2975 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 2976 if(__put_user(set->sig[i], &frame->sf_mask.sig[i])) 2977 goto give_sigsegv; 2978 } 2979 2980 /* 2981 * Arguments to signal handler: 2982 * 2983 * a0 = signal number 2984 * a1 = 0 (should be cause) 2985 * a2 = pointer to struct sigcontext 2986 * 2987 * $25 and PC point to the signal handler, $29 points to the 2988 * struct sigframe. 2989 */ 2990 regs->active_tc.gpr[ 4] = sig; 2991 regs->active_tc.gpr[ 5] = 0; 2992 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc); 2993 regs->active_tc.gpr[29] = frame_addr; 2994 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code); 2995 /* The original kernel code sets CP0_EPC to the handler 2996 * since it returns to userland using eret 2997 * we cannot do this here, and we must set PC directly */ 2998 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler; 2999 mips_set_hflags_isa_mode_from_pc(regs); 3000 unlock_user_struct(frame, frame_addr, 1); 3001 return; 3002 3003 give_sigsegv: 3004 unlock_user_struct(frame, frame_addr, 1); 3005 force_sig(TARGET_SIGSEGV/*, current*/); 3006 } 3007 3008 long do_sigreturn(CPUMIPSState *regs) 3009 { 3010 struct sigframe *frame; 3011 abi_ulong frame_addr; 3012 sigset_t blocked; 3013 target_sigset_t target_set; 3014 int i; 3015 3016 #if defined(DEBUG_SIGNAL) 3017 fprintf(stderr, "do_sigreturn\n"); 3018 #endif 3019 frame_addr = regs->active_tc.gpr[29]; 3020 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 3021 goto badframe; 3022 3023 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 3024 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i])) 3025 goto badframe; 3026 } 3027 3028 target_to_host_sigset_internal(&blocked, &target_set); 3029 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 3030 3031 restore_sigcontext(regs, &frame->sf_sc); 3032 3033 #if 0 3034 /* 3035 * Don't let your children do this ... 3036 */ 3037 __asm__ __volatile__( 3038 "move\t$29, %0\n\t" 3039 "j\tsyscall_exit" 3040 :/* no outputs */ 3041 :"r" (®s)); 3042 /* Unreached */ 3043 #endif 3044 3045 regs->active_tc.PC = regs->CP0_EPC; 3046 mips_set_hflags_isa_mode_from_pc(regs); 3047 /* I am not sure this is right, but it seems to work 3048 * maybe a problem with nested signals ? */ 3049 regs->CP0_EPC = 0; 3050 return -TARGET_QEMU_ESIGRETURN; 3051 3052 badframe: 3053 force_sig(TARGET_SIGSEGV/*, current*/); 3054 return 0; 3055 } 3056 # endif /* O32 */ 3057 3058 static void setup_rt_frame(int sig, struct target_sigaction *ka, 3059 target_siginfo_t *info, 3060 target_sigset_t *set, CPUMIPSState *env) 3061 { 3062 struct target_rt_sigframe *frame; 3063 abi_ulong frame_addr; 3064 int i; 3065 3066 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 3067 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 3068 goto give_sigsegv; 3069 3070 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn); 3071 3072 copy_siginfo_to_user(&frame->rs_info, info); 3073 3074 __put_user(0, &frame->rs_uc.tuc_flags); 3075 __put_user(0, &frame->rs_uc.tuc_link); 3076 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp); 3077 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size); 3078 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), 3079 &frame->rs_uc.tuc_stack.ss_flags); 3080 3081 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext); 3082 3083 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 3084 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]); 3085 } 3086 3087 /* 3088 * Arguments to signal handler: 3089 * 3090 * a0 = signal number 3091 * a1 = pointer to siginfo_t 3092 * a2 = pointer to struct ucontext 3093 * 3094 * $25 and PC point to the signal handler, $29 points to the 3095 * struct sigframe. 3096 */ 3097 env->active_tc.gpr[ 4] = sig; 3098 env->active_tc.gpr[ 5] = frame_addr 3099 + offsetof(struct target_rt_sigframe, rs_info); 3100 env->active_tc.gpr[ 6] = frame_addr 3101 + offsetof(struct target_rt_sigframe, rs_uc); 3102 env->active_tc.gpr[29] = frame_addr; 3103 env->active_tc.gpr[31] = frame_addr 3104 + offsetof(struct target_rt_sigframe, rs_code); 3105 /* The original kernel code sets CP0_EPC to the handler 3106 * since it returns to userland using eret 3107 * we cannot do this here, and we must set PC directly */ 3108 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler; 3109 mips_set_hflags_isa_mode_from_pc(env); 3110 unlock_user_struct(frame, frame_addr, 1); 3111 return; 3112 3113 give_sigsegv: 3114 unlock_user_struct(frame, frame_addr, 1); 3115 force_sig(TARGET_SIGSEGV/*, current*/); 3116 } 3117 3118 long do_rt_sigreturn(CPUMIPSState *env) 3119 { 3120 struct target_rt_sigframe *frame; 3121 abi_ulong frame_addr; 3122 sigset_t blocked; 3123 3124 #if defined(DEBUG_SIGNAL) 3125 fprintf(stderr, "do_rt_sigreturn\n"); 3126 #endif 3127 frame_addr = env->active_tc.gpr[29]; 3128 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 3129 goto badframe; 3130 3131 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask); 3132 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 3133 3134 restore_sigcontext(env, &frame->rs_uc.tuc_mcontext); 3135 3136 if (do_sigaltstack(frame_addr + 3137 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack), 3138 0, get_sp_from_cpustate(env)) == -EFAULT) 3139 goto badframe; 3140 3141 env->active_tc.PC = env->CP0_EPC; 3142 mips_set_hflags_isa_mode_from_pc(env); 3143 /* I am not sure this is right, but it seems to work 3144 * maybe a problem with nested signals ? */ 3145 env->CP0_EPC = 0; 3146 return -TARGET_QEMU_ESIGRETURN; 3147 3148 badframe: 3149 force_sig(TARGET_SIGSEGV/*, current*/); 3150 return 0; 3151 } 3152 3153 #elif defined(TARGET_SH4) 3154 3155 /* 3156 * code and data structures from linux kernel: 3157 * include/asm-sh/sigcontext.h 3158 * arch/sh/kernel/signal.c 3159 */ 3160 3161 struct target_sigcontext { 3162 target_ulong oldmask; 3163 3164 /* CPU registers */ 3165 target_ulong sc_gregs[16]; 3166 target_ulong sc_pc; 3167 target_ulong sc_pr; 3168 target_ulong sc_sr; 3169 target_ulong sc_gbr; 3170 target_ulong sc_mach; 3171 target_ulong sc_macl; 3172 3173 /* FPU registers */ 3174 target_ulong sc_fpregs[16]; 3175 target_ulong sc_xfpregs[16]; 3176 unsigned int sc_fpscr; 3177 unsigned int sc_fpul; 3178 unsigned int sc_ownedfp; 3179 }; 3180 3181 struct target_sigframe 3182 { 3183 struct target_sigcontext sc; 3184 target_ulong extramask[TARGET_NSIG_WORDS-1]; 3185 uint16_t retcode[3]; 3186 }; 3187 3188 3189 struct target_ucontext { 3190 target_ulong tuc_flags; 3191 struct target_ucontext *tuc_link; 3192 target_stack_t tuc_stack; 3193 struct target_sigcontext tuc_mcontext; 3194 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 3195 }; 3196 3197 struct target_rt_sigframe 3198 { 3199 struct target_siginfo info; 3200 struct target_ucontext uc; 3201 uint16_t retcode[3]; 3202 }; 3203 3204 3205 #define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */ 3206 #define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */ 3207 3208 static abi_ulong get_sigframe(struct target_sigaction *ka, 3209 unsigned long sp, size_t frame_size) 3210 { 3211 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) { 3212 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 3213 } 3214 3215 return (sp - frame_size) & -8ul; 3216 } 3217 3218 static void setup_sigcontext(struct target_sigcontext *sc, 3219 CPUSH4State *regs, unsigned long mask) 3220 { 3221 int i; 3222 3223 #define COPY(x) __put_user(regs->x, &sc->sc_##x) 3224 COPY(gregs[0]); COPY(gregs[1]); 3225 COPY(gregs[2]); COPY(gregs[3]); 3226 COPY(gregs[4]); COPY(gregs[5]); 3227 COPY(gregs[6]); COPY(gregs[7]); 3228 COPY(gregs[8]); COPY(gregs[9]); 3229 COPY(gregs[10]); COPY(gregs[11]); 3230 COPY(gregs[12]); COPY(gregs[13]); 3231 COPY(gregs[14]); COPY(gregs[15]); 3232 COPY(gbr); COPY(mach); 3233 COPY(macl); COPY(pr); 3234 COPY(sr); COPY(pc); 3235 #undef COPY 3236 3237 for (i=0; i<16; i++) { 3238 __put_user(regs->fregs[i], &sc->sc_fpregs[i]); 3239 } 3240 __put_user(regs->fpscr, &sc->sc_fpscr); 3241 __put_user(regs->fpul, &sc->sc_fpul); 3242 3243 /* non-iBCS2 extensions.. */ 3244 __put_user(mask, &sc->oldmask); 3245 } 3246 3247 static void restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc, 3248 target_ulong *r0_p) 3249 { 3250 int i; 3251 3252 #define COPY(x) __get_user(regs->x, &sc->sc_##x) 3253 COPY(gregs[1]); 3254 COPY(gregs[2]); COPY(gregs[3]); 3255 COPY(gregs[4]); COPY(gregs[5]); 3256 COPY(gregs[6]); COPY(gregs[7]); 3257 COPY(gregs[8]); COPY(gregs[9]); 3258 COPY(gregs[10]); COPY(gregs[11]); 3259 COPY(gregs[12]); COPY(gregs[13]); 3260 COPY(gregs[14]); COPY(gregs[15]); 3261 COPY(gbr); COPY(mach); 3262 COPY(macl); COPY(pr); 3263 COPY(sr); COPY(pc); 3264 #undef COPY 3265 3266 for (i=0; i<16; i++) { 3267 __get_user(regs->fregs[i], &sc->sc_fpregs[i]); 3268 } 3269 __get_user(regs->fpscr, &sc->sc_fpscr); 3270 __get_user(regs->fpul, &sc->sc_fpul); 3271 3272 regs->tra = -1; /* disable syscall checks */ 3273 __get_user(*r0_p, &sc->sc_gregs[0]); 3274 } 3275 3276 static void setup_frame(int sig, struct target_sigaction *ka, 3277 target_sigset_t *set, CPUSH4State *regs) 3278 { 3279 struct target_sigframe *frame; 3280 abi_ulong frame_addr; 3281 int i; 3282 int err = 0; 3283 int signal; 3284 3285 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame)); 3286 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 3287 goto give_sigsegv; 3288 3289 signal = current_exec_domain_sig(sig); 3290 3291 setup_sigcontext(&frame->sc, regs, set->sig[0]); 3292 3293 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { 3294 __put_user(set->sig[i + 1], &frame->extramask[i]); 3295 } 3296 3297 /* Set up to return from userspace. If provided, use a stub 3298 already in userspace. */ 3299 if (ka->sa_flags & TARGET_SA_RESTORER) { 3300 regs->pr = (unsigned long) ka->sa_restorer; 3301 } else { 3302 /* Generate return code (system call to sigreturn) */ 3303 __put_user(MOVW(2), &frame->retcode[0]); 3304 __put_user(TRAP_NOARG, &frame->retcode[1]); 3305 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]); 3306 regs->pr = (unsigned long) frame->retcode; 3307 } 3308 3309 if (err) 3310 goto give_sigsegv; 3311 3312 /* Set up registers for signal handler */ 3313 regs->gregs[15] = frame_addr; 3314 regs->gregs[4] = signal; /* Arg for signal handler */ 3315 regs->gregs[5] = 0; 3316 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc); 3317 regs->pc = (unsigned long) ka->_sa_handler; 3318 3319 unlock_user_struct(frame, frame_addr, 1); 3320 return; 3321 3322 give_sigsegv: 3323 unlock_user_struct(frame, frame_addr, 1); 3324 force_sig(TARGET_SIGSEGV); 3325 } 3326 3327 static void setup_rt_frame(int sig, struct target_sigaction *ka, 3328 target_siginfo_t *info, 3329 target_sigset_t *set, CPUSH4State *regs) 3330 { 3331 struct target_rt_sigframe *frame; 3332 abi_ulong frame_addr; 3333 int i; 3334 int err = 0; 3335 int signal; 3336 3337 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame)); 3338 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 3339 goto give_sigsegv; 3340 3341 signal = current_exec_domain_sig(sig); 3342 3343 copy_siginfo_to_user(&frame->info, info); 3344 3345 /* Create the ucontext. */ 3346 __put_user(0, &frame->uc.tuc_flags); 3347 __put_user(0, (unsigned long *)&frame->uc.tuc_link); 3348 __put_user((unsigned long)target_sigaltstack_used.ss_sp, 3349 &frame->uc.tuc_stack.ss_sp); 3350 __put_user(sas_ss_flags(regs->gregs[15]), 3351 &frame->uc.tuc_stack.ss_flags); 3352 __put_user(target_sigaltstack_used.ss_size, 3353 &frame->uc.tuc_stack.ss_size); 3354 setup_sigcontext(&frame->uc.tuc_mcontext, 3355 regs, set->sig[0]); 3356 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 3357 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]); 3358 } 3359 3360 /* Set up to return from userspace. If provided, use a stub 3361 already in userspace. */ 3362 if (ka->sa_flags & TARGET_SA_RESTORER) { 3363 regs->pr = (unsigned long) ka->sa_restorer; 3364 } else { 3365 /* Generate return code (system call to sigreturn) */ 3366 __put_user(MOVW(2), &frame->retcode[0]); 3367 __put_user(TRAP_NOARG, &frame->retcode[1]); 3368 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]); 3369 regs->pr = (unsigned long) frame->retcode; 3370 } 3371 3372 if (err) 3373 goto give_sigsegv; 3374 3375 /* Set up registers for signal handler */ 3376 regs->gregs[15] = frame_addr; 3377 regs->gregs[4] = signal; /* Arg for signal handler */ 3378 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info); 3379 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc); 3380 regs->pc = (unsigned long) ka->_sa_handler; 3381 3382 unlock_user_struct(frame, frame_addr, 1); 3383 return; 3384 3385 give_sigsegv: 3386 unlock_user_struct(frame, frame_addr, 1); 3387 force_sig(TARGET_SIGSEGV); 3388 } 3389 3390 long do_sigreturn(CPUSH4State *regs) 3391 { 3392 struct target_sigframe *frame; 3393 abi_ulong frame_addr; 3394 sigset_t blocked; 3395 target_sigset_t target_set; 3396 target_ulong r0; 3397 int i; 3398 int err = 0; 3399 3400 #if defined(DEBUG_SIGNAL) 3401 fprintf(stderr, "do_sigreturn\n"); 3402 #endif 3403 frame_addr = regs->gregs[15]; 3404 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 3405 goto badframe; 3406 3407 __get_user(target_set.sig[0], &frame->sc.oldmask); 3408 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 3409 __get_user(target_set.sig[i], &frame->extramask[i - 1]); 3410 } 3411 3412 if (err) 3413 goto badframe; 3414 3415 target_to_host_sigset_internal(&blocked, &target_set); 3416 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 3417 3418 restore_sigcontext(regs, &frame->sc, &r0); 3419 3420 unlock_user_struct(frame, frame_addr, 0); 3421 return r0; 3422 3423 badframe: 3424 unlock_user_struct(frame, frame_addr, 0); 3425 force_sig(TARGET_SIGSEGV); 3426 return 0; 3427 } 3428 3429 long do_rt_sigreturn(CPUSH4State *regs) 3430 { 3431 struct target_rt_sigframe *frame; 3432 abi_ulong frame_addr; 3433 sigset_t blocked; 3434 target_ulong r0; 3435 3436 #if defined(DEBUG_SIGNAL) 3437 fprintf(stderr, "do_rt_sigreturn\n"); 3438 #endif 3439 frame_addr = regs->gregs[15]; 3440 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 3441 goto badframe; 3442 3443 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask); 3444 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 3445 3446 restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0); 3447 3448 if (do_sigaltstack(frame_addr + 3449 offsetof(struct target_rt_sigframe, uc.tuc_stack), 3450 0, get_sp_from_cpustate(regs)) == -EFAULT) 3451 goto badframe; 3452 3453 unlock_user_struct(frame, frame_addr, 0); 3454 return r0; 3455 3456 badframe: 3457 unlock_user_struct(frame, frame_addr, 0); 3458 force_sig(TARGET_SIGSEGV); 3459 return 0; 3460 } 3461 #elif defined(TARGET_MICROBLAZE) 3462 3463 struct target_sigcontext { 3464 struct target_pt_regs regs; /* needs to be first */ 3465 uint32_t oldmask; 3466 }; 3467 3468 struct target_stack_t { 3469 abi_ulong ss_sp; 3470 int ss_flags; 3471 unsigned int ss_size; 3472 }; 3473 3474 struct target_ucontext { 3475 abi_ulong tuc_flags; 3476 abi_ulong tuc_link; 3477 struct target_stack_t tuc_stack; 3478 struct target_sigcontext tuc_mcontext; 3479 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1]; 3480 }; 3481 3482 /* Signal frames. */ 3483 struct target_signal_frame { 3484 struct target_ucontext uc; 3485 uint32_t extramask[TARGET_NSIG_WORDS - 1]; 3486 uint32_t tramp[2]; 3487 }; 3488 3489 struct rt_signal_frame { 3490 siginfo_t info; 3491 struct ucontext uc; 3492 uint32_t tramp[2]; 3493 }; 3494 3495 static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env) 3496 { 3497 __put_user(env->regs[0], &sc->regs.r0); 3498 __put_user(env->regs[1], &sc->regs.r1); 3499 __put_user(env->regs[2], &sc->regs.r2); 3500 __put_user(env->regs[3], &sc->regs.r3); 3501 __put_user(env->regs[4], &sc->regs.r4); 3502 __put_user(env->regs[5], &sc->regs.r5); 3503 __put_user(env->regs[6], &sc->regs.r6); 3504 __put_user(env->regs[7], &sc->regs.r7); 3505 __put_user(env->regs[8], &sc->regs.r8); 3506 __put_user(env->regs[9], &sc->regs.r9); 3507 __put_user(env->regs[10], &sc->regs.r10); 3508 __put_user(env->regs[11], &sc->regs.r11); 3509 __put_user(env->regs[12], &sc->regs.r12); 3510 __put_user(env->regs[13], &sc->regs.r13); 3511 __put_user(env->regs[14], &sc->regs.r14); 3512 __put_user(env->regs[15], &sc->regs.r15); 3513 __put_user(env->regs[16], &sc->regs.r16); 3514 __put_user(env->regs[17], &sc->regs.r17); 3515 __put_user(env->regs[18], &sc->regs.r18); 3516 __put_user(env->regs[19], &sc->regs.r19); 3517 __put_user(env->regs[20], &sc->regs.r20); 3518 __put_user(env->regs[21], &sc->regs.r21); 3519 __put_user(env->regs[22], &sc->regs.r22); 3520 __put_user(env->regs[23], &sc->regs.r23); 3521 __put_user(env->regs[24], &sc->regs.r24); 3522 __put_user(env->regs[25], &sc->regs.r25); 3523 __put_user(env->regs[26], &sc->regs.r26); 3524 __put_user(env->regs[27], &sc->regs.r27); 3525 __put_user(env->regs[28], &sc->regs.r28); 3526 __put_user(env->regs[29], &sc->regs.r29); 3527 __put_user(env->regs[30], &sc->regs.r30); 3528 __put_user(env->regs[31], &sc->regs.r31); 3529 __put_user(env->sregs[SR_PC], &sc->regs.pc); 3530 } 3531 3532 static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env) 3533 { 3534 __get_user(env->regs[0], &sc->regs.r0); 3535 __get_user(env->regs[1], &sc->regs.r1); 3536 __get_user(env->regs[2], &sc->regs.r2); 3537 __get_user(env->regs[3], &sc->regs.r3); 3538 __get_user(env->regs[4], &sc->regs.r4); 3539 __get_user(env->regs[5], &sc->regs.r5); 3540 __get_user(env->regs[6], &sc->regs.r6); 3541 __get_user(env->regs[7], &sc->regs.r7); 3542 __get_user(env->regs[8], &sc->regs.r8); 3543 __get_user(env->regs[9], &sc->regs.r9); 3544 __get_user(env->regs[10], &sc->regs.r10); 3545 __get_user(env->regs[11], &sc->regs.r11); 3546 __get_user(env->regs[12], &sc->regs.r12); 3547 __get_user(env->regs[13], &sc->regs.r13); 3548 __get_user(env->regs[14], &sc->regs.r14); 3549 __get_user(env->regs[15], &sc->regs.r15); 3550 __get_user(env->regs[16], &sc->regs.r16); 3551 __get_user(env->regs[17], &sc->regs.r17); 3552 __get_user(env->regs[18], &sc->regs.r18); 3553 __get_user(env->regs[19], &sc->regs.r19); 3554 __get_user(env->regs[20], &sc->regs.r20); 3555 __get_user(env->regs[21], &sc->regs.r21); 3556 __get_user(env->regs[22], &sc->regs.r22); 3557 __get_user(env->regs[23], &sc->regs.r23); 3558 __get_user(env->regs[24], &sc->regs.r24); 3559 __get_user(env->regs[25], &sc->regs.r25); 3560 __get_user(env->regs[26], &sc->regs.r26); 3561 __get_user(env->regs[27], &sc->regs.r27); 3562 __get_user(env->regs[28], &sc->regs.r28); 3563 __get_user(env->regs[29], &sc->regs.r29); 3564 __get_user(env->regs[30], &sc->regs.r30); 3565 __get_user(env->regs[31], &sc->regs.r31); 3566 __get_user(env->sregs[SR_PC], &sc->regs.pc); 3567 } 3568 3569 static abi_ulong get_sigframe(struct target_sigaction *ka, 3570 CPUMBState *env, int frame_size) 3571 { 3572 abi_ulong sp = env->regs[1]; 3573 3574 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp)) 3575 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 3576 3577 return ((sp - frame_size) & -8UL); 3578 } 3579 3580 static void setup_frame(int sig, struct target_sigaction *ka, 3581 target_sigset_t *set, CPUMBState *env) 3582 { 3583 struct target_signal_frame *frame; 3584 abi_ulong frame_addr; 3585 int err = 0; 3586 int i; 3587 3588 frame_addr = get_sigframe(ka, env, sizeof *frame); 3589 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 3590 goto badframe; 3591 3592 /* Save the mask. */ 3593 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask); 3594 if (err) 3595 goto badframe; 3596 3597 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 3598 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 3599 goto badframe; 3600 } 3601 3602 setup_sigcontext(&frame->uc.tuc_mcontext, env); 3603 3604 /* Set up to return from userspace. If provided, use a stub 3605 already in userspace. */ 3606 /* minus 8 is offset to cater for "rtsd r15,8" offset */ 3607 if (ka->sa_flags & TARGET_SA_RESTORER) { 3608 env->regs[15] = ((unsigned long)ka->sa_restorer)-8; 3609 } else { 3610 uint32_t t; 3611 /* Note, these encodings are _big endian_! */ 3612 /* addi r12, r0, __NR_sigreturn */ 3613 t = 0x31800000UL | TARGET_NR_sigreturn; 3614 __put_user(t, frame->tramp + 0); 3615 /* brki r14, 0x8 */ 3616 t = 0xb9cc0008UL; 3617 __put_user(t, frame->tramp + 1); 3618 3619 /* Return from sighandler will jump to the tramp. 3620 Negative 8 offset because return is rtsd r15, 8 */ 3621 env->regs[15] = ((unsigned long)frame->tramp) - 8; 3622 } 3623 3624 if (err) 3625 goto badframe; 3626 3627 /* Set up registers for signal handler */ 3628 env->regs[1] = frame_addr; 3629 /* Signal handler args: */ 3630 env->regs[5] = sig; /* Arg 0: signum */ 3631 env->regs[6] = 0; 3632 /* arg 1: sigcontext */ 3633 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc); 3634 3635 /* Offset of 4 to handle microblaze rtid r14, 0 */ 3636 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler; 3637 3638 unlock_user_struct(frame, frame_addr, 1); 3639 return; 3640 badframe: 3641 unlock_user_struct(frame, frame_addr, 1); 3642 force_sig(TARGET_SIGSEGV); 3643 } 3644 3645 static void setup_rt_frame(int sig, struct target_sigaction *ka, 3646 target_siginfo_t *info, 3647 target_sigset_t *set, CPUMBState *env) 3648 { 3649 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n"); 3650 } 3651 3652 long do_sigreturn(CPUMBState *env) 3653 { 3654 struct target_signal_frame *frame; 3655 abi_ulong frame_addr; 3656 target_sigset_t target_set; 3657 sigset_t set; 3658 int i; 3659 3660 frame_addr = env->regs[R_SP]; 3661 /* Make sure the guest isn't playing games. */ 3662 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1)) 3663 goto badframe; 3664 3665 /* Restore blocked signals */ 3666 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask)) 3667 goto badframe; 3668 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 3669 if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) 3670 goto badframe; 3671 } 3672 target_to_host_sigset_internal(&set, &target_set); 3673 do_sigprocmask(SIG_SETMASK, &set, NULL); 3674 3675 restore_sigcontext(&frame->uc.tuc_mcontext, env); 3676 /* We got here through a sigreturn syscall, our path back is via an 3677 rtb insn so setup r14 for that. */ 3678 env->regs[14] = env->sregs[SR_PC]; 3679 3680 unlock_user_struct(frame, frame_addr, 0); 3681 return env->regs[10]; 3682 badframe: 3683 unlock_user_struct(frame, frame_addr, 0); 3684 force_sig(TARGET_SIGSEGV); 3685 } 3686 3687 long do_rt_sigreturn(CPUMBState *env) 3688 { 3689 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n"); 3690 return -TARGET_ENOSYS; 3691 } 3692 3693 #elif defined(TARGET_CRIS) 3694 3695 struct target_sigcontext { 3696 struct target_pt_regs regs; /* needs to be first */ 3697 uint32_t oldmask; 3698 uint32_t usp; /* usp before stacking this gunk on it */ 3699 }; 3700 3701 /* Signal frames. */ 3702 struct target_signal_frame { 3703 struct target_sigcontext sc; 3704 uint32_t extramask[TARGET_NSIG_WORDS - 1]; 3705 uint16_t retcode[4]; /* Trampoline code. */ 3706 }; 3707 3708 struct rt_signal_frame { 3709 siginfo_t *pinfo; 3710 void *puc; 3711 siginfo_t info; 3712 struct ucontext uc; 3713 uint16_t retcode[4]; /* Trampoline code. */ 3714 }; 3715 3716 static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env) 3717 { 3718 __put_user(env->regs[0], &sc->regs.r0); 3719 __put_user(env->regs[1], &sc->regs.r1); 3720 __put_user(env->regs[2], &sc->regs.r2); 3721 __put_user(env->regs[3], &sc->regs.r3); 3722 __put_user(env->regs[4], &sc->regs.r4); 3723 __put_user(env->regs[5], &sc->regs.r5); 3724 __put_user(env->regs[6], &sc->regs.r6); 3725 __put_user(env->regs[7], &sc->regs.r7); 3726 __put_user(env->regs[8], &sc->regs.r8); 3727 __put_user(env->regs[9], &sc->regs.r9); 3728 __put_user(env->regs[10], &sc->regs.r10); 3729 __put_user(env->regs[11], &sc->regs.r11); 3730 __put_user(env->regs[12], &sc->regs.r12); 3731 __put_user(env->regs[13], &sc->regs.r13); 3732 __put_user(env->regs[14], &sc->usp); 3733 __put_user(env->regs[15], &sc->regs.acr); 3734 __put_user(env->pregs[PR_MOF], &sc->regs.mof); 3735 __put_user(env->pregs[PR_SRP], &sc->regs.srp); 3736 __put_user(env->pc, &sc->regs.erp); 3737 } 3738 3739 static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env) 3740 { 3741 __get_user(env->regs[0], &sc->regs.r0); 3742 __get_user(env->regs[1], &sc->regs.r1); 3743 __get_user(env->regs[2], &sc->regs.r2); 3744 __get_user(env->regs[3], &sc->regs.r3); 3745 __get_user(env->regs[4], &sc->regs.r4); 3746 __get_user(env->regs[5], &sc->regs.r5); 3747 __get_user(env->regs[6], &sc->regs.r6); 3748 __get_user(env->regs[7], &sc->regs.r7); 3749 __get_user(env->regs[8], &sc->regs.r8); 3750 __get_user(env->regs[9], &sc->regs.r9); 3751 __get_user(env->regs[10], &sc->regs.r10); 3752 __get_user(env->regs[11], &sc->regs.r11); 3753 __get_user(env->regs[12], &sc->regs.r12); 3754 __get_user(env->regs[13], &sc->regs.r13); 3755 __get_user(env->regs[14], &sc->usp); 3756 __get_user(env->regs[15], &sc->regs.acr); 3757 __get_user(env->pregs[PR_MOF], &sc->regs.mof); 3758 __get_user(env->pregs[PR_SRP], &sc->regs.srp); 3759 __get_user(env->pc, &sc->regs.erp); 3760 } 3761 3762 static abi_ulong get_sigframe(CPUCRISState *env, int framesize) 3763 { 3764 abi_ulong sp; 3765 /* Align the stack downwards to 4. */ 3766 sp = (env->regs[R_SP] & ~3); 3767 return sp - framesize; 3768 } 3769 3770 static void setup_frame(int sig, struct target_sigaction *ka, 3771 target_sigset_t *set, CPUCRISState *env) 3772 { 3773 struct target_signal_frame *frame; 3774 abi_ulong frame_addr; 3775 int err = 0; 3776 int i; 3777 3778 frame_addr = get_sigframe(env, sizeof *frame); 3779 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 3780 goto badframe; 3781 3782 /* 3783 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't 3784 * use this trampoline anymore but it sets it up for GDB. 3785 * In QEMU, using the trampoline simplifies things a bit so we use it. 3786 * 3787 * This is movu.w __NR_sigreturn, r9; break 13; 3788 */ 3789 __put_user(0x9c5f, frame->retcode+0); 3790 __put_user(TARGET_NR_sigreturn, 3791 frame->retcode + 1); 3792 __put_user(0xe93d, frame->retcode + 2); 3793 3794 /* Save the mask. */ 3795 __put_user(set->sig[0], &frame->sc.oldmask); 3796 if (err) 3797 goto badframe; 3798 3799 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 3800 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 3801 goto badframe; 3802 } 3803 3804 setup_sigcontext(&frame->sc, env); 3805 3806 /* Move the stack and setup the arguments for the handler. */ 3807 env->regs[R_SP] = frame_addr; 3808 env->regs[10] = sig; 3809 env->pc = (unsigned long) ka->_sa_handler; 3810 /* Link SRP so the guest returns through the trampoline. */ 3811 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode); 3812 3813 unlock_user_struct(frame, frame_addr, 1); 3814 return; 3815 badframe: 3816 unlock_user_struct(frame, frame_addr, 1); 3817 force_sig(TARGET_SIGSEGV); 3818 } 3819 3820 static void setup_rt_frame(int sig, struct target_sigaction *ka, 3821 target_siginfo_t *info, 3822 target_sigset_t *set, CPUCRISState *env) 3823 { 3824 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n"); 3825 } 3826 3827 long do_sigreturn(CPUCRISState *env) 3828 { 3829 struct target_signal_frame *frame; 3830 abi_ulong frame_addr; 3831 target_sigset_t target_set; 3832 sigset_t set; 3833 int i; 3834 3835 frame_addr = env->regs[R_SP]; 3836 /* Make sure the guest isn't playing games. */ 3837 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1)) 3838 goto badframe; 3839 3840 /* Restore blocked signals */ 3841 if (__get_user(target_set.sig[0], &frame->sc.oldmask)) 3842 goto badframe; 3843 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 3844 if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) 3845 goto badframe; 3846 } 3847 target_to_host_sigset_internal(&set, &target_set); 3848 do_sigprocmask(SIG_SETMASK, &set, NULL); 3849 3850 restore_sigcontext(&frame->sc, env); 3851 unlock_user_struct(frame, frame_addr, 0); 3852 return env->regs[10]; 3853 badframe: 3854 unlock_user_struct(frame, frame_addr, 0); 3855 force_sig(TARGET_SIGSEGV); 3856 } 3857 3858 long do_rt_sigreturn(CPUCRISState *env) 3859 { 3860 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n"); 3861 return -TARGET_ENOSYS; 3862 } 3863 3864 #elif defined(TARGET_OPENRISC) 3865 3866 struct target_sigcontext { 3867 struct target_pt_regs regs; 3868 abi_ulong oldmask; 3869 abi_ulong usp; 3870 }; 3871 3872 struct target_ucontext { 3873 abi_ulong tuc_flags; 3874 abi_ulong tuc_link; 3875 target_stack_t tuc_stack; 3876 struct target_sigcontext tuc_mcontext; 3877 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 3878 }; 3879 3880 struct target_rt_sigframe { 3881 abi_ulong pinfo; 3882 uint64_t puc; 3883 struct target_siginfo info; 3884 struct target_sigcontext sc; 3885 struct target_ucontext uc; 3886 unsigned char retcode[16]; /* trampoline code */ 3887 }; 3888 3889 /* This is the asm-generic/ucontext.h version */ 3890 #if 0 3891 static int restore_sigcontext(CPUOpenRISCState *regs, 3892 struct target_sigcontext *sc) 3893 { 3894 unsigned int err = 0; 3895 unsigned long old_usp; 3896 3897 /* Alwys make any pending restarted system call return -EINTR */ 3898 current_thread_info()->restart_block.fn = do_no_restart_syscall; 3899 3900 /* restore the regs from &sc->regs (same as sc, since regs is first) 3901 * (sc is already checked for VERIFY_READ since the sigframe was 3902 * checked in sys_sigreturn previously) 3903 */ 3904 3905 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) { 3906 goto badframe; 3907 } 3908 3909 /* make sure the U-flag is set so user-mode cannot fool us */ 3910 3911 regs->sr &= ~SR_SM; 3912 3913 /* restore the old USP as it was before we stacked the sc etc. 3914 * (we cannot just pop the sigcontext since we aligned the sp and 3915 * stuff after pushing it) 3916 */ 3917 3918 __get_user(old_usp, &sc->usp); 3919 phx_signal("old_usp 0x%lx", old_usp); 3920 3921 __PHX__ REALLY /* ??? */ 3922 wrusp(old_usp); 3923 regs->gpr[1] = old_usp; 3924 3925 /* TODO: the other ports use regs->orig_XX to disable syscall checks 3926 * after this completes, but we don't use that mechanism. maybe we can 3927 * use it now ? 3928 */ 3929 3930 return err; 3931 3932 badframe: 3933 return 1; 3934 } 3935 #endif 3936 3937 /* Set up a signal frame. */ 3938 3939 static void setup_sigcontext(struct target_sigcontext *sc, 3940 CPUOpenRISCState *regs, 3941 unsigned long mask) 3942 { 3943 unsigned long usp = regs->gpr[1]; 3944 3945 /* copy the regs. they are first in sc so we can use sc directly */ 3946 3947 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/ 3948 3949 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of 3950 the signal handler. The frametype will be restored to its previous 3951 value in restore_sigcontext. */ 3952 /*regs->frametype = CRIS_FRAME_NORMAL;*/ 3953 3954 /* then some other stuff */ 3955 __put_user(mask, &sc->oldmask); 3956 __put_user(usp, &sc->usp); 3957 } 3958 3959 static inline unsigned long align_sigframe(unsigned long sp) 3960 { 3961 unsigned long i; 3962 i = sp & ~3UL; 3963 return i; 3964 } 3965 3966 static inline abi_ulong get_sigframe(struct target_sigaction *ka, 3967 CPUOpenRISCState *regs, 3968 size_t frame_size) 3969 { 3970 unsigned long sp = regs->gpr[1]; 3971 int onsigstack = on_sig_stack(sp); 3972 3973 /* redzone */ 3974 /* This is the X/Open sanctioned signal stack switching. */ 3975 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) { 3976 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 3977 } 3978 3979 sp = align_sigframe(sp - frame_size); 3980 3981 /* 3982 * If we are on the alternate signal stack and would overflow it, don't. 3983 * Return an always-bogus address instead so we will die with SIGSEGV. 3984 */ 3985 3986 if (onsigstack && !likely(on_sig_stack(sp))) { 3987 return -1L; 3988 } 3989 3990 return sp; 3991 } 3992 3993 static void setup_frame(int sig, struct target_sigaction *ka, 3994 target_sigset_t *set, CPUOpenRISCState *env) 3995 { 3996 qemu_log("Not implement.\n"); 3997 } 3998 3999 static void setup_rt_frame(int sig, struct target_sigaction *ka, 4000 target_siginfo_t *info, 4001 target_sigset_t *set, CPUOpenRISCState *env) 4002 { 4003 int err = 0; 4004 abi_ulong frame_addr; 4005 unsigned long return_ip; 4006 struct target_rt_sigframe *frame; 4007 abi_ulong info_addr, uc_addr; 4008 4009 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 4010 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 4011 goto give_sigsegv; 4012 } 4013 4014 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info); 4015 __put_user(info_addr, &frame->pinfo); 4016 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc); 4017 __put_user(uc_addr, &frame->puc); 4018 4019 if (ka->sa_flags & SA_SIGINFO) { 4020 copy_siginfo_to_user(&frame->info, info); 4021 } 4022 4023 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/ 4024 __put_user(0, &frame->uc.tuc_flags); 4025 __put_user(0, &frame->uc.tuc_link); 4026 __put_user(target_sigaltstack_used.ss_sp, 4027 &frame->uc.tuc_stack.ss_sp); 4028 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags); 4029 __put_user(target_sigaltstack_used.ss_size, 4030 &frame->uc.tuc_stack.ss_size); 4031 setup_sigcontext(&frame->sc, env, set->sig[0]); 4032 4033 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/ 4034 4035 /* trampoline - the desired return ip is the retcode itself */ 4036 return_ip = (unsigned long)&frame->retcode; 4037 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */ 4038 __put_user(0xa960, (short *)(frame->retcode + 0)); 4039 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2)); 4040 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4)); 4041 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8)); 4042 4043 if (err) { 4044 goto give_sigsegv; 4045 } 4046 4047 /* TODO what is the current->exec_domain stuff and invmap ? */ 4048 4049 /* Set up registers for signal handler */ 4050 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */ 4051 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */ 4052 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */ 4053 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */ 4054 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */ 4055 4056 /* actually move the usp to reflect the stacked frame */ 4057 env->gpr[1] = (unsigned long)frame; 4058 4059 return; 4060 4061 give_sigsegv: 4062 unlock_user_struct(frame, frame_addr, 1); 4063 if (sig == TARGET_SIGSEGV) { 4064 ka->_sa_handler = TARGET_SIG_DFL; 4065 } 4066 force_sig(TARGET_SIGSEGV); 4067 } 4068 4069 long do_sigreturn(CPUOpenRISCState *env) 4070 { 4071 4072 qemu_log("do_sigreturn: not implemented\n"); 4073 return -TARGET_ENOSYS; 4074 } 4075 4076 long do_rt_sigreturn(CPUOpenRISCState *env) 4077 { 4078 qemu_log("do_rt_sigreturn: not implemented\n"); 4079 return -TARGET_ENOSYS; 4080 } 4081 /* TARGET_OPENRISC */ 4082 4083 #elif defined(TARGET_S390X) 4084 4085 #define __NUM_GPRS 16 4086 #define __NUM_FPRS 16 4087 #define __NUM_ACRS 16 4088 4089 #define S390_SYSCALL_SIZE 2 4090 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */ 4091 4092 #define _SIGCONTEXT_NSIG 64 4093 #define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */ 4094 #define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW) 4095 #define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS) 4096 #define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */ 4097 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00) 4098 4099 typedef struct { 4100 target_psw_t psw; 4101 target_ulong gprs[__NUM_GPRS]; 4102 unsigned int acrs[__NUM_ACRS]; 4103 } target_s390_regs_common; 4104 4105 typedef struct { 4106 unsigned int fpc; 4107 double fprs[__NUM_FPRS]; 4108 } target_s390_fp_regs; 4109 4110 typedef struct { 4111 target_s390_regs_common regs; 4112 target_s390_fp_regs fpregs; 4113 } target_sigregs; 4114 4115 struct target_sigcontext { 4116 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS]; 4117 target_sigregs *sregs; 4118 }; 4119 4120 typedef struct { 4121 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE]; 4122 struct target_sigcontext sc; 4123 target_sigregs sregs; 4124 int signo; 4125 uint8_t retcode[S390_SYSCALL_SIZE]; 4126 } sigframe; 4127 4128 struct target_ucontext { 4129 target_ulong tuc_flags; 4130 struct target_ucontext *tuc_link; 4131 target_stack_t tuc_stack; 4132 target_sigregs tuc_mcontext; 4133 target_sigset_t tuc_sigmask; /* mask last for extensibility */ 4134 }; 4135 4136 typedef struct { 4137 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE]; 4138 uint8_t retcode[S390_SYSCALL_SIZE]; 4139 struct target_siginfo info; 4140 struct target_ucontext uc; 4141 } rt_sigframe; 4142 4143 static inline abi_ulong 4144 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size) 4145 { 4146 abi_ulong sp; 4147 4148 /* Default to using normal stack */ 4149 sp = env->regs[15]; 4150 4151 /* This is the X/Open sanctioned signal stack switching. */ 4152 if (ka->sa_flags & TARGET_SA_ONSTACK) { 4153 if (!sas_ss_flags(sp)) { 4154 sp = target_sigaltstack_used.ss_sp + 4155 target_sigaltstack_used.ss_size; 4156 } 4157 } 4158 4159 /* This is the legacy signal stack switching. */ 4160 else if (/* FIXME !user_mode(regs) */ 0 && 4161 !(ka->sa_flags & TARGET_SA_RESTORER) && 4162 ka->sa_restorer) { 4163 sp = (abi_ulong) ka->sa_restorer; 4164 } 4165 4166 return (sp - frame_size) & -8ul; 4167 } 4168 4169 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs) 4170 { 4171 int i; 4172 //save_access_regs(current->thread.acrs); FIXME 4173 4174 /* Copy a 'clean' PSW mask to the user to avoid leaking 4175 information about whether PER is currently on. */ 4176 __put_user(env->psw.mask, &sregs->regs.psw.mask); 4177 __put_user(env->psw.addr, &sregs->regs.psw.addr); 4178 for (i = 0; i < 16; i++) { 4179 __put_user(env->regs[i], &sregs->regs.gprs[i]); 4180 } 4181 for (i = 0; i < 16; i++) { 4182 __put_user(env->aregs[i], &sregs->regs.acrs[i]); 4183 } 4184 /* 4185 * We have to store the fp registers to current->thread.fp_regs 4186 * to merge them with the emulated registers. 4187 */ 4188 //save_fp_regs(¤t->thread.fp_regs); FIXME 4189 for (i = 0; i < 16; i++) { 4190 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]); 4191 } 4192 } 4193 4194 static void setup_frame(int sig, struct target_sigaction *ka, 4195 target_sigset_t *set, CPUS390XState *env) 4196 { 4197 sigframe *frame; 4198 abi_ulong frame_addr; 4199 4200 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 4201 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__, 4202 (unsigned long long)frame_addr); 4203 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 4204 goto give_sigsegv; 4205 } 4206 4207 qemu_log("%s: 1\n", __FUNCTION__); 4208 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) { 4209 goto give_sigsegv; 4210 } 4211 4212 save_sigregs(env, &frame->sregs); 4213 4214 __put_user((abi_ulong)(unsigned long)&frame->sregs, 4215 (abi_ulong *)&frame->sc.sregs); 4216 4217 /* Set up to return from userspace. If provided, use a stub 4218 already in userspace. */ 4219 if (ka->sa_flags & TARGET_SA_RESTORER) { 4220 env->regs[14] = (unsigned long) 4221 ka->sa_restorer | PSW_ADDR_AMODE; 4222 } else { 4223 env->regs[14] = (unsigned long) 4224 frame->retcode | PSW_ADDR_AMODE; 4225 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn, 4226 (uint16_t *)(frame->retcode))) 4227 goto give_sigsegv; 4228 } 4229 4230 /* Set up backchain. */ 4231 if (__put_user(env->regs[15], (abi_ulong *) frame)) { 4232 goto give_sigsegv; 4233 } 4234 4235 /* Set up registers for signal handler */ 4236 env->regs[15] = frame_addr; 4237 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE; 4238 4239 env->regs[2] = sig; //map_signal(sig); 4240 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc); 4241 4242 /* We forgot to include these in the sigcontext. 4243 To avoid breaking binary compatibility, they are passed as args. */ 4244 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no; 4245 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr; 4246 4247 /* Place signal number on stack to allow backtrace from handler. */ 4248 if (__put_user(env->regs[2], (int *) &frame->signo)) { 4249 goto give_sigsegv; 4250 } 4251 unlock_user_struct(frame, frame_addr, 1); 4252 return; 4253 4254 give_sigsegv: 4255 qemu_log("%s: give_sigsegv\n", __FUNCTION__); 4256 unlock_user_struct(frame, frame_addr, 1); 4257 force_sig(TARGET_SIGSEGV); 4258 } 4259 4260 static void setup_rt_frame(int sig, struct target_sigaction *ka, 4261 target_siginfo_t *info, 4262 target_sigset_t *set, CPUS390XState *env) 4263 { 4264 int i; 4265 rt_sigframe *frame; 4266 abi_ulong frame_addr; 4267 4268 frame_addr = get_sigframe(ka, env, sizeof *frame); 4269 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__, 4270 (unsigned long long)frame_addr); 4271 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 4272 goto give_sigsegv; 4273 } 4274 4275 qemu_log("%s: 1\n", __FUNCTION__); 4276 copy_siginfo_to_user(&frame->info, info); 4277 4278 /* Create the ucontext. */ 4279 __put_user(0, &frame->uc.tuc_flags); 4280 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link); 4281 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp); 4282 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), 4283 &frame->uc.tuc_stack.ss_flags); 4284 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size); 4285 save_sigregs(env, &frame->uc.tuc_mcontext); 4286 for (i = 0; i < TARGET_NSIG_WORDS; i++) { 4287 __put_user((abi_ulong)set->sig[i], 4288 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]); 4289 } 4290 4291 /* Set up to return from userspace. If provided, use a stub 4292 already in userspace. */ 4293 if (ka->sa_flags & TARGET_SA_RESTORER) { 4294 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE; 4295 } else { 4296 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE; 4297 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn, 4298 (uint16_t *)(frame->retcode))) { 4299 goto give_sigsegv; 4300 } 4301 } 4302 4303 /* Set up backchain. */ 4304 if (__put_user(env->regs[15], (abi_ulong *) frame)) { 4305 goto give_sigsegv; 4306 } 4307 4308 /* Set up registers for signal handler */ 4309 env->regs[15] = frame_addr; 4310 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE; 4311 4312 env->regs[2] = sig; //map_signal(sig); 4313 env->regs[3] = frame_addr + offsetof(typeof(*frame), info); 4314 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc); 4315 return; 4316 4317 give_sigsegv: 4318 qemu_log("%s: give_sigsegv\n", __FUNCTION__); 4319 unlock_user_struct(frame, frame_addr, 1); 4320 force_sig(TARGET_SIGSEGV); 4321 } 4322 4323 static int 4324 restore_sigregs(CPUS390XState *env, target_sigregs *sc) 4325 { 4326 int err = 0; 4327 int i; 4328 4329 for (i = 0; i < 16; i++) { 4330 __get_user(env->regs[i], &sc->regs.gprs[i]); 4331 } 4332 4333 __get_user(env->psw.mask, &sc->regs.psw.mask); 4334 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n", 4335 __FUNCTION__, (unsigned long long)sc->regs.psw.addr, 4336 (unsigned long long)env->psw.addr); 4337 __get_user(env->psw.addr, &sc->regs.psw.addr); 4338 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */ 4339 4340 for (i = 0; i < 16; i++) { 4341 __get_user(env->aregs[i], &sc->regs.acrs[i]); 4342 } 4343 for (i = 0; i < 16; i++) { 4344 __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]); 4345 } 4346 4347 return err; 4348 } 4349 4350 long do_sigreturn(CPUS390XState *env) 4351 { 4352 sigframe *frame; 4353 abi_ulong frame_addr = env->regs[15]; 4354 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__, 4355 (unsigned long long)frame_addr); 4356 target_sigset_t target_set; 4357 sigset_t set; 4358 4359 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 4360 goto badframe; 4361 } 4362 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) { 4363 goto badframe; 4364 } 4365 4366 target_to_host_sigset_internal(&set, &target_set); 4367 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */ 4368 4369 if (restore_sigregs(env, &frame->sregs)) { 4370 goto badframe; 4371 } 4372 4373 unlock_user_struct(frame, frame_addr, 0); 4374 return env->regs[2]; 4375 4376 badframe: 4377 unlock_user_struct(frame, frame_addr, 0); 4378 force_sig(TARGET_SIGSEGV); 4379 return 0; 4380 } 4381 4382 long do_rt_sigreturn(CPUS390XState *env) 4383 { 4384 rt_sigframe *frame; 4385 abi_ulong frame_addr = env->regs[15]; 4386 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__, 4387 (unsigned long long)frame_addr); 4388 sigset_t set; 4389 4390 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 4391 goto badframe; 4392 } 4393 target_to_host_sigset(&set, &frame->uc.tuc_sigmask); 4394 4395 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */ 4396 4397 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) { 4398 goto badframe; 4399 } 4400 4401 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0, 4402 get_sp_from_cpustate(env)) == -EFAULT) { 4403 goto badframe; 4404 } 4405 unlock_user_struct(frame, frame_addr, 0); 4406 return env->regs[2]; 4407 4408 badframe: 4409 unlock_user_struct(frame, frame_addr, 0); 4410 force_sig(TARGET_SIGSEGV); 4411 return 0; 4412 } 4413 4414 #elif defined(TARGET_PPC) && !defined(TARGET_PPC64) 4415 4416 /* FIXME: Many of the structures are defined for both PPC and PPC64, but 4417 the signal handling is different enough that we haven't implemented 4418 support for PPC64 yet. Hence the restriction above. 4419 4420 There are various #if'd blocks for code for TARGET_PPC64. These 4421 blocks should go away so that we can successfully run 32-bit and 4422 64-bit binaries on a QEMU configured for PPC64. */ 4423 4424 /* Size of dummy stack frame allocated when calling signal handler. 4425 See arch/powerpc/include/asm/ptrace.h. */ 4426 #if defined(TARGET_PPC64) 4427 #define SIGNAL_FRAMESIZE 128 4428 #else 4429 #define SIGNAL_FRAMESIZE 64 4430 #endif 4431 4432 /* See arch/powerpc/include/asm/sigcontext.h. */ 4433 struct target_sigcontext { 4434 target_ulong _unused[4]; 4435 int32_t signal; 4436 #if defined(TARGET_PPC64) 4437 int32_t pad0; 4438 #endif 4439 target_ulong handler; 4440 target_ulong oldmask; 4441 target_ulong regs; /* struct pt_regs __user * */ 4442 /* TODO: PPC64 includes extra bits here. */ 4443 }; 4444 4445 /* Indices for target_mcontext.mc_gregs, below. 4446 See arch/powerpc/include/asm/ptrace.h for details. */ 4447 enum { 4448 TARGET_PT_R0 = 0, 4449 TARGET_PT_R1 = 1, 4450 TARGET_PT_R2 = 2, 4451 TARGET_PT_R3 = 3, 4452 TARGET_PT_R4 = 4, 4453 TARGET_PT_R5 = 5, 4454 TARGET_PT_R6 = 6, 4455 TARGET_PT_R7 = 7, 4456 TARGET_PT_R8 = 8, 4457 TARGET_PT_R9 = 9, 4458 TARGET_PT_R10 = 10, 4459 TARGET_PT_R11 = 11, 4460 TARGET_PT_R12 = 12, 4461 TARGET_PT_R13 = 13, 4462 TARGET_PT_R14 = 14, 4463 TARGET_PT_R15 = 15, 4464 TARGET_PT_R16 = 16, 4465 TARGET_PT_R17 = 17, 4466 TARGET_PT_R18 = 18, 4467 TARGET_PT_R19 = 19, 4468 TARGET_PT_R20 = 20, 4469 TARGET_PT_R21 = 21, 4470 TARGET_PT_R22 = 22, 4471 TARGET_PT_R23 = 23, 4472 TARGET_PT_R24 = 24, 4473 TARGET_PT_R25 = 25, 4474 TARGET_PT_R26 = 26, 4475 TARGET_PT_R27 = 27, 4476 TARGET_PT_R28 = 28, 4477 TARGET_PT_R29 = 29, 4478 TARGET_PT_R30 = 30, 4479 TARGET_PT_R31 = 31, 4480 TARGET_PT_NIP = 32, 4481 TARGET_PT_MSR = 33, 4482 TARGET_PT_ORIG_R3 = 34, 4483 TARGET_PT_CTR = 35, 4484 TARGET_PT_LNK = 36, 4485 TARGET_PT_XER = 37, 4486 TARGET_PT_CCR = 38, 4487 /* Yes, there are two registers with #39. One is 64-bit only. */ 4488 TARGET_PT_MQ = 39, 4489 TARGET_PT_SOFTE = 39, 4490 TARGET_PT_TRAP = 40, 4491 TARGET_PT_DAR = 41, 4492 TARGET_PT_DSISR = 42, 4493 TARGET_PT_RESULT = 43, 4494 TARGET_PT_REGS_COUNT = 44 4495 }; 4496 4497 /* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC; 4498 on 64-bit PPC, sigcontext and mcontext are one and the same. */ 4499 struct target_mcontext { 4500 target_ulong mc_gregs[48]; 4501 /* Includes fpscr. */ 4502 uint64_t mc_fregs[33]; 4503 target_ulong mc_pad[2]; 4504 /* We need to handle Altivec and SPE at the same time, which no 4505 kernel needs to do. Fortunately, the kernel defines this bit to 4506 be Altivec-register-large all the time, rather than trying to 4507 twiddle it based on the specific platform. */ 4508 union { 4509 /* SPE vector registers. One extra for SPEFSCR. */ 4510 uint32_t spe[33]; 4511 /* Altivec vector registers. The packing of VSCR and VRSAVE 4512 varies depending on whether we're PPC64 or not: PPC64 splits 4513 them apart; PPC32 stuffs them together. */ 4514 #if defined(TARGET_PPC64) 4515 #define QEMU_NVRREG 34 4516 #else 4517 #define QEMU_NVRREG 33 4518 #endif 4519 ppc_avr_t altivec[QEMU_NVRREG]; 4520 #undef QEMU_NVRREG 4521 } mc_vregs __attribute__((__aligned__(16))); 4522 }; 4523 4524 struct target_ucontext { 4525 target_ulong tuc_flags; 4526 target_ulong tuc_link; /* struct ucontext __user * */ 4527 struct target_sigaltstack tuc_stack; 4528 #if !defined(TARGET_PPC64) 4529 int32_t tuc_pad[7]; 4530 target_ulong tuc_regs; /* struct mcontext __user * 4531 points to uc_mcontext field */ 4532 #endif 4533 target_sigset_t tuc_sigmask; 4534 #if defined(TARGET_PPC64) 4535 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */ 4536 struct target_sigcontext tuc_mcontext; 4537 #else 4538 int32_t tuc_maskext[30]; 4539 int32_t tuc_pad2[3]; 4540 struct target_mcontext tuc_mcontext; 4541 #endif 4542 }; 4543 4544 /* See arch/powerpc/kernel/signal_32.c. */ 4545 struct target_sigframe { 4546 struct target_sigcontext sctx; 4547 struct target_mcontext mctx; 4548 int32_t abigap[56]; 4549 }; 4550 4551 struct target_rt_sigframe { 4552 struct target_siginfo info; 4553 struct target_ucontext uc; 4554 int32_t abigap[56]; 4555 }; 4556 4557 /* We use the mc_pad field for the signal return trampoline. */ 4558 #define tramp mc_pad 4559 4560 /* See arch/powerpc/kernel/signal.c. */ 4561 static target_ulong get_sigframe(struct target_sigaction *ka, 4562 CPUPPCState *env, 4563 int frame_size) 4564 { 4565 target_ulong oldsp, newsp; 4566 4567 oldsp = env->gpr[1]; 4568 4569 if ((ka->sa_flags & TARGET_SA_ONSTACK) && 4570 (sas_ss_flags(oldsp) == 0)) { 4571 oldsp = (target_sigaltstack_used.ss_sp 4572 + target_sigaltstack_used.ss_size); 4573 } 4574 4575 newsp = (oldsp - frame_size) & ~0xFUL; 4576 4577 return newsp; 4578 } 4579 4580 static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame, 4581 int sigret) 4582 { 4583 target_ulong msr = env->msr; 4584 int i; 4585 target_ulong ccr = 0; 4586 4587 /* In general, the kernel attempts to be intelligent about what it 4588 needs to save for Altivec/FP/SPE registers. We don't care that 4589 much, so we just go ahead and save everything. */ 4590 4591 /* Save general registers. */ 4592 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 4593 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) { 4594 return 1; 4595 } 4596 } 4597 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]) 4598 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]) 4599 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]) 4600 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER])) 4601 return 1; 4602 4603 for (i = 0; i < ARRAY_SIZE(env->crf); i++) { 4604 ccr |= env->crf[i] << (32 - ((i + 1) * 4)); 4605 } 4606 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR])) 4607 return 1; 4608 4609 /* Save Altivec registers if necessary. */ 4610 if (env->insns_flags & PPC_ALTIVEC) { 4611 for (i = 0; i < ARRAY_SIZE(env->avr); i++) { 4612 ppc_avr_t *avr = &env->avr[i]; 4613 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i]; 4614 4615 if (__put_user(avr->u64[0], &vreg->u64[0]) || 4616 __put_user(avr->u64[1], &vreg->u64[1])) { 4617 return 1; 4618 } 4619 } 4620 /* Set MSR_VR in the saved MSR value to indicate that 4621 frame->mc_vregs contains valid data. */ 4622 msr |= MSR_VR; 4623 if (__put_user((uint32_t)env->spr[SPR_VRSAVE], 4624 &frame->mc_vregs.altivec[32].u32[3])) 4625 return 1; 4626 } 4627 4628 /* Save floating point registers. */ 4629 if (env->insns_flags & PPC_FLOAT) { 4630 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) { 4631 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) { 4632 return 1; 4633 } 4634 } 4635 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32])) 4636 return 1; 4637 } 4638 4639 /* Save SPE registers. The kernel only saves the high half. */ 4640 if (env->insns_flags & PPC_SPE) { 4641 #if defined(TARGET_PPC64) 4642 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 4643 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) { 4644 return 1; 4645 } 4646 } 4647 #else 4648 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) { 4649 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) { 4650 return 1; 4651 } 4652 } 4653 #endif 4654 /* Set MSR_SPE in the saved MSR value to indicate that 4655 frame->mc_vregs contains valid data. */ 4656 msr |= MSR_SPE; 4657 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32])) 4658 return 1; 4659 } 4660 4661 /* Store MSR. */ 4662 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR])) 4663 return 1; 4664 4665 /* Set up the sigreturn trampoline: li r0,sigret; sc. */ 4666 if (sigret) { 4667 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) || 4668 __put_user(0x44000002UL, &frame->tramp[1])) { 4669 return 1; 4670 } 4671 } 4672 4673 return 0; 4674 } 4675 4676 static int restore_user_regs(CPUPPCState *env, 4677 struct target_mcontext *frame, int sig) 4678 { 4679 target_ulong save_r2 = 0; 4680 target_ulong msr; 4681 target_ulong ccr; 4682 4683 int i; 4684 4685 if (!sig) { 4686 save_r2 = env->gpr[2]; 4687 } 4688 4689 /* Restore general registers. */ 4690 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 4691 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) { 4692 return 1; 4693 } 4694 } 4695 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP]) 4696 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR]) 4697 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK]) 4698 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER])) 4699 return 1; 4700 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR])) 4701 return 1; 4702 4703 for (i = 0; i < ARRAY_SIZE(env->crf); i++) { 4704 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf; 4705 } 4706 4707 if (!sig) { 4708 env->gpr[2] = save_r2; 4709 } 4710 /* Restore MSR. */ 4711 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR])) 4712 return 1; 4713 4714 /* If doing signal return, restore the previous little-endian mode. */ 4715 if (sig) 4716 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE); 4717 4718 /* Restore Altivec registers if necessary. */ 4719 if (env->insns_flags & PPC_ALTIVEC) { 4720 for (i = 0; i < ARRAY_SIZE(env->avr); i++) { 4721 ppc_avr_t *avr = &env->avr[i]; 4722 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i]; 4723 4724 if (__get_user(avr->u64[0], &vreg->u64[0]) || 4725 __get_user(avr->u64[1], &vreg->u64[1])) { 4726 return 1; 4727 } 4728 } 4729 /* Set MSR_VEC in the saved MSR value to indicate that 4730 frame->mc_vregs contains valid data. */ 4731 if (__get_user(env->spr[SPR_VRSAVE], 4732 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3]))) 4733 return 1; 4734 } 4735 4736 /* Restore floating point registers. */ 4737 if (env->insns_flags & PPC_FLOAT) { 4738 uint64_t fpscr; 4739 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) { 4740 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) { 4741 return 1; 4742 } 4743 } 4744 if (__get_user(fpscr, &frame->mc_fregs[32])) 4745 return 1; 4746 env->fpscr = (uint32_t) fpscr; 4747 } 4748 4749 /* Save SPE registers. The kernel only saves the high half. */ 4750 if (env->insns_flags & PPC_SPE) { 4751 #if defined(TARGET_PPC64) 4752 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) { 4753 uint32_t hi; 4754 4755 if (__get_user(hi, &frame->mc_vregs.spe[i])) { 4756 return 1; 4757 } 4758 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]); 4759 } 4760 #else 4761 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) { 4762 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) { 4763 return 1; 4764 } 4765 } 4766 #endif 4767 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32])) 4768 return 1; 4769 } 4770 4771 return 0; 4772 } 4773 4774 static void setup_frame(int sig, struct target_sigaction *ka, 4775 target_sigset_t *set, CPUPPCState *env) 4776 { 4777 struct target_sigframe *frame; 4778 struct target_sigcontext *sc; 4779 target_ulong frame_addr, newsp; 4780 int err = 0; 4781 int signal; 4782 4783 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 4784 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1)) 4785 goto sigsegv; 4786 sc = &frame->sctx; 4787 4788 signal = current_exec_domain_sig(sig); 4789 4790 __put_user(ka->_sa_handler, &sc->handler); 4791 __put_user(set->sig[0], &sc->oldmask); 4792 #if defined(TARGET_PPC64) 4793 __put_user(set->sig[0] >> 32, &sc->_unused[3]); 4794 #else 4795 __put_user(set->sig[1], &sc->_unused[3]); 4796 #endif 4797 __put_user(h2g(&frame->mctx), &sc->regs); 4798 __put_user(sig, &sc->signal); 4799 4800 /* Save user regs. */ 4801 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn); 4802 4803 /* The kernel checks for the presence of a VDSO here. We don't 4804 emulate a vdso, so use a sigreturn system call. */ 4805 env->lr = (target_ulong) h2g(frame->mctx.tramp); 4806 4807 /* Turn off all fp exceptions. */ 4808 env->fpscr = 0; 4809 4810 /* Create a stack frame for the caller of the handler. */ 4811 newsp = frame_addr - SIGNAL_FRAMESIZE; 4812 err |= put_user(env->gpr[1], newsp, target_ulong); 4813 4814 if (err) 4815 goto sigsegv; 4816 4817 /* Set up registers for signal handler. */ 4818 env->gpr[1] = newsp; 4819 env->gpr[3] = signal; 4820 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx); 4821 env->nip = (target_ulong) ka->_sa_handler; 4822 /* Signal handlers are entered in big-endian mode. */ 4823 env->msr &= ~MSR_LE; 4824 4825 unlock_user_struct(frame, frame_addr, 1); 4826 return; 4827 4828 sigsegv: 4829 unlock_user_struct(frame, frame_addr, 1); 4830 qemu_log("segfaulting from setup_frame\n"); 4831 force_sig(TARGET_SIGSEGV); 4832 } 4833 4834 static void setup_rt_frame(int sig, struct target_sigaction *ka, 4835 target_siginfo_t *info, 4836 target_sigset_t *set, CPUPPCState *env) 4837 { 4838 struct target_rt_sigframe *rt_sf; 4839 struct target_mcontext *frame; 4840 target_ulong rt_sf_addr, newsp = 0; 4841 int i, err = 0; 4842 int signal; 4843 4844 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf)); 4845 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1)) 4846 goto sigsegv; 4847 4848 signal = current_exec_domain_sig(sig); 4849 4850 copy_siginfo_to_user(&rt_sf->info, info); 4851 4852 __put_user(0, &rt_sf->uc.tuc_flags); 4853 __put_user(0, &rt_sf->uc.tuc_link); 4854 __put_user((target_ulong)target_sigaltstack_used.ss_sp, 4855 &rt_sf->uc.tuc_stack.ss_sp); 4856 __put_user(sas_ss_flags(env->gpr[1]), 4857 &rt_sf->uc.tuc_stack.ss_flags); 4858 __put_user(target_sigaltstack_used.ss_size, 4859 &rt_sf->uc.tuc_stack.ss_size); 4860 __put_user(h2g (&rt_sf->uc.tuc_mcontext), 4861 &rt_sf->uc.tuc_regs); 4862 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 4863 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]); 4864 } 4865 4866 frame = &rt_sf->uc.tuc_mcontext; 4867 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn); 4868 4869 /* The kernel checks for the presence of a VDSO here. We don't 4870 emulate a vdso, so use a sigreturn system call. */ 4871 env->lr = (target_ulong) h2g(frame->tramp); 4872 4873 /* Turn off all fp exceptions. */ 4874 env->fpscr = 0; 4875 4876 /* Create a stack frame for the caller of the handler. */ 4877 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16); 4878 __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp); 4879 4880 if (err) 4881 goto sigsegv; 4882 4883 /* Set up registers for signal handler. */ 4884 env->gpr[1] = newsp; 4885 env->gpr[3] = (target_ulong) signal; 4886 env->gpr[4] = (target_ulong) h2g(&rt_sf->info); 4887 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc); 4888 env->gpr[6] = (target_ulong) h2g(rt_sf); 4889 env->nip = (target_ulong) ka->_sa_handler; 4890 /* Signal handlers are entered in big-endian mode. */ 4891 env->msr &= ~MSR_LE; 4892 4893 unlock_user_struct(rt_sf, rt_sf_addr, 1); 4894 return; 4895 4896 sigsegv: 4897 unlock_user_struct(rt_sf, rt_sf_addr, 1); 4898 qemu_log("segfaulting from setup_rt_frame\n"); 4899 force_sig(TARGET_SIGSEGV); 4900 4901 } 4902 4903 long do_sigreturn(CPUPPCState *env) 4904 { 4905 struct target_sigcontext *sc = NULL; 4906 struct target_mcontext *sr = NULL; 4907 target_ulong sr_addr = 0, sc_addr; 4908 sigset_t blocked; 4909 target_sigset_t set; 4910 4911 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE; 4912 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) 4913 goto sigsegv; 4914 4915 #if defined(TARGET_PPC64) 4916 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32); 4917 #else 4918 if(__get_user(set.sig[0], &sc->oldmask) || 4919 __get_user(set.sig[1], &sc->_unused[3])) 4920 goto sigsegv; 4921 #endif 4922 target_to_host_sigset_internal(&blocked, &set); 4923 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 4924 4925 if (__get_user(sr_addr, &sc->regs)) 4926 goto sigsegv; 4927 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1)) 4928 goto sigsegv; 4929 if (restore_user_regs(env, sr, 1)) 4930 goto sigsegv; 4931 4932 unlock_user_struct(sr, sr_addr, 1); 4933 unlock_user_struct(sc, sc_addr, 1); 4934 return -TARGET_QEMU_ESIGRETURN; 4935 4936 sigsegv: 4937 unlock_user_struct(sr, sr_addr, 1); 4938 unlock_user_struct(sc, sc_addr, 1); 4939 qemu_log("segfaulting from do_sigreturn\n"); 4940 force_sig(TARGET_SIGSEGV); 4941 return 0; 4942 } 4943 4944 /* See arch/powerpc/kernel/signal_32.c. */ 4945 static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig) 4946 { 4947 struct target_mcontext *mcp; 4948 target_ulong mcp_addr; 4949 sigset_t blocked; 4950 target_sigset_t set; 4951 4952 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask), 4953 sizeof (set))) 4954 return 1; 4955 4956 #if defined(TARGET_PPC64) 4957 fprintf (stderr, "do_setcontext: not implemented\n"); 4958 return 0; 4959 #else 4960 if (__get_user(mcp_addr, &ucp->tuc_regs)) 4961 return 1; 4962 4963 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1)) 4964 return 1; 4965 4966 target_to_host_sigset_internal(&blocked, &set); 4967 do_sigprocmask(SIG_SETMASK, &blocked, NULL); 4968 if (restore_user_regs(env, mcp, sig)) 4969 goto sigsegv; 4970 4971 unlock_user_struct(mcp, mcp_addr, 1); 4972 return 0; 4973 4974 sigsegv: 4975 unlock_user_struct(mcp, mcp_addr, 1); 4976 return 1; 4977 #endif 4978 } 4979 4980 long do_rt_sigreturn(CPUPPCState *env) 4981 { 4982 struct target_rt_sigframe *rt_sf = NULL; 4983 target_ulong rt_sf_addr; 4984 4985 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16; 4986 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1)) 4987 goto sigsegv; 4988 4989 if (do_setcontext(&rt_sf->uc, env, 1)) 4990 goto sigsegv; 4991 4992 do_sigaltstack(rt_sf_addr 4993 + offsetof(struct target_rt_sigframe, uc.tuc_stack), 4994 0, env->gpr[1]); 4995 4996 unlock_user_struct(rt_sf, rt_sf_addr, 1); 4997 return -TARGET_QEMU_ESIGRETURN; 4998 4999 sigsegv: 5000 unlock_user_struct(rt_sf, rt_sf_addr, 1); 5001 qemu_log("segfaulting from do_rt_sigreturn\n"); 5002 force_sig(TARGET_SIGSEGV); 5003 return 0; 5004 } 5005 5006 #elif defined(TARGET_M68K) 5007 5008 struct target_sigcontext { 5009 abi_ulong sc_mask; 5010 abi_ulong sc_usp; 5011 abi_ulong sc_d0; 5012 abi_ulong sc_d1; 5013 abi_ulong sc_a0; 5014 abi_ulong sc_a1; 5015 unsigned short sc_sr; 5016 abi_ulong sc_pc; 5017 }; 5018 5019 struct target_sigframe 5020 { 5021 abi_ulong pretcode; 5022 int sig; 5023 int code; 5024 abi_ulong psc; 5025 char retcode[8]; 5026 abi_ulong extramask[TARGET_NSIG_WORDS-1]; 5027 struct target_sigcontext sc; 5028 }; 5029 5030 typedef int target_greg_t; 5031 #define TARGET_NGREG 18 5032 typedef target_greg_t target_gregset_t[TARGET_NGREG]; 5033 5034 typedef struct target_fpregset { 5035 int f_fpcntl[3]; 5036 int f_fpregs[8*3]; 5037 } target_fpregset_t; 5038 5039 struct target_mcontext { 5040 int version; 5041 target_gregset_t gregs; 5042 target_fpregset_t fpregs; 5043 }; 5044 5045 #define TARGET_MCONTEXT_VERSION 2 5046 5047 struct target_ucontext { 5048 abi_ulong tuc_flags; 5049 abi_ulong tuc_link; 5050 target_stack_t tuc_stack; 5051 struct target_mcontext tuc_mcontext; 5052 abi_long tuc_filler[80]; 5053 target_sigset_t tuc_sigmask; 5054 }; 5055 5056 struct target_rt_sigframe 5057 { 5058 abi_ulong pretcode; 5059 int sig; 5060 abi_ulong pinfo; 5061 abi_ulong puc; 5062 char retcode[8]; 5063 struct target_siginfo info; 5064 struct target_ucontext uc; 5065 }; 5066 5067 static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env, 5068 abi_ulong mask) 5069 { 5070 __put_user(mask, &sc->sc_mask); 5071 __put_user(env->aregs[7], &sc->sc_usp); 5072 __put_user(env->dregs[0], &sc->sc_d0); 5073 __put_user(env->dregs[1], &sc->sc_d1); 5074 __put_user(env->aregs[0], &sc->sc_a0); 5075 __put_user(env->aregs[1], &sc->sc_a1); 5076 __put_user(env->sr, &sc->sc_sr); 5077 __put_user(env->pc, &sc->sc_pc); 5078 } 5079 5080 static void 5081 restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0) 5082 { 5083 int temp; 5084 5085 __get_user(env->aregs[7], &sc->sc_usp); 5086 __get_user(env->dregs[1], &sc->sc_d1); 5087 __get_user(env->aregs[0], &sc->sc_a0); 5088 __get_user(env->aregs[1], &sc->sc_a1); 5089 __get_user(env->pc, &sc->sc_pc); 5090 __get_user(temp, &sc->sc_sr); 5091 env->sr = (env->sr & 0xff00) | (temp & 0xff); 5092 5093 *pd0 = tswapl(sc->sc_d0); 5094 } 5095 5096 /* 5097 * Determine which stack to use.. 5098 */ 5099 static inline abi_ulong 5100 get_sigframe(struct target_sigaction *ka, CPUM68KState *regs, 5101 size_t frame_size) 5102 { 5103 unsigned long sp; 5104 5105 sp = regs->aregs[7]; 5106 5107 /* This is the X/Open sanctioned signal stack switching. */ 5108 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) { 5109 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 5110 } 5111 5112 return ((sp - frame_size) & -8UL); 5113 } 5114 5115 static void setup_frame(int sig, struct target_sigaction *ka, 5116 target_sigset_t *set, CPUM68KState *env) 5117 { 5118 struct target_sigframe *frame; 5119 abi_ulong frame_addr; 5120 abi_ulong retcode_addr; 5121 abi_ulong sc_addr; 5122 int err = 0; 5123 int i; 5124 5125 frame_addr = get_sigframe(ka, env, sizeof *frame); 5126 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 5127 goto give_sigsegv; 5128 5129 __put_user(sig, &frame->sig); 5130 5131 sc_addr = frame_addr + offsetof(struct target_sigframe, sc); 5132 __put_user(sc_addr, &frame->psc); 5133 5134 setup_sigcontext(&frame->sc, env, set->sig[0]); 5135 5136 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 5137 if (__put_user(set->sig[i], &frame->extramask[i - 1])) 5138 goto give_sigsegv; 5139 } 5140 5141 /* Set up to return from userspace. */ 5142 5143 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode); 5144 __put_user(retcode_addr, &frame->pretcode); 5145 5146 /* moveq #,d0; trap #0 */ 5147 5148 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16), 5149 (long *)(frame->retcode)); 5150 5151 if (err) 5152 goto give_sigsegv; 5153 5154 /* Set up to return from userspace */ 5155 5156 env->aregs[7] = frame_addr; 5157 env->pc = ka->_sa_handler; 5158 5159 unlock_user_struct(frame, frame_addr, 1); 5160 return; 5161 5162 give_sigsegv: 5163 unlock_user_struct(frame, frame_addr, 1); 5164 force_sig(TARGET_SIGSEGV); 5165 } 5166 5167 static inline int target_rt_setup_ucontext(struct target_ucontext *uc, 5168 CPUM68KState *env) 5169 { 5170 target_greg_t *gregs = uc->tuc_mcontext.gregs; 5171 5172 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version); 5173 __put_user(env->dregs[0], &gregs[0]); 5174 __put_user(env->dregs[1], &gregs[1]); 5175 __put_user(env->dregs[2], &gregs[2]); 5176 __put_user(env->dregs[3], &gregs[3]); 5177 __put_user(env->dregs[4], &gregs[4]); 5178 __put_user(env->dregs[5], &gregs[5]); 5179 __put_user(env->dregs[6], &gregs[6]); 5180 __put_user(env->dregs[7], &gregs[7]); 5181 __put_user(env->aregs[0], &gregs[8]); 5182 __put_user(env->aregs[1], &gregs[9]); 5183 __put_user(env->aregs[2], &gregs[10]); 5184 __put_user(env->aregs[3], &gregs[11]); 5185 __put_user(env->aregs[4], &gregs[12]); 5186 __put_user(env->aregs[5], &gregs[13]); 5187 __put_user(env->aregs[6], &gregs[14]); 5188 __put_user(env->aregs[7], &gregs[15]); 5189 __put_user(env->pc, &gregs[16]); 5190 __put_user(env->sr, &gregs[17]); 5191 5192 return 0; 5193 } 5194 5195 static inline int target_rt_restore_ucontext(CPUM68KState *env, 5196 struct target_ucontext *uc, 5197 int *pd0) 5198 { 5199 int temp; 5200 target_greg_t *gregs = uc->tuc_mcontext.gregs; 5201 5202 __get_user(temp, &uc->tuc_mcontext.version); 5203 if (temp != TARGET_MCONTEXT_VERSION) 5204 goto badframe; 5205 5206 /* restore passed registers */ 5207 __get_user(env->dregs[0], &gregs[0]); 5208 __get_user(env->dregs[1], &gregs[1]); 5209 __get_user(env->dregs[2], &gregs[2]); 5210 __get_user(env->dregs[3], &gregs[3]); 5211 __get_user(env->dregs[4], &gregs[4]); 5212 __get_user(env->dregs[5], &gregs[5]); 5213 __get_user(env->dregs[6], &gregs[6]); 5214 __get_user(env->dregs[7], &gregs[7]); 5215 __get_user(env->aregs[0], &gregs[8]); 5216 __get_user(env->aregs[1], &gregs[9]); 5217 __get_user(env->aregs[2], &gregs[10]); 5218 __get_user(env->aregs[3], &gregs[11]); 5219 __get_user(env->aregs[4], &gregs[12]); 5220 __get_user(env->aregs[5], &gregs[13]); 5221 __get_user(env->aregs[6], &gregs[14]); 5222 __get_user(env->aregs[7], &gregs[15]); 5223 __get_user(env->pc, &gregs[16]); 5224 __get_user(temp, &gregs[17]); 5225 env->sr = (env->sr & 0xff00) | (temp & 0xff); 5226 5227 *pd0 = env->dregs[0]; 5228 return 0; 5229 5230 badframe: 5231 return 1; 5232 } 5233 5234 static void setup_rt_frame(int sig, struct target_sigaction *ka, 5235 target_siginfo_t *info, 5236 target_sigset_t *set, CPUM68KState *env) 5237 { 5238 struct target_rt_sigframe *frame; 5239 abi_ulong frame_addr; 5240 abi_ulong retcode_addr; 5241 abi_ulong info_addr; 5242 abi_ulong uc_addr; 5243 int err = 0; 5244 int i; 5245 5246 frame_addr = get_sigframe(ka, env, sizeof *frame); 5247 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) 5248 goto give_sigsegv; 5249 5250 __put_user(sig, &frame->sig); 5251 5252 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info); 5253 __put_user(info_addr, &frame->pinfo); 5254 5255 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc); 5256 __put_user(uc_addr, &frame->puc); 5257 5258 copy_siginfo_to_user(&frame->info, info); 5259 5260 /* Create the ucontext */ 5261 5262 __put_user(0, &frame->uc.tuc_flags); 5263 __put_user(0, &frame->uc.tuc_link); 5264 __put_user(target_sigaltstack_used.ss_sp, 5265 &frame->uc.tuc_stack.ss_sp); 5266 __put_user(sas_ss_flags(env->aregs[7]), 5267 &frame->uc.tuc_stack.ss_flags); 5268 __put_user(target_sigaltstack_used.ss_size, 5269 &frame->uc.tuc_stack.ss_size); 5270 err |= target_rt_setup_ucontext(&frame->uc, env); 5271 5272 if (err) 5273 goto give_sigsegv; 5274 5275 for(i = 0; i < TARGET_NSIG_WORDS; i++) { 5276 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i])) 5277 goto give_sigsegv; 5278 } 5279 5280 /* Set up to return from userspace. */ 5281 5282 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode); 5283 __put_user(retcode_addr, &frame->pretcode); 5284 5285 /* moveq #,d0; notb d0; trap #0 */ 5286 5287 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16), 5288 (long *)(frame->retcode + 0)); 5289 __put_user(0x4e40, (short *)(frame->retcode + 4)); 5290 5291 if (err) 5292 goto give_sigsegv; 5293 5294 /* Set up to return from userspace */ 5295 5296 env->aregs[7] = frame_addr; 5297 env->pc = ka->_sa_handler; 5298 5299 unlock_user_struct(frame, frame_addr, 1); 5300 return; 5301 5302 give_sigsegv: 5303 unlock_user_struct(frame, frame_addr, 1); 5304 force_sig(TARGET_SIGSEGV); 5305 } 5306 5307 long do_sigreturn(CPUM68KState *env) 5308 { 5309 struct target_sigframe *frame; 5310 abi_ulong frame_addr = env->aregs[7] - 4; 5311 target_sigset_t target_set; 5312 sigset_t set; 5313 int d0, i; 5314 5315 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 5316 goto badframe; 5317 5318 /* set blocked signals */ 5319 5320 if (__get_user(target_set.sig[0], &frame->sc.sc_mask)) 5321 goto badframe; 5322 5323 for(i = 1; i < TARGET_NSIG_WORDS; i++) { 5324 if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) 5325 goto badframe; 5326 } 5327 5328 target_to_host_sigset_internal(&set, &target_set); 5329 do_sigprocmask(SIG_SETMASK, &set, NULL); 5330 5331 /* restore registers */ 5332 5333 restore_sigcontext(env, &frame->sc, &d0); 5334 5335 unlock_user_struct(frame, frame_addr, 0); 5336 return d0; 5337 5338 badframe: 5339 unlock_user_struct(frame, frame_addr, 0); 5340 force_sig(TARGET_SIGSEGV); 5341 return 0; 5342 } 5343 5344 long do_rt_sigreturn(CPUM68KState *env) 5345 { 5346 struct target_rt_sigframe *frame; 5347 abi_ulong frame_addr = env->aregs[7] - 4; 5348 target_sigset_t target_set; 5349 sigset_t set; 5350 int d0; 5351 5352 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) 5353 goto badframe; 5354 5355 target_to_host_sigset_internal(&set, &target_set); 5356 do_sigprocmask(SIG_SETMASK, &set, NULL); 5357 5358 /* restore registers */ 5359 5360 if (target_rt_restore_ucontext(env, &frame->uc, &d0)) 5361 goto badframe; 5362 5363 if (do_sigaltstack(frame_addr + 5364 offsetof(struct target_rt_sigframe, uc.tuc_stack), 5365 0, get_sp_from_cpustate(env)) == -EFAULT) 5366 goto badframe; 5367 5368 unlock_user_struct(frame, frame_addr, 0); 5369 return d0; 5370 5371 badframe: 5372 unlock_user_struct(frame, frame_addr, 0); 5373 force_sig(TARGET_SIGSEGV); 5374 return 0; 5375 } 5376 5377 #elif defined(TARGET_ALPHA) 5378 5379 struct target_sigcontext { 5380 abi_long sc_onstack; 5381 abi_long sc_mask; 5382 abi_long sc_pc; 5383 abi_long sc_ps; 5384 abi_long sc_regs[32]; 5385 abi_long sc_ownedfp; 5386 abi_long sc_fpregs[32]; 5387 abi_ulong sc_fpcr; 5388 abi_ulong sc_fp_control; 5389 abi_ulong sc_reserved1; 5390 abi_ulong sc_reserved2; 5391 abi_ulong sc_ssize; 5392 abi_ulong sc_sbase; 5393 abi_ulong sc_traparg_a0; 5394 abi_ulong sc_traparg_a1; 5395 abi_ulong sc_traparg_a2; 5396 abi_ulong sc_fp_trap_pc; 5397 abi_ulong sc_fp_trigger_sum; 5398 abi_ulong sc_fp_trigger_inst; 5399 }; 5400 5401 struct target_ucontext { 5402 abi_ulong tuc_flags; 5403 abi_ulong tuc_link; 5404 abi_ulong tuc_osf_sigmask; 5405 target_stack_t tuc_stack; 5406 struct target_sigcontext tuc_mcontext; 5407 target_sigset_t tuc_sigmask; 5408 }; 5409 5410 struct target_sigframe { 5411 struct target_sigcontext sc; 5412 unsigned int retcode[3]; 5413 }; 5414 5415 struct target_rt_sigframe { 5416 target_siginfo_t info; 5417 struct target_ucontext uc; 5418 unsigned int retcode[3]; 5419 }; 5420 5421 #define INSN_MOV_R30_R16 0x47fe0410 5422 #define INSN_LDI_R0 0x201f0000 5423 #define INSN_CALLSYS 0x00000083 5424 5425 static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env, 5426 abi_ulong frame_addr, target_sigset_t *set) 5427 { 5428 int i; 5429 5430 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack); 5431 __put_user(set->sig[0], &sc->sc_mask); 5432 __put_user(env->pc, &sc->sc_pc); 5433 __put_user(8, &sc->sc_ps); 5434 5435 for (i = 0; i < 31; ++i) { 5436 __put_user(env->ir[i], &sc->sc_regs[i]); 5437 } 5438 __put_user(0, &sc->sc_regs[31]); 5439 5440 for (i = 0; i < 31; ++i) { 5441 __put_user(env->fir[i], &sc->sc_fpregs[i]); 5442 } 5443 __put_user(0, &sc->sc_fpregs[31]); 5444 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr); 5445 5446 __put_user(0, &sc->sc_traparg_a0); /* FIXME */ 5447 __put_user(0, &sc->sc_traparg_a1); /* FIXME */ 5448 __put_user(0, &sc->sc_traparg_a2); /* FIXME */ 5449 } 5450 5451 static void restore_sigcontext(CPUAlphaState *env, 5452 struct target_sigcontext *sc) 5453 { 5454 uint64_t fpcr; 5455 int i; 5456 5457 __get_user(env->pc, &sc->sc_pc); 5458 5459 for (i = 0; i < 31; ++i) { 5460 __get_user(env->ir[i], &sc->sc_regs[i]); 5461 } 5462 for (i = 0; i < 31; ++i) { 5463 __get_user(env->fir[i], &sc->sc_fpregs[i]); 5464 } 5465 5466 __get_user(fpcr, &sc->sc_fpcr); 5467 cpu_alpha_store_fpcr(env, fpcr); 5468 } 5469 5470 static inline abi_ulong get_sigframe(struct target_sigaction *sa, 5471 CPUAlphaState *env, 5472 unsigned long framesize) 5473 { 5474 abi_ulong sp = env->ir[IR_SP]; 5475 5476 /* This is the X/Open sanctioned signal stack switching. */ 5477 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) { 5478 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size; 5479 } 5480 return (sp - framesize) & -32; 5481 } 5482 5483 static void setup_frame(int sig, struct target_sigaction *ka, 5484 target_sigset_t *set, CPUAlphaState *env) 5485 { 5486 abi_ulong frame_addr, r26; 5487 struct target_sigframe *frame; 5488 int err = 0; 5489 5490 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 5491 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 5492 goto give_sigsegv; 5493 } 5494 5495 setup_sigcontext(&frame->sc, env, frame_addr, set); 5496 5497 if (ka->sa_restorer) { 5498 r26 = ka->sa_restorer; 5499 } else { 5500 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]); 5501 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn, 5502 &frame->retcode[1]); 5503 __put_user(INSN_CALLSYS, &frame->retcode[2]); 5504 /* imb() */ 5505 r26 = frame_addr; 5506 } 5507 5508 unlock_user_struct(frame, frame_addr, 1); 5509 5510 if (err) { 5511 give_sigsegv: 5512 if (sig == TARGET_SIGSEGV) { 5513 ka->_sa_handler = TARGET_SIG_DFL; 5514 } 5515 force_sig(TARGET_SIGSEGV); 5516 } 5517 5518 env->ir[IR_RA] = r26; 5519 env->ir[IR_PV] = env->pc = ka->_sa_handler; 5520 env->ir[IR_A0] = sig; 5521 env->ir[IR_A1] = 0; 5522 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc); 5523 env->ir[IR_SP] = frame_addr; 5524 } 5525 5526 static void setup_rt_frame(int sig, struct target_sigaction *ka, 5527 target_siginfo_t *info, 5528 target_sigset_t *set, CPUAlphaState *env) 5529 { 5530 abi_ulong frame_addr, r26; 5531 struct target_rt_sigframe *frame; 5532 int i, err = 0; 5533 5534 frame_addr = get_sigframe(ka, env, sizeof(*frame)); 5535 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) { 5536 goto give_sigsegv; 5537 } 5538 5539 copy_siginfo_to_user(&frame->info, info); 5540 5541 __put_user(0, &frame->uc.tuc_flags); 5542 __put_user(0, &frame->uc.tuc_link); 5543 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask); 5544 __put_user(target_sigaltstack_used.ss_sp, 5545 &frame->uc.tuc_stack.ss_sp); 5546 __put_user(sas_ss_flags(env->ir[IR_SP]), 5547 &frame->uc.tuc_stack.ss_flags); 5548 __put_user(target_sigaltstack_used.ss_size, 5549 &frame->uc.tuc_stack.ss_size); 5550 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set); 5551 for (i = 0; i < TARGET_NSIG_WORDS; ++i) { 5552 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]); 5553 } 5554 5555 if (ka->sa_restorer) { 5556 r26 = ka->sa_restorer; 5557 } else { 5558 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]); 5559 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn, 5560 &frame->retcode[1]); 5561 __put_user(INSN_CALLSYS, &frame->retcode[2]); 5562 /* imb(); */ 5563 r26 = frame_addr; 5564 } 5565 5566 if (err) { 5567 give_sigsegv: 5568 if (sig == TARGET_SIGSEGV) { 5569 ka->_sa_handler = TARGET_SIG_DFL; 5570 } 5571 force_sig(TARGET_SIGSEGV); 5572 } 5573 5574 env->ir[IR_RA] = r26; 5575 env->ir[IR_PV] = env->pc = ka->_sa_handler; 5576 env->ir[IR_A0] = sig; 5577 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info); 5578 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc); 5579 env->ir[IR_SP] = frame_addr; 5580 } 5581 5582 long do_sigreturn(CPUAlphaState *env) 5583 { 5584 struct target_sigcontext *sc; 5585 abi_ulong sc_addr = env->ir[IR_A0]; 5586 target_sigset_t target_set; 5587 sigset_t set; 5588 5589 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) { 5590 goto badframe; 5591 } 5592 5593 target_sigemptyset(&target_set); 5594 if (__get_user(target_set.sig[0], &sc->sc_mask)) { 5595 goto badframe; 5596 } 5597 5598 target_to_host_sigset_internal(&set, &target_set); 5599 do_sigprocmask(SIG_SETMASK, &set, NULL); 5600 5601 restore_sigcontext(env, sc); 5602 unlock_user_struct(sc, sc_addr, 0); 5603 return env->ir[IR_V0]; 5604 5605 badframe: 5606 unlock_user_struct(sc, sc_addr, 0); 5607 force_sig(TARGET_SIGSEGV); 5608 } 5609 5610 long do_rt_sigreturn(CPUAlphaState *env) 5611 { 5612 abi_ulong frame_addr = env->ir[IR_A0]; 5613 struct target_rt_sigframe *frame; 5614 sigset_t set; 5615 5616 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) { 5617 goto badframe; 5618 } 5619 target_to_host_sigset(&set, &frame->uc.tuc_sigmask); 5620 do_sigprocmask(SIG_SETMASK, &set, NULL); 5621 5622 restore_sigcontext(env, &frame->uc.tuc_mcontext); 5623 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe, 5624 uc.tuc_stack), 5625 0, env->ir[IR_SP]) == -EFAULT) { 5626 goto badframe; 5627 } 5628 5629 unlock_user_struct(frame, frame_addr, 0); 5630 return env->ir[IR_V0]; 5631 5632 5633 badframe: 5634 unlock_user_struct(frame, frame_addr, 0); 5635 force_sig(TARGET_SIGSEGV); 5636 } 5637 5638 #else 5639 5640 static void setup_frame(int sig, struct target_sigaction *ka, 5641 target_sigset_t *set, CPUArchState *env) 5642 { 5643 fprintf(stderr, "setup_frame: not implemented\n"); 5644 } 5645 5646 static void setup_rt_frame(int sig, struct target_sigaction *ka, 5647 target_siginfo_t *info, 5648 target_sigset_t *set, CPUArchState *env) 5649 { 5650 fprintf(stderr, "setup_rt_frame: not implemented\n"); 5651 } 5652 5653 long do_sigreturn(CPUArchState *env) 5654 { 5655 fprintf(stderr, "do_sigreturn: not implemented\n"); 5656 return -TARGET_ENOSYS; 5657 } 5658 5659 long do_rt_sigreturn(CPUArchState *env) 5660 { 5661 fprintf(stderr, "do_rt_sigreturn: not implemented\n"); 5662 return -TARGET_ENOSYS; 5663 } 5664 5665 #endif 5666 5667 void process_pending_signals(CPUArchState *cpu_env) 5668 { 5669 CPUState *cpu = ENV_GET_CPU(cpu_env); 5670 int sig; 5671 abi_ulong handler; 5672 sigset_t set, old_set; 5673 target_sigset_t target_old_set; 5674 struct emulated_sigtable *k; 5675 struct target_sigaction *sa; 5676 struct sigqueue *q; 5677 TaskState *ts = cpu->opaque; 5678 5679 if (!ts->signal_pending) 5680 return; 5681 5682 /* FIXME: This is not threadsafe. */ 5683 k = ts->sigtab; 5684 for(sig = 1; sig <= TARGET_NSIG; sig++) { 5685 if (k->pending) 5686 goto handle_signal; 5687 k++; 5688 } 5689 /* if no signal is pending, just return */ 5690 ts->signal_pending = 0; 5691 return; 5692 5693 handle_signal: 5694 #ifdef DEBUG_SIGNAL 5695 fprintf(stderr, "qemu: process signal %d\n", sig); 5696 #endif 5697 /* dequeue signal */ 5698 q = k->first; 5699 k->first = q->next; 5700 if (!k->first) 5701 k->pending = 0; 5702 5703 sig = gdb_handlesig(cpu, sig); 5704 if (!sig) { 5705 sa = NULL; 5706 handler = TARGET_SIG_IGN; 5707 } else { 5708 sa = &sigact_table[sig - 1]; 5709 handler = sa->_sa_handler; 5710 } 5711 5712 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) { 5713 /* Guest has blocked SIGSEGV but we got one anyway. Assume this 5714 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info 5715 * because it got a real MMU fault), and treat as if default handler. 5716 */ 5717 handler = TARGET_SIG_DFL; 5718 } 5719 5720 if (handler == TARGET_SIG_DFL) { 5721 /* default handler : ignore some signal. The other are job control or fatal */ 5722 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) { 5723 kill(getpid(),SIGSTOP); 5724 } else if (sig != TARGET_SIGCHLD && 5725 sig != TARGET_SIGURG && 5726 sig != TARGET_SIGWINCH && 5727 sig != TARGET_SIGCONT) { 5728 force_sig(sig); 5729 } 5730 } else if (handler == TARGET_SIG_IGN) { 5731 /* ignore sig */ 5732 } else if (handler == TARGET_SIG_ERR) { 5733 force_sig(sig); 5734 } else { 5735 /* compute the blocked signals during the handler execution */ 5736 target_to_host_sigset(&set, &sa->sa_mask); 5737 /* SA_NODEFER indicates that the current signal should not be 5738 blocked during the handler */ 5739 if (!(sa->sa_flags & TARGET_SA_NODEFER)) 5740 sigaddset(&set, target_to_host_signal(sig)); 5741 5742 /* block signals in the handler using Linux */ 5743 do_sigprocmask(SIG_BLOCK, &set, &old_set); 5744 /* save the previous blocked signal state to restore it at the 5745 end of the signal execution (see do_sigreturn) */ 5746 host_to_target_sigset_internal(&target_old_set, &old_set); 5747 5748 /* if the CPU is in VM86 mode, we restore the 32 bit values */ 5749 #if defined(TARGET_I386) && !defined(TARGET_X86_64) 5750 { 5751 CPUX86State *env = cpu_env; 5752 if (env->eflags & VM_MASK) 5753 save_v86_state(env); 5754 } 5755 #endif 5756 /* prepare the stack frame of the virtual CPU */ 5757 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) 5758 /* These targets do not have traditional signals. */ 5759 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env); 5760 #else 5761 if (sa->sa_flags & TARGET_SA_SIGINFO) 5762 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env); 5763 else 5764 setup_frame(sig, sa, &target_old_set, cpu_env); 5765 #endif 5766 if (sa->sa_flags & TARGET_SA_RESETHAND) 5767 sa->_sa_handler = TARGET_SIG_DFL; 5768 } 5769 if (q != &k->info) 5770 free_sigqueue(cpu_env, q); 5771 } 5772