init.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 "Devices/PATADiskDevice.h"
  27. #include "KSyms.h"
  28. #include "Process.h"
  29. #include "RTC.h"
  30. #include "Scheduler.h"
  31. #include <AK/Types.h>
  32. #include <Kernel/ACPI/ACPIDynamicParser.h>
  33. #include <Kernel/ACPI/ACPIStaticParser.h>
  34. #include <Kernel/ACPI/DMIDecoder.h>
  35. #include <Kernel/ACPI/MultiProcessorParser.h>
  36. #include <Kernel/Arch/i386/CPU.h>
  37. #include <Kernel/CMOS.h>
  38. #include <Kernel/Devices/BXVGADevice.h>
  39. #include <Kernel/Devices/DebugLogDevice.h>
  40. #include <Kernel/Devices/DiskPartition.h>
  41. #include <Kernel/Devices/EBRPartitionTable.h>
  42. #include <Kernel/Devices/FloppyDiskDevice.h>
  43. #include <Kernel/Devices/FullDevice.h>
  44. #include <Kernel/Devices/GPTPartitionTable.h>
  45. #include <Kernel/Devices/KeyboardDevice.h>
  46. #include <Kernel/Devices/MBRPartitionTable.h>
  47. #include <Kernel/Devices/MBVGADevice.h>
  48. #include <Kernel/Devices/NullDevice.h>
  49. #include <Kernel/Devices/PATAChannel.h>
  50. #include <Kernel/Devices/PIT.h>
  51. #include <Kernel/Devices/PS2MouseDevice.h>
  52. #include <Kernel/Devices/RandomDevice.h>
  53. #include <Kernel/Devices/SB16.h>
  54. #include <Kernel/Devices/SerialDevice.h>
  55. #include <Kernel/Devices/VMWareBackdoor.h>
  56. #include <Kernel/Devices/ZeroDevice.h>
  57. #include <Kernel/FileSystem/Ext2FileSystem.h>
  58. #include <Kernel/FileSystem/VirtualFileSystem.h>
  59. #include <Kernel/Heap/SlabAllocator.h>
  60. #include <Kernel/Heap/kmalloc.h>
  61. #include <Kernel/Interrupts/APIC.h>
  62. #include <Kernel/Interrupts/InterruptManagement.h>
  63. #include <Kernel/Interrupts/PIC.h>
  64. #include <Kernel/KParams.h>
  65. #include <Kernel/Multiboot.h>
  66. #include <Kernel/Net/LoopbackAdapter.h>
  67. #include <Kernel/Net/NetworkTask.h>
  68. #include <Kernel/PCI/Access.h>
  69. #include <Kernel/PCI/Initializer.h>
  70. #include <Kernel/Random.h>
  71. #include <Kernel/TTY/PTYMultiplexer.h>
  72. #include <Kernel/TTY/VirtualConsole.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_acpi();
  84. static void setup_vmmouse();
  85. static void setup_pci();
  86. static void setup_interrupts();
  87. VirtualConsole* tty0;
  88. extern "C" [[noreturn]] void init()
  89. {
  90. setup_serial_debug();
  91. cpu_setup();
  92. kmalloc_init();
  93. slab_alloc_init();
  94. new KParams(String(reinterpret_cast<const char*>(low_physical_to_virtual(multiboot_info_ptr->cmdline))));
  95. MemoryManager::initialize();
  96. bool text_debug = KParams::the().has("text_debug");
  97. gdt_init();
  98. idt_init();
  99. setup_interrupts();
  100. setup_acpi();
  101. new VFS;
  102. new DebugLogDevice;
  103. new Console;
  104. klog() << "Starting SerenityOS...";
  105. __stack_chk_guard = get_good_random<u32>();
  106. PIT::initialize();
  107. RTC::initialize();
  108. // call global constructors after gtd and itd init
  109. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  110. (*ctor)();
  111. new KeyboardDevice;
  112. new PS2MouseDevice;
  113. setup_vmmouse();
  114. new SB16;
  115. new NullDevice;
  116. if (!get_serial_debug())
  117. new SerialDevice(SERIAL_COM1_ADDR, 64);
  118. new SerialDevice(SERIAL_COM2_ADDR, 65);
  119. new SerialDevice(SERIAL_COM3_ADDR, 66);
  120. new SerialDevice(SERIAL_COM4_ADDR, 67);
  121. VirtualConsole::initialize();
  122. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  123. new VirtualConsole(1);
  124. VirtualConsole::switch_to(0);
  125. // Sample test to see if the ACPI parser is working...
  126. klog() << "ACPI: HPET table @ " << ACPI::Parser::the().find_table("HPET");
  127. setup_pci();
  128. PIT::initialize();
  129. if (text_debug) {
  130. dbg() << "Text mode enabled";
  131. } else {
  132. if (multiboot_info_ptr->framebuffer_type == 1 || multiboot_info_ptr->framebuffer_type == 2) {
  133. new MBVGADevice(
  134. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  135. multiboot_info_ptr->framebuffer_pitch,
  136. multiboot_info_ptr->framebuffer_width,
  137. multiboot_info_ptr->framebuffer_height);
  138. } else {
  139. new BXVGADevice;
  140. }
  141. }
  142. LoopbackAdapter::the();
  143. Process::initialize();
  144. Thread::initialize();
  145. Thread* init_stage2_thread = nullptr;
  146. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2);
  147. Thread* syncd_thread = nullptr;
  148. Process::create_kernel_process(syncd_thread, "syncd", [] {
  149. for (;;) {
  150. VFS::the().sync();
  151. Thread::current->sleep(1 * TICKS_PER_SECOND);
  152. }
  153. });
  154. Process::create_kernel_process(g_finalizer, "Finalizer", [] {
  155. Thread::current->set_priority(THREAD_PRIORITY_LOW);
  156. for (;;) {
  157. {
  158. InterruptDisabler disabler;
  159. if (!g_finalizer_has_work)
  160. Thread::current->wait_on(*g_finalizer_wait_queue);
  161. ASSERT(g_finalizer_has_work);
  162. g_finalizer_has_work = false;
  163. }
  164. Thread::finalize_dying_threads();
  165. }
  166. });
  167. Scheduler::pick_next();
  168. sti();
  169. Scheduler::idle_loop();
  170. ASSERT_NOT_REACHED();
  171. }
  172. void init_stage2()
  173. {
  174. Syscall::initialize();
  175. new ZeroDevice;
  176. new FullDevice;
  177. new RandomDevice;
  178. new PTYMultiplexer;
  179. bool dmi_unreliable = KParams::the().has("dmi_unreliable");
  180. if (dmi_unreliable) {
  181. DMIDecoder::initialize_untrusted();
  182. } else {
  183. DMIDecoder::initialize();
  184. }
  185. bool text_debug = KParams::the().has("text_debug");
  186. bool force_pio = KParams::the().has("force_pio");
  187. auto root = KParams::the().get("root");
  188. if (root.is_empty()) {
  189. root = "/dev/hda";
  190. }
  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. bool ok;
  200. unsigned partition_number = root.to_uint(ok);
  201. if (!ok) {
  202. klog() << "init_stage2: couldn't parse partition number from root kernel parameter";
  203. hang();
  204. }
  205. MBRPartitionTable mbr(root_dev);
  206. if (!mbr.initialize()) {
  207. klog() << "init_stage2: couldn't read MBR from disk";
  208. hang();
  209. }
  210. if (mbr.is_protective_mbr()) {
  211. dbg() << "GPT Partitioned Storage Detected!";
  212. GPTPartitionTable gpt(root_dev);
  213. if (!gpt.initialize()) {
  214. klog() << "init_stage2: couldn't read GPT from disk";
  215. hang();
  216. }
  217. auto partition = gpt.partition(partition_number);
  218. if (!partition) {
  219. klog() << "init_stage2: couldn't get partition " << partition_number;
  220. hang();
  221. }
  222. root_dev = *partition;
  223. } else {
  224. dbg() << "MBR Partitioned Storage Detected!";
  225. if (mbr.contains_ebr()) {
  226. EBRPartitionTable ebr(root_dev);
  227. if (!ebr.initialize()) {
  228. klog() << "init_stage2: couldn't read EBR from disk";
  229. hang();
  230. }
  231. auto partition = ebr.partition(partition_number);
  232. if (!partition) {
  233. klog() << "init_stage2: couldn't get partition " << partition_number;
  234. hang();
  235. }
  236. root_dev = *partition;
  237. } else {
  238. if (partition_number < 1 || partition_number > 4) {
  239. klog() << "init_stage2: invalid partition number " << partition_number << "; expected 1 to 4";
  240. hang();
  241. }
  242. auto partition = mbr.partition(partition_number);
  243. if (!partition) {
  244. klog() << "init_stage2: couldn't get partition " << partition_number;
  245. hang();
  246. }
  247. root_dev = *partition;
  248. }
  249. }
  250. }
  251. auto e2fs = Ext2FS::create(root_dev);
  252. if (!e2fs->initialize()) {
  253. klog() << "init_stage2: couldn't open root filesystem";
  254. hang();
  255. }
  256. if (!VFS::the().mount_root(e2fs)) {
  257. klog() << "VFS::mount_root failed";
  258. hang();
  259. }
  260. Process::current->set_root_directory(VFS::the().root_custody());
  261. dbg() << "Load ksyms";
  262. load_ksyms();
  263. dbg() << "Loaded ksyms";
  264. // Now, detect whether or not there are actually any floppy disks attached to the system
  265. u8 detect = CMOS::read(0x10);
  266. RefPtr<FloppyDiskDevice> fd0;
  267. RefPtr<FloppyDiskDevice> fd1;
  268. if ((detect >> 4) & 0x4) {
  269. fd0 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Master);
  270. klog() << "fd0 is 1.44MB floppy drive";
  271. } else {
  272. klog() << "fd0 type unsupported! Type == 0x", String::format("%x", detect >> 4);
  273. }
  274. if (detect & 0x0f) {
  275. fd1 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Slave);
  276. klog() << "fd1 is 1.44MB floppy drive";
  277. } else {
  278. klog() << "fd1 type unsupported! Type == 0x", String::format("%x", detect & 0x0f);
  279. }
  280. int error;
  281. // SystemServer will start WindowServer, which will be doing graphics.
  282. // From this point on we don't want to touch the VGA text terminal or
  283. // accept keyboard input.
  284. if (text_debug) {
  285. tty0->set_graphical(false);
  286. Thread* thread = nullptr;
  287. Process::create_user_process(thread, "/bin/Shell", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  288. if (error != 0) {
  289. klog() << "init_stage2: error spawning Shell: " << error;
  290. hang();
  291. }
  292. thread->set_priority(THREAD_PRIORITY_HIGH);
  293. } else {
  294. tty0->set_graphical(true);
  295. Thread* thread = nullptr;
  296. Process::create_user_process(thread, "/bin/SystemServer", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  297. if (error != 0) {
  298. klog() << "init_stage2: error spawning SystemServer: " << error;
  299. hang();
  300. }
  301. thread->set_priority(THREAD_PRIORITY_HIGH);
  302. }
  303. {
  304. Thread* thread = nullptr;
  305. Process::create_kernel_process(thread, "NetworkTask", NetworkTask_main);
  306. }
  307. Process::current->sys$exit(0);
  308. ASSERT_NOT_REACHED();
  309. }
  310. void setup_serial_debug()
  311. {
  312. // this is only used one time, directly below here. we can't use this part
  313. // of libc at this point in the boot process, or we'd just pull strstr in
  314. // from <string.h>.
  315. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  316. while (*search)
  317. if (*search++ != *str++)
  318. return false;
  319. return true;
  320. };
  321. // serial_debug will output all the klog() and dbg() data to COM1 at
  322. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  323. // process on live hardware.
  324. //
  325. // note: it must be the first option in the boot cmdline.
  326. u32 cmdline = low_physical_to_virtual(multiboot_info_ptr->cmdline);
  327. if (cmdline && bad_prefix_check(reinterpret_cast<const char*>(cmdline), "serial_debug"))
  328. set_serial_debug(true);
  329. }
  330. extern "C" {
  331. multiboot_info_t* multiboot_info_ptr;
  332. }
  333. // Define some Itanium C++ ABI methods to stop the linker from complaining
  334. // If we actually call these something has gone horribly wrong
  335. void* __dso_handle __attribute__((visibility("hidden")));
  336. extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
  337. {
  338. ASSERT_NOT_REACHED();
  339. return 0;
  340. }
  341. void setup_acpi()
  342. {
  343. if (!KParams::the().has("acpi")) {
  344. ACPI::DynamicParser::initialize_without_rsdp();
  345. return;
  346. }
  347. auto acpi = KParams::the().get("acpi");
  348. if (acpi == "off") {
  349. ACPI::Parser::initialize_limited();
  350. return;
  351. }
  352. if (acpi == "on") {
  353. ACPI::DynamicParser::initialize_without_rsdp();
  354. return;
  355. }
  356. if (acpi == "limited") {
  357. ACPI::StaticParser::initialize_without_rsdp();
  358. return;
  359. }
  360. klog() << "acpi boot argmuent has an invalid value.";
  361. hang();
  362. }
  363. void setup_vmmouse()
  364. {
  365. VMWareBackdoor::initialize();
  366. if (!KParams::the().has("vmmouse")) {
  367. VMWareBackdoor::the().enable_absolute_vmmouse();
  368. return;
  369. }
  370. auto vmmouse = KParams::the().get("vmmouse");
  371. if (vmmouse == "off")
  372. return;
  373. if (vmmouse == "on") {
  374. VMWareBackdoor::the().enable_absolute_vmmouse();
  375. return;
  376. }
  377. klog() << "vmmouse boot argmuent has an invalid value.";
  378. hang();
  379. }
  380. void setup_pci()
  381. {
  382. if (!KParams::the().has("pci_mmio")) {
  383. PCI::Initializer::the().test_and_initialize(false);
  384. PCI::Initializer::the().dismiss();
  385. return;
  386. }
  387. auto pci_mmio = KParams::the().get("pci_mmio");
  388. if (pci_mmio == "on") {
  389. PCI::Initializer::the().test_and_initialize(false);
  390. } else if (pci_mmio == "off") {
  391. PCI::Initializer::the().test_and_initialize(true);
  392. } else {
  393. klog() << "pci_mmio boot argmuent has an invalid value.";
  394. hang();
  395. }
  396. PCI::Initializer::the().dismiss();
  397. }
  398. void setup_interrupts()
  399. {
  400. InterruptManagement::initialize();
  401. if (!KParams::the().has("smp")) {
  402. InterruptManagement::the().switch_to_pic_mode();
  403. return;
  404. }
  405. auto smp = KParams::the().get("smp");
  406. if (smp == "off") {
  407. InterruptManagement::the().switch_to_pic_mode();
  408. return;
  409. }
  410. if (smp == "on") {
  411. ASSERT_NOT_REACHED(); // FIXME: The IOAPIC mode is not stable yet so we can't use it now.
  412. InterruptManagement::the().switch_to_ioapic_mode();
  413. APIC::init();
  414. APIC::enable_bsp();
  415. return;
  416. }
  417. klog() << "smp boot argmuent has an invalid value.";
  418. hang();
  419. }
  420. }