subr_syscall.c revision 79125
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 79125 2001-07-03 05:27:53Z jhb $
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 */
62void
63userret(p, frame, oticks)
64	struct proc *p;
65	struct trapframe *frame;
66	u_quad_t oticks;
67{
68	int sig;
69
70	mtx_lock(&Giant);
71	PROC_LOCK(p);
72	while ((sig = CURSIG(p)) != 0)
73		postsig(sig);
74	mtx_unlock(&Giant);
75
76	mtx_lock_spin(&sched_lock);
77	PROC_UNLOCK_NOSWITCH(p);
78	p->p_pri.pri_level = p->p_pri.pri_user;
79	if (resched_wanted(p)) {
80		/*
81		 * Since we are curproc, a clock interrupt could
82		 * change our priority without changing run queues
83		 * (the running process is not kept on a run queue).
84		 * If this happened after we setrunqueue ourselves but
85		 * before we switch()'ed, we might not be on the queue
86		 * indicated by our priority.
87		 */
88		DROP_GIANT_NOSWITCH();
89		setrunqueue(p);
90		p->p_stats->p_ru.ru_nivcsw++;
91		mi_switch();
92		mtx_unlock_spin(&sched_lock);
93		PICKUP_GIANT();
94		PROC_LOCK(p);
95		while ((sig = CURSIG(p)) != 0)
96			postsig(sig);
97		mtx_lock_spin(&sched_lock);
98		PROC_UNLOCK_NOSWITCH(p);
99	}
100
101	/*
102	 * Charge system time if profiling.
103	 */
104	if (p->p_sflag & PS_PROFIL) {
105		mtx_unlock_spin(&sched_lock);
106		addupc_task(p, TRAPF_PC(frame),
107			    (u_int)(p->p_sticks - oticks) * psratio);
108	} else
109		mtx_unlock_spin(&sched_lock);
110}
111
112/*
113 * Process an asynchronous software trap.
114 * This is relatively easy.
115 */
116void
117ast(framep)
118	struct trapframe *framep;
119{
120	struct proc *p = CURPROC;
121	u_quad_t sticks;
122#if defined(DEV_NPX) && !defined(SMP)
123	int ucode;
124#endif
125
126	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
127
128	/*
129	 * We check for a pending AST here rather than in the assembly as
130	 * acquiring and releasing mutexes in assembly is not fun.
131	 */
132	mtx_lock_spin(&sched_lock);
133	if (!(astpending(p) || resched_wanted(p))) {
134		mtx_unlock_spin(&sched_lock);
135		return;
136	}
137
138	sticks = p->p_sticks;
139	p->p_frame = framep;
140
141	astoff(p);
142	cnt.v_soft++;
143	mtx_intr_enable(&sched_lock);
144	if (p->p_sflag & PS_OWEUPC) {
145		p->p_sflag &= ~PS_OWEUPC;
146		mtx_unlock_spin(&sched_lock);
147		mtx_lock(&Giant);
148		addupc_task(p, p->p_stats->p_prof.pr_addr,
149			    p->p_stats->p_prof.pr_ticks);
150		mtx_lock_spin(&sched_lock);
151	}
152	if (p->p_sflag & PS_ALRMPEND) {
153		p->p_sflag &= ~PS_ALRMPEND;
154		mtx_unlock_spin(&sched_lock);
155		PROC_LOCK(p);
156		psignal(p, SIGVTALRM);
157		PROC_UNLOCK(p);
158		mtx_lock_spin(&sched_lock);
159	}
160#if defined(DEV_NPX) && !defined(SMP)
161	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
162		PCPU_GET(curpcb)->pcb_flags &= ~PCB_NPXTRAP;
163		mtx_unlock_spin(&sched_lock);
164		ucode = npxtrap();
165		if (ucode != -1) {
166			if (!mtx_owned(&Giant))
167				mtx_lock(&Giant);
168			trapsignal(p, SIGFPE, ucode);
169		}
170		mtx_lock_spin(&sched_lock);
171	}
172#endif
173	if (p->p_sflag & PS_PROFPEND) {
174		p->p_sflag &= ~PS_PROFPEND;
175		mtx_unlock_spin(&sched_lock);
176		PROC_LOCK(p);
177		psignal(p, SIGPROF);
178		PROC_UNLOCK(p);
179	} else
180		mtx_unlock_spin(&sched_lock);
181
182	userret(p, framep, sticks);
183
184	if (mtx_owned(&Giant))
185		mtx_unlock(&Giant);
186}
187