init.cpp 8.5 KB

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