]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58
59 /*
60 * Mach kernel startup.
61 */
62
63 #include <debug.h>
64 #include <xpr_debug.h>
65 #include <mach_kdp.h>
66 #include <mach_host.h>
67 #include <norma_vm.h>
68
69 #include <mach/boolean.h>
70 #include <mach/machine.h>
71 #include <mach/thread_act.h>
72 #include <mach/task_special_ports.h>
73 #include <mach/vm_param.h>
74 #include <ipc/ipc_init.h>
75 #include <kern/assert.h>
76 #include <kern/misc_protos.h>
77 #include <kern/clock.h>
78 #include <kern/cpu_number.h>
79 #include <kern/ledger.h>
80 #include <kern/machine.h>
81 #include <kern/processor.h>
82 #include <kern/sched_prim.h>
83 #include <kern/startup.h>
84 #include <kern/task.h>
85 #include <kern/thread.h>
86 #include <kern/timer.h>
87 #include <kern/xpr.h>
88 #include <kern/zalloc.h>
89 #include <kern/locks.h>
90 #include <vm/vm_shared_memory_server.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_init.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pageout.h>
97 #include <machine/pmap.h>
98 #include <machine/commpage.h>
99 #include <libkern/version.h>
100
101 #ifdef __ppc__
102 #include <ppc/Firmware.h>
103 #include <ppc/mappings.h>
104 #include <ppc/serial_io.h>
105 #endif
106
107 static void kernel_bootstrap_thread(void);
108
109 static void load_context(
110 thread_t thread);
111
112 /*
113 * Running in virtual memory, on the interrupt stack.
114 */
115 void
116 kernel_bootstrap(void)
117 {
118 kern_return_t result;
119 thread_t thread;
120
121 lck_mod_init();
122 sched_init();
123 vm_mem_bootstrap();
124 ipc_bootstrap();
125 vm_mem_init();
126 ipc_init();
127
128 /*
129 * As soon as the virtual memory system is up, we record
130 * that this CPU is using the kernel pmap.
131 */
132 PMAP_ACTIVATE_KERNEL(master_cpu);
133
134 mapping_free_prime(); /* Load up with temporary mapping blocks */
135
136 machine_init();
137 kmod_init();
138 clock_init();
139
140 machine_info.memory_size = mem_size;
141 machine_info.max_mem = max_mem;
142 machine_info.major_version = version_major;
143 machine_info.minor_version = version_minor;
144
145 /*
146 * Initialize the IPC, task, and thread subsystems.
147 */
148 ledger_init();
149 task_init();
150 thread_init();
151
152 /*
153 * Create a kernel thread to execute the kernel bootstrap.
154 */
155 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
156 if (result != KERN_SUCCESS)
157 panic("kernel_bootstrap");
158
159 thread->state = TH_RUN;
160 thread_deallocate(thread);
161
162 load_context(thread);
163 /*NOTREACHED*/
164 }
165
166 /*
167 * Now running in a thread. Kick off other services,
168 * invoke user bootstrap, enter pageout loop.
169 */
170 static void
171 kernel_bootstrap_thread(void)
172 {
173 processor_t processor = current_processor();
174 thread_t self = current_thread();
175
176 /*
177 * Create the idle processor thread.
178 */
179 idle_thread_create(processor);
180
181 /*
182 * N.B. Do not stick anything else
183 * before this point.
184 *
185 * Start up the scheduler services.
186 */
187 sched_startup();
188
189 /*
190 * Remain on current processor as
191 * additional processors come online.
192 */
193 thread_bind(self, processor);
194
195 /*
196 * Kick off memory mapping adjustments.
197 */
198 mapping_adjust();
199
200 /*
201 * Create the clock service.
202 */
203 clock_service_create();
204
205 /*
206 * Create the device service.
207 */
208 device_service_create();
209
210 shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
211
212 #ifdef IOKIT
213 {
214 PE_init_iokit();
215 }
216 #endif
217
218 (void) spllo(); /* Allow interruptions */
219
220 /*
221 * Fill in the comm area (mapped into every task address space.)
222 */
223 commpage_populate();
224
225 /*
226 * Start the user bootstrap.
227 */
228 #ifdef MACH_BSD
229 {
230 bsd_init();
231 }
232 #endif
233
234 #if __ppc__
235 serial_keyboard_init(); /* Start serial keyboard if wanted */
236 #endif
237
238 thread_bind(self, PROCESSOR_NULL);
239
240 /*
241 * Become the pageout daemon.
242 */
243 vm_pageout();
244 /*NOTREACHED*/
245 }
246
247 /*
248 * slave_main:
249 *
250 * Load the first thread to start a processor.
251 */
252 void
253 slave_main(void)
254 {
255 processor_t processor = current_processor();
256 thread_t thread;
257
258 /*
259 * Use the idle processor thread if there
260 * is no dedicated start up thread.
261 */
262 if (processor->next_thread == THREAD_NULL) {
263 thread = processor->idle_thread;
264 thread->continuation = (thread_continue_t)processor_start_thread;
265 thread->parameter = NULL;
266 }
267 else {
268 thread = processor->next_thread;
269 processor->next_thread = THREAD_NULL;
270 }
271
272 load_context(thread);
273 /*NOTREACHED*/
274 }
275
276 /*
277 * processor_start_thread:
278 *
279 * First thread to execute on a started processor.
280 *
281 * Called at splsched.
282 */
283 void
284 processor_start_thread(void)
285 {
286 processor_t processor = current_processor();
287 thread_t self = current_thread();
288
289 slave_machine_init();
290
291 /*
292 * If running the idle processor thread,
293 * reenter the idle loop, else terminate.
294 */
295 if (self == processor->idle_thread)
296 thread_block((thread_continue_t)idle_thread);
297
298 thread_terminate(self);
299 /*NOTREACHED*/
300 }
301
302 /*
303 * load_context:
304 *
305 * Start the first thread on a processor.
306 */
307 static void
308 load_context(
309 thread_t thread)
310 {
311 processor_t processor = current_processor();
312
313 machine_set_current_thread(thread);
314 processor_up(processor);
315
316 PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
317
318 /*
319 * Acquire a stack if none attached. The panic
320 * should never occur since the thread is expected
321 * to have reserved stack.
322 */
323 if (!thread->kernel_stack) {
324 if (!stack_alloc_try(thread))
325 panic("load_context");
326 }
327
328 /*
329 * The idle processor threads are not counted as
330 * running for load calculations.
331 */
332 if (!(thread->state & TH_IDLE))
333 pset_run_incr(thread->processor_set);
334
335 processor->active_thread = thread;
336 processor->current_pri = thread->sched_pri;
337 processor->deadline = UINT64_MAX;
338 thread->last_processor = processor;
339
340 processor->last_dispatch = mach_absolute_time();
341 timer_switch((uint32_t)processor->last_dispatch,
342 &PROCESSOR_DATA(processor, offline_timer));
343
344 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
345
346 machine_load_context(thread);
347 /*NOTREACHED*/
348 }