]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/thread_swap.c
xnu-124.13.tar.gz
[apple/xnu.git] / osfmk / kern / thread_swap.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * @OSF_COPYRIGHT@
24 */
25/*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50/*
51 */
52/*
53 *
54 * File: kern/thread_swap.c
55 * Author: Avadis Tevanian, Jr.
56 * Date: 1987
57 *
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.
62 *
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.
67 *
68 */
69
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>
76#include <kern/sf.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>
83
84queue_head_t swapin_queue;
85decl_simple_lock_data(, swapper_lock_data)
86
87#define swapper_lock() simple_lock(&swapper_lock_data)
88#define swapper_unlock() simple_unlock(&swapper_lock_data)
89
90mach_counter_t c_swapin_thread_block;
91
92/*
93 * swapper_init: [exported]
94 *
95 * Initialize the swapper module.
96 */
97void swapper_init()
98{
99 queue_init(&swapin_queue);
100 simple_lock_init(&swapper_lock_data, ETAP_THREAD_SWAPPER);
101}
102
103/*
104 * thread_swapin: [exported]
105 *
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.
108 *
109 * We don't bother with stack_alloc_try to optimize swapin;
110 * our callers have already tried that route.
111 */
112
113void thread_swapin(thread)
114 thread_t thread;
115{
116 switch (thread->state & TH_STACK_STATE) {
117 case TH_STACK_HANDOFF:
118 /*
119 * Swapped out - queue for swapin thread.
120 */
121 thread->state = (thread->state & ~TH_STACK_STATE)
122 | TH_STACK_COMING_IN;
123 swapper_lock();
124 enqueue_tail(&swapin_queue, (queue_entry_t) thread);
125 swapper_unlock();
126 thread_wakeup((event_t) &swapin_queue);
127 break;
128
129 case TH_STACK_COMING_IN:
130 /*
131 * Already queued for swapin thread, or being
132 * swapped in.
133 */
134 break;
135
136 default:
137 /*
138 * Already swapped in.
139 */
140 panic("thread_swapin");
141 }
142}
143
144/*
145 * thread_doswapin:
146 *
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).
150 */
151void thread_doswapin(thread)
152 register thread_t thread;
153{
154 spl_t s;
155 vm_offset_t stack;
156
157 /*
158 * do machdep allocation
159 */
160
161 /*
162 * Allocate the kernel stack.
163 */
164 stack = stack_alloc(thread, thread_continue);
165 assert(stack);
166
167 /*
168 * Place on run queue.
169 */
170
171 s = splsched();
172 thread_lock(thread);
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);
177 (void) splx(s);
178}
179
180/*
181 * swapin_thread: [exported]
182 *
183 * This procedure executes as a kernel thread. Threads that need to
184 * be swapped in are swapped in by this thread.
185 */
186void swapin_thread_continue()
187{
188 for (;;) {
189 register thread_t thread;
190 spl_t s;
191
192 s = splsched();
193 swapper_lock();
194
195 while ((thread = (thread_t) dequeue_head(&swapin_queue))
196 != THREAD_NULL) {
197 swapper_unlock();
198 (void) splx(s);
199
200 thread_doswapin(thread); /* may block */
201
202 s = splsched();
203 swapper_lock();
204 }
205
206 assert_wait((event_t) &swapin_queue, THREAD_UNINT);
207 swapper_unlock();
208 (void) splx(s);
209 counter(c_swapin_thread_block++);
210#if defined (__i386__)
211 thread_block((void (*)(void)) 0);
212#else
213 thread_block(swapin_thread_continue);
214#endif
215 }
216}
217
218void swapin_thread()
219{
220 stack_privilege(current_thread());
221 current_thread()->vm_privilege = TRUE;
222
223 swapin_thread_continue();
224 /*NOTREACHED*/
225}