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