subr_syscall.c revision 173601
1/*-
2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 173601 2007-11-14 06:51:33Z julian $");
42
43#include "opt_ktrace.h"
44#include "opt_mac.h"
45#ifdef __i386__
46#include "opt_npx.h"
47#endif
48#include "opt_sched.h"
49
50#include <sys/param.h>
51#include <sys/bus.h>
52#include <sys/kernel.h>
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <sys/proc.h>
56#include <sys/ktr.h>
57#include <sys/resourcevar.h>
58#include <sys/sched.h>
59#include <sys/signalvar.h>
60#include <sys/systm.h>
61#include <sys/vmmeter.h>
62#ifdef KTRACE
63#include <sys/uio.h>
64#include <sys/ktrace.h>
65#endif
66
67#include <machine/cpu.h>
68#include <machine/pcb.h>
69
70#include <security/mac/mac_framework.h>
71
72/*
73 * Define the code needed before returning to user mode, for trap and
74 * syscall.
75 */
76void
77userret(struct thread *td, struct trapframe *frame)
78{
79	struct proc *p = td->td_proc;
80
81	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
82            td->td_name);
83#ifdef DIAGNOSTIC
84	/* Check that we called signotify() enough. */
85	PROC_LOCK(p);
86	thread_lock(td);
87	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
88	    (td->td_flags & TDF_ASTPENDING) == 0))
89		printf("failed to set signal flags properly for ast()\n");
90	thread_unlock(td);
91	PROC_UNLOCK(p);
92#endif
93
94#ifdef KTRACE
95	KTRUSERRET(td);
96#endif
97
98	/*
99	 * If this thread tickled GEOM, we need to wait for the giggling to
100	 * stop before we return to userland
101	 */
102	if (td->td_pflags & TDP_GEOM)
103		g_waitidle();
104
105	/*
106	 * We need to check to see if we have to exit or wait due to a
107	 * single threading requirement or some other STOP condition.
108	 * Don't bother doing all the work if the stop bits are not set
109	 * at this time.. If we miss it, we miss it.. no big deal.
110	 */
111	if (P_SHOULDSTOP(p)) {
112		PROC_LOCK(p);
113		thread_suspend_check(0);	/* Can suspend or kill */
114		PROC_UNLOCK(p);
115	}
116
117#ifdef KSE
118	/*
119	 * Do special thread processing, e.g. upcall tweaking and such.
120	 */
121	if (p->p_flag & P_SA)
122		thread_userret(td, frame);
123#endif
124
125	/*
126	 * Charge system time if profiling.
127	 */
128	if (p->p_flag & P_PROFIL) {
129
130		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
131	}
132
133	/*
134	 * Let the scheduler adjust our priority etc.
135	 */
136	sched_userret(td);
137	KASSERT(td->td_locks == 0,
138	    ("userret: Returning with %d locks held.", td->td_locks));
139}
140
141/*
142 * Process an asynchronous software trap.
143 * This is relatively easy.
144 * This function will return with preemption disabled.
145 */
146void
147ast(struct trapframe *framep)
148{
149	struct thread *td;
150	struct proc *p;
151	int flags;
152	int sig;
153#if defined(DEV_NPX) && !defined(SMP)
154	int ucode;
155	ksiginfo_t ksi;
156#endif
157
158	td = curthread;
159	p = td->td_proc;
160
161	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
162            p->p_comm);
163	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
164	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
165	mtx_assert(&Giant, MA_NOTOWNED);
166	THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
167	td->td_frame = framep;
168	td->td_pticks = 0;
169
170#ifdef KSE
171	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
172		thread_user_enter(td);
173#endif
174
175	/*
176	 * This updates the td_flag's for the checks below in one
177	 * "atomic" operation with turning off the astpending flag.
178	 * If another AST is triggered while we are handling the
179	 * AST's saved in flags, the astpending flag will be set and
180	 * ast() will be called again.
181	 */
182	thread_lock(td);
183	flags = td->td_flags;
184	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
185	    TDF_NEEDRESCHED | TDF_INTERRUPT | TDF_ALRMPEND | TDF_PROFPEND |
186	    TDF_MACPEND);
187	thread_unlock(td);
188	PCPU_INC(cnt.v_trap);
189
190	/*
191	 * XXXKSE While the fact that we owe a user profiling
192	 * tick is stored per thread in this code, the statistics
193	 * themselves are still stored per process.
194	 * This should probably change, by which I mean that
195	 * possibly the location of both might change.
196	 */
197	if (td->td_ucred != p->p_ucred)
198		cred_update_thread(td);
199	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
200		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
201		td->td_profil_ticks = 0;
202		td->td_pflags &= ~TDP_OWEUPC;
203	}
204	if (flags & TDF_ALRMPEND) {
205		PROC_LOCK(p);
206		psignal(p, SIGVTALRM);
207		PROC_UNLOCK(p);
208	}
209#if defined(DEV_NPX) && !defined(SMP)
210	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
211		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
212		    PCB_NPXTRAP);
213		ucode = npxtrap();
214		if (ucode != -1) {
215			ksiginfo_init_trap(&ksi);
216			ksi.ksi_signo = SIGFPE;
217			ksi.ksi_code = ucode;
218			trapsignal(td, &ksi);
219		}
220	}
221#endif
222	if (flags & TDF_PROFPEND) {
223		PROC_LOCK(p);
224		psignal(p, SIGPROF);
225		PROC_UNLOCK(p);
226	}
227#ifdef MAC
228	if (flags & TDF_MACPEND)
229		mac_thread_userret(td);
230#endif
231	if (flags & TDF_NEEDRESCHED) {
232#ifdef KTRACE
233		if (KTRPOINT(td, KTR_CSW))
234			ktrcsw(1, 1);
235#endif
236		thread_lock(td);
237		sched_prio(td, td->td_user_pri);
238		SCHED_STAT_INC(switch_needresched);
239		mi_switch(SW_INVOL, NULL);
240		thread_unlock(td);
241#ifdef KTRACE
242		if (KTRPOINT(td, KTR_CSW))
243			ktrcsw(0, 1);
244#endif
245	}
246	if (flags & TDF_NEEDSIGCHK) {
247		PROC_LOCK(p);
248		mtx_lock(&p->p_sigacts->ps_mtx);
249		while ((sig = cursig(td)) != 0)
250			postsig(sig);
251		mtx_unlock(&p->p_sigacts->ps_mtx);
252		PROC_UNLOCK(p);
253	}
254
255	userret(td, framep);
256	mtx_assert(&Giant, MA_NOTOWNED);
257}
258