subr_syscall.c revision 118240
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 118240 2003-07-31 01:36:24Z peter $");
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/kse.h>
57#include <sys/ktr.h>
58#include <sys/resourcevar.h>
59#include <sys/sched.h>
60#include <sys/signalvar.h>
61#include <sys/systm.h>
62#include <sys/vmmeter.h>
63#ifdef KTRACE
64#include <sys/uio.h>
65#include <sys/ktrace.h>
66#endif
67
68#include <machine/cpu.h>
69#include <machine/pcb.h>
70
71/*
72 * Define the code needed before returning to user mode, for
73 * trap and syscall.
74 *
75 * MPSAFE
76 */
77void
78userret(td, frame, oticks)
79	struct thread *td;
80	struct trapframe *frame;
81	u_int oticks;
82{
83	struct proc *p = td->td_proc;
84
85	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
86            p->p_comm);
87#ifdef INVARIANTS
88	/* Check that we called signotify() enough. */
89	PROC_LOCK(p);
90	mtx_lock_spin(&sched_lock);
91	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
92	    (td->td_flags & TDF_ASTPENDING) == 0))
93		printf("failed to set signal flags properly for ast()\n");
94	mtx_unlock_spin(&sched_lock);
95	PROC_UNLOCK(p);
96#endif
97
98	/*
99	 * Let the scheduler adjust our priority etc.
100	 */
101	sched_userret(td);
102
103	/*
104	 * We need to check to see if we have to exit or wait due to a
105	 * single threading requirement or some other STOP condition.
106	 * Don't bother doing all the work if the stop bits are not set
107	 * at this time.. If we miss it, we miss it.. no big deal.
108	 */
109	if (P_SHOULDSTOP(p)) {
110		PROC_LOCK(p);
111		thread_suspend_check(0);	/* Can suspend or kill */
112		PROC_UNLOCK(p);
113	}
114
115	/*
116	 * Do special thread processing, e.g. upcall tweaking and such.
117	 */
118	if (p->p_flag & P_SA) {
119		thread_userret(td, frame);
120	}
121
122	/*
123	 * Charge system time if profiling.
124	 */
125	if (p->p_flag & P_PROFIL) {
126		quad_t ticks;
127
128		mtx_lock_spin(&sched_lock);
129		ticks = td->td_sticks - oticks;
130		mtx_unlock_spin(&sched_lock);
131		addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio);
132	}
133}
134
135/*
136 * Process an asynchronous software trap.
137 * This is relatively easy.
138 * This function will return with preemption disabled.
139 */
140void
141ast(struct trapframe *framep)
142{
143	struct thread *td;
144	struct proc *p;
145	struct kse *ke;
146	struct ksegrp *kg;
147	struct rlimit *rlim;
148	u_int prticks, sticks;
149	int sflag;
150	int flags;
151	int sig;
152#if defined(DEV_NPX) && !defined(SMP)
153	int ucode;
154#endif
155
156	td = curthread;
157	p = td->td_proc;
158	kg = td->td_ksegrp;
159
160	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
161            p->p_comm);
162	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
163	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
164	mtx_assert(&Giant, MA_NOTOWNED);
165	mtx_assert(&sched_lock, MA_NOTOWNED);
166	td->td_frame = framep;
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	ke = td->td_kse;
177	sticks = td->td_sticks;
178	flags = td->td_flags;
179	sflag = p->p_sflag;
180	p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
181#ifdef MAC
182	p->p_sflag &= ~PS_MACPEND;
183#endif
184	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
185	    TDF_NEEDRESCHED | TDF_OWEUPC | TDF_INTERRUPT);
186	cnt.v_soft++;
187	prticks = 0;
188	if (flags & TDF_OWEUPC && p->p_flag & P_PROFIL) {
189		prticks = p->p_stats->p_prof.pr_ticks;
190		p->p_stats->p_prof.pr_ticks = 0;
191	}
192	mtx_unlock_spin(&sched_lock);
193	/*
194	 * XXXKSE While the fact that we owe a user profiling
195	 * tick is stored per KSE in this code, the statistics
196	 * themselves are still stored per process.
197	 * This should probably change, by which I mean that
198	 * possibly the location of both might change.
199	 */
200
201	if (td->td_ucred != p->p_ucred)
202		cred_update_thread(td);
203	if (flags & TDF_OWEUPC && p->p_flag & P_PROFIL)
204		addupc_task(td, p->p_stats->p_prof.pr_addr, prticks);
205	if (sflag & PS_ALRMPEND) {
206		PROC_LOCK(p);
207		psignal(p, SIGVTALRM);
208		PROC_UNLOCK(p);
209	}
210#if defined(DEV_NPX) && !defined(SMP)
211	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
212		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
213		    PCB_NPXTRAP);
214		ucode = npxtrap();
215		if (ucode != -1) {
216			trapsignal(td, SIGFPE, ucode);
217		}
218	}
219#endif
220	if (sflag & PS_PROFPEND) {
221		PROC_LOCK(p);
222		psignal(p, SIGPROF);
223		PROC_UNLOCK(p);
224	}
225	if (sflag & PS_XCPU) {
226		PROC_LOCK(p);
227		rlim = &p->p_rlimit[RLIMIT_CPU];
228		mtx_lock_spin(&sched_lock);
229		if (p->p_runtime.sec >= rlim->rlim_max) {
230			mtx_unlock_spin(&sched_lock);
231			killproc(p, "exceeded maximum CPU limit");
232		} else {
233			if (p->p_cpulimit < rlim->rlim_max)
234				p->p_cpulimit += 5;
235			mtx_unlock_spin(&sched_lock);
236			psignal(p, SIGXCPU);
237		}
238		PROC_UNLOCK(p);
239	}
240#ifdef MAC
241	if (sflag & PS_MACPEND)
242		mac_thread_userret(td);
243#endif
244	if (flags & TDF_NEEDRESCHED) {
245#ifdef KTRACE
246		if (KTRPOINT(td, KTR_CSW))
247			ktrcsw(1, 0);
248#endif
249		mtx_lock_spin(&sched_lock);
250		sched_prio(td, kg->kg_user_pri);
251		p->p_stats->p_ru.ru_nivcsw++;
252		mi_switch();
253		mtx_unlock_spin(&sched_lock);
254#ifdef KTRACE
255		if (KTRPOINT(td, KTR_CSW))
256			ktrcsw(0, 0);
257#endif
258	}
259	if (flags & TDF_NEEDSIGCHK) {
260		PROC_LOCK(p);
261		mtx_lock(&p->p_sigacts->ps_mtx);
262		while ((sig = cursig(td)) != 0)
263			postsig(sig);
264		mtx_unlock(&p->p_sigacts->ps_mtx);
265		PROC_UNLOCK(p);
266	}
267
268	userret(td, framep, sticks);
269#ifdef DIAGNOSTIC
270	cred_free_thread(td);
271#endif
272	mtx_assert(&Giant, MA_NOTOWNED);
273}
274