xref: /freebsd/sys/kern/subr_trap.c (revision 79065dba2a88baf780767cbb1bb31d4525a08528)
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  * MPSAFE
63  */
64 void
65 userret(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 
74 #ifdef INVARIANTS
75 	/* Check that we called signotify() enough. */
76 	mtx_lock(&Giant);
77 	PROC_LOCK(p);
78 	mtx_lock_spin(&sched_lock);
79 	if (SIGPENDING(p) && ((p->p_sflag & PS_NEEDSIGCHK) == 0 ||
80 	    (p->p_kse.ke_flags & KEF_ASTPENDING) == 0))
81 		printf("failed to set signal flags proprly for ast()\n");
82 	mtx_unlock_spin(&sched_lock);
83 	PROC_UNLOCK(p);
84 	mtx_unlock(&Giant);
85 #endif
86 
87 	/*
88 	 * XXX we cheat slightly on the locking here to avoid locking in
89 	 * the usual case.  Setting td_priority here is essentially an
90 	 * incomplete workaround for not setting it properly elsewhere.
91 	 * Now that some interrupt handlers are threads, not setting it
92 	 * properly elsewhere can clobber it in the window between setting
93 	 * it here and returning to user mode, so don't waste time setting
94 	 * it perfectly here.
95 	 */
96 	if (td->td_priority != kg->kg_user_pri) {
97 		mtx_lock_spin(&sched_lock);
98 		td->td_priority = kg->kg_user_pri;
99 		mtx_unlock_spin(&sched_lock);
100 	}
101 
102 	/*
103 	 * Charge system time if profiling.
104 	 *
105 	 * XXX should move PS_PROFIL to a place that can obviously be
106 	 * accessed safely without sched_lock.
107 	 */
108 	if (p->p_sflag & PS_PROFIL) {
109 		quad_t ticks;
110 
111 		mtx_lock_spin(&sched_lock);
112 		ticks = ke->ke_sticks - oticks;
113 		mtx_unlock_spin(&sched_lock);
114 		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
115 	}
116 }
117 
118 /*
119  * Process an asynchronous software trap.
120  * This is relatively easy.
121  * This function will return with preemption disabled.
122  */
123 void
124 ast(framep)
125 	struct trapframe *framep;
126 {
127 	struct thread *td = curthread;
128 	struct proc *p = td->td_proc;
129 	struct kse *ke = td->td_kse;
130 	struct ksegrp *kg = td->td_ksegrp;
131 	u_int prticks, sticks;
132 	int sflag;
133 	int flags;
134 	int sig;
135 #if defined(DEV_NPX) && !defined(SMP)
136 	int ucode;
137 #endif
138 
139 	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
140 #ifdef WITNESS
141 	if (witness_list(td))
142 		panic("Returning to user mode with mutex(s) held");
143 #endif
144 	mtx_assert(&Giant, MA_NOTOWNED);
145 	mtx_assert(&sched_lock, MA_NOTOWNED);
146 	prticks = 0;		/* XXX: Quiet warning. */
147 	td->td_frame = framep;
148 	/*
149 	 * This updates the p_sflag's for the checks below in one
150 	 * "atomic" operation with turning off the astpending flag.
151 	 * If another AST is triggered while we are handling the
152 	 * AST's saved in sflag, the astpending flag will be set and
153 	 * ast() will be called again.
154 	 */
155 	mtx_lock_spin(&sched_lock);
156 	sticks = ke->ke_sticks;
157 	sflag = p->p_sflag;
158 	flags = ke->ke_flags;
159 	p->p_sflag &= ~(PS_ALRMPEND | PS_NEEDSIGCHK | PS_PROFPEND);
160 	ke->ke_flags &= ~(KEF_ASTPENDING | KEF_NEEDRESCHED | KEF_OWEUPC);
161 	cnt.v_soft++;
162 	if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
163 		prticks = p->p_stats->p_prof.pr_ticks;
164 		p->p_stats->p_prof.pr_ticks = 0;
165 	}
166 	mtx_unlock_spin(&sched_lock);
167 
168 	if (td->td_ucred != p->p_ucred)
169 		cred_update_thread(td);
170 	if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
171 		addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
172 	if (sflag & PS_ALRMPEND) {
173 		PROC_LOCK(p);
174 		psignal(p, SIGVTALRM);
175 		PROC_UNLOCK(p);
176 	}
177 #if defined(DEV_NPX) && !defined(SMP)
178 	if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
179 		atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
180 		    PCB_NPXTRAP);
181 		ucode = npxtrap();
182 		if (ucode != -1) {
183 			trapsignal(p, SIGFPE, ucode);
184 		}
185 	}
186 #endif
187 	if (sflag & PS_PROFPEND) {
188 		PROC_LOCK(p);
189 		psignal(p, SIGPROF);
190 		PROC_UNLOCK(p);
191 	}
192 	if (flags & KEF_NEEDRESCHED) {
193 		mtx_lock_spin(&sched_lock);
194 		td->td_priority = kg->kg_user_pri;
195 		setrunqueue(td);
196 		p->p_stats->p_ru.ru_nivcsw++;
197 		mi_switch();
198 		mtx_unlock_spin(&sched_lock);
199 	}
200 	if (sflag & PS_NEEDSIGCHK) {
201 		mtx_lock(&Giant);
202 		PROC_LOCK(p);
203 		while ((sig = CURSIG(p)) != 0)
204 			postsig(sig);
205 		PROC_UNLOCK(p);
206 		mtx_unlock(&Giant);
207 	}
208 
209 	userret(td, framep, sticks);
210 #ifdef DIAGNOSTIC
211 	cred_free_thread(td);
212 #endif
213 	mtx_assert(&Giant, MA_NOTOWNED);
214 }
215