init.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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_ctors;
  78. extern ctor_func_t end_ctors;
  79. extern u32 __stack_chk_guard;
  80. u32 __stack_chk_guard;
  81. namespace Kernel {
  82. [[noreturn]] static void init_stage2();
  83. static void setup_serial_debug();
  84. VirtualConsole* tty0;
  85. static Processor s_bsp_processor; // global but let's keep it "private"
  86. // SerenityOS Kernel C++ entry point :^)
  87. //
  88. // This is where C++ execution begins, after boot.S transfers control here.
  89. //
  90. // The purpose of init() is to start multi-tasking. It does the bare minimum
  91. // amount of work needed to start the scheduler.
  92. //
  93. // Once multi-tasking is ready, we spawn a new thread that starts in the
  94. // init_stage2() function. Initialization continues there.
  95. extern "C" [[noreturn]] void init()
  96. {
  97. setup_serial_debug();
  98. s_bsp_processor.early_initialize(0);
  99. kmalloc_init();
  100. slab_alloc_init();
  101. s_bsp_processor.initialize(0);
  102. CommandLine::initialize(reinterpret_cast<const char*>(low_physical_to_virtual(multiboot_info_ptr->cmdline)));
  103. MemoryManager::initialize(0);
  104. // Invoke all static global constructors in the kernel.
  105. // Note that we want to do this as early as possible.
  106. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  107. (*ctor)();
  108. APIC::initialize();
  109. InterruptManagement::initialize();
  110. ACPI::initialize();
  111. new VFS;
  112. new KeyboardDevice;
  113. new PS2MouseDevice;
  114. new Console;
  115. klog() << "Starting SerenityOS...";
  116. __stack_chk_guard = get_fast_random<u32>();
  117. TimeManagement::initialize();
  118. new NullDevice;
  119. if (!get_serial_debug())
  120. new SerialDevice(SERIAL_COM1_ADDR, 64);
  121. new SerialDevice(SERIAL_COM2_ADDR, 65);
  122. new SerialDevice(SERIAL_COM3_ADDR, 66);
  123. new SerialDevice(SERIAL_COM4_ADDR, 67);
  124. VirtualConsole::initialize();
  125. tty0 = new VirtualConsole(0);
  126. new VirtualConsole(1);
  127. VirtualConsole::switch_to(0);
  128. Process::initialize();
  129. Scheduler::initialize(0);
  130. Thread* init_stage2_thread = nullptr;
  131. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2);
  132. Scheduler::start();
  133. ASSERT_NOT_REACHED();
  134. }
  135. //
  136. // This is where C++ execution begins for APs, after boot.S transfers control here.
  137. //
  138. // The purpose of init_ap() is to initialize APs for multi-tasking.
  139. //
  140. extern "C" [[noreturn]] void init_ap(u32 cpu, Processor* processor_info)
  141. {
  142. processor_info->early_initialize(cpu);
  143. klog() << "CPU #" << cpu << " processor_info at " << VirtualAddress(FlatPtr(processor_info));
  144. processor_info->initialize(cpu);
  145. MemoryManager::initialize(cpu);
  146. APIC::the().enable(cpu);
  147. Scheduler::initialize(cpu);
  148. Scheduler::start();
  149. ASSERT_NOT_REACHED();
  150. }
  151. void init_stage2()
  152. {
  153. SyncTask::spawn();
  154. FinalizerTask::spawn();
  155. PCI::initialize();
  156. bool text_mode = kernel_command_line().lookup("boot_mode").value_or("graphical") == "text";
  157. if (text_mode) {
  158. dbg() << "Text mode enabled";
  159. } else {
  160. bool bxvga_found = false;
  161. PCI::enumerate([&](const PCI::Address&, PCI::ID id) {
  162. if (id.vendor_id == 0x1234 && id.device_id == 0x1111)
  163. bxvga_found = true;
  164. });
  165. if (bxvga_found) {
  166. new BXVGADevice;
  167. } else {
  168. if (multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB || multiboot_info_ptr->framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT) {
  169. new MBVGADevice(
  170. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  171. multiboot_info_ptr->framebuffer_pitch,
  172. multiboot_info_ptr->framebuffer_width,
  173. multiboot_info_ptr->framebuffer_height);
  174. } else {
  175. new BXVGADevice;
  176. }
  177. }
  178. }
  179. E1000NetworkAdapter::detect();
  180. RTL8139NetworkAdapter::detect();
  181. LoopbackAdapter::the();
  182. Syscall::initialize();
  183. new ZeroDevice;
  184. new FullDevice;
  185. new RandomDevice;
  186. new PTYMultiplexer;
  187. new SB16;
  188. VMWareBackdoor::initialize();
  189. bool force_pio = kernel_command_line().contains("force_pio");
  190. auto root = kernel_command_line().lookup("root").value_or("/dev/hda");
  191. if (!root.starts_with("/dev/hda")) {
  192. klog() << "init_stage2: root filesystem must be on the first IDE hard drive (/dev/hda)";
  193. hang();
  194. }
  195. auto pata0 = PATAChannel::create(PATAChannel::ChannelType::Primary, force_pio);
  196. NonnullRefPtr<BlockDevice> root_dev = *pata0->master_device();
  197. root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
  198. if (root.length()) {
  199. auto partition_number = root.to_uint();
  200. if (!partition_number.has_value()) {
  201. klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
  202. hang();
  203. }
  204. MBRPartitionTable mbr(root_dev);
  205. if (!mbr.initialize()) {
  206. klog() << "init_stage2: couldn't read MBR from disk";
  207. hang();
  208. }
  209. if (mbr.is_protective_mbr()) {
  210. dbg() << "GPT Partitioned Storage Detected!";
  211. GPTPartitionTable gpt(root_dev);
  212. if (!gpt.initialize()) {
  213. klog() << "init_stage2: couldn't read GPT from disk";
  214. hang();
  215. }
  216. auto partition = gpt.partition(partition_number.value());
  217. if (!partition) {
  218. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  219. hang();
  220. }
  221. root_dev = *partition;
  222. } else {
  223. dbg() << "MBR Partitioned Storage Detected!";
  224. if (mbr.contains_ebr()) {
  225. EBRPartitionTable ebr(root_dev);
  226. if (!ebr.initialize()) {
  227. klog() << "init_stage2: couldn't read EBR from disk";
  228. hang();
  229. }
  230. auto partition = ebr.partition(partition_number.value());
  231. if (!partition) {
  232. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  233. hang();
  234. }
  235. root_dev = *partition;
  236. } else {
  237. if (partition_number.value() < 1 || partition_number.value() > 4) {
  238. klog() << "init_stage2: invalid partition number " << partition_number.value() << "; expected 1 to 4";
  239. hang();
  240. }
  241. auto partition = mbr.partition(partition_number.value());
  242. if (!partition) {
  243. klog() << "init_stage2: couldn't get partition " << partition_number.value();
  244. hang();
  245. }
  246. root_dev = *partition;
  247. }
  248. }
  249. }
  250. auto e2fs = Ext2FS::create(*FileDescription::create(root_dev));
  251. if (!e2fs->initialize()) {
  252. klog() << "init_stage2: couldn't open root filesystem";
  253. hang();
  254. }
  255. if (!VFS::the().mount_root(e2fs)) {
  256. klog() << "VFS::mount_root failed";
  257. hang();
  258. }
  259. Process::current()->set_root_directory(VFS::the().root_custody());
  260. load_kernel_symbol_table();
  261. int error;
  262. // FIXME: It would be nicer to set the mode from userspace.
  263. tty0->set_graphical(!text_mode);
  264. Thread* thread = nullptr;
  265. auto userspace_init = kernel_command_line().lookup("init").value_or("/bin/SystemServer");
  266. Process::create_user_process(thread, userspace_init, (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  267. if (error != 0) {
  268. klog() << "init_stage2: error spawning SystemServer: " << error;
  269. hang();
  270. }
  271. thread->set_priority(THREAD_PRIORITY_HIGH);
  272. NetworkTask::spawn();
  273. Process::current()->sys$exit(0);
  274. ASSERT_NOT_REACHED();
  275. }
  276. void setup_serial_debug()
  277. {
  278. // this is only used one time, directly below here. we can't use this part
  279. // of libc at this point in the boot process, or we'd just pull strstr in
  280. // from <string.h>.
  281. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  282. while (*search)
  283. if (*search++ != *str++)
  284. return false;
  285. return true;
  286. };
  287. // serial_debug will output all the klog() and dbg() data to COM1 at
  288. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  289. // process on live hardware.
  290. //
  291. // note: it must be the first option in the boot cmdline.
  292. u32 cmdline = low_physical_to_virtual(multiboot_info_ptr->cmdline);
  293. if (cmdline && bad_prefix_check(reinterpret_cast<const char*>(cmdline), "serial_debug"))
  294. set_serial_debug(true);
  295. }
  296. extern "C" {
  297. multiboot_info_t* multiboot_info_ptr;
  298. }
  299. // Define some Itanium C++ ABI methods to stop the linker from complaining
  300. // If we actually call these something has gone horribly wrong
  301. void* __dso_handle __attribute__((visibility("hidden")));
  302. extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
  303. {
  304. ASSERT_NOT_REACHED();
  305. return 0;
  306. }
  307. }