init.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. auto* shell_process = Process::create_user_process("/bin/Shell", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  152. if (error != 0) {
  153. kprintf("init_stage2: error spawning Shell: %d\n", error);
  154. hang();
  155. }
  156. shell_process->main_thread().set_priority(ThreadPriority::High);
  157. } else {
  158. tty0->set_graphical(true);
  159. auto* system_server_process = Process::create_user_process("/bin/SystemServer", (uid_t)0, (gid_t)0, (pid_t)0, error, {}, {}, tty0);
  160. if (error != 0) {
  161. kprintf("init_stage2: error spawning SystemServer: %d\n", error);
  162. hang();
  163. }
  164. system_server_process->main_thread().set_priority(ThreadPriority::High);
  165. }
  166. Process::create_kernel_process("NetworkTask", NetworkTask_main);
  167. current->process().sys$exit(0);
  168. ASSERT_NOT_REACHED();
  169. }
  170. extern "C" {
  171. multiboot_info_t* multiboot_info_ptr;
  172. }
  173. typedef void (*ctor_func_t)();
  174. // Defined in the linker script
  175. extern ctor_func_t start_ctors;
  176. extern ctor_func_t end_ctors;
  177. // Define some Itanium C++ ABI methods to stop the linker from complaining
  178. // If we actually call these something has gone horribly wrong
  179. void* __dso_handle __attribute__((visibility ("hidden")));
  180. extern "C" int __cxa_atexit ( void (*)(void *), void *, void *)
  181. {
  182. ASSERT_NOT_REACHED();
  183. return 0;
  184. }
  185. extern "C" [[noreturn]] void init(u32 physical_address_for_kernel_page_tables)
  186. {
  187. // this is only used one time, directly below here. we can't use this part
  188. // of libc at this point in the boot process, or we'd just pull strstr in
  189. // from <string.h>.
  190. auto bad_prefix_check = [](const char* str, const char* search) -> bool {
  191. while (*search)
  192. if (*search++ != *str++)
  193. return false;
  194. return true;
  195. };
  196. // serial_debug will output all the kprintf and dbgprintf data to COM1 at
  197. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  198. // process on live hardware.
  199. //
  200. // note: it must be the first option in the boot cmdline.
  201. if (multiboot_info_ptr->cmdline && bad_prefix_check(reinterpret_cast<const char*>(multiboot_info_ptr->cmdline), "serial_debug"))
  202. set_serial_debug(true);
  203. sse_init();
  204. kmalloc_init();
  205. slab_alloc_init();
  206. // must come after kmalloc_init because we use AK_MAKE_ETERNAL in KParams
  207. new KParams(String(reinterpret_cast<const char*>(multiboot_info_ptr->cmdline)));
  208. bool text_debug = KParams::the().has("text_debug");
  209. vfs = new VFS;
  210. dev_debuglog = new DebugLogDevice;
  211. auto console = make<Console>();
  212. RTC::initialize();
  213. PIC::initialize();
  214. gdt_init();
  215. idt_init();
  216. // call global constructors after gtd and itd init
  217. for (ctor_func_t* ctor = &start_ctors; ctor < &end_ctors; ctor++)
  218. (*ctor)();
  219. keyboard = new KeyboardDevice;
  220. ps2mouse = new PS2MouseDevice;
  221. sb16 = new SB16;
  222. dev_null = new NullDevice;
  223. if (!get_serial_debug())
  224. ttyS0 = new SerialDevice(SERIAL_COM1_ADDR, 64);
  225. ttyS1 = new SerialDevice(SERIAL_COM2_ADDR, 65);
  226. ttyS2 = new SerialDevice(SERIAL_COM3_ADDR, 66);
  227. ttyS3 = new SerialDevice(SERIAL_COM4_ADDR, 67);
  228. VirtualConsole::initialize();
  229. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  230. tty1 = new VirtualConsole(1);
  231. VirtualConsole::switch_to(0);
  232. kprintf("Starting Serenity Operating System...\n");
  233. MemoryManager::initialize(physical_address_for_kernel_page_tables);
  234. if (APIC::init())
  235. APIC::enable(0);
  236. PIT::initialize();
  237. PCI::enumerate_all([](const PCI::Address& address, PCI::ID id) {
  238. kprintf("PCI device: bus=%d slot=%d function=%d id=%w:%w\n",
  239. address.bus(),
  240. address.slot(),
  241. address.function(),
  242. id.vendor_id,
  243. id.device_id);
  244. });
  245. if (text_debug) {
  246. dbgprintf("Text mode enabled\n");
  247. } else {
  248. if (multiboot_info_ptr->framebuffer_type == 1 || multiboot_info_ptr->framebuffer_type == 2) {
  249. new MBVGADevice(
  250. PhysicalAddress((u32)(multiboot_info_ptr->framebuffer_addr)),
  251. multiboot_info_ptr->framebuffer_pitch,
  252. multiboot_info_ptr->framebuffer_width,
  253. multiboot_info_ptr->framebuffer_height);
  254. } else {
  255. new BXVGADevice;
  256. }
  257. }
  258. LoopbackAdapter::the();
  259. auto e1000 = E1000NetworkAdapter::autodetect();
  260. auto rtl8139 = RTL8139NetworkAdapter::autodetect();
  261. Process::initialize();
  262. Thread::initialize();
  263. Process::create_kernel_process("init_stage2", init_stage2);
  264. Process::create_kernel_process("syncd", [] {
  265. for (;;) {
  266. VFS::the().sync();
  267. current->sleep(1 * TICKS_PER_SECOND);
  268. }
  269. });
  270. Process::create_kernel_process("Finalizer", [] {
  271. g_finalizer = current;
  272. current->set_priority(ThreadPriority::Low);
  273. for (;;) {
  274. current->wait_on(*g_finalizer_wait_queue);
  275. Thread::finalize_dying_threads();
  276. }
  277. });
  278. Scheduler::pick_next();
  279. sti();
  280. Scheduler::idle_loop();
  281. ASSERT_NOT_REACHED();
  282. }