init.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "system.h"
  9. #include "Disk.h"
  10. #include "PIC.h"
  11. #include "StdLib.h"
  12. #include "Syscall.h"
  13. #include "CMOS.h"
  14. #include "Userspace.h"
  15. #include "IDEDiskDevice.h"
  16. #include <VirtualFileSystem/NullDevice.h>
  17. #include <VirtualFileSystem/ZeroDevice.h>
  18. #include <VirtualFileSystem/FullDevice.h>
  19. #include <VirtualFileSystem/RandomDevice.h>
  20. #include <VirtualFileSystem/Ext2FileSystem.h>
  21. #include <VirtualFileSystem/VirtualFileSystem.h>
  22. #include <VirtualFileSystem/FileHandle.h>
  23. #include <AK/OwnPtr.h>
  24. #include "MemoryManager.h"
  25. #include <ELFLoader/ELFLoader.h>
  26. #include "Console.h"
  27. #include "ProcFileSystem.h"
  28. #include "RTC.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, 5, *dev_zero);
  82. auto dev_null = make<NullDevice>();
  83. vfs->registerCharacterDevice(1, 3, *dev_null);
  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. vfs->mount(ProcFileSystem::the(), "/proc");
  94. #endif
  95. #ifdef TEST_CRASHY_USER_PROCESSES
  96. new Task(user_main, "user", Task::Ring3);
  97. #endif
  98. #ifdef TEST_ELF_LOADER
  99. {
  100. auto testExecutable = vfs->open("/bin/id");
  101. ASSERT(testExecutable);
  102. auto testExecutableData = testExecutable->readEntireFile();
  103. ASSERT(testExecutableData);
  104. ExecSpace space;
  105. space.loadELF(move(testExecutableData));
  106. auto* elf_entry = space.symbolPtr("_start");
  107. ASSERT(elf_entry);
  108. typedef int (*MainFunctionPtr)(void);
  109. kprintf("elf_entry: %p\n", elf_entry);
  110. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  111. kprintf("it returned %d\n", rc);
  112. }
  113. #endif
  114. #ifdef STRESS_TEST_SPAWNING
  115. dword lastAlloc = sum_alloc;
  116. for (unsigned i = 0; i < 100; ++i) {
  117. int error;
  118. auto* shTask = Task::createUserTask("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error);
  119. kprintf("malloc stats: alloc:%u free:%u\n", sum_alloc, sum_free);
  120. kprintf("sizeof(Task):%u\n", sizeof(Task));
  121. kprintf("delta:%u\n",sum_alloc - lastAlloc);
  122. lastAlloc = sum_alloc;
  123. sleep(600);
  124. }
  125. #endif
  126. int error;
  127. auto* shTask = Task::createUserTask("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error);
  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. //sleep(3600 * TICKS_PER_SECOND);
  138. asm("hlt");
  139. }
  140. }
  141. void init()
  142. {
  143. cli();
  144. kmalloc_init();
  145. vga_init();
  146. auto console = make<Console>();
  147. RTC::initialize();
  148. PIC::initialize();
  149. gdt_init();
  150. idt_init();
  151. MemoryManager::initialize();
  152. VirtualFileSystem::initializeGlobals();
  153. StringImpl::initializeGlobals();
  154. PIT::initialize();
  155. memset(&system, 0, sizeof(system));
  156. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  157. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  158. kprintf("%u kB base memory\n", base_memory);
  159. kprintf("%u kB extended memory\n", ext_memory);
  160. auto procfs = ProcFileSystem::create();
  161. procfs->initialize();
  162. Task::initialize();
  163. Task::createKernelTask(undertaker_main, "undertaker");
  164. Task::createKernelTask(init_stage2, "init");
  165. scheduleNewTask();
  166. sti();
  167. // This now becomes the idle task :^)
  168. for (;;) {
  169. asm("hlt");
  170. }
  171. }