subr_syscall.c revision 99072
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 * $FreeBSD: head/sys/kern/subr_trap.c 99072 2002-06-29 17:26:22Z julian $
39 */
40
41#ifdef __i386__
42#include "opt_npx.h"
43#endif
44
45#include <sys/param.h>
46#include <sys/bus.h>
47#include <sys/kernel.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/proc.h>
51#include <sys/kse.h>
52#include <sys/ktr.h>
53#include <sys/resourcevar.h>
54#include <sys/signalvar.h>
55#include <sys/systm.h>
56#include <sys/vmmeter.h>
57#include <machine/cpu.h>
58#include <machine/pcb.h>
59
60/*
61 * Define the code needed before returning to user mode, for
62 * trap and syscall.
63 *
64 * MPSAFE
65 */
66void
67userret(td, frame, oticks)
68	struct thread *td;
69	struct trapframe *frame;
70	u_int oticks;
71{
72	struct proc *p = td->td_proc;
73	struct kse *ke = td->td_kse;
74	struct ksegrp *kg = td->td_ksegrp;
75
76	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
77            p->p_comm);
78#ifdef INVARIANTS
79	/* Check that we called signotify() enough. */
80	mtx_lock(&Giant);
81	PROC_LOCK(p);
82	mtx_lock_spin(&sched_lock);
83	if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 ||
84	    (ke->ke_flags & KEF_ASTPENDING) == 0))
85		printf("failed to set signal flags proprly for ast()\n");
86	mtx_unlock_spin(&sched_lock);
87	PROC_UNLOCK(p);
88	mtx_unlock(&Giant);
89#endif
90
91	/*
92	 * XXX we cheat slightly on the locking here to avoid locking in
93	 * the usual case.  Setting td_priority here is essentially an
94	 * incomplete workaround for not setting it properly elsewhere.
95	 * Now that some interrupt handlers are threads, not setting it
96	 * properly elsewhere can clobber it in the window between setting
97	 * it here and returning to user mode, so don't waste time setting
98	 * it perfectly here.
99	 */
100	if (td->td_priority != kg->kg_user_pri) {
101		mtx_lock_spin(&sched_lock);
102		td->td_priority = kg->kg_user_pri;
103		mtx_unlock_spin(&sched_lock);
104	}
105
106	/*
107	 * We need to check to see if we have to exit or wait due to a
108	 * single threading requirement or some other STOP condition.
109	 */
110	PROC_LOCK(p);
111	thread_suspend_check(0);	/* Can suspend or kill */
112	PROC_UNLOCK(p);
113
114	/*
115	 * DO special thread processing, e.g. upcall tweaking and such
116	 */
117	if (p->p_flag & P_KSES) {
118		thread_userret(p, kg, ke, td, frame);
119		/* printf("KSE thread returned"); */
120	}
121
122	/*
123	 * Charge system time if profiling.
124	 *
125	 * XXX should move PS_PROFIL to a place that can obviously be
126	 * accessed safely without sched_lock.
127	 */
128	if (p->p_sflag & PS_PROFIL) {
129		quad_t ticks;
130
131		mtx_lock_spin(&sched_lock);
132		ticks = ke->ke_sticks - oticks;
133		mtx_unlock_spin(&sched_lock);
134		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
135	}
136}
137
138/*
139 * Process an asynchronous software trap.
140 * This is relatively easy.
141 * This function will return with preemption disabled.
142 */
143void
144ast(struct trapframe *framep)
145{
146	struct thread *td = curthread;
147	struct proc *p = td->td_proc;
148	struct kse *ke = td->td_kse;
149	struct ksegrp *kg = td->td_ksegrp;
150	u_int prticks, sticks;
151	int sflag;
152	int flags;
153	int sig;
154#if defined(DEV_NPX) && !defined(SMP)
155	int ucode;
156#endif
157
158	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
159            p->p_comm);
160	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
161#ifdef WITNESS
162	if (witness_list(td))
163		panic("Returning to user mode with mutex(s) held");
164#endif
165	mtx_assert(&Giant, MA_NOTOWNED);
166	mtx_assert(&sched_lock, MA_NOTOWNED);
167	prticks = 0;		/* XXX: Quiet warning. */
168	td->td_frame = framep;
169	/*
170	 * This updates the p_sflag's for the checks below in one
171	 * "atomic" operation with turning off the astpending flag.
172	 * If another AST is triggered while we are handling the
173	 * AST's saved in sflag, the astpending flag will be set and
174	 * ast() will be called again.
175	 */
176	mtx_lock_spin(&sched_lock);
177	sticks = ke->ke_sticks;
178	sflag = p->p_sflag;
179	flags = ke->ke_flags;
180	p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND);
181	ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC);
182	cnt.v_soft++;
183	if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
184		prticks = p->p_stats->p_prof.pr_ticks;
185		p->p_stats->p_prof.pr_ticks = 0;
186	}
187	mtx_unlock_spin(&sched_lock);
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
196	if (td->td_ucred != p->p_ucred)
197		cred_update_thread(td);
198	if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
199		addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
200	if (sflag & PS_ALRMPEND) {
201		PROC_LOCK(p);
202		psignal(p, SIGVTALRM);
203		PROC_UNLOCK(p);
204	}
205#if defined(DEV_NPX) && !defined(SMP)
206	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
207		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
208		    PCB_NPXTRAP);
209		ucode = npxtrap();
210		if (ucode != -1) {
211			trapsignal(p, SIGFPE, ucode);
212		}
213	}
214#endif
215	if (sflag & PS_PROFPEND) {
216		PROC_LOCK(p);
217		psignal(p, SIGPROF);
218		PROC_UNLOCK(p);
219	}
220	if (flags & KEF_NEEDRESCHED) {
221		mtx_lock_spin(&sched_lock);
222		td->td_priority = kg->kg_user_pri;
223		p->p_stats->p_ru.ru_nivcsw++;
224		mi_switch();
225		mtx_unlock_spin(&sched_lock);
226	}
227	if (sflag & PS_NEEDSIGCHK) {
228		PROC_LOCK(p);
229		while ((sig = cursig(td)) != 0)
230			postsig(sig);
231		PROC_UNLOCK(p);
232	}
233
234	userret(td, framep, sticks);
235	mtx_assert(&Giant, MA_NOTOWNED);
236}
237