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.
54 * File: kern/thread_swap.c
55 * Author: Avadis Tevanian, Jr.
58 * Mach thread swapper:
59 * Swap in threads that need to be run. This is done here
60 * by the swapper thread since it cannot be done (in general)
61 * when the kernel tries to place a thread on a run queue.
63 * Note: The act of swapping a thread in Mach does not mean that
64 * its memory gets forcibly swapped to secondary storage. The memory
65 * for the task corresponding to a swapped thread is paged out
66 * through the normal paging mechanism.
70 #include <kern/thread.h>
71 #include <kern/lock.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_kern.h>
74 #include <mach/vm_param.h>
75 #include <kern/sched_prim.h>
77 #include <kern/processor.h>
78 #include <kern/thread_swap.h>
79 #include <kern/spl.h> /* for splsched */
80 #include <kern/misc_protos.h>
81 #include <kern/counters.h>
82 #include <mach/policy.h>
84 queue_head_t swapin_queue
;
85 decl_simple_lock_data(, swapper_lock_data
)
87 #define swapper_lock() simple_lock(&swapper_lock_data)
88 #define swapper_unlock() simple_unlock(&swapper_lock_data)
90 mach_counter_t c_swapin_thread_block
;
93 * swapper_init: [exported]
95 * Initialize the swapper module.
99 queue_init(&swapin_queue
);
100 simple_lock_init(&swapper_lock_data
, ETAP_THREAD_SWAPPER
);
104 * thread_swapin: [exported]
106 * Place the specified thread in the list of threads to swapin. It
107 * is assumed that the thread is locked, therefore we are at splsched.
109 * We don't bother with stack_alloc_try to optimize swapin;
110 * our callers have already tried that route.
113 void thread_swapin(thread
)
116 switch (thread
->state
& TH_STACK_STATE
) {
117 case TH_STACK_HANDOFF
:
119 * Swapped out - queue for swapin thread.
121 thread
->state
= (thread
->state
& ~TH_STACK_STATE
)
122 | TH_STACK_COMING_IN
;
124 enqueue_tail(&swapin_queue
, (queue_entry_t
) thread
);
126 thread_wakeup((event_t
) &swapin_queue
);
129 case TH_STACK_COMING_IN
:
131 * Already queued for swapin thread, or being
138 * Already swapped in.
140 panic("thread_swapin");
147 * Swapin the specified thread, if it should be runnable, then put
148 * it on a run queue. No locks should be held on entry, as it is
149 * likely that this routine will sleep (waiting for stack allocation).
151 void thread_doswapin(thread
)
152 register thread_t thread
;
158 * do machdep allocation
162 * Allocate the kernel stack.
164 stack
= stack_alloc(thread
, thread_continue
);
168 * Place on run queue.
173 thread
->state
&= ~(TH_STACK_HANDOFF
| TH_STACK_COMING_IN
);
174 if (thread
->state
& TH_RUN
)
175 thread_setrun(thread
, TRUE
, FALSE
);
176 thread_unlock(thread
);
181 * swapin_thread: [exported]
183 * This procedure executes as a kernel thread. Threads that need to
184 * be swapped in are swapped in by this thread.
186 void swapin_thread_continue()
189 register thread_t thread
;
195 while ((thread
= (thread_t
) dequeue_head(&swapin_queue
))
200 thread_doswapin(thread
); /* may block */
206 assert_wait((event_t
) &swapin_queue
, THREAD_UNINT
);
209 counter(c_swapin_thread_block
++);
210 #if defined (__i386__)
211 thread_block((void (*)(void)) 0);
213 thread_block(swapin_thread_continue
);
220 stack_privilege(current_thread());
221 current_thread()->vm_privilege
= TRUE
;
223 swapin_thread_continue();