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