2009-06-28 13:25:05 +04:00
|
|
|
/* $NetBSD: kern_idle.c,v 1.22 2009/06/28 09:25:05 ad Exp $ */
|
2007-05-17 18:51:11 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c)2002, 2006, 2007 YAMAMOTO Takashi,
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
2009-06-28 13:25:05 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: kern_idle.c,v 1.22 2009/06/28 09:25:05 ad Exp $");
|
2007-05-17 18:51:11 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/cpu.h>
|
|
|
|
#include <sys/idle.h>
|
2007-11-06 03:42:39 +03:00
|
|
|
#include <sys/kthread.h>
|
2007-05-17 18:51:11 +04:00
|
|
|
#include <sys/lockdebug.h>
|
|
|
|
#include <sys/kmem.h>
|
2007-07-21 23:06:20 +04:00
|
|
|
#include <sys/proc.h>
|
2008-04-04 21:21:22 +04:00
|
|
|
#include <sys/atomic.h>
|
2007-05-17 18:51:11 +04:00
|
|
|
|
|
|
|
#include <uvm/uvm.h>
|
|
|
|
#include <uvm/uvm_extern.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
idle_loop(void *dummy)
|
|
|
|
{
|
|
|
|
struct cpu_info *ci = curcpu();
|
2008-05-30 02:33:27 +04:00
|
|
|
struct schedstate_percpu *spc;
|
2007-05-17 18:51:11 +04:00
|
|
|
struct lwp *l = curlwp;
|
|
|
|
|
2008-05-24 16:59:06 +04:00
|
|
|
ci->ci_data.cpu_onproc = l;
|
|
|
|
|
2007-10-09 00:06:17 +04:00
|
|
|
/* Update start time for this thread. */
|
2007-11-15 23:12:25 +03:00
|
|
|
lwp_lock(l);
|
2007-12-22 04:14:53 +03:00
|
|
|
binuptime(&l->l_stime);
|
2007-11-15 23:12:25 +03:00
|
|
|
lwp_unlock(l);
|
2007-10-09 00:06:17 +04:00
|
|
|
|
2009-06-28 13:25:05 +04:00
|
|
|
/*
|
|
|
|
* Use spl0() here to ensure that we have the correct interrupt
|
|
|
|
* priority. This may be the first thread running on the CPU,
|
|
|
|
* in which case we took a dirtbag route to get here.
|
|
|
|
*/
|
2008-05-30 02:33:27 +04:00
|
|
|
spc = &ci->ci_schedstate;
|
2009-06-28 13:25:05 +04:00
|
|
|
(void)splsched();
|
2008-05-30 02:33:27 +04:00
|
|
|
spc->spc_flags |= SPCF_RUNNING;
|
2009-06-28 13:25:05 +04:00
|
|
|
spl0();
|
2008-04-24 17:56:30 +04:00
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
KERNEL_UNLOCK_ALL(l, NULL);
|
|
|
|
l->l_stat = LSONPROC;
|
2008-05-30 02:33:27 +04:00
|
|
|
for (;;) {
|
2007-05-17 18:51:11 +04:00
|
|
|
LOCKDEBUG_BARRIER(NULL, 0);
|
|
|
|
KASSERT((l->l_flag & LW_IDLE) != 0);
|
|
|
|
KASSERT(ci == curcpu());
|
|
|
|
KASSERT(l == curlwp);
|
|
|
|
KASSERT(CURCPU_IDLE_P());
|
2007-11-06 03:42:39 +03:00
|
|
|
KASSERT(l->l_priority == PRI_IDLE);
|
2007-05-17 18:51:11 +04:00
|
|
|
|
2008-05-30 02:33:27 +04:00
|
|
|
sched_idle();
|
2007-05-17 18:51:11 +04:00
|
|
|
if (!sched_curcpu_runnable_p()) {
|
2008-06-11 17:42:02 +04:00
|
|
|
if ((spc->spc_flags & SPCF_OFFLINE) == 0) {
|
|
|
|
uvm_pageidlezero();
|
|
|
|
}
|
2008-06-04 16:45:28 +04:00
|
|
|
if (!sched_curcpu_runnable_p()) {
|
|
|
|
cpu_idle();
|
|
|
|
if (!sched_curcpu_runnable_p() &&
|
|
|
|
!ci->ci_want_resched) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-17 18:51:11 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-14 17:26:57 +03:00
|
|
|
KASSERT(l->l_mutex == l->l_cpu->ci_schedstate.spc_lwplock);
|
2007-05-17 18:51:11 +04:00
|
|
|
lwp_lock(l);
|
|
|
|
mi_switch(l);
|
|
|
|
KASSERT(curlwp == l);
|
|
|
|
KASSERT(l->l_stat == LSONPROC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
create_idle_lwp(struct cpu_info *ci)
|
|
|
|
{
|
2007-11-06 03:42:39 +03:00
|
|
|
lwp_t *l;
|
2007-05-17 18:51:11 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
KASSERT(ci->ci_data.cpu_idlelwp == NULL);
|
2007-11-06 03:42:39 +03:00
|
|
|
error = kthread_create(PRI_IDLE, KTHREAD_MPSAFE | KTHREAD_IDLE,
|
2008-03-11 01:20:14 +03:00
|
|
|
ci, idle_loop, NULL, &l, "idle/%u", ci->ci_index);
|
2007-11-06 03:42:39 +03:00
|
|
|
if (error != 0)
|
|
|
|
panic("create_idle_lwp: error %d", error);
|
|
|
|
lwp_lock(l);
|
|
|
|
l->l_flag |= LW_IDLE;
|
|
|
|
lwp_unlock(l);
|
2007-05-17 18:51:11 +04:00
|
|
|
l->l_cpu = ci;
|
|
|
|
ci->ci_data.cpu_idlelwp = l;
|
2007-11-06 03:42:39 +03:00
|
|
|
|
2007-05-17 18:51:11 +04:00
|
|
|
return error;
|
|
|
|
}
|