]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2005 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 <console/serial_protos.h>
91 #include <vm/vm_shared_memory_server.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_init.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pageout.h>
98 #include <machine/pmap.h>
99 #include <machine/commpage.h>
100 #include <libkern/version.h>
101
102 #ifdef __ppc__
103 #include <ppc/Firmware.h>
104 #include <ppc/mappings.h>
105 #endif
106
107 static void kernel_bootstrap_thread(void);
108
109 static void load_context(
110 thread_t thread);
111
112 #ifdef i386
113 extern void cpu_window_init(int);
114 #endif
115
116
117 /*
118 * Running in virtual memory, on the interrupt stack.
119 */
120
121 void
122 kernel_bootstrap(void)
123 {
124 kern_return_t result;
125 thread_t thread;
126
127 lck_mod_init();
128 sched_init();
129 vm_mem_bootstrap();
130 ipc_bootstrap();
131 vm_mem_init();
132 ipc_init();
133
134 /*
135 * As soon as the virtual memory system is up, we record
136 * that this CPU is using the kernel pmap.
137 */
138 PMAP_ACTIVATE_KERNEL(master_cpu);
139
140 mapping_free_prime(); /* Load up with temporary mapping blocks */
141
142 machine_init();
143 kmod_init();
144 clock_init();
145
146 machine_info.memory_size = mem_size;
147 machine_info.max_mem = max_mem;
148 machine_info.major_version = version_major;
149 machine_info.minor_version = version_minor;
150
151 /*
152 * Initialize the IPC, task, and thread subsystems.
153 */
154 ledger_init();
155 task_init();
156 thread_init();
157
158 /*
159 * Create a kernel thread to execute the kernel bootstrap.
160 */
161 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
162
163 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
164
165 thread->state = TH_RUN;
166 thread_deallocate(thread);
167
168 load_context(thread);
169 /*NOTREACHED*/
170 }
171
172 /*
173 * Now running in a thread. Kick off other services,
174 * invoke user bootstrap, enter pageout loop.
175 */
176 static void
177 kernel_bootstrap_thread(void)
178 {
179 processor_t processor = current_processor();
180 thread_t self = current_thread();
181
182 /*
183 * Create the idle processor thread.
184 */
185 idle_thread_create(processor);
186
187 /*
188 * N.B. Do not stick anything else
189 * before this point.
190 *
191 * Start up the scheduler services.
192 */
193 sched_startup();
194
195 /*
196 * Remain on current processor as
197 * additional processors come online.
198 */
199 thread_bind(self, processor);
200
201 /*
202 * Kick off memory mapping adjustments.
203 */
204 mapping_adjust();
205
206 /*
207 * Create the clock service.
208 */
209 clock_service_create();
210
211 /*
212 * Create the device service.
213 */
214 device_service_create();
215
216 shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
217
218 #ifdef IOKIT
219 {
220 PE_init_iokit();
221 }
222 #endif
223
224 (void) spllo(); /* Allow interruptions */
225
226 /*
227 * Fill in the comm area (mapped into every task address space.)
228 */
229 commpage_populate();
230
231 #ifdef i386
232 /*
233 * create and initialize a copy window
234 * for processor 0
235 */
236 cpu_window_init(0);
237 #endif
238
239 /*
240 * Start the user bootstrap.
241 */
242 #ifdef MACH_BSD
243 {
244 bsd_init();
245 }
246 #endif
247
248 serial_keyboard_init(); /* Start serial keyboard if wanted */
249
250 thread_bind(self, PROCESSOR_NULL);
251
252 /*
253 * Become the pageout daemon.
254 */
255 vm_pageout();
256 /*NOTREACHED*/
257 }
258
259 /*
260 * slave_main:
261 *
262 * Load the first thread to start a processor.
263 */
264 void
265 slave_main(void)
266 {
267 processor_t processor = current_processor();
268 thread_t thread;
269
270 /*
271 * Use the idle processor thread if there
272 * is no dedicated start up thread.
273 */
274 if (processor->next_thread == THREAD_NULL) {
275 thread = processor->idle_thread;
276 thread->continuation = (thread_continue_t)processor_start_thread;
277 thread->parameter = NULL;
278 }
279 else {
280 thread = processor->next_thread;
281 processor->next_thread = THREAD_NULL;
282 }
283
284 load_context(thread);
285 /*NOTREACHED*/
286 }
287
288 /*
289 * processor_start_thread:
290 *
291 * First thread to execute on a started processor.
292 *
293 * Called at splsched.
294 */
295 void
296 processor_start_thread(void)
297 {
298 processor_t processor = current_processor();
299 thread_t self = current_thread();
300
301 slave_machine_init();
302
303 /*
304 * If running the idle processor thread,
305 * reenter the idle loop, else terminate.
306 */
307 if (self == processor->idle_thread)
308 thread_block((thread_continue_t)idle_thread);
309
310 thread_terminate(self);
311 /*NOTREACHED*/
312 }
313
314 /*
315 * load_context:
316 *
317 * Start the first thread on a processor.
318 */
319 static void
320 load_context(
321 thread_t thread)
322 {
323 processor_t processor = current_processor();
324
325 machine_set_current_thread(thread);
326 processor_up(processor);
327
328 PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
329
330 /*
331 * Acquire a stack if none attached. The panic
332 * should never occur since the thread is expected
333 * to have reserved stack.
334 */
335 if (!thread->kernel_stack) {
336 if (!stack_alloc_try(thread))
337 panic("load_context");
338 }
339
340 /*
341 * The idle processor threads are not counted as
342 * running for load calculations.
343 */
344 if (!(thread->state & TH_IDLE))
345 pset_run_incr(thread->processor_set);
346
347 processor->active_thread = thread;
348 processor->current_pri = thread->sched_pri;
349 processor->deadline = UINT64_MAX;
350 thread->last_processor = processor;
351
352 processor->last_dispatch = mach_absolute_time();
353 timer_switch((uint32_t)processor->last_dispatch,
354 &PROCESSOR_DATA(processor, offline_timer));
355
356 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
357
358 machine_load_context(thread);
359 /*NOTREACHED*/
360 }