init.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. vfs->mount_root(e2fs);
  123. dbgprintf("Load ksyms\n");
  124. load_ksyms();
  125. dbgprintf("Loaded ksyms\n");
  126. // Now, detect whether or not there are actually any floppy disks attached to the system
  127. u8 detect = CMOS::read(0x10);
  128. RefPtr<FloppyDiskDevice> fd0;
  129. RefPtr<FloppyDiskDevice> fd1;
  130. if ((detect >> 4) & 0x4) {
  131. fd0 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Master);
  132. kprintf("fd0 is 1.44MB floppy drive\n");
  133. } else {
  134. kprintf("fd0 type unsupported! Type == 0x%x\n", detect >> 4);
  135. }
  136. if (detect & 0x0f) {
  137. fd1 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Slave);
  138. kprintf("fd1 is 1.44MB floppy drive");
  139. } else {
  140. kprintf("fd1 type unsupported! Type == 0x%x\n", detect & 0x0f);
  141. }
  142. int error;
  143. // SystemServer will start WindowServer, which will be doing graphics.
  144. // From this point on we don't want to touch the VGA text terminal or
  145. // accept keyboard input.
  146. if (text_debug) {
  147. tty0->set_graphical(false);
  148. auto* shell_process = Process::create_user_process("/bin/Shell", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  149. if (error != 0) {
  150. kprintf("init_stage2: error spawning Shell: %d\n", error);
  151. hang();
  152. }
  153. shell_process->main_thread().set_priority(ThreadPriority::High);
  154. } else {
  155. tty0->set_graphical(true);
  156. auto* system_server_process = Process::create_user_process("/bin/SystemServer", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  157. if (error != 0) {
  158. kprintf("init_stage2: error spawning SystemServer: %d\n", error);
  159. hang();
  160. }
  161. system_server_process->main_thread().set_priority(ThreadPriority::High);
  162. }
  163. Process::create_kernel_process("NetworkTask", NetworkTask_main);
  164. current->process().sys$exit(0);
  165. ASSERT_NOT_REACHED();
  166. }
  167. extern "C" {
  168. multiboot_info_t* multiboot_info_ptr;
  169. }
  170. typedef void (*ctor_func_t)();
  171. // Defined in the linker script
  172. extern ctor_func_t start_ctors;
  173. extern ctor_func_t end_ctors;
  174. // Define some Itanium C++ ABI methods to stop the linker from complaining
  175. // If we actually call these something has gone horribly wrong
  176. void* __dso_handle __attribute__((visibility ("hidden")));
  177. extern "C" int __cxa_atexit ( void (*)(void *), void *, void *)
  178. {
  179. ASSERT_NOT_REACHED();
  180. return 0;
  181. }
  182. extern "C" [[noreturn]] void init(u32 physical_address_for_kernel_page_tables)
  183. {
  184. // this is only used one time, directly below here. we can't use this part
  185. // of libc at this point in the boot process, or we'd just pull strstr in
  186. // from <string.h>.
  187. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  188. while (*search)
  189. if (*search++ != *str++)
  190. return false;
  191. return true;
  192. };
  193. // serial_debug will output all the kprintf and dbgprintf data to COM1 at
  194. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  195. // process on live hardware.
  196. //
  197. // note: it must be the first option in the boot cmdline.
  198. if (multiboot_info_ptr->cmdline && bad_prefix_check(reinterpret_cast<const char*>(multiboot_info_ptr->cmdline), "serial_debug"))
  199. set_serial_debug(true);
  200. sse_init();
  201. kmalloc_init();
  202. slab_alloc_init();
  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 text_debug = KParams::the().has("text_debug");
  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(physical_address_for_kernel_page_tables);
  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 (text_debug) {
  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. Process::initialize();
  259. Thread::initialize();
  260. Process::create_kernel_process("init_stage2", init_stage2);
  261. Process::create_kernel_process("syncd", [] {
  262. for (;;) {
  263. VFS::the().sync();
  264. current->sleep(1 * TICKS_PER_SECOND);
  265. }
  266. });
  267. Process::create_kernel_process("Finalizer", [] {
  268. g_finalizer = current;
  269. current->set_priority(ThreadPriority::Low);
  270. for (;;) {
  271. Thread::finalize_dying_threads();
  272. (void)current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Lurking);
  273. }
  274. });
  275. Scheduler::pick_next();
  276. sti();
  277. Scheduler::idle_loop();
  278. ASSERT_NOT_REACHED();
  279. }