init.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Types.h>
  27. #include <Kernel/ACPI/DynamicParser.h>
  28. #include <Kernel/ACPI/Initialize.h>
  29. #include <Kernel/ACPI/MultiProcessorParser.h>
  30. #include <Kernel/Arch/x86/CPU.h>
  31. #include <Kernel/CMOS.h>
  32. #include <Kernel/CommandLine.h>
  33. #include <Kernel/DMI.h>
  34. #include <Kernel/Devices/BXVGADevice.h>
  35. #include <Kernel/Devices/FullDevice.h>
  36. #include <Kernel/Devices/HID/HIDManagement.h>
  37. #include <Kernel/Devices/MBVGADevice.h>
  38. #include <Kernel/Devices/MemoryDevice.h>
  39. #include <Kernel/Devices/NullDevice.h>
  40. #include <Kernel/Devices/RandomDevice.h>
  41. #include <Kernel/Devices/SB16.h>
  42. #include <Kernel/Devices/SerialDevice.h>
  43. #include <Kernel/Devices/USB/UHCIController.h>
  44. #include <Kernel/Devices/VMWareBackdoor.h>
  45. #include <Kernel/Devices/ZeroDevice.h>
  46. #include <Kernel/FileSystem/Ext2FileSystem.h>
  47. #include <Kernel/FileSystem/VirtualFileSystem.h>
  48. #include <Kernel/Heap/SlabAllocator.h>
  49. #include <Kernel/Heap/kmalloc.h>
  50. #include <Kernel/Interrupts/APIC.h>
  51. #include <Kernel/Interrupts/InterruptManagement.h>
  52. #include <Kernel/Interrupts/PIC.h>
  53. #include <Kernel/KSyms.h>
  54. #include <Kernel/Multiboot.h>
  55. #include <Kernel/Net/E1000NetworkAdapter.h>
  56. #include <Kernel/Net/LoopbackAdapter.h>
  57. #include <Kernel/Net/NE2000NetworkAdapter.h>
  58. #include <Kernel/Net/NetworkTask.h>
  59. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  60. #include <Kernel/PCI/Access.h>
  61. #include <Kernel/PCI/Initializer.h>
  62. #include <Kernel/Panic.h>
  63. #include <Kernel/Process.h>
  64. #include <Kernel/RTC.h>
  65. #include <Kernel/Random.h>
  66. #include <Kernel/Scheduler.h>
  67. #include <Kernel/Storage/StorageManagement.h>
  68. #include <Kernel/TTY/PTYMultiplexer.h>
  69. #include <Kernel/TTY/VirtualConsole.h>
  70. #include <Kernel/Tasks/FinalizerTask.h>
  71. #include <Kernel/Tasks/SyncTask.h>
  72. #include <Kernel/Time/TimeManagement.h>
  73. #include <Kernel/VM/MemoryManager.h>
  74. #include <Kernel/WorkQueue.h>
  75. #include <Kernel/kstdio.h>
  76. // Defined in the linker script
  77. typedef void (*ctor_func_t)();
  78. extern ctor_func_t start_heap_ctors;
  79. extern ctor_func_t end_heap_ctors;
  80. extern ctor_func_t start_ctors;
  81. extern ctor_func_t end_ctors;
  82. extern u32 __stack_chk_guard;
  83. u32 __stack_chk_guard;
  84. extern "C" u8* start_of_safemem_text;
  85. extern "C" u8* end_of_safemem_text;
  86. extern "C" u8* start_of_safemem_atomic_text;
  87. extern "C" u8* end_of_safemem_atomic_text;
  88. extern "C" FlatPtr end_of_kernel_image;
  89. multiboot_module_entry_t multiboot_copy_boot_modules_array[16];
  90. size_t multiboot_copy_boot_modules_count;
  91. extern "C" const char kernel_cmdline[4096];
  92. namespace Kernel {
  93. [[noreturn]] static void init_stage2(void*);
  94. static void setup_serial_debug();
  95. // boot.S expects these functions to exactly have the following signatures.
  96. // We declare them here to ensure their signatures don't accidentally change.
  97. extern "C" void init_finished(u32 cpu);
  98. extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info);
  99. extern "C" [[noreturn]] void init();
  100. READONLY_AFTER_INIT VirtualConsole* tty0;
  101. static Processor s_bsp_processor; // global but let's keep it "private"
  102. // SerenityOS Kernel C++ entry point :^)
  103. //
  104. // This is where C++ execution begins, after boot.S transfers control here.
  105. //
  106. // The purpose of init() is to start multi-tasking. It does the bare minimum
  107. // amount of work needed to start the scheduler.
  108. //
  109. // Once multi-tasking is ready, we spawn a new thread that starts in the
  110. // init_stage2() function. Initialization continues there.
  111. extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
  112. {
  113. if ((FlatPtr)&end_of_kernel_image >= 0xc1000000u) {
  114. // The kernel has grown too large again!
  115. asm volatile("cli;hlt");
  116. }
  117. setup_serial_debug();
  118. // We need to copy the command line before kmalloc is initialized,
  119. // as it may overwrite parts of multiboot!
  120. CommandLine::early_initialize(kernel_cmdline);
  121. 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));
  122. multiboot_copy_boot_modules_count = multiboot_info_ptr->mods_count;
  123. s_bsp_processor.early_initialize(0);
  124. // Invoke the constructors needed for the kernel heap
  125. for (ctor_func_t* ctor = &start_heap_ctors; ctor < &end_heap_ctors; ctor++)
  126. (*ctor)();
  127. kmalloc_init();
  128. slab_alloc_init();
  129. s_bsp_processor.initialize(0);
  130. CommandLine::initialize();
  131. MemoryManager::initialize(0);
  132. // Ensure that the safemem sections are not empty. This could happen if the linker accidentally discards the sections.
  133. VERIFY(&start_of_safemem_text != &end_of_safemem_text);
  134. VERIFY(&start_of_safemem_atomic_text != &end_of_safemem_atomic_text);
  135. // Invoke all static global constructors in the kernel.
  136. // Note that we want to do this as early as possible.
  137. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  138. (*ctor)();
  139. APIC::initialize();
  140. InterruptManagement::initialize();
  141. ACPI::initialize();
  142. VFS::initialize();
  143. Console::initialize();
  144. dmesgln("Starting SerenityOS...");
  145. TimeManagement::initialize(0);
  146. __stack_chk_guard = get_fast_random<u32>();
  147. NullDevice::initialize();
  148. if (!get_serial_debug())
  149. new SerialDevice(SERIAL_COM1_ADDR, 64);
  150. new SerialDevice(SERIAL_COM2_ADDR, 65);
  151. new SerialDevice(SERIAL_COM3_ADDR, 66);
  152. new SerialDevice(SERIAL_COM4_ADDR, 67);
  153. VMWareBackdoor::the(); // don't wait until first mouse packet
  154. HIDManagement::initialize();
  155. VirtualConsole::initialize();
  156. tty0 = new VirtualConsole(0);
  157. for (unsigned i = 1; i < s_max_virtual_consoles; i++) {
  158. new VirtualConsole(i);
  159. }
  160. VirtualConsole::switch_to(0);
  161. Thread::initialize();
  162. Process::initialize();
  163. Scheduler::initialize();
  164. WorkQueue::initialize();
  165. {
  166. RefPtr<Thread> init_stage2_thread;
  167. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2, nullptr);
  168. // We need to make sure we drop the reference for init_stage2_thread
  169. // before calling into Scheduler::start, otherwise we will have a
  170. // dangling Thread that never gets cleaned up
  171. }
  172. Scheduler::start();
  173. VERIFY_NOT_REACHED();
  174. }
  175. //
  176. // This is where C++ execution begins for APs, after boot.S transfers control here.
  177. //
  178. // The purpose of init_ap() is to initialize APs for multi-tasking.
  179. //
  180. extern "C" UNMAP_AFTER_INIT [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
  181. {
  182. processor_info->early_initialize(cpu);
  183. processor_info->initialize(cpu);
  184. MemoryManager::initialize(cpu);
  185. Scheduler::set_idle_thread(APIC::the().get_idle_thread(cpu));
  186. Scheduler::start();
  187. VERIFY_NOT_REACHED();
  188. }
  189. //
  190. // This method is called once a CPU enters the scheduler and its idle thread
  191. // At this point the initial boot stack can be freed
  192. //
  193. extern "C" UNMAP_AFTER_INIT void init_finished(u32 cpu)
  194. {
  195. if (cpu == 0) {
  196. // TODO: we can reuse the boot stack, maybe for kmalloc()?
  197. } else {
  198. APIC::the().init_finished(cpu);
  199. TimeManagement::initialize(cpu);
  200. }
  201. }
  202. void init_stage2(void*)
  203. {
  204. if (APIC::initialized() && APIC::the().enabled_processor_count() > 1) {
  205. // We can't start the APs until we have a scheduler up and running.
  206. // We need to be able to process ICI messages, otherwise another
  207. // core may send too many and end up deadlocking once the pool is
  208. // exhausted
  209. APIC::the().boot_aps();
  210. }
  211. SyncTask::spawn();
  212. FinalizerTask::spawn();
  213. PCI::initialize();
  214. auto boot_profiling = kernel_command_line().is_boot_profiling_enabled();
  215. auto is_text_mode = kernel_command_line().is_text_mode();
  216. if (is_text_mode) {
  217. dbgln("Text mode enabled");
  218. } else {
  219. bool bxvga_found = false;
  220. PCI::enumerate([&](const PCI::Address&, PCI::ID id) {
  221. if ((id.vendor_id == 0x1234 && id.device_id == 0x1111) || (id.vendor_id == 0x80ee && id.device_id == 0xbeef))
  222. bxvga_found = true;
  223. });
  224. if (bxvga_found) {
  225. BXVGADevice::initialize();
  226. } else {
  227. if (multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB || multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT) {
  228. new MBVGADevice(
  229. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  230. multiboot_info_ptr->framebuffer_pitch,
  231. multiboot_info_ptr->framebuffer_width,
  232. multiboot_info_ptr->framebuffer_height);
  233. } else {
  234. BXVGADevice::initialize();
  235. }
  236. }
  237. }
  238. USB::UHCIController::detect();
  239. DMIExpose::initialize();
  240. E1000NetworkAdapter::detect();
  241. NE2000NetworkAdapter::detect();
  242. RTL8139NetworkAdapter::detect();
  243. LoopbackAdapter::the();
  244. Syscall::initialize();
  245. new MemoryDevice;
  246. new ZeroDevice;
  247. new FullDevice;
  248. new RandomDevice;
  249. PTYMultiplexer::initialize();
  250. SB16::detect();
  251. StorageManagement::initialize(kernel_command_line().root_device(), kernel_command_line().is_force_pio());
  252. if (!VFS::the().mount_root(StorageManagement::the().root_filesystem())) {
  253. PANIC("VFS::mount_root failed");
  254. }
  255. Process::current()->set_root_directory(VFS::the().root_custody());
  256. load_kernel_symbol_table();
  257. // NOTE: Everything marked READONLY_AFTER_INIT becomes non-writable after this point.
  258. MM.protect_readonly_after_init_memory();
  259. // NOTE: Everything marked UNMAP_AFTER_INIT becomes inaccessible after this point.
  260. MM.unmap_memory_after_init();
  261. int error;
  262. // FIXME: It would be nicer to set the mode from userspace.
  263. tty0->set_graphical(!is_text_mode);
  264. RefPtr<Thread> thread;
  265. auto userspace_init = kernel_command_line().userspace_init();
  266. auto init_args = kernel_command_line().userspace_init_args();
  267. Process::create_user_process(thread, userspace_init, (uid_t)0, (gid_t)0, ProcessID(0), error, move(init_args), {}, tty0);
  268. if (error != 0) {
  269. PANIC("init_stage2: Error spawning SystemServer: {}", error);
  270. }
  271. thread->set_priority(THREAD_PRIORITY_HIGH);
  272. if (boot_profiling) {
  273. dbgln("Starting full system boot profiling");
  274. auto result = Process::current()->sys$profiling_enable(-1);
  275. VERIFY(!result.is_error());
  276. }
  277. NetworkTask::spawn();
  278. Process::current()->sys$exit(0);
  279. VERIFY_NOT_REACHED();
  280. }
  281. UNMAP_AFTER_INIT void setup_serial_debug()
  282. {
  283. // serial_debug will output all the dbgln() data to COM1 at
  284. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  285. // process on live hardware.
  286. if (StringView(kernel_cmdline).contains("serial_debug")) {
  287. set_serial_debug(true);
  288. }
  289. }
  290. extern "C" {
  291. multiboot_info_t* multiboot_info_ptr;
  292. }
  293. // Define some Itanium C++ ABI methods to stop the linker from complaining.
  294. // If we actually call these something has gone horribly wrong
  295. void* __dso_handle __attribute__((visibility("hidden")));
  296. }