]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
54596deed7aa408dbe39184b0d86069bdce27927
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 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 */
55
56 /*
57 * Mach kernel startup.
58 */
59
60 #include <debug.h>
61 #include <xpr_debug.h>
62 #include <mach_kdp.h>
63 #include <cpus.h>
64 #include <mach_host.h>
65 #include <norma_vm.h>
66 #include <etap.h>
67
68 #include <mach/boolean.h>
69 #include <mach/machine.h>
70 #include <mach/task_special_ports.h>
71 #include <mach/vm_param.h>
72 #include <ipc/ipc_init.h>
73 #include <kern/assert.h>
74 #include <kern/misc_protos.h>
75 #include <kern/clock.h>
76 #include <kern/cpu_number.h>
77 #include <kern/etap_macros.h>
78 #include <kern/machine.h>
79 #include <kern/processor.h>
80 #include <kern/sched_prim.h>
81 #include <kern/mk_sp.h>
82 #include <kern/startup.h>
83 #include <kern/task.h>
84 #include <kern/thread.h>
85 #include <kern/timer.h>
86 #include <kern/timer_call.h>
87 #include <kern/xpr.h>
88 #include <kern/zalloc.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_init.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <machine/pmap.h>
96 #include <machine/commpage.h>
97 #include <sys/version.h>
98
99 #ifdef __ppc__
100 #include <ppc/Firmware.h>
101 #include <ppc/mappings.h>
102 #endif
103
104 /* Externs XXX */
105 extern void rtclock_reset(void);
106
107 /* Forwards */
108 void cpu_launch_first_thread(
109 thread_t thread);
110 void start_kernel_threads(void);
111 void swapin_thread();
112
113 /*
114 * Running in virtual memory, on the interrupt stack.
115 * Does not return. Dispatches initial thread.
116 *
117 * Assumes that master_cpu is set.
118 */
119 void
120 setup_main(void)
121 {
122 thread_t startup_thread;
123
124 sched_init();
125 vm_mem_bootstrap();
126 ipc_bootstrap();
127 vm_mem_init();
128 ipc_init();
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
136 #ifdef __ppc__
137 mapping_free_prime(); /* Load up with temporary mapping blocks */
138 #endif
139
140 machine_init();
141 kmod_init();
142 clock_init();
143
144 init_timers();
145 timer_call_initialize();
146
147 machine_info.max_cpus = NCPUS;
148 machine_info.memory_size = mem_size;
149 machine_info.avail_cpus = 0;
150 machine_info.major_version = KERNEL_MAJOR_VERSION;
151 machine_info.minor_version = KERNEL_MINOR_VERSION;
152
153 /*
154 * Initialize the IPC, task, and thread subsystems.
155 */
156 ledger_init();
157 task_init();
158 act_init();
159 thread_init();
160
161 /*
162 * Initialize the Event Trace Analysis Package.
163 * Dynamic Phase: 2 of 2
164 */
165 etap_init_phase2();
166
167 /*
168 * Create a kernel thread to start the other kernel
169 * threads.
170 */
171 startup_thread = kernel_thread_with_priority(
172 kernel_task, MAXPRI_KERNEL,
173 start_kernel_threads, TRUE, FALSE);
174 /*
175 * Pretend it is already running.
176 *
177 * We can do this without locking, because nothing
178 * else is running yet.
179 */
180 startup_thread->state = TH_RUN;
181 hw_atomic_add(&startup_thread->processor_set->run_count, 1);
182
183 /*
184 * Start the thread.
185 */
186 cpu_launch_first_thread(startup_thread);
187 /*NOTREACHED*/
188 panic("cpu_launch_first_thread returns!");
189 }
190
191 /*
192 * Now running in a thread. Create the rest of the kernel threads
193 * and the bootstrap task.
194 */
195 void
196 start_kernel_threads(void)
197 {
198 register int i;
199
200 thread_bind(current_thread(), cpu_to_processor(cpu_number()));
201
202 /*
203 * Create the idle threads and the other
204 * service threads.
205 */
206 for (i = 0; i < NCPUS; i++) {
207 processor_t processor = cpu_to_processor(i);
208 thread_t thread;
209 spl_t s;
210
211 thread = kernel_thread_with_priority(
212 kernel_task, MAXPRI_KERNEL,
213 idle_thread, TRUE, FALSE);
214 s = splsched();
215 thread_lock(thread);
216 thread_bind_locked(thread, processor);
217 processor->idle_thread = thread;
218 thread->ref_count++;
219 thread->state |= TH_IDLE;
220 thread_go_locked(thread, THREAD_AWAKENED);
221 thread_unlock(thread);
222 splx(s);
223 }
224
225 /*
226 * Initialize the thread reaper mechanism.
227 */
228 thread_reaper_init();
229
230 /*
231 * Initialize the stack swapin mechanism.
232 */
233 swapin_init();
234
235 /*
236 * Initialize the periodic scheduler mechanism.
237 */
238 sched_tick_init();
239
240 /*
241 * Initialize the thread callout mechanism.
242 */
243 thread_call_initialize();
244
245 /*
246 * Invoke some black magic.
247 */
248 #if __ppc__
249 mapping_adjust();
250 #endif
251
252 /*
253 * Create the clock service.
254 */
255 clock_service_create();
256
257 /*
258 * Create the device service.
259 */
260 device_service_create();
261
262 shared_file_boot_time_init();
263
264 #ifdef IOKIT
265 {
266 PE_init_iokit();
267 }
268 #endif
269
270 (void) spllo(); /* Allow interruptions */
271
272 /*
273 * Fill in the comm area (mapped into every task address space.)
274 */
275 commpage_populate();
276
277 /*
278 * Start the user bootstrap.
279 */
280
281 #ifdef MACH_BSD
282 {
283 extern void bsd_init(void);
284 bsd_init();
285 }
286 #endif
287
288 thread_bind(current_thread(), PROCESSOR_NULL);
289
290 /*
291 * Become the pageout daemon.
292 */
293
294 vm_pageout();
295 /*NOTREACHED*/
296 }
297
298 void
299 slave_main(void)
300 {
301 processor_t myprocessor = current_processor();
302 thread_t thread;
303
304 myprocessor->cpu_data = get_cpu_data();
305 thread = myprocessor->next_thread;
306 myprocessor->next_thread = THREAD_NULL;
307 if (thread == THREAD_NULL) {
308 thread = machine_wake_thread;
309 machine_wake_thread = THREAD_NULL;
310 }
311 thread_machine_set_current(thread);
312 if (thread == machine_wake_thread)
313 thread_bind(thread, myprocessor);
314
315 cpu_launch_first_thread(thread);
316 /*NOTREACHED*/
317 panic("slave_main");
318 }
319
320 /*
321 * Now running in a thread context
322 */
323 void
324 start_cpu_thread(void)
325 {
326 processor_t processor;
327
328 processor = cpu_to_processor(cpu_number());
329
330 slave_machine_init();
331
332 if (processor->processor_self == IP_NULL) {
333 ipc_processor_init(processor);
334 ipc_processor_enable(processor);
335 }
336
337 (void) thread_terminate(current_act());
338 }
339
340 /*
341 * Start up the first thread on a CPU.
342 */
343 void
344 cpu_launch_first_thread(
345 thread_t thread)
346 {
347 register int mycpu = cpu_number();
348 processor_t processor = cpu_to_processor(mycpu);
349
350 processor->cpu_data->preemption_level = 0;
351
352 cpu_up(mycpu);
353 start_timer(&kernel_timer[mycpu]);
354 clock_get_uptime(&processor->last_dispatch);
355
356 if (thread == THREAD_NULL || thread == processor->idle_thread)
357 panic("cpu_launch_first_thread");
358
359 rtclock_reset(); /* start realtime clock ticking */
360 PMAP_ACTIVATE_KERNEL(mycpu);
361
362 thread_machine_set_current(thread);
363 thread_lock(thread);
364 thread->state &= ~TH_UNINT;
365 thread->last_processor = processor;
366 processor->current_pri = thread->sched_pri;
367 _mk_sp_thread_begin(thread, processor);
368 thread_unlock(thread);
369 timer_switch(&thread->system_timer);
370
371 PMAP_ACTIVATE_USER(thread->top_act, mycpu);
372
373 /* preemption enabled by load_context */
374 load_context(thread);
375 /*NOTREACHED*/
376 }