init.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "Process.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. #include "VirtualConsole.h"
  29. #define TEST_VFS
  30. #define KSYMS
  31. #define SPAWN_MULTIPLE_SHELLS
  32. //#define STRESS_TEST_SPAWNING
  33. //#define TEST_ELF_LOADER
  34. system_t system;
  35. VirtualConsole* tty0;
  36. VirtualConsole* tty1;
  37. VirtualConsole* tty2;
  38. VirtualConsole* tty3;
  39. Keyboard* keyboard;
  40. static byte parseHexDigit(char nibble)
  41. {
  42. if (nibble >= '0' && nibble <= '9')
  43. return nibble - '0';
  44. ASSERT(nibble >= 'a' && nibble <= 'f');
  45. return 10 + (nibble - 'a');
  46. }
  47. static Vector<KSym, KmallocEternalAllocator>* s_ksyms;
  48. Vector<KSym, KmallocEternalAllocator>& ksyms()
  49. {
  50. return *s_ksyms;
  51. }
  52. const KSym* ksymbolicate(dword address)
  53. {
  54. if (address < ksyms().first().address || address > ksyms().last().address)
  55. return nullptr;
  56. for (unsigned i = 0; i < ksyms().size(); ++i) {
  57. if (address < ksyms()[i + 1].address)
  58. return &ksyms()[i];
  59. }
  60. return nullptr;
  61. }
  62. static void loadKsyms(const ByteBuffer& buffer)
  63. {
  64. // FIXME: It's gross that this vector grows dynamically rather than being sized-to-fit.
  65. // We're wasting that eternal kmalloc memory.
  66. s_ksyms = new Vector<KSym, KmallocEternalAllocator>;
  67. auto* bufptr = (const char*)buffer.pointer();
  68. auto* startOfName = bufptr;
  69. dword address = 0;
  70. while (bufptr < buffer.endPointer()) {
  71. for (unsigned i = 0; i < 8; ++i)
  72. address = (address << 4) | parseHexDigit(*(bufptr++));
  73. bufptr += 3;
  74. startOfName = bufptr;
  75. while (*(++bufptr)) {
  76. if (*bufptr == '\n') {
  77. break;
  78. }
  79. }
  80. // FIXME: The Strings here should be eternally allocated too.
  81. ksyms().append({ address, String(startOfName, bufptr - startOfName) });
  82. ++bufptr;
  83. }
  84. }
  85. static void undertaker_main() NORETURN;
  86. static void undertaker_main()
  87. {
  88. for (;;) {
  89. Process::doHouseKeeping();
  90. sleep(300);
  91. }
  92. }
  93. static void spawn_stress() NORETURN;
  94. static void spawn_stress()
  95. {
  96. dword lastAlloc = sum_alloc;
  97. for (unsigned i = 0; i < 10000; ++i) {
  98. int error;
  99. Process::createUserProcess("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error, nullptr, tty0);
  100. kprintf("malloc stats: alloc:%u free:%u page_aligned:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_page_aligned, kmalloc_sum_eternal);
  101. kprintf("delta:%u\n", sum_alloc - lastAlloc);
  102. lastAlloc = sum_alloc;
  103. sleep(60);
  104. }
  105. for (;;) {
  106. asm volatile("hlt");
  107. }
  108. }
  109. static void init_stage2() NORETURN;
  110. static void init_stage2()
  111. {
  112. Syscall::initialize();
  113. Disk::initialize();
  114. #ifdef TEST_VFS
  115. auto vfs = make<VirtualFileSystem>();
  116. auto dev_zero = make<ZeroDevice>();
  117. vfs->registerCharacterDevice(*dev_zero);
  118. auto dev_null = make<NullDevice>();
  119. vfs->registerCharacterDevice(*dev_null);
  120. auto dev_full = make<FullDevice>();
  121. vfs->registerCharacterDevice(*dev_full);
  122. auto dev_random = make<RandomDevice>();
  123. vfs->registerCharacterDevice(*dev_random);
  124. vfs->registerCharacterDevice(*keyboard);
  125. vfs->registerCharacterDevice(*tty0);
  126. vfs->registerCharacterDevice(*tty1);
  127. vfs->registerCharacterDevice(*tty2);
  128. vfs->registerCharacterDevice(*tty3);
  129. auto dev_hd0 = IDEDiskDevice::create();
  130. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  131. e2fs->initialize();
  132. vfs->mountRoot(e2fs.copyRef());
  133. #ifdef KSYMS
  134. {
  135. int error;
  136. auto handle = vfs->open("/kernel.map", error);
  137. if (!handle) {
  138. kprintf("Failed to open /kernel.map\n");
  139. } else {
  140. auto buffer = handle->readEntireFile();
  141. ASSERT(buffer);
  142. loadKsyms(buffer);
  143. }
  144. }
  145. #endif
  146. vfs->mount(ProcFileSystem::the(), "/proc");
  147. #endif
  148. #ifdef TEST_ELF_LOADER
  149. {
  150. auto testExecutable = vfs->open("/bin/id");
  151. ASSERT(testExecutable);
  152. auto testExecutableData = testExecutable->readEntireFile();
  153. ASSERT(testExecutableData);
  154. ExecSpace space;
  155. space.loadELF(move(testExecutableData));
  156. auto* elf_entry = space.symbolPtr("_start");
  157. ASSERT(elf_entry);
  158. typedef int (*MainFunctionPtr)(void);
  159. kprintf("elf_entry: %p\n", elf_entry);
  160. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  161. kprintf("it returned %d\n", rc);
  162. }
  163. #endif
  164. int error;
  165. auto* sh0 = Process::createUserProcess("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, nullptr, tty0);
  166. #ifdef SPAWN_MULTIPLE_SHELLS
  167. auto* sh1 = Process::createUserProcess("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, nullptr, tty1);
  168. auto* sh2 = Process::createUserProcess("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, nullptr, tty2);
  169. auto* sh3 = Process::createUserProcess("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, nullptr, tty3);
  170. #endif
  171. #ifdef STRESS_TEST_SPAWNING
  172. Process::createKernelProcess(spawn_stress, "spawn_stress");
  173. #endif
  174. #if 0
  175. // It would be nice to exit this process, but right now it instantiates all kinds of things.
  176. // At the very least it needs to be made sure those things stick around as appropriate.
  177. DO_SYSCALL_A1(Syscall::PosixExit, 413);
  178. kprintf("uh, we're still going after calling sys$exit...\n");
  179. HANG;
  180. #endif
  181. for (;;) {
  182. //sleep(3600 * TICKS_PER_SECOND);
  183. asm("hlt");
  184. }
  185. }
  186. void init()
  187. {
  188. cli();
  189. kmalloc_init();
  190. vga_init();
  191. auto console = make<Console>();
  192. RTC::initialize();
  193. PIC::initialize();
  194. gdt_init();
  195. idt_init();
  196. keyboard = new Keyboard;
  197. VirtualConsole::initialize();
  198. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  199. tty1 = new VirtualConsole(1);
  200. tty2 = new VirtualConsole(2);
  201. tty3 = new VirtualConsole(3);
  202. VirtualConsole::switch_to(0);
  203. kprintf("Starting Serenity Operating System...\n");
  204. MemoryManager::initialize();
  205. VirtualFileSystem::initializeGlobals();
  206. StringImpl::initializeGlobals();
  207. PIT::initialize();
  208. memset(&system, 0, sizeof(system));
  209. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  210. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  211. kprintf("%u kB base memory\n", base_memory);
  212. kprintf("%u kB extended memory\n", ext_memory);
  213. auto procfs = ProcFileSystem::create();
  214. procfs->initialize();
  215. Process::initialize();
  216. Process::createKernelProcess(undertaker_main, "undertaker");
  217. Process::createKernelProcess(init_stage2, "init");
  218. scheduleNewProcess();
  219. sti();
  220. // This now becomes the idle process :^)
  221. for (;;) {
  222. asm("hlt");
  223. }
  224. }
  225. void log_try_lock(const char* where)
  226. {
  227. kprintf("[%u] >>> locking... (%s)\n", current->pid(), where);
  228. }
  229. void log_locked(const char* where)
  230. {
  231. kprintf("[%u] >>> locked() in %s\n", current->pid(), where);
  232. }
  233. void log_unlocked(const char* where)
  234. {
  235. kprintf("[%u] <<< unlocked()\n", current->pid(), where);
  236. }