xref: /haiku/src/system/kernel/main.cpp (revision 15fb7d88e971c4d6c787c6a3a5c159afb1ebf77b)
1 /*
2  * Copyright 2018, Jérôme Duval, jerome.duval@gmail.com.
3  * Copyright 2002-2020, 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, debug level: %d\n", get_haiku_revision(),
134 			KDEBUG_LEVEL);
135 
136 		// init modules
137 		TRACE("init CPU\n");
138 		cpu_init(&sKernelArgs);
139 		cpu_init_percpu(&sKernelArgs, currentCPU);
140 		TRACE("init interrupts\n");
141 		int_init(&sKernelArgs);
142 
143 		TRACE("init VM\n");
144 		vm_init(&sKernelArgs);
145 			// Before vm_init_post_sem() is called, we have to make sure that
146 			// the boot loader allocated region is not used anymore
147 		boot_item_init();
148 		debug_init_post_vm(&sKernelArgs);
149 		low_resource_manager_init();
150 
151 		// now we can use the heap and create areas
152 		arch_platform_init_post_vm(&sKernelArgs);
153 		lock_debug_init();
154 		TRACE("init driver_settings\n");
155 		driver_settings_init(&sKernelArgs);
156 		debug_init_post_settings(&sKernelArgs);
157 		TRACE("init notification services\n");
158 		notifications_init();
159 		TRACE("init teams\n");
160 		team_init(&sKernelArgs);
161 		TRACE("init ELF loader\n");
162 		elf_init(&sKernelArgs);
163 		TRACE("init modules\n");
164 		module_init(&sKernelArgs);
165 		TRACE("init semaphores\n");
166 		haiku_sem_init(&sKernelArgs);
167 		TRACE("init interrupts post vm\n");
168 		int_init_post_vm(&sKernelArgs);
169 		cpu_init_post_vm(&sKernelArgs);
170 		commpage_init();
171 #ifdef _COMPAT_MODE
172 		commpage_compat_init();
173 #endif
174 		call_all_cpus_sync(non_boot_cpu_init, &sKernelArgs);
175 
176 		TRACE("init system info\n");
177 		system_info_init(&sKernelArgs);
178 
179 		TRACE("init SMP\n");
180 		smp_init(&sKernelArgs);
181 		cpu_build_topology_tree();
182 		TRACE("init timer\n");
183 		timer_init(&sKernelArgs);
184 		TRACE("init real time clock\n");
185 		rtc_init(&sKernelArgs);
186 		timer_init_post_rtc();
187 
188 		TRACE("init condition variables\n");
189 		condition_variable_init();
190 
191 		// now we can create and use semaphores
192 		TRACE("init VM semaphores\n");
193 		vm_init_post_sem(&sKernelArgs);
194 		TRACE("init generic syscall\n");
195 		generic_syscall_init();
196 		smp_init_post_generic_syscalls();
197 		TRACE("init scheduler\n");
198 		scheduler_init();
199 		TRACE("init threads\n");
200 		thread_init(&sKernelArgs);
201 		TRACE("init kernel daemons\n");
202 		kernel_daemon_init();
203 		arch_platform_init_post_thread(&sKernelArgs);
204 
205 		TRACE("init I/O interrupts\n");
206 		int_init_io(&sKernelArgs);
207 		TRACE("init VM threads\n");
208 		vm_init_post_thread(&sKernelArgs);
209 		low_resource_manager_init_post_thread();
210 		TRACE("init DPC\n");
211 		dpc_init();
212 		TRACE("init VFS\n");
213 		vfs_init(&sKernelArgs);
214 #if ENABLE_SWAP_SUPPORT
215 		TRACE("init swap support\n");
216 		swap_init();
217 #endif
218 		TRACE("init POSIX semaphores\n");
219 		realtime_sem_init();
220 		xsi_sem_init();
221 		xsi_msg_init();
222 
223 		// Start a thread to finish initializing the rest of the system. Note,
224 		// it won't be scheduled before calling scheduler_start() (on any CPU).
225 		TRACE("spawning main2 thread\n");
226 		thread_id thread = spawn_kernel_thread(&main2, "main2",
227 			B_NORMAL_PRIORITY, NULL);
228 		resume_thread(thread);
229 
230 		// We're ready to start the scheduler and enable interrupts on all CPUs.
231 		scheduler_enable_scheduling();
232 
233 		// bring up the AP cpus in a lock step fashion
234 		TRACE("waking up AP cpus\n");
235 		sCpuRendezvous = sCpuRendezvous2 = 0;
236 		smp_wake_up_non_boot_cpus();
237 		smp_cpu_rendezvous(&sCpuRendezvous); // wait until they're booted
238 
239 		// exit the kernel startup phase (mutexes, etc work from now on out)
240 		TRACE("exiting kernel startup\n");
241 		gKernelStartup = false;
242 
243 		smp_cpu_rendezvous(&sCpuRendezvous2);
244 			// release the AP cpus to go enter the scheduler
245 
246 		TRACE("starting scheduler on cpu 0 and enabling interrupts\n");
247 		scheduler_start();
248 		enable_interrupts();
249 	} else {
250 		// lets make sure we're in sync with the main cpu
251 		// the boot processor has probably been sending us
252 		// tlb sync messages all along the way, but we've
253 		// been ignoring them
254 		arch_cpu_global_TLB_invalidate();
255 
256 		// this is run for each non boot processor after they've been set loose
257 		smp_per_cpu_init(&sKernelArgs, currentCPU);
258 
259 		// wait for all other AP cpus to get to this point
260 		smp_cpu_rendezvous(&sCpuRendezvous);
261 		smp_cpu_rendezvous(&sCpuRendezvous2);
262 
263 		// welcome to the machine
264 		scheduler_start();
265 		enable_interrupts();
266 	}
267 
268 #ifdef TRACE_BOOT
269 	// We disable interrupts for this dprintf(), since otherwise dprintf()
270 	// would acquires a mutex, which is something we must not do in an idle
271 	// thread, or otherwise the scheduler would be seriously unhappy.
272 	disable_interrupts();
273 	TRACE("main: done... begin idle loop on cpu %d\n", currentCPU);
274 	enable_interrupts();
275 #endif
276 
277 	for (;;)
278 		cpu_idle();
279 
280 	return 0;
281 }
282 
283 
284 static int32
285 main2(void* /*unused*/)
286 {
287 	TRACE("start of main2: initializing devices\n");
288 
289 #if SYSTEM_PROFILER
290 	start_system_profiler(SYSTEM_PROFILE_SIZE, SYSTEM_PROFILE_STACK_DEPTH,
291 		SYSTEM_PROFILE_INTERVAL);
292 #endif
293 	boot_splash_init(sKernelArgs.boot_splash);
294 
295 	commpage_init_post_cpus();
296 #ifdef _COMPAT_MODE
297 	commpage_compat_init_post_cpus();
298 #endif
299 
300 	TRACE("init ports\n");
301 	port_init(&sKernelArgs);
302 
303 	TRACE("init user mutex\n");
304 	user_mutex_init();
305 
306 	TRACE("init system notifications\n");
307 	system_notifications_init();
308 
309 	TRACE("Init modules\n");
310 	boot_splash_set_stage(BOOT_SPLASH_STAGE_1_INIT_MODULES);
311 	module_init_post_threads();
312 
313 	// init userland debugging
314 	TRACE("Init Userland debugging\n");
315 	init_user_debug();
316 
317 	// init the messaging service
318 	TRACE("Init Messaging Service\n");
319 	init_messaging_service();
320 
321 	/* bootstrap all the filesystems */
322 	TRACE("Bootstrap file systems\n");
323 	boot_splash_set_stage(BOOT_SPLASH_STAGE_2_BOOTSTRAP_FS);
324 	vfs_bootstrap_file_systems();
325 
326 	TRACE("Init Device Manager\n");
327 	boot_splash_set_stage(BOOT_SPLASH_STAGE_3_INIT_DEVICES);
328 	device_manager_init(&sKernelArgs);
329 
330 	TRACE("Add preloaded old-style drivers\n");
331 	legacy_driver_add_preloaded(&sKernelArgs);
332 
333 	int_init_post_device_manager(&sKernelArgs);
334 
335 	TRACE("Mount boot file system\n");
336 	boot_splash_set_stage(BOOT_SPLASH_STAGE_4_MOUNT_BOOT_FS);
337 	vfs_mount_boot_file_system(&sKernelArgs);
338 
339 #if ENABLE_SWAP_SUPPORT
340 	TRACE("swap_init_post_modules\n");
341 	swap_init_post_modules();
342 #endif
343 
344 	// CPU specific modules may now be available
345 	boot_splash_set_stage(BOOT_SPLASH_STAGE_5_INIT_CPU_MODULES);
346 	cpu_init_post_modules(&sKernelArgs);
347 
348 	TRACE("vm_init_post_modules\n");
349 	boot_splash_set_stage(BOOT_SPLASH_STAGE_6_INIT_VM_MODULES);
350 	vm_init_post_modules(&sKernelArgs);
351 
352 	TRACE("debug_init_post_modules\n");
353 	debug_init_post_modules(&sKernelArgs);
354 
355 	TRACE("device_manager_init_post_modules\n");
356 	device_manager_init_post_modules(&sKernelArgs);
357 
358 	boot_splash_set_stage(BOOT_SPLASH_STAGE_7_RUN_BOOT_SCRIPT);
359 	boot_splash_uninit();
360 		// NOTE: We could introduce a syscall to draw more icons indicating
361 		// stages in the boot script itself. Then we should not free the image.
362 		// In that case we should copy it over to the kernel heap, so that we
363 		// can still free the kernel args.
364 
365 	// The boot splash screen is the last user of the kernel args.
366 	// Note: don't confuse the kernel_args structure (which is never freed)
367 	// with the kernel args ranges it contains (and which are freed here).
368 	vm_free_kernel_args(&sKernelArgs);
369 
370 	// start the init process
371 	{
372 		KPath serverPath;
373 		status_t status = __find_directory(B_SYSTEM_SERVERS_DIRECTORY,
374 			gBootDevice, false, serverPath.LockBuffer(),
375 			serverPath.BufferSize());
376 		if (status != B_OK)
377 			dprintf("main2: find_directory() failed: %s\n", strerror(status));
378 		serverPath.UnlockBuffer();
379 		status = serverPath.Append("/launch_daemon");
380 		if (status != B_OK) {
381 			dprintf("main2: constructing path to launch_daemon failed: %s\n",
382 			strerror(status));
383 		}
384 
385 		const char* args[] = { serverPath.Path(), NULL };
386 		int32 argc = 1;
387 		thread_id thread;
388 
389 		thread = load_image(argc, args, NULL);
390 		if (thread >= B_OK) {
391 			resume_thread(thread);
392 			TRACE("launch_daemon started\n");
393 		} else {
394 			dprintf("error starting \"%s\" error = %" B_PRId32 " \n",
395 				args[0], thread);
396 		}
397 	}
398 
399 	return 0;
400 }
401 
402