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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/param.h> 30 #include <sys/vmparam.h> 31 #include <sys/types.h> 32 #include <sys/sysmacros.h> 33 #include <sys/systm.h> 34 #include <sys/cmn_err.h> 35 #include <sys/signal.h> 36 #include <sys/stack.h> 37 #include <sys/cred.h> 38 #include <sys/user.h> 39 #include <sys/debug.h> 40 #include <sys/errno.h> 41 #include <sys/proc.h> 42 #include <sys/var.h> 43 #include <sys/inline.h> 44 #include <sys/syscall.h> 45 #include <sys/ucontext.h> 46 #include <sys/cpuvar.h> 47 #include <sys/siginfo.h> 48 #include <sys/trap.h> 49 #include <sys/machtrap.h> 50 #include <sys/sysinfo.h> 51 #include <sys/procfs.h> 52 #include <sys/prsystm.h> 53 #include <sys/fpu/fpusystm.h> 54 #include <sys/modctl.h> 55 #include <sys/aio_impl.h> 56 #include <c2/audit.h> 57 #include <sys/tnf.h> 58 #include <sys/tnf_probe.h> 59 #include <sys/machpcb.h> 60 #include <sys/privregs.h> 61 #include <sys/copyops.h> 62 #include <sys/timer.h> 63 #include <sys/priv.h> 64 #include <sys/msacct.h> 65 66 int syscalltrace = 0; 67 #ifdef SYSCALLTRACE 68 static kmutex_t systrace_lock; /* syscall tracing lock */ 69 #endif /* SYSCALLTRACE */ 70 71 static krwlock_t *lock_syscall(struct sysent *, uint_t); 72 73 #ifdef _SYSCALL32_IMPL 74 static struct sysent * 75 lwp_getsysent(klwp_t *lwp) 76 { 77 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) 78 return (sysent); 79 return (sysent32); 80 } 81 #define LWP_GETSYSENT(lwp) (lwp_getsysent(lwp)) 82 #else 83 #define LWP_GETSYSENT(lwp) (sysent) 84 #endif 85 86 /* 87 * Arrange for the real time profiling signal to be dispatched. 88 */ 89 void 90 realsigprof(int sysnum, int error) 91 { 92 proc_t *p; 93 klwp_t *lwp; 94 95 if (curthread->t_rprof->rp_anystate == 0) 96 return; 97 p = ttoproc(curthread); 98 lwp = ttolwp(curthread); 99 mutex_enter(&p->p_lock); 100 if (sigismember(&p->p_ignore, SIGPROF) || 101 signal_is_blocked(curthread, SIGPROF)) { 102 mutex_exit(&p->p_lock); 103 return; 104 } 105 lwp->lwp_siginfo.si_signo = SIGPROF; 106 lwp->lwp_siginfo.si_code = PROF_SIG; 107 lwp->lwp_siginfo.si_errno = error; 108 hrt2ts(gethrtime(), &lwp->lwp_siginfo.si_tstamp); 109 lwp->lwp_siginfo.si_syscall = sysnum; 110 lwp->lwp_siginfo.si_nsysarg = (sysnum > 0 && sysnum < NSYSCALL) ? 111 LWP_GETSYSENT(lwp)[sysnum].sy_narg : 0; 112 lwp->lwp_siginfo.si_fault = lwp->lwp_lastfault; 113 lwp->lwp_siginfo.si_faddr = lwp->lwp_lastfaddr; 114 lwp->lwp_lastfault = 0; 115 lwp->lwp_lastfaddr = NULL; 116 sigtoproc(p, curthread, SIGPROF); 117 mutex_exit(&p->p_lock); 118 ASSERT(lwp->lwp_cursig == 0); 119 if (issig(FORREAL)) { 120 psig(); 121 } 122 mutex_enter(&p->p_lock); 123 lwp->lwp_siginfo.si_signo = 0; 124 bzero(curthread->t_rprof, sizeof (*curthread->t_rprof)); 125 mutex_exit(&p->p_lock); 126 } 127 128 /* 129 * Called to restore the lwp's register window just before 130 * returning to user level (only if the registers have been 131 * fetched or modified through /proc). 132 */ 133 /*ARGSUSED1*/ 134 void 135 xregrestore(klwp_t *lwp, int shared) 136 { 137 /* 138 * If locals+ins were modified by /proc copy them out. 139 * Also copy to the shared window, if necessary. 140 */ 141 if (lwp->lwp_pcb.pcb_xregstat == XREGMODIFIED) { 142 struct machpcb *mpcb = lwptompcb(lwp); 143 caddr_t sp = (caddr_t)lwptoregs(lwp)->r_sp; 144 145 size_t rwinsize; 146 caddr_t rwp; 147 int is64; 148 149 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) { 150 rwinsize = sizeof (struct rwindow); 151 rwp = sp + STACK_BIAS; 152 is64 = 1; 153 } else { 154 rwinsize = sizeof (struct rwindow32); 155 sp = (caddr_t)(caddr32_t)sp; 156 rwp = sp; 157 is64 = 0; 158 } 159 160 if (is64) 161 (void) copyout_nowatch(&lwp->lwp_pcb.pcb_xregs, 162 rwp, rwinsize); 163 else { 164 struct rwindow32 rwindow32; 165 int watched; 166 167 watched = watch_disable_addr(rwp, rwinsize, S_WRITE); 168 rwindow_nto32(&lwp->lwp_pcb.pcb_xregs, &rwindow32); 169 (void) copyout(&rwindow32, rwp, rwinsize); 170 if (watched) 171 watch_enable_addr(rwp, rwinsize, S_WRITE); 172 } 173 174 /* also copy to the user return window */ 175 mpcb->mpcb_rsp[0] = sp; 176 mpcb->mpcb_rsp[1] = NULL; 177 bcopy(&lwp->lwp_pcb.pcb_xregs, &mpcb->mpcb_rwin[0], 178 sizeof (lwp->lwp_pcb.pcb_xregs)); 179 } 180 lwp->lwp_pcb.pcb_xregstat = XREGNONE; 181 } 182 183 184 /* 185 * Get the arguments to the current system call. 186 * lwp->lwp_ap normally points to the out regs in the reg structure. 187 * If the user is going to change the out registers and might want to 188 * get the args (for /proc tracing), it must copy the args elsewhere 189 * via save_syscall_args(). 190 */ 191 uint_t 192 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp) 193 { 194 kthread_t *t = lwptot(lwp); 195 uint_t code = t->t_sysnum; 196 long mask; 197 long *ap; 198 int nargs; 199 200 if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32) 201 mask = (uint32_t)0xffffffffU; 202 else 203 mask = 0xffffffffffffffff; 204 205 if (code != 0 && code < NSYSCALL) { 206 207 nargs = LWP_GETSYSENT(lwp)[code].sy_narg; 208 209 ASSERT(nargs <= MAXSYSARGS); 210 211 *nargsp = nargs; 212 ap = lwp->lwp_ap; 213 while (nargs-- > 0) 214 *argp++ = *ap++ & mask; 215 } else { 216 *nargsp = 0; 217 } 218 return (code); 219 } 220 221 #ifdef _SYSCALL32_IMPL 222 /* 223 * Get the arguments to the current 32-bit system call. 224 */ 225 uint_t 226 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp) 227 { 228 long args[MAXSYSARGS]; 229 uint_t i, code; 230 231 code = get_syscall_args(lwp, args, nargsp); 232 for (i = 0; i != *nargsp; i++) 233 *argp++ = (int)args[i]; 234 return (code); 235 } 236 #endif 237 238 /* 239 * Save the system call arguments in a safe place. 240 * lwp->lwp_ap normally points to the out regs in the reg structure. 241 * If the user is going to change the out registers, g1, or the stack, 242 * and might want to get the args (for /proc tracing), it must copy 243 * the args elsewhere via save_syscall_args(). 244 * 245 * This may be called from stop() even when we're not in a system call. 246 * Since there's no easy way to tell, this must be safe (not panic). 247 * If the copyins get data faults, return non-zero. 248 */ 249 int 250 save_syscall_args() 251 { 252 kthread_t *t = curthread; 253 klwp_t *lwp = ttolwp(t); 254 struct regs *rp = lwptoregs(lwp); 255 uint_t code = t->t_sysnum; 256 uint_t nargs; 257 int i; 258 caddr_t ua; 259 model_t datamodel; 260 261 if (lwp->lwp_argsaved || code == 0) 262 return (0); /* args already saved or not needed */ 263 264 if (code >= NSYSCALL) { 265 nargs = 0; /* illegal syscall */ 266 } else { 267 struct sysent *se = LWP_GETSYSENT(lwp); 268 struct sysent *callp = se + code; 269 270 nargs = callp->sy_narg; 271 if (LOADABLE_SYSCALL(callp) && nargs == 0) { 272 krwlock_t *module_lock; 273 274 /* 275 * Find out how many arguments the system 276 * call uses. 277 * 278 * We have the property that loaded syscalls 279 * never change the number of arguments they 280 * use after they've been loaded once. This 281 * allows us to stop for /proc tracing without 282 * holding the module lock. 283 * /proc is assured that sy_narg is valid. 284 */ 285 module_lock = lock_syscall(se, code); 286 nargs = callp->sy_narg; 287 rw_exit(module_lock); 288 } 289 } 290 291 /* 292 * Fetch the system call arguments. 293 */ 294 if (nargs == 0) 295 goto out; 296 297 298 ASSERT(nargs <= MAXSYSARGS); 299 300 if ((datamodel = lwp_getdatamodel(lwp)) == DATAMODEL_ILP32) { 301 302 if (rp->r_g1 == 0) { /* indirect syscall */ 303 304 lwp->lwp_arg[0] = (uint32_t)rp->r_o1; 305 lwp->lwp_arg[1] = (uint32_t)rp->r_o2; 306 lwp->lwp_arg[2] = (uint32_t)rp->r_o3; 307 lwp->lwp_arg[3] = (uint32_t)rp->r_o4; 308 lwp->lwp_arg[4] = (uint32_t)rp->r_o5; 309 if (nargs > 5) { 310 ua = (caddr_t)(caddr32_t)(rp->r_sp + 311 MINFRAME32); 312 for (i = 5; i < nargs; i++) { 313 uint32_t a; 314 if (fuword32(ua, &a) != 0) 315 return (-1); 316 lwp->lwp_arg[i] = a; 317 ua += sizeof (a); 318 } 319 } 320 } else { 321 lwp->lwp_arg[0] = (uint32_t)rp->r_o0; 322 lwp->lwp_arg[1] = (uint32_t)rp->r_o1; 323 lwp->lwp_arg[2] = (uint32_t)rp->r_o2; 324 lwp->lwp_arg[3] = (uint32_t)rp->r_o3; 325 lwp->lwp_arg[4] = (uint32_t)rp->r_o4; 326 lwp->lwp_arg[5] = (uint32_t)rp->r_o5; 327 if (nargs > 6) { 328 ua = (caddr_t)(caddr32_t)(rp->r_sp + 329 MINFRAME32); 330 for (i = 6; i < nargs; i++) { 331 uint32_t a; 332 if (fuword32(ua, &a) != 0) 333 return (-1); 334 lwp->lwp_arg[i] = a; 335 ua += sizeof (a); 336 } 337 } 338 } 339 } else { 340 ASSERT(datamodel == DATAMODEL_LP64); 341 lwp->lwp_arg[0] = rp->r_o0; 342 lwp->lwp_arg[1] = rp->r_o1; 343 lwp->lwp_arg[2] = rp->r_o2; 344 lwp->lwp_arg[3] = rp->r_o3; 345 lwp->lwp_arg[4] = rp->r_o4; 346 lwp->lwp_arg[5] = rp->r_o5; 347 if (nargs > 6) { 348 ua = (caddr_t)rp->r_sp + MINFRAME + STACK_BIAS; 349 for (i = 6; i < nargs; i++) { 350 unsigned long a; 351 if (fulword(ua, &a) != 0) 352 return (-1); 353 lwp->lwp_arg[i] = a; 354 ua += sizeof (a); 355 } 356 } 357 } 358 359 out: 360 lwp->lwp_ap = lwp->lwp_arg; 361 lwp->lwp_argsaved = 1; 362 t->t_post_sys = 1; /* so lwp_ap will be reset */ 363 return (0); 364 } 365 366 void 367 reset_syscall_args(void) 368 { 369 klwp_t *lwp = ttolwp(curthread); 370 371 lwp->lwp_ap = (long *)&lwptoregs(lwp)->r_o0; 372 lwp->lwp_argsaved = 0; 373 } 374 375 /* 376 * nonexistent system call-- signal lwp (may want to handle it) 377 * flag error if lwp won't see signal immediately 378 * This works for old or new calling sequence. 379 */ 380 int64_t 381 nosys() 382 { 383 tsignal(curthread, SIGSYS); 384 return ((int64_t)set_errno(ENOSYS)); 385 } 386 387 /* 388 * Perform pre-system-call processing, including stopping for tracing, 389 * auditing, microstate-accounting, etc. 390 * 391 * This routine is called only if the t_pre_sys flag is set. Any condition 392 * requiring pre-syscall handling must set the t_pre_sys flag. If the 393 * condition is persistent, this routine will repost t_pre_sys. 394 */ 395 int 396 pre_syscall(int arg0) 397 { 398 unsigned int code; 399 kthread_t *t = curthread; 400 proc_t *p = ttoproc(t); 401 klwp_t *lwp = ttolwp(t); 402 struct regs *rp = lwptoregs(lwp); 403 int repost; 404 405 t->t_pre_sys = repost = 0; /* clear pre-syscall processing flag */ 406 407 ASSERT(t->t_schedflag & TS_DONT_SWAP); 408 409 syscall_mstate(LMS_USER, LMS_SYSTEM); 410 411 /* 412 * The syscall arguments in the out registers should be pointed to 413 * by lwp_ap. If the args need to be copied so that the outs can 414 * be changed without losing the ability to get the args for /proc, 415 * they can be saved by save_syscall_args(), and lwp_ap will be 416 * restored by post_syscall(). 417 */ 418 ASSERT(lwp->lwp_ap == (long *)&rp->r_o0); 419 420 /* 421 * Make sure the thread is holding the latest credentials for the 422 * process. The credentials in the process right now apply to this 423 * thread for the entire system call. 424 */ 425 if (t->t_cred != p->p_cred) { 426 cred_t *oldcred = t->t_cred; 427 /* 428 * DTrace accesses t_cred in probe context. t_cred must 429 * always be either NULL, or point to a valid, allocated cred 430 * structure. 431 */ 432 t->t_cred = crgetcred(); 433 crfree(oldcred); 434 } 435 436 /* 437 * Undo special arrangements to single-step the lwp 438 * so that a debugger will see valid register contents. 439 * Also so that the pc is valid for syncfpu(). 440 * Also so that a syscall like exec() can be stepped. 441 */ 442 if (lwp->lwp_pcb.pcb_step != STEP_NONE) { 443 (void) prundostep(); 444 repost = 1; 445 } 446 447 /* 448 * Check for indirect system call in case we stop for tracing. 449 * Don't allow multiple indirection. 450 */ 451 code = t->t_sysnum; 452 if (code == 0 && arg0 != 0) { /* indirect syscall */ 453 code = arg0; 454 t->t_sysnum = arg0; 455 } 456 457 /* 458 * From the proc(4) manual page: 459 * When entry to a system call is being traced, the traced process 460 * stops after having begun the call to the system but before the 461 * system call arguments have been fetched from the process. 462 * If proc changes the args we must refetch them after starting. 463 */ 464 if (PTOU(p)->u_systrap) { 465 if (prismember(&PTOU(p)->u_entrymask, code)) { 466 /* 467 * Recheck stop condition, now that lock is held. 468 */ 469 mutex_enter(&p->p_lock); 470 if (PTOU(p)->u_systrap && 471 prismember(&PTOU(p)->u_entrymask, code)) { 472 stop(PR_SYSENTRY, code); 473 /* 474 * Must refetch args since they were 475 * possibly modified by /proc. Indicate 476 * that the valid copy is in the 477 * registers. 478 */ 479 lwp->lwp_argsaved = 0; 480 lwp->lwp_ap = (long *)&rp->r_o0; 481 } 482 mutex_exit(&p->p_lock); 483 } 484 repost = 1; 485 } 486 487 if (lwp->lwp_sysabort) { 488 /* 489 * lwp_sysabort may have been set via /proc while the process 490 * was stopped on PR_SYSENTRY. If so, abort the system call. 491 * Override any error from the copyin() of the arguments. 492 */ 493 lwp->lwp_sysabort = 0; 494 (void) set_errno(EINTR); /* sets post-sys processing */ 495 t->t_pre_sys = 1; /* repost anyway */ 496 return (1); /* don't do system call, return EINTR */ 497 } 498 499 #ifdef C2_AUDIT 500 if (audit_active) { /* begin auditing for this syscall */ 501 int error; 502 if (error = audit_start(T_SYSCALL, code, 0, lwp)) { 503 t->t_pre_sys = 1; /* repost anyway */ 504 lwp->lwp_error = 0; /* for old drivers */ 505 return (error); 506 } 507 repost = 1; 508 } 509 #endif /* C2_AUDIT */ 510 511 #ifndef NPROBE 512 /* Kernel probe */ 513 if (tnf_tracing_active) { 514 TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */, 515 tnf_sysnum, sysnum, t->t_sysnum); 516 t->t_post_sys = 1; /* make sure post_syscall runs */ 517 repost = 1; 518 } 519 #endif /* NPROBE */ 520 521 #ifdef SYSCALLTRACE 522 if (syscalltrace) { 523 int i; 524 long *ap; 525 char *cp; 526 char *sysname; 527 struct sysent *callp; 528 529 if (code >= NSYSCALL) 530 callp = &nosys_ent; /* nosys has no args */ 531 else 532 callp = LWP_GETSYSENT(lwp) + code; 533 (void) save_syscall_args(); 534 mutex_enter(&systrace_lock); 535 printf("%d: ", p->p_pid); 536 if (code >= NSYSCALL) 537 printf("0x%x", code); 538 else { 539 sysname = mod_getsysname(code); 540 printf("%s[0x%x]", sysname == NULL ? "NULL" : 541 sysname, code); 542 } 543 cp = "("; 544 for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) { 545 printf("%s%lx", cp, *ap); 546 cp = ", "; 547 } 548 if (i) 549 printf(")"); 550 printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread); 551 mutex_exit(&systrace_lock); 552 } 553 #endif /* SYSCALLTRACE */ 554 555 /* 556 * If there was a continuing reason for pre-syscall processing, 557 * set the t_pre_sys flag for the next system call. 558 */ 559 if (repost) 560 t->t_pre_sys = 1; 561 lwp->lwp_error = 0; /* for old drivers */ 562 lwp->lwp_badpriv = PRIV_NONE; /* for privilege tracing */ 563 return (0); 564 } 565 566 /* 567 * Post-syscall processing. Perform abnormal system call completion 568 * actions such as /proc tracing, profiling, signals, preemption, etc. 569 * 570 * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set. 571 * Any condition requiring pre-syscall handling must set one of these. 572 * If the condition is persistent, this routine will repost t_post_sys. 573 */ 574 void 575 post_syscall(long rval1, long rval2) 576 { 577 kthread_t *t = curthread; 578 proc_t *p = curproc; 579 klwp_t *lwp = ttolwp(t); 580 struct regs *rp = lwptoregs(lwp); 581 uint_t error; 582 int code = t->t_sysnum; 583 int repost = 0; 584 int proc_stop = 0; /* non-zero if stopping for /proc */ 585 int sigprof = 0; /* non-zero if sending SIGPROF */ 586 587 t->t_post_sys = 0; 588 589 error = lwp->lwp_errno; 590 591 /* 592 * Code can be zero if this is a new LWP returning after a forkall(), 593 * other than the one which matches the one in the parent which called 594 * forkall(). In these LWPs, skip most of post-syscall activity. 595 */ 596 if (code == 0) 597 goto sig_check; 598 599 #ifdef C2_AUDIT 600 if (audit_active) { /* put out audit record for this syscall */ 601 rval_t rval; /* fix audit_finish() someday */ 602 603 /* XX64 -- truncation of 64-bit return values? */ 604 rval.r_val1 = (int)rval1; 605 rval.r_val2 = (int)rval2; 606 audit_finish(T_SYSCALL, code, error, &rval); 607 repost = 1; 608 } 609 #endif /* C2_AUDIT */ 610 611 if (curthread->t_pdmsg != NULL) { 612 char *m = curthread->t_pdmsg; 613 614 uprintf("%s", m); 615 kmem_free(m, strlen(m) + 1); 616 curthread->t_pdmsg = NULL; 617 } 618 619 /* 620 * If we're going to stop for /proc tracing, set the flag and 621 * save the arguments so that the return values don't smash them. 622 */ 623 if (PTOU(p)->u_systrap) { 624 if (prismember(&PTOU(p)->u_exitmask, code)) { 625 proc_stop = 1; 626 (void) save_syscall_args(); 627 } 628 repost = 1; 629 } 630 631 /* 632 * Similarly check to see if SIGPROF might be sent. 633 */ 634 if (curthread->t_rprof != NULL && 635 curthread->t_rprof->rp_anystate != 0) { 636 (void) save_syscall_args(); 637 sigprof = 1; 638 } 639 640 if (lwp->lwp_eosys == NORMALRETURN) { 641 if (error == 0) { 642 #ifdef SYSCALLTRACE 643 if (syscalltrace) { 644 mutex_enter(&systrace_lock); 645 printf( 646 "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n", 647 p->p_pid, rval1, rval2, curthread); 648 mutex_exit(&systrace_lock); 649 } 650 #endif /* SYSCALLTRACE */ 651 rp->r_tstate &= ~TSTATE_IC; 652 rp->r_o0 = rval1; 653 rp->r_o1 = rval2; 654 } else { 655 int sig; 656 657 #ifdef SYSCALLTRACE 658 if (syscalltrace) { 659 mutex_enter(&systrace_lock); 660 printf("%d: error=%d, id 0x%p\n", 661 p->p_pid, error, curthread); 662 mutex_exit(&systrace_lock); 663 } 664 #endif /* SYSCALLTRACE */ 665 if (error == EINTR && t->t_activefd.a_stale) 666 error = EBADF; 667 if (error == EINTR && 668 (sig = lwp->lwp_cursig) != 0 && 669 sigismember(&PTOU(p)->u_sigrestart, sig) && 670 PTOU(p)->u_signal[sig - 1] != SIG_DFL && 671 PTOU(p)->u_signal[sig - 1] != SIG_IGN) 672 error = ERESTART; 673 rp->r_o0 = error; 674 rp->r_tstate |= TSTATE_IC; 675 } 676 /* 677 * The default action is to redo the trap instruction. 678 * We increment the pc and npc past it for NORMALRETURN. 679 * JUSTRETURN has set up a new pc and npc already. 680 * RESTARTSYS automatically restarts by leaving pc and npc 681 * alone. 682 */ 683 rp->r_pc = rp->r_npc; 684 rp->r_npc += 4; 685 } 686 687 /* 688 * From the proc(4) manual page: 689 * When exit from a system call is being traced, the traced process 690 * stops on completion of the system call just prior to checking for 691 * signals and returning to user level. At this point all return 692 * values have been stored into the traced process's saved registers. 693 */ 694 if (proc_stop) { 695 mutex_enter(&p->p_lock); 696 if (PTOU(p)->u_systrap && 697 prismember(&PTOU(p)->u_exitmask, code)) 698 stop(PR_SYSEXIT, code); 699 mutex_exit(&p->p_lock); 700 } 701 702 /* 703 * If we are the parent returning from a successful 704 * vfork, wait for the child to exec or exit. 705 * This code must be here and not in the bowels of the system 706 * so that /proc can intercept exit from vfork in a timely way. 707 */ 708 if (code == SYS_vfork && rp->r_o1 == 0 && error == 0) 709 vfwait((pid_t)rval1); 710 711 /* 712 * If profiling is active, bill the current PC in user-land 713 * and keep reposting until profiling is disabled. 714 */ 715 if (p->p_prof.pr_scale) { 716 if (lwp->lwp_oweupc) 717 profil_tick(rp->r_pc); 718 repost = 1; 719 } 720 721 sig_check: 722 /* 723 * Reset flag for next time. 724 * We must do this after stopping on PR_SYSEXIT 725 * because /proc uses the information in lwp_eosys. 726 */ 727 lwp->lwp_eosys = NORMALRETURN; 728 clear_stale_fd(); 729 730 if (t->t_astflag | t->t_sig_check) { 731 /* 732 * Turn off the AST flag before checking all the conditions that 733 * may have caused an AST. This flag is on whenever a signal or 734 * unusual condition should be handled after the next trap or 735 * syscall. 736 */ 737 astoff(t); 738 t->t_sig_check = 0; 739 740 mutex_enter(&p->p_lock); 741 if (curthread->t_proc_flag & TP_CHANGEBIND) { 742 timer_lwpbind(); 743 curthread->t_proc_flag &= ~TP_CHANGEBIND; 744 } 745 mutex_exit(&p->p_lock); 746 747 /* 748 * for kaio requests on the special kaio poll queue, 749 * copyout their results to user memory. 750 */ 751 if (p->p_aio) 752 aio_cleanup(0); 753 754 /* 755 * If this LWP was asked to hold, call holdlwp(), which will 756 * stop. holdlwps() sets this up and calls pokelwps() which 757 * sets the AST flag. 758 * 759 * Also check TP_EXITLWP, since this is used by fresh new LWPs 760 * through lwp_rtt(). That flag is set if the lwp_create(2) 761 * syscall failed after creating the LWP. 762 */ 763 if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP)) 764 holdlwp(); 765 766 /* 767 * All code that sets signals and makes ISSIG_PENDING 768 * evaluate true must set t_sig_check afterwards. 769 */ 770 if (ISSIG_PENDING(t, lwp, p)) { 771 if (issig(FORREAL)) 772 psig(); 773 t->t_sig_check = 1; /* recheck next time */ 774 } 775 776 if (sigprof) { 777 realsigprof(code, error); 778 t->t_sig_check = 1; /* recheck next time */ 779 } 780 781 /* 782 * If a performance counter overflow interrupt was 783 * delivered *during* the syscall, then re-enable the 784 * AST so that we take a trip through trap() to cause 785 * the SIGEMT to be delivered. 786 */ 787 if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) 788 aston(t); 789 790 /* 791 * If an asynchronous hardware error is pending, turn AST flag 792 * back on. AST will be checked again before we return to user 793 * mode and we'll come back through trap() to handle the error. 794 */ 795 if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) 796 aston(t); 797 } 798 799 /* 800 * Restore register window if a debugger modified it. 801 * Set up to perform a single-step if a debugger requested it. 802 */ 803 if (lwp->lwp_pcb.pcb_xregstat != XREGNONE) 804 xregrestore(lwp, 1); 805 806 lwp->lwp_errno = 0; /* clear error for next time */ 807 808 #ifndef NPROBE 809 /* Kernel probe */ 810 if (tnf_tracing_active) { 811 TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */, 812 tnf_long, rval1, rval1, 813 tnf_long, rval2, rval2, 814 tnf_long, errno, (long)error); 815 repost = 1; 816 } 817 #endif /* NPROBE */ 818 819 /* 820 * Set state to LWP_USER here so preempt won't give us a kernel 821 * priority if it occurs after this point. Call CL_TRAPRET() to 822 * restore the user-level priority. 823 * 824 * It is important that no locks (other than spinlocks) be entered 825 * after this point before returning to user mode (unless lwp_state 826 * is set back to LWP_SYS). 827 * 828 * Sampled times past this point are charged to the user. 829 */ 830 lwp->lwp_state = LWP_USER; 831 832 if (t->t_trapret) { 833 t->t_trapret = 0; 834 thread_lock(t); 835 CL_TRAPRET(t); 836 thread_unlock(t); 837 } 838 if (CPU->cpu_runrun) 839 preempt(); 840 841 /* 842 * t_post_sys will be set if pcb_step is active. 843 */ 844 if (lwp->lwp_pcb.pcb_step != STEP_NONE) { 845 prdostep(); 846 repost = 1; 847 } 848 849 t->t_sysnum = 0; /* no longer in a system call */ 850 851 /* 852 * In case the args were copied to the lwp, reset the 853 * pointer so the next syscall will have the right lwp_ap pointer. 854 */ 855 lwp->lwp_ap = (long *)&rp->r_o0; 856 lwp->lwp_argsaved = 0; 857 858 /* 859 * If there was a continuing reason for post-syscall processing, 860 * set the t_post_sys flag for the next system call. 861 */ 862 if (repost) 863 t->t_post_sys = 1; 864 865 /* 866 * If there is a ustack registered for this lwp, and the stack rlimit 867 * has been altered, read in the ustack. If the saved stack rlimit 868 * matches the bounds of the ustack, update the ustack to reflect 869 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable 870 * stack checking by setting the size to 0. 871 */ 872 if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) { 873 rlim64_t new_size; 874 model_t model; 875 caddr_t top; 876 struct rlimit64 rl; 877 878 mutex_enter(&p->p_lock); 879 new_size = p->p_stk_ctl; 880 model = p->p_model; 881 top = p->p_usrstack; 882 (void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl); 883 mutex_exit(&p->p_lock); 884 885 if (rl.rlim_cur == RLIM64_INFINITY) 886 new_size = 0; 887 888 if (model == DATAMODEL_NATIVE) { 889 stack_t stk; 890 891 if (copyin((stack_t *)lwp->lwp_ustack, &stk, 892 sizeof (stack_t)) == 0 && 893 (stk.ss_size == lwp->lwp_old_stk_ctl || 894 stk.ss_size == 0) && 895 stk.ss_sp == top - stk.ss_size) { 896 stk.ss_sp = (void *)((uintptr_t)stk.ss_sp + 897 stk.ss_size - new_size); 898 stk.ss_size = new_size; 899 900 (void) copyout(&stk, 901 (stack_t *)lwp->lwp_ustack, 902 sizeof (stack_t)); 903 } 904 } else { 905 stack32_t stk32; 906 907 if (copyin((stack32_t *)lwp->lwp_ustack, &stk32, 908 sizeof (stack32_t)) == 0 && 909 (stk32.ss_size == lwp->lwp_old_stk_ctl || 910 stk32.ss_size == 0) && 911 stk32.ss_sp == (caddr32_t)(top - stk32.ss_size)) { 912 stk32.ss_sp += stk32.ss_size - new_size; 913 stk32.ss_size = new_size; 914 915 (void) copyout(&stk32, 916 (stack32_t *)lwp->lwp_ustack, 917 sizeof (stack32_t)); 918 } 919 } 920 921 lwp->lwp_old_stk_ctl = 0; 922 } 923 924 syscall_mstate(LMS_SYSTEM, LMS_USER); 925 } 926 927 /* 928 * Call a system call which takes a pointer to the user args struct and 929 * a pointer to the return values. This is a bit slower than the standard 930 * C arg-passing method in some cases. 931 */ 932 int64_t 933 syscall_ap() 934 { 935 uint_t error; 936 struct sysent *callp; 937 rval_t rval; 938 klwp_t *lwp = ttolwp(curthread); 939 struct regs *rp = lwptoregs(lwp); 940 941 callp = LWP_GETSYSENT(lwp) + curthread->t_sysnum; 942 943 /* 944 * If the arguments don't fit in registers %o0 - o5, make sure they 945 * have been copied to the lwp_arg array. 946 */ 947 if (callp->sy_narg > 6 && save_syscall_args()) 948 return ((int64_t)set_errno(EFAULT)); 949 950 rval.r_val1 = 0; 951 rval.r_val2 = (int)rp->r_o1; 952 lwp->lwp_error = 0; /* for old drivers */ 953 error = (*(callp->sy_call))(lwp->lwp_ap, &rval); 954 if (error) 955 return ((int64_t)set_errno(error)); 956 return (rval.r_vals); 957 } 958 959 /* 960 * Load system call module. 961 * Returns with pointer to held read lock for module. 962 */ 963 static krwlock_t * 964 lock_syscall(struct sysent *table, uint_t code) 965 { 966 krwlock_t *module_lock; 967 struct modctl *modp; 968 int id; 969 struct sysent *callp; 970 971 module_lock = table[code].sy_lock; 972 callp = &table[code]; 973 974 /* 975 * Optimization to only call modload if we don't have a loaded 976 * syscall. 977 */ 978 rw_enter(module_lock, RW_READER); 979 if (LOADED_SYSCALL(callp)) 980 return (module_lock); 981 rw_exit(module_lock); 982 983 for (;;) { 984 if ((id = modload("sys", syscallnames[code])) == -1) 985 break; 986 987 /* 988 * If we loaded successfully at least once, the modctl 989 * will still be valid, so we try to grab it by filename. 990 * If this call fails, it's because the mod_filename 991 * was changed after the call to modload() (mod_hold_by_name() 992 * is the likely culprit). We can safely just take 993 * another lap if this is the case; the modload() will 994 * change the mod_filename back to one by which we can 995 * find the modctl. 996 */ 997 modp = mod_find_by_filename("sys", syscallnames[code]); 998 999 if (modp == NULL) 1000 continue; 1001 1002 mutex_enter(&mod_lock); 1003 1004 if (!modp->mod_installed) { 1005 mutex_exit(&mod_lock); 1006 continue; 1007 } 1008 break; 1009 } 1010 1011 rw_enter(module_lock, RW_READER); 1012 1013 if (id != -1) 1014 mutex_exit(&mod_lock); 1015 1016 return (module_lock); 1017 } 1018 1019 /* 1020 * Loadable syscall support. 1021 * If needed, load the module, then reserve it by holding a read 1022 * lock for the duration of the call. 1023 * Later, if the syscall is not unloadable, it could patch the vector. 1024 */ 1025 /*ARGSUSED*/ 1026 int64_t 1027 loadable_syscall( 1028 long a0, long a1, long a2, long a3, 1029 long a4, long a5, long a6, long a7) 1030 { 1031 int64_t rval; 1032 struct sysent *callp; 1033 struct sysent *se = LWP_GETSYSENT(ttolwp(curthread)); 1034 krwlock_t *module_lock; 1035 int code; 1036 1037 code = curthread->t_sysnum; 1038 callp = se + code; 1039 1040 /* 1041 * Try to autoload the system call if necessary. 1042 */ 1043 module_lock = lock_syscall(se, code); 1044 THREAD_KPRI_RELEASE(); /* drop priority given by rw_enter */ 1045 1046 /* 1047 * we've locked either the loaded syscall or nosys 1048 */ 1049 if (callp->sy_flags & SE_ARGC) { 1050 int64_t (*sy_call)(); 1051 1052 sy_call = (int64_t (*)())callp->sy_call; 1053 rval = (*sy_call)(a0, a1, a2, a3, a4, a5); 1054 } else { 1055 rval = syscall_ap(); 1056 } 1057 1058 THREAD_KPRI_REQUEST(); /* regain priority from read lock */ 1059 rw_exit(module_lock); 1060 return (rval); 1061 } 1062 1063 /* 1064 * Handle indirect system calls. 1065 * This interface should be deprecated. The library can handle 1066 * this more efficiently, but keep this implementation for old binaries. 1067 * 1068 * XX64 Needs some work. 1069 */ 1070 int64_t 1071 indir(int code, long a0, long a1, long a2, long a3, long a4) 1072 { 1073 klwp_t *lwp = ttolwp(curthread); 1074 struct sysent *callp; 1075 1076 if (code <= 0 || code >= NSYSCALL) 1077 return (nosys()); 1078 1079 ASSERT(lwp->lwp_ap != NULL); 1080 1081 curthread->t_sysnum = code; 1082 callp = LWP_GETSYSENT(lwp) + code; 1083 1084 /* 1085 * Handle argument setup, unless already done in pre_syscall(). 1086 */ 1087 if (callp->sy_narg > 5) { 1088 if (save_syscall_args()) /* move args to LWP array */ 1089 return ((int64_t)set_errno(EFAULT)); 1090 } else if (!lwp->lwp_argsaved) { 1091 long *ap; 1092 1093 ap = lwp->lwp_ap; /* args haven't been saved */ 1094 lwp->lwp_ap = ap + 1; /* advance arg pointer */ 1095 curthread->t_post_sys = 1; /* so lwp_ap will be reset */ 1096 } 1097 return ((*callp->sy_callc)(a0, a1, a2, a3, a4, lwp->lwp_arg[5])); 1098 } 1099 1100 /* 1101 * set_errno - set an error return from the current system call. 1102 * This could be a macro. 1103 * This returns the value it is passed, so that the caller can 1104 * use tail-recursion-elimination and do return (set_errno(ERRNO)); 1105 */ 1106 uint_t 1107 set_errno(uint_t error) 1108 { 1109 ASSERT(error != 0); /* must not be used to clear errno */ 1110 1111 curthread->t_post_sys = 1; /* have post_syscall do error return */ 1112 return (ttolwp(curthread)->lwp_errno = error); 1113 } 1114 1115 /* 1116 * set_proc_pre_sys - Set pre-syscall processing for entire process. 1117 */ 1118 void 1119 set_proc_pre_sys(proc_t *p) 1120 { 1121 kthread_t *t; 1122 kthread_t *first; 1123 1124 ASSERT(MUTEX_HELD(&p->p_lock)); 1125 1126 t = first = p->p_tlist; 1127 do { 1128 t->t_pre_sys = 1; 1129 } while ((t = t->t_forw) != first); 1130 } 1131 1132 /* 1133 * set_proc_post_sys - Set post-syscall processing for entire process. 1134 */ 1135 void 1136 set_proc_post_sys(proc_t *p) 1137 { 1138 kthread_t *t; 1139 kthread_t *first; 1140 1141 ASSERT(MUTEX_HELD(&p->p_lock)); 1142 1143 t = first = p->p_tlist; 1144 do { 1145 t->t_post_sys = 1; 1146 } while ((t = t->t_forw) != first); 1147 } 1148 1149 /* 1150 * set_proc_sys - Set pre- and post-syscall processing for entire process. 1151 */ 1152 void 1153 set_proc_sys(proc_t *p) 1154 { 1155 kthread_t *t; 1156 kthread_t *first; 1157 1158 ASSERT(MUTEX_HELD(&p->p_lock)); 1159 1160 t = first = p->p_tlist; 1161 do { 1162 t->t_pre_sys = 1; 1163 t->t_post_sys = 1; 1164 } while ((t = t->t_forw) != first); 1165 } 1166 1167 /* 1168 * set_all_proc_sys - set pre- and post-syscall processing flags for all 1169 * user processes. 1170 * 1171 * This is needed when auditing, tracing, or other facilities which affect 1172 * all processes are turned on. 1173 */ 1174 void 1175 set_all_proc_sys() 1176 { 1177 kthread_t *t; 1178 kthread_t *first; 1179 1180 mutex_enter(&pidlock); 1181 t = first = curthread; 1182 do { 1183 t->t_pre_sys = 1; 1184 t->t_post_sys = 1; 1185 } while ((t = t->t_next) != first); 1186 mutex_exit(&pidlock); 1187 } 1188 1189 /* 1190 * set_proc_ast - Set asynchronous service trap (AST) flag for all 1191 * threads in process. 1192 */ 1193 void 1194 set_proc_ast(proc_t *p) 1195 { 1196 kthread_t *t; 1197 kthread_t *first; 1198 1199 ASSERT(MUTEX_HELD(&p->p_lock)); 1200 1201 t = first = p->p_tlist; 1202 do { 1203 aston(t); 1204 } while ((t = t->t_forw) != first); 1205 } 1206