init.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/DMIDecoder.h>
  29. #include <Kernel/ACPI/Initialize.h>
  30. #include <Kernel/ACPI/MultiProcessorParser.h>
  31. #include <Kernel/Arch/i386/CPU.h>
  32. #include <Kernel/CMOS.h>
  33. #include <Kernel/CommandLine.h>
  34. #include <Kernel/Devices/BXVGADevice.h>
  35. #include <Kernel/Devices/DiskPartition.h>
  36. #include <Kernel/Devices/EBRPartitionTable.h>
  37. #include <Kernel/Devices/FullDevice.h>
  38. #include <Kernel/Devices/GPTPartitionTable.h>
  39. #include <Kernel/Devices/KeyboardDevice.h>
  40. #include <Kernel/Devices/MBRPartitionTable.h>
  41. #include <Kernel/Devices/MBVGADevice.h>
  42. #include <Kernel/Devices/NullDevice.h>
  43. #include <Kernel/Devices/PATAChannel.h>
  44. #include <Kernel/Devices/PATADiskDevice.h>
  45. #include <Kernel/Devices/PS2MouseDevice.h>
  46. #include <Kernel/Devices/RandomDevice.h>
  47. #include <Kernel/Devices/SB16.h>
  48. #include <Kernel/Devices/SerialDevice.h>
  49. #include <Kernel/Devices/VMWareBackdoor.h>
  50. #include <Kernel/Devices/ZeroDevice.h>
  51. #include <Kernel/FileSystem/Ext2FileSystem.h>
  52. #include <Kernel/FileSystem/VirtualFileSystem.h>
  53. #include <Kernel/Heap/SlabAllocator.h>
  54. #include <Kernel/Heap/kmalloc.h>
  55. #include <Kernel/Interrupts/APIC.h>
  56. #include <Kernel/Interrupts/InterruptManagement.h>
  57. #include <Kernel/Interrupts/PIC.h>
  58. #include <Kernel/KSyms.h>
  59. #include <Kernel/Multiboot.h>
  60. #include <Kernel/Net/LoopbackAdapter.h>
  61. #include <Kernel/Net/NetworkTask.h>
  62. #include <Kernel/PCI/Access.h>
  63. #include <Kernel/PCI/Initializer.h>
  64. #include <Kernel/Process.h>
  65. #include <Kernel/RTC.h>
  66. #include <Kernel/Random.h>
  67. #include <Kernel/Scheduler.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. // Defined in the linker script
  75. typedef void (*ctor_func_t)();
  76. extern ctor_func_t start_ctors;
  77. extern ctor_func_t end_ctors;
  78. extern u32 __stack_chk_guard;
  79. u32 __stack_chk_guard;
  80. namespace Kernel {
  81. [[noreturn]] static void init_stage2();
  82. static void setup_serial_debug();
  83. static void setup_time_management();
  84. VirtualConsole* tty0;
  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" [[noreturn]] void init()
  95. {
  96. setup_serial_debug();
  97. cpu_setup();
  98. kmalloc_init();
  99. slab_alloc_init();
  100. CommandLine::initialize(reinterpret_cast<const char*>(low_physical_to_virtual(multiboot_info_ptr->cmdline)));
  101. MemoryManager::initialize();
  102. gdt_init();
  103. idt_init();
  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. InterruptManagement::initialize();
  109. ACPI::initialize();
  110. new VFS;
  111. new KeyboardDevice;
  112. new PS2MouseDevice;
  113. new Console;
  114. klog() << "Starting SerenityOS...";
  115. __stack_chk_guard = get_good_random<u32>();
  116. setup_time_management();
  117. new NullDevice;
  118. if (!get_serial_debug())
  119. new SerialDevice(SERIAL_COM1_ADDR, 64);
  120. new SerialDevice(SERIAL_COM2_ADDR, 65);
  121. new SerialDevice(SERIAL_COM3_ADDR, 66);
  122. new SerialDevice(SERIAL_COM4_ADDR, 67);
  123. VirtualConsole::initialize();
  124. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  125. new VirtualConsole(1);
  126. VirtualConsole::switch_to(0);
  127. Process::initialize();
  128. Thread::initialize();
  129. Thread* init_stage2_thread = nullptr;
  130. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2);
  131. Scheduler::pick_next();
  132. sti();
  133. Scheduler::idle_loop();
  134. ASSERT_NOT_REACHED();
  135. }
  136. void init_stage2()
  137. {
  138. SyncTask::spawn();
  139. FinalizerTask::spawn();
  140. PCI::initialize();
  141. if (kernel_command_line().contains("text_debug")) {
  142. dbg() << "Text mode enabled";
  143. } else {
  144. if (multiboot_info_ptr->framebuffer_type == 1 || multiboot_info_ptr->framebuffer_type == 2) {
  145. new MBVGADevice(
  146. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  147. multiboot_info_ptr->framebuffer_pitch,
  148. multiboot_info_ptr->framebuffer_width,
  149. multiboot_info_ptr->framebuffer_height);
  150. } else {
  151. new BXVGADevice;
  152. }
  153. }
  154. LoopbackAdapter::the();
  155. Syscall::initialize();
  156. new ZeroDevice;
  157. new FullDevice;
  158. new RandomDevice;
  159. new PTYMultiplexer;
  160. new SB16;
  161. VMWareBackdoor::initialize();
  162. bool dmi_unreliable = kernel_command_line().contains("dmi_unreliable");
  163. if (dmi_unreliable) {
  164. DMIDecoder::initialize_untrusted();
  165. } else {
  166. DMIDecoder::initialize();
  167. }
  168. bool text_debug = kernel_command_line().contains("text_debug");
  169. bool force_pio = kernel_command_line().contains("force_pio");
  170. auto root = kernel_command_line().get("root");
  171. if (root.is_empty()) {
  172. root = "/dev/hda";
  173. }
  174. if (!root.starts_with("/dev/hda")) {
  175. klog() << "init_stage2: root filesystem must be on the first IDE hard drive (/dev/hda)";
  176. hang();
  177. }
  178. auto pata0 = PATAChannel::create(PATAChannel::ChannelType::Primary, force_pio);
  179. NonnullRefPtr<BlockDevice> root_dev = *pata0->master_device();
  180. root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
  181. if (root.length()) {
  182. bool ok;
  183. unsigned partition_number = root.to_uint(ok);
  184. if (!ok) {
  185. klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
  186. hang();
  187. }
  188. MBRPartitionTable mbr(root_dev);
  189. if (!mbr.initialize()) {
  190. klog() << "init_stage2: couldn't read MBR from disk";
  191. hang();
  192. }
  193. if (mbr.is_protective_mbr()) {
  194. dbg() << "GPT Partitioned Storage Detected!";
  195. GPTPartitionTable gpt(root_dev);
  196. if (!gpt.initialize()) {
  197. klog() << "init_stage2: couldn't read GPT from disk";
  198. hang();
  199. }
  200. auto partition = gpt.partition(partition_number);
  201. if (!partition) {
  202. klog() << "init_stage2: couldn't get partition " << partition_number;
  203. hang();
  204. }
  205. root_dev = *partition;
  206. } else {
  207. dbg() << "MBR Partitioned Storage Detected!";
  208. if (mbr.contains_ebr()) {
  209. EBRPartitionTable ebr(root_dev);
  210. if (!ebr.initialize()) {
  211. klog() << "init_stage2: couldn't read EBR from disk";
  212. hang();
  213. }
  214. auto partition = ebr.partition(partition_number);
  215. if (!partition) {
  216. klog() << "init_stage2: couldn't get partition " << partition_number;
  217. hang();
  218. }
  219. root_dev = *partition;
  220. } else {
  221. if (partition_number < 1 || partition_number > 4) {
  222. klog() << "init_stage2: invalid partition number " << partition_number << "; expected 1 to 4";
  223. hang();
  224. }
  225. auto partition = mbr.partition(partition_number);
  226. if (!partition) {
  227. klog() << "init_stage2: couldn't get partition " << partition_number;
  228. hang();
  229. }
  230. root_dev = *partition;
  231. }
  232. }
  233. }
  234. auto e2fs = Ext2FS::create(*FileDescription::create(root_dev));
  235. if (!e2fs->initialize()) {
  236. klog() << "init_stage2: couldn't open root filesystem";
  237. hang();
  238. }
  239. if (!VFS::the().mount_root(e2fs)) {
  240. klog() << "VFS::mount_root failed";
  241. hang();
  242. }
  243. Process::current->set_root_directory(VFS::the().root_custody());
  244. load_kernel_symbol_table();
  245. int error;
  246. // SystemServer will start WindowServer, which will be doing graphics.
  247. // From this point on we don't want to touch the VGA text terminal or
  248. // accept keyboard input.
  249. if (text_debug) {
  250. tty0->set_graphical(false);
  251. Thread* thread = nullptr;
  252. Process::create_user_process(thread, "/bin/Shell", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  253. if (error != 0) {
  254. klog() << "init_stage2: error spawning Shell: " << error;
  255. hang();
  256. }
  257. thread->set_priority(THREAD_PRIORITY_HIGH);
  258. } else {
  259. tty0->set_graphical(true);
  260. Thread* thread = nullptr;
  261. Process::create_user_process(thread, "/bin/SystemServer", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  262. if (error != 0) {
  263. klog() << "init_stage2: error spawning SystemServer: " << error;
  264. hang();
  265. }
  266. thread->set_priority(THREAD_PRIORITY_HIGH);
  267. }
  268. NetworkTask::spawn();
  269. Process::current->sys$exit(0);
  270. ASSERT_NOT_REACHED();
  271. }
  272. void setup_serial_debug()
  273. {
  274. // this is only used one time, directly below here. we can't use this part
  275. // of libc at this point in the boot process, or we'd just pull strstr in
  276. // from <string.h>.
  277. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  278. while (*search)
  279. if (*search++ != *str++)
  280. return false;
  281. return true;
  282. };
  283. // serial_debug will output all the klog() and dbg() data to COM1 at
  284. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  285. // process on live hardware.
  286. //
  287. // note: it must be the first option in the boot cmdline.
  288. u32 cmdline = low_physical_to_virtual(multiboot_info_ptr->cmdline);
  289. if (cmdline && bad_prefix_check(reinterpret_cast<const char*>(cmdline), "serial_debug"))
  290. set_serial_debug(true);
  291. }
  292. extern "C" {
  293. multiboot_info_t* multiboot_info_ptr;
  294. }
  295. // Define some Itanium C++ ABI methods to stop the linker from complaining
  296. // If we actually call these something has gone horribly wrong
  297. void* __dso_handle __attribute__((visibility("hidden")));
  298. extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
  299. {
  300. ASSERT_NOT_REACHED();
  301. return 0;
  302. }
  303. void setup_time_management()
  304. {
  305. if (!kernel_command_line().contains("time")) {
  306. TimeManagement::initialize(true);
  307. return;
  308. }
  309. auto time = kernel_command_line().get("time");
  310. if (time == "legacy") {
  311. TimeManagement::initialize(false);
  312. return;
  313. }
  314. if (time == "modern") {
  315. TimeManagement::initialize(true);
  316. return;
  317. }
  318. kprintf("time boot argmuent has an invalid value.\n");
  319. hang();
  320. }
  321. }