headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / kernel / main.cpp
blobf3a0a9b38856e6413e70ce45fd19a2789a9d3642
1 /*
2 * Copyright 2002-2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
7 */
10 /*! This is main - initializes the kernel and launches the Bootscript */
13 #include <string.h>
15 #include <FindDirectory.h>
16 #include <OS.h>
18 #include <arch/platform.h>
19 #include <boot_device.h>
20 #include <boot_item.h>
21 #include <boot_splash.h>
22 #include <commpage.h>
23 #include <condition_variable.h>
24 #include <cpu.h>
25 #include <debug.h>
26 #include <DPC.h>
27 #include <elf.h>
28 #include <find_directory_private.h>
29 #include <fs/devfs.h>
30 #include <fs/KPath.h>
31 #include <int.h>
32 #include <kdevice_manager.h>
33 #include <kdriver_settings.h>
34 #include <kernel_daemon.h>
35 #include <kmodule.h>
36 #include <kscheduler.h>
37 #include <ksyscalls.h>
38 #include <ksystem_info.h>
39 #include <lock.h>
40 #include <low_resource_manager.h>
41 #include <messaging.h>
42 #include <Notifications.h>
43 #include <port.h>
44 #include <posix/realtime_sem.h>
45 #include <posix/xsi_message_queue.h>
46 #include <posix/xsi_semaphore.h>
47 #include <real_time_clock.h>
48 #include <sem.h>
49 #include <smp.h>
50 #include <system_profiler.h>
51 #include <team.h>
52 #include <timer.h>
53 #include <user_debugger.h>
54 #include <user_mutex.h>
55 #include <vfs.h>
56 #include <vm/vm.h>
57 #include <boot/kernel_args.h>
59 #include "vm/VMAnonymousCache.h"
62 //#define TRACE_BOOT
63 #ifdef TRACE_BOOT
64 # define TRACE(x...) dprintf("INIT: " x)
65 #else
66 # define TRACE(x...) ;
67 #endif
70 void *__dso_handle;
72 bool gKernelStartup = true;
73 bool gKernelShutdown = false;
75 static kernel_args sKernelArgs;
76 static uint32 sCpuRendezvous;
77 static uint32 sCpuRendezvous2;
78 static uint32 sCpuRendezvous3;
80 static int32 main2(void *);
83 static void
84 non_boot_cpu_init(void* args, int currentCPU)
86 kernel_args* kernelArgs = (kernel_args*)args;
87 if (currentCPU != 0)
88 cpu_init_percpu(kernelArgs, currentCPU);
92 extern "C" int
93 _start(kernel_args *bootKernelArgs, int currentCPU)
95 if (bootKernelArgs->kernel_args_size != sizeof(kernel_args)
96 || bootKernelArgs->version != CURRENT_KERNEL_ARGS_VERSION) {
97 // This is something we cannot handle right now - release kernels
98 // should always be able to handle the kernel_args of earlier
99 // released kernels.
100 debug_early_boot_message("Version mismatch between boot loader and "
101 "kernel!\n");
102 return -1;
105 smp_set_num_cpus(bootKernelArgs->num_cpus);
107 // wait for all the cpus to get here
108 smp_cpu_rendezvous(&sCpuRendezvous);
110 // the passed in kernel args are in a non-allocated range of memory
111 if (currentCPU == 0)
112 memcpy(&sKernelArgs, bootKernelArgs, sizeof(kernel_args));
114 smp_cpu_rendezvous(&sCpuRendezvous2);
116 // do any pre-booting cpu config
117 cpu_preboot_init_percpu(&sKernelArgs, currentCPU);
118 thread_preboot_init_percpu(&sKernelArgs, currentCPU);
120 // if we're not a boot cpu, spin here until someone wakes us up
121 if (smp_trap_non_boot_cpus(currentCPU, &sCpuRendezvous3)) {
122 // init platform
123 arch_platform_init(&sKernelArgs);
125 // setup debug output
126 debug_init(&sKernelArgs);
127 set_dprintf_enabled(true);
128 dprintf("Welcome to kernel debugger output!\n");
129 dprintf("Haiku revision: %s\n", get_haiku_revision());
131 // init modules
132 TRACE("init CPU\n");
133 cpu_init(&sKernelArgs);
134 cpu_init_percpu(&sKernelArgs, currentCPU);
135 TRACE("init interrupts\n");
136 int_init(&sKernelArgs);
138 TRACE("init VM\n");
139 vm_init(&sKernelArgs);
140 // Before vm_init_post_sem() is called, we have to make sure that
141 // the boot loader allocated region is not used anymore
142 boot_item_init();
143 debug_init_post_vm(&sKernelArgs);
144 low_resource_manager_init();
146 // now we can use the heap and create areas
147 arch_platform_init_post_vm(&sKernelArgs);
148 lock_debug_init();
149 TRACE("init driver_settings\n");
150 driver_settings_init(&sKernelArgs);
151 debug_init_post_settings(&sKernelArgs);
152 TRACE("init notification services\n");
153 notifications_init();
154 TRACE("init teams\n");
155 team_init(&sKernelArgs);
156 TRACE("init ELF loader\n");
157 elf_init(&sKernelArgs);
158 TRACE("init modules\n");
159 module_init(&sKernelArgs);
160 TRACE("init semaphores\n");
161 haiku_sem_init(&sKernelArgs);
162 TRACE("init interrupts post vm\n");
163 int_init_post_vm(&sKernelArgs);
164 cpu_init_post_vm(&sKernelArgs);
165 commpage_init();
166 call_all_cpus_sync(non_boot_cpu_init, &sKernelArgs);
168 TRACE("init system info\n");
169 system_info_init(&sKernelArgs);
171 TRACE("init SMP\n");
172 smp_init(&sKernelArgs);
173 cpu_build_topology_tree();
174 TRACE("init timer\n");
175 timer_init(&sKernelArgs);
176 TRACE("init real time clock\n");
177 rtc_init(&sKernelArgs);
178 timer_init_post_rtc();
180 TRACE("init condition variables\n");
181 condition_variable_init();
183 // now we can create and use semaphores
184 TRACE("init VM semaphores\n");
185 vm_init_post_sem(&sKernelArgs);
186 TRACE("init generic syscall\n");
187 generic_syscall_init();
188 smp_init_post_generic_syscalls();
189 TRACE("init scheduler\n");
190 scheduler_init();
191 TRACE("init threads\n");
192 thread_init(&sKernelArgs);
193 TRACE("init kernel daemons\n");
194 kernel_daemon_init();
195 arch_platform_init_post_thread(&sKernelArgs);
197 TRACE("init I/O interrupts\n");
198 int_init_io(&sKernelArgs);
199 TRACE("init VM threads\n");
200 vm_init_post_thread(&sKernelArgs);
201 low_resource_manager_init_post_thread();
202 TRACE("init DPC\n");
203 dpc_init();
204 TRACE("init VFS\n");
205 vfs_init(&sKernelArgs);
206 #if ENABLE_SWAP_SUPPORT
207 TRACE("init swap support\n");
208 swap_init();
209 #endif
210 TRACE("init POSIX semaphores\n");
211 realtime_sem_init();
212 xsi_sem_init();
213 xsi_msg_init();
215 // Start a thread to finish initializing the rest of the system. Note,
216 // it won't be scheduled before calling scheduler_start() (on any CPU).
217 TRACE("spawning main2 thread\n");
218 thread_id thread = spawn_kernel_thread(&main2, "main2",
219 B_NORMAL_PRIORITY, NULL);
220 resume_thread(thread);
222 // We're ready to start the scheduler and enable interrupts on all CPUs.
223 scheduler_enable_scheduling();
225 // bring up the AP cpus in a lock step fashion
226 TRACE("waking up AP cpus\n");
227 sCpuRendezvous = sCpuRendezvous2 = 0;
228 smp_wake_up_non_boot_cpus();
229 smp_cpu_rendezvous(&sCpuRendezvous); // wait until they're booted
231 // exit the kernel startup phase (mutexes, etc work from now on out)
232 TRACE("exiting kernel startup\n");
233 gKernelStartup = false;
235 smp_cpu_rendezvous(&sCpuRendezvous2);
236 // release the AP cpus to go enter the scheduler
238 TRACE("starting scheduler on cpu 0 and enabling interrupts\n");
239 scheduler_start();
240 enable_interrupts();
241 } else {
242 // lets make sure we're in sync with the main cpu
243 // the boot processor has probably been sending us
244 // tlb sync messages all along the way, but we've
245 // been ignoring them
246 arch_cpu_global_TLB_invalidate();
248 // this is run for each non boot processor after they've been set loose
249 smp_per_cpu_init(&sKernelArgs, currentCPU);
251 // wait for all other AP cpus to get to this point
252 smp_cpu_rendezvous(&sCpuRendezvous);
253 smp_cpu_rendezvous(&sCpuRendezvous2);
255 // welcome to the machine
256 scheduler_start();
257 enable_interrupts();
260 #ifdef TRACE_BOOT
261 // We disable interrupts for this dprintf(), since otherwise dprintf()
262 // would acquires a mutex, which is something we must not do in an idle
263 // thread, or otherwise the scheduler would be seriously unhappy.
264 disable_interrupts();
265 TRACE("main: done... begin idle loop on cpu %d\n", currentCPU);
266 enable_interrupts();
267 #endif
269 for (;;)
270 cpu_idle();
272 return 0;
276 static int32
277 main2(void* /*unused*/)
279 TRACE("start of main2: initializing devices\n");
281 #if SYSTEM_PROFILER
282 start_system_profiler(SYSTEM_PROFILE_SIZE, SYSTEM_PROFILE_STACK_DEPTH,
283 SYSTEM_PROFILE_INTERVAL);
284 #endif
285 boot_splash_init(sKernelArgs.boot_splash);
287 commpage_init_post_cpus();
289 TRACE("init ports\n");
290 port_init(&sKernelArgs);
292 TRACE("init user mutex\n");
293 user_mutex_init();
295 TRACE("init system notifications\n");
296 system_notifications_init();
298 TRACE("Init modules\n");
299 boot_splash_set_stage(BOOT_SPLASH_STAGE_1_INIT_MODULES);
300 module_init_post_threads();
302 // init userland debugging
303 TRACE("Init Userland debugging\n");
304 init_user_debug();
306 // init the messaging service
307 TRACE("Init Messaging Service\n");
308 init_messaging_service();
310 /* bootstrap all the filesystems */
311 TRACE("Bootstrap file systems\n");
312 boot_splash_set_stage(BOOT_SPLASH_STAGE_2_BOOTSTRAP_FS);
313 vfs_bootstrap_file_systems();
315 TRACE("Init Device Manager\n");
316 boot_splash_set_stage(BOOT_SPLASH_STAGE_3_INIT_DEVICES);
317 device_manager_init(&sKernelArgs);
319 TRACE("Add preloaded old-style drivers\n");
320 legacy_driver_add_preloaded(&sKernelArgs);
322 int_init_post_device_manager(&sKernelArgs);
324 TRACE("Mount boot file system\n");
325 boot_splash_set_stage(BOOT_SPLASH_STAGE_4_MOUNT_BOOT_FS);
326 vfs_mount_boot_file_system(&sKernelArgs);
328 #if ENABLE_SWAP_SUPPORT
329 TRACE("swap_init_post_modules\n");
330 swap_init_post_modules();
331 #endif
333 // CPU specific modules may now be available
334 boot_splash_set_stage(BOOT_SPLASH_STAGE_5_INIT_CPU_MODULES);
335 cpu_init_post_modules(&sKernelArgs);
337 TRACE("vm_init_post_modules\n");
338 boot_splash_set_stage(BOOT_SPLASH_STAGE_6_INIT_VM_MODULES);
339 vm_init_post_modules(&sKernelArgs);
341 TRACE("debug_init_post_modules\n");
342 debug_init_post_modules(&sKernelArgs);
344 TRACE("device_manager_init_post_modules\n");
345 device_manager_init_post_modules(&sKernelArgs);
347 boot_splash_set_stage(BOOT_SPLASH_STAGE_7_RUN_BOOT_SCRIPT);
348 boot_splash_uninit();
349 // NOTE: We could introduce a syscall to draw more icons indicating
350 // stages in the boot script itself. Then we should not free the image.
351 // In that case we should copy it over to the kernel heap, so that we
352 // can still free the kernel args.
354 // The boot splash screen is the last user of the kernel args.
355 // Note: don't confuse the kernel_args structure (which is never freed)
356 // with the kernel args ranges it contains (and which are freed here).
357 vm_free_kernel_args(&sKernelArgs);
359 // start the init process
361 KPath serverPath;
362 status_t status = __find_directory(B_SYSTEM_SERVERS_DIRECTORY,
363 gBootDevice, false, serverPath.LockBuffer(),
364 serverPath.BufferSize());
365 if (status != B_OK)
366 dprintf("main2: find_directory() failed: %s\n", strerror(status));
367 serverPath.UnlockBuffer();
368 status = serverPath.Append("/launch_daemon");
369 if (status != B_OK) {
370 dprintf("main2: constructing path to launch_daemon failed: %s\n",
371 strerror(status));
374 const char* args[] = { serverPath.Path(), NULL };
375 int32 argc = 1;
376 thread_id thread;
378 thread = load_image(argc, args, NULL);
379 if (thread >= B_OK) {
380 resume_thread(thread);
381 TRACE("launch_daemon started\n");
382 } else {
383 dprintf("error starting \"%s\" error = %" B_PRId32 " \n",
384 args[0], thread);
388 return 0;