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