]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
de355530 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
de355530 A |
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. | |
1c79356b A |
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; | |
9bccf70c | 67 | decl_simple_lock_data(,swapin_lock) |
1c79356b A |
68 | |
69 | mach_counter_t c_swapin_thread_block; | |
70 | ||
0b4e3aa0 A |
71 | void swapin_thread(void); |
72 | ||
1c79356b | 73 | /* |
0b4e3aa0 | 74 | * swapin_init: [exported] |
1c79356b A |
75 | * |
76 | * Initialize the swapper module. | |
77 | */ | |
0b4e3aa0 A |
78 | void |
79 | swapin_init(void) | |
1c79356b A |
80 | { |
81 | queue_init(&swapin_queue); | |
9bccf70c | 82 | simple_lock_init(&swapin_lock, ETAP_THREAD_SWAPPER); |
0b4e3aa0 A |
83 | kernel_thread_with_priority( |
84 | kernel_task, BASEPRI_PREEMPT - 2, | |
85 | swapin_thread, TRUE, TRUE); | |
1c79356b A |
86 | } |
87 | ||
88 | /* | |
89 | * thread_swapin: [exported] | |
90 | * | |
9bccf70c A |
91 | * Place the specified thread in the list of threads to swapin. |
92 | * Called with thread locked, returned unlocked. | |
1c79356b A |
93 | */ |
94 | ||
0b4e3aa0 A |
95 | void |
96 | thread_swapin( | |
97 | register thread_t thread) | |
1c79356b A |
98 | { |
99 | switch (thread->state & TH_STACK_STATE) { | |
0b4e3aa0 | 100 | |
1c79356b A |
101 | case TH_STACK_HANDOFF: |
102 | /* | |
9bccf70c | 103 | * Swapped out. |
1c79356b | 104 | */ |
0b4e3aa0 | 105 | thread->state = (thread->state & ~TH_STACK_STATE) | TH_STACK_ALLOC; |
9bccf70c A |
106 | thread_unlock(thread); |
107 | simple_lock(&swapin_lock); | |
1c79356b | 108 | enqueue_tail(&swapin_queue, (queue_entry_t) thread); |
9bccf70c A |
109 | simple_unlock(&swapin_lock); |
110 | thread_wakeup((event_t)&swapin_queue); | |
1c79356b A |
111 | break; |
112 | ||
9bccf70c | 113 | case TH_STACK_ALLOC: |
1c79356b | 114 | /* |
9bccf70c | 115 | * Already queued. |
1c79356b | 116 | */ |
9bccf70c | 117 | thread_unlock(thread); |
1c79356b A |
118 | break; |
119 | ||
9bccf70c | 120 | default: |
1c79356b A |
121 | /* |
122 | * Already swapped in. | |
123 | */ | |
124 | panic("thread_swapin"); | |
125 | } | |
126 | } | |
127 | ||
128 | /* | |
129 | * thread_doswapin: | |
130 | * | |
131 | * Swapin the specified thread, if it should be runnable, then put | |
9bccf70c | 132 | * it on a run queue. |
1c79356b | 133 | */ |
0b4e3aa0 A |
134 | void |
135 | thread_doswapin( | |
136 | register thread_t thread) | |
1c79356b | 137 | { |
0b4e3aa0 A |
138 | vm_offset_t stack; |
139 | spl_t s; | |
1c79356b A |
140 | |
141 | /* | |
142 | * Allocate the kernel stack. | |
143 | */ | |
144 | stack = stack_alloc(thread, thread_continue); | |
145 | assert(stack); | |
146 | ||
147 | /* | |
148 | * Place on run queue. | |
149 | */ | |
1c79356b A |
150 | s = splsched(); |
151 | thread_lock(thread); | |
0b4e3aa0 | 152 | thread->state &= ~(TH_STACK_HANDOFF | TH_STACK_ALLOC); |
1c79356b | 153 | if (thread->state & TH_RUN) |
9bccf70c | 154 | thread_setrun(thread, HEAD_Q); |
1c79356b A |
155 | thread_unlock(thread); |
156 | (void) splx(s); | |
157 | } | |
158 | ||
159 | /* | |
160 | * swapin_thread: [exported] | |
161 | * | |
162 | * This procedure executes as a kernel thread. Threads that need to | |
163 | * be swapped in are swapped in by this thread. | |
164 | */ | |
0b4e3aa0 A |
165 | void |
166 | swapin_thread_continue(void) | |
1c79356b | 167 | { |
0b4e3aa0 A |
168 | register thread_t thread; |
169 | ||
0b4e3aa0 | 170 | (void)splsched(); |
9bccf70c | 171 | simple_lock(&swapin_lock); |
1c79356b | 172 | |
0b4e3aa0 | 173 | while ((thread = (thread_t)dequeue_head(&swapin_queue)) != THREAD_NULL) { |
9bccf70c | 174 | simple_unlock(&swapin_lock); |
0b4e3aa0 | 175 | (void)spllo(); |
1c79356b | 176 | |
0b4e3aa0 | 177 | thread_doswapin(thread); |
1c79356b | 178 | |
0b4e3aa0 | 179 | (void)splsched(); |
9bccf70c | 180 | simple_lock(&swapin_lock); |
0b4e3aa0 | 181 | } |
1c79356b | 182 | |
9bccf70c A |
183 | assert_wait((event_t)&swapin_queue, THREAD_UNINT); |
184 | simple_unlock(&swapin_lock); | |
0b4e3aa0 | 185 | (void)spllo(); |
1c79356b | 186 | |
0b4e3aa0 | 187 | counter(c_swapin_thread_block++); |
0b4e3aa0 | 188 | thread_block(swapin_thread_continue); |
0b4e3aa0 | 189 | /*NOTREACHED*/ |
1c79356b A |
190 | } |
191 | ||
0b4e3aa0 A |
192 | void |
193 | swapin_thread(void) | |
1c79356b | 194 | { |
0b4e3aa0 A |
195 | thread_t self = current_thread(); |
196 | ||
197 | stack_privilege(self); | |
1c79356b A |
198 | |
199 | swapin_thread_continue(); | |
200 | /*NOTREACHED*/ | |
201 | } |