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