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