init.cpp 8.3 KB

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