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