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 static 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 } 479 #ifdef C2_AUDIT 480 if (audit_active) { /* put out audit record for this syscall */ 481 rval_t rval; 482 483 /* XX64 -- truncation of 64-bit return values? */ 484 rval.r_val1 = (int)rval1; 485 rval.r_val2 = (int)rval2; 486 audit_finish(T_SYSCALL, code, error, &rval); 487 repost = 1; 488 } 489 #endif /* C2_AUDIT */ 490 491 if (curthread->t_pdmsg != NULL) { 492 char *m = curthread->t_pdmsg; 493 494 uprintf("%s", m); 495 kmem_free(m, strlen(m) + 1); 496 curthread->t_pdmsg = NULL; 497 } 498 499 /* 500 * If we're going to stop for /proc tracing, set the flag and 501 * save the arguments so that the return values don't smash them. 502 */ 503 if (PTOU(p)->u_systrap) { 504 if (prismember(&PTOU(p)->u_exitmask, code)) { 505 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 506 (void) save_syscall_args(); 507 proc_stop = 1; 508 } 509 repost = 1; 510 } 511 512 /* 513 * Similarly check to see if SIGPROF might be sent. 514 */ 515 if (curthread->t_rprof != NULL && 516 curthread->t_rprof->rp_anystate != 0) { 517 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 518 (void) save_syscall_args(); 519 sigprof = 1; 520 } 521 522 if (lwp->lwp_eosys == NORMALRETURN) { 523 if (error == 0) { 524 #ifdef SYSCALLTRACE 525 if (syscalltrace) { 526 mutex_enter(&systrace_lock); 527 printf( 528 "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n", 529 p->p_pid, rval1, rval2, curthread); 530 mutex_exit(&systrace_lock); 531 } 532 #endif /* SYSCALLTRACE */ 533 rp->r_ps &= ~PS_C; 534 rp->r_r0 = rval1; 535 rp->r_r1 = rval2; 536 } else { 537 int sig; 538 #ifdef SYSCALLTRACE 539 if (syscalltrace) { 540 mutex_enter(&systrace_lock); 541 printf("%d: error=%d, id 0x%p\n", 542 p->p_pid, error, curthread); 543 mutex_exit(&systrace_lock); 544 } 545 #endif /* SYSCALLTRACE */ 546 if (error == EINTR && t->t_activefd.a_stale) 547 error = EBADF; 548 if (error == EINTR && 549 (sig = lwp->lwp_cursig) != 0 && 550 sigismember(&PTOU(p)->u_sigrestart, sig) && 551 PTOU(p)->u_signal[sig - 1] != SIG_DFL && 552 PTOU(p)->u_signal[sig - 1] != SIG_IGN) 553 error = ERESTART; 554 rp->r_r0 = error; 555 rp->r_ps |= PS_C; 556 } 557 } 558 559 /* 560 * From the proc(4) manual page: 561 * When exit from a system call is being traced, the traced process 562 * stops on completion of the system call just prior to checking for 563 * signals and returning to user level. At this point all return 564 * values have been stored into the traced process's saved registers. 565 */ 566 if (proc_stop) { 567 mutex_enter(&p->p_lock); 568 if (PTOU(p)->u_systrap && 569 prismember(&PTOU(p)->u_exitmask, code)) 570 stop(PR_SYSEXIT, code); 571 mutex_exit(&p->p_lock); 572 } 573 574 /* 575 * If we are the parent returning from a successful 576 * vfork, wait for the child to exec or exit. 577 * This code must be here and not in the bowels of the system 578 * so that /proc can intercept exit from vfork in a timely way. 579 */ 580 if (code == SYS_vfork && rp->r_r1 == 0 && error == 0) 581 vfwait((pid_t)rval1); 582 583 /* 584 * If profiling is active, bill the current PC in user-land 585 * and keep reposting until profiling is disabled. 586 */ 587 if (p->p_prof.pr_scale) { 588 if (lwp->lwp_oweupc) 589 profil_tick(rp->r_pc); 590 repost = 1; 591 } 592 593 sig_check: 594 /* 595 * Reset flag for next time. 596 * We must do this after stopping on PR_SYSEXIT 597 * because /proc uses the information in lwp_eosys. 598 */ 599 lwp->lwp_eosys = NORMALRETURN; 600 clear_stale_fd(); 601 t->t_flag &= ~T_FORKALL; 602 603 /* 604 * If a single-step trap occurred on a syscall (see trap()) 605 * recognize it now. Do this before checking for signals 606 * because deferred_singlestep_trap() may generate a SIGTRAP to 607 * the LWP or may otherwise mark the LWP to call issig(FORREAL). 608 */ 609 if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING) 610 deferred_singlestep_trap((caddr_t)rp->r_pc); 611 612 if (t->t_astflag | t->t_sig_check) { 613 /* 614 * Turn off the AST flag before checking all the conditions that 615 * may have caused an AST. This flag is on whenever a signal or 616 * unusual condition should be handled after the next trap or 617 * syscall. 618 */ 619 astoff(t); 620 t->t_sig_check = 0; 621 622 mutex_enter(&p->p_lock); 623 if (curthread->t_proc_flag & TP_CHANGEBIND) { 624 timer_lwpbind(); 625 curthread->t_proc_flag &= ~TP_CHANGEBIND; 626 } 627 mutex_exit(&p->p_lock); 628 629 /* 630 * for kaio requests on the special kaio poll queue, 631 * copyout their results to user memory. 632 */ 633 if (p->p_aio) 634 aio_cleanup(0); 635 /* 636 * If this LWP was asked to hold, call holdlwp(), which will 637 * stop. holdlwps() sets this up and calls pokelwps() which 638 * sets the AST flag. 639 * 640 * Also check TP_EXITLWP, since this is used by fresh new LWPs 641 * through lwp_rtt(). That flag is set if the lwp_create(2) 642 * syscall failed after creating the LWP. 643 */ 644 if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP)) 645 holdlwp(); 646 647 /* 648 * All code that sets signals and makes ISSIG_PENDING 649 * evaluate true must set t_sig_check afterwards. 650 */ 651 if (ISSIG_PENDING(t, lwp, p)) { 652 if (issig(FORREAL)) 653 psig(); 654 t->t_sig_check = 1; /* recheck next time */ 655 } 656 657 if (sigprof) { 658 realsigprof(code, 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) 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 static 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 NORMAL_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 ((pcb->pcb_flags & NORMAL_STEP) || 817 !(pcb->pcb_flags & WATCH_STEP)) { 818 siginfo.si_signo = SIGTRAP; 819 siginfo.si_code = TRAP_TRACE; 820 siginfo.si_addr = pc; 821 fault = FLTTRACE; 822 if (pcb->pcb_flags & WATCH_STEP) 823 (void) undo_watch_step(NULL); 824 } else { 825 fault = undo_watch_step(&siginfo); 826 } 827 pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP); 828 829 if (fault) { 830 /* 831 * Remember the fault and fault adddress 832 * for real-time (SIGPROF) profiling. 833 */ 834 lwp->lwp_lastfault = fault; 835 lwp->lwp_lastfaddr = siginfo.si_addr; 836 /* 837 * If a debugger has declared this fault to be an 838 * event of interest, stop the lwp. Otherwise just 839 * deliver the associated signal. 840 */ 841 if (prismember(&p->p_fltmask, fault) && 842 stop_on_fault(fault, &siginfo) == 0) 843 siginfo.si_signo = 0; 844 } 845 846 if (siginfo.si_signo) 847 trapsig(&siginfo, 1); 848 } 849 850 /* 851 * nonexistent system call-- signal lwp (may want to handle it) 852 * flag error if lwp won't see signal immediately 853 */ 854 int64_t 855 nosys() 856 { 857 tsignal(curthread, SIGSYS); 858 return (set_errno(ENOSYS)); 859 } 860 861 /* 862 * Execute a 32-bit system call on behalf of the current thread. 863 */ 864 void 865 dosyscall(void) 866 { 867 /* 868 * Need space on the stack to store syscall arguments. 869 */ 870 long syscall_args[MAXSYSARGS]; 871 struct sysent *se; 872 int64_t ret; 873 874 syscall_mstate(LMS_TRAP, LMS_SYSTEM); 875 876 ASSERT(curproc->p_model == DATAMODEL_ILP32); 877 878 CPU_STATS_ENTER_K(); 879 CPU_STATS_ADDQ(CPU, sys, syscall, 1); 880 CPU_STATS_EXIT_K(); 881 882 se = syscall_entry(curthread, syscall_args); 883 884 /* 885 * syscall_entry() copied all 8 arguments into syscall_args. 886 */ 887 ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2], 888 syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6], 889 syscall_args[7]); 890 891 syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32)); 892 syscall_mstate(LMS_SYSTEM, LMS_TRAP); 893 } 894 895 /* 896 * Get the arguments to the current system call. See comment atop 897 * save_syscall_args() regarding lwp_ap usage. 898 */ 899 900 uint_t 901 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp) 902 { 903 kthread_t *t = lwptot(lwp); 904 ulong_t mask = 0xfffffffful; 905 uint_t code; 906 long *ap; 907 int nargs; 908 909 #if defined(_LP64) 910 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64) 911 mask = 0xfffffffffffffffful; 912 #endif 913 914 /* 915 * The thread lock must be held while looking at the arguments to ensure 916 * they don't go away via post_syscall(). 917 * get_syscall_args() is the only routine to read them which is callable 918 * outside the LWP in question and hence the only one that must be 919 * synchronized in this manner. 920 */ 921 thread_lock(t); 922 923 code = t->t_sysnum; 924 ap = lwp->lwp_ap; 925 926 thread_unlock(t); 927 928 if (code != 0 && code < NSYSCALL) { 929 nargs = LWP_GETSYSENT(lwp)[code].sy_narg; 930 931 ASSERT(nargs <= MAXSYSARGS); 932 933 *nargsp = nargs; 934 while (nargs-- > 0) 935 *argp++ = *ap++ & mask; 936 } else { 937 *nargsp = 0; 938 } 939 940 return (code); 941 } 942 943 #ifdef _SYSCALL32_IMPL 944 /* 945 * Get the arguments to the current 32-bit system call. 946 */ 947 uint_t 948 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp) 949 { 950 long args[MAXSYSARGS]; 951 uint_t i, code; 952 953 code = get_syscall_args(lwp, args, nargsp); 954 955 for (i = 0; i != *nargsp; i++) 956 *argp++ = (int)args[i]; 957 return (code); 958 } 959 #endif 960 961 /* 962 * Save the system call arguments in a safe place. 963 * 964 * On the i386 kernel: 965 * 966 * Copy the users args prior to changing the stack or stack pointer. 967 * This is so /proc will be able to get a valid copy of the 968 * args from the user stack even after the user stack has been changed. 969 * Note that the kernel stack copy of the args may also have been 970 * changed by a system call handler which takes C-style arguments. 971 * 972 * Note that this may be called by stop() from trap(). In that case 973 * t_sysnum will be zero (syscall_exit clears it), so no args will be 974 * copied. 975 * 976 * On the amd64 kernel: 977 * 978 * For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9 979 * in the reg structure. If the user is going to change the argument 980 * registers, rax, or the stack and might want to get the args (for 981 * /proc tracing), it must copy the args elsewhere via save_syscall_args(). 982 * 983 * For 32-bit applications, lwp->lwp_ap normally points to a copy of 984 * the system call arguments on the kernel stack made from the user 985 * stack. Copy the args prior to change the stack or stack pointer. 986 * This is so /proc will be able to get a valid copy of the args 987 * from the user stack even after that stack has been changed. 988 * 989 * This may be called from stop() even when we're not in a system call. 990 * Since there's no easy way to tell, this must be safe (not panic). 991 * If the copyins get data faults, return non-zero. 992 */ 993 int 994 save_syscall_args() 995 { 996 kthread_t *t = curthread; 997 klwp_t *lwp = ttolwp(t); 998 uint_t code = t->t_sysnum; 999 uint_t nargs; 1000 1001 if (lwp->lwp_argsaved || code == 0) 1002 return (0); /* args already saved or not needed */ 1003 1004 if (code >= NSYSCALL) { 1005 nargs = 0; /* illegal syscall */ 1006 } else { 1007 struct sysent *se = LWP_GETSYSENT(lwp); 1008 struct sysent *callp = se + code; 1009 1010 nargs = callp->sy_narg; 1011 if (LOADABLE_SYSCALL(callp) && nargs == 0) { 1012 krwlock_t *module_lock; 1013 1014 /* 1015 * Find out how many arguments the system 1016 * call uses. 1017 * 1018 * We have the property that loaded syscalls 1019 * never change the number of arguments they 1020 * use after they've been loaded once. This 1021 * allows us to stop for /proc tracing without 1022 * holding the module lock. 1023 * /proc is assured that sy_narg is valid. 1024 */ 1025 module_lock = lock_syscall(se, code); 1026 nargs = callp->sy_narg; 1027 rw_exit(module_lock); 1028 } 1029 } 1030 1031 /* 1032 * Fetch the system call arguments. 1033 */ 1034 if (nargs == 0) 1035 goto out; 1036 1037 ASSERT(nargs <= MAXSYSARGS); 1038 1039 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1040 #if defined(_LP64) 1041 struct regs *rp = lwptoregs(lwp); 1042 1043 lwp->lwp_arg[0] = rp->r_rdi; 1044 lwp->lwp_arg[1] = rp->r_rsi; 1045 lwp->lwp_arg[2] = rp->r_rdx; 1046 lwp->lwp_arg[3] = rp->r_rcx; 1047 lwp->lwp_arg[4] = rp->r_r8; 1048 lwp->lwp_arg[5] = rp->r_r9; 1049 if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6)) 1050 return (-1); 1051 } else { 1052 #endif 1053 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs)) 1054 return (-1); 1055 } 1056 out: 1057 lwp->lwp_ap = lwp->lwp_arg; 1058 lwp->lwp_argsaved = 1; 1059 t->t_post_sys = 1; /* so lwp_ap will be reset */ 1060 return (0); 1061 } 1062 1063 void 1064 reset_syscall_args(void) 1065 { 1066 ttolwp(curthread)->lwp_argsaved = 0; 1067 } 1068 1069 /* 1070 * Call a system call which takes a pointer to the user args struct and 1071 * a pointer to the return values. This is a bit slower than the standard 1072 * C arg-passing method in some cases. 1073 */ 1074 int64_t 1075 syscall_ap(void) 1076 { 1077 uint_t error; 1078 struct sysent *callp; 1079 rval_t rval; 1080 kthread_t *t = curthread; 1081 klwp_t *lwp = ttolwp(t); 1082 struct regs *rp = lwptoregs(lwp); 1083 1084 callp = LWP_GETSYSENT(lwp) + t->t_sysnum; 1085 1086 #if defined(__amd64) 1087 /* 1088 * If the arguments don't fit in registers %rdi-%r9, make sure they 1089 * have been copied to the lwp_arg array. 1090 */ 1091 if (callp->sy_narg > 6 && save_syscall_args()) 1092 return ((int64_t)set_errno(EFAULT)); 1093 #endif 1094 1095 rval.r_val1 = 0; 1096 rval.r_val2 = rp->r_r1; 1097 lwp->lwp_error = 0; /* for old drivers */ 1098 error = (*(callp->sy_call))(lwp->lwp_ap, &rval); 1099 if (error) 1100 return ((longlong_t)set_errno(error)); 1101 return (rval.r_vals); 1102 } 1103 1104 /* 1105 * Load system call module. 1106 * Returns with pointer to held read lock for module. 1107 */ 1108 static krwlock_t * 1109 lock_syscall(struct sysent *table, uint_t code) 1110 { 1111 krwlock_t *module_lock; 1112 struct modctl *modp; 1113 int id; 1114 struct sysent *callp; 1115 1116 callp = table + code; 1117 module_lock = callp->sy_lock; 1118 1119 /* 1120 * Optimization to only call modload if we don't have a loaded 1121 * syscall. 1122 */ 1123 rw_enter(module_lock, RW_READER); 1124 if (LOADED_SYSCALL(callp)) 1125 return (module_lock); 1126 rw_exit(module_lock); 1127 1128 for (;;) { 1129 if ((id = modload("sys", syscallnames[code])) == -1) 1130 break; 1131 1132 /* 1133 * If we loaded successfully at least once, the modctl 1134 * will still be valid, so we try to grab it by filename. 1135 * If this call fails, it's because the mod_filename 1136 * was changed after the call to modload() (mod_hold_by_name() 1137 * is the likely culprit). We can safely just take 1138 * another lap if this is the case; the modload() will 1139 * change the mod_filename back to one by which we can 1140 * find the modctl. 1141 */ 1142 modp = mod_find_by_filename("sys", syscallnames[code]); 1143 1144 if (modp == NULL) 1145 continue; 1146 1147 mutex_enter(&mod_lock); 1148 1149 if (!modp->mod_installed) { 1150 mutex_exit(&mod_lock); 1151 continue; 1152 } 1153 break; 1154 } 1155 rw_enter(module_lock, RW_READER); 1156 1157 if (id != -1) 1158 mutex_exit(&mod_lock); 1159 1160 return (module_lock); 1161 } 1162 1163 /* 1164 * Loadable syscall support. 1165 * If needed, load the module, then reserve it by holding a read 1166 * lock for the duration of the call. 1167 * Later, if the syscall is not unloadable, it could patch the vector. 1168 */ 1169 /*ARGSUSED*/ 1170 int64_t 1171 loadable_syscall( 1172 long a0, long a1, long a2, long a3, 1173 long a4, long a5, long a6, long a7) 1174 { 1175 klwp_t *lwp = ttolwp(curthread); 1176 int64_t rval; 1177 struct sysent *callp; 1178 struct sysent *se = LWP_GETSYSENT(lwp); 1179 krwlock_t *module_lock; 1180 int code, error = 0; 1181 int64_t (*sy_call)(); 1182 1183 code = curthread->t_sysnum; 1184 callp = se + code; 1185 1186 /* 1187 * Try to autoload the system call if necessary 1188 */ 1189 module_lock = lock_syscall(se, code); 1190 THREAD_KPRI_RELEASE(); /* drop priority given by rw_enter */ 1191 1192 /* 1193 * we've locked either the loaded syscall or nosys 1194 */ 1195 1196 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) { 1197 #if defined(_LP64) 1198 if (callp->sy_flags & SE_ARGC) { 1199 sy_call = (int64_t (*)())callp->sy_call; 1200 rval = (*sy_call)(a0, a1, a2, a3, a4, a5); 1201 } else 1202 rval = syscall_ap(); 1203 } else { 1204 #endif 1205 /* 1206 * Now that it's loaded, make sure enough args were copied. 1207 */ 1208 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg)) 1209 error = EFAULT; 1210 if (error) { 1211 rval = set_errno(error); 1212 } else if (callp->sy_flags & SE_ARGC) { 1213 sy_call = (int64_t (*)())callp->sy_call; 1214 rval = (*sy_call)(lwp->lwp_ap[0], lwp->lwp_ap[1], 1215 lwp->lwp_ap[2], lwp->lwp_ap[3], lwp->lwp_ap[4], 1216 lwp->lwp_ap[5]); 1217 } else 1218 rval = syscall_ap(); 1219 } 1220 1221 THREAD_KPRI_REQUEST(); /* regain priority from read lock */ 1222 rw_exit(module_lock); 1223 return (rval); 1224 } 1225 1226 /* 1227 * Indirect syscall handled in libc on x86 architectures 1228 */ 1229 int64_t 1230 indir() 1231 { 1232 return (nosys()); 1233 } 1234 1235 /* 1236 * set_errno - set an error return from the current system call. 1237 * This could be a macro. 1238 * This returns the value it is passed, so that the caller can 1239 * use tail-recursion-elimination and do return (set_errno(ERRNO)); 1240 */ 1241 uint_t 1242 set_errno(uint_t error) 1243 { 1244 ASSERT(error != 0); /* must not be used to clear errno */ 1245 1246 curthread->t_post_sys = 1; /* have post_syscall do error return */ 1247 return (ttolwp(curthread)->lwp_errno = error); 1248 } 1249 1250 /* 1251 * set_proc_pre_sys - Set pre-syscall processing for entire process. 1252 */ 1253 void 1254 set_proc_pre_sys(proc_t *p) 1255 { 1256 kthread_t *t; 1257 kthread_t *first; 1258 1259 ASSERT(MUTEX_HELD(&p->p_lock)); 1260 1261 t = first = p->p_tlist; 1262 do { 1263 t->t_pre_sys = 1; 1264 } while ((t = t->t_forw) != first); 1265 } 1266 1267 /* 1268 * set_proc_post_sys - Set post-syscall processing for entire process. 1269 */ 1270 void 1271 set_proc_post_sys(proc_t *p) 1272 { 1273 kthread_t *t; 1274 kthread_t *first; 1275 1276 ASSERT(MUTEX_HELD(&p->p_lock)); 1277 1278 t = first = p->p_tlist; 1279 do { 1280 t->t_post_sys = 1; 1281 } while ((t = t->t_forw) != first); 1282 } 1283 1284 /* 1285 * set_proc_sys - Set pre- and post-syscall processing for entire process. 1286 */ 1287 void 1288 set_proc_sys(proc_t *p) 1289 { 1290 kthread_t *t; 1291 kthread_t *first; 1292 1293 ASSERT(MUTEX_HELD(&p->p_lock)); 1294 1295 t = first = p->p_tlist; 1296 do { 1297 t->t_pre_sys = 1; 1298 t->t_post_sys = 1; 1299 } while ((t = t->t_forw) != first); 1300 } 1301 1302 /* 1303 * set_all_proc_sys - set pre- and post-syscall processing flags for all 1304 * user processes. 1305 * 1306 * This is needed when auditing, tracing, or other facilities which affect 1307 * all processes are turned on. 1308 */ 1309 void 1310 set_all_proc_sys() 1311 { 1312 kthread_t *t; 1313 kthread_t *first; 1314 1315 mutex_enter(&pidlock); 1316 t = first = curthread; 1317 do { 1318 t->t_pre_sys = 1; 1319 t->t_post_sys = 1; 1320 } while ((t = t->t_next) != first); 1321 mutex_exit(&pidlock); 1322 } 1323 1324 /* 1325 * set_proc_ast - Set asynchronous service trap (AST) flag for all 1326 * threads in process. 1327 */ 1328 void 1329 set_proc_ast(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 aston(t); 1339 } while ((t = t->t_forw) != first); 1340 } 1341