subr_syscall.c revision 144101
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 144101 2005-03-25 09:56:56Z jeff $");
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(td, frame, oticks)
78	struct thread *td;
79	struct trapframe *frame;
80	u_int oticks;
81{
82	struct proc *p = td->td_proc;
83
84	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
85            p->p_comm);
86#ifdef DIAGNOSTIC
87	/* Check that we called signotify() enough. */
88	PROC_LOCK(p);
89	mtx_lock_spin(&sched_lock);
90	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
91	    (td->td_flags & TDF_ASTPENDING) == 0))
92		printf("failed to set signal flags properly for ast()\n");
93	mtx_unlock_spin(&sched_lock);
94	PROC_UNLOCK(p);
95#endif
96
97	/*
98	 * If this thread tickled GEOM, we need to wait for the giggling to
99	 * stop before we return to userland
100	 */
101	if (td->td_pflags & TDP_GEOM)
102		g_waitidle();
103
104	/*
105	 * We need to check to see if we have to exit or wait due to a
106	 * single threading requirement or some other STOP condition.
107	 * Don't bother doing all the work if the stop bits are not set
108	 * at this time.. If we miss it, we miss it.. no big deal.
109	 */
110	if (P_SHOULDSTOP(p)) {
111		PROC_LOCK(p);
112		thread_suspend_check(0);	/* Can suspend or kill */
113		PROC_UNLOCK(p);
114	}
115
116	/*
117	 * Do special thread processing, e.g. upcall tweaking and such.
118	 */
119	if (p->p_flag & P_SA)
120		thread_userret(td, frame);
121
122	/*
123	 * Charge system time if profiling.
124	 */
125	if (p->p_flag & P_PROFIL) {
126		quad_t ticks;
127
128		ticks = td->td_sticks - oticks;
129		addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio);
130	}
131
132	/*
133	 * Let the scheduler adjust our priority etc.
134	 */
135	sched_userret(td);
136#if 0
137	/* This is not presently working. */
138	KASSERT(td->td_locks == 0,
139	    ("userret: Returning with %d locks held.", td->td_locks));
140#endif
141}
142
143/*
144 * Process an asynchronous software trap.
145 * This is relatively easy.
146 * This function will return with preemption disabled.
147 */
148void
149ast(struct trapframe *framep)
150{
151	struct thread *td;
152	struct proc *p;
153	struct ksegrp *kg;
154	struct rlimit rlim;
155	u_int sticks;
156	int sflag;
157	int flags;
158	int sig;
159#if defined(DEV_NPX) && !defined(SMP)
160	int ucode;
161#endif
162
163	td = curthread;
164	p = td->td_proc;
165	kg = td->td_ksegrp;
166
167	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
168            p->p_comm);
169	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
170	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
171	mtx_assert(&Giant, MA_NOTOWNED);
172	mtx_assert(&sched_lock, MA_NOTOWNED);
173	td->td_frame = framep;
174	sticks = td->td_sticks;
175
176	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
177		thread_user_enter(td);
178
179	/*
180	 * This updates the p_sflag's for the checks below in one
181	 * "atomic" operation with turning off the astpending flag.
182	 * If another AST is triggered while we are handling the
183	 * AST's saved in sflag, the astpending flag will be set and
184	 * ast() will be called again.
185	 */
186	mtx_lock_spin(&sched_lock);
187	flags = td->td_flags;
188	sflag = p->p_sflag;
189	p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
190#ifdef MAC
191	p->p_sflag &= ~PS_MACPEND;
192#endif
193	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
194	    TDF_NEEDRESCHED | TDF_INTERRUPT);
195	cnt.v_soft++;
196	mtx_unlock_spin(&sched_lock);
197
198	/*
199	 * XXXKSE While the fact that we owe a user profiling
200	 * tick is stored per KSE in this code, the statistics
201	 * themselves are still stored per process.
202	 * This should probably change, by which I mean that
203	 * possibly the location of both might change.
204	 */
205	if (td->td_ucred != p->p_ucred)
206		cred_update_thread(td);
207	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
208		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
209		td->td_profil_ticks = 0;
210		td->td_pflags &= ~TDP_OWEUPC;
211	}
212	if (sflag & PS_ALRMPEND) {
213		PROC_LOCK(p);
214		psignal(p, SIGVTALRM);
215		PROC_UNLOCK(p);
216	}
217#if defined(DEV_NPX) && !defined(SMP)
218	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
219		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
220		    PCB_NPXTRAP);
221		ucode = npxtrap();
222		if (ucode != -1) {
223			trapsignal(td, SIGFPE, ucode);
224		}
225	}
226#endif
227	if (sflag & PS_PROFPEND) {
228		PROC_LOCK(p);
229		psignal(p, SIGPROF);
230		PROC_UNLOCK(p);
231	}
232	if (sflag & PS_XCPU) {
233		PROC_LOCK(p);
234		lim_rlimit(p, RLIMIT_CPU, &rlim);
235		mtx_lock_spin(&sched_lock);
236		if (p->p_rux.rux_runtime.sec >= rlim.rlim_max) {
237			mtx_unlock_spin(&sched_lock);
238			killproc(p, "exceeded maximum CPU limit");
239		} else {
240			if (p->p_cpulimit < rlim.rlim_max)
241				p->p_cpulimit += 5;
242			mtx_unlock_spin(&sched_lock);
243			psignal(p, SIGXCPU);
244		}
245		PROC_UNLOCK(p);
246	}
247#ifdef MAC
248	if (sflag & PS_MACPEND)
249		mac_thread_userret(td);
250#endif
251	if (flags & TDF_NEEDRESCHED) {
252#ifdef KTRACE
253		if (KTRPOINT(td, KTR_CSW))
254			ktrcsw(1, 1);
255#endif
256		mtx_lock_spin(&sched_lock);
257		sched_prio(td, kg->kg_user_pri);
258		mi_switch(SW_INVOL, NULL);
259		mtx_unlock_spin(&sched_lock);
260#ifdef KTRACE
261		if (KTRPOINT(td, KTR_CSW))
262			ktrcsw(0, 1);
263#endif
264	}
265	if (flags & TDF_NEEDSIGCHK) {
266		PROC_LOCK(p);
267		mtx_lock(&p->p_sigacts->ps_mtx);
268		while ((sig = cursig(td)) != 0)
269			postsig(sig);
270		mtx_unlock(&p->p_sigacts->ps_mtx);
271		PROC_UNLOCK(p);
272	}
273
274	userret(td, framep, sticks);
275	mtx_assert(&Giant, MA_NOTOWNED);
276}
277