xref: /freebsd/sys/kern/subr_trap.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
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  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "opt_ktrace.h"
44 #include "opt_mac.h"
45 #ifdef __i386__
46 #include "opt_npx.h"
47 #endif
48 
49 #include <sys/param.h>
50 #include <sys/bus.h>
51 #include <sys/kernel.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/ktr.h>
56 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
58 #include <sys/signalvar.h>
59 #include <sys/systm.h>
60 #include <sys/vmmeter.h>
61 #ifdef KTRACE
62 #include <sys/uio.h>
63 #include <sys/ktrace.h>
64 #endif
65 
66 #include <machine/cpu.h>
67 #include <machine/pcb.h>
68 
69 #include <security/mac/mac_framework.h>
70 
71 /*
72  * Define the code needed before returning to user mode, for
73  * trap and syscall.
74  *
75  * MPSAFE
76  */
77 void
78 userret(struct thread *td, struct trapframe *frame)
79 {
80 	struct proc *p = td->td_proc;
81 
82 	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
83             p->p_comm);
84 #ifdef DIAGNOSTIC
85 	/* Check that we called signotify() enough. */
86 	PROC_LOCK(p);
87 	mtx_lock_spin(&sched_lock);
88 	if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
89 	    (td->td_flags & TDF_ASTPENDING) == 0))
90 		printf("failed to set signal flags properly for ast()\n");
91 	mtx_unlock_spin(&sched_lock);
92 	PROC_UNLOCK(p);
93 #endif
94 
95 #ifdef KTRACE
96 	KTRUSERRET(td);
97 #endif
98 
99 	/*
100 	 * If this thread tickled GEOM, we need to wait for the giggling to
101 	 * stop before we return to userland
102 	 */
103 	if (td->td_pflags & TDP_GEOM)
104 		g_waitidle();
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 #ifdef KSE
119 	/*
120 	 * Do special thread processing, e.g. upcall tweaking and such.
121 	 */
122 	if (p->p_flag & P_SA)
123 		thread_userret(td, frame);
124 #endif
125 
126 	/*
127 	 * Charge system time if profiling.
128 	 */
129 	if (p->p_flag & P_PROFIL) {
130 
131 		addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
132 	}
133 
134 	/*
135 	 * Let the scheduler adjust our priority etc.
136 	 */
137 	sched_userret(td);
138 	KASSERT(td->td_locks == 0,
139 	    ("userret: Returning with %d locks held.", td->td_locks));
140 }
141 
142 /*
143  * Process an asynchronous software trap.
144  * This is relatively easy.
145  * This function will return with preemption disabled.
146  */
147 void
148 ast(struct trapframe *framep)
149 {
150 	struct thread *td;
151 	struct proc *p;
152 	struct rlimit rlim;
153 	int sflag;
154 	int flags;
155 	int sig;
156 #if defined(DEV_NPX) && !defined(SMP)
157 	int ucode;
158 	ksiginfo_t ksi;
159 #endif
160 
161 	td = curthread;
162 	p = td->td_proc;
163 
164 	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
165             p->p_comm);
166 	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
167 	WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
168 	mtx_assert(&Giant, MA_NOTOWNED);
169 	mtx_assert(&sched_lock, MA_NOTOWNED);
170 	td->td_frame = framep;
171 	td->td_pticks = 0;
172 
173 #ifdef KSE
174 	if ((p->p_flag & P_SA) && (td->td_mailbox == NULL))
175 		thread_user_enter(td);
176 #endif
177 
178 	/*
179 	 * This updates the p_sflag's for the checks below in one
180 	 * "atomic" operation with turning off the astpending flag.
181 	 * If another AST is triggered while we are handling the
182 	 * AST's saved in sflag, the astpending flag will be set and
183 	 * ast() will be called again.
184 	 */
185 	mtx_lock_spin(&sched_lock);
186 	flags = td->td_flags;
187 	sflag = p->p_sflag;
188 	if (p->p_sflag & (PS_ALRMPEND | PS_PROFPEND | PS_XCPU))
189 		p->p_sflag &= ~(PS_ALRMPEND | PS_PROFPEND | PS_XCPU);
190 #ifdef MAC
191 	if (p->p_sflag & PS_MACPEND)
192 		p->p_sflag &= ~PS_MACPEND;
193 #endif
194 	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK |
195 	    TDF_NEEDRESCHED | TDF_INTERRUPT);
196 	cnt.v_trap++;
197 	mtx_unlock_spin(&sched_lock);
198 
199 	/*
200 	 * XXXKSE While the fact that we owe a user profiling
201 	 * tick is stored per thread in this code, the statistics
202 	 * themselves are still stored per process.
203 	 * This should probably change, by which I mean that
204 	 * possibly the location of both might change.
205 	 */
206 	if (td->td_ucred != p->p_ucred)
207 		cred_update_thread(td);
208 	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
209 		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
210 		td->td_profil_ticks = 0;
211 		td->td_pflags &= ~TDP_OWEUPC;
212 	}
213 	if (sflag & PS_ALRMPEND) {
214 		PROC_LOCK(p);
215 		psignal(p, SIGVTALRM);
216 		PROC_UNLOCK(p);
217 	}
218 #if defined(DEV_NPX) && !defined(SMP)
219 	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
220 		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
221 		    PCB_NPXTRAP);
222 		ucode = npxtrap();
223 		if (ucode != -1) {
224 			ksiginfo_init_trap(&ksi);
225 			ksi.ksi_signo = SIGFPE;
226 			ksi.ksi_code = ucode;
227 			trapsignal(td, &ksi);
228 		}
229 	}
230 #endif
231 	if (sflag & PS_PROFPEND) {
232 		PROC_LOCK(p);
233 		psignal(p, SIGPROF);
234 		PROC_UNLOCK(p);
235 	}
236 	if (sflag & PS_XCPU) {
237 		PROC_LOCK(p);
238 		lim_rlimit(p, RLIMIT_CPU, &rlim);
239 		mtx_lock_spin(&sched_lock);
240 		if (p->p_rux.rux_runtime >= rlim.rlim_max * cpu_tickrate()) {
241 			mtx_unlock_spin(&sched_lock);
242 			killproc(p, "exceeded maximum CPU limit");
243 		} else {
244 			if (p->p_cpulimit < rlim.rlim_max)
245 				p->p_cpulimit += 5;
246 			mtx_unlock_spin(&sched_lock);
247 			psignal(p, SIGXCPU);
248 		}
249 		PROC_UNLOCK(p);
250 	}
251 #ifdef MAC
252 	if (sflag & PS_MACPEND)
253 		mac_thread_userret(td);
254 #endif
255 	if (flags & TDF_NEEDRESCHED) {
256 #ifdef KTRACE
257 		if (KTRPOINT(td, KTR_CSW))
258 			ktrcsw(1, 1);
259 #endif
260 		mtx_lock_spin(&sched_lock);
261 		sched_prio(td, td->td_user_pri);
262 		mi_switch(SW_INVOL, NULL);
263 		mtx_unlock_spin(&sched_lock);
264 #ifdef KTRACE
265 		if (KTRPOINT(td, KTR_CSW))
266 			ktrcsw(0, 1);
267 #endif
268 	}
269 	if (flags & TDF_NEEDSIGCHK) {
270 		PROC_LOCK(p);
271 		mtx_lock(&p->p_sigacts->ps_mtx);
272 		while ((sig = cursig(td)) != 0)
273 			postsig(sig);
274 		mtx_unlock(&p->p_sigacts->ps_mtx);
275 		PROC_UNLOCK(p);
276 	}
277 
278 	userret(td, framep);
279 	mtx_assert(&Giant, MA_NOTOWNED);
280 }
281