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