xref: /freebsd/sys/kern/subr_trap.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
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 	int sig;
74 
75 	mtx_lock(&Giant);
76 	PROC_LOCK(p);
77 	while ((sig = CURSIG(p)) != 0)
78 		postsig(sig);
79 	PROC_UNLOCK(p);
80 	mtx_unlock(&Giant);
81 
82 	mtx_lock_spin(&sched_lock);
83 	kg->kg_pri.pri_level = kg->kg_pri.pri_user;
84 	if (ke->ke_flags & KEF_NEEDRESCHED) {
85 		/*
86 		 * Since we are curproc, a clock interrupt could
87 		 * change our priority without changing run queues
88 		 * (the running process is not kept on a run queue).
89 		 * If this happened after we setrunqueue ourselves but
90 		 * before we switch()'ed, we might not be on the queue
91 		 * indicated by our priority.
92 		 */
93 		DROP_GIANT_NOSWITCH();
94 		setrunqueue(td);
95 		p->p_stats->p_ru.ru_nivcsw++;
96 		mi_switch();
97 		mtx_unlock_spin(&sched_lock);
98 		PICKUP_GIANT();
99 		mtx_lock(&Giant);
100 		PROC_LOCK(p);
101 		while ((sig = CURSIG(p)) != 0)
102 			postsig(sig);
103 		mtx_unlock(&Giant);
104 		PROC_UNLOCK(p);
105 		mtx_lock_spin(&sched_lock);
106 	}
107 
108 	/*
109 	 * Charge system time if profiling.
110 	 */
111 	if (p->p_sflag & PS_PROFIL) {
112 		quad_t ticks;
113 
114 		ticks = ke->ke_sticks - oticks;
115 		mtx_unlock_spin(&sched_lock);
116 		addupc_task(ke, TRAPF_PC(frame), (u_int)ticks * psratio);
117 	} else
118 		mtx_unlock_spin(&sched_lock);
119 }
120 
121 /*
122  * Process an asynchronous software trap.
123  * This is relatively easy.
124  * This function will return with preemption disabled.
125  */
126 void
127 ast(framep)
128 	struct trapframe *framep;
129 {
130 	struct thread *td = curthread;
131 	struct proc *p = td->td_proc;
132 	struct kse *ke = td->td_kse;
133 	u_int prticks, sticks;
134 	critical_t s;
135 	int sflag;
136 	int flags;
137 #if defined(DEV_NPX) && !defined(SMP)
138 	int ucode;
139 #endif
140 
141 	KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
142 	KASSERT(td->td_ucred == NULL, ("leaked ucred"));
143 #ifdef WITNESS
144 	if (witness_list(td))
145 		panic("Returning to user mode with mutex(s) held");
146 #endif
147 	mtx_assert(&Giant, MA_NOTOWNED);
148 	prticks = 0;		/* XXX: Quiet warning. */
149 	s = cpu_critical_enter();
150 	while ((ke->ke_flags & (KEF_ASTPENDING | KEF_NEEDRESCHED)) != 0) {
151 		cpu_critical_exit(s);
152 		td->td_frame = framep;
153 		/*
154 		 * This updates the p_sflag's for the checks below in one
155 		 * "atomic" operation with turning off the astpending flag.
156 		 * If another AST is triggered while we are handling the
157 		 * AST's saved in sflag, the astpending flag will be set and
158 		 * we will loop again.
159 		 */
160 		mtx_lock_spin(&sched_lock);
161 		sticks = ke->ke_sticks;
162 		sflag = p->p_sflag;
163 		flags = ke->ke_flags;
164 		p->p_sflag &= ~(PS_PROFPEND | PS_ALRMPEND);
165 		ke->ke_flags &= ~(KEF_OWEUPC | KEF_ASTPENDING);
166 		cnt.v_soft++;
167 		if (flags & KEF_OWEUPC && sflag & PS_PROFIL) {
168 			prticks = p->p_stats->p_prof.pr_ticks;
169 			p->p_stats->p_prof.pr_ticks = 0;
170 		}
171 		mtx_unlock_spin(&sched_lock);
172 		PROC_LOCK(p);
173 		td->td_ucred = crhold(p->p_ucred);
174 		PROC_UNLOCK(p);
175 		if (flags & KEF_OWEUPC && sflag & PS_PROFIL)
176 			addupc_task(ke, p->p_stats->p_prof.pr_addr, prticks);
177 		if (sflag & PS_ALRMPEND) {
178 			PROC_LOCK(p);
179 			psignal(p, SIGVTALRM);
180 			PROC_UNLOCK(p);
181 		}
182 #if defined(DEV_NPX) && !defined(SMP)
183 		if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
184 			atomic_clear_char(&PCPU_GET(curpcb)->pcb_flags,
185 			    PCB_NPXTRAP);
186 			ucode = npxtrap();
187 			if (ucode != -1) {
188 				trapsignal(p, SIGFPE, ucode);
189 			}
190 		}
191 #endif
192 		if (sflag & PS_PROFPEND) {
193 			PROC_LOCK(p);
194 			psignal(p, SIGPROF);
195 			PROC_UNLOCK(p);
196 		}
197 
198 		userret(td, framep, sticks);
199 		mtx_lock(&Giant);
200 		crfree(td->td_ucred);
201 		mtx_unlock(&Giant);
202 		td->td_ucred = NULL;
203 		s = cpu_critical_enter();
204 	}
205 	mtx_assert(&Giant, MA_NOTOWNED);
206 	/*
207 	 * We need to keep interrupts disabled so that if any further AST's
208 	 * come in, the interrupt they come in on will be delayed until we
209 	 * finish returning to userland.  We assume that the return to userland
210 	 * will perform the equivalent of cpu_critical_exit().
211 	 */
212 }
213