2000-09-07 01:33:02 +00:00
|
|
|
/*-
|
2004-07-25 19:49:01 +00:00
|
|
|
* Copyright (C) 2000-2004 The FreeBSD Project. All rights reserved.
|
2000-09-07 01:33:02 +00:00
|
|
|
*
|
2004-07-25 19:49:01 +00:00
|
|
|
* 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 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 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.
|
2000-09-07 01:33:02 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2001-05-10 17:45:49 +00:00
|
|
|
#include <sys/kthread.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2002-10-12 05:32:24 +00:00
|
|
|
#include <sys/sched.h>
|
2000-09-07 01:33:02 +00:00
|
|
|
#include <sys/unistd.h>
|
2004-09-01 06:42:02 +00:00
|
|
|
#ifdef SMP
|
|
|
|
#include <sys/smp.h>
|
|
|
|
#endif
|
2000-09-07 01:33:02 +00:00
|
|
|
|
|
|
|
static void idle_setup(void *dummy);
|
|
|
|
SYSINIT(idle_setup, SI_SUB_SCHED_IDLE, SI_ORDER_FIRST, idle_setup, NULL)
|
|
|
|
|
|
|
|
static void idle_proc(void *dummy);
|
|
|
|
|
|
|
|
/*
|
2003-10-19 02:43:57 +00:00
|
|
|
* Set up per-cpu idle process contexts. The AP's shouldn't be running or
|
2001-02-09 14:59:43 +00:00
|
|
|
* accessing their idle processes at this point, so don't bother with
|
|
|
|
* locking.
|
2000-09-07 01:33:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
idle_setup(void *dummy)
|
|
|
|
{
|
2001-04-27 19:28:25 +00:00
|
|
|
#ifdef SMP
|
2001-12-11 23:33:44 +00:00
|
|
|
struct pcpu *pc;
|
2001-04-27 19:28:25 +00:00
|
|
|
#endif
|
|
|
|
struct proc *p;
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
struct thread *td;
|
2000-09-07 01:33:02 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
#ifdef SMP
|
2001-12-11 23:33:44 +00:00
|
|
|
SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
|
2001-04-27 19:28:25 +00:00
|
|
|
error = kthread_create(idle_proc, NULL, &p,
|
2002-10-02 07:44:29 +00:00
|
|
|
RFSTOPPED | RFHIGHPID, 0, "idle: cpu%d", pc->pc_cpuid);
|
2002-02-07 20:58:47 +00:00
|
|
|
pc->pc_idlethread = FIRST_THREAD_IN_PROC(p);
|
2001-12-18 00:27:18 +00:00
|
|
|
if (pc->pc_curthread == NULL) {
|
2001-12-11 23:33:44 +00:00
|
|
|
pc->pc_curthread = pc->pc_idlethread;
|
2001-12-18 00:27:18 +00:00
|
|
|
pc->pc_idlethread->td_critnest = 0;
|
|
|
|
}
|
2000-09-07 01:33:02 +00:00
|
|
|
#else
|
2001-04-27 19:28:25 +00:00
|
|
|
error = kthread_create(idle_proc, NULL, &p,
|
2002-10-02 07:44:29 +00:00
|
|
|
RFSTOPPED | RFHIGHPID, 0, "idle");
|
2002-02-07 20:58:47 +00:00
|
|
|
PCPU_SET(idlethread, FIRST_THREAD_IN_PROC(p));
|
2000-09-07 01:33:02 +00:00
|
|
|
#endif
|
|
|
|
if (error)
|
|
|
|
panic("idle_setup: kthread_create error %d\n", error);
|
|
|
|
|
2003-04-17 22:25:35 +00:00
|
|
|
PROC_LOCK(p);
|
2001-04-27 19:28:25 +00:00
|
|
|
p->p_flag |= P_NOLOAD;
|
2003-04-17 22:25:35 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
td = FIRST_THREAD_IN_PROC(p);
|
2004-07-02 19:09:50 +00:00
|
|
|
TD_SET_CAN_RUN(td);
|
2003-05-02 00:33:12 +00:00
|
|
|
td->td_flags |= TDF_IDLETD;
|
2004-06-28 16:19:50 +00:00
|
|
|
td->td_priority = PRI_MAX_IDLE;
|
2003-04-17 22:25:35 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
2001-04-27 19:28:25 +00:00
|
|
|
#ifdef SMP
|
2000-09-07 01:33:02 +00:00
|
|
|
}
|
2001-04-27 19:28:25 +00:00
|
|
|
#endif
|
2000-09-07 01:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-10-19 02:43:57 +00:00
|
|
|
* The actual idle process.
|
2000-09-07 01:33:02 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
idle_proc(void *dummy)
|
|
|
|
{
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
struct proc *p;
|
2003-10-19 02:43:57 +00:00
|
|
|
struct thread *td;
|
2004-09-01 06:42:02 +00:00
|
|
|
#ifdef SMP
|
|
|
|
cpumask_t mycpu;
|
|
|
|
#endif
|
2000-09-07 01:33:02 +00:00
|
|
|
|
Part 1 of KSE-III
The ability to schedule multiple threads per process
(one one cpu) by making ALL system calls optionally asynchronous.
to come: ia64 and power-pc patches, patches for gdb, test program (in tools)
Reviewed by: Almost everyone who counts
(at various times, peter, jhb, matt, alfred, mini, bernd,
and a cast of thousands)
NOTE: this is still Beta code, and contains lots of debugging stuff.
expect slight instability in signals..
2002-06-29 17:26:22 +00:00
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
2004-09-01 06:42:02 +00:00
|
|
|
#ifdef SMP
|
|
|
|
mycpu = PCPU_GET(cpumask);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
idle_cpus_mask |= mycpu;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
#endif
|
2000-09-07 01:33:02 +00:00
|
|
|
for (;;) {
|
|
|
|
mtx_assert(&Giant, MA_NOTOWNED);
|
|
|
|
|
2003-10-19 02:43:57 +00:00
|
|
|
while (sched_runnable() == 0)
|
2000-10-19 07:47:16 +00:00
|
|
|
cpu_idle();
|
2000-09-07 01:33:02 +00:00
|
|
|
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2004-09-01 06:42:02 +00:00
|
|
|
#ifdef SMP
|
|
|
|
idle_cpus_mask &= ~mycpu;
|
|
|
|
#endif
|
2004-07-02 19:09:50 +00:00
|
|
|
mi_switch(SW_VOL, NULL);
|
2004-09-01 06:42:02 +00:00
|
|
|
#ifdef SMP
|
|
|
|
idle_cpus_mask |= mycpu;
|
|
|
|
#endif
|
Change and clean the mutex lock interface.
mtx_enter(lock, type) becomes:
mtx_lock(lock) for sleep locks (MTX_DEF-initialized locks)
mtx_lock_spin(lock) for spin locks (MTX_SPIN-initialized)
similarily, for releasing a lock, we now have:
mtx_unlock(lock) for MTX_DEF and mtx_unlock_spin(lock) for MTX_SPIN.
We change the caller interface for the two different types of locks
because the semantics are entirely different for each case, and this
makes it explicitly clear and, at the same time, it rids us of the
extra `type' argument.
The enter->lock and exit->unlock change has been made with the idea
that we're "locking data" and not "entering locked code" in mind.
Further, remove all additional "flags" previously passed to the
lock acquire/release routines with the exception of two:
MTX_QUIET and MTX_NOSWITCH
The functionality of these flags is preserved and they can be passed
to the lock/unlock routines by calling the corresponding wrappers:
mtx_{lock, unlock}_flags(lock, flag(s)) and
mtx_{lock, unlock}_spin_flags(lock, flag(s)) for MTX_DEF and MTX_SPIN
locks, respectively.
Re-inline some lock acq/rel code; in the sleep lock case, we only
inline the _obtain_lock()s in order to ensure that the inlined code
fits into a cache line. In the spin lock case, we inline recursion and
actually only perform a function call if we need to spin. This change
has been made with the idea that we generally tend to avoid spin locks
and that also the spin locks that we do have and are heavily used
(i.e. sched_lock) do recurse, and therefore in an effort to reduce
function call overhead for some architectures (such as alpha), we
inline recursion for this case.
Create a new malloc type for the witness code and retire from using
the M_DEV type. The new type is called M_WITNESS and is only declared
if WITNESS is enabled.
Begin cleaning up some machdep/mutex.h code - specifically updated the
"optimized" inlined code in alpha/mutex.h and wrote MTX_LOCK_SPIN
and MTX_UNLOCK_SPIN asm macros for the i386/mutex.h as we presently
need those.
Finally, caught up to the interface changes in all sys code.
Contributors: jake, jhb, jasone (in no particular order)
2001-02-09 06:11:45 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2000-09-07 01:33:02 +00:00
|
|
|
}
|
|
|
|
}
|