init.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Types.h>
  7. #include <Kernel/ACPI/DynamicParser.h>
  8. #include <Kernel/ACPI/Initialize.h>
  9. #include <Kernel/ACPI/MultiProcessorParser.h>
  10. #include <Kernel/Arch/PC/BIOS.h>
  11. #include <Kernel/Arch/x86/Processor.h>
  12. #include <Kernel/Bus/PCI/Access.h>
  13. #include <Kernel/Bus/PCI/Initializer.h>
  14. #include <Kernel/Bus/USB/UHCIController.h>
  15. #include <Kernel/CMOS.h>
  16. #include <Kernel/CommandLine.h>
  17. #include <Kernel/Devices/FullDevice.h>
  18. #include <Kernel/Devices/HID/HIDManagement.h>
  19. #include <Kernel/Devices/MemoryDevice.h>
  20. #include <Kernel/Devices/NullDevice.h>
  21. #include <Kernel/Devices/PCISerialDevice.h>
  22. #include <Kernel/Devices/RandomDevice.h>
  23. #include <Kernel/Devices/SB16.h>
  24. #include <Kernel/Devices/SerialDevice.h>
  25. #include <Kernel/Devices/VMWareBackdoor.h>
  26. #include <Kernel/Devices/ZeroDevice.h>
  27. #include <Kernel/FileSystem/Ext2FileSystem.h>
  28. #include <Kernel/FileSystem/SysFS.h>
  29. #include <Kernel/FileSystem/VirtualFileSystem.h>
  30. #include <Kernel/Graphics/GraphicsManagement.h>
  31. #include <Kernel/Heap/SlabAllocator.h>
  32. #include <Kernel/Heap/kmalloc.h>
  33. #include <Kernel/Interrupts/APIC.h>
  34. #include <Kernel/Interrupts/InterruptManagement.h>
  35. #include <Kernel/Interrupts/PIC.h>
  36. #include <Kernel/KSyms.h>
  37. #include <Kernel/Multiboot.h>
  38. #include <Kernel/Net/NetworkTask.h>
  39. #include <Kernel/Net/NetworkingManagement.h>
  40. #include <Kernel/Panic.h>
  41. #include <Kernel/Prekernel/BootInfo.h>
  42. #include <Kernel/Process.h>
  43. #include <Kernel/ProcessExposed.h>
  44. #include <Kernel/RTC.h>
  45. #include <Kernel/Random.h>
  46. #include <Kernel/Scheduler.h>
  47. #include <Kernel/Sections.h>
  48. #include <Kernel/Storage/StorageManagement.h>
  49. #include <Kernel/TTY/ConsoleManagement.h>
  50. #include <Kernel/TTY/PTYMultiplexer.h>
  51. #include <Kernel/TTY/VirtualConsole.h>
  52. #include <Kernel/Tasks/FinalizerTask.h>
  53. #include <Kernel/Tasks/SyncTask.h>
  54. #include <Kernel/Time/TimeManagement.h>
  55. #include <Kernel/VM/MemoryManager.h>
  56. #include <Kernel/VirtIO/VirtIO.h>
  57. #include <Kernel/WorkQueue.h>
  58. #include <Kernel/kstdio.h>
  59. // Defined in the linker script
  60. typedef void (*ctor_func_t)();
  61. extern ctor_func_t start_heap_ctors;
  62. extern ctor_func_t end_heap_ctors;
  63. extern ctor_func_t start_ctors;
  64. extern ctor_func_t end_ctors;
  65. extern size_t __stack_chk_guard;
  66. size_t __stack_chk_guard;
  67. extern "C" u8* start_of_safemem_text;
  68. extern "C" u8* end_of_safemem_text;
  69. extern "C" u8* start_of_safemem_atomic_text;
  70. extern "C" u8* end_of_safemem_atomic_text;
  71. extern "C" u8* end_of_kernel_image;
  72. multiboot_module_entry_t multiboot_copy_boot_modules_array[16];
  73. size_t multiboot_copy_boot_modules_count;
  74. READONLY_AFTER_INIT bool g_in_early_boot;
  75. namespace Kernel {
  76. [[noreturn]] static void init_stage2(void*);
  77. static void setup_serial_debug();
  78. // boot.S expects these functions to exactly have the following signatures.
  79. // We declare them here to ensure their signatures don't accidentally change.
  80. extern "C" void init_finished(u32 cpu) __attribute__((used));
  81. extern "C" [[noreturn]] void init_ap(FlatPtr cpu, Processor* processor_info);
  82. extern "C" [[noreturn]] void init(BootInfo const&);
  83. READONLY_AFTER_INIT VirtualConsole* tty0;
  84. static Processor s_bsp_processor; // global but let's keep it "private"
  85. // SerenityOS Kernel C++ entry point :^)
  86. //
  87. // This is where C++ execution begins, after boot.S transfers control here.
  88. //
  89. // The purpose of init() is to start multi-tasking. It does the bare minimum
  90. // amount of work needed to start the scheduler.
  91. //
  92. // Once multi-tasking is ready, we spawn a new thread that starts in the
  93. // init_stage2() function. Initialization continues there.
  94. extern "C" {
  95. u8 const* start_of_prekernel_image;
  96. u8 const* end_of_prekernel_image;
  97. __attribute__((section(".boot_bss"))) FlatPtr kernel_base;
  98. #if ARCH(X86_64)
  99. extern "C" u32 gdt64ptr;
  100. extern "C" u16 code64_sel;
  101. FlatPtr boot_pml4t;
  102. #endif
  103. FlatPtr boot_pdpt;
  104. FlatPtr boot_pd0;
  105. FlatPtr boot_pd_kernel;
  106. FlatPtr boot_pd_kernel_pt1023;
  107. const char* kernel_cmdline;
  108. }
  109. extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info)
  110. {
  111. g_in_early_boot = true;
  112. multiboot_info_ptr = boot_info.multiboot_info_ptr;
  113. start_of_prekernel_image = boot_info.start_of_prekernel_image;
  114. end_of_prekernel_image = boot_info.end_of_prekernel_image;
  115. kernel_base = boot_info.kernel_base;
  116. #if ARCH(X86_64)
  117. gdt64ptr = boot_info.gdt64ptr;
  118. code64_sel = boot_info.code64_sel;
  119. boot_pml4t = boot_info.boot_pml4t;
  120. #endif
  121. boot_pdpt = boot_info.boot_pdpt;
  122. boot_pd0 = boot_info.boot_pd0;
  123. boot_pd_kernel = boot_info.boot_pd_kernel;
  124. boot_pd_kernel_pt1023 = boot_info.boot_pd_kernel_pt1023;
  125. kernel_cmdline = boot_info.kernel_cmdline;
  126. setup_serial_debug();
  127. // We need to copy the command line before kmalloc is initialized,
  128. // as it may overwrite parts of multiboot!
  129. CommandLine::early_initialize(kernel_cmdline);
  130. memcpy(multiboot_copy_boot_modules_array, (u8*)low_physical_to_virtual(multiboot_info_ptr->mods_addr), multiboot_info_ptr->mods_count * sizeof(multiboot_module_entry_t));
  131. multiboot_copy_boot_modules_count = multiboot_info_ptr->mods_count;
  132. s_bsp_processor.early_initialize(0);
  133. // Invoke the constructors needed for the kernel heap
  134. for (ctor_func_t* ctor = &start_heap_ctors; ctor < &end_heap_ctors; ctor++)
  135. (*ctor)();
  136. kmalloc_init();
  137. slab_alloc_init();
  138. load_kernel_symbol_table();
  139. ConsoleDevice::initialize();
  140. s_bsp_processor.initialize(0);
  141. CommandLine::initialize();
  142. MemoryManager::initialize(0);
  143. // Ensure that the safemem sections are not empty. This could happen if the linker accidentally discards the sections.
  144. VERIFY(&start_of_safemem_text != &end_of_safemem_text);
  145. VERIFY(&start_of_safemem_atomic_text != &end_of_safemem_atomic_text);
  146. // Invoke all static global constructors in the kernel.
  147. // Note that we want to do this as early as possible.
  148. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  149. (*ctor)();
  150. APIC::initialize();
  151. InterruptManagement::initialize();
  152. ACPI::initialize();
  153. // Initialize TimeManagement before using randomness!
  154. TimeManagement::initialize(0);
  155. __stack_chk_guard = get_fast_random<size_t>();
  156. ProcFSComponentRegistry::initialize();
  157. Thread::initialize();
  158. Process::initialize();
  159. Scheduler::initialize();
  160. dmesgln("Starting SerenityOS...");
  161. {
  162. RefPtr<Thread> init_stage2_thread;
  163. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2, nullptr, THREAD_AFFINITY_DEFAULT, Process::RegisterProcess::No);
  164. // We need to make sure we drop the reference for init_stage2_thread
  165. // before calling into Scheduler::start, otherwise we will have a
  166. // dangling Thread that never gets cleaned up
  167. }
  168. Scheduler::start();
  169. VERIFY_NOT_REACHED();
  170. }
  171. //
  172. // This is where C++ execution begins for APs, after boot.S transfers control here.
  173. //
  174. // The purpose of init_ap() is to initialize APs for multi-tasking.
  175. //
  176. extern "C" [[noreturn]] UNMAP_AFTER_INIT void init_ap(FlatPtr cpu, Processor* processor_info)
  177. {
  178. processor_info->early_initialize(cpu);
  179. processor_info->initialize(cpu);
  180. MemoryManager::initialize(cpu);
  181. Scheduler::set_idle_thread(APIC::the().get_idle_thread(cpu));
  182. Scheduler::start();
  183. VERIFY_NOT_REACHED();
  184. }
  185. //
  186. // This method is called once a CPU enters the scheduler and its idle thread
  187. // At this point the initial boot stack can be freed
  188. //
  189. extern "C" UNMAP_AFTER_INIT void init_finished(u32 cpu)
  190. {
  191. if (cpu == 0) {
  192. // TODO: we can reuse the boot stack, maybe for kmalloc()?
  193. } else {
  194. APIC::the().init_finished(cpu);
  195. TimeManagement::initialize(cpu);
  196. }
  197. }
  198. void init_stage2(void*)
  199. {
  200. // This is a little bit of a hack. We can't register our process at the time we're
  201. // creating it, but we need to be registered otherwise finalization won't be happy.
  202. // The colonel process gets away without having to do this because it never exits.
  203. Process::register_new(*Process::current());
  204. WorkQueue::initialize();
  205. if (APIC::initialized() && APIC::the().enabled_processor_count() > 1) {
  206. // We can't start the APs until we have a scheduler up and running.
  207. // We need to be able to process ICI messages, otherwise another
  208. // core may send too many and end up deadlocking once the pool is
  209. // exhausted
  210. APIC::the().boot_aps();
  211. }
  212. // Initialize the PCI Bus as early as possible, for early boot (PCI based) serial logging
  213. SysFSComponentRegistry::initialize();
  214. PCI::initialize();
  215. PCISerialDevice::detect();
  216. VirtualFileSystem::initialize();
  217. NullDevice::initialize();
  218. if (!get_serial_debug())
  219. (void)SerialDevice::must_create(0).leak_ref();
  220. (void)SerialDevice::must_create(1).leak_ref();
  221. (void)SerialDevice::must_create(2).leak_ref();
  222. (void)SerialDevice::must_create(3).leak_ref();
  223. VMWareBackdoor::the(); // don't wait until first mouse packet
  224. HIDManagement::initialize();
  225. GraphicsManagement::the().initialize();
  226. ConsoleManagement::the().initialize();
  227. SyncTask::spawn();
  228. FinalizerTask::spawn();
  229. auto boot_profiling = kernel_command_line().is_boot_profiling_enabled();
  230. USB::UHCIController::detect();
  231. BIOSSysFSDirectory::initialize();
  232. ACPI::ACPISysFSDirectory::initialize();
  233. VirtIO::detect();
  234. NetworkingManagement::the().initialize();
  235. Syscall::initialize();
  236. (void)MemoryDevice::must_create().leak_ref();
  237. (void)ZeroDevice::must_create().leak_ref();
  238. (void)FullDevice::must_create().leak_ref();
  239. (void)RandomDevice::must_create().leak_ref();
  240. PTYMultiplexer::initialize();
  241. SB16::detect();
  242. StorageManagement::initialize(kernel_command_line().root_device(), kernel_command_line().is_force_pio());
  243. if (!VirtualFileSystem::the().mount_root(StorageManagement::the().root_filesystem())) {
  244. PANIC("VirtualFileSystem::mount_root failed");
  245. }
  246. Process::current()->set_root_directory(VirtualFileSystem::the().root_custody());
  247. // Switch out of early boot mode.
  248. g_in_early_boot = false;
  249. // NOTE: Everything marked READONLY_AFTER_INIT becomes non-writable after this point.
  250. MM.protect_readonly_after_init_memory();
  251. // NOTE: Everything marked UNMAP_AFTER_INIT becomes inaccessible after this point.
  252. MM.unmap_text_after_init();
  253. // NOTE: Everything in the .ksyms section becomes inaccessible after this point.
  254. MM.unmap_ksyms_after_init();
  255. int error;
  256. // FIXME: It would be nicer to set the mode from userspace.
  257. // FIXME: It would be smarter to not hardcode that the first tty is the only graphical one
  258. ConsoleManagement::the().first_tty()->set_graphical(GraphicsManagement::the().framebuffer_devices_exist());
  259. RefPtr<Thread> thread;
  260. auto userspace_init = kernel_command_line().userspace_init();
  261. auto init_args = kernel_command_line().userspace_init_args();
  262. Process::create_user_process(thread, userspace_init, (uid_t)0, (gid_t)0, ProcessID(0), error, move(init_args), {}, tty0);
  263. if (error != 0) {
  264. PANIC("init_stage2: Error spawning SystemServer: {}", error);
  265. }
  266. thread->set_priority(THREAD_PRIORITY_HIGH);
  267. if (boot_profiling) {
  268. dbgln("Starting full system boot profiling");
  269. auto result = Process::current()->sys$profiling_enable(-1, ~0ull);
  270. VERIFY(!result.is_error());
  271. }
  272. NetworkTask::spawn();
  273. Process::current()->sys$exit(0);
  274. VERIFY_NOT_REACHED();
  275. }
  276. UNMAP_AFTER_INIT void setup_serial_debug()
  277. {
  278. // serial_debug will output all the dbgln() data to COM1 at
  279. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  280. // process on live hardware.
  281. if (StringView(kernel_cmdline).contains("serial_debug")) {
  282. set_serial_debug(true);
  283. }
  284. }
  285. extern "C" {
  286. multiboot_info_t* multiboot_info_ptr;
  287. }
  288. // Define some Itanium C++ ABI methods to stop the linker from complaining.
  289. // If we actually call these something has gone horribly wrong
  290. void* __dso_handle __attribute__((visibility("hidden")));
  291. }