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