1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 29 /* All Rights Reserved */ 30 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/sysmacros.h> 34 #include <sys/signal.h> 35 #include <sys/systm.h> 36 #include <sys/user.h> 37 #include <sys/mman.h> 38 #include <sys/class.h> 39 #include <sys/proc.h> 40 #include <sys/procfs.h> 41 #include <sys/buf.h> 42 #include <sys/kmem.h> 43 #include <sys/cred.h> 44 #include <sys/archsystm.h> 45 #include <sys/vmparam.h> 46 #include <sys/prsystm.h> 47 #include <sys/reboot.h> 48 #include <sys/uadmin.h> 49 #include <sys/vfs.h> 50 #include <sys/vnode.h> 51 #include <sys/file.h> 52 #include <sys/session.h> 53 #include <sys/ucontext.h> 54 #include <sys/dnlc.h> 55 #include <sys/var.h> 56 #include <sys/cmn_err.h> 57 #include <sys/debugreg.h> 58 #include <sys/thread.h> 59 #include <sys/vtrace.h> 60 #include <sys/consdev.h> 61 #include <sys/psw.h> 62 #include <sys/regset.h> 63 64 #include <sys/privregs.h> 65 66 #include <sys/stack.h> 67 #include <sys/swap.h> 68 #include <vm/hat.h> 69 #include <vm/anon.h> 70 #include <vm/as.h> 71 #include <vm/page.h> 72 #include <vm/seg.h> 73 #include <vm/seg_kmem.h> 74 #include <vm/seg_map.h> 75 #include <vm/seg_vn.h> 76 #include <sys/exec.h> 77 #include <sys/acct.h> 78 #include <sys/core.h> 79 #include <sys/corectl.h> 80 #include <sys/modctl.h> 81 #include <sys/tuneable.h> 82 #include <c2/audit.h> 83 #include <sys/bootconf.h> 84 #include <sys/dumphdr.h> 85 #include <sys/promif.h> 86 #include <sys/systeminfo.h> 87 #include <sys/kdi.h> 88 #include <sys/contract_impl.h> 89 #include <sys/x86_archext.h> 90 91 /* 92 * Construct the execution environment for the user's signal 93 * handler and arrange for control to be given to it on return 94 * to userland. The library code now calls setcontext() to 95 * clean up after the signal handler, so sigret() is no longer 96 * needed. 97 * 98 * (The various 'volatile' declarations are need to ensure that values 99 * are correct on the error return from on_fault().) 100 */ 101 102 103 /* 104 * An amd64 signal frame looks like this on the stack: 105 * 106 * old %rsp: 107 * <128 bytes of untouched stack space> 108 * <a siginfo_t [optional]> 109 * <a ucontext_t> 110 * <siginfo_t *> 111 * <signal number> 112 * new %rsp: <return address (deliberately invalid)> 113 * 114 * The signal number and siginfo_t pointer are only pushed onto the stack in 115 * order to allow stack backtraces. The actual signal handling code expects the 116 * arguments in registers. 117 */ 118 119 struct sigframe { 120 caddr_t retaddr; 121 long signo; 122 siginfo_t *sip; 123 }; 124 125 int 126 sendsig(int sig, k_siginfo_t *sip, void (*hdlr)()) 127 { 128 volatile int minstacksz; 129 int newstack; 130 label_t ljb; 131 volatile caddr_t sp; 132 caddr_t fp; 133 volatile struct regs *rp; 134 volatile greg_t upc; 135 volatile proc_t *p = ttoproc(curthread); 136 struct as *as = p->p_as; 137 klwp_t *lwp = ttolwp(curthread); 138 ucontext_t *volatile tuc = NULL; 139 ucontext_t *uc; 140 siginfo_t *sip_addr; 141 volatile int watched; 142 143 /* 144 * This routine is utterly dependent upon STACK_ALIGN being 145 * 16 and STACK_ENTRY_ALIGN being 8. Let's just acknowledge 146 * that and require it. 147 */ 148 149 #if STACK_ALIGN != 16 || STACK_ENTRY_ALIGN != 8 150 #error "sendsig() amd64 did not find the expected stack alignments" 151 #endif 152 153 rp = lwptoregs(lwp); 154 upc = rp->r_pc; 155 156 /* 157 * Since we're setting up to run the signal handler we have to 158 * arrange that the stack at entry to the handler is (only) 159 * STACK_ENTRY_ALIGN (i.e. 8) byte aligned so that when the handler 160 * executes its push of %rbp, the stack realigns to STACK_ALIGN 161 * (i.e. 16) correctly. 162 * 163 * The new sp will point to the sigframe and the ucontext_t. The 164 * above means that sp (and thus sigframe) will be 8-byte aligned, 165 * but not 16-byte aligned. ucontext_t, however, contains %xmm regs 166 * which must be 16-byte aligned. Because of this, for correct 167 * alignment, sigframe must be a multiple of 8-bytes in length, but 168 * not 16-bytes. This will place ucontext_t at a nice 16-byte boundary. 169 */ 170 171 /* LINTED: logical expression always true: op "||" */ 172 ASSERT((sizeof (struct sigframe) % 16) == 8); 173 174 minstacksz = sizeof (struct sigframe) + SA(sizeof (*uc)); 175 if (sip != NULL) 176 minstacksz += SA(sizeof (siginfo_t)); 177 ASSERT((minstacksz & (STACK_ENTRY_ALIGN - 1ul)) == 0); 178 179 /* 180 * Figure out whether we will be handling this signal on 181 * an alternate stack specified by the user. Then allocate 182 * and validate the stack requirements for the signal handler 183 * context. on_fault will catch any faults. 184 */ 185 newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) && 186 !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE)); 187 188 if (newstack) { 189 fp = (caddr_t)(SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) + 190 SA(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN); 191 } else { 192 /* 193 * Drop below the 128-byte reserved region of the stack frame 194 * we're interrupting. 195 */ 196 fp = (caddr_t)rp->r_sp - STACK_RESERVE; 197 } 198 199 /* 200 * Force proper stack pointer alignment, even in the face of a 201 * misaligned stack pointer from user-level before the signal. 202 */ 203 fp = (caddr_t)((uintptr_t)fp & ~(STACK_ENTRY_ALIGN - 1ul)); 204 205 /* 206 * Most of the time during normal execution, the stack pointer 207 * is aligned on a STACK_ALIGN (i.e. 16 byte) boundary. However, 208 * (for example) just after a call instruction (which pushes 209 * the return address), the callers stack misaligns until the 210 * 'push %rbp' happens in the callee prolog. So while we should 211 * expect the stack pointer to be always at least STACK_ENTRY_ALIGN 212 * aligned, we should -not- expect it to always be STACK_ALIGN aligned. 213 * We now adjust to ensure that the new sp is aligned to 214 * STACK_ENTRY_ALIGN but not to STACK_ALIGN. 215 */ 216 sp = fp - minstacksz; 217 if (((uintptr_t)sp & (STACK_ALIGN - 1ul)) == 0) { 218 sp -= STACK_ENTRY_ALIGN; 219 minstacksz = fp - sp; 220 } 221 222 /* 223 * Now, make sure the resulting signal frame address is sane 224 */ 225 if (sp >= as->a_userlimit || fp >= as->a_userlimit) { 226 #ifdef DEBUG 227 printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n", 228 PTOU(p)->u_comm, p->p_pid, sig); 229 printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n", 230 (void *)sp, (void *)hdlr, (uintptr_t)upc); 231 printf("sp above USERLIMIT\n"); 232 #endif 233 return (0); 234 } 235 236 watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE); 237 238 if (on_fault(&ljb)) 239 goto badstack; 240 241 if (sip != NULL) { 242 zoneid_t zoneid; 243 244 fp -= SA(sizeof (siginfo_t)); 245 uzero(fp, sizeof (siginfo_t)); 246 if (SI_FROMUSER(sip) && 247 (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID && 248 zoneid != sip->si_zoneid) { 249 k_siginfo_t sani_sip = *sip; 250 251 sani_sip.si_pid = p->p_zone->zone_zsched->p_pid; 252 sani_sip.si_uid = 0; 253 sani_sip.si_ctid = -1; 254 sani_sip.si_zoneid = zoneid; 255 copyout_noerr(&sani_sip, fp, sizeof (sani_sip)); 256 } else 257 copyout_noerr(sip, fp, sizeof (*sip)); 258 sip_addr = (siginfo_t *)fp; 259 260 if (sig == SIGPROF && 261 curthread->t_rprof != NULL && 262 curthread->t_rprof->rp_anystate) { 263 /* 264 * We stand on our head to deal with 265 * the real time profiling signal. 266 * Fill in the stuff that doesn't fit 267 * in a normal k_siginfo structure. 268 */ 269 int i = sip->si_nsysarg; 270 271 while (--i >= 0) 272 sulword_noerr( 273 (ulong_t *)&(sip_addr->si_sysarg[i]), 274 (ulong_t)lwp->lwp_arg[i]); 275 copyout_noerr(curthread->t_rprof->rp_state, 276 sip_addr->si_mstate, 277 sizeof (curthread->t_rprof->rp_state)); 278 } 279 } else 280 sip_addr = NULL; 281 282 /* 283 * save the current context on the user stack directly after the 284 * sigframe. Since sigframe is 8-byte-but-not-16-byte aligned, 285 * and since sizeof (struct sigframe) is 24, this guarantees 286 * 16-byte alignment for ucontext_t and its %xmm registers. 287 */ 288 uc = (ucontext_t *)(sp + sizeof (struct sigframe)); 289 tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP); 290 no_fault(); 291 savecontext(tuc, &lwp->lwp_sigoldmask); 292 if (on_fault(&ljb)) 293 goto badstack; 294 copyout_noerr(tuc, uc, sizeof (*tuc)); 295 kmem_free(tuc, sizeof (*tuc)); 296 tuc = NULL; 297 298 lwp->lwp_oldcontext = (uintptr_t)uc; 299 300 if (newstack) { 301 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK; 302 if (lwp->lwp_ustack) 303 copyout_noerr(&lwp->lwp_sigaltstack, 304 (stack_t *)lwp->lwp_ustack, sizeof (stack_t)); 305 } 306 307 /* 308 * Set up signal handler return and stack linkage 309 */ 310 { 311 struct sigframe frame; 312 313 /* 314 * ensure we never return "normally" 315 */ 316 frame.retaddr = (caddr_t)(uintptr_t)-1L; 317 frame.signo = sig; 318 frame.sip = sip_addr; 319 copyout_noerr(&frame, sp, sizeof (frame)); 320 } 321 322 no_fault(); 323 if (watched) 324 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE); 325 326 /* 327 * Set up user registers for execution of signal handler. 328 */ 329 rp->r_sp = (greg_t)sp; 330 rp->r_pc = (greg_t)hdlr; 331 rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL); 332 333 rp->r_rdi = sig; 334 rp->r_rsi = (uintptr_t)sip_addr; 335 rp->r_rdx = (uintptr_t)uc; 336 337 if ((rp->r_cs & 0xffff) != UCS_SEL || 338 (rp->r_ss & 0xffff) != UDS_SEL) { 339 /* 340 * Try our best to deliver the signal. 341 */ 342 rp->r_cs = UCS_SEL; 343 rp->r_ss = UDS_SEL; 344 } 345 346 /* 347 * Don't set lwp_eosys here. sendsig() is called via psig() after 348 * lwp_eosys is handled, so setting it here would affect the next 349 * system call. 350 */ 351 return (1); 352 353 badstack: 354 no_fault(); 355 if (watched) 356 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE); 357 if (tuc) 358 kmem_free(tuc, sizeof (*tuc)); 359 #ifdef DEBUG 360 printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n", 361 PTOU(p)->u_comm, p->p_pid, sig); 362 printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n", 363 (void *)sp, (void *)hdlr, (uintptr_t)upc); 364 #endif 365 return (0); 366 } 367 368 #ifdef _SYSCALL32_IMPL 369 370 /* 371 * An i386 SVR4/ABI signal frame looks like this on the stack: 372 * 373 * old %esp: 374 * <a siginfo32_t [optional]> 375 * <a ucontext32_t> 376 * <pointer to that ucontext32_t> 377 * <pointer to that siginfo32_t> 378 * <signo> 379 * new %esp: <return address (deliberately invalid)> 380 */ 381 struct sigframe32 { 382 caddr32_t retaddr; 383 uint32_t signo; 384 caddr32_t sip; 385 caddr32_t ucp; 386 }; 387 388 int 389 sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)()) 390 { 391 volatile int minstacksz; 392 int newstack; 393 label_t ljb; 394 volatile caddr_t sp; 395 caddr_t fp; 396 volatile struct regs *rp; 397 volatile greg_t upc; 398 volatile proc_t *p = ttoproc(curthread); 399 klwp_t *lwp = ttolwp(curthread); 400 ucontext32_t *volatile tuc = NULL; 401 ucontext32_t *uc; 402 siginfo32_t *sip_addr; 403 volatile int watched; 404 405 rp = lwptoregs(lwp); 406 upc = rp->r_pc; 407 408 minstacksz = SA32(sizeof (struct sigframe32)) + SA32(sizeof (*uc)); 409 if (sip != NULL) 410 minstacksz += SA32(sizeof (siginfo32_t)); 411 ASSERT((minstacksz & (STACK_ALIGN32 - 1)) == 0); 412 413 /* 414 * Figure out whether we will be handling this signal on 415 * an alternate stack specified by the user. Then allocate 416 * and validate the stack requirements for the signal handler 417 * context. on_fault will catch any faults. 418 */ 419 newstack = sigismember(&PTOU(curproc)->u_sigonstack, sig) && 420 !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE)); 421 422 if (newstack) { 423 fp = (caddr_t)(SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) + 424 SA32(lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN32); 425 } else if ((rp->r_ss & 0xffff) != UDS_SEL) { 426 user_desc_t *ldt; 427 /* 428 * If the stack segment selector is -not- pointing at 429 * the UDS_SEL descriptor and we have an LDT entry for 430 * it instead, add the base address to find the effective va. 431 */ 432 if ((ldt = p->p_ldt) != NULL) 433 fp = (caddr_t)rp->r_sp + 434 USEGD_GETBASE(&ldt[SELTOIDX(rp->r_ss)]); 435 else 436 fp = (caddr_t)rp->r_sp; 437 } else 438 fp = (caddr_t)rp->r_sp; 439 440 /* 441 * Force proper stack pointer alignment, even in the face of a 442 * misaligned stack pointer from user-level before the signal. 443 * Don't use the SA32() macro because that rounds up, not down. 444 */ 445 fp = (caddr_t)((uintptr_t)fp & ~(STACK_ALIGN32 - 1)); 446 sp = fp - minstacksz; 447 448 /* 449 * Make sure lwp hasn't trashed its stack 450 */ 451 if (sp >= (caddr_t)(uintptr_t)USERLIMIT32 || 452 fp >= (caddr_t)(uintptr_t)USERLIMIT32) { 453 #ifdef DEBUG 454 printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n", 455 PTOU(p)->u_comm, p->p_pid, sig); 456 printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n", 457 (void *)sp, (void *)hdlr, (uintptr_t)upc); 458 printf("sp above USERLIMIT\n"); 459 #endif 460 return (0); 461 } 462 463 watched = watch_disable_addr((caddr_t)sp, minstacksz, S_WRITE); 464 465 if (on_fault(&ljb)) 466 goto badstack; 467 468 if (sip != NULL) { 469 siginfo32_t si32; 470 zoneid_t zoneid; 471 472 siginfo_kto32(sip, &si32); 473 if (SI_FROMUSER(sip) && 474 (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID && 475 zoneid != sip->si_zoneid) { 476 si32.si_pid = p->p_zone->zone_zsched->p_pid; 477 si32.si_uid = 0; 478 si32.si_ctid = -1; 479 si32.si_zoneid = zoneid; 480 } 481 fp -= SA32(sizeof (si32)); 482 uzero(fp, sizeof (si32)); 483 copyout_noerr(&si32, fp, sizeof (si32)); 484 sip_addr = (siginfo32_t *)fp; 485 486 if (sig == SIGPROF && 487 curthread->t_rprof != NULL && 488 curthread->t_rprof->rp_anystate) { 489 /* 490 * We stand on our head to deal with 491 * the real-time profiling signal. 492 * Fill in the stuff that doesn't fit 493 * in a normal k_siginfo structure. 494 */ 495 int i = sip->si_nsysarg; 496 497 while (--i >= 0) 498 suword32_noerr(&(sip_addr->si_sysarg[i]), 499 (uint32_t)lwp->lwp_arg[i]); 500 copyout_noerr(curthread->t_rprof->rp_state, 501 sip_addr->si_mstate, 502 sizeof (curthread->t_rprof->rp_state)); 503 } 504 } else 505 sip_addr = NULL; 506 507 /* save the current context on the user stack */ 508 fp -= SA32(sizeof (*tuc)); 509 uc = (ucontext32_t *)fp; 510 tuc = kmem_alloc(sizeof (*tuc), KM_SLEEP); 511 no_fault(); 512 savecontext32(tuc, &lwp->lwp_sigoldmask); 513 if (on_fault(&ljb)) 514 goto badstack; 515 copyout_noerr(tuc, uc, sizeof (*tuc)); 516 kmem_free(tuc, sizeof (*tuc)); 517 tuc = NULL; 518 519 lwp->lwp_oldcontext = (uintptr_t)uc; 520 521 if (newstack) { 522 lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK; 523 if (lwp->lwp_ustack) { 524 stack32_t stk32; 525 526 stk32.ss_sp = (caddr32_t)(uintptr_t) 527 lwp->lwp_sigaltstack.ss_sp; 528 stk32.ss_size = (size32_t) 529 lwp->lwp_sigaltstack.ss_size; 530 stk32.ss_flags = (int32_t) 531 lwp->lwp_sigaltstack.ss_flags; 532 copyout_noerr(&stk32, 533 (stack32_t *)lwp->lwp_ustack, sizeof (stk32)); 534 } 535 } 536 537 /* 538 * Set up signal handler arguments 539 */ 540 { 541 struct sigframe32 frame32; 542 543 frame32.sip = (caddr32_t)(uintptr_t)sip_addr; 544 frame32.ucp = (caddr32_t)(uintptr_t)uc; 545 frame32.signo = sig; 546 frame32.retaddr = 0xffffffff; /* never return! */ 547 copyout_noerr(&frame32, sp, sizeof (frame32)); 548 } 549 550 no_fault(); 551 if (watched) 552 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE); 553 554 rp->r_sp = (greg_t)(uintptr_t)sp; 555 rp->r_pc = (greg_t)(uintptr_t)hdlr; 556 rp->r_ps = PSL_USER | (rp->r_ps & PS_IOPL); 557 558 if ((rp->r_cs & 0xffff) != U32CS_SEL || 559 (rp->r_ss & 0xffff) != UDS_SEL) { 560 /* 561 * Try our best to deliver the signal. 562 */ 563 rp->r_cs = U32CS_SEL; 564 rp->r_ss = UDS_SEL; 565 } 566 567 /* 568 * Don't set lwp_eosys here. sendsig() is called via psig() after 569 * lwp_eosys is handled, so setting it here would affect the next 570 * system call. 571 */ 572 return (1); 573 574 badstack: 575 no_fault(); 576 if (watched) 577 watch_enable_addr((caddr_t)sp, minstacksz, S_WRITE); 578 if (tuc) 579 kmem_free(tuc, sizeof (*tuc)); 580 #ifdef DEBUG 581 printf("sendsig32: bad signal stack cmd=%s pid=%d, sig=%d\n", 582 PTOU(p)->u_comm, p->p_pid, sig); 583 printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n", 584 (void *)sp, (void *)hdlr, (uintptr_t)upc); 585 #endif 586 return (0); 587 } 588 589 #endif /* _SYSCALL32_IMPL */ 590