init.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "Devices/PATADiskDevice.h"
  2. #include "KSyms.h"
  3. #include "Process.h"
  4. #include "RTC.h"
  5. #include "Scheduler.h"
  6. #include "kstdio.h"
  7. #include <AK/Types.h>
  8. #include <Kernel/ACPI/ACPIDynamicParser.h>
  9. #include <Kernel/ACPI/ACPIStaticParser.h>
  10. #include <Kernel/ACPI/DMIDecoder.h>
  11. #include <Kernel/Arch/i386/APIC.h>
  12. #include <Kernel/Arch/i386/CPU.h>
  13. #include <Kernel/Arch/i386/PIC.h>
  14. #include <Kernel/Arch/i386/PIT.h>
  15. #include <Kernel/CMOS.h>
  16. #include <Kernel/Devices/BXVGADevice.h>
  17. #include <Kernel/Devices/DebugLogDevice.h>
  18. #include <Kernel/Devices/DiskPartition.h>
  19. #include <Kernel/Devices/FloppyDiskDevice.h>
  20. #include <Kernel/Devices/FullDevice.h>
  21. #include <Kernel/Devices/GPTPartitionTable.h>
  22. #include <Kernel/Devices/KeyboardDevice.h>
  23. #include <Kernel/Devices/MBRPartitionTable.h>
  24. #include <Kernel/Devices/MBVGADevice.h>
  25. #include <Kernel/Devices/NullDevice.h>
  26. #include <Kernel/Devices/PATAChannel.h>
  27. #include <Kernel/Devices/PS2MouseDevice.h>
  28. #include <Kernel/Devices/RandomDevice.h>
  29. #include <Kernel/Devices/SB16.h>
  30. #include <Kernel/Devices/SerialDevice.h>
  31. #include <Kernel/Devices/ZeroDevice.h>
  32. #include <Kernel/FileSystem/Ext2FileSystem.h>
  33. #include <Kernel/FileSystem/VirtualFileSystem.h>
  34. #include <Kernel/Heap/SlabAllocator.h>
  35. #include <Kernel/Heap/kmalloc.h>
  36. #include <Kernel/KParams.h>
  37. #include <Kernel/Multiboot.h>
  38. #include <Kernel/Net/E1000NetworkAdapter.h>
  39. #include <Kernel/Net/LoopbackAdapter.h>
  40. #include <Kernel/Net/NetworkTask.h>
  41. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  42. #include <Kernel/PCI/Access.h>
  43. #include <Kernel/PCI/Initializer.h>
  44. #include <Kernel/Random.h>
  45. #include <Kernel/TTY/PTYMultiplexer.h>
  46. #include <Kernel/TTY/VirtualConsole.h>
  47. #include <Kernel/VM/MemoryManager.h>
  48. VirtualConsole* tty0;
  49. VirtualConsole* tty1;
  50. KeyboardDevice* keyboard;
  51. PS2MouseDevice* ps2mouse;
  52. SB16* sb16;
  53. DebugLogDevice* dev_debuglog;
  54. NullDevice* dev_null;
  55. SerialDevice* ttyS0;
  56. SerialDevice* ttyS1;
  57. SerialDevice* ttyS2;
  58. SerialDevice* ttyS3;
  59. VFS* vfs;
  60. [[noreturn]] static void init_stage2()
  61. {
  62. Syscall::initialize();
  63. auto dev_zero = make<ZeroDevice>();
  64. auto dev_full = make<FullDevice>();
  65. auto dev_random = make<RandomDevice>();
  66. auto dev_ptmx = make<PTYMultiplexer>();
  67. bool text_debug = KParams::the().has("text_debug");
  68. bool force_pio = KParams::the().has("force_pio");
  69. auto root = KParams::the().get("root");
  70. if (root.is_empty()) {
  71. root = "/dev/hda";
  72. }
  73. bool dmi_unreliable = KParams::the().has("dmi_unreliable");
  74. if (dmi_unreliable) {
  75. DMIDecoder::initialize_untrusted();
  76. } else {
  77. DMIDecoder::initialize();
  78. }
  79. if (!root.starts_with("/dev/hda")) {
  80. kprintf("init_stage2: root filesystem must be on the first IDE hard drive (/dev/hda)\n");
  81. hang();
  82. }
  83. auto pata0 = PATAChannel::create(PATAChannel::ChannelType::Primary, force_pio);
  84. NonnullRefPtr<DiskDevice> root_dev = *pata0->master_device();
  85. root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
  86. if (root.length()) {
  87. bool ok;
  88. unsigned partition_number = root.to_uint(ok);
  89. if (!ok) {
  90. kprintf("init_stage2: couldn't parse partition number from root kernel parameter\n");
  91. hang();
  92. }
  93. if (partition_number < 1 || partition_number > 4) {
  94. kprintf("init_stage2: invalid partition number %d; expected 1 to 4\n", partition_number);
  95. hang();
  96. }
  97. MBRPartitionTable mbr(root_dev);
  98. if (!mbr.initialize()) {
  99. kprintf("init_stage2: couldn't read MBR from disk\n");
  100. hang();
  101. }
  102. if (mbr.is_protective_mbr()) {
  103. dbgprintf("GPT Partitioned Storage Detected!\n");
  104. GPTPartitionTable gpt(root_dev);
  105. if (!gpt.initialize()) {
  106. kprintf("init_stage2: couldn't read GPT from disk\n");
  107. hang();
  108. }
  109. auto partition = gpt.partition(partition_number);
  110. if (!partition) {
  111. kprintf("init_stage2: couldn't get partition %d\n", partition_number);
  112. hang();
  113. }
  114. root_dev = *partition;
  115. } else {
  116. dbgprintf("MBR Partitioned Storage Detected!\n");
  117. auto partition = mbr.partition(partition_number);
  118. if (!partition) {
  119. kprintf("init_stage2: couldn't get partition %d\n", partition_number);
  120. hang();
  121. }
  122. root_dev = *partition;
  123. }
  124. }
  125. auto e2fs = Ext2FS::create(root_dev);
  126. if (!e2fs->initialize()) {
  127. kprintf("init_stage2: couldn't open root filesystem\n");
  128. hang();
  129. }
  130. if (!vfs->mount_root(e2fs)) {
  131. kprintf("VFS::mount_root failed\n");
  132. hang();
  133. }
  134. current->process().set_root_directory(vfs->root_custody());
  135. dbgprintf("Load ksyms\n");
  136. load_ksyms();
  137. dbgprintf("Loaded ksyms\n");
  138. // Now, detect whether or not there are actually any floppy disks attached to the system
  139. u8 detect = CMOS::read(0x10);
  140. RefPtr<FloppyDiskDevice> fd0;
  141. RefPtr<FloppyDiskDevice> fd1;
  142. if ((detect >> 4) & 0x4) {
  143. fd0 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Master);
  144. kprintf("fd0 is 1.44MB floppy drive\n");
  145. } else {
  146. kprintf("fd0 type unsupported! Type == 0x%x\n", detect >> 4);
  147. }
  148. if (detect & 0x0f) {
  149. fd1 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Slave);
  150. kprintf("fd1 is 1.44MB floppy drive");
  151. } else {
  152. kprintf("fd1 type unsupported! Type == 0x%x\n", detect & 0x0f);
  153. }
  154. int error;
  155. // SystemServer will start WindowServer, which will be doing graphics.
  156. // From this point on we don't want to touch the VGA text terminal or
  157. // accept keyboard input.
  158. if (text_debug) {
  159. tty0->set_graphical(false);
  160. Thread* thread = nullptr;
  161. Process::create_user_process(thread, "/bin/Shell", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  162. if (error != 0) {
  163. kprintf("init_stage2: error spawning Shell: %d\n", error);
  164. hang();
  165. }
  166. thread->set_priority(THREAD_PRIORITY_HIGH);
  167. } else {
  168. tty0->set_graphical(true);
  169. Thread* thread = nullptr;
  170. Process::create_user_process(thread, "/bin/SystemServer", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  171. if (error != 0) {
  172. kprintf("init_stage2: error spawning SystemServer: %d\n", error);
  173. hang();
  174. }
  175. thread->set_priority(THREAD_PRIORITY_HIGH);
  176. }
  177. {
  178. Thread* thread = nullptr;
  179. Process::create_kernel_process(thread, "NetworkTask", NetworkTask_main);
  180. }
  181. current->process().sys$exit(0);
  182. ASSERT_NOT_REACHED();
  183. }
  184. extern "C" {
  185. multiboot_info_t* multiboot_info_ptr;
  186. }
  187. typedef void (*ctor_func_t)();
  188. // Defined in the linker script
  189. extern ctor_func_t start_ctors;
  190. extern ctor_func_t end_ctors;
  191. // Define some Itanium C++ ABI methods to stop the linker from complaining
  192. // If we actually call these something has gone horribly wrong
  193. void* __dso_handle __attribute__((visibility("hidden")));
  194. extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
  195. {
  196. ASSERT_NOT_REACHED();
  197. return 0;
  198. }
  199. extern u32 __stack_chk_guard;
  200. u32 __stack_chk_guard;
  201. extern "C" [[noreturn]] void init()
  202. {
  203. // this is only used one time, directly below here. we can't use this part
  204. // of libc at this point in the boot process, or we'd just pull strstr in
  205. // from <string.h>.
  206. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  207. while (*search)
  208. if (*search++ != *str++)
  209. return false;
  210. return true;
  211. };
  212. // serial_debug will output all the kprintf and dbgprintf data to COM1 at
  213. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  214. // process on live hardware.
  215. //
  216. // note: it must be the first option in the boot cmdline.
  217. u32 cmdline = low_physical_to_virtual(multiboot_info_ptr->cmdline);
  218. if (cmdline && bad_prefix_check(reinterpret_cast<const char*>(cmdline), "serial_debug"))
  219. set_serial_debug(true);
  220. detect_cpu_features();
  221. kmalloc_init();
  222. slab_alloc_init();
  223. // must come after kmalloc_init because we use AK_MAKE_ETERNAL in KParams
  224. new KParams(String(reinterpret_cast<const char*>(cmdline)));
  225. bool text_debug = KParams::the().has("text_debug");
  226. bool complete_acpi_disable = KParams::the().has("noacpi");
  227. bool dynamic_acpi_disable = KParams::the().has("noacpi_aml");
  228. bool pci_mmio_disable = KParams::the().has("nopci_mmio");
  229. complete_acpi_disable = true;
  230. MemoryManager::initialize();
  231. if (complete_acpi_disable) {
  232. ACPIParser::initialize_limited();
  233. } else {
  234. if (!dynamic_acpi_disable) {
  235. ACPIDynamicParser::initialize_without_rsdp();
  236. } else {
  237. ACPIStaticParser::initialize_without_rsdp();
  238. }
  239. }
  240. vfs = new VFS;
  241. dev_debuglog = new DebugLogDevice;
  242. auto console = make<Console>();
  243. kprintf("Starting SerenityOS...\n");
  244. if (g_cpu_supports_sse) {
  245. sse_init();
  246. kprintf("x86: SSE support enabled\n");
  247. }
  248. if (g_cpu_supports_umip) {
  249. asm volatile(
  250. "mov %cr4, %eax\n"
  251. "orl $0x800, %eax\n"
  252. "mov %eax, %cr4\n");
  253. kprintf("x86: UMIP support enabled\n");
  254. }
  255. if (g_cpu_supports_tsc) {
  256. asm volatile(
  257. "mov %cr4, %eax\n"
  258. "orl $0x4, %eax\n"
  259. "mov %eax, %cr4\n");
  260. kprintf("x86: RDTSC support restricted\n");
  261. }
  262. if (g_cpu_supports_rdrand) {
  263. kprintf("x86: Using RDRAND for good randomness\n");
  264. } else {
  265. kprintf("x86: No RDRAND support detected. Randomness will be shitty\n");
  266. }
  267. __stack_chk_guard = get_good_random<u32>();
  268. RTC::initialize();
  269. PIC::initialize();
  270. gdt_init();
  271. idt_init();
  272. // call global constructors after gtd and itd init
  273. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  274. (*ctor)();
  275. keyboard = new KeyboardDevice;
  276. ps2mouse = new PS2MouseDevice;
  277. sb16 = new SB16;
  278. dev_null = new NullDevice;
  279. if (!get_serial_debug())
  280. ttyS0 = new SerialDevice(SERIAL_COM1_ADDR, 64);
  281. ttyS1 = new SerialDevice(SERIAL_COM2_ADDR, 65);
  282. ttyS2 = new SerialDevice(SERIAL_COM3_ADDR, 66);
  283. ttyS3 = new SerialDevice(SERIAL_COM4_ADDR, 67);
  284. VirtualConsole::initialize();
  285. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  286. tty1 = new VirtualConsole(1);
  287. VirtualConsole::switch_to(0);
  288. // Sample test to see if the ACPI parser is working...
  289. kprintf("ACPI: HPET table @ P 0x%x\n", ACPIParser::the().find_table("HPET"));
  290. PCI::Initializer::the().test_and_initialize(pci_mmio_disable);
  291. PCI::Initializer::the().dismiss();
  292. if (APIC::init())
  293. APIC::enable(0);
  294. PIT::initialize();
  295. PCI::enumerate_all([](const PCI::Address& address, PCI::ID id) {
  296. kprintf("PCI: device @ %w:%b:%b.%d [%w:%w]\n",
  297. address.seg(),
  298. address.bus(),
  299. address.slot(),
  300. address.function(),
  301. id.vendor_id,
  302. id.device_id);
  303. });
  304. if (text_debug) {
  305. dbgprintf("Text mode enabled\n");
  306. } else {
  307. if (multiboot_info_ptr->framebuffer_type == 1 || multiboot_info_ptr->framebuffer_type == 2) {
  308. new MBVGADevice(
  309. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  310. multiboot_info_ptr->framebuffer_pitch,
  311. multiboot_info_ptr->framebuffer_width,
  312. multiboot_info_ptr->framebuffer_height);
  313. } else {
  314. new BXVGADevice;
  315. }
  316. }
  317. LoopbackAdapter::the();
  318. auto e1000 = E1000NetworkAdapter::autodetect();
  319. auto rtl8139 = RTL8139NetworkAdapter::autodetect();
  320. Process::initialize();
  321. Thread::initialize();
  322. Thread* init_stage2_thread = nullptr;
  323. Process::create_kernel_process(init_stage2_thread, "init_stage2", init_stage2);
  324. Thread* syncd_thread = nullptr;
  325. Process::create_kernel_process(syncd_thread, "syncd", [] {
  326. for (;;) {
  327. VFS::the().sync();
  328. current->sleep(1 * TICKS_PER_SECOND);
  329. }
  330. });
  331. Process::create_kernel_process(g_finalizer, "Finalizer", [] {
  332. current->set_priority(THREAD_PRIORITY_LOW);
  333. for (;;) {
  334. current->wait_on(*g_finalizer_wait_queue);
  335. Thread::finalize_dying_threads();
  336. }
  337. });
  338. Scheduler::pick_next();
  339. sti();
  340. Scheduler::idle_loop();
  341. ASSERT_NOT_REACHED();
  342. }