init.cpp 9.1 KB

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