init.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #define TEST_VFS
  29. //#define STRESS_TEST_SPAWNING
  30. //#define TEST_ELF_LOADER
  31. //#define TEST_CRASHY_USER_PROCESSES
  32. system_t system;
  33. void banner()
  34. {
  35. kprintf("\n");
  36. vga_set_attr(0x0a);
  37. kprintf(" _____ _ _ \n");
  38. vga_set_attr(0x0b);
  39. kprintf("| __|___ ___| |_ ___ ___| |_ \n");
  40. vga_set_attr(0x0c);
  41. kprintf("| | | -_| _| . | -_| _| _|\n");
  42. vga_set_attr(0x0d);
  43. kprintf("|_____|___|_| |___|___|_| |_| \n");
  44. vga_set_attr(0x07);
  45. kprintf("\n");
  46. }
  47. #ifdef TEST_CRASHY_USER_PROCESSES
  48. static void user_main() NORETURN;
  49. static void user_main()
  50. {
  51. DO_SYSCALL_A3(0x3000, 2, 3, 4);
  52. // Crash ourselves!
  53. char* x = reinterpret_cast<char*>(0xbeefbabe);
  54. *x = 1;
  55. HANG;
  56. for (;;) {
  57. // nothing?
  58. Userspace::sleep(1 * TICKS_PER_SECOND);
  59. }
  60. }
  61. #endif
  62. static void undertaker_main() NORETURN;
  63. static void undertaker_main()
  64. {
  65. for (;;) {
  66. Task::doHouseKeeping();
  67. sleep(300);
  68. }
  69. }
  70. static void init_stage2() NORETURN;
  71. static void init_stage2()
  72. {
  73. kprintf("init stage2...\n");
  74. Syscall::initialize();
  75. auto keyboard = make<Keyboard>();
  76. Disk::initialize();
  77. #ifdef TEST_VFS
  78. auto vfs = make<VirtualFileSystem>();
  79. auto dev_zero = make<ZeroDevice>();
  80. vfs->registerCharacterDevice(1, 3, *dev_zero);
  81. auto dev_null = make<NullDevice>();
  82. vfs->registerCharacterDevice(1, 5, *dev_zero);
  83. auto dev_full = make<FullDevice>();
  84. vfs->registerCharacterDevice(1, 7, *dev_full);
  85. auto dev_random = make<RandomDevice>();
  86. vfs->registerCharacterDevice(1, 8, *dev_random);
  87. vfs->registerCharacterDevice(85, 1, *keyboard);
  88. auto dev_hd0 = IDEDiskDevice::create();
  89. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  90. e2fs->initialize();
  91. vfs->mountRoot(e2fs.copyRef());
  92. auto procfs = ProcFileSystem::create();
  93. procfs->initialize();
  94. vfs->mount(procfs.copyRef(), "/proc");
  95. #endif
  96. #ifdef TEST_CRASHY_USER_PROCESSES
  97. new Task(user_main, "user", Task::Ring3);
  98. #endif
  99. #ifdef TEST_ELF_LOADER
  100. {
  101. auto testExecutable = vfs->open("/bin/id");
  102. ASSERT(testExecutable);
  103. auto testExecutableData = testExecutable->readEntireFile();
  104. ASSERT(testExecutableData);
  105. ExecSpace space;
  106. space.loadELF(move(testExecutableData));
  107. auto* elf_entry = space.symbolPtr("_start");
  108. ASSERT(elf_entry);
  109. typedef int (*MainFunctionPtr)(void);
  110. kprintf("elf_entry: %p\n", elf_entry);
  111. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  112. kprintf("it returned %d\n", rc);
  113. }
  114. #endif
  115. #ifdef STRESS_TEST_SPAWNING
  116. dword lastAlloc = sum_alloc;
  117. for (unsigned i = 0; i < 100; ++i) {
  118. int error;
  119. auto* shTask = Task::createUserTask("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error);
  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. int error;
  128. auto* shTask = Task::createUserTask("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error);
  129. banner();
  130. #if 0
  131. // It would be nice to exit this process, but right now it instantiates all kinds of things.
  132. // At the very least it needs to be made sure those things stick around as appropriate.
  133. DO_SYSCALL_A1(Syscall::PosixExit, 413);
  134. kprintf("uh, we're still going after calling sys$exit...\n");
  135. HANG;
  136. #endif
  137. for (;;) {
  138. //sleep(3600 * TICKS_PER_SECOND);
  139. asm("hlt");
  140. }
  141. }
  142. void init()
  143. {
  144. cli();
  145. kmalloc_init();
  146. vga_init();
  147. auto console = make<Console>();
  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. Task::initialize();
  161. Task::createKernelTask(undertaker_main, "undertaker");
  162. Task::createKernelTask(init_stage2, "init");
  163. scheduleNewTask();
  164. sti();
  165. // This now becomes the idle task :^)
  166. for (;;) {
  167. asm("hlt");
  168. }
  169. }