]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
d7e50217 A |
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 | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
d7e50217 A |
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. | |
1c79356b A |
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 | */ | |
1c79356b A |
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> | |
1c79356b A |
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; | |
9bccf70c | 70 | decl_simple_lock_data(,swapin_lock) |
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); | |
9bccf70c | 85 | simple_lock_init(&swapin_lock, ETAP_THREAD_SWAPPER); |
0b4e3aa0 A |
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 | * | |
9bccf70c A |
94 | * Place the specified thread in the list of threads to swapin. |
95 | * Called with thread locked, returned unlocked. | |
1c79356b A |
96 | */ |
97 | ||
0b4e3aa0 A |
98 | void |
99 | thread_swapin( | |
100 | register thread_t thread) | |
1c79356b A |
101 | { |
102 | switch (thread->state & TH_STACK_STATE) { | |
0b4e3aa0 | 103 | |
1c79356b A |
104 | case TH_STACK_HANDOFF: |
105 | /* | |
9bccf70c | 106 | * Swapped out. |
1c79356b | 107 | */ |
0b4e3aa0 | 108 | thread->state = (thread->state & ~TH_STACK_STATE) | TH_STACK_ALLOC; |
9bccf70c A |
109 | thread_unlock(thread); |
110 | simple_lock(&swapin_lock); | |
1c79356b | 111 | enqueue_tail(&swapin_queue, (queue_entry_t) thread); |
9bccf70c A |
112 | simple_unlock(&swapin_lock); |
113 | thread_wakeup((event_t)&swapin_queue); | |
1c79356b A |
114 | break; |
115 | ||
9bccf70c | 116 | case TH_STACK_ALLOC: |
1c79356b | 117 | /* |
9bccf70c | 118 | * Already queued. |
1c79356b | 119 | */ |
9bccf70c | 120 | thread_unlock(thread); |
1c79356b A |
121 | break; |
122 | ||
9bccf70c | 123 | default: |
1c79356b A |
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 | |
9bccf70c | 135 | * it on a run queue. |
1c79356b | 136 | */ |
0b4e3aa0 A |
137 | void |
138 | thread_doswapin( | |
139 | register thread_t thread) | |
1c79356b | 140 | { |
0b4e3aa0 A |
141 | vm_offset_t stack; |
142 | spl_t s; | |
1c79356b A |
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 | */ | |
1c79356b A |
153 | s = splsched(); |
154 | thread_lock(thread); | |
0b4e3aa0 | 155 | thread->state &= ~(TH_STACK_HANDOFF | TH_STACK_ALLOC); |
1c79356b | 156 | if (thread->state & TH_RUN) |
9bccf70c | 157 | thread_setrun(thread, HEAD_Q); |
1c79356b A |
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 | */ | |
0b4e3aa0 A |
168 | void |
169 | swapin_thread_continue(void) | |
1c79356b | 170 | { |
0b4e3aa0 A |
171 | register thread_t thread; |
172 | ||
0b4e3aa0 | 173 | (void)splsched(); |
9bccf70c | 174 | simple_lock(&swapin_lock); |
1c79356b | 175 | |
0b4e3aa0 | 176 | while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) { |
9bccf70c | 177 | simple_unlock(&swapin_lock); |
0b4e3aa0 | 178 | (void)spllo(); |
1c79356b | 179 | |
0b4e3aa0 | 180 | thread_doswapin(thread); |
1c79356b | 181 | |
0b4e3aa0 | 182 | (void)splsched(); |
9bccf70c | 183 | simple_lock(&swapin_lock); |
0b4e3aa0 | 184 | } |
1c79356b | 185 | |
9bccf70c A |
186 | assert_wait((event_t)&swapin_queue, THREAD_UNINT); |
187 | simple_unlock(&swapin_lock); | |
0b4e3aa0 | 188 | (void)spllo(); |
1c79356b | 189 | |
0b4e3aa0 | 190 | counter(c_swapin_thread_block++); |
0b4e3aa0 | 191 | thread_block(swapin_thread_continue); |
0b4e3aa0 | 192 | /*NOTREACHED*/ |
1c79356b A |
193 | } |
194 | ||
0b4e3aa0 A |
195 | void |
196 | swapin_thread(void) | |
1c79356b | 197 | { |
0b4e3aa0 A |
198 | thread_t self = current_thread(); |
199 | ||
200 | stack_privilege(self); | |
1c79356b A |
201 | |
202 | swapin_thread_continue(); | |
203 | /*NOTREACHED*/ | |
204 | } |