init.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/i386/CPU.h>
  31. #include <Kernel/CMOS.h>
  32. #include <Kernel/CommandLine.h>
  33. #include <Kernel/Devices/BXVGADevice.h>
  34. #include <Kernel/Devices/DiskPartition.h>
  35. #include <Kernel/Devices/EBRPartitionTable.h>
  36. #include <Kernel/Devices/FullDevice.h>
  37. #include <Kernel/Devices/GPTPartitionTable.h>
  38. #include <Kernel/Devices/KeyboardDevice.h>
  39. #include <Kernel/Devices/MBRPartitionTable.h>
  40. #include <Kernel/Devices/MBVGADevice.h>
  41. #include <Kernel/Devices/NullDevice.h>
  42. #include <Kernel/Devices/PATAChannel.h>
  43. #include <Kernel/Devices/PATADiskDevice.h>
  44. #include <Kernel/Devices/PS2MouseDevice.h>
  45. #include <Kernel/Devices/RandomDevice.h>
  46. #include <Kernel/Devices/SB16.h>
  47. #include <Kernel/Devices/SerialDevice.h>
  48. #include <Kernel/Devices/VMWareBackdoor.h>
  49. #include <Kernel/Devices/ZeroDevice.h>
  50. #include <Kernel/FileSystem/Ext2FileSystem.h>
  51. #include <Kernel/FileSystem/VirtualFileSystem.h>
  52. #include <Kernel/Heap/SlabAllocator.h>
  53. #include <Kernel/Heap/kmalloc.h>
  54. #include <Kernel/Interrupts/APIC.h>
  55. #include <Kernel/Interrupts/InterruptManagement.h>
  56. #include <Kernel/Interrupts/PIC.h>
  57. #include <Kernel/KSyms.h>
  58. #include <Kernel/Multiboot.h>
  59. #include <Kernel/Net/E1000NetworkAdapter.h>
  60. #include <Kernel/Net/LoopbackAdapter.h>
  61. #include <Kernel/Net/NetworkTask.h>
  62. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  63. #include <Kernel/PCI/Access.h>
  64. #include <Kernel/PCI/Initializer.h>
  65. #include <Kernel/Process.h>
  66. #include <Kernel/RTC.h>
  67. #include <Kernel/Random.h>
  68. #include <Kernel/Scheduler.h>
  69. #include <Kernel/TTY/PTYMultiplexer.h>
  70. #include <Kernel/TTY/VirtualConsole.h>
  71. #include <Kernel/Tasks/FinalizerTask.h>
  72. #include <Kernel/Tasks/SyncTask.h>
  73. #include <Kernel/Time/TimeManagement.h>
  74. #include <Kernel/VM/MemoryManager.h>
  75. // Defined in the linker script
  76. typedef void (*ctor_func_t)();
  77. extern ctor_func_t start_heap_ctors;
  78. extern ctor_func_t end_heap_ctors;
  79. extern ctor_func_t start_ctors;
  80. extern ctor_func_t end_ctors;
  81. extern u32 __stack_chk_guard;
  82. u32 __stack_chk_guard;
  83. namespace Kernel {
  84. [[noreturn]] static void init_stage2();
  85. static void setup_serial_debug();
  86. // boot.S expects these functions precisely this this. We declare them here
  87. // to ensure the signatures don't accidentally change.
  88. extern "C" void init_finished(u32 cpu);
  89. extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info);
  90. extern "C" [[noreturn]] void init();
  91. VirtualConsole* tty0;
  92. static Processor s_bsp_processor; // global but let's keep it "private"
  93. // SerenityOS Kernel C++ entry point :^)
  94. //
  95. // This is where C++ execution begins, after boot.S transfers control here.
  96. //
  97. // The purpose of init() is to start multi-tasking. It does the bare minimum
  98. // amount of work needed to start the scheduler.
  99. //
  100. // Once multi-tasking is ready, we spawn a new thread that starts in the
  101. // init_stage2() function. Initialization continues there.
  102. extern "C" [[noreturn]] void init()
  103. {
  104. setup_serial_debug();
  105. // We need to copy the command line before kmalloc is initialized,
  106. // as it may overwrite parts of multiboot!
  107. CommandLine::early_initialize(reinterpret_cast<const char*>(low_physical_to_virtual(multiboot_info_ptr->cmdline)));
  108. s_bsp_processor.early_initialize(0);
  109. // Invoke the constructors needed for the kernel heap
  110. for (ctor_func_t* ctor = &start_heap_ctors; ctor < &end_heap_ctors; ctor++)
  111. (*ctor)();
  112. kmalloc_init();
  113. slab_alloc_init();
  114. s_bsp_processor.initialize(0);
  115. CommandLine::initialize();
  116. MemoryManager::initialize(0);
  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. KeyboardDevice::initialize();
  126. PS2MouseDevice::create();
  127. Console::initialize();
  128. klog() << "Starting SerenityOS...";
  129. __stack_chk_guard = get_fast_random<u32>();
  130. TimeManagement::initialize();
  131. NullDevice::initialize();
  132. if (!get_serial_debug())
  133. new SerialDevice(SERIAL_COM1_ADDR, 64);
  134. new SerialDevice(SERIAL_COM2_ADDR, 65);
  135. new SerialDevice(SERIAL_COM3_ADDR, 66);
  136. new SerialDevice(SERIAL_COM4_ADDR, 67);
  137. VirtualConsole::initialize();
  138. tty0 = new VirtualConsole(0);
  139. for (unsigned i = 1; i < s_max_virtual_consoles; i++) {
  140. new VirtualConsole(i);
  141. }
  142. VirtualConsole::switch_to(0);
  143. Process::initialize();
  144. Scheduler::initialize();
  145. Thread* init_stage2_thread = nullptr;
  146. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2);
  147. Scheduler::start();
  148. ASSERT_NOT_REACHED();
  149. }
  150. //
  151. // This is where C++ execution begins for APs, after boot.S transfers control here.
  152. //
  153. // The purpose of init_ap() is to initialize APs for multi-tasking.
  154. //
  155. extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
  156. {
  157. processor_info->early_initialize(cpu);
  158. processor_info->initialize(cpu);
  159. MemoryManager::initialize(cpu);
  160. Scheduler::set_idle_thread(APIC::the().get_idle_thread(cpu));
  161. Scheduler::start();
  162. ASSERT_NOT_REACHED();
  163. }
  164. //
  165. // This method is called once a CPU enters the scheduler and its idle thread
  166. // At this point the initial boot stack can be freed
  167. //
  168. extern "C" void init_finished(u32 cpu)
  169. {
  170. if (cpu == 0) {
  171. // TODO: we can reuse the boot stack, maybe for kmalloc()?
  172. } else {
  173. APIC::the().init_finished(cpu);
  174. }
  175. }
  176. void init_stage2()
  177. {
  178. if (APIC::initialized() && APIC::the().enabled_processor_count() > 1) {
  179. // We can't start the APs until we have a scheduler up and running.
  180. // We need to be able to process ICI messages, otherwise another
  181. // core may send too many and end up deadlocking once the pool is
  182. // exhausted
  183. APIC::the().boot_aps();
  184. }
  185. SyncTask::spawn();
  186. FinalizerTask::spawn();
  187. PCI::initialize();
  188. bool text_mode = kernel_command_line().lookup("boot_mode").value_or("graphical") == "text";
  189. if (text_mode) {
  190. dbg() << "Text mode enabled";
  191. } else {
  192. bool bxvga_found = false;
  193. PCI::enumerate([&](const PCI::Address&, PCI::ID id) {
  194. if ((id.vendor_id == 0x1234 && id.device_id == 0x1111) || (id.vendor_id == 0x80ee && id.device_id == 0xbeef))
  195. bxvga_found = true;
  196. });
  197. if (bxvga_found) {
  198. BXVGADevice::initialize();
  199. } else {
  200. if (multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB || multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT) {
  201. new MBVGADevice(
  202. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  203. multiboot_info_ptr->framebuffer_pitch,
  204. multiboot_info_ptr->framebuffer_width,
  205. multiboot_info_ptr->framebuffer_height);
  206. } else {
  207. BXVGADevice::initialize();
  208. }
  209. }
  210. }
  211. E1000NetworkAdapter::detect();
  212. RTL8139NetworkAdapter::detect();
  213. LoopbackAdapter::the();
  214. Syscall::initialize();
  215. new ZeroDevice;
  216. new FullDevice;
  217. new RandomDevice;
  218. PTYMultiplexer::initialize();
  219. new SB16;
  220. bool force_pio = kernel_command_line().contains("force_pio");
  221. auto root = kernel_command_line().lookup("root").value_or("/dev/hda");
  222. if (!root.starts_with("/dev/hda")) {
  223. klog() << "init_stage2: root filesystem must be on the first IDE hard drive (/dev/hda)";
  224. Processor::halt();
  225. }
  226. auto pata0 = PATAChannel::create(PATAChannel::ChannelType::Primary, force_pio);
  227. NonnullRefPtr<BlockDevice> root_dev = *pata0->master_device();
  228. root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
  229. if (root.length()) {
  230. auto partition_number = root.to_uint();
  231. if (!partition_number.has_value()) {
  232. klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
  233. Processor::halt();
  234. }
  235. MBRPartitionTable mbr(root_dev);
  236. if (!mbr.initialize()) {
  237. klog() << "init_stage2: couldn't read MBR from disk";
  238. Processor::halt();
  239. }
  240. if (mbr.is_protective_mbr()) {
  241. dbg() << "GPT Partitioned Storage Detected!";
  242. GPTPartitionTable gpt(root_dev);
  243. if (!gpt.initialize()) {
  244. klog() << "init_stage2: couldn't read GPT from disk";
  245. Processor::halt();
  246. }
  247. auto partition = gpt.partition(partition_number.value());
  248. if (!partition) {
  249. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  250. Processor::halt();
  251. }
  252. root_dev = *partition;
  253. } else {
  254. dbg() << "MBR Partitioned Storage Detected!";
  255. if (mbr.contains_ebr()) {
  256. EBRPartitionTable ebr(root_dev);
  257. if (!ebr.initialize()) {
  258. klog() << "init_stage2: couldn't read EBR from disk";
  259. Processor::halt();
  260. }
  261. auto partition = ebr.partition(partition_number.value());
  262. if (!partition) {
  263. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  264. Processor::halt();
  265. }
  266. root_dev = *partition;
  267. } else {
  268. if (partition_number.value() < 1 || partition_number.value() > 4) {
  269. klog() << "init_stage2: invalid partition number " << partition_number.value() << "; expected 1 to 4";
  270. Processor::halt();
  271. }
  272. auto partition = mbr.partition(partition_number.value());
  273. if (!partition) {
  274. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  275. Processor::halt();
  276. }
  277. root_dev = *partition;
  278. }
  279. }
  280. }
  281. auto e2fs = Ext2FS::create(*FileDescription::create(root_dev));
  282. if (!e2fs->initialize()) {
  283. klog() << "init_stage2: couldn't open root filesystem";
  284. Processor::halt();
  285. }
  286. if (!VFS::the().mount_root(e2fs)) {
  287. klog() << "VFS::mount_root failed";
  288. Processor::halt();
  289. }
  290. Process::current()->set_root_directory(VFS::the().root_custody());
  291. load_kernel_symbol_table();
  292. int error;
  293. // FIXME: It would be nicer to set the mode from userspace.
  294. tty0->set_graphical(!text_mode);
  295. Thread* thread = nullptr;
  296. auto userspace_init = kernel_command_line().lookup("init").value_or("/bin/SystemServer");
  297. Process::create_user_process(thread, userspace_init, (uid_t)0, (gid_t)0, ProcessID(0), error, {}, {}, tty0);
  298. if (error != 0) {
  299. klog() << "init_stage2: error spawning SystemServer: " << error;
  300. Processor::halt();
  301. }
  302. thread->set_priority(THREAD_PRIORITY_HIGH);
  303. NetworkTask::spawn();
  304. Process::current()->sys$exit(0);
  305. ASSERT_NOT_REACHED();
  306. }
  307. void setup_serial_debug()
  308. {
  309. // serial_debug will output all the klog() and dbg() data to COM1 at
  310. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  311. // process on live hardware.
  312. //
  313. // note: it must be the first option in the boot cmdline.
  314. u32 cmdline = low_physical_to_virtual(multiboot_info_ptr->cmdline);
  315. if (cmdline && StringView(reinterpret_cast<const char*>(cmdline)).starts_with("serial_debug"))
  316. set_serial_debug(true);
  317. }
  318. extern "C" {
  319. multiboot_info_t* multiboot_info_ptr;
  320. }
  321. // Define some Itanium C++ ABI methods to stop the linker from complaining.
  322. // If we actually call these something has gone horribly wrong
  323. void* __dso_handle __attribute__((visibility("hidden")));
  324. }