xref: /freebsd/sys/kern/subr_trap.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 #include "opt_mac.h"
42 #ifdef __i386__
43 #include "opt_npx.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/bus.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/mac.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/kse.h>
54 #include <sys/ktr.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/signalvar.h>
58 #include <sys/systm.h>
59 #include <sys/vmmeter.h>
60 #include <machine/cpu.h>
61 #include <machine/pcb.h>
62 
63 /*
64  * Define the code needed before returning to user mode, for
65  * trap and syscall.
66  *
67  * MPSAFE
68  */
69 void
70 userret(td, frame, oticks)
71 	struct thread *td;
72 	struct trapframe *frame;
73 	u_int oticks;
74 {
75 	struct proc *p = td->td_proc;
76 
77 	CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
78             p->p_comm);
79 #ifdef INVARIANTS
80 	/* Check that we called signotify() enough. */
81 	mtx_lock(&Giant);
82 	PROC_LOCK(p);
83 	mtx_lock_spin(&sched_lock);
84 	if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 ||
85 	    (td->td_flags & TDF_ASTPENDING) == 0))
86 		printf("failed to set signal flags properly for ast()\n");
87 	mtx_unlock_spin(&sched_lock);
88 	PROC_UNLOCK(p);
89 	mtx_unlock(&Giant);
90 #endif
91 
92 	/*
93 	 * Let the scheduler adjust our priority etc.
94 	 */
95 	sched_userret(td);
96 
97 	/*
98 	 * We need to check to see if we have to exit or wait due to a
99 	 * single threading requirement or some other STOP condition.
100 	 * Don't bother doing all the work if the stop bits are not set
101 	 * at this time.. If we miss it, we miss it.. no big deal.
102 	 */
103 	if (P_SHOULDSTOP(p)) {
104 		PROC_LOCK(p);
105 		thread_suspend_check(0);	/* Can suspend or kill */
106 		PROC_UNLOCK(p);
107 	}
108 
109 	/*
110 	 * Do special thread processing, e.g. upcall tweaking and such.
111 	 */
112 	if (p->p_flag & P_THREADED) {
113 		thread_userret(td, frame);
114 	}
115 
116 	/*
117 	 * Charge system time if profiling.
118 	 *
119 	 * XXX should move PS_PROFIL to a place that can obviously be
120 	 * accessed safely without sched_lock.
121 	 */
122 	if (p->p_sflag & PS_PROFIL) {
123 		quad_t ticks;
124 
125 		mtx_lock_spin(&sched_lock);
126 		ticks = td->td_sticks - oticks;
127 		mtx_unlock_spin(&sched_lock);
128 		addupc_task(td, TRAPF_PC(frame), (u_int)ticks * psratio);
129 	}
130 }
131 
132 /*
133  * Process an asynchronous software trap.
134  * This is relatively easy.
135  * This function will return with preemption disabled.
136  */
137 void
138 ast(struct trapframe *framep)
139 {
140 	struct thread *td;
141 	struct proc *p;
142 	struct kse *ke;
143 	struct ksegrp *kg;
144 	struct rlimit *rlim;
145 	u_int prticks, sticks;
146 	int sflag;
147 	int flags;
148 	int sig;
149 #if defined(DEV_NPX) && !defined(SMP)
150 	int ucode;
151 #endif
152 
153 	td = curthread;
154 	p = td->td_proc;
155 	kg = td->td_ksegrp;
156 
157 	CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
158             p->p_comm);
159 	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
160 #ifdef WITNESS
161 	if (witness_list(td))
162 		panic("Returning to user mode with mutex(s) held");
163 #endif
164 	mtx_assert(&Giant, MA_NOTOWNED);
165 	mtx_assert(&sched_lock, MA_NOTOWNED);
166 	td->td_frame = framep;
167 
168 	/*
169 	 * This updates the p_sflag's for the checks below in one
170 	 * "atomic" operation with turning off the astpending flag.
171 	 * If another AST is triggered while we are handling the
172 	 * AST's saved in sflag, the astpending flag will be set and
173 	 * ast() will be called again.
174 	 */
175 	mtx_lock_spin(&sched_lock);
176 	ke = td->td_kse;
177 	sticks = td->td_sticks;
178 	flags = td->td_flags;
179 	sflag = p->p_sflag;
180 	p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND | PS_XCPU);
181 #ifdef MAC
182 	p->p_sflag &= ~PS_MACPEND;
183 #endif
184 	td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDRESCHED | TDF_OWEUPC);
185 	cnt.v_soft++;
186 	prticks = 0;
187 	if (flags & TDF_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 & TDF_OWEUPC && sflag & PS_PROFIL)
203 		addupc_task(td, 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 (sflag & PS_XCPU) {
225 		PROC_LOCK(p);
226 		rlim = &p->p_rlimit[RLIMIT_CPU];
227 		if (p->p_runtime.sec >= rlim->rlim_max)
228 			killproc(p, "exceeded maximum CPU limit");
229 		else {
230 			psignal(p, SIGXCPU);
231 			mtx_lock_spin(&sched_lock);
232 			if (p->p_cpulimit < rlim->rlim_max)
233 				p->p_cpulimit += 5;
234 			mtx_unlock_spin(&sched_lock);
235 		}
236 		PROC_UNLOCK(p);
237 	}
238 #ifdef MAC
239 	if (sflag & PS_MACPEND)
240 		mac_thread_userret(td);
241 #endif
242 	if (flags & TDF_NEEDRESCHED) {
243 		mtx_lock_spin(&sched_lock);
244 		sched_prio(td, kg->kg_user_pri);
245 		p->p_stats->p_ru.ru_nivcsw++;
246 		mi_switch();
247 		mtx_unlock_spin(&sched_lock);
248 	}
249 	if (sflag & PS_NEEDSIGCHK) {
250 		int sigs;
251 
252 		sigs = 0;
253 		PROC_LOCK(p);
254 		while ((sig = cursig(td)) != 0) {
255 			postsig(sig);
256 			sigs++;
257 		}
258 		PROC_UNLOCK(p);
259 		if (p->p_flag & P_THREADED && sigs)
260 			thread_signal_upcall(td);
261 	}
262 
263 	userret(td, framep, sticks);
264 #ifdef DIAGNOSTIC
265 	cred_free_thread(td);
266 #endif
267 	mtx_assert(&Giant, MA_NOTOWNED);
268 }
269