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