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 2007 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/signal.h> 35 #include <sys/stack.h> 36 #include <sys/cred.h> 37 #include <sys/cmn_err.h> 38 #include <sys/user.h> 39 #include <sys/privregs.h> 40 #include <sys/psw.h> 41 #include <sys/debug.h> 42 #include <sys/errno.h> 43 #include <sys/proc.h> 44 #include <sys/modctl.h> 45 #include <sys/var.h> 46 #include <sys/inline.h> 47 #include <sys/syscall.h> 48 #include <sys/ucontext.h> 49 #include <sys/cpuvar.h> 50 #include <sys/siginfo.h> 51 #include <sys/trap.h> 52 #include <sys/vtrace.h> 53 #include <sys/sysinfo.h> 54 #include <sys/procfs.h> 55 #include <c2/audit.h> 56 #include <sys/modctl.h> 57 #include <sys/aio_impl.h> 58 #include <sys/tnf.h> 59 #include <sys/tnf_probe.h> 60 #include <sys/copyops.h> 61 #include <sys/priv.h> 62 #include <sys/msacct.h> 63 64 int syscalltrace = 0; 65 #ifdef SYSCALLTRACE 66 static kmutex_t systrace_lock; /* syscall tracing lock */ 67 #else 68 #define syscalltrace 0 69 #endif /* SYSCALLTRACE */ 70 71 typedef int64_t (*llfcn_t)(); /* function returning long long */ 72 73 int pre_syscall(void); 74 void post_syscall(long rval1, long rval2); 75 static krwlock_t *lock_syscall(struct sysent *, uint_t); 76 void deferred_singlestep_trap(caddr_t); 77 78 #ifdef _SYSCALL32_IMPL 79 #define LWP_GETSYSENT(lwp) \ 80 (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? sysent : sysent32) 81 #else 82 #define LWP_GETSYSENT(lwp) (sysent) 83 #endif 84 85 /* 86 * Arrange for the real time profiling signal to be dispatched. 87 */ 88 void 89 realsigprof(int sysnum, int error) 90 { 91 proc_t *p; 92 klwp_t *lwp; 93 94 if (curthread->t_rprof->rp_anystate == 0) 95 return; 96 p = ttoproc(curthread); 97 lwp = ttolwp(curthread); 98 mutex_enter(&p->p_lock); 99 if (sigismember(&p->p_ignore, SIGPROF) || 100 signal_is_blocked(curthread, SIGPROF)) { 101 mutex_exit(&p->p_lock); 102 return; 103 } 104 lwp->lwp_siginfo.si_signo = SIGPROF; 105 lwp->lwp_siginfo.si_code = PROF_SIG; 106 lwp->lwp_siginfo.si_errno = error; 107 hrt2ts(gethrtime(), &lwp->lwp_siginfo.si_tstamp); 108 lwp->lwp_siginfo.si_syscall = sysnum; 109 lwp->lwp_siginfo.si_nsysarg = (sysnum > 0 && sysnum < NSYSCALL) ? 110 LWP_GETSYSENT(lwp)[sysnum].sy_narg : 0; 111 lwp->lwp_siginfo.si_fault = lwp->lwp_lastfault; 112 lwp->lwp_siginfo.si_faddr = lwp->lwp_lastfaddr; 113 lwp->lwp_lastfault = 0; 114 lwp->lwp_lastfaddr = NULL; 115 sigtoproc(p, curthread, SIGPROF); 116 mutex_exit(&p->p_lock); 117 ASSERT(lwp->lwp_cursig == 0); 118 if (issig(FORREAL)) 119 psig(); 120 mutex_enter(&p->p_lock); 121 lwp->lwp_siginfo.si_signo = 0; 122 bzero(curthread->t_rprof, sizeof (*curthread->t_rprof)); 123 mutex_exit(&p->p_lock); 124 } 125 126 /* 127 * If watchpoints are active, don't make copying in of 128 * system call arguments take a read watchpoint trap. 129 */ 130 static int 131 copyin_args(struct regs *rp, long *ap, uint_t nargs) 132 { 133 greg_t *sp = 1 + (greg_t *)rp->r_sp; /* skip ret addr */ 134 135 ASSERT(nargs <= MAXSYSARGS); 136 137 return (copyin_nowatch(sp, ap, nargs * sizeof (*sp))); 138 } 139 140 #if defined(_SYSCALL32_IMPL) 141 static int 142 copyin_args32(struct regs *rp, long *ap, uint_t nargs) 143 { 144 greg32_t *sp = 1 + (greg32_t *)rp->r_sp; /* skip ret addr */ 145 uint32_t a32[MAXSYSARGS]; 146 int rc; 147 148 ASSERT(nargs <= MAXSYSARGS); 149 150 if ((rc = copyin_nowatch(sp, a32, nargs * sizeof (*sp))) == 0) { 151 uint32_t *a32p = &a32[0]; 152 153 while (nargs--) 154 *ap++ = (ulong_t)*a32p++; 155 } 156 return (rc); 157 } 158 #define COPYIN_ARGS32 copyin_args32 159 #else 160 #define COPYIN_ARGS32 copyin_args 161 #endif 162 163 /* 164 * Error handler for system calls where arg copy gets fault. 165 */ 166 static longlong_t 167 syscall_err() 168 { 169 return (0); 170 } 171 172 /* 173 * Corresponding sysent entry to allow syscall_entry caller 174 * to invoke syscall_err. 175 */ 176 static struct sysent sysent_err = { 177 0, SE_32RVAL1, NULL, NULL, (llfcn_t)syscall_err 178 }; 179 180 /* 181 * Called from syscall() when a non-trivial 32-bit system call occurs. 182 * Sets up the args and returns a pointer to the handler. 183 */ 184 struct sysent * 185 syscall_entry(kthread_t *t, long *argp) 186 { 187 klwp_t *lwp = ttolwp(t); 188 struct regs *rp = lwptoregs(lwp); 189 unsigned int code; 190 struct sysent *callp; 191 struct sysent *se = LWP_GETSYSENT(lwp); 192 int error = 0; 193 uint_t nargs; 194 195 ASSERT(t == curthread && curthread->t_schedflag & TS_DONT_SWAP); 196 197 lwp->lwp_ru.sysc++; 198 lwp->lwp_eosys = NORMALRETURN; /* assume this will be normal */ 199 200 /* 201 * Set lwp_ap to point to the args, even if none are needed for this 202 * system call. This is for the loadable-syscall case where the 203 * number of args won't be known until the system call is loaded, and 204 * also maintains a non-NULL lwp_ap setup for get_syscall_args(). Note 205 * that lwp_ap MUST be set to a non-NULL value _BEFORE_ t_sysnum is 206 * set to non-zero; otherwise get_syscall_args(), seeing a non-zero 207 * t_sysnum for this thread, will charge ahead and dereference lwp_ap. 208 */ 209 lwp->lwp_ap = argp; /* for get_syscall_args */ 210 211 code = rp->r_r0; 212 t->t_sysnum = (short)code; 213 callp = code >= NSYSCALL ? &nosys_ent : se + code; 214 215 if ((t->t_pre_sys | syscalltrace) != 0) { 216 error = pre_syscall(); 217 218 /* 219 * pre_syscall() has taken care so that lwp_ap is current; 220 * it either points to syscall-entry-saved amd64 regs, 221 * or it points to lwp_arg[], which has been re-copied from 222 * the ia32 ustack, but either way, it's a current copy after 223 * /proc has possibly mucked with the syscall args. 224 */ 225 226 if (error) 227 return (&sysent_err); /* use dummy handler */ 228 } 229 230 /* 231 * Fetch the system call arguments to the kernel stack copy used 232 * for syscall handling. 233 * Note: for loadable system calls the number of arguments required 234 * may not be known at this point, and will be zero if the system call 235 * was never loaded. Once the system call has been loaded, the number 236 * of args is not allowed to be changed. 237 */ 238 if ((nargs = (uint_t)callp->sy_narg) != 0 && 239 COPYIN_ARGS32(rp, argp, nargs)) { 240 (void) set_errno(EFAULT); 241 return (&sysent_err); /* use dummy handler */ 242 } 243 244 return (callp); /* return sysent entry for caller */ 245 } 246 247 void 248 syscall_exit(kthread_t *t, long rval1, long rval2) 249 { 250 /* 251 * Handle signals and other post-call events if necessary. 252 */ 253 if ((t->t_post_sys_ast | syscalltrace) == 0) { 254 klwp_t *lwp = ttolwp(t); 255 struct regs *rp = lwptoregs(lwp); 256 257 /* 258 * Normal return. 259 * Clear error indication and set return values. 260 */ 261 rp->r_ps &= ~PS_C; /* reset carry bit */ 262 rp->r_r0 = rval1; 263 rp->r_r1 = rval2; 264 lwp->lwp_state = LWP_USER; 265 } else 266 post_syscall(rval1, rval2); 267 t->t_sysnum = 0; /* invalidate args */ 268 } 269 270 /* 271 * Perform pre-system-call processing, including stopping for tracing, 272 * auditing, etc. 273 * 274 * This routine is called only if the t_pre_sys flag is set. Any condition 275 * requiring pre-syscall handling must set the t_pre_sys flag. If the 276 * condition is persistent, this routine will repost t_pre_sys. 277 */ 278 int 279 pre_syscall() 280 { 281 kthread_t *t = curthread; 282 unsigned code = t->t_sysnum; 283 klwp_t *lwp = ttolwp(t); 284 proc_t *p = ttoproc(t); 285 int repost; 286 287 t->t_pre_sys = repost = 0; /* clear pre-syscall processing flag */ 288 289 ASSERT(t->t_schedflag & TS_DONT_SWAP); 290 291 #if defined(DEBUG) 292 /* 293 * On the i386 kernel, lwp_ap points at the piece of the thread 294 * stack that we copy the users arguments into. 295 * 296 * On the amd64 kernel, the syscall arguments in the rdi..r9 297 * registers should be pointed at by lwp_ap. If the args need to 298 * be copied so that those registers can be changed without losing 299 * the ability to get the args for /proc, they can be saved by 300 * save_syscall_args(), and lwp_ap will be restored by post_syscall(). 301 */ 302 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 303 #if defined(_LP64) 304 ASSERT(lwp->lwp_ap == (long *)&lwptoregs(lwp)->r_rdi); 305 } else { 306 #endif 307 ASSERT((caddr_t)lwp->lwp_ap > t->t_stkbase && 308 (caddr_t)lwp->lwp_ap < t->t_stk); 309 } 310 #endif /* DEBUG */ 311 312 /* 313 * Make sure the thread is holding the latest credentials for the 314 * process. The credentials in the process right now apply to this 315 * thread for the entire system call. 316 */ 317 if (t->t_cred != p->p_cred) { 318 cred_t *oldcred = t->t_cred; 319 /* 320 * DTrace accesses t_cred in probe context. t_cred must 321 * always be either NULL, or point to a valid, allocated cred 322 * structure. 323 */ 324 t->t_cred = crgetcred(); 325 crfree(oldcred); 326 } 327 328 /* 329 * From the proc(4) manual page: 330 * When entry to a system call is being traced, the traced process 331 * stops after having begun the call to the system but before the 332 * system call arguments have been fetched from the process. 333 */ 334 if (PTOU(p)->u_systrap) { 335 if (prismember(&PTOU(p)->u_entrymask, code)) { 336 mutex_enter(&p->p_lock); 337 /* 338 * Recheck stop condition, now that lock is held. 339 */ 340 if (PTOU(p)->u_systrap && 341 prismember(&PTOU(p)->u_entrymask, code)) { 342 stop(PR_SYSENTRY, code); 343 344 /* 345 * /proc may have modified syscall args, 346 * either in regs for amd64 or on ustack 347 * for ia32. Either way, arrange to 348 * copy them again, both for the syscall 349 * handler and for other consumers in 350 * post_syscall (like audit). Here, we 351 * only do amd64, and just set lwp_ap 352 * back to the kernel-entry stack copy; 353 * the syscall ml code redoes 354 * move-from-regs to set up for the 355 * syscall handler after we return. For 356 * ia32, save_syscall_args() below makes 357 * an lwp_ap-accessible copy. 358 */ 359 #if defined(_LP64) 360 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 361 lwp->lwp_argsaved = 0; 362 lwp->lwp_ap = 363 (long *)&lwptoregs(lwp)->r_rdi; 364 } 365 #endif 366 } 367 mutex_exit(&p->p_lock); 368 } 369 repost = 1; 370 } 371 372 /* 373 * ia32 kernel, or ia32 proc on amd64 kernel: keep args in 374 * lwp_arg for post-syscall processing, regardless of whether 375 * they might have been changed in /proc above. 376 */ 377 #if defined(_LP64) 378 if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE) 379 #endif 380 (void) save_syscall_args(); 381 382 if (lwp->lwp_sysabort) { 383 /* 384 * lwp_sysabort may have been set via /proc while the process 385 * was stopped on PR_SYSENTRY. If so, abort the system call. 386 * Override any error from the copyin() of the arguments. 387 */ 388 lwp->lwp_sysabort = 0; 389 (void) set_errno(EINTR); /* forces post_sys */ 390 t->t_pre_sys = 1; /* repost anyway */ 391 return (1); /* don't do system call, return EINTR */ 392 } 393 394 #ifdef C2_AUDIT 395 if (audit_active) { /* begin auditing for this syscall */ 396 int error; 397 if (error = audit_start(T_SYSCALL, code, 0, lwp)) { 398 t->t_pre_sys = 1; /* repost anyway */ 399 (void) set_errno(error); 400 return (1); 401 } 402 repost = 1; 403 } 404 #endif /* C2_AUDIT */ 405 406 #ifndef NPROBE 407 /* Kernel probe */ 408 if (tnf_tracing_active) { 409 TNF_PROBE_1(syscall_start, "syscall thread", /* CSTYLED */, 410 tnf_sysnum, sysnum, t->t_sysnum); 411 t->t_post_sys = 1; /* make sure post_syscall runs */ 412 repost = 1; 413 } 414 #endif /* NPROBE */ 415 416 #ifdef SYSCALLTRACE 417 if (syscalltrace) { 418 int i; 419 long *ap; 420 char *cp; 421 char *sysname; 422 struct sysent *callp; 423 424 if (code >= NSYSCALL) 425 callp = &nosys_ent; /* nosys has no args */ 426 else 427 callp = LWP_GETSYSENT(lwp) + code; 428 (void) save_syscall_args(); 429 mutex_enter(&systrace_lock); 430 printf("%d: ", p->p_pid); 431 if (code >= NSYSCALL) 432 printf("0x%x", code); 433 else { 434 sysname = mod_getsysname(code); 435 printf("%s[0x%x/0x%p]", sysname == NULL ? "NULL" : 436 sysname, code, callp->sy_callc); 437 } 438 cp = "("; 439 for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) { 440 printf("%s%lx", cp, *ap); 441 cp = ", "; 442 } 443 if (i) 444 printf(")"); 445 printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread); 446 mutex_exit(&systrace_lock); 447 } 448 #endif /* SYSCALLTRACE */ 449 450 /* 451 * If there was a continuing reason for pre-syscall processing, 452 * set the t_pre_sys flag for the next system call. 453 */ 454 if (repost) 455 t->t_pre_sys = 1; 456 lwp->lwp_error = 0; /* for old drivers */ 457 lwp->lwp_badpriv = PRIV_NONE; 458 return (0); 459 } 460 461 462 /* 463 * Post-syscall processing. Perform abnormal system call completion 464 * actions such as /proc tracing, profiling, signals, preemption, etc. 465 * 466 * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set. 467 * Any condition requiring pre-syscall handling must set one of these. 468 * If the condition is persistent, this routine will repost t_post_sys. 469 */ 470 void 471 post_syscall(long rval1, long rval2) 472 { 473 kthread_t *t = curthread; 474 klwp_t *lwp = ttolwp(t); 475 proc_t *p = ttoproc(t); 476 struct regs *rp = lwptoregs(lwp); 477 uint_t error; 478 uint_t code = t->t_sysnum; 479 int repost = 0; 480 int proc_stop = 0; /* non-zero if stopping */ 481 int sigprof = 0; /* non-zero if sending SIGPROF */ 482 483 t->t_post_sys = 0; 484 485 error = lwp->lwp_errno; 486 487 /* 488 * Code can be zero if this is a new LWP returning after a forkall(), 489 * other than the one which matches the one in the parent which called 490 * forkall(). In these LWPs, skip most of post-syscall activity. 491 */ 492 if (code == 0) 493 goto sig_check; 494 /* 495 * If the trace flag is set, mark the lwp to take a single-step trap 496 * on return to user level (below). The x86 lcall interface and 497 * sysenter has already done this, and turned off the flag, but 498 * amd64 syscall interface has not. 499 */ 500 if (rp->r_ps & PS_T) { 501 lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING; 502 rp->r_ps &= ~PS_T; 503 aston(curthread); 504 } 505 #ifdef C2_AUDIT 506 if (audit_active) { /* put out audit record for this syscall */ 507 rval_t rval; 508 509 /* XX64 -- truncation of 64-bit return values? */ 510 rval.r_val1 = (int)rval1; 511 rval.r_val2 = (int)rval2; 512 audit_finish(T_SYSCALL, code, error, &rval); 513 repost = 1; 514 } 515 #endif /* C2_AUDIT */ 516 517 if (curthread->t_pdmsg != NULL) { 518 char *m = curthread->t_pdmsg; 519 520 uprintf("%s", m); 521 kmem_free(m, strlen(m) + 1); 522 curthread->t_pdmsg = NULL; 523 } 524 525 /* 526 * If we're going to stop for /proc tracing, set the flag and 527 * save the arguments so that the return values don't smash them. 528 */ 529 if (PTOU(p)->u_systrap) { 530 if (prismember(&PTOU(p)->u_exitmask, code)) { 531 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 532 (void) save_syscall_args(); 533 proc_stop = 1; 534 } 535 repost = 1; 536 } 537 538 /* 539 * Similarly check to see if SIGPROF might be sent. 540 */ 541 if (curthread->t_rprof != NULL && 542 curthread->t_rprof->rp_anystate != 0) { 543 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 544 (void) save_syscall_args(); 545 sigprof = 1; 546 } 547 548 if (lwp->lwp_eosys == NORMALRETURN) { 549 if (error == 0) { 550 #ifdef SYSCALLTRACE 551 if (syscalltrace) { 552 mutex_enter(&systrace_lock); 553 printf( 554 "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n", 555 p->p_pid, rval1, rval2, curthread); 556 mutex_exit(&systrace_lock); 557 } 558 #endif /* SYSCALLTRACE */ 559 rp->r_ps &= ~PS_C; 560 rp->r_r0 = rval1; 561 rp->r_r1 = rval2; 562 } else { 563 int sig; 564 #ifdef SYSCALLTRACE 565 if (syscalltrace) { 566 mutex_enter(&systrace_lock); 567 printf("%d: error=%d, id 0x%p\n", 568 p->p_pid, error, curthread); 569 mutex_exit(&systrace_lock); 570 } 571 #endif /* SYSCALLTRACE */ 572 if (error == EINTR && t->t_activefd.a_stale) 573 error = EBADF; 574 if (error == EINTR && 575 (sig = lwp->lwp_cursig) != 0 && 576 sigismember(&PTOU(p)->u_sigrestart, sig) && 577 PTOU(p)->u_signal[sig - 1] != SIG_DFL && 578 PTOU(p)->u_signal[sig - 1] != SIG_IGN) 579 error = ERESTART; 580 rp->r_r0 = error; 581 rp->r_ps |= PS_C; 582 } 583 } 584 585 /* 586 * From the proc(4) manual page: 587 * When exit from a system call is being traced, the traced process 588 * stops on completion of the system call just prior to checking for 589 * signals and returning to user level. At this point all return 590 * values have been stored into the traced process's saved registers. 591 */ 592 if (proc_stop) { 593 mutex_enter(&p->p_lock); 594 if (PTOU(p)->u_systrap && 595 prismember(&PTOU(p)->u_exitmask, code)) 596 stop(PR_SYSEXIT, code); 597 mutex_exit(&p->p_lock); 598 } 599 600 /* 601 * If we are the parent returning from a successful 602 * vfork, wait for the child to exec or exit. 603 * This code must be here and not in the bowels of the system 604 * so that /proc can intercept exit from vfork in a timely way. 605 */ 606 if (t->t_flag & T_VFPARENT) { 607 ASSERT(code == SYS_vfork || code == SYS_forksys); 608 ASSERT(rp->r_r1 == 0 && error == 0); 609 vfwait((pid_t)rval1); 610 t->t_flag &= ~T_VFPARENT; 611 } 612 613 /* 614 * If profiling is active, bill the current PC in user-land 615 * and keep reposting until profiling is disabled. 616 */ 617 if (p->p_prof.pr_scale) { 618 if (lwp->lwp_oweupc) 619 profil_tick(rp->r_pc); 620 repost = 1; 621 } 622 623 sig_check: 624 /* 625 * Reset flag for next time. 626 * We must do this after stopping on PR_SYSEXIT 627 * because /proc uses the information in lwp_eosys. 628 */ 629 lwp->lwp_eosys = NORMALRETURN; 630 clear_stale_fd(); 631 t->t_flag &= ~T_FORKALL; 632 633 if (t->t_astflag | t->t_sig_check) { 634 /* 635 * Turn off the AST flag before checking all the conditions that 636 * may have caused an AST. This flag is on whenever a signal or 637 * unusual condition should be handled after the next trap or 638 * syscall. 639 */ 640 astoff(t); 641 /* 642 * If a single-step trap occurred on a syscall (see trap()) 643 * recognize it now. Do this before checking for signals 644 * because deferred_singlestep_trap() may generate a SIGTRAP to 645 * the LWP or may otherwise mark the LWP to call issig(FORREAL). 646 */ 647 if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING) 648 deferred_singlestep_trap((caddr_t)rp->r_pc); 649 650 t->t_sig_check = 0; 651 652 /* 653 * The following check is legal for the following reasons: 654 * 1) The thread we are checking, is ourselves, so there is 655 * no way the proc can go away. 656 * 2) The only time we need to be protected by the 657 * lock is if the binding is changed. 658 * 659 * Note we will still take the lock and check the binding 660 * if the condition was true without the lock held. This 661 * prevents lock contention among threads owned by the 662 * same proc. 663 */ 664 665 if (curthread->t_proc_flag & TP_CHANGEBIND) { 666 mutex_enter(&p->p_lock); 667 if (curthread->t_proc_flag & TP_CHANGEBIND) { 668 timer_lwpbind(); 669 curthread->t_proc_flag &= ~TP_CHANGEBIND; 670 } 671 mutex_exit(&p->p_lock); 672 } 673 674 /* 675 * for kaio requests on the special kaio poll queue, 676 * copyout their results to user memory. 677 */ 678 if (p->p_aio) 679 aio_cleanup(0); 680 /* 681 * If this LWP was asked to hold, call holdlwp(), which will 682 * stop. holdlwps() sets this up and calls pokelwps() which 683 * sets the AST flag. 684 * 685 * Also check TP_EXITLWP, since this is used by fresh new LWPs 686 * through lwp_rtt(). That flag is set if the lwp_create(2) 687 * syscall failed after creating the LWP. 688 */ 689 if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP)) 690 holdlwp(); 691 692 /* 693 * All code that sets signals and makes ISSIG_PENDING 694 * evaluate true must set t_sig_check afterwards. 695 */ 696 if (ISSIG_PENDING(t, lwp, p)) { 697 if (issig(FORREAL)) 698 psig(); 699 t->t_sig_check = 1; /* recheck next time */ 700 } 701 702 if (sigprof) { 703 realsigprof(code, error); 704 t->t_sig_check = 1; /* recheck next time */ 705 } 706 707 /* 708 * If a performance counter overflow interrupt was 709 * delivered *during* the syscall, then re-enable the 710 * AST so that we take a trip through trap() to cause 711 * the SIGEMT to be delivered. 712 */ 713 if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) 714 aston(t); 715 716 /* 717 * /proc can't enable/disable the trace bit itself 718 * because that could race with the call gate used by 719 * system calls via "lcall". If that happened, an 720 * invalid EFLAGS would result. prstep()/prnostep() 721 * therefore schedule an AST for the purpose. 722 */ 723 if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) { 724 lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP; 725 rp->r_ps |= PS_T; 726 } 727 if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) { 728 lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP; 729 rp->r_ps &= ~PS_T; 730 } 731 } 732 733 lwp->lwp_errno = 0; /* clear error for next time */ 734 735 #ifndef NPROBE 736 /* Kernel probe */ 737 if (tnf_tracing_active) { 738 TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */, 739 tnf_long, rval1, rval1, 740 tnf_long, rval2, rval2, 741 tnf_long, errno, (long)error); 742 repost = 1; 743 } 744 #endif /* NPROBE */ 745 746 /* 747 * Set state to LWP_USER here so preempt won't give us a kernel 748 * priority if it occurs after this point. Call CL_TRAPRET() to 749 * restore the user-level priority. 750 * 751 * It is important that no locks (other than spinlocks) be entered 752 * after this point before returning to user mode (unless lwp_state 753 * is set back to LWP_SYS). 754 * 755 * XXX Sampled times past this point are charged to the user. 756 */ 757 lwp->lwp_state = LWP_USER; 758 759 if (t->t_trapret) { 760 t->t_trapret = 0; 761 thread_lock(t); 762 CL_TRAPRET(t); 763 thread_unlock(t); 764 } 765 if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ) 766 preempt(); 767 768 lwp->lwp_errno = 0; /* clear error for next time */ 769 770 /* 771 * The thread lock must be held in order to clear sysnum and reset 772 * lwp_ap atomically with respect to other threads in the system that 773 * may be looking at the args via lwp_ap from get_syscall_args(). 774 */ 775 776 thread_lock(t); 777 t->t_sysnum = 0; /* no longer in a system call */ 778 779 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 780 #if defined(_LP64) 781 /* 782 * In case the args were copied to the lwp, reset the 783 * pointer so the next syscall will have the right 784 * lwp_ap pointer. 785 */ 786 lwp->lwp_ap = (long *)&rp->r_rdi; 787 } else { 788 #endif 789 lwp->lwp_ap = NULL; /* reset on every syscall entry */ 790 } 791 thread_unlock(t); 792 793 lwp->lwp_argsaved = 0; 794 795 /* 796 * If there was a continuing reason for post-syscall processing, 797 * set the t_post_sys flag for the next system call. 798 */ 799 if (repost) 800 t->t_post_sys = 1; 801 802 /* 803 * If there is a ustack registered for this lwp, and the stack rlimit 804 * has been altered, read in the ustack. If the saved stack rlimit 805 * matches the bounds of the ustack, update the ustack to reflect 806 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable 807 * stack checking by setting the size to 0. 808 */ 809 if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) { 810 rlim64_t new_size; 811 caddr_t top; 812 stack_t stk; 813 struct rlimit64 rl; 814 815 mutex_enter(&p->p_lock); 816 new_size = p->p_stk_ctl; 817 top = p->p_usrstack; 818 (void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl); 819 mutex_exit(&p->p_lock); 820 821 if (rl.rlim_cur == RLIM64_INFINITY) 822 new_size = 0; 823 824 if (copyin((stack_t *)lwp->lwp_ustack, &stk, 825 sizeof (stack_t)) == 0 && 826 (stk.ss_size == lwp->lwp_old_stk_ctl || 827 stk.ss_size == 0) && 828 stk.ss_sp == top - stk.ss_size) { 829 stk.ss_sp = (void *)((uintptr_t)stk.ss_sp + 830 stk.ss_size - (uintptr_t)new_size); 831 stk.ss_size = new_size; 832 833 (void) copyout(&stk, (stack_t *)lwp->lwp_ustack, 834 sizeof (stack_t)); 835 } 836 837 lwp->lwp_old_stk_ctl = 0; 838 } 839 } 840 841 /* 842 * Called from post_syscall() when a deferred singlestep is to be taken. 843 */ 844 void 845 deferred_singlestep_trap(caddr_t pc) 846 { 847 proc_t *p = ttoproc(curthread); 848 klwp_t *lwp = ttolwp(curthread); 849 pcb_t *pcb = &lwp->lwp_pcb; 850 uint_t fault = 0; 851 k_siginfo_t siginfo; 852 853 bzero(&siginfo, sizeof (siginfo)); 854 855 /* 856 * If both NORMAL_STEP and WATCH_STEP are in 857 * effect, give precedence to WATCH_STEP. 858 * If neither is set, user must have set the 859 * PS_T bit in %efl; treat this as NORMAL_STEP. 860 */ 861 if ((fault = undo_watch_step(&siginfo)) == 0 && 862 ((pcb->pcb_flags & NORMAL_STEP) || 863 !(pcb->pcb_flags & WATCH_STEP))) { 864 siginfo.si_signo = SIGTRAP; 865 siginfo.si_code = TRAP_TRACE; 866 siginfo.si_addr = pc; 867 fault = FLTTRACE; 868 } 869 pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP); 870 871 if (fault) { 872 /* 873 * Remember the fault and fault adddress 874 * for real-time (SIGPROF) profiling. 875 */ 876 lwp->lwp_lastfault = fault; 877 lwp->lwp_lastfaddr = siginfo.si_addr; 878 /* 879 * If a debugger has declared this fault to be an 880 * event of interest, stop the lwp. Otherwise just 881 * deliver the associated signal. 882 */ 883 if (prismember(&p->p_fltmask, fault) && 884 stop_on_fault(fault, &siginfo) == 0) 885 siginfo.si_signo = 0; 886 } 887 888 if (siginfo.si_signo) 889 trapsig(&siginfo, 1); 890 } 891 892 /* 893 * nonexistent system call-- signal lwp (may want to handle it) 894 * flag error if lwp won't see signal immediately 895 */ 896 int64_t 897 nosys() 898 { 899 tsignal(curthread, SIGSYS); 900 return (set_errno(ENOSYS)); 901 } 902 903 /* 904 * Execute a 32-bit system call on behalf of the current thread. 905 */ 906 void 907 dosyscall(void) 908 { 909 /* 910 * Need space on the stack to store syscall arguments. 911 */ 912 long syscall_args[MAXSYSARGS]; 913 struct sysent *se; 914 int64_t ret; 915 916 syscall_mstate(LMS_TRAP, LMS_SYSTEM); 917 918 ASSERT(curproc->p_model == DATAMODEL_ILP32); 919 920 CPU_STATS_ENTER_K(); 921 CPU_STATS_ADDQ(CPU, sys, syscall, 1); 922 CPU_STATS_EXIT_K(); 923 924 se = syscall_entry(curthread, syscall_args); 925 926 /* 927 * syscall_entry() copied all 8 arguments into syscall_args. 928 */ 929 ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2], 930 syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6], 931 syscall_args[7]); 932 933 syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32)); 934 syscall_mstate(LMS_SYSTEM, LMS_TRAP); 935 } 936 937 /* 938 * Get the arguments to the current system call. See comment atop 939 * save_syscall_args() regarding lwp_ap usage. 940 */ 941 942 uint_t 943 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp) 944 { 945 kthread_t *t = lwptot(lwp); 946 ulong_t mask = 0xfffffffful; 947 uint_t code; 948 long *ap; 949 int nargs; 950 951 #if defined(_LP64) 952 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 953 mask = 0xfffffffffffffffful; 954 #endif 955 956 /* 957 * The thread lock must be held while looking at the arguments to ensure 958 * they don't go away via post_syscall(). 959 * get_syscall_args() is the only routine to read them which is callable 960 * outside the LWP in question and hence the only one that must be 961 * synchronized in this manner. 962 */ 963 thread_lock(t); 964 965 code = t->t_sysnum; 966 ap = lwp->lwp_ap; 967 968 thread_unlock(t); 969 970 if (code != 0 && code < NSYSCALL) { 971 nargs = LWP_GETSYSENT(lwp)[code].sy_narg; 972 973 ASSERT(nargs <= MAXSYSARGS); 974 975 *nargsp = nargs; 976 while (nargs-- > 0) 977 *argp++ = *ap++ & mask; 978 } else { 979 *nargsp = 0; 980 } 981 982 return (code); 983 } 984 985 #ifdef _SYSCALL32_IMPL 986 /* 987 * Get the arguments to the current 32-bit system call. 988 */ 989 uint_t 990 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp) 991 { 992 long args[MAXSYSARGS]; 993 uint_t i, code; 994 995 code = get_syscall_args(lwp, args, nargsp); 996 997 for (i = 0; i != *nargsp; i++) 998 *argp++ = (int)args[i]; 999 return (code); 1000 } 1001 #endif 1002 1003 /* 1004 * Save the system call arguments in a safe place. 1005 * 1006 * On the i386 kernel: 1007 * 1008 * Copy the users args prior to changing the stack or stack pointer. 1009 * This is so /proc will be able to get a valid copy of the 1010 * args from the user stack even after the user stack has been changed. 1011 * Note that the kernel stack copy of the args may also have been 1012 * changed by a system call handler which takes C-style arguments. 1013 * 1014 * Note that this may be called by stop() from trap(). In that case 1015 * t_sysnum will be zero (syscall_exit clears it), so no args will be 1016 * copied. 1017 * 1018 * On the amd64 kernel: 1019 * 1020 * For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9 1021 * in the reg structure. If the user is going to change the argument 1022 * registers, rax, or the stack and might want to get the args (for 1023 * /proc tracing), it must copy the args elsewhere via save_syscall_args(). 1024 * 1025 * For 32-bit applications, lwp->lwp_ap normally points to a copy of 1026 * the system call arguments on the kernel stack made from the user 1027 * stack. Copy the args prior to change the stack or stack pointer. 1028 * This is so /proc will be able to get a valid copy of the args 1029 * from the user stack even after that stack has been changed. 1030 * 1031 * This may be called from stop() even when we're not in a system call. 1032 * Since there's no easy way to tell, this must be safe (not panic). 1033 * If the copyins get data faults, return non-zero. 1034 */ 1035 int 1036 save_syscall_args() 1037 { 1038 kthread_t *t = curthread; 1039 klwp_t *lwp = ttolwp(t); 1040 uint_t code = t->t_sysnum; 1041 uint_t nargs; 1042 1043 if (lwp->lwp_argsaved || code == 0) 1044 return (0); /* args already saved or not needed */ 1045 1046 if (code >= NSYSCALL) { 1047 nargs = 0; /* illegal syscall */ 1048 } else { 1049 struct sysent *se = LWP_GETSYSENT(lwp); 1050 struct sysent *callp = se + code; 1051 1052 nargs = callp->sy_narg; 1053 if (LOADABLE_SYSCALL(callp) && nargs == 0) { 1054 krwlock_t *module_lock; 1055 1056 /* 1057 * Find out how many arguments the system 1058 * call uses. 1059 * 1060 * We have the property that loaded syscalls 1061 * never change the number of arguments they 1062 * use after they've been loaded once. This 1063 * allows us to stop for /proc tracing without 1064 * holding the module lock. 1065 * /proc is assured that sy_narg is valid. 1066 */ 1067 module_lock = lock_syscall(se, code); 1068 nargs = callp->sy_narg; 1069 rw_exit(module_lock); 1070 } 1071 } 1072 1073 /* 1074 * Fetch the system call arguments. 1075 */ 1076 if (nargs == 0) 1077 goto out; 1078 1079 ASSERT(nargs <= MAXSYSARGS); 1080 1081 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1082 #if defined(_LP64) 1083 struct regs *rp = lwptoregs(lwp); 1084 1085 lwp->lwp_arg[0] = rp->r_rdi; 1086 lwp->lwp_arg[1] = rp->r_rsi; 1087 lwp->lwp_arg[2] = rp->r_rdx; 1088 lwp->lwp_arg[3] = rp->r_rcx; 1089 lwp->lwp_arg[4] = rp->r_r8; 1090 lwp->lwp_arg[5] = rp->r_r9; 1091 if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6)) 1092 return (-1); 1093 } else { 1094 #endif 1095 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs)) 1096 return (-1); 1097 } 1098 out: 1099 lwp->lwp_ap = lwp->lwp_arg; 1100 lwp->lwp_argsaved = 1; 1101 t->t_post_sys = 1; /* so lwp_ap will be reset */ 1102 return (0); 1103 } 1104 1105 void 1106 reset_syscall_args(void) 1107 { 1108 ttolwp(curthread)->lwp_argsaved = 0; 1109 } 1110 1111 /* 1112 * Call a system call which takes a pointer to the user args struct and 1113 * a pointer to the return values. This is a bit slower than the standard 1114 * C arg-passing method in some cases. 1115 */ 1116 int64_t 1117 syscall_ap(void) 1118 { 1119 uint_t error; 1120 struct sysent *callp; 1121 rval_t rval; 1122 kthread_t *t = curthread; 1123 klwp_t *lwp = ttolwp(t); 1124 struct regs *rp = lwptoregs(lwp); 1125 1126 callp = LWP_GETSYSENT(lwp) + t->t_sysnum; 1127 1128 #if defined(__amd64) 1129 /* 1130 * If the arguments don't fit in registers %rdi-%r9, make sure they 1131 * have been copied to the lwp_arg array. 1132 */ 1133 if (callp->sy_narg > 6 && save_syscall_args()) 1134 return ((int64_t)set_errno(EFAULT)); 1135 #endif 1136 1137 rval.r_val1 = 0; 1138 rval.r_val2 = rp->r_r1; 1139 lwp->lwp_error = 0; /* for old drivers */ 1140 error = (*(callp->sy_call))(lwp->lwp_ap, &rval); 1141 if (error) 1142 return ((longlong_t)set_errno(error)); 1143 return (rval.r_vals); 1144 } 1145 1146 /* 1147 * Load system call module. 1148 * Returns with pointer to held read lock for module. 1149 */ 1150 static krwlock_t * 1151 lock_syscall(struct sysent *table, uint_t code) 1152 { 1153 krwlock_t *module_lock; 1154 struct modctl *modp; 1155 int id; 1156 struct sysent *callp; 1157 1158 callp = table + code; 1159 module_lock = callp->sy_lock; 1160 1161 /* 1162 * Optimization to only call modload if we don't have a loaded 1163 * syscall. 1164 */ 1165 rw_enter(module_lock, RW_READER); 1166 if (LOADED_SYSCALL(callp)) 1167 return (module_lock); 1168 rw_exit(module_lock); 1169 1170 for (;;) { 1171 if ((id = modload("sys", syscallnames[code])) == -1) 1172 break; 1173 1174 /* 1175 * If we loaded successfully at least once, the modctl 1176 * will still be valid, so we try to grab it by filename. 1177 * If this call fails, it's because the mod_filename 1178 * was changed after the call to modload() (mod_hold_by_name() 1179 * is the likely culprit). We can safely just take 1180 * another lap if this is the case; the modload() will 1181 * change the mod_filename back to one by which we can 1182 * find the modctl. 1183 */ 1184 modp = mod_find_by_filename("sys", syscallnames[code]); 1185 1186 if (modp == NULL) 1187 continue; 1188 1189 mutex_enter(&mod_lock); 1190 1191 if (!modp->mod_installed) { 1192 mutex_exit(&mod_lock); 1193 continue; 1194 } 1195 break; 1196 } 1197 rw_enter(module_lock, RW_READER); 1198 1199 if (id != -1) 1200 mutex_exit(&mod_lock); 1201 1202 return (module_lock); 1203 } 1204 1205 /* 1206 * Loadable syscall support. 1207 * If needed, load the module, then reserve it by holding a read 1208 * lock for the duration of the call. 1209 * Later, if the syscall is not unloadable, it could patch the vector. 1210 */ 1211 /*ARGSUSED*/ 1212 int64_t 1213 loadable_syscall( 1214 long a0, long a1, long a2, long a3, 1215 long a4, long a5, long a6, long a7) 1216 { 1217 klwp_t *lwp = ttolwp(curthread); 1218 int64_t rval; 1219 struct sysent *callp; 1220 struct sysent *se = LWP_GETSYSENT(lwp); 1221 krwlock_t *module_lock; 1222 int code, error = 0; 1223 int64_t (*sy_call)(); 1224 1225 code = curthread->t_sysnum; 1226 callp = se + code; 1227 1228 /* 1229 * Try to autoload the system call if necessary 1230 */ 1231 module_lock = lock_syscall(se, code); 1232 THREAD_KPRI_RELEASE(); /* drop priority given by rw_enter */ 1233 1234 /* 1235 * we've locked either the loaded syscall or nosys 1236 */ 1237 1238 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1239 #if defined(_LP64) 1240 if (callp->sy_flags & SE_ARGC) { 1241 sy_call = (int64_t (*)())callp->sy_call; 1242 rval = (*sy_call)(a0, a1, a2, a3, a4, a5); 1243 } else 1244 rval = syscall_ap(); 1245 } else { 1246 #endif 1247 /* 1248 * Now that it's loaded, make sure enough args were copied. 1249 */ 1250 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg)) 1251 error = EFAULT; 1252 if (error) { 1253 rval = set_errno(error); 1254 } else if (callp->sy_flags & SE_ARGC) { 1255 sy_call = (int64_t (*)())callp->sy_call; 1256 rval = (*sy_call)(lwp->lwp_ap[0], lwp->lwp_ap[1], 1257 lwp->lwp_ap[2], lwp->lwp_ap[3], lwp->lwp_ap[4], 1258 lwp->lwp_ap[5]); 1259 } else 1260 rval = syscall_ap(); 1261 } 1262 1263 THREAD_KPRI_REQUEST(); /* regain priority from read lock */ 1264 rw_exit(module_lock); 1265 return (rval); 1266 } 1267 1268 /* 1269 * Indirect syscall handled in libc on x86 architectures 1270 */ 1271 int64_t 1272 indir() 1273 { 1274 return (nosys()); 1275 } 1276 1277 /* 1278 * set_errno - set an error return from the current system call. 1279 * This could be a macro. 1280 * This returns the value it is passed, so that the caller can 1281 * use tail-recursion-elimination and do return (set_errno(ERRNO)); 1282 */ 1283 uint_t 1284 set_errno(uint_t error) 1285 { 1286 ASSERT(error != 0); /* must not be used to clear errno */ 1287 1288 curthread->t_post_sys = 1; /* have post_syscall do error return */ 1289 return (ttolwp(curthread)->lwp_errno = error); 1290 } 1291 1292 /* 1293 * set_proc_pre_sys - Set pre-syscall processing for entire process. 1294 */ 1295 void 1296 set_proc_pre_sys(proc_t *p) 1297 { 1298 kthread_t *t; 1299 kthread_t *first; 1300 1301 ASSERT(MUTEX_HELD(&p->p_lock)); 1302 1303 t = first = p->p_tlist; 1304 do { 1305 t->t_pre_sys = 1; 1306 } while ((t = t->t_forw) != first); 1307 } 1308 1309 /* 1310 * set_proc_post_sys - Set post-syscall processing for entire process. 1311 */ 1312 void 1313 set_proc_post_sys(proc_t *p) 1314 { 1315 kthread_t *t; 1316 kthread_t *first; 1317 1318 ASSERT(MUTEX_HELD(&p->p_lock)); 1319 1320 t = first = p->p_tlist; 1321 do { 1322 t->t_post_sys = 1; 1323 } while ((t = t->t_forw) != first); 1324 } 1325 1326 /* 1327 * set_proc_sys - Set pre- and post-syscall processing for entire process. 1328 */ 1329 void 1330 set_proc_sys(proc_t *p) 1331 { 1332 kthread_t *t; 1333 kthread_t *first; 1334 1335 ASSERT(MUTEX_HELD(&p->p_lock)); 1336 1337 t = first = p->p_tlist; 1338 do { 1339 t->t_pre_sys = 1; 1340 t->t_post_sys = 1; 1341 } while ((t = t->t_forw) != first); 1342 } 1343 1344 /* 1345 * set_all_proc_sys - set pre- and post-syscall processing flags for all 1346 * user processes. 1347 * 1348 * This is needed when auditing, tracing, or other facilities which affect 1349 * all processes are turned on. 1350 */ 1351 void 1352 set_all_proc_sys() 1353 { 1354 kthread_t *t; 1355 kthread_t *first; 1356 1357 mutex_enter(&pidlock); 1358 t = first = curthread; 1359 do { 1360 t->t_pre_sys = 1; 1361 t->t_post_sys = 1; 1362 } while ((t = t->t_next) != first); 1363 mutex_exit(&pidlock); 1364 } 1365 1366 /* 1367 * set_proc_ast - Set asynchronous service trap (AST) flag for all 1368 * threads in process. 1369 */ 1370 void 1371 set_proc_ast(proc_t *p) 1372 { 1373 kthread_t *t; 1374 kthread_t *first; 1375 1376 ASSERT(MUTEX_HELD(&p->p_lock)); 1377 1378 t = first = p->p_tlist; 1379 do { 1380 aston(t); 1381 } while ((t = t->t_forw) != first); 1382 } 1383