init.cpp 8.8 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/FileDescriptor.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. #include "Scheduler.h"
  30. #define TEST_VFS
  31. #define KSYMS
  32. #define SPAWN_MULTIPLE_SHELLS
  33. //#define STRESS_TEST_SPAWNING
  34. //#define TEST_ELF_LOADER
  35. system_t system;
  36. VirtualConsole* tty0;
  37. VirtualConsole* tty1;
  38. VirtualConsole* tty2;
  39. VirtualConsole* tty3;
  40. Keyboard* keyboard;
  41. static byte parseHexDigit(char nibble)
  42. {
  43. if (nibble >= '0' && nibble <= '9')
  44. return nibble - '0';
  45. ASSERT(nibble >= 'a' && nibble <= 'f');
  46. return 10 + (nibble - 'a');
  47. }
  48. #ifdef KSYMS
  49. static Vector<KSym, KmallocEternalAllocator>* s_ksyms;
  50. static bool s_ksyms_ready;
  51. Vector<KSym, KmallocEternalAllocator>& ksyms()
  52. {
  53. return *s_ksyms;
  54. }
  55. volatile bool ksyms_ready()
  56. {
  57. return s_ksyms_ready;
  58. }
  59. const KSym* ksymbolicate(dword address)
  60. {
  61. if (address < ksyms().first().address || address > ksyms().last().address)
  62. return nullptr;
  63. for (unsigned i = 0; i < ksyms().size(); ++i) {
  64. if (address < ksyms()[i + 1].address)
  65. return &ksyms()[i];
  66. }
  67. return nullptr;
  68. }
  69. static void loadKsyms(const ByteBuffer& buffer)
  70. {
  71. // FIXME: It's gross that this vector grows dynamically rather than being sized-to-fit.
  72. // We're wasting that eternal kmalloc memory.
  73. s_ksyms = new Vector<KSym, KmallocEternalAllocator>;
  74. auto* bufptr = (const char*)buffer.pointer();
  75. auto* startOfName = bufptr;
  76. dword address = 0;
  77. while (bufptr < buffer.endPointer()) {
  78. for (unsigned i = 0; i < 8; ++i)
  79. address = (address << 4) | parseHexDigit(*(bufptr++));
  80. bufptr += 3;
  81. startOfName = bufptr;
  82. while (*(++bufptr)) {
  83. if (*bufptr == '\n') {
  84. break;
  85. }
  86. }
  87. // FIXME: The Strings here should be eternally allocated too.
  88. ksyms().append({ address, String(startOfName, bufptr - startOfName) });
  89. ++bufptr;
  90. }
  91. s_ksyms_ready = true;
  92. }
  93. void dump_backtrace(bool use_ksyms)
  94. {
  95. if (!current) {
  96. HANG;
  97. return;
  98. }
  99. extern volatile bool ksyms_ready();
  100. if (use_ksyms && !ksyms_ready()) {
  101. HANG;
  102. return;
  103. }
  104. struct RecognizedSymbol {
  105. dword address;
  106. const KSym* ksym;
  107. };
  108. Vector<RecognizedSymbol> recognizedSymbols;
  109. if (use_ksyms) {
  110. for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  111. dword retaddr = stackPtr[1];
  112. if (auto* ksym = ksymbolicate(retaddr))
  113. recognizedSymbols.append({ retaddr, ksym });
  114. }
  115. } else{
  116. for (dword* stackPtr = (dword*)&use_ksyms; current->isValidAddressForKernel(LinearAddress((dword)stackPtr)); stackPtr = (dword*)*stackPtr) {
  117. dword retaddr = stackPtr[1];
  118. kprintf("%x (next: %x)\n", retaddr, stackPtr ? (dword*)*stackPtr : 0);
  119. }
  120. return;
  121. }
  122. size_t bytesNeeded = 0;
  123. for (auto& symbol : recognizedSymbols) {
  124. bytesNeeded += symbol.ksym->name.length() + 8 + 16;
  125. }
  126. for (auto& symbol : recognizedSymbols) {
  127. unsigned offset = symbol.address - symbol.ksym->address;
  128. dbgprintf("%p %s +%u\n", symbol.address, symbol.ksym->name.characters(), offset);
  129. }
  130. }
  131. #endif
  132. static void undertaker_main() NORETURN;
  133. static void undertaker_main()
  134. {
  135. for (;;) {
  136. Process::doHouseKeeping();
  137. sleep(300);
  138. }
  139. }
  140. static void spawn_stress() NORETURN;
  141. static void spawn_stress()
  142. {
  143. dword lastAlloc = sum_alloc;
  144. for (unsigned i = 0; i < 10000; ++i) {
  145. int error;
  146. Process::create_user_process("/bin/id", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), tty0);
  147. // kprintf("malloc stats: alloc:%u free:%u page_aligned:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_page_aligned, kmalloc_sum_eternal);
  148. // kprintf("delta:%u\n", sum_alloc - lastAlloc);
  149. lastAlloc = sum_alloc;
  150. sleep(60);
  151. }
  152. for (;;) {
  153. asm volatile("hlt");
  154. }
  155. }
  156. static void init_stage2() NORETURN;
  157. static void init_stage2()
  158. {
  159. Syscall::initialize();
  160. Disk::initialize();
  161. #ifdef TEST_VFS
  162. auto vfs = make<VirtualFileSystem>();
  163. auto dev_zero = make<ZeroDevice>();
  164. vfs->registerCharacterDevice(*dev_zero);
  165. auto dev_null = make<NullDevice>();
  166. vfs->registerCharacterDevice(*dev_null);
  167. auto dev_full = make<FullDevice>();
  168. vfs->registerCharacterDevice(*dev_full);
  169. auto dev_random = make<RandomDevice>();
  170. vfs->registerCharacterDevice(*dev_random);
  171. vfs->registerCharacterDevice(*keyboard);
  172. vfs->registerCharacterDevice(*tty0);
  173. vfs->registerCharacterDevice(*tty1);
  174. vfs->registerCharacterDevice(*tty2);
  175. vfs->registerCharacterDevice(*tty3);
  176. auto dev_hd0 = IDEDiskDevice::create();
  177. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  178. e2fs->initialize();
  179. vfs->mountRoot(e2fs.copyRef());
  180. #ifdef KSYMS
  181. {
  182. int error;
  183. auto descriptor = vfs->open("/kernel.map", error);
  184. if (!descriptor) {
  185. kprintf("Failed to open /kernel.map\n");
  186. } else {
  187. auto buffer = descriptor->readEntireFile();
  188. ASSERT(buffer);
  189. loadKsyms(buffer);
  190. }
  191. }
  192. #endif
  193. vfs->mount(ProcFileSystem::the(), "/proc");
  194. #endif
  195. #ifdef TEST_ELF_LOADER
  196. {
  197. auto testExecutable = vfs->open("/bin/id");
  198. ASSERT(testExecutable);
  199. auto testExecutableData = testExecutable->readEntireFile();
  200. ASSERT(testExecutableData);
  201. ExecSpace space;
  202. space.loadELF(move(testExecutableData));
  203. auto* elf_entry = space.symbol_ptr("_start");
  204. ASSERT(elf_entry);
  205. typedef int (*MainFunctionPtr)(void);
  206. kprintf("elf_entry: %p\n", elf_entry);
  207. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  208. kprintf("it returned %d\n", rc);
  209. }
  210. #endif
  211. int error;
  212. Process::create_user_process("/bin/sh", (uid_t)100, (gid_t)100, (pid_t)0, error, Vector<String>(), Vector<String>(), 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()
  225. {
  226. cli();
  227. #ifdef KSYMS
  228. s_ksyms = nullptr;
  229. s_ksyms_ready = false;
  230. #endif
  231. kmalloc_init();
  232. vga_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(undertaker_main, "undertaker");
  259. Process::create_kernel_process(init_stage2, "init_stage2");
  260. Scheduler::pick_next();
  261. sti();
  262. // This now becomes the idle process :^)
  263. for (;;) {
  264. asm("hlt");
  265. }
  266. }
  267. void log_try_lock(const char* where)
  268. {
  269. kprintf("[%u] >>> locking... (%s)\n", current->pid(), where);
  270. }
  271. void log_locked(const char* where)
  272. {
  273. kprintf("[%u] >>> locked() in %s\n", current->pid(), where);
  274. }
  275. void log_unlocked(const char* where)
  276. {
  277. kprintf("[%u] <<< unlocked()\n", current->pid(), where);
  278. }