init.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include "types.h"
  2. #include "kmalloc.h"
  3. #include "i386.h"
  4. #include "i8253.h"
  5. #include "Keyboard.h"
  6. #include "Process.h"
  7. #include "system.h"
  8. #include "PIC.h"
  9. #include "StdLib.h"
  10. #include "Syscall.h"
  11. #include "CMOS.h"
  12. #include "IDEDiskDevice.h"
  13. #include <VirtualFileSystem/NullDevice.h>
  14. #include <VirtualFileSystem/ZeroDevice.h>
  15. #include <VirtualFileSystem/FullDevice.h>
  16. #include <VirtualFileSystem/RandomDevice.h>
  17. #include <VirtualFileSystem/Ext2FileSystem.h>
  18. #include <VirtualFileSystem/VirtualFileSystem.h>
  19. #include <VirtualFileSystem/FileDescriptor.h>
  20. #include <AK/OwnPtr.h>
  21. #include "MemoryManager.h"
  22. #include <ELFLoader/ELFLoader.h>
  23. #include "Console.h"
  24. #include "ProcFileSystem.h"
  25. #include "RTC.h"
  26. #include "VirtualConsole.h"
  27. #include "Scheduler.h"
  28. #define TEST_VFS
  29. #define KSYMS
  30. #define SPAWN_MULTIPLE_SHELLS
  31. //#define STRESS_TEST_SPAWNING
  32. //#define TEST_ELF_LOADER
  33. system_t system;
  34. VirtualConsole* tty0;
  35. VirtualConsole* tty1;
  36. VirtualConsole* tty2;
  37. VirtualConsole* tty3;
  38. Keyboard* keyboard;
  39. static byte parseHexDigit(char nibble)
  40. {
  41. if (nibble >= '0' && nibble <= '9')
  42. return nibble - '0';
  43. ASSERT(nibble >= 'a' && nibble <= 'f');
  44. return 10 + (nibble - 'a');
  45. }
  46. #ifdef KSYMS
  47. static Vector<KSym, KmallocEternalAllocator>* s_ksyms;
  48. static bool s_ksyms_ready;
  49. Vector<KSym, KmallocEternalAllocator>& ksyms()
  50. {
  51. return *s_ksyms;
  52. }
  53. bool ksyms_ready()
  54. {
  55. return s_ksyms_ready;
  56. }
  57. const KSym* ksymbolicate(dword address)
  58. {
  59. if (address < ksyms().first().address || address > ksyms().last().address)
  60. return nullptr;
  61. for (unsigned i = 0; i < ksyms().size(); ++i) {
  62. if (address < ksyms()[i + 1].address)
  63. return &ksyms()[i];
  64. }
  65. return nullptr;
  66. }
  67. static void loadKsyms(const ByteBuffer& buffer)
  68. {
  69. // FIXME: It's gross that this vector grows dynamically rather than being sized-to-fit.
  70. // We're wasting that eternal kmalloc memory.
  71. s_ksyms = new Vector<KSym, KmallocEternalAllocator>;
  72. auto* bufptr = (const char*)buffer.pointer();
  73. auto* startOfName = bufptr;
  74. dword address = 0;
  75. dword ksym_count = 0;
  76. for (unsigned i = 0; i < 8; ++i)
  77. ksym_count = (ksym_count << 4) | parseHexDigit(*(bufptr++));
  78. s_ksyms->ensureCapacity(ksym_count);
  79. ++bufptr; // skip newline
  80. kprintf("Loading ksyms: \033[s");
  81. while (bufptr < buffer.endPointer()) {
  82. for (unsigned i = 0; i < 8; ++i)
  83. address = (address << 4) | parseHexDigit(*(bufptr++));
  84. bufptr += 3;
  85. startOfName = bufptr;
  86. while (*(++bufptr)) {
  87. if (*bufptr == '\n') {
  88. break;
  89. }
  90. }
  91. // FIXME: The Strings here should be eternally allocated too.
  92. ksyms().append({ address, String(startOfName, bufptr - startOfName) });
  93. kprintf("\033[u\033[s%u/%u", ksyms().size(), ksym_count);
  94. ++bufptr;
  95. }
  96. kprintf("\n");
  97. s_ksyms_ready = true;
  98. }
  99. void dump_backtrace(bool use_ksyms)
  100. {
  101. if (!current) {
  102. HANG;
  103. return;
  104. }
  105. if (use_ksyms && !ksyms_ready()) {
  106. HANG;
  107. return;
  108. }
  109. struct RecognizedSymbol {
  110. dword address;
  111. const KSym* ksym;
  112. };
  113. Vector<RecognizedSymbol> recognizedSymbols;
  114. if (use_ksyms) {
  115. for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  116. dword retaddr = stackPtr[1];
  117. if (auto* ksym = ksymbolicate(retaddr))
  118. recognizedSymbols.append({ retaddr, ksym });
  119. }
  120. } else{
  121. for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  122. dword retaddr = stackPtr[1];
  123. kprintf("%x (next: %x)\n", retaddr, stackPtr ? (dword*)*stackPtr : 0);
  124. }
  125. return;
  126. }
  127. size_t bytesNeeded = 0;
  128. for (auto& symbol : recognizedSymbols) {
  129. bytesNeeded += symbol.ksym->name.length() + 8 + 16;
  130. }
  131. for (auto& symbol : recognizedSymbols) {
  132. unsigned offset = symbol.address - symbol.ksym->address;
  133. dbgprintf("%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
  134. }
  135. }
  136. #endif
  137. #ifdef STRESS_TEST_SPAWNING
  138. static void spawn_stress() NORETURN;
  139. static void spawn_stress()
  140. {
  141. dword lastAlloc = sum_alloc;
  142. for (unsigned i = 0; i < 10000; ++i) {
  143. int error;
  144. Process::create_user_process("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty0);
  145. kprintf("malloc stats: alloc:%u free:%u page_aligned:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_page_aligned, kmalloc_sum_eternal);
  146. kprintf("delta:%u\n", sum_alloc - lastAlloc);
  147. lastAlloc = sum_alloc;
  148. sleep(60);
  149. }
  150. for (;;) {
  151. asm volatile("hlt");
  152. }
  153. }
  154. #endif
  155. static void init_stage2() NORETURN;
  156. static void init_stage2()
  157. {
  158. Syscall::initialize();
  159. #ifdef TEST_VFS
  160. auto vfs = make<VirtualFileSystem>();
  161. auto dev_zero = make<ZeroDevice>();
  162. vfs->registerCharacterDevice(*dev_zero);
  163. auto dev_null = make<NullDevice>();
  164. vfs->registerCharacterDevice(*dev_null);
  165. auto dev_full = make<FullDevice>();
  166. vfs->registerCharacterDevice(*dev_full);
  167. auto dev_random = make<RandomDevice>();
  168. vfs->registerCharacterDevice(*dev_random);
  169. vfs->registerCharacterDevice(*keyboard);
  170. vfs->registerCharacterDevice(*tty0);
  171. vfs->registerCharacterDevice(*tty1);
  172. vfs->registerCharacterDevice(*tty2);
  173. vfs->registerCharacterDevice(*tty3);
  174. auto dev_hd0 = IDEDiskDevice::create();
  175. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  176. e2fs->initialize();
  177. vfs->mountRoot(e2fs.copyRef());
  178. #ifdef KSYMS
  179. {
  180. int error;
  181. auto descriptor = vfs->open("/kernel.map", error);
  182. if (!descriptor) {
  183. kprintf("Failed to open /kernel.map\n");
  184. } else {
  185. auto buffer = descriptor->readEntireFile();
  186. ASSERT(buffer);
  187. loadKsyms(buffer);
  188. }
  189. }
  190. #endif
  191. vfs->mount(ProcFileSystem::the(), "/proc");
  192. #endif
  193. #ifdef TEST_ELF_LOADER
  194. {
  195. auto testExecutable = vfs->open("/bin/id");
  196. ASSERT(testExecutable);
  197. auto testExecutableData = testExecutable->readEntireFile();
  198. ASSERT(testExecutableData);
  199. ExecSpace space;
  200. space.loadELF(move(testExecutableData));
  201. auto* elf_entry = space.symbol_ptr("_start");
  202. ASSERT(elf_entry);
  203. typedef int (*MainFunctionPtr)(void);
  204. kprintf("elf_entry: %p\n", elf_entry);
  205. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  206. kprintf("it returned %d\n", rc);
  207. }
  208. #endif
  209. Vector<String> environment;
  210. environment.append("TERM=ansi");
  211. int error;
  212. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), move(environment), tty0);
  213. #ifdef SPAWN_MULTIPLE_SHELLS
  214. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty1);
  215. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty2);
  216. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty3);
  217. #endif
  218. #ifdef STRESS_TEST_SPAWNING
  219. Process::create_kernel_process(spawn_stress, "spawn_stress");
  220. #endif
  221. current->sys$exit(0);
  222. ASSERT_NOT_REACHED();
  223. }
  224. void init() NORETURN;
  225. void init()
  226. {
  227. cli();
  228. #ifdef KSYMS
  229. s_ksyms = nullptr;
  230. s_ksyms_ready = false;
  231. #endif
  232. kmalloc_init();
  233. auto console = make<Console>();
  234. RTC::initialize();
  235. PIC::initialize();
  236. gdt_init();
  237. idt_init();
  238. keyboard = new Keyboard;
  239. VirtualConsole::initialize();
  240. tty0 = new VirtualConsole(0, VirtualConsole::AdoptCurrentVGABuffer);
  241. tty1 = new VirtualConsole(1);
  242. tty2 = new VirtualConsole(2);
  243. tty3 = new VirtualConsole(3);
  244. VirtualConsole::switch_to(0);
  245. kprintf("Starting Serenity Operating System...\n");
  246. MemoryManager::initialize();
  247. VirtualFileSystem::initializeGlobals();
  248. StringImpl::initializeGlobals();
  249. PIT::initialize();
  250. memset(&system, 0, sizeof(system));
  251. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  252. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  253. kprintf("%u kB base memory\n", base_memory);
  254. kprintf("%u kB extended memory\n", ext_memory);
  255. auto procfs = ProcFileSystem::create();
  256. procfs->initialize();
  257. Process::initialize();
  258. Process::create_kernel_process(init_stage2, "init_stage2");
  259. Scheduler::pick_next();
  260. sti();
  261. // This now becomes the idle process :^)
  262. for (;;) {
  263. asm("hlt");
  264. }
  265. }
  266. void log_try_lock(const char* where)
  267. {
  268. kprintf("[%u] >>> locking... (%s)\n", current->pid(), where);
  269. }
  270. void log_locked(const char* where)
  271. {
  272. kprintf("[%u] >>> locked() in %s\n", current->pid(), where);
  273. }
  274. void log_unlocked(const char* where)
  275. {
  276. kprintf("[%u] <<< unlocked()\n", current->pid(), where);
  277. }