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