subr_syscall.c revision 155504
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 155504 2006-02-10 14:59:16Z davidxu $");
42
43#include "opt_ktrace.h"
44#include "opt_mac.h"
45#ifdef __i386__
46#include "opt_npx.h"
47#endif
48
49#include <sys/param.h>
50#include <sys/bus.h>
51#include <sys/kernel.h>
52#include <sys/lock.h>
53#include <sys/mac.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/*
71 * Define the code needed before returning to user mode, for
72 * trap and syscall.
73 *
74 * MPSAFE
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            p->p_comm);
83#ifdef DIAGNOSTIC
84	/* Check that we called signotify() enough. */
85	PROC_LOCK(p);
86	mtx_lock_spin(&sched_lock);
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	mtx_unlock_spin(&sched_lock);
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	/*
118	 * Do special thread processing, e.g. upcall tweaking and such.
119	 */
120	if (p->p_flag & P_SA)
121		thread_userret(td, frame);
122
123	/*
124	 * Charge system time if profiling.
125	 */
126	if (p->p_flag & P_PROFIL) {
127
128		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
129	}
130
131	/*
132	 * Let the scheduler adjust our priority etc.
133	 */
134	sched_userret(td);
135	KASSERT(td->td_locks == 0,
136	    ("userret: Returning with %d locks held.", td->td_locks));
137}
138
139/*
140 * Process an asynchronous software trap.
141 * This is relatively easy.
142 * This function will return with preemption disabled.
143 */
144void
145ast(struct trapframe *framep)
146{
147	struct thread *td;
148	struct proc *p;
149	struct ksegrp *kg;
150	struct rlimit rlim;
151	int sflag;
152	int flags;
153	int sig;
154#if defined(DEV_NPX) && !defined(SMP)
155	int ucode;
156	ksiginfo_t ksi;
157#endif
158
159	td = curthread;
160	p = td->td_proc;
161	kg = td->td_ksegrp;
162
163	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
164            p->p_comm);
165	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
166	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
167	mtx_assert(&Giant, MA_NOTOWNED);
168	mtx_assert(&sched_lock, MA_NOTOWNED);
169	td->td_frame = framep;
170	td->td_pticks = 0;
171
172	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
173		thread_user_enter(td);
174
175	/*
176	 * This updates the p_sflag'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 sflag, the astpending flag will be set and
180	 * ast() will be called again.
181	 */
182	mtx_lock_spin(&sched_lock);
183	flags = td->td_flags;
184	sflag = p->p_sflag;
185	if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND | PS_XCPU))
186		p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
187#ifdef MAC
188	if (p->p_sflag & PS_MACPEND)
189		p->p_sflag &= ~PS_MACPEND;
190#endif
191	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
192	    TDF_NEEDRESCHED | TDF_INTERRUPT);
193	cnt.v_soft++;
194	mtx_unlock_spin(&sched_lock);
195
196	/*
197	 * XXXKSE While the fact that we owe a user profiling
198	 * tick is stored per KSE in this code, the statistics
199	 * themselves are still stored per process.
200	 * This should probably change, by which I mean that
201	 * possibly the location of both might change.
202	 */
203	if (td->td_ucred != p->p_ucred)
204		cred_update_thread(td);
205	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
206		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
207		td->td_profil_ticks = 0;
208		td->td_pflags &= ~TDP_OWEUPC;
209	}
210	if (sflag & PS_ALRMPEND) {
211		PROC_LOCK(p);
212		psignal(p, SIGVTALRM);
213		PROC_UNLOCK(p);
214	}
215#if defined(DEV_NPX) && !defined(SMP)
216	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
217		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
218		    PCB_NPXTRAP);
219		ucode = npxtrap();
220		if (ucode != -1) {
221			ksiginfo_init_trap(&ksi);
222			ksi.ksi_signo = SIGFPE;
223			ksi.ksi_code = ucode;
224			trapsignal(td, &ksi);
225		}
226	}
227#endif
228	if (sflag & PS_PROFPEND) {
229		PROC_LOCK(p);
230		psignal(p, SIGPROF);
231		PROC_UNLOCK(p);
232	}
233	if (sflag & PS_XCPU) {
234		PROC_LOCK(p);
235		lim_rlimit(p, RLIMIT_CPU, &rlim);
236		mtx_lock_spin(&sched_lock);
237		if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
238			mtx_unlock_spin(&sched_lock);
239			killproc(p, "exceeded maximum CPU limit");
240		} else {
241			if (p->p_cpulimit < rlim.rlim_max)
242				p->p_cpulimit += 5;
243			mtx_unlock_spin(&sched_lock);
244			psignal(p, SIGXCPU);
245		}
246		PROC_UNLOCK(p);
247	}
248#ifdef MAC
249	if (sflag & PS_MACPEND)
250		mac_thread_userret(td);
251#endif
252	if (flags & TDF_NEEDRESCHED) {
253#ifdef KTRACE
254		if (KTRPOINT(td, KTR_CSW))
255			ktrcsw(1, 1);
256#endif
257		mtx_lock_spin(&sched_lock);
258		sched_prio(td, kg->kg_user_pri);
259		mi_switch(SW_INVOL, NULL);
260		mtx_unlock_spin(&sched_lock);
261#ifdef KTRACE
262		if (KTRPOINT(td, KTR_CSW))
263			ktrcsw(0, 1);
264#endif
265	}
266	if (flags & TDF_NEEDSIGCHK) {
267		PROC_LOCK(p);
268		mtx_lock(&p->p_sigacts->ps_mtx);
269		while ((sig = cursig(td)) != 0)
270			postsig(sig);
271		mtx_unlock(&p->p_sigacts->ps_mtx);
272		PROC_UNLOCK(p);
273	}
274
275	userret(td, framep);
276	mtx_assert(&Giant, MA_NOTOWNED);
277}
278