]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-344.23.tar.gz
[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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 /*
54 * Mach kernel startup.
55 */
56
57 #include <debug.h>
58 #include <xpr_debug.h>
59 #include <mach_kdp.h>
60 #include <cpus.h>
61 #include <mach_host.h>
62 #include <norma_vm.h>
63 #include <etap.h>
64
65 #include <mach/boolean.h>
66 #include <mach/machine.h>
67 #include <mach/task_special_ports.h>
68 #include <mach/vm_param.h>
69 #include <ipc/ipc_init.h>
70 #include <kern/assert.h>
71 #include <kern/misc_protos.h>
72 #include <kern/clock.h>
73 #include <kern/cpu_number.h>
74 #include <kern/etap_macros.h>
75 #include <kern/machine.h>
76 #include <kern/processor.h>
77 #include <kern/sched_prim.h>
78 #include <kern/mk_sp.h>
79 #include <kern/startup.h>
80 #include <kern/task.h>
81 #include <kern/thread.h>
82 #include <kern/timer.h>
83 #include <kern/timer_call.h>
84 #include <kern/xpr.h>
85 #include <kern/zalloc.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_init.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <machine/pmap.h>
93 #include <sys/version.h>
94
95 #ifdef __ppc__
96 #include <ppc/Firmware.h>
97 #include <ppc/mappings.h>
98 #endif
99
100 /* Externs XXX */
101 extern void rtclock_reset(void);
102
103 /* Forwards */
104 void cpu_launch_first_thread(
105 thread_t thread);
106 void start_kernel_threads(void);
107 void swapin_thread();
108
109 /*
110 * Running in virtual memory, on the interrupt stack.
111 * Does not return. Dispatches initial thread.
112 *
113 * Assumes that master_cpu is set.
114 */
115 void
116 setup_main(void)
117 {
118 thread_t startup_thread;
119
120 sched_init();
121 vm_mem_bootstrap();
122 ipc_bootstrap();
123 vm_mem_init();
124 ipc_init();
125
126 /*
127 * As soon as the virtual memory system is up, we record
128 * that this CPU is using the kernel pmap.
129 */
130 PMAP_ACTIVATE_KERNEL(master_cpu);
131
132 #ifdef __ppc__
133 mapping_free_prime(); /* Load up with temporary mapping blocks */
134 #endif
135
136 machine_init();
137 kmod_init();
138 clock_init();
139
140 init_timers();
141 timer_call_initialize();
142
143 machine_info.max_cpus = NCPUS;
144 machine_info.memory_size = mem_size;
145 machine_info.avail_cpus = 0;
146 machine_info.major_version = KERNEL_MAJOR_VERSION;
147 machine_info.minor_version = KERNEL_MINOR_VERSION;
148
149 /*
150 * Initialize the IPC, task, and thread subsystems.
151 */
152 ledger_init();
153 task_init();
154 act_init();
155 thread_init();
156
157 /*
158 * Initialize the Event Trace Analysis Package.
159 * Dynamic Phase: 2 of 2
160 */
161 etap_init_phase2();
162
163 /*
164 * Create a kernel thread to start the other kernel
165 * threads.
166 */
167 startup_thread = kernel_thread_with_priority(
168 kernel_task, MAXPRI_KERNEL,
169 start_kernel_threads, TRUE, FALSE);
170 /*
171 * Pretend it is already running.
172 *
173 * We can do this without locking, because nothing
174 * else is running yet.
175 */
176 startup_thread->state = TH_RUN;
177 hw_atomic_add(&startup_thread->processor_set->run_count, 1);
178
179 /*
180 * Start the thread.
181 */
182 cpu_launch_first_thread(startup_thread);
183 /*NOTREACHED*/
184 panic("cpu_launch_first_thread returns!");
185 }
186
187 /*
188 * Now running in a thread. Create the rest of the kernel threads
189 * and the bootstrap task.
190 */
191 void
192 start_kernel_threads(void)
193 {
194 register int i;
195
196 thread_bind(current_thread(), cpu_to_processor(cpu_number()));
197
198 /*
199 * Create the idle threads and the other
200 * service threads.
201 */
202 for (i = 0; i < NCPUS; i++) {
203 processor_t processor = cpu_to_processor(i);
204 thread_t thread;
205 spl_t s;
206
207 thread = kernel_thread_with_priority(
208 kernel_task, MAXPRI_KERNEL,
209 idle_thread, TRUE, FALSE);
210 s = splsched();
211 thread_lock(thread);
212 thread_bind_locked(thread, processor);
213 processor->idle_thread = thread;
214 thread->ref_count++;
215 thread->state |= TH_IDLE;
216 thread_go_locked(thread, THREAD_AWAKENED);
217 thread_unlock(thread);
218 splx(s);
219 }
220
221 /*
222 * Initialize the thread reaper mechanism.
223 */
224 thread_reaper_init();
225
226 /*
227 * Initialize the stack swapin mechanism.
228 */
229 swapin_init();
230
231 /*
232 * Initialize the periodic scheduler mechanism.
233 */
234 sched_tick_init();
235
236 /*
237 * Initialize the thread callout mechanism.
238 */
239 thread_call_initialize();
240
241 /*
242 * Invoke some black magic.
243 */
244 #if __ppc__
245 mapping_adjust();
246 #endif
247
248 /*
249 * Create the clock service.
250 */
251 clock_service_create();
252
253 /*
254 * Create the device service.
255 */
256 device_service_create();
257
258 shared_file_boot_time_init();
259
260 #ifdef IOKIT
261 {
262 PE_init_iokit();
263 }
264 #endif
265
266 /*
267 * Start the user bootstrap.
268 */
269
270 (void) spllo(); /* Allow interruptions */
271
272 #ifdef MACH_BSD
273 {
274 extern void bsd_init(void);
275 bsd_init();
276 }
277 #endif
278
279 thread_bind(current_thread(), PROCESSOR_NULL);
280
281 /*
282 * Become the pageout daemon.
283 */
284
285 vm_pageout();
286 /*NOTREACHED*/
287 }
288
289 void
290 slave_main(void)
291 {
292 processor_t myprocessor = current_processor();
293 thread_t thread;
294
295 myprocessor->cpu_data = get_cpu_data();
296 thread = myprocessor->next_thread;
297 myprocessor->next_thread = THREAD_NULL;
298 if (thread == THREAD_NULL) {
299 thread = machine_wake_thread;
300 machine_wake_thread = THREAD_NULL;
301 }
302 thread_machine_set_current(thread);
303 if (thread == machine_wake_thread)
304 thread_bind(thread, myprocessor);
305
306 cpu_launch_first_thread(thread);
307 /*NOTREACHED*/
308 panic("slave_main");
309 }
310
311 /*
312 * Now running in a thread context
313 */
314 void
315 start_cpu_thread(void)
316 {
317 processor_t processor;
318
319 processor = cpu_to_processor(cpu_number());
320
321 slave_machine_init();
322
323 if (processor->processor_self == IP_NULL) {
324 ipc_processor_init(processor);
325 ipc_processor_enable(processor);
326 }
327
328 (void) thread_terminate(current_act());
329 }
330
331 /*
332 * Start up the first thread on a CPU.
333 */
334 void
335 cpu_launch_first_thread(
336 thread_t thread)
337 {
338 register int mycpu = cpu_number();
339 processor_t processor = cpu_to_processor(mycpu);
340
341 processor->cpu_data->preemption_level = 0;
342
343 cpu_up(mycpu);
344 start_timer(&kernel_timer[mycpu]);
345 clock_get_uptime(&processor->last_dispatch);
346
347 if (thread == THREAD_NULL || thread == processor->idle_thread)
348 panic("cpu_launch_first_thread");
349
350 rtclock_reset(); /* start realtime clock ticking */
351 PMAP_ACTIVATE_KERNEL(mycpu);
352
353 thread_machine_set_current(thread);
354 thread_lock(thread);
355 thread->state &= ~TH_UNINT;
356 thread->last_processor = processor;
357 processor->current_pri = thread->sched_pri;
358 _mk_sp_thread_begin(thread, processor);
359 thread_unlock(thread);
360 timer_switch(&thread->system_timer);
361
362 PMAP_ACTIVATE_USER(thread->top_act, mycpu);
363
364 /* preemption enabled by load_context */
365 load_context(thread);
366 /*NOTREACHED*/
367 }