2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
53 #include <kern/thread.h>
54 #include <kern/lock.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_kern.h>
57 #include <mach/vm_param.h>
58 #include <kern/sched_prim.h>
59 #include <kern/processor.h>
60 #include <kern/thread_swap.h>
61 #include <kern/spl.h> /* for splsched */
62 #include <kern/misc_protos.h>
63 #include <kern/counters.h>
64 #include <mach/policy.h>
66 queue_head_t swapin_queue
;
67 decl_simple_lock_data(,swapin_lock
)
69 mach_counter_t c_swapin_thread_block
;
71 void swapin_thread(void);
74 * swapin_init: [exported]
76 * Initialize the swapper module.
81 queue_init(&swapin_queue
);
82 simple_lock_init(&swapin_lock
, ETAP_THREAD_SWAPPER
);
83 kernel_thread_with_priority(
84 kernel_task
, BASEPRI_PREEMPT
- 2,
85 swapin_thread
, TRUE
, TRUE
);
89 * thread_swapin: [exported]
91 * Place the specified thread in the list of threads to swapin.
92 * Called with thread locked, returned unlocked.
97 register thread_t thread
)
99 switch (thread
->state
& TH_STACK_STATE
) {
101 case TH_STACK_HANDOFF
:
105 thread
->state
= (thread
->state
& ~TH_STACK_STATE
) | TH_STACK_ALLOC
;
106 thread_unlock(thread
);
107 simple_lock(&swapin_lock
);
108 enqueue_tail(&swapin_queue
, (queue_entry_t
) thread
);
109 simple_unlock(&swapin_lock
);
110 thread_wakeup((event_t
)&swapin_queue
);
117 thread_unlock(thread
);
122 * Already swapped in.
124 panic("thread_swapin");
131 * Swapin the specified thread, if it should be runnable, then put
136 register thread_t thread
)
142 * Allocate the kernel stack.
144 stack
= stack_alloc(thread
, thread_continue
);
148 * Place on run queue.
152 thread
->state
&= ~(TH_STACK_HANDOFF
| TH_STACK_ALLOC
);
153 if (thread
->state
& TH_RUN
)
154 thread_setrun(thread
, HEAD_Q
);
155 thread_unlock(thread
);
160 * swapin_thread: [exported]
162 * This procedure executes as a kernel thread. Threads that need to
163 * be swapped in are swapped in by this thread.
166 swapin_thread_continue(void)
168 register thread_t thread
;
171 simple_lock(&swapin_lock
);
173 while ((thread
= (thread_t
)dequeue_head(&swapin_queue
)) != THREAD_NULL
) {
174 simple_unlock(&swapin_lock
);
177 thread_doswapin(thread
);
180 simple_lock(&swapin_lock
);
183 assert_wait((event_t
)&swapin_queue
, THREAD_UNINT
);
184 simple_unlock(&swapin_lock
);
187 counter(c_swapin_thread_block
++);
188 thread_block(swapin_thread_continue
);
195 thread_t self
= current_thread();
197 stack_privilege(self
);
199 swapin_thread_continue();