]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-123.5.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/sf.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 pager_mux_hash_init();
126
127 /*
128 * As soon as the virtual memory system is up, we record
129 * that this CPU is using the kernel pmap.
130 */
131 PMAP_ACTIVATE_KERNEL(master_cpu);
132
133 #ifdef __ppc__
134 mapping_free_prime(); /* Load up with temporary mapping blocks */
135 #endif
136
137 machine_init();
138 kmod_init();
139 clock_init();
140
141 init_timers();
142 timer_call_initialize();
143
144 machine_info.max_cpus = NCPUS;
145 machine_info.memory_size = mem_size;
146 machine_info.avail_cpus = 0;
147 machine_info.major_version = KERNEL_MAJOR_VERSION;
148 machine_info.minor_version = KERNEL_MINOR_VERSION;
149
150 /*
151 * Initialize the IPC, task, and thread subsystems.
152 */
153 ledger_init();
154 swapper_init();
155 task_init();
156 act_init();
157 thread_init();
158 subsystem_init();
159
160 /*
161 * Initialize the Event Trace Analysis Package.
162 * Dynamic Phase: 2 of 2
163 */
164 etap_init_phase2();
165
166 /*
167 * Create a kernel thread to start the other kernel
168 * threads. Thread_resume (from kernel_thread) calls
169 * thread_setrun, which may look at current thread;
170 * we must avoid this, since there is no current thread.
171 */
172 startup_thread = kernel_thread_with_priority(kernel_task, MAXPRI_KERNBAND,
173 start_kernel_threads, FALSE);
174
175 /*
176 * Pretend it is already running, and resume it.
177 * Since it looks as if it is running, thread_resume
178 * will not try to put it on the run queues.
179 *
180 * We can do all of this without locking, because nothing
181 * else is running yet.
182 */
183 startup_thread->state = TH_RUN;
184 (void) thread_resume(startup_thread->top_act);
185 /*
186 * Start the thread.
187 */
188 cpu_launch_first_thread(startup_thread);
189 /*NOTREACHED*/
190 panic("cpu_launch_first_thread returns!");
191 }
192
193 /*
194 * Now running in a thread. Create the rest of the kernel threads
195 * and the bootstrap task.
196 */
197 void
198 start_kernel_threads(void)
199 {
200 register int i;
201
202 thread_bind(current_thread(), cpu_to_processor(cpu_number()));
203
204 /*
205 * Create the idle threads and the other
206 * service threads.
207 */
208 for (i = 0; i < NCPUS; i++) {
209 if (1 /*machine_slot[i].is_cpu*/) {
210 processor_t processor = cpu_to_processor(i);
211 thread_t thread;
212 spl_t s;
213
214 thread = kernel_thread_with_priority(kernel_task,
215 MAXPRI_KERNBAND, idle_thread, FALSE);
216 s = splsched();
217 thread_lock(thread);
218 thread_bind_locked(thread, processor);
219 processor->idle_thread = thread;
220 thread->state |= TH_IDLE;
221 thread_go_locked(thread, THREAD_AWAKENED);
222 thread_unlock(thread);
223 splx(s);
224 }
225 }
226
227 /*
228 * Initialize the thread callout mechanism.
229 */
230 thread_call_initialize();
231
232 /*
233 * Invoke some black magic.
234 */
235 #if __ppc__
236 mapping_adjust();
237 #endif
238
239 /*
240 * Invoke the thread reaper mechanism.
241 */
242 thread_reaper();
243
244 /*
245 * Start the stack swapin thread
246 */
247 kernel_thread(kernel_task, swapin_thread);
248
249 /*
250 * Invoke the periodic scheduler mechanism.
251 */
252 kernel_thread(kernel_task, sched_tick_thread);
253
254 /*
255 * Create the clock service.
256 */
257 clock_service_create();
258
259 /*
260 * Create the device service.
261 */
262 device_service_create();
263
264 shared_file_boot_time_init();
265
266 #ifdef IOKIT
267 {
268 PE_init_iokit();
269 }
270 #endif
271
272 /*
273 * Start the user bootstrap.
274 */
275
276 (void) spllo(); /* Allow interruptions */
277
278 #ifdef MACH_BSD
279 {
280 extern void bsd_init(void);
281 bsd_init();
282 }
283 #endif
284
285 thread_bind(current_thread(), PROCESSOR_NULL);
286
287 /*
288 * Become the pageout daemon.
289 */
290
291 vm_pageout();
292 /*NOTREACHED*/
293 }
294
295 void
296 slave_main(void)
297 {
298 processor_t myprocessor = current_processor();
299 thread_t thread;
300
301 thread = myprocessor->next_thread;
302 myprocessor->next_thread = THREAD_NULL;
303 if (thread == THREAD_NULL) {
304 thread = machine_wake_thread;
305 machine_wake_thread = THREAD_NULL;
306 }
307 cpu_launch_first_thread(thread);
308 /*NOTREACHED*/
309 panic("slave_main");
310 }
311
312 /*
313 * Now running in a thread context
314 */
315 void
316 start_cpu_thread(void)
317 {
318 processor_t processor;
319
320 processor = cpu_to_processor(cpu_number());
321
322 slave_machine_init();
323
324 if (processor->processor_self == IP_NULL) {
325 ipc_processor_init(processor);
326 ipc_processor_enable(processor);
327 }
328
329 #if 0
330 printf("start_cpu_thread done on cpu %x\n", cpu_number());
331 #endif
332 /* TODO: Mark this processor ready to dispatch threads */
333
334 (void) thread_terminate(current_act());
335 }
336
337 /*
338 * Start up the first thread on a CPU.
339 */
340 void
341 cpu_launch_first_thread(
342 thread_t thread)
343 {
344 register int mycpu = cpu_number();
345
346 /* initialize preemption disabled */
347 cpu_data[mycpu].preemption_level = 1;
348
349 cpu_up(mycpu);
350 start_timer(&kernel_timer[mycpu]);
351
352 if (thread == THREAD_NULL) {
353 thread = cpu_to_processor(mycpu)->idle_thread;
354 if (thread == THREAD_NULL)
355 panic("cpu_launch_first_thread");
356 }
357
358 rtclock_reset(); /* start realtime clock ticking */
359 PMAP_ACTIVATE_KERNEL(mycpu);
360
361 thread_machine_set_current(thread);
362 thread_lock(thread);
363 thread->state &= ~TH_UNINT;
364 thread_unlock(thread);
365 timer_switch(&thread->system_timer);
366
367 PMAP_ACTIVATE_USER(thread->top_act, mycpu);
368
369 /* preemption enabled by load_context */
370 load_context(thread);
371 /*NOTREACHED*/
372 }