subr_syscall.c revision 135573
1326Salm/*-
2326Salm * Copyright (C) 1994, David Greenman
3326Salm * Copyright (c) 1990, 1993
4326Salm *	The Regents of the University of California.  All rights reserved.
5326Salm *
6326Salm * This code is derived from software contributed to Berkeley by
7326Salm * the University of Utah, and William Jolitz.
8326Salm *
9326Salm * Redistribution and use in source and binary forms, with or without
10326Salm * modification, are permitted provided that the following conditions
11326Salm * are met:
12326Salm * 1. Redistributions of source code must retain the above copyright
13326Salm *    notice, this list of conditions and the following disclaimer.
14326Salm * 2. Redistributions in binary form must reproduce the above copyright
15326Salm *    notice, this list of conditions and the following disclaimer in the
16326Salm *    documentation and/or other materials provided with the distribution.
17326Salm * 3. All advertising materials mentioning features or use of this software
18326Salm *    must display the following acknowledgement:
19326Salm *	This product includes software developed by the University of
20326Salm *	California, Berkeley and its contributors.
21326Salm * 4. Neither the name of the University nor the names of its contributors
22326Salm *    may be used to endorse or promote products derived from this software
23326Salm *    without specific prior written permission.
24326Salm *
25326Salm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26326Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27326Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28326Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29326Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30326Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31326Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32326Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33621Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
342941Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35326Salm * SUCH DAMAGE.
36326Salm *
372941Sbde *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
382941Sbde */
392941Sbde
40326Salm#include <sys/cdefs.h>
412941Sbde__FBSDID("$FreeBSD: head/sys/kern/subr_trap.c 135573 2004-09-22 15:24:33Z jhb $");
42326Salm
43326Salm#include "opt_ktrace.h"
442941Sbde#include "opt_mac.h"
452941Sbde#ifdef __i386__
462941Sbde#include "opt_npx.h"
472941Sbde#endif
482941Sbde
49326Salm#include <sys/param.h>
50326Salm#include <sys/bus.h>
511834Swollman#include <sys/kernel.h>
52326Salm#include <sys/lock.h>
53326Salm#include <sys/mac.h>
54326Salm#include <sys/mutex.h>
552941Sbde#include <sys/proc.h>
562941Sbde#include <sys/ktr.h>
572941Sbde#include <sys/resourcevar.h>
582941Sbde#include <sys/sched.h>
59719Swollman#include <sys/signalvar.h>
60326Salm#include <sys/systm.h>
61326Salm#include <sys/vmmeter.h>
62326Salm#ifdef KTRACE
63338Salm#include <sys/uio.h>
64338Salm#include <sys/ktrace.h>
65326Salm#endif
66338Salm
67326Salm#include <machine/cpu.h>
682941Sbde#include <machine/pcb.h>
692941Sbde
702941Sbde/*
712941Sbde * Define the code needed before returning to user mode, for
722941Sbde * trap and syscall.
732941Sbde *
742941Sbde * MPSAFE
752941Sbde */
76338Salmvoid
77326Salmuserret(td, frame, oticks)
782941Sbde	struct thread *td;
79326Salm	struct trapframe *frame;
80326Salm	u_int oticks;
812941Sbde{
82326Salm	struct proc *p = td->td_proc;
83326Salm
84338Salm	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
85326Salm            p->p_comm);
86326Salm#ifdef DIAGNOSTIC
87326Salm	/* Check that we called signotify() enough. */
88326Salm	PROC_LOCK(p);
89326Salm	mtx_lock_spin(&sched_lock);
90338Salm	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
91338Salm	    (td->td_flags & TDF_ASTPENDING) == 0))
92326Salm		printf("failed to set signal flags properly for ast()\n");
93338Salm	mtx_unlock_spin(&sched_lock);
94338Salm	PROC_UNLOCK(p);
95326Salm#endif
962941Sbde
972941Sbde	/*
982941Sbde	 * Let the scheduler adjust our priority etc.
992941Sbde	 */
100338Salm	sched_userret(td);
101338Salm
1022941Sbde	/*
103338Salm	 * We need to check to see if we have to exit or wait due to a
104326Salm	 * single threading requirement or some other STOP condition.
105326Salm	 * Don't bother doing all the work if the stop bits are not set
106326Salm	 * at this time.. If we miss it, we miss it.. no big deal.
107326Salm	 */
108326Salm	if (P_SHOULDSTOP(p)) {
109326Salm		PROC_LOCK(p);
110326Salm		thread_suspend_check(0);	/* Can suspend or kill */
111326Salm		PROC_UNLOCK(p);
112326Salm	}
113326Salm
114326Salm	/*
115326Salm	 * Do special thread processing, e.g. upcall tweaking and such.
116326Salm	 */
117326Salm	if (p->p_flag & P_SA)
118326Salm		thread_userret(td, frame);
119326Salm
120326Salm	/*
121326Salm	 * Charge system time if profiling.
122	 */
123	if (p->p_flag & P_PROFIL) {
124		quad_t ticks;
125
126		mtx_lock_spin(&sched_lock);
127		ticks = td->td_sticks - oticks;
128		mtx_unlock_spin(&sched_lock);
129		addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio);
130	}
131}
132
133/*
134 * Process an asynchronous software trap.
135 * This is relatively easy.
136 * This function will return with preemption disabled.
137 */
138void
139ast(struct trapframe *framep)
140{
141	struct thread *td;
142	struct proc *p;
143	struct ksegrp *kg;
144	struct rlimit rlim;
145	u_int sticks;
146	int sflag;
147	int flags;
148	int sig;
149#if defined(DEV_NPX) && !defined(SMP)
150	int ucode;
151#endif
152
153	td = curthread;
154	p = td->td_proc;
155	kg = td->td_ksegrp;
156
157	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
158            p->p_comm);
159	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
160	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
161	mtx_assert(&Giant, MA_NOTOWNED);
162	mtx_assert(&sched_lock, MA_NOTOWNED);
163	td->td_frame = framep;
164
165	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
166		thread_user_enter(td);
167
168	/*
169	 * This updates the p_sflag's for the checks below in one
170	 * "atomic" operation with turning off the astpending flag.
171	 * If another AST is triggered while we are handling the
172	 * AST's saved in sflag, the astpending flag will be set and
173	 * ast() will be called again.
174	 */
175	mtx_lock_spin(&sched_lock);
176	sticks = td->td_sticks;
177	flags = td->td_flags;
178	sflag = p->p_sflag;
179	p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
180#ifdef MAC
181	p->p_sflag &= ~PS_MACPEND;
182#endif
183	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
184	    TDF_NEEDRESCHED | TDF_INTERRUPT);
185	cnt.v_soft++;
186	mtx_unlock_spin(&sched_lock);
187
188	/*
189	 * XXXKSE While the fact that we owe a user profiling
190	 * tick is stored per KSE in this code, the statistics
191	 * themselves are still stored per process.
192	 * This should probably change, by which I mean that
193	 * possibly the location of both might change.
194	 */
195	if (td->td_ucred != p->p_ucred)
196		cred_update_thread(td);
197	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
198		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
199		td->td_profil_ticks = 0;
200		td->td_pflags &= ~TDP_OWEUPC;
201	}
202	if (sflag & PS_ALRMPEND) {
203		PROC_LOCK(p);
204		psignal(p, SIGVTALRM);
205		PROC_UNLOCK(p);
206	}
207#if defined(DEV_NPX) && !defined(SMP)
208	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
209		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
210		    PCB_NPXTRAP);
211		ucode = npxtrap();
212		if (ucode != -1) {
213			trapsignal(td, SIGFPE, ucode);
214		}
215	}
216#endif
217	if (sflag & PS_PROFPEND) {
218		PROC_LOCK(p);
219		psignal(p, SIGPROF);
220		PROC_UNLOCK(p);
221	}
222	if (sflag & PS_XCPU) {
223		PROC_LOCK(p);
224		lim_rlimit(p, RLIMIT_CPU, &rlim);
225		mtx_lock_spin(&sched_lock);
226		if (p->p_runtime.sec >= rlim.rlim_max) {
227			mtx_unlock_spin(&sched_lock);
228			killproc(p, "exceeded maximum CPU limit");
229		} else {
230			if (p->p_cpulimit < rlim.rlim_max)
231				p->p_cpulimit += 5;
232			mtx_unlock_spin(&sched_lock);
233			psignal(p, SIGXCPU);
234		}
235		PROC_UNLOCK(p);
236	}
237#ifdef MAC
238	if (sflag & PS_MACPEND)
239		mac_thread_userret(td);
240#endif
241	if (flags & TDF_NEEDRESCHED) {
242#ifdef KTRACE
243		if (KTRPOINT(td, KTR_CSW))
244			ktrcsw(1, 1);
245#endif
246		mtx_lock_spin(&sched_lock);
247		sched_prio(td, kg->kg_user_pri);
248		mi_switch(SW_INVOL, NULL);
249		mtx_unlock_spin(&sched_lock);
250#ifdef KTRACE
251		if (KTRPOINT(td, KTR_CSW))
252			ktrcsw(0, 1);
253#endif
254	}
255	if (flags & TDF_NEEDSIGCHK) {
256		PROC_LOCK(p);
257		mtx_lock(&p->p_sigacts->ps_mtx);
258		while ((sig = cursig(td)) != 0)
259			postsig(sig);
260		mtx_unlock(&p->p_sigacts->ps_mtx);
261		PROC_UNLOCK(p);
262	}
263
264	userret(td, framep, sticks);
265	mtx_assert(&Giant, MA_NOTOWNED);
266}
267