Home | History | Annotate | Line # | Download | only in kern
kern_synch.c revision 1.268
      1 /*	$NetBSD: kern_synch.c,v 1.268 2009/10/03 01:30:25 elad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2009
      5  *    The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  * NASA Ames Research Center, by Charles M. Hannum, Andrew Doran and
     11  * Daniel Sieger.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1982, 1986, 1990, 1991, 1993
     37  *	The Regents of the University of California.  All rights reserved.
     38  * (c) UNIX System Laboratories, Inc.
     39  * All or some portions of this file are derived from material licensed
     40  * to the University of California by American Telephone and Telegraph
     41  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     42  * the permission of UNIX System Laboratories, Inc.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.268 2009/10/03 01:30:25 elad Exp $");
     73 
     74 #include "opt_kstack.h"
     75 #include "opt_perfctrs.h"
     76 #include "opt_sa.h"
     77 
     78 #define	__MUTEX_PRIVATE
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/proc.h>
     83 #include <sys/kernel.h>
     84 #if defined(PERFCTRS)
     85 #include <sys/pmc.h>
     86 #endif
     87 #include <sys/cpu.h>
     88 #include <sys/resourcevar.h>
     89 #include <sys/sched.h>
     90 #include <sys/sa.h>
     91 #include <sys/savar.h>
     92 #include <sys/syscall_stats.h>
     93 #include <sys/sleepq.h>
     94 #include <sys/lockdebug.h>
     95 #include <sys/evcnt.h>
     96 #include <sys/intr.h>
     97 #include <sys/lwpctl.h>
     98 #include <sys/atomic.h>
     99 #include <sys/simplelock.h>
    100 #include <sys/kauth.h>
    101 
    102 #include <uvm/uvm_extern.h>
    103 
    104 #include <dev/lockstat.h>
    105 
    106 static u_int	sched_unsleep(struct lwp *, bool);
    107 static void	sched_changepri(struct lwp *, pri_t);
    108 static void	sched_lendpri(struct lwp *, pri_t);
    109 static void	resched_cpu(struct lwp *);
    110 
    111 syncobj_t sleep_syncobj = {
    112 	SOBJ_SLEEPQ_SORTED,
    113 	sleepq_unsleep,
    114 	sleepq_changepri,
    115 	sleepq_lendpri,
    116 	syncobj_noowner,
    117 };
    118 
    119 syncobj_t sched_syncobj = {
    120 	SOBJ_SLEEPQ_SORTED,
    121 	sched_unsleep,
    122 	sched_changepri,
    123 	sched_lendpri,
    124 	syncobj_noowner,
    125 };
    126 
    127 callout_t 	sched_pstats_ch;
    128 unsigned	sched_pstats_ticks;
    129 kcondvar_t	lbolt;			/* once a second sleep address */
    130 
    131 kauth_listener_t	sched_listener;
    132 
    133 /* Preemption event counters */
    134 static struct evcnt kpreempt_ev_crit;
    135 static struct evcnt kpreempt_ev_klock;
    136 static struct evcnt kpreempt_ev_immed;
    137 
    138 /*
    139  * During autoconfiguration or after a panic, a sleep will simply lower the
    140  * priority briefly to allow interrupts, then return.  The priority to be
    141  * used (safepri) is machine-dependent, thus this value is initialized and
    142  * maintained in the machine-dependent layers.  This priority will typically
    143  * be 0, or the lowest priority that is safe for use on the interrupt stack;
    144  * it can be made higher to block network software interrupts after panics.
    145  */
    146 int	safepri;
    147 
    148 static int
    149 sched_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    150     void *arg0, void *arg1, void *arg2, void *arg3)
    151 {
    152 	struct proc *p;
    153 	int result;
    154 
    155 	result = KAUTH_RESULT_DEFER;
    156 	p = arg0;
    157 
    158 	switch (action) {
    159 	case KAUTH_PROCESS_SCHEDULER_GETPARAM:
    160 		if (kauth_cred_uidmatch(cred, p->p_cred))
    161 			result = KAUTH_RESULT_ALLOW;
    162 		break;
    163 
    164 	case KAUTH_PROCESS_SCHEDULER_SETPARAM:
    165 		if (kauth_cred_uidmatch(cred, p->p_cred)) {
    166 			struct lwp *l;
    167 			int policy;
    168 			pri_t priority;
    169 
    170 			l = arg1;
    171 			policy = (int)(unsigned long)arg2;
    172 			priority = (pri_t)(unsigned long)arg3;
    173 
    174 			if ((policy == l->l_class ||
    175 			    (policy != SCHED_FIFO && policy != SCHED_RR)) &&
    176 			    priority <= l->l_priority)
    177 				result = KAUTH_RESULT_ALLOW;
    178 		}
    179 
    180 		break;
    181 
    182 	case KAUTH_PROCESS_SCHEDULER_GETAFFINITY:
    183 		result = KAUTH_RESULT_ALLOW;
    184 		break;
    185 
    186 	case KAUTH_PROCESS_SCHEDULER_SETAFFINITY:
    187 		/* Privileged; we let the secmodel handle this. */
    188 		break;
    189 
    190 	default:
    191 		break;
    192 	}
    193 
    194 	return result;
    195 }
    196 
    197 void
    198 sched_init(void)
    199 {
    200 
    201 	cv_init(&lbolt, "lbolt");
    202 	callout_init(&sched_pstats_ch, CALLOUT_MPSAFE);
    203 	callout_setfunc(&sched_pstats_ch, sched_pstats, NULL);
    204 
    205 	evcnt_attach_dynamic(&kpreempt_ev_crit, EVCNT_TYPE_MISC, NULL,
    206 	   "kpreempt", "defer: critical section");
    207 	evcnt_attach_dynamic(&kpreempt_ev_klock, EVCNT_TYPE_MISC, NULL,
    208 	   "kpreempt", "defer: kernel_lock");
    209 	evcnt_attach_dynamic(&kpreempt_ev_immed, EVCNT_TYPE_MISC, NULL,
    210 	   "kpreempt", "immediate");
    211 
    212 	sched_pstats(NULL);
    213 
    214 	sched_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    215 	    sched_listener_cb, NULL);
    216 }
    217 
    218 /*
    219  * OBSOLETE INTERFACE
    220  *
    221  * General sleep call.  Suspends the current LWP until a wakeup is
    222  * performed on the specified identifier.  The LWP will then be made
    223  * runnable with the specified priority.  Sleeps at most timo/hz seconds (0
    224  * means no timeout).  If pri includes PCATCH flag, signals are checked
    225  * before and after sleeping, else signals are not checked.  Returns 0 if
    226  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
    227  * signal needs to be delivered, ERESTART is returned if the current system
    228  * call should be restarted if possible, and EINTR is returned if the system
    229  * call should be interrupted by the signal (return EINTR).
    230  *
    231  * The interlock is held until we are on a sleep queue. The interlock will
    232  * be locked before returning back to the caller unless the PNORELOCK flag
    233  * is specified, in which case the interlock will always be unlocked upon
    234  * return.
    235  */
    236 int
    237 ltsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
    238 	volatile struct simplelock *interlock)
    239 {
    240 	struct lwp *l = curlwp;
    241 	sleepq_t *sq;
    242 	kmutex_t *mp;
    243 	int error;
    244 
    245 	KASSERT((l->l_pflag & LP_INTR) == 0);
    246 
    247 	if (sleepq_dontsleep(l)) {
    248 		(void)sleepq_abort(NULL, 0);
    249 		if ((priority & PNORELOCK) != 0)
    250 			simple_unlock(interlock);
    251 		return 0;
    252 	}
    253 
    254 	l->l_kpriority = true;
    255 	sq = sleeptab_lookup(&sleeptab, ident, &mp);
    256 	sleepq_enter(sq, l, mp);
    257 	sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj);
    258 
    259 	if (interlock != NULL) {
    260 		KASSERT(simple_lock_held(interlock));
    261 		simple_unlock(interlock);
    262 	}
    263 
    264 	error = sleepq_block(timo, priority & PCATCH);
    265 
    266 	if (interlock != NULL && (priority & PNORELOCK) == 0)
    267 		simple_lock(interlock);
    268 
    269 	return error;
    270 }
    271 
    272 int
    273 mtsleep(wchan_t ident, pri_t priority, const char *wmesg, int timo,
    274 	kmutex_t *mtx)
    275 {
    276 	struct lwp *l = curlwp;
    277 	sleepq_t *sq;
    278 	kmutex_t *mp;
    279 	int error;
    280 
    281 	KASSERT((l->l_pflag & LP_INTR) == 0);
    282 
    283 	if (sleepq_dontsleep(l)) {
    284 		(void)sleepq_abort(mtx, (priority & PNORELOCK) != 0);
    285 		return 0;
    286 	}
    287 
    288 	l->l_kpriority = true;
    289 	sq = sleeptab_lookup(&sleeptab, ident, &mp);
    290 	sleepq_enter(sq, l, mp);
    291 	sleepq_enqueue(sq, ident, wmesg, &sleep_syncobj);
    292 	mutex_exit(mtx);
    293 	error = sleepq_block(timo, priority & PCATCH);
    294 
    295 	if ((priority & PNORELOCK) == 0)
    296 		mutex_enter(mtx);
    297 
    298 	return error;
    299 }
    300 
    301 /*
    302  * General sleep call for situations where a wake-up is not expected.
    303  */
    304 int
    305 kpause(const char *wmesg, bool intr, int timo, kmutex_t *mtx)
    306 {
    307 	struct lwp *l = curlwp;
    308 	kmutex_t *mp;
    309 	sleepq_t *sq;
    310 	int error;
    311 
    312 	if (sleepq_dontsleep(l))
    313 		return sleepq_abort(NULL, 0);
    314 
    315 	if (mtx != NULL)
    316 		mutex_exit(mtx);
    317 	l->l_kpriority = true;
    318 	sq = sleeptab_lookup(&sleeptab, l, &mp);
    319 	sleepq_enter(sq, l, mp);
    320 	sleepq_enqueue(sq, l, wmesg, &sleep_syncobj);
    321 	error = sleepq_block(timo, intr);
    322 	if (mtx != NULL)
    323 		mutex_enter(mtx);
    324 
    325 	return error;
    326 }
    327 
    328 #ifdef KERN_SA
    329 /*
    330  * sa_awaken:
    331  *
    332  *	We believe this lwp is an SA lwp. If it's yielding,
    333  * let it know it needs to wake up.
    334  *
    335  *	We are called and exit with the lwp locked. We are
    336  * called in the middle of wakeup operations, so we need
    337  * to not touch the locks at all.
    338  */
    339 void
    340 sa_awaken(struct lwp *l)
    341 {
    342 	/* LOCK_ASSERT(lwp_locked(l, NULL)); */
    343 
    344 	if (l == l->l_savp->savp_lwp && l->l_flag & LW_SA_YIELD)
    345 		l->l_flag &= ~LW_SA_IDLE;
    346 }
    347 #endif /* KERN_SA */
    348 
    349 /*
    350  * OBSOLETE INTERFACE
    351  *
    352  * Make all LWPs sleeping on the specified identifier runnable.
    353  */
    354 void
    355 wakeup(wchan_t ident)
    356 {
    357 	sleepq_t *sq;
    358 	kmutex_t *mp;
    359 
    360 	if (__predict_false(cold))
    361 		return;
    362 
    363 	sq = sleeptab_lookup(&sleeptab, ident, &mp);
    364 	sleepq_wake(sq, ident, (u_int)-1, mp);
    365 }
    366 
    367 /*
    368  * OBSOLETE INTERFACE
    369  *
    370  * Make the highest priority LWP first in line on the specified
    371  * identifier runnable.
    372  */
    373 void
    374 wakeup_one(wchan_t ident)
    375 {
    376 	sleepq_t *sq;
    377 	kmutex_t *mp;
    378 
    379 	if (__predict_false(cold))
    380 		return;
    381 
    382 	sq = sleeptab_lookup(&sleeptab, ident, &mp);
    383 	sleepq_wake(sq, ident, 1, mp);
    384 }
    385 
    386 
    387 /*
    388  * General yield call.  Puts the current LWP back on its run queue and
    389  * performs a voluntary context switch.  Should only be called when the
    390  * current LWP explicitly requests it (eg sched_yield(2)).
    391  */
    392 void
    393 yield(void)
    394 {
    395 	struct lwp *l = curlwp;
    396 
    397 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
    398 	lwp_lock(l);
    399 	KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
    400 	KASSERT(l->l_stat == LSONPROC);
    401 	l->l_kpriority = false;
    402 	(void)mi_switch(l);
    403 	KERNEL_LOCK(l->l_biglocks, l);
    404 }
    405 
    406 /*
    407  * General preemption call.  Puts the current LWP back on its run queue
    408  * and performs an involuntary context switch.
    409  */
    410 void
    411 preempt(void)
    412 {
    413 	struct lwp *l = curlwp;
    414 
    415 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
    416 	lwp_lock(l);
    417 	KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_lwplock));
    418 	KASSERT(l->l_stat == LSONPROC);
    419 	l->l_kpriority = false;
    420 	l->l_nivcsw++;
    421 	(void)mi_switch(l);
    422 	KERNEL_LOCK(l->l_biglocks, l);
    423 }
    424 
    425 /*
    426  * Handle a request made by another agent to preempt the current LWP
    427  * in-kernel.  Usually called when l_dopreempt may be non-zero.
    428  *
    429  * Character addresses for lockstat only.
    430  */
    431 static char	in_critical_section;
    432 static char	kernel_lock_held;
    433 static char	is_softint;
    434 static char	cpu_kpreempt_enter_fail;
    435 
    436 bool
    437 kpreempt(uintptr_t where)
    438 {
    439 	uintptr_t failed;
    440 	lwp_t *l;
    441 	int s, dop, lsflag;
    442 
    443 	l = curlwp;
    444 	failed = 0;
    445 	while ((dop = l->l_dopreempt) != 0) {
    446 		if (l->l_stat != LSONPROC) {
    447 			/*
    448 			 * About to block (or die), let it happen.
    449 			 * Doesn't really count as "preemption has
    450 			 * been blocked", since we're going to
    451 			 * context switch.
    452 			 */
    453 			l->l_dopreempt = 0;
    454 			return true;
    455 		}
    456 		if (__predict_false((l->l_flag & LW_IDLE) != 0)) {
    457 			/* Can't preempt idle loop, don't count as failure. */
    458 			l->l_dopreempt = 0;
    459 			return true;
    460 		}
    461 		if (__predict_false(l->l_nopreempt != 0)) {
    462 			/* LWP holds preemption disabled, explicitly. */
    463 			if ((dop & DOPREEMPT_COUNTED) == 0) {
    464 				kpreempt_ev_crit.ev_count++;
    465 			}
    466 			failed = (uintptr_t)&in_critical_section;
    467 			break;
    468 		}
    469 		if (__predict_false((l->l_pflag & LP_INTR) != 0)) {
    470 			/* Can't preempt soft interrupts yet. */
    471 			l->l_dopreempt = 0;
    472 			failed = (uintptr_t)&is_softint;
    473 			break;
    474 		}
    475 		s = splsched();
    476 		if (__predict_false(l->l_blcnt != 0 ||
    477 		    curcpu()->ci_biglock_wanted != NULL)) {
    478 			/* Hold or want kernel_lock, code is not MT safe. */
    479 			splx(s);
    480 			if ((dop & DOPREEMPT_COUNTED) == 0) {
    481 				kpreempt_ev_klock.ev_count++;
    482 			}
    483 			failed = (uintptr_t)&kernel_lock_held;
    484 			break;
    485 		}
    486 		if (__predict_false(!cpu_kpreempt_enter(where, s))) {
    487 			/*
    488 			 * It may be that the IPL is too high.
    489 			 * kpreempt_enter() can schedule an
    490 			 * interrupt to retry later.
    491 			 */
    492 			splx(s);
    493 			failed = (uintptr_t)&cpu_kpreempt_enter_fail;
    494 			break;
    495 		}
    496 		/* Do it! */
    497 		if (__predict_true((dop & DOPREEMPT_COUNTED) == 0)) {
    498 			kpreempt_ev_immed.ev_count++;
    499 		}
    500 		lwp_lock(l);
    501 		mi_switch(l);
    502 		l->l_nopreempt++;
    503 		splx(s);
    504 
    505 		/* Take care of any MD cleanup. */
    506 		cpu_kpreempt_exit(where);
    507 		l->l_nopreempt--;
    508 	}
    509 
    510 	if (__predict_true(!failed)) {
    511 		return false;
    512 	}
    513 
    514 	/* Record preemption failure for reporting via lockstat. */
    515 	atomic_or_uint(&l->l_dopreempt, DOPREEMPT_COUNTED);
    516 	lsflag = 0;
    517 	LOCKSTAT_ENTER(lsflag);
    518 	if (__predict_false(lsflag)) {
    519 		if (where == 0) {
    520 			where = (uintptr_t)__builtin_return_address(0);
    521 		}
    522 		/* Preemption is on, might recurse, so make it atomic. */
    523 		if (atomic_cas_ptr_ni((void *)&l->l_pfailaddr, NULL,
    524 		    (void *)where) == NULL) {
    525 			LOCKSTAT_START_TIMER(lsflag, l->l_pfailtime);
    526 			l->l_pfaillock = failed;
    527 		}
    528 	}
    529 	LOCKSTAT_EXIT(lsflag);
    530 	return true;
    531 }
    532 
    533 /*
    534  * Return true if preemption is explicitly disabled.
    535  */
    536 bool
    537 kpreempt_disabled(void)
    538 {
    539 	const lwp_t *l = curlwp;
    540 
    541 	return l->l_nopreempt != 0 || l->l_stat == LSZOMB ||
    542 	    (l->l_flag & LW_IDLE) != 0 || cpu_kpreempt_disabled();
    543 }
    544 
    545 /*
    546  * Disable kernel preemption.
    547  */
    548 void
    549 kpreempt_disable(void)
    550 {
    551 
    552 	KPREEMPT_DISABLE(curlwp);
    553 }
    554 
    555 /*
    556  * Reenable kernel preemption.
    557  */
    558 void
    559 kpreempt_enable(void)
    560 {
    561 
    562 	KPREEMPT_ENABLE(curlwp);
    563 }
    564 
    565 /*
    566  * Compute the amount of time during which the current lwp was running.
    567  *
    568  * - update l_rtime unless it's an idle lwp.
    569  */
    570 
    571 void
    572 updatertime(lwp_t *l, const struct bintime *now)
    573 {
    574 
    575 	if (__predict_false(l->l_flag & LW_IDLE))
    576 		return;
    577 
    578 	/* rtime += now - stime */
    579 	bintime_add(&l->l_rtime, now);
    580 	bintime_sub(&l->l_rtime, &l->l_stime);
    581 }
    582 
    583 /*
    584  * Select next LWP from the current CPU to run..
    585  */
    586 static inline lwp_t *
    587 nextlwp(struct cpu_info *ci, struct schedstate_percpu *spc)
    588 {
    589 	lwp_t *newl;
    590 
    591 	/*
    592 	 * Let sched_nextlwp() select the LWP to run the CPU next.
    593 	 * If no LWP is runnable, select the idle LWP.
    594 	 *
    595 	 * Note that spc_lwplock might not necessary be held, and
    596 	 * new thread would be unlocked after setting the LWP-lock.
    597 	 */
    598 	newl = sched_nextlwp();
    599 	if (newl != NULL) {
    600 		sched_dequeue(newl);
    601 		KASSERT(lwp_locked(newl, spc->spc_mutex));
    602 		newl->l_stat = LSONPROC;
    603 		newl->l_cpu = ci;
    604 		newl->l_pflag |= LP_RUNNING;
    605 		lwp_setlock(newl, spc->spc_lwplock);
    606 	} else {
    607 		newl = ci->ci_data.cpu_idlelwp;
    608 		newl->l_stat = LSONPROC;
    609 		newl->l_pflag |= LP_RUNNING;
    610 	}
    611 
    612 	/*
    613 	 * Only clear want_resched if there are no pending (slow)
    614 	 * software interrupts.
    615 	 */
    616 	ci->ci_want_resched = ci->ci_data.cpu_softints;
    617 	spc->spc_flags &= ~SPCF_SWITCHCLEAR;
    618 	spc->spc_curpriority = lwp_eprio(newl);
    619 
    620 	return newl;
    621 }
    622 
    623 /*
    624  * The machine independent parts of context switch.
    625  *
    626  * Returns 1 if another LWP was actually run.
    627  */
    628 int
    629 mi_switch(lwp_t *l)
    630 {
    631 	struct cpu_info *ci;
    632 	struct schedstate_percpu *spc;
    633 	struct lwp *newl;
    634 	int retval, oldspl;
    635 	struct bintime bt;
    636 	bool returning;
    637 
    638 	KASSERT(lwp_locked(l, NULL));
    639 	KASSERT(kpreempt_disabled());
    640 	LOCKDEBUG_BARRIER(l->l_mutex, 1);
    641 
    642 	kstack_check_magic(l);
    643 
    644 	binuptime(&bt);
    645 
    646 	KASSERT((l->l_pflag & LP_RUNNING) != 0);
    647 	KASSERT(l->l_cpu == curcpu());
    648 	ci = l->l_cpu;
    649 	spc = &ci->ci_schedstate;
    650 	returning = false;
    651 	newl = NULL;
    652 
    653 	/*
    654 	 * If we have been asked to switch to a specific LWP, then there
    655 	 * is no need to inspect the run queues.  If a soft interrupt is
    656 	 * blocking, then return to the interrupted thread without adjusting
    657 	 * VM context or its start time: neither have been changed in order
    658 	 * to take the interrupt.
    659 	 */
    660 	if (l->l_switchto != NULL) {
    661 		if ((l->l_pflag & LP_INTR) != 0) {
    662 			returning = true;
    663 			softint_block(l);
    664 			if ((l->l_pflag & LP_TIMEINTR) != 0)
    665 				updatertime(l, &bt);
    666 		}
    667 		newl = l->l_switchto;
    668 		l->l_switchto = NULL;
    669 	}
    670 #ifndef __HAVE_FAST_SOFTINTS
    671 	else if (ci->ci_data.cpu_softints != 0) {
    672 		/* There are pending soft interrupts, so pick one. */
    673 		newl = softint_picklwp();
    674 		newl->l_stat = LSONPROC;
    675 		newl->l_pflag |= LP_RUNNING;
    676 	}
    677 #endif	/* !__HAVE_FAST_SOFTINTS */
    678 
    679 	/* Count time spent in current system call */
    680 	if (!returning) {
    681 		SYSCALL_TIME_SLEEP(l);
    682 
    683 		/*
    684 		 * XXXSMP If we are using h/w performance counters,
    685 		 * save context.
    686 		 */
    687 #if PERFCTRS
    688 		if (PMC_ENABLED(l->l_proc)) {
    689 			pmc_save_context(l->l_proc);
    690 		}
    691 #endif
    692 		updatertime(l, &bt);
    693 	}
    694 
    695 	/* Lock the runqueue */
    696 	KASSERT(l->l_stat != LSRUN);
    697 	mutex_spin_enter(spc->spc_mutex);
    698 
    699 	/*
    700 	 * If on the CPU and we have gotten this far, then we must yield.
    701 	 */
    702 	if (l->l_stat == LSONPROC && l != newl) {
    703 		KASSERT(lwp_locked(l, spc->spc_lwplock));
    704 		if ((l->l_flag & LW_IDLE) == 0) {
    705 			l->l_stat = LSRUN;
    706 			lwp_setlock(l, spc->spc_mutex);
    707 			sched_enqueue(l, true);
    708 			/* Handle migration case */
    709 			KASSERT(spc->spc_migrating == NULL);
    710 			if (l->l_target_cpu !=  NULL) {
    711 				spc->spc_migrating = l;
    712 			}
    713 		} else
    714 			l->l_stat = LSIDL;
    715 	}
    716 
    717 	/* Pick new LWP to run. */
    718 	if (newl == NULL) {
    719 		newl = nextlwp(ci, spc);
    720 	}
    721 
    722 	/* Items that must be updated with the CPU locked. */
    723 	if (!returning) {
    724 		/* Update the new LWP's start time. */
    725 		newl->l_stime = bt;
    726 
    727 		/*
    728 		 * ci_curlwp changes when a fast soft interrupt occurs.
    729 		 * We use cpu_onproc to keep track of which kernel or
    730 		 * user thread is running 'underneath' the software
    731 		 * interrupt.  This is important for time accounting,
    732 		 * itimers and forcing user threads to preempt (aston).
    733 		 */
    734 		ci->ci_data.cpu_onproc = newl;
    735 	}
    736 
    737 	/*
    738 	 * Preemption related tasks.  Must be done with the current
    739 	 * CPU locked.
    740 	 */
    741 	cpu_did_resched(l);
    742 	l->l_dopreempt = 0;
    743 	if (__predict_false(l->l_pfailaddr != 0)) {
    744 		LOCKSTAT_FLAG(lsflag);
    745 		LOCKSTAT_ENTER(lsflag);
    746 		LOCKSTAT_STOP_TIMER(lsflag, l->l_pfailtime);
    747 		LOCKSTAT_EVENT_RA(lsflag, l->l_pfaillock, LB_NOPREEMPT|LB_SPIN,
    748 		    1, l->l_pfailtime, l->l_pfailaddr);
    749 		LOCKSTAT_EXIT(lsflag);
    750 		l->l_pfailtime = 0;
    751 		l->l_pfaillock = 0;
    752 		l->l_pfailaddr = 0;
    753 	}
    754 
    755 	if (l != newl) {
    756 		struct lwp *prevlwp;
    757 
    758 		/* Release all locks, but leave the current LWP locked */
    759 		if (l->l_mutex == spc->spc_mutex) {
    760 			/*
    761 			 * Drop spc_lwplock, if the current LWP has been moved
    762 			 * to the run queue (it is now locked by spc_mutex).
    763 			 */
    764 			mutex_spin_exit(spc->spc_lwplock);
    765 		} else {
    766 			/*
    767 			 * Otherwise, drop the spc_mutex, we are done with the
    768 			 * run queues.
    769 			 */
    770 			mutex_spin_exit(spc->spc_mutex);
    771 		}
    772 
    773 		/*
    774 		 * Mark that context switch is going to be performed
    775 		 * for this LWP, to protect it from being switched
    776 		 * to on another CPU.
    777 		 */
    778 		KASSERT(l->l_ctxswtch == 0);
    779 		l->l_ctxswtch = 1;
    780 		l->l_ncsw++;
    781 		KASSERT((l->l_pflag & LP_RUNNING) != 0);
    782 		l->l_pflag &= ~LP_RUNNING;
    783 
    784 		/*
    785 		 * Increase the count of spin-mutexes before the release
    786 		 * of the last lock - we must remain at IPL_SCHED during
    787 		 * the context switch.
    788 		 */
    789 		oldspl = MUTEX_SPIN_OLDSPL(ci);
    790 		ci->ci_mtx_count--;
    791 		lwp_unlock(l);
    792 
    793 		/* Count the context switch on this CPU. */
    794 		ci->ci_data.cpu_nswtch++;
    795 
    796 		/* Update status for lwpctl, if present. */
    797 		if (l->l_lwpctl != NULL)
    798 			l->l_lwpctl->lc_curcpu = LWPCTL_CPU_NONE;
    799 
    800 		/*
    801 		 * Save old VM context, unless a soft interrupt
    802 		 * handler is blocking.
    803 		 */
    804 		if (!returning)
    805 			pmap_deactivate(l);
    806 
    807 		/*
    808 		 * We may need to spin-wait for if 'newl' is still
    809 		 * context switching on another CPU.
    810 		 */
    811 		if (__predict_false(newl->l_ctxswtch != 0)) {
    812 			u_int count;
    813 			count = SPINLOCK_BACKOFF_MIN;
    814 			while (newl->l_ctxswtch)
    815 				SPINLOCK_BACKOFF(count);
    816 		}
    817 
    818 		/* Switch to the new LWP.. */
    819 		prevlwp = cpu_switchto(l, newl, returning);
    820 		ci = curcpu();
    821 
    822 		/*
    823 		 * Switched away - we have new curlwp.
    824 		 * Restore VM context and IPL.
    825 		 */
    826 		pmap_activate(l);
    827 		uvm_emap_switch(l);
    828 
    829 		if (prevlwp != NULL) {
    830 			/* Normalize the count of the spin-mutexes */
    831 			ci->ci_mtx_count++;
    832 			/* Unmark the state of context switch */
    833 			membar_exit();
    834 			prevlwp->l_ctxswtch = 0;
    835 		}
    836 
    837 		/* Update status for lwpctl, if present. */
    838 		if (l->l_lwpctl != NULL) {
    839 			l->l_lwpctl->lc_curcpu = (int)cpu_index(ci);
    840 			l->l_lwpctl->lc_pctr++;
    841 		}
    842 
    843 		KASSERT(l->l_cpu == ci);
    844 		splx(oldspl);
    845 		retval = 1;
    846 	} else {
    847 		/* Nothing to do - just unlock and return. */
    848 		mutex_spin_exit(spc->spc_mutex);
    849 		lwp_unlock(l);
    850 		retval = 0;
    851 	}
    852 
    853 	KASSERT(l == curlwp);
    854 	KASSERT(l->l_stat == LSONPROC);
    855 
    856 	/*
    857 	 * XXXSMP If we are using h/w performance counters, restore context.
    858 	 * XXXSMP preemption problem.
    859 	 */
    860 #if PERFCTRS
    861 	if (PMC_ENABLED(l->l_proc)) {
    862 		pmc_restore_context(l->l_proc);
    863 	}
    864 #endif
    865 	SYSCALL_TIME_WAKEUP(l);
    866 	LOCKDEBUG_BARRIER(NULL, 1);
    867 
    868 	return retval;
    869 }
    870 
    871 /*
    872  * The machine independent parts of context switch to oblivion.
    873  * Does not return.  Call with the LWP unlocked.
    874  */
    875 void
    876 lwp_exit_switchaway(lwp_t *l)
    877 {
    878 	struct cpu_info *ci;
    879 	struct lwp *newl;
    880 	struct bintime bt;
    881 
    882 	ci = l->l_cpu;
    883 
    884 	KASSERT(kpreempt_disabled());
    885 	KASSERT(l->l_stat == LSZOMB || l->l_stat == LSIDL);
    886 	KASSERT(ci == curcpu());
    887 	LOCKDEBUG_BARRIER(NULL, 0);
    888 
    889 	kstack_check_magic(l);
    890 
    891 	/* Count time spent in current system call */
    892 	SYSCALL_TIME_SLEEP(l);
    893 	binuptime(&bt);
    894 	updatertime(l, &bt);
    895 
    896 	/* Must stay at IPL_SCHED even after releasing run queue lock. */
    897 	(void)splsched();
    898 
    899 	/*
    900 	 * Let sched_nextlwp() select the LWP to run the CPU next.
    901 	 * If no LWP is runnable, select the idle LWP.
    902 	 *
    903 	 * Note that spc_lwplock might not necessary be held, and
    904 	 * new thread would be unlocked after setting the LWP-lock.
    905 	 */
    906 	spc_lock(ci);
    907 #ifndef __HAVE_FAST_SOFTINTS
    908 	if (ci->ci_data.cpu_softints != 0) {
    909 		/* There are pending soft interrupts, so pick one. */
    910 		newl = softint_picklwp();
    911 		newl->l_stat = LSONPROC;
    912 		newl->l_pflag |= LP_RUNNING;
    913 	} else
    914 #endif	/* !__HAVE_FAST_SOFTINTS */
    915 	{
    916 		newl = nextlwp(ci, &ci->ci_schedstate);
    917 	}
    918 
    919 	/* Update the new LWP's start time. */
    920 	newl->l_stime = bt;
    921 	l->l_pflag &= ~LP_RUNNING;
    922 
    923 	/*
    924 	 * ci_curlwp changes when a fast soft interrupt occurs.
    925 	 * We use cpu_onproc to keep track of which kernel or
    926 	 * user thread is running 'underneath' the software
    927 	 * interrupt.  This is important for time accounting,
    928 	 * itimers and forcing user threads to preempt (aston).
    929 	 */
    930 	ci->ci_data.cpu_onproc = newl;
    931 
    932 	/*
    933 	 * Preemption related tasks.  Must be done with the current
    934 	 * CPU locked.
    935 	 */
    936 	cpu_did_resched(l);
    937 
    938 	/* Unlock the run queue. */
    939 	spc_unlock(ci);
    940 
    941 	/* Count the context switch on this CPU. */
    942 	ci->ci_data.cpu_nswtch++;
    943 
    944 	/* Update status for lwpctl, if present. */
    945 	if (l->l_lwpctl != NULL)
    946 		l->l_lwpctl->lc_curcpu = LWPCTL_CPU_EXITED;
    947 
    948 	/*
    949 	 * We may need to spin-wait for if 'newl' is still
    950 	 * context switching on another CPU.
    951 	 */
    952 	if (__predict_false(newl->l_ctxswtch != 0)) {
    953 		u_int count;
    954 		count = SPINLOCK_BACKOFF_MIN;
    955 		while (newl->l_ctxswtch)
    956 			SPINLOCK_BACKOFF(count);
    957 	}
    958 
    959 	/* Switch to the new LWP.. */
    960 	(void)cpu_switchto(NULL, newl, false);
    961 
    962 	for (;;) continue;	/* XXX: convince gcc about "noreturn" */
    963 	/* NOTREACHED */
    964 }
    965 
    966 /*
    967  * Change LWP state to be runnable, placing it on the run queue if it is
    968  * in memory, and awakening the swapper if it isn't in memory.
    969  *
    970  * Call with the process and LWP locked.  Will return with the LWP unlocked.
    971  */
    972 void
    973 setrunnable(struct lwp *l)
    974 {
    975 	struct proc *p = l->l_proc;
    976 	struct cpu_info *ci;
    977 
    978 	KASSERT((l->l_flag & LW_IDLE) == 0);
    979 	KASSERT(mutex_owned(p->p_lock));
    980 	KASSERT(lwp_locked(l, NULL));
    981 	KASSERT(l->l_mutex != l->l_cpu->ci_schedstate.spc_mutex);
    982 
    983 	switch (l->l_stat) {
    984 	case LSSTOP:
    985 		/*
    986 		 * If we're being traced (possibly because someone attached us
    987 		 * while we were stopped), check for a signal from the debugger.
    988 		 */
    989 		if ((p->p_slflag & PSL_TRACED) != 0 && p->p_xstat != 0)
    990 			signotify(l);
    991 		p->p_nrlwps++;
    992 		break;
    993 	case LSSUSPENDED:
    994 		l->l_flag &= ~LW_WSUSPEND;
    995 		p->p_nrlwps++;
    996 		cv_broadcast(&p->p_lwpcv);
    997 		break;
    998 	case LSSLEEP:
    999 		KASSERT(l->l_wchan != NULL);
   1000 		break;
   1001 	default:
   1002 		panic("setrunnable: lwp %p state was %d", l, l->l_stat);
   1003 	}
   1004 
   1005 #ifdef KERN_SA
   1006 	if (l->l_proc->p_sa)
   1007 		sa_awaken(l);
   1008 #endif /* KERN_SA */
   1009 
   1010 	/*
   1011 	 * If the LWP was sleeping interruptably, then it's OK to start it
   1012 	 * again.  If not, mark it as still sleeping.
   1013 	 */
   1014 	if (l->l_wchan != NULL) {
   1015 		l->l_stat = LSSLEEP;
   1016 		/* lwp_unsleep() will release the lock. */
   1017 		lwp_unsleep(l, true);
   1018 		return;
   1019 	}
   1020 
   1021 	/*
   1022 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
   1023 	 * about to call mi_switch(), in which case it will yield.
   1024 	 */
   1025 	if ((l->l_pflag & LP_RUNNING) != 0) {
   1026 		l->l_stat = LSONPROC;
   1027 		l->l_slptime = 0;
   1028 		lwp_unlock(l);
   1029 		return;
   1030 	}
   1031 
   1032 	/*
   1033 	 * Look for a CPU to run.
   1034 	 * Set the LWP runnable.
   1035 	 */
   1036 	ci = sched_takecpu(l);
   1037 	l->l_cpu = ci;
   1038 	spc_lock(ci);
   1039 	lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
   1040 	sched_setrunnable(l);
   1041 	l->l_stat = LSRUN;
   1042 	l->l_slptime = 0;
   1043 
   1044 	/*
   1045 	 * If thread is swapped out - wake the swapper to bring it back in.
   1046 	 * Otherwise, enter it into a run queue.
   1047 	 */
   1048 	if (l->l_flag & LW_INMEM) {
   1049 		sched_enqueue(l, false);
   1050 		resched_cpu(l);
   1051 		lwp_unlock(l);
   1052 	} else {
   1053 		lwp_unlock(l);
   1054 		uvm_kick_scheduler();
   1055 	}
   1056 }
   1057 
   1058 /*
   1059  * suspendsched:
   1060  *
   1061  *	Convert all non-LW_SYSTEM LSSLEEP or LSRUN LWPs to LSSUSPENDED.
   1062  */
   1063 void
   1064 suspendsched(void)
   1065 {
   1066 	CPU_INFO_ITERATOR cii;
   1067 	struct cpu_info *ci;
   1068 	struct lwp *l;
   1069 	struct proc *p;
   1070 
   1071 	/*
   1072 	 * We do this by process in order not to violate the locking rules.
   1073 	 */
   1074 	mutex_enter(proc_lock);
   1075 	PROCLIST_FOREACH(p, &allproc) {
   1076 		if ((p->p_flag & PK_MARKER) != 0)
   1077 			continue;
   1078 
   1079 		mutex_enter(p->p_lock);
   1080 		if ((p->p_flag & PK_SYSTEM) != 0) {
   1081 			mutex_exit(p->p_lock);
   1082 			continue;
   1083 		}
   1084 
   1085 		p->p_stat = SSTOP;
   1086 
   1087 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1088 			if (l == curlwp)
   1089 				continue;
   1090 
   1091 			lwp_lock(l);
   1092 
   1093 			/*
   1094 			 * Set L_WREBOOT so that the LWP will suspend itself
   1095 			 * when it tries to return to user mode.  We want to
   1096 			 * try and get to get as many LWPs as possible to
   1097 			 * the user / kernel boundary, so that they will
   1098 			 * release any locks that they hold.
   1099 			 */
   1100 			l->l_flag |= (LW_WREBOOT | LW_WSUSPEND);
   1101 
   1102 			if (l->l_stat == LSSLEEP &&
   1103 			    (l->l_flag & LW_SINTR) != 0) {
   1104 				/* setrunnable() will release the lock. */
   1105 				setrunnable(l);
   1106 				continue;
   1107 			}
   1108 
   1109 			lwp_unlock(l);
   1110 		}
   1111 
   1112 		mutex_exit(p->p_lock);
   1113 	}
   1114 	mutex_exit(proc_lock);
   1115 
   1116 	/*
   1117 	 * Kick all CPUs to make them preempt any LWPs running in user mode.
   1118 	 * They'll trap into the kernel and suspend themselves in userret().
   1119 	 */
   1120 	for (CPU_INFO_FOREACH(cii, ci)) {
   1121 		spc_lock(ci);
   1122 		cpu_need_resched(ci, RESCHED_IMMED);
   1123 		spc_unlock(ci);
   1124 	}
   1125 }
   1126 
   1127 /*
   1128  * sched_unsleep:
   1129  *
   1130  *	The is called when the LWP has not been awoken normally but instead
   1131  *	interrupted: for example, if the sleep timed out.  Because of this,
   1132  *	it's not a valid action for running or idle LWPs.
   1133  */
   1134 static u_int
   1135 sched_unsleep(struct lwp *l, bool cleanup)
   1136 {
   1137 
   1138 	lwp_unlock(l);
   1139 	panic("sched_unsleep");
   1140 }
   1141 
   1142 static void
   1143 resched_cpu(struct lwp *l)
   1144 {
   1145 	struct cpu_info *ci = ci = l->l_cpu;
   1146 
   1147 	KASSERT(lwp_locked(l, NULL));
   1148 	if (lwp_eprio(l) > ci->ci_schedstate.spc_curpriority)
   1149 		cpu_need_resched(ci, 0);
   1150 }
   1151 
   1152 static void
   1153 sched_changepri(struct lwp *l, pri_t pri)
   1154 {
   1155 
   1156 	KASSERT(lwp_locked(l, NULL));
   1157 
   1158 	if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
   1159 		KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
   1160 		sched_dequeue(l);
   1161 		l->l_priority = pri;
   1162 		sched_enqueue(l, false);
   1163 	} else {
   1164 		l->l_priority = pri;
   1165 	}
   1166 	resched_cpu(l);
   1167 }
   1168 
   1169 static void
   1170 sched_lendpri(struct lwp *l, pri_t pri)
   1171 {
   1172 
   1173 	KASSERT(lwp_locked(l, NULL));
   1174 
   1175 	if (l->l_stat == LSRUN && (l->l_flag & LW_INMEM) != 0) {
   1176 		KASSERT(lwp_locked(l, l->l_cpu->ci_schedstate.spc_mutex));
   1177 		sched_dequeue(l);
   1178 		l->l_inheritedprio = pri;
   1179 		sched_enqueue(l, false);
   1180 	} else {
   1181 		l->l_inheritedprio = pri;
   1182 	}
   1183 	resched_cpu(l);
   1184 }
   1185 
   1186 struct lwp *
   1187 syncobj_noowner(wchan_t wchan)
   1188 {
   1189 
   1190 	return NULL;
   1191 }
   1192 
   1193 /* Decay 95% of proc::p_pctcpu in 60 seconds, ccpu = exp(-1/20) */
   1194 const fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;
   1195 
   1196 /*
   1197  * sched_pstats:
   1198  *
   1199  * Update process statistics and check CPU resource allocation.
   1200  * Call scheduler-specific hook to eventually adjust process/LWP
   1201  * priorities.
   1202  */
   1203 /* ARGSUSED */
   1204 void
   1205 sched_pstats(void *arg)
   1206 {
   1207 	const int clkhz = (stathz != 0 ? stathz : hz);
   1208 	static bool backwards;
   1209 	struct rlimit *rlim;
   1210 	struct lwp *l;
   1211 	struct proc *p;
   1212 	long runtm;
   1213 	fixpt_t lpctcpu;
   1214 	u_int lcpticks;
   1215 	int sig;
   1216 
   1217 	sched_pstats_ticks++;
   1218 
   1219 	mutex_enter(proc_lock);
   1220 	PROCLIST_FOREACH(p, &allproc) {
   1221 		if (__predict_false((p->p_flag & PK_MARKER) != 0))
   1222 			continue;
   1223 
   1224 		/*
   1225 		 * Increment time in/out of memory and sleep
   1226 		 * time (if sleeping), ignore overflow.
   1227 		 */
   1228 		mutex_enter(p->p_lock);
   1229 		runtm = p->p_rtime.sec;
   1230 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1231 			if (__predict_false((l->l_flag & LW_IDLE) != 0))
   1232 				continue;
   1233 			lwp_lock(l);
   1234 			runtm += l->l_rtime.sec;
   1235 			l->l_swtime++;
   1236 			sched_lwp_stats(l);
   1237 			lwp_unlock(l);
   1238 
   1239 			l->l_pctcpu = (l->l_pctcpu * ccpu) >> FSHIFT;
   1240 			if (l->l_slptime != 0)
   1241 				continue;
   1242 
   1243 			lpctcpu = l->l_pctcpu;
   1244 			lcpticks = atomic_swap_uint(&l->l_cpticks, 0);
   1245 			lpctcpu += ((FSCALE - ccpu) *
   1246 			    (lcpticks * FSCALE / clkhz)) >> FSHIFT;
   1247 			l->l_pctcpu = lpctcpu;
   1248 		}
   1249 		/* Calculating p_pctcpu only for ps(1) */
   1250 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
   1251 
   1252 		/*
   1253 		 * Check if the process exceeds its CPU resource allocation.
   1254 		 * If over max, kill it.
   1255 		 */
   1256 		rlim = &p->p_rlimit[RLIMIT_CPU];
   1257 		sig = 0;
   1258 		if (__predict_false(runtm >= rlim->rlim_cur)) {
   1259 			if (runtm >= rlim->rlim_max)
   1260 				sig = SIGKILL;
   1261 			else {
   1262 				sig = SIGXCPU;
   1263 				if (rlim->rlim_cur < rlim->rlim_max)
   1264 					rlim->rlim_cur += 5;
   1265 			}
   1266 		}
   1267 		mutex_exit(p->p_lock);
   1268 		if (__predict_false(runtm < 0)) {
   1269 			if (!backwards) {
   1270 				backwards = true;
   1271 				printf("WARNING: negative runtime; "
   1272 				    "monotonic clock has gone backwards\n");
   1273 			}
   1274 		} else if (__predict_false(sig)) {
   1275 			KASSERT((p->p_flag & PK_SYSTEM) == 0);
   1276 			psignal(p, sig);
   1277 		}
   1278 	}
   1279 	mutex_exit(proc_lock);
   1280 	uvm_meter();
   1281 	cv_wakeup(&lbolt);
   1282 	callout_schedule(&sched_pstats_ch, hz);
   1283 }
   1284