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