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