]>
Commit | Line | Data |
---|---|---|
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 | */ | |
1c79356b A |
52 | |
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> | |
1c79356b A |
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> | |
65 | ||
66 | queue_head_t swapin_queue; | |
0b4e3aa0 | 67 | decl_simple_lock_data(,swapin_lock_data) |
1c79356b | 68 | |
0b4e3aa0 A |
69 | #define swapin_lock() simple_lock(&swapin_lock_data) |
70 | #define swapin_unlock() simple_unlock(&swapin_lock_data) | |
1c79356b A |
71 | |
72 | mach_counter_t c_swapin_thread_block; | |
73 | ||
0b4e3aa0 A |
74 | void swapin_thread(void); |
75 | ||
1c79356b | 76 | /* |
0b4e3aa0 | 77 | * swapin_init: [exported] |
1c79356b A |
78 | * |
79 | * Initialize the swapper module. | |
80 | */ | |
0b4e3aa0 A |
81 | void |
82 | swapin_init(void) | |
1c79356b A |
83 | { |
84 | queue_init(&swapin_queue); | |
0b4e3aa0 A |
85 | simple_lock_init(&swapin_lock_data, ETAP_THREAD_SWAPPER); |
86 | kernel_thread_with_priority( | |
87 | kernel_task, BASEPRI_PREEMPT - 2, | |
88 | swapin_thread, TRUE, TRUE); | |
1c79356b A |
89 | } |
90 | ||
91 | /* | |
92 | * thread_swapin: [exported] | |
93 | * | |
94 | * Place the specified thread in the list of threads to swapin. It | |
95 | * is assumed that the thread is locked, therefore we are at splsched. | |
96 | * | |
97 | * We don't bother with stack_alloc_try to optimize swapin; | |
98 | * our callers have already tried that route. | |
99 | */ | |
100 | ||
0b4e3aa0 A |
101 | void |
102 | thread_swapin( | |
103 | register thread_t thread) | |
1c79356b A |
104 | { |
105 | switch (thread->state & TH_STACK_STATE) { | |
0b4e3aa0 | 106 | |
1c79356b A |
107 | case TH_STACK_HANDOFF: |
108 | /* | |
109 | * Swapped out - queue for swapin thread. | |
110 | */ | |
0b4e3aa0 A |
111 | thread->state = (thread->state & ~TH_STACK_STATE) | TH_STACK_ALLOC; |
112 | swapin_lock(); | |
1c79356b | 113 | enqueue_tail(&swapin_queue, (queue_entry_t) thread); |
0b4e3aa0 | 114 | swapin_unlock(); |
1c79356b A |
115 | thread_wakeup((event_t) &swapin_queue); |
116 | break; | |
117 | ||
0b4e3aa0 | 118 | case TH_STACK_ALLOC: |
1c79356b A |
119 | /* |
120 | * Already queued for swapin thread, or being | |
121 | * swapped in. | |
122 | */ | |
123 | break; | |
124 | ||
125 | default: | |
126 | /* | |
127 | * Already swapped in. | |
128 | */ | |
129 | panic("thread_swapin"); | |
130 | } | |
131 | } | |
132 | ||
133 | /* | |
134 | * thread_doswapin: | |
135 | * | |
136 | * Swapin the specified thread, if it should be runnable, then put | |
137 | * it on a run queue. No locks should be held on entry, as it is | |
138 | * likely that this routine will sleep (waiting for stack allocation). | |
139 | */ | |
0b4e3aa0 A |
140 | void |
141 | thread_doswapin( | |
142 | register thread_t thread) | |
1c79356b | 143 | { |
0b4e3aa0 A |
144 | vm_offset_t stack; |
145 | spl_t s; | |
1c79356b A |
146 | |
147 | /* | |
148 | * Allocate the kernel stack. | |
149 | */ | |
150 | stack = stack_alloc(thread, thread_continue); | |
151 | assert(stack); | |
152 | ||
153 | /* | |
154 | * Place on run queue. | |
155 | */ | |
156 | ||
157 | s = splsched(); | |
158 | thread_lock(thread); | |
0b4e3aa0 | 159 | thread->state &= ~(TH_STACK_HANDOFF | TH_STACK_ALLOC); |
1c79356b A |
160 | if (thread->state & TH_RUN) |
161 | thread_setrun(thread, TRUE, FALSE); | |
162 | thread_unlock(thread); | |
163 | (void) splx(s); | |
164 | } | |
165 | ||
166 | /* | |
167 | * swapin_thread: [exported] | |
168 | * | |
169 | * This procedure executes as a kernel thread. Threads that need to | |
170 | * be swapped in are swapped in by this thread. | |
171 | */ | |
0b4e3aa0 A |
172 | void |
173 | swapin_thread_continue(void) | |
1c79356b | 174 | { |
0b4e3aa0 A |
175 | register thread_t thread; |
176 | ||
177 | #if defined(__i386__) | |
178 | loop: | |
179 | #endif | |
180 | (void)splsched(); | |
181 | swapin_lock(); | |
1c79356b | 182 | |
0b4e3aa0 A |
183 | while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) { |
184 | swapin_unlock(); | |
185 | (void)spllo(); | |
1c79356b | 186 | |
0b4e3aa0 | 187 | thread_doswapin(thread); |
1c79356b | 188 | |
0b4e3aa0 A |
189 | (void)splsched(); |
190 | swapin_lock(); | |
191 | } | |
1c79356b | 192 | |
0b4e3aa0 A |
193 | assert_wait((event_t) &swapin_queue, THREAD_UNINT); |
194 | swapin_unlock(); | |
195 | (void)spllo(); | |
1c79356b | 196 | |
0b4e3aa0 | 197 | counter(c_swapin_thread_block++); |
1c79356b | 198 | #if defined (__i386__) |
0b4e3aa0 A |
199 | thread_block((void (*)(void)) 0); |
200 | goto loop; | |
1c79356b | 201 | #else |
0b4e3aa0 | 202 | thread_block(swapin_thread_continue); |
1c79356b | 203 | #endif |
0b4e3aa0 | 204 | /*NOTREACHED*/ |
1c79356b A |
205 | } |
206 | ||
0b4e3aa0 A |
207 | void |
208 | swapin_thread(void) | |
1c79356b | 209 | { |
0b4e3aa0 A |
210 | thread_t self = current_thread(); |
211 | ||
212 | stack_privilege(self); | |
1c79356b A |
213 | |
214 | swapin_thread_continue(); | |
215 | /*NOTREACHED*/ | |
216 | } |