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