init.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "types.h"
  2. #include "VGA.h"
  3. #include "kmalloc.h"
  4. #include "i386.h"
  5. #include "i8253.h"
  6. #include "Keyboard.h"
  7. #include "Task.h"
  8. #include "IPC.h"
  9. #include "system.h"
  10. #include "Disk.h"
  11. #include "PIC.h"
  12. #include "StdLib.h"
  13. #include "Syscall.h"
  14. #include "CMOS.h"
  15. #include "Userspace.h"
  16. #include "IDEDiskDevice.h"
  17. #include <VirtualFileSystem/NullDevice.h>
  18. #include <VirtualFileSystem/ZeroDevice.h>
  19. #include <VirtualFileSystem/FullDevice.h>
  20. #include <VirtualFileSystem/RandomDevice.h>
  21. #include <VirtualFileSystem/Ext2FileSystem.h>
  22. #include <VirtualFileSystem/VirtualFileSystem.h>
  23. #include <VirtualFileSystem/FileHandle.h>
  24. #include <AK/OwnPtr.h>
  25. #include "MemoryManager.h"
  26. #include <ELFLoader/ELFLoader.h>
  27. #include "Console.h"
  28. #include "ProcFileSystem.h"
  29. #define TEST_VFS
  30. //#define STRESS_TEST_SPAWNING
  31. //#define TEST_ELF_LOADER
  32. //#define TEST_CRASHY_USER_PROCESSES
  33. system_t system;
  34. void banner()
  35. {
  36. kprintf("\n");
  37. vga_set_attr(0x0a);
  38. kprintf(" _____ _ _ \n");
  39. vga_set_attr(0x0b);
  40. kprintf("| __|___ ___| |_ ___ ___| |_ \n");
  41. vga_set_attr(0x0c);
  42. kprintf("| | | -_| _| . | -_| _| _|\n");
  43. vga_set_attr(0x0d);
  44. kprintf("|_____|___|_| |___|___|_| |_| \n");
  45. vga_set_attr(0x07);
  46. kprintf("\n");
  47. }
  48. #ifdef TEST_CRASHY_USER_PROCESSES
  49. static void user_main() NORETURN;
  50. static void user_main()
  51. {
  52. DO_SYSCALL_A3(0x3000, 2, 3, 4);
  53. // Crash ourselves!
  54. char* x = reinterpret_cast<char*>(0xbeefbabe);
  55. *x = 1;
  56. HANG;
  57. for (;;) {
  58. // nothing?
  59. Userspace::sleep(1 * TICKS_PER_SECOND);
  60. }
  61. }
  62. #endif
  63. static void undertaker_main() NORETURN;
  64. static void undertaker_main()
  65. {
  66. for (;;) {
  67. Task::doHouseKeeping();
  68. sleep(300);
  69. }
  70. }
  71. static void init_stage2() NORETURN;
  72. static void init_stage2()
  73. {
  74. kprintf("init stage2...\n");
  75. Syscall::initialize();
  76. auto keyboard = make<Keyboard>();
  77. Disk::initialize();
  78. #ifdef TEST_VFS
  79. auto vfs = make<VirtualFileSystem>();
  80. auto dev_zero = make<ZeroDevice>();
  81. vfs->registerCharacterDevice(1, 3, *dev_zero);
  82. auto dev_null = make<NullDevice>();
  83. vfs->registerCharacterDevice(1, 5, *dev_zero);
  84. auto dev_full = make<FullDevice>();
  85. vfs->registerCharacterDevice(1, 7, *dev_full);
  86. auto dev_random = make<RandomDevice>();
  87. vfs->registerCharacterDevice(1, 8, *dev_random);
  88. vfs->registerCharacterDevice(85, 1, *keyboard);
  89. auto dev_hd0 = IDEDiskDevice::create();
  90. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  91. e2fs->initialize();
  92. vfs->mountRoot(e2fs.copyRef());
  93. auto procfs = ProcFileSystem::create();
  94. procfs->initialize();
  95. vfs->mount(procfs.copyRef(), "/proc");
  96. #endif
  97. #ifdef TEST_CRASHY_USER_PROCESSES
  98. new Task(user_main, "user", IPC::Handle::UserTask, Task::Ring3);
  99. #endif
  100. #ifdef TEST_ELF_LOADER
  101. {
  102. auto testExecutable = vfs->open("/bin/id");
  103. ASSERT(testExecutable);
  104. auto testExecutableData = testExecutable->readEntireFile();
  105. ASSERT(testExecutableData);
  106. ExecSpace space;
  107. space.loadELF(move(testExecutableData));
  108. auto* elf_entry = space.symbolPtr("_start");
  109. ASSERT(elf_entry);
  110. typedef int (*MainFunctionPtr)(void);
  111. kprintf("elf_entry: %p\n", elf_entry);
  112. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  113. kprintf("it returned %d\n", rc);
  114. }
  115. #endif
  116. #ifdef STRESS_TEST_SPAWNING
  117. dword lastAlloc = sum_alloc;
  118. for (unsigned i = 0; i < 100; ++i) {
  119. auto* shTask = Task::create("/bin/id", (uid_t)100, (gid_t)100);
  120. kprintf("malloc stats: alloc:%u free:%u\n", sum_alloc, sum_free);
  121. kprintf("sizeof(Task):%u\n", sizeof(Task));
  122. kprintf("delta:%u\n",sum_alloc - lastAlloc);
  123. lastAlloc = sum_alloc;
  124. sleep(600);
  125. }
  126. #endif
  127. auto* shTask = Task::create("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0);
  128. banner();
  129. #if 0
  130. // It would be nice to exit this process, but right now it instantiates all kinds of things.
  131. // At the very least it needs to be made sure those things stick around as appropriate.
  132. DO_SYSCALL_A1(Syscall::PosixExit, 413);
  133. kprintf("uh, we're still going after calling sys$exit...\n");
  134. HANG;
  135. #endif
  136. for (;;) {
  137. asm("hlt");
  138. }
  139. }
  140. void init()
  141. {
  142. cli();
  143. kmalloc_init();
  144. vga_init();
  145. auto console = make<Console>();
  146. PIC::initialize();
  147. gdt_init();
  148. idt_init();
  149. MemoryManager::initialize();
  150. VirtualFileSystem::initializeGlobals();
  151. StringImpl::initializeGlobals();
  152. PIT::initialize();
  153. memset(&system, 0, sizeof(system));
  154. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  155. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  156. kprintf("%u kB base memory\n", base_memory);
  157. kprintf("%u kB extended memory\n", ext_memory);
  158. Task::initialize();
  159. new Task(undertaker_main, "undertaker", IPC::Handle::UserTask, Task::Ring0);
  160. auto* init2 = new Task(init_stage2, "init", IPC::Handle::InitTask, Task::Ring0);
  161. scheduleNewTask();
  162. sti();
  163. // This now becomes the idle task :^)
  164. for (;;) {
  165. asm("hlt");
  166. }
  167. }