xref: /haiku/src/system/kernel/main.cpp (revision 079eccf655ba39812b421ae1b87a727d41b50354)
1 /*
2  * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
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 <OS.h>
13 
14 #include <arch/platform.h>
15 #include <boot_device.h>
16 #include <boot_item.h>
17 #include <boot_splash.h>
18 #include <cbuf.h>
19 #include <commpage.h>
20 #include <condition_variable.h>
21 #include <cpu.h>
22 #include <debug.h>
23 #include <elf.h>
24 #include <fs/devfs.h>
25 #include <fs/KPath.h>
26 #include <FindDirectory.h>
27 #include <int.h>
28 #include <kdevice_manager.h>
29 #include <kdriver_settings.h>
30 #include <kernel_daemon.h>
31 #include <kmodule.h>
32 #include <kscheduler.h>
33 #include <ksyscalls.h>
34 #include <lock.h>
35 #include <messaging.h>
36 #include <Notifications.h>
37 #include <port.h>
38 #include <posix/realtime_sem.h>
39 #include <real_time_clock.h>
40 #include <sem.h>
41 #include <smp.h>
42 #include <system_info.h>
43 #include <team.h>
44 #include <timer.h>
45 #include <user_debugger.h>
46 #include <vfs.h>
47 #include <vm.h>
48 #include <boot/kernel_args.h>
49 
50 #include <string.h>
51 
52 
53 //#define TRACE_BOOT
54 #ifdef TRACE_BOOT
55 #	define TRACE(x...) dprintf("INIT: " x)
56 #else
57 #	define TRACE(x...) ;
58 #endif
59 
60 bool kernel_startup = true;
61 
62 static kernel_args sKernelArgs;
63 static uint32 sCpuRendezvous;
64 static uint32 sCpuRendezvous2;
65 
66 static int32 main2(void *);
67 
68 #ifdef  __cplusplus
69 extern "C" {
70 #endif
71 
72 extern int _start(kernel_args *bootKernelArgs, int cpu);
73 	/* keep compiler happy */
74 
75 #ifdef __cplusplus
76 } /* extern "C" */
77 #endif
78 
79 int
80 _start(kernel_args *bootKernelArgs, int currentCPU)
81 {
82 	if (bootKernelArgs->kernel_args_size != sizeof(kernel_args)
83 		|| bootKernelArgs->version != CURRENT_KERNEL_ARGS_VERSION) {
84 		// This is something we cannot handle right now - release kernels
85 		// should always be able to handle the kernel_args of earlier
86 		// released kernels.
87 		debug_early_boot_message("Version mismatch between boot loader and "
88 			"kernel!\n");
89 		return -1;
90 	}
91 
92 	smp_set_num_cpus(bootKernelArgs->num_cpus);
93 
94 	// wait for all the cpus to get here
95 	smp_cpu_rendezvous(&sCpuRendezvous, currentCPU);
96 
97 	// the passed in kernel args are in a non-allocated range of memory
98 	if (currentCPU == 0)
99 		memcpy(&sKernelArgs, bootKernelArgs, sizeof(kernel_args));
100 
101 	smp_cpu_rendezvous(&sCpuRendezvous2, currentCPU);
102 
103 	// do any pre-booting cpu config
104 	cpu_preboot_init_percpu(&sKernelArgs, currentCPU);
105 	thread_preboot_init_percpu(&sKernelArgs, currentCPU);
106 
107 	// if we're not a boot cpu, spin here until someone wakes us up
108 	if (smp_trap_non_boot_cpus(currentCPU)) {
109 		thread_id thread;
110 
111 		// init platform
112 		arch_platform_init(&sKernelArgs);
113 
114 		// setup debug output
115 		debug_init(&sKernelArgs);
116 		set_dprintf_enabled(true);
117 		dprintf("Welcome to kernel debugger output!\n");
118 		dprintf("Haiku revision: %lu\n", get_haiku_revision());
119 
120 		// init modules
121 		TRACE("init CPU\n");
122 		cpu_init(&sKernelArgs);
123 		cpu_init_percpu(&sKernelArgs, currentCPU);
124 		TRACE("init interrupts\n");
125 		int_init(&sKernelArgs);
126 
127 		TRACE("init VM\n");
128 		vm_init(&sKernelArgs);
129 			// Before vm_init_post_sem() is called, we have to make sure that
130 			// the boot loader allocated region is not used anymore
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 		boot_item_init();
137 		driver_settings_init(&sKernelArgs);
138 		debug_init_post_vm(&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 teams\n");
165 		team_init(&sKernelArgs);
166 		TRACE("init threads\n");
167 		thread_init(&sKernelArgs);
168 		TRACE("init ports\n");
169 		port_init(&sKernelArgs);
170 		TRACE("init kernel daemons\n");
171 		kernel_daemon_init();
172 		arch_platform_init_post_thread(&sKernelArgs);
173 		realtime_sem_init();
174 
175 		TRACE("init VM threads\n");
176 		vm_init_post_thread(&sKernelArgs);
177 		TRACE("init ELF loader\n");
178 		elf_init(&sKernelArgs);
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 
186 		// bring up the AP cpus in a lock step fashion
187 		TRACE("waking up AP cpus\n");
188 		sCpuRendezvous = sCpuRendezvous2 = 0;
189 		smp_wake_up_non_boot_cpus();
190 		smp_cpu_rendezvous(&sCpuRendezvous, 0); // wait until they're booted
191 
192 		// exit the kernel startup phase (mutexes, etc work from now on out)
193 		TRACE("exiting kernel startup\n");
194 		kernel_startup = false;
195 
196 		smp_cpu_rendezvous(&sCpuRendezvous2, 0);
197 			// release the AP cpus to go enter the scheduler
198 
199 		TRACE("enabling interrupts and starting scheduler on cpu 0\n");
200 		enable_interrupts();
201 		scheduler_start();
202 
203 		// start a thread to finish initializing the rest of the system
204 		TRACE("starting main2 thread\n");
205 		thread = spawn_kernel_thread(&main2, "main2", B_NORMAL_PRIORITY, NULL);
206 		TRACE("resuming main2 thread...\n");
207 		resume_thread(thread);
208 	} else {
209 		// lets make sure we're in sync with the main cpu
210 		// the boot processor has probably been sending us
211 		// tlb sync messages all along the way, but we've
212 		// been ignoring them
213 		arch_cpu_global_TLB_invalidate();
214 
215 		// this is run for each non boot processor after they've been set loose
216 		cpu_init_percpu(&sKernelArgs, currentCPU);
217 		smp_per_cpu_init(&sKernelArgs, currentCPU);
218 
219 		// wait for all other AP cpus to get to this point
220 		smp_cpu_rendezvous(&sCpuRendezvous, currentCPU);
221 		smp_cpu_rendezvous(&sCpuRendezvous2, currentCPU);
222 
223 		// welcome to the machine
224 		enable_interrupts();
225 		scheduler_start();
226 	}
227 
228 	TRACE("main: done... begin idle loop on cpu %d\n", currentCPU);
229 	for (;;)
230 		arch_cpu_idle();
231 
232 	return 0;
233 }
234 
235 static int32
236 main2(void *unused)
237 {
238 	(void)(unused);
239 
240 	TRACE("start of main2: initializing devices\n");
241 
242 	boot_splash_init(sKernelArgs.boot_splash);
243 
244 	TRACE("Init modules\n");
245 	boot_splash_set_stage(BOOT_SPLASH_STAGE_1_INIT_MODULES);
246 	module_init(&sKernelArgs);
247 
248 	// ToDo: the preloaded image debug data is placed in the kernel args, and
249 	//	thus, if they are enabled, the kernel args shouldn't be freed, so
250 	//	that we don't have to copy them.
251 	//	What is yet missing is a mechanism that controls this (via driver settings).
252 	if (0) {
253 		// module_init() is supposed to be the last user of the kernel args
254 		// Note: don't confuse the kernel_args structure (which is never freed)
255 		// with the kernel args ranges it contains (and which are freed here).
256 		vm_free_kernel_args(&sKernelArgs);
257 	}
258 
259 	// init userland debugging
260 	TRACE("Init Userland debugging\n");
261 	init_user_debug();
262 
263 	// init the messaging service
264 	TRACE("Init Messaging Service\n");
265 	init_messaging_service();
266 
267 	/* bootstrap all the filesystems */
268 	TRACE("Bootstrap file systems\n");
269 	boot_splash_set_stage(BOOT_SPLASH_STAGE_2_BOOTSTRAP_FS);
270 	vfs_bootstrap_file_systems();
271 
272 	TRACE("Init Device Manager\n");
273 	boot_splash_set_stage(BOOT_SPLASH_STAGE_3_INIT_DEVICES);
274 	device_manager_init(&sKernelArgs);
275 
276 	TRACE("Add preloaded old-style drivers\n");
277 	legacy_driver_add_preloaded(&sKernelArgs);
278 
279 	int_init_post_device_manager(&sKernelArgs);
280 
281 	TRACE("Mount boot file system\n");
282 	boot_splash_set_stage(BOOT_SPLASH_STAGE_4_MOUNT_BOOT_FS);
283 	vfs_mount_boot_file_system(&sKernelArgs);
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 	boot_splash_set_stage(BOOT_SPLASH_STAGE_7_RUN_BOOT_SCRIPT);
300 //	kernel_args_free(sKernelArgs.boot_splash);
301 // NOTE: We could introduce a syscall to draw more icons indicating
302 // stages in the boot script itself. Then we should not free the image.
303 
304 	// start the init process
305 	{
306 		KPath bootScriptPath;
307 		status_t status = find_directory(B_BEOS_SYSTEM_DIRECTORY, gBootDevice,
308 			false, bootScriptPath.LockBuffer(), bootScriptPath.BufferSize());
309 		if (status != B_OK)
310 			dprintf("main2: find_directory() failed: %s\n", strerror(status));
311 		bootScriptPath.UnlockBuffer();
312 		status = bootScriptPath.Append("boot/Bootscript");
313 		if (status != B_OK) {
314 			dprintf("main2: constructing path to Bootscript failed: "
315 				"%s\n", strerror(status));
316 		}
317 
318 		const char *args[] = { "/bin/sh", bootScriptPath.Path(), NULL };
319 		int32 argc = 2;
320 		thread_id thread;
321 
322 		thread = load_image(argc, args, NULL);
323 		if (thread >= B_OK) {
324 			resume_thread(thread);
325 			TRACE("Bootscript started\n");
326 		} else
327 			dprintf("error starting \"%s\" error = %ld \n", args[0], thread);
328 	}
329 
330 	return 0;
331 }
332 
333