The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_trap.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: c273359295f222159406cc2dd06a6af5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.