init.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "Devices/PATADiskDevice.h"
  2. #include "KSyms.h"
  3. #include "Process.h"
  4. #include "RTC.h"
  5. #include "Scheduler.h"
  6. #include "kmalloc.h"
  7. #include "kstdio.h"
  8. #include <AK/Types.h>
  9. #include <Kernel/Arch/i386/CPU.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/KeyboardDevice.h>
  19. #include <Kernel/Devices/MBRPartitionTable.h>
  20. #include <Kernel/Devices/NullDevice.h>
  21. #include <Kernel/Devices/PATAChannel.h>
  22. #include <Kernel/Devices/PS2MouseDevice.h>
  23. #include <Kernel/Devices/RandomDevice.h>
  24. #include <Kernel/Devices/SB16.h>
  25. #include <Kernel/Devices/SerialDevice.h>
  26. #include <Kernel/Devices/ZeroDevice.h>
  27. #include <Kernel/FileSystem/DevPtsFS.h>
  28. #include <Kernel/FileSystem/Ext2FileSystem.h>
  29. #include <Kernel/FileSystem/ProcFS.h>
  30. #include <Kernel/FileSystem/VirtualFileSystem.h>
  31. #include <Kernel/KParams.h>
  32. #include <Kernel/Multiboot.h>
  33. #include <Kernel/Net/E1000NetworkAdapter.h>
  34. #include <Kernel/Net/NetworkTask.h>
  35. #include <Kernel/PCI.h>
  36. #include <Kernel/TTY/PTYMultiplexer.h>
  37. #include <Kernel/TTY/VirtualConsole.h>
  38. #include <Kernel/VM/MemoryManager.h>
  39. VirtualConsole* tty0;
  40. VirtualConsole* tty1;
  41. VirtualConsole* tty2;
  42. VirtualConsole* tty3;
  43. KeyboardDevice* keyboard;
  44. PS2MouseDevice* ps2mouse;
  45. SB16* sb16;
  46. DebugLogDevice* dev_debuglog;
  47. NullDevice* dev_null;
  48. SerialDevice* ttyS0;
  49. SerialDevice* ttyS1;
  50. SerialDevice* ttyS2;
  51. SerialDevice* ttyS3;
  52. VFS* vfs;
  53. [[noreturn]] static void init_stage2()
  54. {
  55. Syscall::initialize();
  56. auto dev_zero = make<ZeroDevice>();
  57. auto dev_full = make<FullDevice>();
  58. auto dev_random = make<RandomDevice>();
  59. auto dev_ptmx = make<PTYMultiplexer>();
  60. auto root = KParams::the().get("root");
  61. if (root.is_empty()) {
  62. root = "/dev/hda";
  63. }
  64. if (!root.starts_with("/dev/hda")) {
  65. kprintf("init_stage2: root filesystem must be on the first IDE hard drive (/dev/hda)\n");
  66. hang();
  67. }
  68. auto pata0 = PATAChannel::create(PATAChannel::ChannelType::Primary);
  69. NonnullRefPtr<DiskDevice> root_dev = *pata0->master_device();
  70. root = root.substring(strlen("/dev/hda"), root.length() - strlen("/dev/hda"));
  71. if (root.length()) {
  72. bool ok;
  73. unsigned partition_number = root.to_uint(ok);
  74. if (!ok) {
  75. kprintf("init_stage2: couldn't parse partition number from root kernel parameter\n");
  76. hang();
  77. }
  78. if (partition_number < 1 || partition_number > 4) {
  79. kprintf("init_stage2: invalid partition number %d; expected 1 to 4\n", partition_number);
  80. hang();
  81. }
  82. MBRPartitionTable mbr(root_dev);
  83. if (!mbr.initialize()) {
  84. kprintf("init_stage2: couldn't read MBR from disk\n");
  85. hang();
  86. }
  87. auto partition = mbr.partition(partition_number);
  88. if (!partition) {
  89. kprintf("init_stage2: couldn't get partition %d\n", partition_number);
  90. hang();
  91. }
  92. root_dev = *partition;
  93. }
  94. auto e2fs = Ext2FS::create(root_dev);
  95. if (!e2fs->initialize()) {
  96. kprintf("init_stage2: couldn't open root filesystem\n");
  97. hang();
  98. }
  99. vfs->mount_root(e2fs);
  100. dbgprintf("Load ksyms\n");
  101. load_ksyms();
  102. dbgprintf("Loaded ksyms\n");
  103. vfs->mount(ProcFS::the(), "/proc");
  104. vfs->mount(DevPtsFS::the(), "/dev/pts");
  105. // Now, detect whether or not there are actually any floppy disks attached to the system
  106. u8 detect = CMOS::read(0x10);
  107. RefPtr<FloppyDiskDevice> fd0;
  108. RefPtr<FloppyDiskDevice> fd1;
  109. if ((detect >> 4) & 0x4) {
  110. fd0 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Master);
  111. kprintf("fd0 is 1.44MB floppy drive\n");
  112. } else {
  113. kprintf("fd0 type unsupported! Type == 0x%x\n", detect >> 4);
  114. }
  115. if (detect & 0x0f) {
  116. fd1 = FloppyDiskDevice::create(FloppyDiskDevice::DriveType::Slave);
  117. kprintf("fd1 is 1.44MB floppy drive");
  118. } else {
  119. kprintf("fd1 type unsupported! Type == 0x%x\n", detect & 0x0f);
  120. }
  121. int error;
  122. auto* system_server_process = Process::create_user_process("/bin/SystemServer", (uid_t)100, (gid_t)100, (pid_t)0, error, {}, {}, tty0);
  123. if (error != 0) {
  124. kprintf("init_stage2: error spawning SystemServer: %d\n", error);
  125. hang();
  126. }
  127. system_server_process->set_priority(Process::HighPriority);
  128. auto* tty1_process = Process::create_user_process("/bin/TTYServer", (uid_t)0, (gid_t)0, (pid_t)0, error, { "/bin/TTYServer", "tty1" }, {}, tty1);
  129. if (error != 0) {
  130. kprintf("init_stage2: error spawning TTYServer for tty1: %d\n", error);
  131. hang();
  132. }
  133. tty1_process->set_priority(Process::HighPriority);
  134. auto* tty2_process = Process::create_user_process("/bin/TTYServer", (uid_t)0, (gid_t)0, (pid_t)0, error, { "/bin/TTYServer", "tty2" }, {}, tty2);
  135. if (error != 0) {
  136. kprintf("init_stage2: error spawning TTYServer for tty2: %d\n", error);
  137. hang();
  138. }
  139. tty2_process->set_priority(Process::HighPriority);
  140. auto* tty3_process = Process::create_user_process("/bin/TTYServer", (uid_t)0, (gid_t)0, (pid_t)0, error, { "/bin/TTYServer", "tty3" }, {}, tty3);
  141. if (error != 0) {
  142. kprintf("init_stage2: error spawning TTYServer for tty3: %d\n", error);
  143. hang();
  144. }
  145. tty3_process->set_priority(Process::HighPriority);
  146. current->process().sys$exit(0);
  147. ASSERT_NOT_REACHED();
  148. }
  149. extern "C" {
  150. multiboot_info_t* multiboot_info_ptr;
  151. }
  152. extern "C" [[noreturn]] void init()
  153. {
  154. // this is only used one time, directly below here. we can't use this part
  155. // of libc at this point in the boot process, or we'd just pull strstr in
  156. // from <string.h>.
  157. auto bad_prefix_check = [](const char *str, const char *search) -> bool {
  158. while (*search)
  159. if (*search++ != *str++)
  160. return false;
  161. return true;
  162. };
  163. // serial_debug will output all the kprintf and dbgprintf data to COM1 at
  164. // 8-N-1 57600 baud. this is particularly useful for debugging the boot
  165. // process on live hardware.
  166. //
  167. // note: it must be the first option in the boot cmdline.
  168. if (multiboot_info_ptr->cmdline && bad_prefix_check(reinterpret_cast<const char*>(multiboot_info_ptr->cmdline), "serial_debug"))
  169. set_serial_debug(true);
  170. sse_init();
  171. kmalloc_init();
  172. init_ksyms();
  173. // must come after kmalloc_init because we use AK_MAKE_ETERNAL in KParams
  174. new KParams(String(reinterpret_cast<const char*>(multiboot_info_ptr->cmdline)));
  175. vfs = new VFS;
  176. dev_debuglog = new DebugLogDevice;
  177. auto console = make<Console>();
  178. RTC::initialize();
  179. PIC::initialize();
  180. gdt_init();
  181. idt_init();
  182. keyboard = new KeyboardDevice;
  183. ps2mouse = new PS2MouseDevice;
  184. sb16 = new SB16;
  185. dev_null = new NullDevice;
  186. if (!get_serial_debug())
  187. ttyS0 = new SerialDevice(SERIAL_COM1_ADDR, 64);
  188. ttyS1 = new SerialDevice(SERIAL_COM2_ADDR, 65);
  189. ttyS2 = new SerialDevice(SERIAL_COM3_ADDR, 66);
  190. ttyS3 = new SerialDevice(SERIAL_COM4_ADDR, 67);
  191. VirtualConsole::initialize();
  192. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  193. tty1 = new VirtualConsole(1);
  194. tty2 = new VirtualConsole(2);
  195. tty3 = new VirtualConsole(3);
  196. VirtualConsole::switch_to(0);
  197. kprintf("Starting Serenity Operating System...\n");
  198. MemoryManager::initialize();
  199. PIT::initialize();
  200. PCI::enumerate_all([](const PCI::Address& address, PCI::ID id) {
  201. kprintf("PCI device: bus=%d slot=%d function=%d id=%w:%w\n",
  202. address.bus(),
  203. address.slot(),
  204. address.function(),
  205. id.vendor_id,
  206. id.device_id
  207. );
  208. });
  209. new BXVGADevice;
  210. auto e1000 = E1000NetworkAdapter::autodetect();
  211. NonnullRefPtr<ProcFS> new_procfs = ProcFS::create();
  212. new_procfs->initialize();
  213. auto devptsfs = DevPtsFS::create();
  214. devptsfs->initialize();
  215. Process::initialize();
  216. Thread::initialize();
  217. Process::create_kernel_process("init_stage2", init_stage2);
  218. Process::create_kernel_process("syncd", [] {
  219. for (;;) {
  220. Syscall::sync();
  221. current->sleep(1 * TICKS_PER_SECOND);
  222. }
  223. });
  224. Process::create_kernel_process("Finalizer", [] {
  225. g_finalizer = current;
  226. current->process().set_priority(Process::LowPriority);
  227. for (;;) {
  228. Thread::finalize_dying_threads();
  229. (void)current->block<Thread::SemiPermanentBlocker>(Thread::SemiPermanentBlocker::Reason::Lurking);
  230. }
  231. });
  232. Process::create_kernel_process("NetworkTask", NetworkTask_main);
  233. Scheduler::pick_next();
  234. sti();
  235. // This now becomes the idle process :^)
  236. for (;;) {
  237. asm("hlt");
  238. }
  239. }