init.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/x86/CPU.h>
  11. #include <Kernel/CMOS.h>
  12. #include <Kernel/CommandLine.h>
  13. #include <Kernel/DMI.h>
  14. #include <Kernel/Devices/FullDevice.h>
  15. #include <Kernel/Devices/HID/HIDManagement.h>
  16. #include <Kernel/Devices/MemoryDevice.h>
  17. #include <Kernel/Devices/NullDevice.h>
  18. #include <Kernel/Devices/RandomDevice.h>
  19. #include <Kernel/Devices/SB16.h>
  20. #include <Kernel/Devices/SerialDevice.h>
  21. #include <Kernel/Devices/USB/UHCIController.h>
  22. #include <Kernel/Devices/VMWareBackdoor.h>
  23. #include <Kernel/Devices/ZeroDevice.h>
  24. #include <Kernel/FileSystem/Ext2FileSystem.h>
  25. #include <Kernel/FileSystem/VirtualFileSystem.h>
  26. #include <Kernel/Graphics/GraphicsManagement.h>
  27. #include <Kernel/Heap/SlabAllocator.h>
  28. #include <Kernel/Heap/kmalloc.h>
  29. #include <Kernel/Interrupts/APIC.h>
  30. #include <Kernel/Interrupts/InterruptManagement.h>
  31. #include <Kernel/Interrupts/PIC.h>
  32. #include <Kernel/KSyms.h>
  33. #include <Kernel/Multiboot.h>
  34. #include <Kernel/Net/E1000NetworkAdapter.h>
  35. #include <Kernel/Net/LoopbackAdapter.h>
  36. #include <Kernel/Net/NE2000NetworkAdapter.h>
  37. #include <Kernel/Net/NetworkTask.h>
  38. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  39. #include <Kernel/PCI/Access.h>
  40. #include <Kernel/PCI/Initializer.h>
  41. #include <Kernel/Panic.h>
  42. #include <Kernel/Process.h>
  43. #include <Kernel/RTC.h>
  44. #include <Kernel/Random.h>
  45. #include <Kernel/Scheduler.h>
  46. #include <Kernel/Storage/StorageManagement.h>
  47. #include <Kernel/TTY/ConsoleManagement.h>
  48. #include <Kernel/TTY/PTYMultiplexer.h>
  49. #include <Kernel/TTY/VirtualConsole.h>
  50. #include <Kernel/Tasks/FinalizerTask.h>
  51. #include <Kernel/Tasks/SyncTask.h>
  52. #include <Kernel/Time/TimeManagement.h>
  53. #include <Kernel/VM/MemoryManager.h>
  54. #include <Kernel/VirtIO/VirtIO.h>
  55. #include <Kernel/WorkQueue.h>
  56. #include <Kernel/kstdio.h>
  57. // Defined in the linker script
  58. typedef void (*ctor_func_t)();
  59. extern ctor_func_t start_heap_ctors;
  60. extern ctor_func_t end_heap_ctors;
  61. extern ctor_func_t start_ctors;
  62. extern ctor_func_t end_ctors;
  63. extern u32 __stack_chk_guard;
  64. u32 __stack_chk_guard;
  65. extern "C" u8* start_of_safemem_text;
  66. extern "C" u8* end_of_safemem_text;
  67. extern "C" u8* start_of_safemem_atomic_text;
  68. extern "C" u8* end_of_safemem_atomic_text;
  69. extern "C" u8* end_of_kernel_image;
  70. multiboot_module_entry_t multiboot_copy_boot_modules_array[16];
  71. size_t multiboot_copy_boot_modules_count;
  72. extern "C" const char kernel_cmdline[4096];
  73. namespace Kernel {
  74. [[noreturn]] static void init_stage2(void*);
  75. static void setup_serial_debug();
  76. // boot.S expects these functions to exactly have the following signatures.
  77. // We declare them here to ensure their signatures don't accidentally change.
  78. extern "C" void init_finished(u32 cpu) __attribute__((used));
  79. extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info);
  80. extern "C" [[noreturn]] void init();
  81. READONLY_AFTER_INIT VirtualConsole* tty0;
  82. static Processor s_bsp_processor; // global but let's keep it "private"
  83. // SerenityOS Kernel C++ entry point :^)
  84. //
  85. // This is where C++ execution begins, after boot.S transfers control here.
  86. //
  87. // The purpose of init() is to start multi-tasking. It does the bare minimum
  88. // amount of work needed to start the scheduler.
  89. //
  90. // Once multi-tasking is ready, we spawn a new thread that starts in the
  91. // init_stage2() function. Initialization continues there.
  92. extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
  93. {
  94. if ((FlatPtr)&end_of_kernel_image >= 0xc2000000u) {
  95. // The kernel has grown too large again!
  96. asm volatile("cli;hlt");
  97. }
  98. setup_serial_debug();
  99. // We need to copy the command line before kmalloc is initialized,
  100. // as it may overwrite parts of multiboot!
  101. CommandLine::early_initialize(kernel_cmdline);
  102. 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));
  103. multiboot_copy_boot_modules_count = multiboot_info_ptr->mods_count;
  104. s_bsp_processor.early_initialize(0);
  105. // Invoke the constructors needed for the kernel heap
  106. for (ctor_func_t* ctor = &start_heap_ctors; ctor < &end_heap_ctors; ctor++)
  107. (*ctor)();
  108. kmalloc_init();
  109. slab_alloc_init();
  110. ConsoleDevice::initialize();
  111. s_bsp_processor.initialize(0);
  112. CommandLine::initialize();
  113. MemoryManager::initialize(0);
  114. // Ensure that the safemem sections are not empty. This could happen if the linker accidentally discards the sections.
  115. VERIFY(&start_of_safemem_text != &end_of_safemem_text);
  116. VERIFY(&start_of_safemem_atomic_text != &end_of_safemem_atomic_text);
  117. // Invoke all static global constructors in the kernel.
  118. // Note that we want to do this as early as possible.
  119. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  120. (*ctor)();
  121. APIC::initialize();
  122. InterruptManagement::initialize();
  123. ACPI::initialize();
  124. VFS::initialize();
  125. dmesgln("Starting SerenityOS...");
  126. TimeManagement::initialize(0);
  127. __stack_chk_guard = get_fast_random<u32>();
  128. NullDevice::initialize();
  129. if (!get_serial_debug())
  130. new SerialDevice(SERIAL_COM1_ADDR, 64);
  131. new SerialDevice(SERIAL_COM2_ADDR, 65);
  132. new SerialDevice(SERIAL_COM3_ADDR, 66);
  133. new SerialDevice(SERIAL_COM4_ADDR, 67);
  134. VMWareBackdoor::the(); // don't wait until first mouse packet
  135. HIDManagement::initialize();
  136. PCI::initialize();
  137. GraphicsManagement::the().initialize();
  138. ConsoleManagement::the().initialize();
  139. Thread::initialize();
  140. Process::initialize();
  141. Scheduler::initialize();
  142. WorkQueue::initialize();
  143. {
  144. RefPtr<Thread> init_stage2_thread;
  145. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2, nullptr);
  146. // We need to make sure we drop the reference for init_stage2_thread
  147. // before calling into Scheduler::start, otherwise we will have a
  148. // dangling Thread that never gets cleaned up
  149. }
  150. Scheduler::start();
  151. VERIFY_NOT_REACHED();
  152. }
  153. //
  154. // This is where C++ execution begins for APs, after boot.S transfers control here.
  155. //
  156. // The purpose of init_ap() is to initialize APs for multi-tasking.
  157. //
  158. extern "C" UNMAP_AFTER_INIT [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
  159. {
  160. processor_info->early_initialize(cpu);
  161. processor_info->initialize(cpu);
  162. MemoryManager::initialize(cpu);
  163. Scheduler::set_idle_thread(APIC::the().get_idle_thread(cpu));
  164. Scheduler::start();
  165. VERIFY_NOT_REACHED();
  166. }
  167. //
  168. // This method is called once a CPU enters the scheduler and its idle thread
  169. // At this point the initial boot stack can be freed
  170. //
  171. extern "C" UNMAP_AFTER_INIT void init_finished(u32 cpu)
  172. {
  173. if (cpu == 0) {
  174. // TODO: we can reuse the boot stack, maybe for kmalloc()?
  175. } else {
  176. APIC::the().init_finished(cpu);
  177. TimeManagement::initialize(cpu);
  178. }
  179. }
  180. void init_stage2(void*)
  181. {
  182. if (APIC::initialized() && APIC::the().enabled_processor_count() > 1) {
  183. // We can't start the APs until we have a scheduler up and running.
  184. // We need to be able to process ICI messages, otherwise another
  185. // core may send too many and end up deadlocking once the pool is
  186. // exhausted
  187. APIC::the().boot_aps();
  188. }
  189. SyncTask::spawn();
  190. FinalizerTask::spawn();
  191. auto boot_profiling = kernel_command_line().is_boot_profiling_enabled();
  192. USB::UHCIController::detect();
  193. DMIExpose::initialize();
  194. VirtIO::detect();
  195. E1000NetworkAdapter::detect();
  196. NE2000NetworkAdapter::detect();
  197. RTL8139NetworkAdapter::detect();
  198. LoopbackAdapter::the();
  199. Syscall::initialize();
  200. new MemoryDevice;
  201. new ZeroDevice;
  202. new FullDevice;
  203. new RandomDevice;
  204. PTYMultiplexer::initialize();
  205. SB16::detect();
  206. StorageManagement::initialize(kernel_command_line().root_device(), kernel_command_line().is_force_pio());
  207. if (!VFS::the().mount_root(StorageManagement::the().root_filesystem())) {
  208. PANIC("VFS::mount_root failed");
  209. }
  210. Process::current()->set_root_directory(VFS::the().root_custody());
  211. load_kernel_symbol_table();
  212. // NOTE: Everything marked READONLY_AFTER_INIT becomes non-writable after this point.
  213. MM.protect_readonly_after_init_memory();
  214. // NOTE: Everything marked UNMAP_AFTER_INIT becomes inaccessible after this point.
  215. MM.unmap_memory_after_init();
  216. int error;
  217. // FIXME: It would be nicer to set the mode from userspace.
  218. // FIXME: It would be smarter to not hardcode that the first tty is the only graphical one
  219. ConsoleManagement::the().first_tty()->set_graphical(GraphicsManagement::the().framebuffer_devices_exist());
  220. RefPtr<Thread> thread;
  221. auto userspace_init = kernel_command_line().userspace_init();
  222. auto init_args = kernel_command_line().userspace_init_args();
  223. Process::create_user_process(thread, userspace_init, (uid_t)0, (gid_t)0, ProcessID(0), error, move(init_args), {}, tty0);
  224. if (error != 0) {
  225. PANIC("init_stage2: Error spawning SystemServer: {}", error);
  226. }
  227. thread->set_priority(THREAD_PRIORITY_HIGH);
  228. if (boot_profiling) {
  229. dbgln("Starting full system boot profiling");
  230. auto result = Process::current()->sys$profiling_enable(-1);
  231. VERIFY(!result.is_error());
  232. }
  233. NetworkTask::spawn();
  234. Process::current()->sys$exit(0);
  235. VERIFY_NOT_REACHED();
  236. }
  237. UNMAP_AFTER_INIT void setup_serial_debug()
  238. {
  239. // serial_debug will output all the dbgln() data to COM1 at
  240. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  241. // process on live hardware.
  242. if (StringView(kernel_cmdline).contains("serial_debug")) {
  243. set_serial_debug(true);
  244. }
  245. }
  246. extern "C" {
  247. multiboot_info_t* multiboot_info_ptr;
  248. }
  249. // Define some Itanium C++ ABI methods to stop the linker from complaining.
  250. // If we actually call these something has gone horribly wrong
  251. void* __dso_handle __attribute__((visibility("hidden")));
  252. }