subr_syscall.c revision 99714
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 99714 2002-07-10 06:40: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	 * Don't bother doing all the work if the stop bits are not set
110	 * at this time.. If we miss it, we miss it.. no big deal.
111	 */
112	if (P_SHOULDSTOP(p)) {
113		PROC_LOCK(p);
114		thread_suspend_check(0);	/* Can suspend or kill */
115		PROC_UNLOCK(p);
116	}
117
118	/*
119	 * DO special thread processing, e.g. upcall tweaking and such
120	 */
121	if (p->p_flag & P_KSES) {
122		thread_userret(p, kg, ke, td, frame);
123		/* printf("KSE thread returned"); */
124	}
125
126	/*
127	 * Charge system time if profiling.
128	 *
129	 * XXX should move PS_PROFIL to a place that can obviously be
130	 * accessed safely without sched_lock.
131	 */
132	if (p->p_sflag & PS_PROFIL) {
133		quad_t ticks;
134
135		mtx_lock_spin(&sched_lock);
136		ticks = ke->ke_sticks - oticks;
137		mtx_unlock_spin(&sched_lock);
138		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
139	}
140}
141
142/*
143 * Process an asynchronous software trap.
144 * This is relatively easy.
145 * This function will return with preemption disabled.
146 */
147void
148ast(struct trapframe *framep)
149{
150	struct thread *td = curthread;
151	struct proc *p = td->td_proc;
152	struct kse *ke = td->td_kse;
153	struct ksegrp *kg = td->td_ksegrp;
154	u_int prticks, sticks;
155	int sflag;
156	int flags;
157	int sig;
158#if defined(DEV_NPX) && !defined(SMP)
159	int ucode;
160#endif
161
162	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
163            p->p_comm);
164	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
165#ifdef WITNESS
166	if (witness_list(td))
167		panic("Returning to user mode with mutex(s) held");
168#endif
169	mtx_assert(&Giant, MA_NOTOWNED);
170	mtx_assert(&sched_lock, MA_NOTOWNED);
171	prticks = 0;		/* XXX: Quiet warning. */
172	td->td_frame = framep;
173	/*
174	 * This updates the p_sflag's for the checks below in one
175	 * "atomic" operation with turning off the astpending flag.
176	 * If another AST is triggered while we are handling the
177	 * AST's saved in sflag, the astpending flag will be set and
178	 * ast() will be called again.
179	 */
180	mtx_lock_spin(&sched_lock);
181	sticks = ke->ke_sticks;
182	sflag = p->p_sflag;
183	flags = ke->ke_flags;
184	p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND);
185	ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC);
186	cnt.v_soft++;
187	if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
188		prticks = p->p_stats->p_prof.pr_ticks;
189		p->p_stats->p_prof.pr_ticks = 0;
190	}
191	mtx_unlock_spin(&sched_lock);
192	/*
193	 * XXXKSE While the fact that we owe a user profiling
194	 * tick is stored per KSE in this code, the statistics
195	 * themselves are still stored per process.
196	 * This should probably change, by which I mean that
197	 * possibly the location of both might change.
198	 */
199
200	if (td->td_ucred != p->p_ucred)
201		cred_update_thread(td);
202	if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
203		addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
204	if (sflag & PS_ALRMPEND) {
205		PROC_LOCK(p);
206		psignal(p, SIGVTALRM);
207		PROC_UNLOCK(p);
208	}
209#if defined(DEV_NPX) && !defined(SMP)
210	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
211		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
212		    PCB_NPXTRAP);
213		ucode = npxtrap();
214		if (ucode != -1) {
215			trapsignal(p, SIGFPE, ucode);
216		}
217	}
218#endif
219	if (sflag & PS_PROFPEND) {
220		PROC_LOCK(p);
221		psignal(p, SIGPROF);
222		PROC_UNLOCK(p);
223	}
224	if (flags & KEF_NEEDRESCHED) {
225		mtx_lock_spin(&sched_lock);
226		td->td_priority = kg->kg_user_pri;
227		p->p_stats->p_ru.ru_nivcsw++;
228		mi_switch();
229		mtx_unlock_spin(&sched_lock);
230	}
231	if (sflag & PS_NEEDSIGCHK) {
232		PROC_LOCK(p);
233		while ((sig = cursig(td)) != 0)
234			postsig(sig);
235		PROC_UNLOCK(p);
236	}
237
238	userret(td, framep, sticks);
239	mtx_assert(&Giant, MA_NOTOWNED);
240}
241