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 2006 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 (code == SYS_vfork && rp->r_r1 == 0 && error == 0) 607 vfwait((pid_t)rval1); 608 609 /* 610 * If profiling is active, bill the current PC in user-land 611 * and keep reposting until profiling is disabled. 612 */ 613 if (p->p_prof.pr_scale) { 614 if (lwp->lwp_oweupc) 615 profil_tick(rp->r_pc); 616 repost = 1; 617 } 618 619 sig_check: 620 /* 621 * Reset flag for next time. 622 * We must do this after stopping on PR_SYSEXIT 623 * because /proc uses the information in lwp_eosys. 624 */ 625 lwp->lwp_eosys = NORMALRETURN; 626 clear_stale_fd(); 627 t->t_flag &= ~T_FORKALL; 628 629 if (t->t_astflag | t->t_sig_check) { 630 /* 631 * Turn off the AST flag before checking all the conditions that 632 * may have caused an AST. This flag is on whenever a signal or 633 * unusual condition should be handled after the next trap or 634 * syscall. 635 */ 636 astoff(t); 637 /* 638 * If a single-step trap occurred on a syscall (see trap()) 639 * recognize it now. Do this before checking for signals 640 * because deferred_singlestep_trap() may generate a SIGTRAP to 641 * the LWP or may otherwise mark the LWP to call issig(FORREAL). 642 */ 643 if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING) 644 deferred_singlestep_trap((caddr_t)rp->r_pc); 645 646 t->t_sig_check = 0; 647 648 /* 649 * The following check is legal for the following reasons: 650 * 1) The thread we are checking, is ourselves, so there is 651 * no way the proc can go away. 652 * 2) The only time we need to be protected by the 653 * lock is if the binding is changed. 654 * 655 * Note we will still take the lock and check the binding 656 * if the condition was true without the lock held. This 657 * prevents lock contention among threads owned by the 658 * same proc. 659 */ 660 661 if (curthread->t_proc_flag & TP_CHANGEBIND) { 662 mutex_enter(&p->p_lock); 663 if (curthread->t_proc_flag & TP_CHANGEBIND) { 664 timer_lwpbind(); 665 curthread->t_proc_flag &= ~TP_CHANGEBIND; 666 } 667 mutex_exit(&p->p_lock); 668 } 669 670 /* 671 * for kaio requests on the special kaio poll queue, 672 * copyout their results to user memory. 673 */ 674 if (p->p_aio) 675 aio_cleanup(0); 676 /* 677 * If this LWP was asked to hold, call holdlwp(), which will 678 * stop. holdlwps() sets this up and calls pokelwps() which 679 * sets the AST flag. 680 * 681 * Also check TP_EXITLWP, since this is used by fresh new LWPs 682 * through lwp_rtt(). That flag is set if the lwp_create(2) 683 * syscall failed after creating the LWP. 684 */ 685 if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP)) 686 holdlwp(); 687 688 /* 689 * All code that sets signals and makes ISSIG_PENDING 690 * evaluate true must set t_sig_check afterwards. 691 */ 692 if (ISSIG_PENDING(t, lwp, p)) { 693 if (issig(FORREAL)) 694 psig(); 695 t->t_sig_check = 1; /* recheck next time */ 696 } 697 698 if (sigprof) { 699 realsigprof(code, error); 700 t->t_sig_check = 1; /* recheck next time */ 701 } 702 703 /* 704 * If a performance counter overflow interrupt was 705 * delivered *during* the syscall, then re-enable the 706 * AST so that we take a trip through trap() to cause 707 * the SIGEMT to be delivered. 708 */ 709 if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW) 710 aston(t); 711 712 /* 713 * /proc can't enable/disable the trace bit itself 714 * because that could race with the call gate used by 715 * system calls via "lcall". If that happened, an 716 * invalid EFLAGS would result. prstep()/prnostep() 717 * therefore schedule an AST for the purpose. 718 */ 719 if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) { 720 lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP; 721 rp->r_ps |= PS_T; 722 } 723 if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) { 724 lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP; 725 rp->r_ps &= ~PS_T; 726 } 727 } 728 729 lwp->lwp_errno = 0; /* clear error for next time */ 730 731 #ifndef NPROBE 732 /* Kernel probe */ 733 if (tnf_tracing_active) { 734 TNF_PROBE_3(syscall_end, "syscall thread", /* CSTYLED */, 735 tnf_long, rval1, rval1, 736 tnf_long, rval2, rval2, 737 tnf_long, errno, (long)error); 738 repost = 1; 739 } 740 #endif /* NPROBE */ 741 742 /* 743 * Set state to LWP_USER here so preempt won't give us a kernel 744 * priority if it occurs after this point. Call CL_TRAPRET() to 745 * restore the user-level priority. 746 * 747 * It is important that no locks (other than spinlocks) be entered 748 * after this point before returning to user mode (unless lwp_state 749 * is set back to LWP_SYS). 750 * 751 * XXX Sampled times past this point are charged to the user. 752 */ 753 lwp->lwp_state = LWP_USER; 754 755 if (t->t_trapret) { 756 t->t_trapret = 0; 757 thread_lock(t); 758 CL_TRAPRET(t); 759 thread_unlock(t); 760 } 761 if (CPU->cpu_runrun) 762 preempt(); 763 764 lwp->lwp_errno = 0; /* clear error for next time */ 765 766 /* 767 * The thread lock must be held in order to clear sysnum and reset 768 * lwp_ap atomically with respect to other threads in the system that 769 * may be looking at the args via lwp_ap from get_syscall_args(). 770 */ 771 772 thread_lock(t); 773 t->t_sysnum = 0; /* no longer in a system call */ 774 775 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 776 #if defined(_LP64) 777 /* 778 * In case the args were copied to the lwp, reset the 779 * pointer so the next syscall will have the right 780 * lwp_ap pointer. 781 */ 782 lwp->lwp_ap = (long *)&rp->r_rdi; 783 } else { 784 #endif 785 lwp->lwp_ap = NULL; /* reset on every syscall entry */ 786 } 787 thread_unlock(t); 788 789 lwp->lwp_argsaved = 0; 790 791 /* 792 * If there was a continuing reason for post-syscall processing, 793 * set the t_post_sys flag for the next system call. 794 */ 795 if (repost) 796 t->t_post_sys = 1; 797 798 /* 799 * If there is a ustack registered for this lwp, and the stack rlimit 800 * has been altered, read in the ustack. If the saved stack rlimit 801 * matches the bounds of the ustack, update the ustack to reflect 802 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable 803 * stack checking by setting the size to 0. 804 */ 805 if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) { 806 rlim64_t new_size; 807 caddr_t top; 808 stack_t stk; 809 struct rlimit64 rl; 810 811 mutex_enter(&p->p_lock); 812 new_size = p->p_stk_ctl; 813 top = p->p_usrstack; 814 (void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl); 815 mutex_exit(&p->p_lock); 816 817 if (rl.rlim_cur == RLIM64_INFINITY) 818 new_size = 0; 819 820 if (copyin((stack_t *)lwp->lwp_ustack, &stk, 821 sizeof (stack_t)) == 0 && 822 (stk.ss_size == lwp->lwp_old_stk_ctl || 823 stk.ss_size == 0) && 824 stk.ss_sp == top - stk.ss_size) { 825 stk.ss_sp = (void *)((uintptr_t)stk.ss_sp + 826 stk.ss_size - (uintptr_t)new_size); 827 stk.ss_size = new_size; 828 829 (void) copyout(&stk, (stack_t *)lwp->lwp_ustack, 830 sizeof (stack_t)); 831 } 832 833 lwp->lwp_old_stk_ctl = 0; 834 } 835 } 836 837 /* 838 * Called from post_syscall() when a deferred singlestep is to be taken. 839 */ 840 void 841 deferred_singlestep_trap(caddr_t pc) 842 { 843 proc_t *p = ttoproc(curthread); 844 klwp_t *lwp = ttolwp(curthread); 845 pcb_t *pcb = &lwp->lwp_pcb; 846 uint_t fault = 0; 847 k_siginfo_t siginfo; 848 849 bzero(&siginfo, sizeof (siginfo)); 850 851 /* 852 * If both NORMAL_STEP and WATCH_STEP are in 853 * effect, give precedence to NORMAL_STEP. 854 * If neither is set, user must have set the 855 * PS_T bit in %efl; treat this as NORMAL_STEP. 856 */ 857 if ((pcb->pcb_flags & NORMAL_STEP) || 858 !(pcb->pcb_flags & WATCH_STEP)) { 859 siginfo.si_signo = SIGTRAP; 860 siginfo.si_code = TRAP_TRACE; 861 siginfo.si_addr = pc; 862 fault = FLTTRACE; 863 if (pcb->pcb_flags & WATCH_STEP) 864 (void) undo_watch_step(NULL); 865 } else { 866 fault = undo_watch_step(&siginfo); 867 } 868 pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP); 869 870 if (fault) { 871 /* 872 * Remember the fault and fault adddress 873 * for real-time (SIGPROF) profiling. 874 */ 875 lwp->lwp_lastfault = fault; 876 lwp->lwp_lastfaddr = siginfo.si_addr; 877 /* 878 * If a debugger has declared this fault to be an 879 * event of interest, stop the lwp. Otherwise just 880 * deliver the associated signal. 881 */ 882 if (prismember(&p->p_fltmask, fault) && 883 stop_on_fault(fault, &siginfo) == 0) 884 siginfo.si_signo = 0; 885 } 886 887 if (siginfo.si_signo) 888 trapsig(&siginfo, 1); 889 } 890 891 /* 892 * nonexistent system call-- signal lwp (may want to handle it) 893 * flag error if lwp won't see signal immediately 894 */ 895 int64_t 896 nosys() 897 { 898 tsignal(curthread, SIGSYS); 899 return (set_errno(ENOSYS)); 900 } 901 902 /* 903 * Execute a 32-bit system call on behalf of the current thread. 904 */ 905 void 906 dosyscall(void) 907 { 908 /* 909 * Need space on the stack to store syscall arguments. 910 */ 911 long syscall_args[MAXSYSARGS]; 912 struct sysent *se; 913 int64_t ret; 914 915 syscall_mstate(LMS_TRAP, LMS_SYSTEM); 916 917 ASSERT(curproc->p_model == DATAMODEL_ILP32); 918 919 CPU_STATS_ENTER_K(); 920 CPU_STATS_ADDQ(CPU, sys, syscall, 1); 921 CPU_STATS_EXIT_K(); 922 923 se = syscall_entry(curthread, syscall_args); 924 925 /* 926 * syscall_entry() copied all 8 arguments into syscall_args. 927 */ 928 ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2], 929 syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6], 930 syscall_args[7]); 931 932 syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32)); 933 syscall_mstate(LMS_SYSTEM, LMS_TRAP); 934 } 935 936 /* 937 * Get the arguments to the current system call. See comment atop 938 * save_syscall_args() regarding lwp_ap usage. 939 */ 940 941 uint_t 942 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp) 943 { 944 kthread_t *t = lwptot(lwp); 945 ulong_t mask = 0xfffffffful; 946 uint_t code; 947 long *ap; 948 int nargs; 949 950 #if defined(_LP64) 951 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 952 mask = 0xfffffffffffffffful; 953 #endif 954 955 /* 956 * The thread lock must be held while looking at the arguments to ensure 957 * they don't go away via post_syscall(). 958 * get_syscall_args() is the only routine to read them which is callable 959 * outside the LWP in question and hence the only one that must be 960 * synchronized in this manner. 961 */ 962 thread_lock(t); 963 964 code = t->t_sysnum; 965 ap = lwp->lwp_ap; 966 967 thread_unlock(t); 968 969 if (code != 0 && code < NSYSCALL) { 970 nargs = LWP_GETSYSENT(lwp)[code].sy_narg; 971 972 ASSERT(nargs <= MAXSYSARGS); 973 974 *nargsp = nargs; 975 while (nargs-- > 0) 976 *argp++ = *ap++ & mask; 977 } else { 978 *nargsp = 0; 979 } 980 981 return (code); 982 } 983 984 #ifdef _SYSCALL32_IMPL 985 /* 986 * Get the arguments to the current 32-bit system call. 987 */ 988 uint_t 989 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp) 990 { 991 long args[MAXSYSARGS]; 992 uint_t i, code; 993 994 code = get_syscall_args(lwp, args, nargsp); 995 996 for (i = 0; i != *nargsp; i++) 997 *argp++ = (int)args[i]; 998 return (code); 999 } 1000 #endif 1001 1002 /* 1003 * Save the system call arguments in a safe place. 1004 * 1005 * On the i386 kernel: 1006 * 1007 * Copy the users args prior to changing the stack or stack pointer. 1008 * This is so /proc will be able to get a valid copy of the 1009 * args from the user stack even after the user stack has been changed. 1010 * Note that the kernel stack copy of the args may also have been 1011 * changed by a system call handler which takes C-style arguments. 1012 * 1013 * Note that this may be called by stop() from trap(). In that case 1014 * t_sysnum will be zero (syscall_exit clears it), so no args will be 1015 * copied. 1016 * 1017 * On the amd64 kernel: 1018 * 1019 * For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9 1020 * in the reg structure. If the user is going to change the argument 1021 * registers, rax, or the stack and might want to get the args (for 1022 * /proc tracing), it must copy the args elsewhere via save_syscall_args(). 1023 * 1024 * For 32-bit applications, lwp->lwp_ap normally points to a copy of 1025 * the system call arguments on the kernel stack made from the user 1026 * stack. Copy the args prior to change the stack or stack pointer. 1027 * This is so /proc will be able to get a valid copy of the args 1028 * from the user stack even after that stack has been changed. 1029 * 1030 * This may be called from stop() even when we're not in a system call. 1031 * Since there's no easy way to tell, this must be safe (not panic). 1032 * If the copyins get data faults, return non-zero. 1033 */ 1034 int 1035 save_syscall_args() 1036 { 1037 kthread_t *t = curthread; 1038 klwp_t *lwp = ttolwp(t); 1039 uint_t code = t->t_sysnum; 1040 uint_t nargs; 1041 1042 if (lwp->lwp_argsaved || code == 0) 1043 return (0); /* args already saved or not needed */ 1044 1045 if (code >= NSYSCALL) { 1046 nargs = 0; /* illegal syscall */ 1047 } else { 1048 struct sysent *se = LWP_GETSYSENT(lwp); 1049 struct sysent *callp = se + code; 1050 1051 nargs = callp->sy_narg; 1052 if (LOADABLE_SYSCALL(callp) && nargs == 0) { 1053 krwlock_t *module_lock; 1054 1055 /* 1056 * Find out how many arguments the system 1057 * call uses. 1058 * 1059 * We have the property that loaded syscalls 1060 * never change the number of arguments they 1061 * use after they've been loaded once. This 1062 * allows us to stop for /proc tracing without 1063 * holding the module lock. 1064 * /proc is assured that sy_narg is valid. 1065 */ 1066 module_lock = lock_syscall(se, code); 1067 nargs = callp->sy_narg; 1068 rw_exit(module_lock); 1069 } 1070 } 1071 1072 /* 1073 * Fetch the system call arguments. 1074 */ 1075 if (nargs == 0) 1076 goto out; 1077 1078 ASSERT(nargs <= MAXSYSARGS); 1079 1080 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1081 #if defined(_LP64) 1082 struct regs *rp = lwptoregs(lwp); 1083 1084 lwp->lwp_arg[0] = rp->r_rdi; 1085 lwp->lwp_arg[1] = rp->r_rsi; 1086 lwp->lwp_arg[2] = rp->r_rdx; 1087 lwp->lwp_arg[3] = rp->r_rcx; 1088 lwp->lwp_arg[4] = rp->r_r8; 1089 lwp->lwp_arg[5] = rp->r_r9; 1090 if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6)) 1091 return (-1); 1092 } else { 1093 #endif 1094 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs)) 1095 return (-1); 1096 } 1097 out: 1098 lwp->lwp_ap = lwp->lwp_arg; 1099 lwp->lwp_argsaved = 1; 1100 t->t_post_sys = 1; /* so lwp_ap will be reset */ 1101 return (0); 1102 } 1103 1104 void 1105 reset_syscall_args(void) 1106 { 1107 ttolwp(curthread)->lwp_argsaved = 0; 1108 } 1109 1110 /* 1111 * Call a system call which takes a pointer to the user args struct and 1112 * a pointer to the return values. This is a bit slower than the standard 1113 * C arg-passing method in some cases. 1114 */ 1115 int64_t 1116 syscall_ap(void) 1117 { 1118 uint_t error; 1119 struct sysent *callp; 1120 rval_t rval; 1121 kthread_t *t = curthread; 1122 klwp_t *lwp = ttolwp(t); 1123 struct regs *rp = lwptoregs(lwp); 1124 1125 callp = LWP_GETSYSENT(lwp) + t->t_sysnum; 1126 1127 #if defined(__amd64) 1128 /* 1129 * If the arguments don't fit in registers %rdi-%r9, make sure they 1130 * have been copied to the lwp_arg array. 1131 */ 1132 if (callp->sy_narg > 6 && save_syscall_args()) 1133 return ((int64_t)set_errno(EFAULT)); 1134 #endif 1135 1136 rval.r_val1 = 0; 1137 rval.r_val2 = rp->r_r1; 1138 lwp->lwp_error = 0; /* for old drivers */ 1139 error = (*(callp->sy_call))(lwp->lwp_ap, &rval); 1140 if (error) 1141 return ((longlong_t)set_errno(error)); 1142 return (rval.r_vals); 1143 } 1144 1145 /* 1146 * Load system call module. 1147 * Returns with pointer to held read lock for module. 1148 */ 1149 static krwlock_t * 1150 lock_syscall(struct sysent *table, uint_t code) 1151 { 1152 krwlock_t *module_lock; 1153 struct modctl *modp; 1154 int id; 1155 struct sysent *callp; 1156 1157 callp = table + code; 1158 module_lock = callp->sy_lock; 1159 1160 /* 1161 * Optimization to only call modload if we don't have a loaded 1162 * syscall. 1163 */ 1164 rw_enter(module_lock, RW_READER); 1165 if (LOADED_SYSCALL(callp)) 1166 return (module_lock); 1167 rw_exit(module_lock); 1168 1169 for (;;) { 1170 if ((id = modload("sys", syscallnames[code])) == -1) 1171 break; 1172 1173 /* 1174 * If we loaded successfully at least once, the modctl 1175 * will still be valid, so we try to grab it by filename. 1176 * If this call fails, it's because the mod_filename 1177 * was changed after the call to modload() (mod_hold_by_name() 1178 * is the likely culprit). We can safely just take 1179 * another lap if this is the case; the modload() will 1180 * change the mod_filename back to one by which we can 1181 * find the modctl. 1182 */ 1183 modp = mod_find_by_filename("sys", syscallnames[code]); 1184 1185 if (modp == NULL) 1186 continue; 1187 1188 mutex_enter(&mod_lock); 1189 1190 if (!modp->mod_installed) { 1191 mutex_exit(&mod_lock); 1192 continue; 1193 } 1194 break; 1195 } 1196 rw_enter(module_lock, RW_READER); 1197 1198 if (id != -1) 1199 mutex_exit(&mod_lock); 1200 1201 return (module_lock); 1202 } 1203 1204 /* 1205 * Loadable syscall support. 1206 * If needed, load the module, then reserve it by holding a read 1207 * lock for the duration of the call. 1208 * Later, if the syscall is not unloadable, it could patch the vector. 1209 */ 1210 /*ARGSUSED*/ 1211 int64_t 1212 loadable_syscall( 1213 long a0, long a1, long a2, long a3, 1214 long a4, long a5, long a6, long a7) 1215 { 1216 klwp_t *lwp = ttolwp(curthread); 1217 int64_t rval; 1218 struct sysent *callp; 1219 struct sysent *se = LWP_GETSYSENT(lwp); 1220 krwlock_t *module_lock; 1221 int code, error = 0; 1222 int64_t (*sy_call)(); 1223 1224 code = curthread->t_sysnum; 1225 callp = se + code; 1226 1227 /* 1228 * Try to autoload the system call if necessary 1229 */ 1230 module_lock = lock_syscall(se, code); 1231 THREAD_KPRI_RELEASE(); /* drop priority given by rw_enter */ 1232 1233 /* 1234 * we've locked either the loaded syscall or nosys 1235 */ 1236 1237 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1238 #if defined(_LP64) 1239 if (callp->sy_flags & SE_ARGC) { 1240 sy_call = (int64_t (*)())callp->sy_call; 1241 rval = (*sy_call)(a0, a1, a2, a3, a4, a5); 1242 } else 1243 rval = syscall_ap(); 1244 } else { 1245 #endif 1246 /* 1247 * Now that it's loaded, make sure enough args were copied. 1248 */ 1249 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg)) 1250 error = EFAULT; 1251 if (error) { 1252 rval = set_errno(error); 1253 } else if (callp->sy_flags & SE_ARGC) { 1254 sy_call = (int64_t (*)())callp->sy_call; 1255 rval = (*sy_call)(lwp->lwp_ap[0], lwp->lwp_ap[1], 1256 lwp->lwp_ap[2], lwp->lwp_ap[3], lwp->lwp_ap[4], 1257 lwp->lwp_ap[5]); 1258 } else 1259 rval = syscall_ap(); 1260 } 1261 1262 THREAD_KPRI_REQUEST(); /* regain priority from read lock */ 1263 rw_exit(module_lock); 1264 return (rval); 1265 } 1266 1267 /* 1268 * Indirect syscall handled in libc on x86 architectures 1269 */ 1270 int64_t 1271 indir() 1272 { 1273 return (nosys()); 1274 } 1275 1276 /* 1277 * set_errno - set an error return from the current system call. 1278 * This could be a macro. 1279 * This returns the value it is passed, so that the caller can 1280 * use tail-recursion-elimination and do return (set_errno(ERRNO)); 1281 */ 1282 uint_t 1283 set_errno(uint_t error) 1284 { 1285 ASSERT(error != 0); /* must not be used to clear errno */ 1286 1287 curthread->t_post_sys = 1; /* have post_syscall do error return */ 1288 return (ttolwp(curthread)->lwp_errno = error); 1289 } 1290 1291 /* 1292 * set_proc_pre_sys - Set pre-syscall processing for entire process. 1293 */ 1294 void 1295 set_proc_pre_sys(proc_t *p) 1296 { 1297 kthread_t *t; 1298 kthread_t *first; 1299 1300 ASSERT(MUTEX_HELD(&p->p_lock)); 1301 1302 t = first = p->p_tlist; 1303 do { 1304 t->t_pre_sys = 1; 1305 } while ((t = t->t_forw) != first); 1306 } 1307 1308 /* 1309 * set_proc_post_sys - Set post-syscall processing for entire process. 1310 */ 1311 void 1312 set_proc_post_sys(proc_t *p) 1313 { 1314 kthread_t *t; 1315 kthread_t *first; 1316 1317 ASSERT(MUTEX_HELD(&p->p_lock)); 1318 1319 t = first = p->p_tlist; 1320 do { 1321 t->t_post_sys = 1; 1322 } while ((t = t->t_forw) != first); 1323 } 1324 1325 /* 1326 * set_proc_sys - Set pre- and post-syscall processing for entire process. 1327 */ 1328 void 1329 set_proc_sys(proc_t *p) 1330 { 1331 kthread_t *t; 1332 kthread_t *first; 1333 1334 ASSERT(MUTEX_HELD(&p->p_lock)); 1335 1336 t = first = p->p_tlist; 1337 do { 1338 t->t_pre_sys = 1; 1339 t->t_post_sys = 1; 1340 } while ((t = t->t_forw) != first); 1341 } 1342 1343 /* 1344 * set_all_proc_sys - set pre- and post-syscall processing flags for all 1345 * user processes. 1346 * 1347 * This is needed when auditing, tracing, or other facilities which affect 1348 * all processes are turned on. 1349 */ 1350 void 1351 set_all_proc_sys() 1352 { 1353 kthread_t *t; 1354 kthread_t *first; 1355 1356 mutex_enter(&pidlock); 1357 t = first = curthread; 1358 do { 1359 t->t_pre_sys = 1; 1360 t->t_post_sys = 1; 1361 } while ((t = t->t_next) != first); 1362 mutex_exit(&pidlock); 1363 } 1364 1365 /* 1366 * set_proc_ast - Set asynchronous service trap (AST) flag for all 1367 * threads in process. 1368 */ 1369 void 1370 set_proc_ast(proc_t *p) 1371 { 1372 kthread_t *t; 1373 kthread_t *first; 1374 1375 ASSERT(MUTEX_HELD(&p->p_lock)); 1376 1377 t = first = p->p_tlist; 1378 do { 1379 aston(t); 1380 } while ((t = t->t_forw) != first); 1381 } 1382