dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / arch / x86 / kernel / os / syscall.c
blob1df5ed9f0676b23653fb3460970d132ef9008b6c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/param.h>
27 #include <sys/vmparam.h>
28 #include <sys/types.h>
29 #include <sys/sysmacros.h>
30 #include <sys/systm.h>
31 #include <sys/signal.h>
32 #include <sys/stack.h>
33 #include <sys/cred.h>
34 #include <sys/cmn_err.h>
35 #include <sys/user.h>
36 #include <sys/privregs.h>
37 #include <sys/psw.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
40 #include <sys/proc.h>
41 #include <sys/modctl.h>
42 #include <sys/var.h>
43 #include <sys/inline.h>
44 #include <sys/syscall.h>
45 #include <sys/ucontext.h>
46 #include <sys/cpuvar.h>
47 #include <sys/siginfo.h>
48 #include <sys/trap.h>
49 #include <sys/vtrace.h>
50 #include <sys/sysinfo.h>
51 #include <sys/procfs.h>
52 #include <sys/prsystm.h>
53 #include <c2/audit.h>
54 #include <sys/modctl.h>
55 #include <sys/aio_impl.h>
56 #include <sys/tnf.h>
57 #include <sys/tnf_probe.h>
58 #include <sys/copyops.h>
59 #include <sys/priv.h>
60 #include <sys/msacct.h>
62 int syscalltrace = 0;
63 #ifdef SYSCALLTRACE
64 static kmutex_t systrace_lock; /* syscall tracing lock */
65 #else
66 #define syscalltrace 0
67 #endif /* SYSCALLTRACE */
69 typedef int64_t (*llfcn_t)(); /* function returning long long */
71 int pre_syscall(void);
72 void post_syscall(long rval1, long rval2);
73 static krwlock_t *lock_syscall(struct sysent *, uint_t);
74 void deferred_singlestep_trap(caddr_t);
76 #ifdef _SYSCALL32_IMPL
77 #define LWP_GETSYSENT(lwp) \
78 (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE ? sysent : sysent32)
79 #else
80 #define LWP_GETSYSENT(lwp) (sysent)
81 #endif
84 * If watchpoints are active, don't make copying in of
85 * system call arguments take a read watchpoint trap.
87 static int
88 copyin_args(struct regs *rp, long *ap, uint_t nargs)
90 greg_t *sp = 1 + (greg_t *)rp->r_sp; /* skip ret addr */
92 ASSERT(nargs <= MAXSYSARGS);
94 return (copyin_nowatch(sp, ap, nargs * sizeof (*sp)));
97 #if defined(_SYSCALL32_IMPL)
98 static int
99 copyin_args32(struct regs *rp, long *ap, uint_t nargs)
101 greg32_t *sp = 1 + (greg32_t *)rp->r_sp; /* skip ret addr */
102 uint32_t a32[MAXSYSARGS];
103 int rc;
105 ASSERT(nargs <= MAXSYSARGS);
107 if ((rc = copyin_nowatch(sp, a32, nargs * sizeof (*sp))) == 0) {
108 uint32_t *a32p = &a32[0];
110 while (nargs--)
111 *ap++ = (ulong_t)*a32p++;
113 return (rc);
115 #define COPYIN_ARGS32 copyin_args32
116 #else
117 #define COPYIN_ARGS32 copyin_args
118 #endif
121 * Error handler for system calls where arg copy gets fault.
123 static longlong_t
124 syscall_err()
126 return (0);
130 * Corresponding sysent entry to allow syscall_entry caller
131 * to invoke syscall_err.
133 static struct sysent sysent_err = {
134 0, SE_32RVAL1, NULL, NULL, (llfcn_t)syscall_err
138 * Called from syscall() when a non-trivial 32-bit system call occurs.
139 * Sets up the args and returns a pointer to the handler.
141 struct sysent *
142 syscall_entry(kthread_t *t, long *argp)
144 klwp_t *lwp = ttolwp(t);
145 struct regs *rp = lwptoregs(lwp);
146 unsigned int code;
147 struct sysent *callp;
148 struct sysent *se = LWP_GETSYSENT(lwp);
149 int error = 0;
150 uint_t nargs;
152 ASSERT(t == curthread);
154 lwp->lwp_ru.sysc++;
155 lwp->lwp_eosys = NORMALRETURN; /* assume this will be normal */
158 * Set lwp_ap to point to the args, even if none are needed for this
159 * system call. This is for the loadable-syscall case where the
160 * number of args won't be known until the system call is loaded, and
161 * also maintains a non-NULL lwp_ap setup for get_syscall_args(). Note
162 * that lwp_ap MUST be set to a non-NULL value _BEFORE_ t_sysnum is
163 * set to non-zero; otherwise get_syscall_args(), seeing a non-zero
164 * t_sysnum for this thread, will charge ahead and dereference lwp_ap.
166 lwp->lwp_ap = argp; /* for get_syscall_args */
168 code = rp->r_r0;
169 t->t_sysnum = (short)code;
170 callp = code >= NSYSCALL ? &nosys_ent : se + code;
172 if ((t->t_pre_sys | syscalltrace) != 0) {
173 error = pre_syscall();
176 * pre_syscall() has taken care so that lwp_ap is current;
177 * it either points to syscall-entry-saved amd64 regs,
178 * or it points to lwp_arg[], which has been re-copied from
179 * the ia32 ustack, but either way, it's a current copy after
180 * /proc has possibly mucked with the syscall args.
183 if (error)
184 return (&sysent_err); /* use dummy handler */
188 * Fetch the system call arguments to the kernel stack copy used
189 * for syscall handling.
190 * Note: for loadable system calls the number of arguments required
191 * may not be known at this point, and will be zero if the system call
192 * was never loaded. Once the system call has been loaded, the number
193 * of args is not allowed to be changed.
195 if ((nargs = (uint_t)callp->sy_narg) != 0 &&
196 COPYIN_ARGS32(rp, argp, nargs)) {
197 (void) set_errno(EFAULT);
198 return (&sysent_err); /* use dummy handler */
201 return (callp); /* return sysent entry for caller */
204 void
205 syscall_exit(kthread_t *t, long rval1, long rval2)
208 * Handle signals and other post-call events if necessary.
210 if ((t->t_post_sys_ast | syscalltrace) == 0) {
211 klwp_t *lwp = ttolwp(t);
212 struct regs *rp = lwptoregs(lwp);
215 * Normal return.
216 * Clear error indication and set return values.
218 rp->r_ps &= ~PS_C; /* reset carry bit */
219 rp->r_r0 = rval1;
220 rp->r_r1 = rval2;
221 lwp->lwp_state = LWP_USER;
222 } else
223 post_syscall(rval1, rval2);
224 t->t_sysnum = 0; /* invalidate args */
228 * Perform pre-system-call processing, including stopping for tracing,
229 * auditing, etc.
231 * This routine is called only if the t_pre_sys flag is set. Any condition
232 * requiring pre-syscall handling must set the t_pre_sys flag. If the
233 * condition is persistent, this routine will repost t_pre_sys.
236 pre_syscall()
238 kthread_t *t = curthread;
239 unsigned code = t->t_sysnum;
240 klwp_t *lwp = ttolwp(t);
241 proc_t *p = ttoproc(t);
242 int repost;
244 t->t_pre_sys = repost = 0; /* clear pre-syscall processing flag */
246 #if defined(DEBUG)
248 * On the i386 kernel, lwp_ap points at the piece of the thread
249 * stack that we copy the users arguments into.
251 * On the amd64 kernel, the syscall arguments in the rdi..r9
252 * registers should be pointed at by lwp_ap. If the args need to
253 * be copied so that those registers can be changed without losing
254 * the ability to get the args for /proc, they can be saved by
255 * save_syscall_args(), and lwp_ap will be restored by post_syscall().
257 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
258 #if defined(_LP64)
259 ASSERT(lwp->lwp_ap == (long *)&lwptoregs(lwp)->r_rdi);
260 } else {
261 #endif
262 ASSERT((caddr_t)lwp->lwp_ap > t->t_stkbase &&
263 (caddr_t)lwp->lwp_ap < t->t_stk);
265 #endif /* DEBUG */
268 * Make sure the thread is holding the latest credentials for the
269 * process. The credentials in the process right now apply to this
270 * thread for the entire system call.
272 if (t->t_cred != p->p_cred) {
273 cred_t *oldcred = t->t_cred;
275 * DTrace accesses t_cred in probe context. t_cred must
276 * always be either NULL, or point to a valid, allocated cred
277 * structure.
279 t->t_cred = crgetcred();
280 crfree(oldcred);
284 * From the proc(4) manual page:
285 * When entry to a system call is being traced, the traced process
286 * stops after having begun the call to the system but before the
287 * system call arguments have been fetched from the process.
289 if (PTOU(p)->u_systrap) {
290 if (prismember(&PTOU(p)->u_entrymask, code)) {
291 mutex_enter(&p->p_lock);
293 * Recheck stop condition, now that lock is held.
295 if (PTOU(p)->u_systrap &&
296 prismember(&PTOU(p)->u_entrymask, code)) {
297 stop(PR_SYSENTRY, code);
300 * /proc may have modified syscall args,
301 * either in regs for amd64 or on ustack
302 * for ia32. Either way, arrange to
303 * copy them again, both for the syscall
304 * handler and for other consumers in
305 * post_syscall (like audit). Here, we
306 * only do amd64, and just set lwp_ap
307 * back to the kernel-entry stack copy;
308 * the syscall ml code redoes
309 * move-from-regs to set up for the
310 * syscall handler after we return. For
311 * ia32, save_syscall_args() below makes
312 * an lwp_ap-accessible copy.
314 #if defined(_LP64)
315 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
316 lwp->lwp_argsaved = 0;
317 lwp->lwp_ap =
318 (long *)&lwptoregs(lwp)->r_rdi;
320 #endif
322 mutex_exit(&p->p_lock);
324 repost = 1;
328 * ia32 kernel, or ia32 proc on amd64 kernel: keep args in
329 * lwp_arg for post-syscall processing, regardless of whether
330 * they might have been changed in /proc above.
332 #if defined(_LP64)
333 if (lwp_getdatamodel(lwp) != DATAMODEL_NATIVE)
334 #endif
335 (void) save_syscall_args();
337 if (lwp->lwp_sysabort) {
339 * lwp_sysabort may have been set via /proc while the process
340 * was stopped on PR_SYSENTRY. If so, abort the system call.
341 * Override any error from the copyin() of the arguments.
343 lwp->lwp_sysabort = 0;
344 (void) set_errno(EINTR); /* forces post_sys */
345 t->t_pre_sys = 1; /* repost anyway */
346 return (1); /* don't do system call, return EINTR */
350 * begin auditing for this syscall if the c2audit module is loaded
351 * and auditing is enabled
353 if (audit_active == C2AUDIT_LOADED) {
354 uint32_t auditing = au_zone_getstate(NULL);
356 if (auditing & AU_AUDIT_MASK) {
357 int error;
358 if (error = audit_start(T_SYSCALL, code, auditing, \
359 0, lwp)) {
360 t->t_pre_sys = 1; /* repost anyway */
361 (void) set_errno(error);
362 return (1);
364 repost = 1;
368 #ifndef NPROBE
369 /* Kernel probe */
370 if (tnf_tracing_active) {
371 t->t_post_sys = 1; /* make sure post_syscall runs */
372 repost = 1;
374 #endif /* NPROBE */
376 #ifdef SYSCALLTRACE
377 if (syscalltrace) {
378 int i;
379 long *ap;
380 char *cp;
381 char *sysname;
382 struct sysent *callp;
384 if (code >= NSYSCALL)
385 callp = &nosys_ent; /* nosys has no args */
386 else
387 callp = LWP_GETSYSENT(lwp) + code;
388 (void) save_syscall_args();
389 mutex_enter(&systrace_lock);
390 printf("%d: ", p->p_pid);
391 if (code >= NSYSCALL)
392 printf("0x%x", code);
393 else {
394 sysname = mod_getsysname(code);
395 printf("%s[0x%x/0x%p]", sysname == NULL ? "NULL" :
396 sysname, code, callp->sy_callc);
398 cp = "(";
399 for (i = 0, ap = lwp->lwp_ap; i < callp->sy_narg; i++, ap++) {
400 printf("%s%lx", cp, *ap);
401 cp = ", ";
403 if (i)
404 printf(")");
405 printf(" %s id=0x%p\n", PTOU(p)->u_comm, curthread);
406 mutex_exit(&systrace_lock);
408 #endif /* SYSCALLTRACE */
411 * If there was a continuing reason for pre-syscall processing,
412 * set the t_pre_sys flag for the next system call.
414 if (repost)
415 t->t_pre_sys = 1;
416 lwp->lwp_error = 0; /* for old drivers */
417 lwp->lwp_badpriv = PRIV_NONE;
418 return (0);
423 * Post-syscall processing. Perform abnormal system call completion
424 * actions such as /proc tracing, profiling, signals, preemption, etc.
426 * This routine is called only if t_post_sys, t_sig_check, or t_astflag is set.
427 * Any condition requiring pre-syscall handling must set one of these.
428 * If the condition is persistent, this routine will repost t_post_sys.
430 void
431 post_syscall(long rval1, long rval2)
433 kthread_t *t = curthread;
434 klwp_t *lwp = ttolwp(t);
435 proc_t *p = ttoproc(t);
436 struct regs *rp = lwptoregs(lwp);
437 uint_t error;
438 uint_t code = t->t_sysnum;
439 int repost = 0;
440 int proc_stop = 0; /* non-zero if stopping */
441 int sigprof = 0; /* non-zero if sending SIGPROF */
443 t->t_post_sys = 0;
445 error = lwp->lwp_errno;
448 * Code can be zero if this is a new LWP returning after a forkall(),
449 * other than the one which matches the one in the parent which called
450 * forkall(). In these LWPs, skip most of post-syscall activity.
452 if (code == 0)
453 goto sig_check;
455 * If the trace flag is set, mark the lwp to take a single-step trap
456 * on return to user level (below). The x86 lcall interface and
457 * sysenter has already done this, and turned off the flag, but
458 * amd64 syscall interface has not.
460 if (rp->r_ps & PS_T) {
461 lwp->lwp_pcb.pcb_flags |= DEBUG_PENDING;
462 rp->r_ps &= ~PS_T;
463 aston(curthread);
466 /* put out audit record for this syscall */
467 if (AU_AUDITING()) {
468 rval_t rval;
470 /* XX64 -- truncation of 64-bit return values? */
471 rval.r_val1 = (int)rval1;
472 rval.r_val2 = (int)rval2;
473 audit_finish(T_SYSCALL, code, error, &rval);
474 repost = 1;
477 if (curthread->t_pdmsg != NULL) {
478 char *m = curthread->t_pdmsg;
480 uprintf("%s", m);
481 kmem_free(m, strlen(m) + 1);
482 curthread->t_pdmsg = NULL;
486 * If we're going to stop for /proc tracing, set the flag and
487 * save the arguments so that the return values don't smash them.
489 if (PTOU(p)->u_systrap) {
490 if (prismember(&PTOU(p)->u_exitmask, code)) {
491 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
492 (void) save_syscall_args();
493 proc_stop = 1;
495 repost = 1;
499 * Similarly check to see if SIGPROF might be sent.
501 if (curthread->t_rprof != NULL &&
502 curthread->t_rprof->rp_anystate != 0) {
503 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
504 (void) save_syscall_args();
505 sigprof = 1;
508 if (lwp->lwp_eosys == NORMALRETURN) {
509 if (error == 0) {
510 #ifdef SYSCALLTRACE
511 if (syscalltrace) {
512 mutex_enter(&systrace_lock);
513 printf(
514 "%d: r_val1=0x%lx, r_val2=0x%lx, id 0x%p\n",
515 p->p_pid, rval1, rval2, curthread);
516 mutex_exit(&systrace_lock);
518 #endif /* SYSCALLTRACE */
519 rp->r_ps &= ~PS_C;
520 rp->r_r0 = rval1;
521 rp->r_r1 = rval2;
522 } else {
523 int sig;
524 #ifdef SYSCALLTRACE
525 if (syscalltrace) {
526 mutex_enter(&systrace_lock);
527 printf("%d: error=%d, id 0x%p\n",
528 p->p_pid, error, curthread);
529 mutex_exit(&systrace_lock);
531 #endif /* SYSCALLTRACE */
532 if (error == EINTR && t->t_activefd.a_stale)
533 error = EBADF;
534 if (error == EINTR &&
535 (sig = lwp->lwp_cursig) != 0 &&
536 sigismember(&PTOU(p)->u_sigrestart, sig) &&
537 PTOU(p)->u_signal[sig - 1] != SIG_DFL &&
538 PTOU(p)->u_signal[sig - 1] != SIG_IGN)
539 error = ERESTART;
540 rp->r_r0 = error;
541 rp->r_ps |= PS_C;
546 * From the proc(4) manual page:
547 * When exit from a system call is being traced, the traced process
548 * stops on completion of the system call just prior to checking for
549 * signals and returning to user level. At this point all return
550 * values have been stored into the traced process's saved registers.
552 if (proc_stop) {
553 mutex_enter(&p->p_lock);
554 if (PTOU(p)->u_systrap &&
555 prismember(&PTOU(p)->u_exitmask, code))
556 stop(PR_SYSEXIT, code);
557 mutex_exit(&p->p_lock);
561 * If we are the parent returning from a successful
562 * vfork, wait for the child to exec or exit.
563 * This code must be here and not in the bowels of the system
564 * so that /proc can intercept exit from vfork in a timely way.
566 if (t->t_flag & T_VFPARENT) {
567 ASSERT(code == SYS_vfork || code == SYS_forksys);
568 ASSERT(rp->r_r1 == 0 && error == 0);
569 vfwait((pid_t)rval1);
570 t->t_flag &= ~T_VFPARENT;
574 * If profiling is active, bill the current PC in user-land
575 * and keep reposting until profiling is disabled.
577 if (p->p_prof.pr_scale) {
578 if (lwp->lwp_oweupc)
579 profil_tick(rp->r_pc);
580 repost = 1;
583 sig_check:
585 * Reset flag for next time.
586 * We must do this after stopping on PR_SYSEXIT
587 * because /proc uses the information in lwp_eosys.
589 lwp->lwp_eosys = NORMALRETURN;
590 clear_stale_fd();
591 t->t_flag &= ~T_FORKALL;
593 if (t->t_astflag | t->t_sig_check) {
595 * Turn off the AST flag before checking all the conditions that
596 * may have caused an AST. This flag is on whenever a signal or
597 * unusual condition should be handled after the next trap or
598 * syscall.
600 astoff(t);
602 * If a single-step trap occurred on a syscall (see trap())
603 * recognize it now. Do this before checking for signals
604 * because deferred_singlestep_trap() may generate a SIGTRAP to
605 * the LWP or may otherwise mark the LWP to call issig(FORREAL).
607 if (lwp->lwp_pcb.pcb_flags & DEBUG_PENDING)
608 deferred_singlestep_trap((caddr_t)rp->r_pc);
610 t->t_sig_check = 0;
613 * The following check is legal for the following reasons:
614 * 1) The thread we are checking, is ourselves, so there is
615 * no way the proc can go away.
616 * 2) The only time we need to be protected by the
617 * lock is if the binding is changed.
619 * Note we will still take the lock and check the binding
620 * if the condition was true without the lock held. This
621 * prevents lock contention among threads owned by the
622 * same proc.
625 if (curthread->t_proc_flag & TP_CHANGEBIND) {
626 mutex_enter(&p->p_lock);
627 if (curthread->t_proc_flag & TP_CHANGEBIND) {
628 timer_lwpbind();
629 curthread->t_proc_flag &= ~TP_CHANGEBIND;
631 mutex_exit(&p->p_lock);
635 * for kaio requests on the special kaio poll queue,
636 * copyout their results to user memory.
638 if (p->p_aio)
639 aio_cleanup(0);
641 * If this LWP was asked to hold, call holdlwp(), which will
642 * stop. holdlwps() sets this up and calls pokelwps() which
643 * sets the AST flag.
645 * Also check TP_EXITLWP, since this is used by fresh new LWPs
646 * through lwp_rtt(). That flag is set if the lwp_create(2)
647 * syscall failed after creating the LWP.
649 if (ISHOLD(p) || (t->t_proc_flag & TP_EXITLWP))
650 holdlwp();
653 * All code that sets signals and makes ISSIG_PENDING
654 * evaluate true must set t_sig_check afterwards.
656 if (ISSIG_PENDING(t, lwp, p)) {
657 if (issig(FORREAL))
658 psig();
659 t->t_sig_check = 1; /* recheck next time */
662 if (sigprof) {
663 int nargs = (code > 0 && code < NSYSCALL)?
664 LWP_GETSYSENT(lwp)[code].sy_narg : 0;
665 realsigprof(code, nargs, error);
666 t->t_sig_check = 1; /* recheck next time */
670 * If a performance counter overflow interrupt was
671 * delivered *during* the syscall, then re-enable the
672 * AST so that we take a trip through trap() to cause
673 * the SIGEMT to be delivered.
675 if (lwp->lwp_pcb.pcb_flags & CPC_OVERFLOW)
676 aston(t);
679 * /proc can't enable/disable the trace bit itself
680 * because that could race with the call gate used by
681 * system calls via "lcall". If that happened, an
682 * invalid EFLAGS would result. prstep()/prnostep()
683 * therefore schedule an AST for the purpose.
685 if (lwp->lwp_pcb.pcb_flags & REQUEST_STEP) {
686 lwp->lwp_pcb.pcb_flags &= ~REQUEST_STEP;
687 rp->r_ps |= PS_T;
689 if (lwp->lwp_pcb.pcb_flags & REQUEST_NOSTEP) {
690 lwp->lwp_pcb.pcb_flags &= ~REQUEST_NOSTEP;
691 rp->r_ps &= ~PS_T;
695 lwp->lwp_errno = 0; /* clear error for next time */
697 #ifndef NPROBE
698 /* Kernel probe */
699 if (tnf_tracing_active) {
700 repost = 1;
702 #endif /* NPROBE */
705 * Set state to LWP_USER here so preempt won't give us a kernel
706 * priority if it occurs after this point. Call CL_TRAPRET() to
707 * restore the user-level priority.
709 * It is important that no locks (other than spinlocks) be entered
710 * after this point before returning to user mode (unless lwp_state
711 * is set back to LWP_SYS).
713 * XXX Sampled times past this point are charged to the user.
715 lwp->lwp_state = LWP_USER;
717 if (t->t_trapret) {
718 t->t_trapret = 0;
719 thread_lock(t);
720 CL_TRAPRET(t);
721 thread_unlock(t);
723 if (CPU->cpu_runrun || t->t_schedflag & TS_ANYWAITQ)
724 preempt();
725 prunstop();
727 lwp->lwp_errno = 0; /* clear error for next time */
730 * The thread lock must be held in order to clear sysnum and reset
731 * lwp_ap atomically with respect to other threads in the system that
732 * may be looking at the args via lwp_ap from get_syscall_args().
735 thread_lock(t);
736 t->t_sysnum = 0; /* no longer in a system call */
738 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
739 #if defined(_LP64)
741 * In case the args were copied to the lwp, reset the
742 * pointer so the next syscall will have the right
743 * lwp_ap pointer.
745 lwp->lwp_ap = (long *)&rp->r_rdi;
746 } else {
747 #endif
748 lwp->lwp_ap = NULL; /* reset on every syscall entry */
750 thread_unlock(t);
752 lwp->lwp_argsaved = 0;
755 * If there was a continuing reason for post-syscall processing,
756 * set the t_post_sys flag for the next system call.
758 if (repost)
759 t->t_post_sys = 1;
762 * If there is a ustack registered for this lwp, and the stack rlimit
763 * has been altered, read in the ustack. If the saved stack rlimit
764 * matches the bounds of the ustack, update the ustack to reflect
765 * the new rlimit. If the new stack rlimit is RLIM_INFINITY, disable
766 * stack checking by setting the size to 0.
768 if (lwp->lwp_ustack != 0 && lwp->lwp_old_stk_ctl != 0) {
769 rlim64_t new_size;
770 caddr_t top;
771 stack_t stk;
772 struct rlimit64 rl;
774 mutex_enter(&p->p_lock);
775 new_size = p->p_stk_ctl;
776 top = p->p_usrstack;
777 (void) rctl_rlimit_get(rctlproc_legacy[RLIMIT_STACK], p, &rl);
778 mutex_exit(&p->p_lock);
780 if (rl.rlim_cur == RLIM64_INFINITY)
781 new_size = 0;
783 if (copyin((stack_t *)lwp->lwp_ustack, &stk,
784 sizeof (stack_t)) == 0 &&
785 (stk.ss_size == lwp->lwp_old_stk_ctl ||
786 stk.ss_size == 0) &&
787 stk.ss_sp == top - stk.ss_size) {
788 stk.ss_sp = (void *)((uintptr_t)stk.ss_sp +
789 stk.ss_size - (uintptr_t)new_size);
790 stk.ss_size = new_size;
792 (void) copyout(&stk, (stack_t *)lwp->lwp_ustack,
793 sizeof (stack_t));
796 lwp->lwp_old_stk_ctl = 0;
801 * Called from post_syscall() when a deferred singlestep is to be taken.
803 void
804 deferred_singlestep_trap(caddr_t pc)
806 proc_t *p = ttoproc(curthread);
807 klwp_t *lwp = ttolwp(curthread);
808 pcb_t *pcb = &lwp->lwp_pcb;
809 uint_t fault = 0;
810 k_siginfo_t siginfo;
812 bzero(&siginfo, sizeof (siginfo));
815 * If both NORMAL_STEP and WATCH_STEP are in
816 * effect, give precedence to WATCH_STEP.
817 * If neither is set, user must have set the
818 * PS_T bit in %efl; treat this as NORMAL_STEP.
820 if ((fault = undo_watch_step(&siginfo)) == 0 &&
821 ((pcb->pcb_flags & NORMAL_STEP) ||
822 !(pcb->pcb_flags & WATCH_STEP))) {
823 siginfo.si_signo = SIGTRAP;
824 siginfo.si_code = TRAP_TRACE;
825 siginfo.si_addr = pc;
826 fault = FLTTRACE;
828 pcb->pcb_flags &= ~(DEBUG_PENDING|NORMAL_STEP|WATCH_STEP);
830 if (fault) {
832 * Remember the fault and fault adddress
833 * for real-time (SIGPROF) profiling.
835 lwp->lwp_lastfault = fault;
836 lwp->lwp_lastfaddr = siginfo.si_addr;
838 * If a debugger has declared this fault to be an
839 * event of interest, stop the lwp. Otherwise just
840 * deliver the associated signal.
842 if (prismember(&p->p_fltmask, fault) &&
843 stop_on_fault(fault, &siginfo) == 0)
844 siginfo.si_signo = 0;
847 if (siginfo.si_signo)
848 trapsig(&siginfo, 1);
852 * nonexistent system call-- signal lwp (may want to handle it)
853 * flag error if lwp won't see signal immediately
855 int64_t
856 nosys()
858 tsignal(curthread, SIGSYS);
859 return (set_errno(ENOSYS));
863 * Execute a 32-bit system call on behalf of the current thread.
865 void
866 dosyscall(void)
869 * Need space on the stack to store syscall arguments.
871 long syscall_args[MAXSYSARGS];
872 struct sysent *se;
873 int64_t ret;
875 syscall_mstate(LMS_TRAP, LMS_SYSTEM);
877 ASSERT(curproc->p_model == DATAMODEL_ILP32);
879 CPU_STATS_ENTER_K();
880 CPU_STATS_ADDQ(CPU, sys, syscall, 1);
881 CPU_STATS_EXIT_K();
883 se = syscall_entry(curthread, syscall_args);
886 * syscall_entry() copied all 8 arguments into syscall_args.
888 ret = se->sy_callc(syscall_args[0], syscall_args[1], syscall_args[2],
889 syscall_args[3], syscall_args[4], syscall_args[5], syscall_args[6],
890 syscall_args[7]);
892 syscall_exit(curthread, (int)ret & 0xffffffffu, (int)(ret >> 32));
893 syscall_mstate(LMS_SYSTEM, LMS_TRAP);
897 * Get the arguments to the current system call. See comment atop
898 * save_syscall_args() regarding lwp_ap usage.
901 uint_t
902 get_syscall_args(klwp_t *lwp, long *argp, int *nargsp)
904 kthread_t *t = lwptot(lwp);
905 ulong_t mask = 0xfffffffful;
906 uint_t code;
907 long *ap;
908 int nargs;
910 #if defined(_LP64)
911 if (lwp_getdatamodel(lwp) == DATAMODEL_LP64)
912 mask = 0xfffffffffffffffful;
913 #endif
916 * The thread lock must be held while looking at the arguments to ensure
917 * they don't go away via post_syscall().
918 * get_syscall_args() is the only routine to read them which is callable
919 * outside the LWP in question and hence the only one that must be
920 * synchronized in this manner.
922 thread_lock(t);
924 code = t->t_sysnum;
925 ap = lwp->lwp_ap;
927 thread_unlock(t);
929 if (code != 0 && code < NSYSCALL) {
930 nargs = LWP_GETSYSENT(lwp)[code].sy_narg;
932 ASSERT(nargs <= MAXSYSARGS);
934 *nargsp = nargs;
935 while (nargs-- > 0)
936 *argp++ = *ap++ & mask;
937 } else {
938 *nargsp = 0;
941 return (code);
944 #ifdef _SYSCALL32_IMPL
946 * Get the arguments to the current 32-bit system call.
948 uint_t
949 get_syscall32_args(klwp_t *lwp, int *argp, int *nargsp)
951 long args[MAXSYSARGS];
952 uint_t i, code;
954 code = get_syscall_args(lwp, args, nargsp);
956 for (i = 0; i != *nargsp; i++)
957 *argp++ = (int)args[i];
958 return (code);
960 #endif
963 * Save the system call arguments in a safe place.
965 * On the i386 kernel:
967 * Copy the users args prior to changing the stack or stack pointer.
968 * This is so /proc will be able to get a valid copy of the
969 * args from the user stack even after the user stack has been changed.
970 * Note that the kernel stack copy of the args may also have been
971 * changed by a system call handler which takes C-style arguments.
973 * Note that this may be called by stop() from trap(). In that case
974 * t_sysnum will be zero (syscall_exit clears it), so no args will be
975 * copied.
977 * On the amd64 kernel:
979 * For 64-bit applications, lwp->lwp_ap normally points to %rdi..%r9
980 * in the reg structure. If the user is going to change the argument
981 * registers, rax, or the stack and might want to get the args (for
982 * /proc tracing), it must copy the args elsewhere via save_syscall_args().
984 * For 32-bit applications, lwp->lwp_ap normally points to a copy of
985 * the system call arguments on the kernel stack made from the user
986 * stack. Copy the args prior to change the stack or stack pointer.
987 * This is so /proc will be able to get a valid copy of the args
988 * from the user stack even after that stack has been changed.
990 * This may be called from stop() even when we're not in a system call.
991 * Since there's no easy way to tell, this must be safe (not panic).
992 * If the copyins get data faults, return non-zero.
995 save_syscall_args()
997 kthread_t *t = curthread;
998 klwp_t *lwp = ttolwp(t);
999 uint_t code = t->t_sysnum;
1000 uint_t nargs;
1002 if (lwp->lwp_argsaved || code == 0)
1003 return (0); /* args already saved or not needed */
1005 if (code >= NSYSCALL) {
1006 nargs = 0; /* illegal syscall */
1007 } else {
1008 struct sysent *se = LWP_GETSYSENT(lwp);
1009 struct sysent *callp = se + code;
1011 nargs = callp->sy_narg;
1012 if (LOADABLE_SYSCALL(callp) && nargs == 0) {
1013 krwlock_t *module_lock;
1016 * Find out how many arguments the system
1017 * call uses.
1019 * We have the property that loaded syscalls
1020 * never change the number of arguments they
1021 * use after they've been loaded once. This
1022 * allows us to stop for /proc tracing without
1023 * holding the module lock.
1024 * /proc is assured that sy_narg is valid.
1026 module_lock = lock_syscall(se, code);
1027 nargs = callp->sy_narg;
1028 rw_exit(module_lock);
1033 * Fetch the system call arguments.
1035 if (nargs == 0)
1036 goto out;
1038 ASSERT(nargs <= MAXSYSARGS);
1040 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1041 #if defined(_LP64)
1042 struct regs *rp = lwptoregs(lwp);
1044 lwp->lwp_arg[0] = rp->r_rdi;
1045 lwp->lwp_arg[1] = rp->r_rsi;
1046 lwp->lwp_arg[2] = rp->r_rdx;
1047 lwp->lwp_arg[3] = rp->r_rcx;
1048 lwp->lwp_arg[4] = rp->r_r8;
1049 lwp->lwp_arg[5] = rp->r_r9;
1050 if (nargs > 6 && copyin_args(rp, &lwp->lwp_arg[6], nargs - 6))
1051 return (-1);
1052 } else {
1053 #endif
1054 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_arg, nargs))
1055 return (-1);
1057 out:
1058 lwp->lwp_ap = lwp->lwp_arg;
1059 lwp->lwp_argsaved = 1;
1060 t->t_post_sys = 1; /* so lwp_ap will be reset */
1061 return (0);
1064 void
1065 reset_syscall_args(void)
1067 ttolwp(curthread)->lwp_argsaved = 0;
1071 * Call a system call which takes a pointer to the user args struct and
1072 * a pointer to the return values. This is a bit slower than the standard
1073 * C arg-passing method in some cases.
1075 int64_t
1076 syscall_ap(void)
1078 uint_t error;
1079 struct sysent *callp;
1080 rval_t rval;
1081 kthread_t *t = curthread;
1082 klwp_t *lwp = ttolwp(t);
1083 struct regs *rp = lwptoregs(lwp);
1085 callp = LWP_GETSYSENT(lwp) + t->t_sysnum;
1087 #if defined(__amd64)
1089 * If the arguments don't fit in registers %rdi-%r9, make sure they
1090 * have been copied to the lwp_arg array.
1092 if (callp->sy_narg > 6 && save_syscall_args())
1093 return ((int64_t)set_errno(EFAULT));
1094 #endif
1096 rval.r_val1 = 0;
1097 rval.r_val2 = rp->r_r1;
1098 lwp->lwp_error = 0; /* for old drivers */
1099 error = (*(callp->sy_call))(lwp->lwp_ap, &rval);
1100 if (error)
1101 return ((longlong_t)set_errno(error));
1102 return (rval.r_vals);
1106 * Load system call module.
1107 * Returns with pointer to held read lock for module.
1109 static krwlock_t *
1110 lock_syscall(struct sysent *table, uint_t code)
1112 krwlock_t *module_lock;
1113 struct modctl *modp;
1114 int id;
1115 struct sysent *callp;
1117 callp = table + code;
1118 module_lock = callp->sy_lock;
1121 * Optimization to only call modload if we don't have a loaded
1122 * syscall.
1124 rw_enter(module_lock, RW_READER);
1125 if (LOADED_SYSCALL(callp))
1126 return (module_lock);
1127 rw_exit(module_lock);
1129 for (;;) {
1130 if ((id = modload("sys", syscallnames[code])) == -1)
1131 break;
1134 * If we loaded successfully at least once, the modctl
1135 * will still be valid, so we try to grab it by filename.
1136 * If this call fails, it's because the mod_filename
1137 * was changed after the call to modload() (mod_hold_by_name()
1138 * is the likely culprit). We can safely just take
1139 * another lap if this is the case; the modload() will
1140 * change the mod_filename back to one by which we can
1141 * find the modctl.
1143 modp = mod_find_by_filename("sys", syscallnames[code]);
1145 if (modp == NULL)
1146 continue;
1148 mutex_enter(&mod_lock);
1150 if (!modp->mod_installed) {
1151 mutex_exit(&mod_lock);
1152 continue;
1154 break;
1156 rw_enter(module_lock, RW_READER);
1158 if (id != -1)
1159 mutex_exit(&mod_lock);
1161 return (module_lock);
1165 * Loadable syscall support.
1166 * If needed, load the module, then reserve it by holding a read
1167 * lock for the duration of the call.
1168 * Later, if the syscall is not unloadable, it could patch the vector.
1170 /*ARGSUSED*/
1171 int64_t
1172 loadable_syscall(
1173 long a0, long a1, long a2, long a3,
1174 long a4, long a5, long a6, long a7)
1176 klwp_t *lwp = ttolwp(curthread);
1177 int64_t rval;
1178 struct sysent *callp;
1179 struct sysent *se = LWP_GETSYSENT(lwp);
1180 krwlock_t *module_lock;
1181 int code, error = 0;
1182 int64_t (*sy_call)();
1184 code = curthread->t_sysnum;
1185 callp = se + code;
1188 * Try to autoload the system call if necessary
1190 module_lock = lock_syscall(se, code);
1191 THREAD_KPRI_RELEASE(); /* drop priority given by rw_enter */
1194 * we've locked either the loaded syscall or nosys
1197 if (lwp_getdatamodel(lwp) == DATAMODEL_NATIVE) {
1198 #if defined(_LP64)
1199 if (callp->sy_flags & SE_ARGC) {
1200 sy_call = (int64_t (*)())callp->sy_call;
1201 rval = (*sy_call)(a0, a1, a2, a3, a4, a5);
1202 } else
1203 rval = syscall_ap();
1204 } else {
1205 #endif
1207 * Now that it's loaded, make sure enough args were copied.
1209 if (COPYIN_ARGS32(lwptoregs(lwp), lwp->lwp_ap, callp->sy_narg))
1210 error = EFAULT;
1211 if (error) {
1212 rval = set_errno(error);
1213 } else if (callp->sy_flags & SE_ARGC) {
1214 sy_call = (int64_t (*)())callp->sy_call;
1215 rval = (*sy_call)(lwp->lwp_ap[0], lwp->lwp_ap[1],
1216 lwp->lwp_ap[2], lwp->lwp_ap[3], lwp->lwp_ap[4],
1217 lwp->lwp_ap[5]);
1218 } else
1219 rval = syscall_ap();
1222 THREAD_KPRI_REQUEST(); /* regain priority from read lock */
1223 rw_exit(module_lock);
1224 return (rval);
1228 * Indirect syscall handled in libc on x86 architectures
1230 int64_t
1231 indir()
1233 return (nosys());
1237 * set_errno - set an error return from the current system call.
1238 * This could be a macro.
1239 * This returns the value it is passed, so that the caller can
1240 * use tail-recursion-elimination and do return (set_errno(ERRNO));
1242 uint_t
1243 set_errno(uint_t error)
1245 ASSERT(error != 0); /* must not be used to clear errno */
1247 curthread->t_post_sys = 1; /* have post_syscall do error return */
1248 return (ttolwp(curthread)->lwp_errno = error);
1252 * set_proc_pre_sys - Set pre-syscall processing for entire process.
1254 void
1255 set_proc_pre_sys(proc_t *p)
1257 kthread_t *t;
1258 kthread_t *first;
1260 ASSERT(MUTEX_HELD(&p->p_lock));
1262 t = first = p->p_tlist;
1263 do {
1264 t->t_pre_sys = 1;
1265 } while ((t = t->t_forw) != first);
1269 * set_proc_post_sys - Set post-syscall processing for entire process.
1271 void
1272 set_proc_post_sys(proc_t *p)
1274 kthread_t *t;
1275 kthread_t *first;
1277 ASSERT(MUTEX_HELD(&p->p_lock));
1279 t = first = p->p_tlist;
1280 do {
1281 t->t_post_sys = 1;
1282 } while ((t = t->t_forw) != first);
1286 * set_proc_sys - Set pre- and post-syscall processing for entire process.
1288 void
1289 set_proc_sys(proc_t *p)
1291 kthread_t *t;
1292 kthread_t *first;
1294 ASSERT(MUTEX_HELD(&p->p_lock));
1296 t = first = p->p_tlist;
1297 do {
1298 t->t_pre_sys = 1;
1299 t->t_post_sys = 1;
1300 } while ((t = t->t_forw) != first);
1304 * set_all_proc_sys - set pre- and post-syscall processing flags for all
1305 * user processes.
1307 * This is needed when auditing, tracing, or other facilities which affect
1308 * all processes are turned on.
1310 void
1311 set_all_proc_sys()
1313 kthread_t *t;
1314 kthread_t *first;
1316 mutex_enter(&pidlock);
1317 t = first = curthread;
1318 do {
1319 t->t_pre_sys = 1;
1320 t->t_post_sys = 1;
1321 } while ((t = t->t_next) != first);
1322 mutex_exit(&pidlock);
1326 * set_all_zone_usr_proc_sys - set pre- and post-syscall processing flags for
1327 * all user processes running in the zone of the current process
1329 * This is needed when auditing, tracing, or other facilities which affect
1330 * all processes are turned on.
1332 void
1333 set_all_zone_usr_proc_sys(zoneid_t zoneid)
1335 proc_t *p;
1336 kthread_t *t;
1338 mutex_enter(&pidlock);
1339 for (p = practive; p != NULL; p = p->p_next) {
1340 /* skip kernel and incomplete processes */
1341 if (p->p_exec == NULLVP || p->p_as == &kas ||
1342 p->p_stat == SIDL || p->p_stat == SZOMB ||
1343 (p->p_flag & (SSYS | SEXITING | SEXITLWPS)))
1344 continue;
1346 * Only processes in the given zone (eventually in
1347 * all zones) are taken into account
1349 if (zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) {
1350 mutex_enter(&p->p_lock);
1351 if ((t = p->p_tlist) == NULL) {
1352 mutex_exit(&p->p_lock);
1353 continue;
1356 * Set pre- and post-syscall processing flags
1357 * for all threads of the process
1359 do {
1360 t->t_pre_sys = 1;
1361 t->t_post_sys = 1;
1362 } while (p->p_tlist != (t = t->t_forw));
1363 mutex_exit(&p->p_lock);
1366 mutex_exit(&pidlock);
1370 * set_proc_ast - Set asynchronous service trap (AST) flag for all
1371 * threads in process.
1373 void
1374 set_proc_ast(proc_t *p)
1376 kthread_t *t;
1377 kthread_t *first;
1379 ASSERT(MUTEX_HELD(&p->p_lock));
1381 t = first = p->p_tlist;
1382 do {
1383 aston(t);
1384 } while ((t = t->t_forw) != first);