2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
56 #include <kern/thread.h>
57 #include <kern/lock.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <mach/vm_param.h>
61 #include <kern/sched_prim.h>
62 #include <kern/processor.h>
63 #include <kern/thread_swap.h>
64 #include <kern/spl.h> /* for splsched */
65 #include <kern/misc_protos.h>
66 #include <kern/counters.h>
67 #include <mach/policy.h>
69 queue_head_t swapin_queue
;
70 decl_simple_lock_data(,swapin_lock
)
72 mach_counter_t c_swapin_thread_block
;
74 void swapin_thread(void);
77 * swapin_init: [exported]
79 * Initialize the swapper module.
84 queue_init(&swapin_queue
);
85 simple_lock_init(&swapin_lock
, ETAP_THREAD_SWAPPER
);
86 kernel_thread_with_priority(
87 kernel_task
, BASEPRI_PREEMPT
- 2,
88 swapin_thread
, TRUE
, TRUE
);
92 * thread_swapin: [exported]
94 * Place the specified thread in the list of threads to swapin.
95 * Called with thread locked, returned unlocked.
100 register thread_t thread
)
102 switch (thread
->state
& TH_STACK_STATE
) {
104 case TH_STACK_HANDOFF
:
108 thread
->state
= (thread
->state
& ~TH_STACK_STATE
) | TH_STACK_ALLOC
;
109 thread_unlock(thread
);
110 simple_lock(&swapin_lock
);
111 enqueue_tail(&swapin_queue
, (queue_entry_t
) thread
);
112 simple_unlock(&swapin_lock
);
113 thread_wakeup((event_t
)&swapin_queue
);
120 thread_unlock(thread
);
125 * Already swapped in.
127 panic("thread_swapin");
134 * Swapin the specified thread, if it should be runnable, then put
139 register thread_t thread
)
145 * Allocate the kernel stack.
147 stack
= stack_alloc(thread
, thread_continue
);
151 * Place on run queue.
155 thread
->state
&= ~(TH_STACK_HANDOFF
| TH_STACK_ALLOC
);
156 if (thread
->state
& TH_RUN
)
157 thread_setrun(thread
, HEAD_Q
);
158 thread_unlock(thread
);
163 * swapin_thread: [exported]
165 * This procedure executes as a kernel thread. Threads that need to
166 * be swapped in are swapped in by this thread.
169 swapin_thread_continue(void)
171 register thread_t thread
;
174 simple_lock(&swapin_lock
);
176 while ((thread
= (thread_t
)dequeue_head(&swapin_queue
)) != THREAD_NULL
) {
177 simple_unlock(&swapin_lock
);
180 thread_doswapin(thread
);
183 simple_lock(&swapin_lock
);
186 assert_wait((event_t
)&swapin_queue
, THREAD_UNINT
);
187 simple_unlock(&swapin_lock
);
190 counter(c_swapin_thread_block
++);
191 thread_block(swapin_thread_continue
);
198 thread_t self
= current_thread();
200 stack_privilege(self
);
202 swapin_thread_continue();