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