init.cpp 8.6 KB

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