init.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "types.h"
  2. #include "kmalloc.h"
  3. #include "i386.h"
  4. #include "i8253.h"
  5. #include "Keyboard.h"
  6. #include "Process.h"
  7. #include "system.h"
  8. #include "PIC.h"
  9. #include "CMOS.h"
  10. #include "IDEDiskDevice.h"
  11. #include "KSyms.h"
  12. #include <VirtualFileSystem/NullDevice.h>
  13. #include <VirtualFileSystem/ZeroDevice.h>
  14. #include <VirtualFileSystem/FullDevice.h>
  15. #include <VirtualFileSystem/RandomDevice.h>
  16. #include <VirtualFileSystem/Ext2FileSystem.h>
  17. #include <VirtualFileSystem/VirtualFileSystem.h>
  18. #include <Widgets/GUIEventDevice.h>
  19. #include "MemoryManager.h"
  20. #include "ProcFileSystem.h"
  21. #include "RTC.h"
  22. #include "VirtualConsole.h"
  23. #include "Scheduler.h"
  24. #include "PS2MouseDevice.h"
  25. #include "MasterPTY.h"
  26. #include "SlavePTY.h"
  27. #define SPAWN_GUI_TEST_APP
  28. //#define SPAWN_MULTIPLE_SHELLS
  29. //#define STRESS_TEST_SPAWNING
  30. system_t system;
  31. VirtualConsole* tty0;
  32. VirtualConsole* tty1;
  33. VirtualConsole* tty2;
  34. VirtualConsole* tty3;
  35. Keyboard* keyboard;
  36. PS2MouseDevice* ps2mouse;
  37. GUIEventDevice* gui_event_device;
  38. MasterPTY* ptm0;
  39. MasterPTY* ptm1;
  40. MasterPTY* ptm2;
  41. MasterPTY* ptm3;
  42. SlavePTY* pts0;
  43. SlavePTY* pts1;
  44. SlavePTY* pts2;
  45. SlavePTY* pts3;
  46. #ifdef STRESS_TEST_SPAWNING
  47. static void spawn_stress() NORETURN;
  48. static void spawn_stress()
  49. {
  50. dword last_sum_alloc = sum_alloc;
  51. for (unsigned i = 0; i < 10000; ++i) {
  52. int error;
  53. Process::create_user_process("/bin/true", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, { }, tty0);
  54. dbgprintf("malloc stats: alloc:%u free:%u eternal:%u !delta:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal, sum_alloc - last_sum_alloc);
  55. last_sum_alloc = sum_alloc;
  56. sleep(60);
  57. }
  58. for (;;) {
  59. asm volatile("hlt");
  60. }
  61. }
  62. #endif
  63. static void make_pty_pair(unsigned index)
  64. {
  65. auto* master = new MasterPTY(index);
  66. auto* slave = new SlavePTY(index);
  67. master->set_slave(*slave);
  68. slave->set_master(*master);
  69. VFS::the().register_character_device(*master);
  70. VFS::the().register_character_device(*slave);
  71. }
  72. static void init_stage2() NORETURN;
  73. static void init_stage2()
  74. {
  75. Syscall::initialize();
  76. auto vfs = make<VFS>();
  77. auto dev_zero = make<ZeroDevice>();
  78. vfs->register_character_device(*dev_zero);
  79. auto dev_null = make<NullDevice>();
  80. vfs->register_character_device(*dev_null);
  81. auto dev_full = make<FullDevice>();
  82. vfs->register_character_device(*dev_full);
  83. auto dev_random = make<RandomDevice>();
  84. vfs->register_character_device(*dev_random);
  85. make_pty_pair(0);
  86. make_pty_pair(1);
  87. make_pty_pair(2);
  88. make_pty_pair(3);
  89. vfs->register_character_device(*keyboard);
  90. vfs->register_character_device(*ps2mouse);
  91. vfs->register_character_device(*gui_event_device);
  92. vfs->register_character_device(*tty0);
  93. vfs->register_character_device(*tty1);
  94. vfs->register_character_device(*tty2);
  95. vfs->register_character_device(*tty3);
  96. auto dev_hd0 = IDEDiskDevice::create();
  97. auto e2fs = Ext2FS::create(dev_hd0.copyRef());
  98. e2fs->initialize();
  99. vfs->mount_root(e2fs.copyRef());
  100. load_ksyms();
  101. vfs->mount(ProcFS::the(), "/proc");
  102. Vector<String> environment;
  103. environment.append("TERM=ansi");
  104. int error;
  105. //Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0);
  106. Process::create_user_process("/bin/Terminal", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0);
  107. #ifdef SPAWN_GUI_TEST_APP
  108. Process::create_user_process("/bin/guitest", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0);
  109. #endif
  110. #ifdef SPAWN_MULTIPLE_SHELLS
  111. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, { }, tty1);
  112. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, { }, tty2);
  113. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, { }, tty3);
  114. #endif
  115. #ifdef STRESS_TEST_SPAWNING
  116. Process::create_kernel_process("spawn_stress", spawn_stress);
  117. #endif
  118. extern void WindowServer_main();
  119. Process::create_kernel_process("WindowServer", WindowServer_main);
  120. current->sys$exit(0);
  121. ASSERT_NOT_REACHED();
  122. }
  123. void init() NORETURN;
  124. void init()
  125. {
  126. cli();
  127. kmalloc_init();
  128. init_ksyms();
  129. auto console = make<Console>();
  130. RTC::initialize();
  131. PIC::initialize();
  132. gdt_init();
  133. idt_init();
  134. keyboard = new Keyboard;
  135. ps2mouse = new PS2MouseDevice;
  136. gui_event_device = new GUIEventDevice;
  137. VirtualConsole::initialize();
  138. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  139. tty1 = new VirtualConsole(1);
  140. tty2 = new VirtualConsole(2);
  141. tty3 = new VirtualConsole(3);
  142. VirtualConsole::switch_to(0);
  143. kprintf("Starting Serenity Operating System...\n");
  144. MemoryManager::initialize();
  145. VFS::initialize_globals();
  146. StringImpl::initialize_globals();
  147. PIT::initialize();
  148. memset(&system, 0, sizeof(system));
  149. word base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  150. word ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  151. kprintf("%u kB base memory\n", base_memory);
  152. kprintf("%u kB extended memory\n", ext_memory);
  153. auto procfs = ProcFS::create();
  154. procfs->initialize();
  155. Process::initialize();
  156. Process::create_kernel_process("init_stage2", init_stage2);
  157. Process::create_kernel_process("syncd", [] {
  158. for (;;) {
  159. Syscall::sync();
  160. sleep(10 * TICKS_PER_SECOND);
  161. }
  162. });
  163. Scheduler::pick_next();
  164. sti();
  165. // This now becomes the idle process :^)
  166. for (;;) {
  167. asm("hlt");
  168. }
  169. }