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