]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. | |
7 | * | |
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 | |
13 | * file. | |
14 | * | |
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. | |
22 | * | |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * @OSF_COPYRIGHT@ | |
27 | */ | |
28 | /* | |
29 | * Mach Operating System | |
30 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
31 | * All Rights Reserved. | |
32 | * | |
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. | |
38 | * | |
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. | |
42 | * | |
43 | * Carnegie Mellon requests users of this software to return to | |
44 | * | |
45 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
46 | * School of Computer Science | |
47 | * Carnegie Mellon University | |
48 | * Pittsburgh PA 15213-3890 | |
49 | * | |
50 | * any improvements or extensions that they make and grant Carnegie Mellon | |
51 | * the rights to redistribute these changes. | |
52 | */ | |
53 | /* | |
54 | */ | |
55 | ||
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> | |
68 | ||
69 | queue_head_t swapin_queue; | |
70 | decl_simple_lock_data(,swapin_lock) | |
71 | ||
72 | mach_counter_t c_swapin_thread_block; | |
73 | ||
74 | void swapin_thread(void); | |
75 | ||
76 | /* | |
77 | * swapin_init: [exported] | |
78 | * | |
79 | * Initialize the swapper module. | |
80 | */ | |
81 | void | |
82 | swapin_init(void) | |
83 | { | |
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); | |
89 | } | |
90 | ||
91 | /* | |
92 | * thread_swapin: [exported] | |
93 | * | |
94 | * Place the specified thread in the list of threads to swapin. | |
95 | * Called with thread locked, returned unlocked. | |
96 | */ | |
97 | ||
98 | void | |
99 | thread_swapin( | |
100 | register thread_t thread) | |
101 | { | |
102 | switch (thread->state & TH_STACK_STATE) { | |
103 | ||
104 | case TH_STACK_HANDOFF: | |
105 | /* | |
106 | * Swapped out. | |
107 | */ | |
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); | |
114 | break; | |
115 | ||
116 | case TH_STACK_ALLOC: | |
117 | /* | |
118 | * Already queued. | |
119 | */ | |
120 | thread_unlock(thread); | |
121 | break; | |
122 | ||
123 | default: | |
124 | /* | |
125 | * Already swapped in. | |
126 | */ | |
127 | panic("thread_swapin"); | |
128 | } | |
129 | } | |
130 | ||
131 | /* | |
132 | * thread_doswapin: | |
133 | * | |
134 | * Swapin the specified thread, if it should be runnable, then put | |
135 | * it on a run queue. | |
136 | */ | |
137 | void | |
138 | thread_doswapin( | |
139 | register thread_t thread) | |
140 | { | |
141 | vm_offset_t stack; | |
142 | spl_t s; | |
143 | ||
144 | /* | |
145 | * Allocate the kernel stack. | |
146 | */ | |
147 | stack = stack_alloc(thread, thread_continue); | |
148 | assert(stack); | |
149 | ||
150 | /* | |
151 | * Place on run queue. | |
152 | */ | |
153 | s = splsched(); | |
154 | thread_lock(thread); | |
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); | |
159 | (void) splx(s); | |
160 | } | |
161 | ||
162 | /* | |
163 | * swapin_thread: [exported] | |
164 | * | |
165 | * This procedure executes as a kernel thread. Threads that need to | |
166 | * be swapped in are swapped in by this thread. | |
167 | */ | |
168 | void | |
169 | swapin_thread_continue(void) | |
170 | { | |
171 | register thread_t thread; | |
172 | ||
173 | (void)splsched(); | |
174 | simple_lock(&swapin_lock); | |
175 | ||
176 | while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) { | |
177 | simple_unlock(&swapin_lock); | |
178 | (void)spllo(); | |
179 | ||
180 | thread_doswapin(thread); | |
181 | ||
182 | (void)splsched(); | |
183 | simple_lock(&swapin_lock); | |
184 | } | |
185 | ||
186 | assert_wait((event_t)&swapin_queue, THREAD_UNINT); | |
187 | simple_unlock(&swapin_lock); | |
188 | (void)spllo(); | |
189 | ||
190 | counter(c_swapin_thread_block++); | |
191 | thread_block(swapin_thread_continue); | |
192 | /*NOTREACHED*/ | |
193 | } | |
194 | ||
195 | void | |
196 | swapin_thread(void) | |
197 | { | |
198 | thread_t self = current_thread(); | |
199 | ||
200 | stack_privilege(self); | |
201 | ||
202 | swapin_thread_continue(); | |
203 | /*NOTREACHED*/ | |
204 | } |