init.cpp 11 KB

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