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