subr_syscall.c revision 90538
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 90538 2002-02-11 20:37:54Z 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/resourcevar.h>
52#include <sys/signalvar.h>
53#include <sys/systm.h>
54#include <sys/vmmeter.h>
55#include <machine/cpu.h>
56#include <machine/pcb.h>
57
58/*
59 * Define the code needed before returning to user mode, for
60 * trap and syscall.
61 *
62 * MPSAFE
63 */
64void
65userret(td, frame, oticks)
66	struct thread *td;
67	struct trapframe *frame;
68	u_int oticks;
69{
70	struct proc *p = td->td_proc;
71	struct kse *ke = td->td_kse;
72	struct ksegrp *kg = td->td_ksegrp;
73	int sig;
74
75	mtx_lock(&Giant);
76	PROC_LOCK(p);
77	while ((sig = CURSIG(p)) != 0)
78		postsig(sig);
79	PROC_UNLOCK(p);
80	mtx_unlock(&Giant);
81
82	mtx_lock_spin(&sched_lock);
83	td->td_priority = kg->kg_user_pri;
84	if (ke->ke_flags & KEF_NEEDRESCHED) {
85		DROP_GIANT();
86		setrunqueue(td);
87		p->p_stats->p_ru.ru_nivcsw++;
88		mi_switch();
89		mtx_unlock_spin(&sched_lock);
90		PICKUP_GIANT();
91		mtx_lock(&Giant);
92		PROC_LOCK(p);
93		while ((sig = CURSIG(p)) != 0)
94			postsig(sig);
95		mtx_unlock(&Giant);
96		PROC_UNLOCK(p);
97		mtx_lock_spin(&sched_lock);
98	}
99
100	/*
101	 * Charge system time if profiling.
102	 */
103	if (p->p_sflag & PS_PROFIL) {
104		quad_t ticks;
105
106		ticks = ke->ke_sticks - oticks;
107		mtx_unlock_spin(&sched_lock);
108		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
109	} else
110		mtx_unlock_spin(&sched_lock);
111}
112
113/*
114 * Process an asynchronous software trap.
115 * This is relatively easy.
116 * This function will return with preemption disabled.
117 */
118void
119ast(framep)
120	struct trapframe *framep;
121{
122	struct thread *td = curthread;
123	struct proc *p = td->td_proc;
124	struct kse *ke = td->td_kse;
125	u_int prticks, sticks;
126	critical_t s;
127	int sflag;
128	int flags;
129#if defined(DEV_NPX) && !defined(SMP)
130	int ucode;
131#endif
132
133	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
134	KASSERT(td->td_ucred == NULL, ("leaked ucred"));
135#ifdef WITNESS
136	if (witness_list(td))
137		panic("Returning to user mode with mutex(s) held");
138#endif
139	mtx_assert(&Giant, MA_NOTOWNED);
140	prticks = 0;		/* XXX: Quiet warning. */
141	s = cpu_critical_enter();
142	while ((ke->ke_flags & (KEF_ASTPENDING | KEF_NEEDRESCHED)) != 0) {
143		cpu_critical_exit(s);
144		td->td_frame = framep;
145		/*
146		 * This updates the p_sflag's for the checks below in one
147		 * "atomic" operation with turning off the astpending flag.
148		 * If another AST is triggered while we are handling the
149		 * AST's saved in sflag, the astpending flag will be set and
150		 * we will loop again.
151		 */
152		mtx_lock_spin(&sched_lock);
153		sticks = ke->ke_sticks;
154		sflag = p->p_sflag;
155		flags = ke->ke_flags;
156		p->p_sflag &= ~(PS_PROFPEND | PS_ALRMPEND);
157		ke->ke_flags &= ~(KEF_OWEUPC | KEF_ASTPENDING);
158		cnt.v_soft++;
159		if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
160			prticks = p->p_stats->p_prof.pr_ticks;
161			p->p_stats->p_prof.pr_ticks = 0;
162		}
163		mtx_unlock_spin(&sched_lock);
164		PROC_LOCK(p);
165		td->td_ucred = crhold(p->p_ucred);
166		PROC_UNLOCK(p);
167		if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
168			addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
169		if (sflag & PS_ALRMPEND) {
170			PROC_LOCK(p);
171			psignal(p, SIGVTALRM);
172			PROC_UNLOCK(p);
173		}
174#if defined(DEV_NPX) && !defined(SMP)
175		if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
176			atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
177			    PCB_NPXTRAP);
178			ucode = npxtrap();
179			if (ucode != -1) {
180				trapsignal(p, SIGFPE, ucode);
181			}
182		}
183#endif
184		if (sflag & PS_PROFPEND) {
185			PROC_LOCK(p);
186			psignal(p, SIGPROF);
187			PROC_UNLOCK(p);
188		}
189
190		userret(td, framep, sticks);
191		mtx_lock(&Giant);
192		crfree(td->td_ucred);
193		mtx_unlock(&Giant);
194		td->td_ucred = NULL;
195		s = cpu_critical_enter();
196	}
197	mtx_assert(&Giant, MA_NOTOWNED);
198	/*
199	 * We need to keep interrupts disabled so that if any further AST's
200	 * come in, the interrupt they come in on will be delayed until we
201	 * finish returning to userland.  We assume that the return to userland
202	 * will perform the equivalent of cpu_critical_exit().
203	 */
204}
205