init.cpp 10 KB

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