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