init.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "IDEDiskDevice.h"
  15. #include <VirtualFileSystem/NullDevice.h>
  16. #include <VirtualFileSystem/ZeroDevice.h>
  17. #include <VirtualFileSystem/FullDevice.h>
  18. #include <VirtualFileSystem/RandomDevice.h>
  19. #include <VirtualFileSystem/Ext2FileSystem.h>
  20. #include <VirtualFileSystem/VirtualFileSystem.h>
  21. #include <VirtualFileSystem/FileHandle.h>
  22. #include <AK/OwnPtr.h>
  23. #include "MemoryManager.h"
  24. #include <ELFLoader/ELFLoader.h>
  25. #include "Console.h"
  26. #include "ProcFileSystem.h"
  27. #include "RTC.h"
  28. #define TEST_VFS
  29. #define KSYMS
  30. //#define STRESS_TEST_SPAWNING
  31. //#define TEST_ELF_LOADER
  32. system_t system;
  33. void banner()
  34. {
  35. kprintf("\n\033[33;1mWelcome to \033[36;1mSerenity OS!\033[0m\n\n");
  36. }
  37. static byte parseHexDigit(char nibble)
  38. {
  39. if (nibble >= '0' && nibble <= '9')
  40. return nibble - '0';
  41. ASSERT(nibble >= 'a' && nibble <= 'f');
  42. return 10 + (nibble - 'a');
  43. }
  44. static Vector<KSym>* s_ksyms;
  45. Vector<KSym>& ksyms()
  46. {
  47. return *s_ksyms;
  48. }
  49. const KSym* ksymbolicate(dword address)
  50. {
  51. if (address < ksyms().first().address || address > ksyms().last().address)
  52. return nullptr;
  53. for (unsigned i = 0; i < ksyms().size(); ++i) {
  54. if (address < ksyms()[i + 1].address)
  55. return &ksyms()[i];
  56. }
  57. return nullptr;
  58. }
  59. static void loadKsyms(const ByteBuffer& buffer)
  60. {
  61. s_ksyms = new Vector<KSym>;
  62. auto* bufptr = (const char*)buffer.pointer();
  63. auto* startOfName = bufptr;
  64. dword address = 0;
  65. while (bufptr < buffer.endPointer()) {
  66. for (unsigned i = 0; i < 8; ++i)
  67. address = (address << 4) | parseHexDigit(*(bufptr++));
  68. bufptr += 3;
  69. startOfName = bufptr;
  70. while (*(++bufptr)) {
  71. if (*bufptr == '\n') {
  72. break;
  73. }
  74. }
  75. ksyms().append({ address, String(startOfName, bufptr - startOfName) });
  76. ++bufptr;
  77. }
  78. }
  79. static void undertaker_main() NORETURN;
  80. static void undertaker_main()
  81. {
  82. for (;;) {
  83. Task::doHouseKeeping();
  84. sleep(300);
  85. }
  86. }
  87. static void init_stage2() NORETURN;
  88. static void init_stage2()
  89. {
  90. kprintf("init stage2...\n");
  91. Syscall::initialize();
  92. auto keyboard = make<Keyboard>();
  93. Disk::initialize();
  94. #ifdef TEST_VFS
  95. auto vfs = make<VirtualFileSystem>();
  96. auto dev_zero = make<ZeroDevice>();
  97. vfs->registerCharacterDevice(1, 5, *dev_zero);
  98. auto dev_null = make<NullDevice>();
  99. vfs->registerCharacterDevice(1, 3, *dev_null);
  100. auto dev_full = make<FullDevice>();
  101. vfs->registerCharacterDevice(1, 7, *dev_full);
  102. auto dev_random = make<RandomDevice>();
  103. vfs->registerCharacterDevice(1, 8, *dev_random);
  104. vfs->registerCharacterDevice(85, 1, *keyboard);
  105. auto dev_hd0 = IDEDiskDevice::create();
  106. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  107. e2fs->initialize();
  108. vfs->mountRoot(e2fs.copyRef());
  109. #ifdef KSYMS
  110. {
  111. int error;
  112. auto handle = vfs->open("/kernel.map", error);
  113. if (!handle) {
  114. kprintf("Failed to open /kernel.map\n");
  115. } else {
  116. auto buffer = handle->readEntireFile();
  117. ASSERT(buffer);
  118. loadKsyms(buffer);
  119. }
  120. }
  121. #endif
  122. vfs->mount(ProcFileSystem::the(), "/proc");
  123. #endif
  124. #ifdef TEST_ELF_LOADER
  125. {
  126. auto testExecutable = vfs->open("/bin/id");
  127. ASSERT(testExecutable);
  128. auto testExecutableData = testExecutable->readEntireFile();
  129. ASSERT(testExecutableData);
  130. ExecSpace space;
  131. space.loadELF(move(testExecutableData));
  132. auto* elf_entry = space.symbolPtr("_start");
  133. ASSERT(elf_entry);
  134. typedef int (*MainFunctionPtr)(void);
  135. kprintf("elf_entry: %p\n", elf_entry);
  136. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  137. kprintf("it returned %d\n", rc);
  138. }
  139. #endif
  140. #ifdef STRESS_TEST_SPAWNING
  141. dword lastAlloc = sum_alloc;
  142. for (unsigned i = 0; i < 100; ++i) {
  143. int error;
  144. auto* shTask = Task::createUserTask("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error);
  145. kprintf("malloc stats: alloc:%u free:%u\n", sum_alloc, sum_free);
  146. kprintf("sizeof(Task):%u\n", sizeof(Task));
  147. kprintf("delta:%u\n",sum_alloc - lastAlloc);
  148. lastAlloc = sum_alloc;
  149. sleep(600);
  150. }
  151. #endif
  152. int error;
  153. auto* shTask = Task::createUserTask("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error);
  154. banner();
  155. #if 0
  156. // It would be nice to exit this process, but right now it instantiates all kinds of things.
  157. // At the very least it needs to be made sure those things stick around as appropriate.
  158. DO_SYSCALL_A1(Syscall::PosixExit, 413);
  159. kprintf("uh, we're still going after calling sys$exit...\n");
  160. HANG;
  161. #endif
  162. for (;;) {
  163. //sleep(3600 * TICKS_PER_SECOND);
  164. asm("hlt");
  165. }
  166. }
  167. void init()
  168. {
  169. cli();
  170. kmalloc_init();
  171. vga_init();
  172. auto console = make<Console>();
  173. RTC::initialize();
  174. PIC::initialize();
  175. gdt_init();
  176. idt_init();
  177. MemoryManager::initialize();
  178. VirtualFileSystem::initializeGlobals();
  179. StringImpl::initializeGlobals();
  180. PIT::initialize();
  181. memset(&system, 0, sizeof(system));
  182. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  183. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  184. kprintf("%u kB base memory\n", base_memory);
  185. kprintf("%u kB extended memory\n", ext_memory);
  186. auto procfs = ProcFileSystem::create();
  187. procfs->initialize();
  188. Task::initialize();
  189. Task::createKernelTask(undertaker_main, "undertaker");
  190. Task::createKernelTask(init_stage2, "init");
  191. scheduleNewTask();
  192. sti();
  193. // This now becomes the idle task :^)
  194. for (;;) {
  195. asm("hlt");
  196. }
  197. }