init.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "Task.h"
  8. #include "IPC.h"
  9. #include "system.h"
  10. #include "Disk.h"
  11. #include "PIC.h"
  12. #include "StdLib.h"
  13. #include "Syscall.h"
  14. #include "CMOS.h"
  15. #include "Userspace.h"
  16. #include "IDEDiskDevice.h"
  17. #include <VirtualFileSystem/NullDevice.h>
  18. #include <VirtualFileSystem/ZeroDevice.h>
  19. #include <VirtualFileSystem/FullDevice.h>
  20. #include <VirtualFileSystem/RandomDevice.h>
  21. #include <VirtualFileSystem/Ext2FileSystem.h>
  22. #include <VirtualFileSystem/VirtualFileSystem.h>
  23. #include <VirtualFileSystem/FileHandle.h>
  24. #include <AK/OwnPtr.h>
  25. #include "MemoryManager.h"
  26. #include <ELFLoader/ELFLoader.h>
  27. #include "Console.h"
  28. #include "ProcFileSystem.h"
  29. #define TEST_VFS
  30. //#define TEST_ELF_LOADER
  31. //#define TEST_CRASHY_USER_PROCESSES
  32. static void motd_main() NORETURN;
  33. static void motd_main()
  34. {
  35. kprintf("Hello in motd_main!\n");
  36. int fd = Userspace::open("/test.asm");
  37. kprintf("motd: fd=%d\n", fd);
  38. ASSERT(fd != -1);
  39. DO_SYSCALL_A3(0x2000, 1, 2, 3);
  40. kprintf("getuid(): %u\n", Userspace::getuid());
  41. auto buffer = DataBuffer::createUninitialized(33);
  42. memset(buffer->data(), 0, buffer->length());
  43. int nread = Userspace::read(fd, buffer->data(), buffer->length() - 1);
  44. kprintf("read(): %d\n", nread);
  45. buffer->data()[nread] = 0;
  46. kprintf("read(): '%s'\n", buffer->data());
  47. for (;;) {
  48. //kill(4, 5);
  49. sleep(1 * TICKS_PER_SECOND);
  50. }
  51. }
  52. static void syscall_test_main() NORETURN;
  53. static void syscall_test_main()
  54. {
  55. kprintf("Hello in syscall_test_main!\n");
  56. for (;;) {
  57. Userspace::getuid();
  58. // Userspace::yield();
  59. //kprintf("getuid(): %u\n", Userspace::getuid());
  60. sleep(1 * TICKS_PER_SECOND);
  61. }
  62. }
  63. static void user_main() NORETURN;
  64. static void user_main()
  65. {
  66. DO_SYSCALL_A3(0x3000, 2, 3, 4);
  67. // Crash ourselves!
  68. char* x = reinterpret_cast<char*>(0xbeefbabe);
  69. *x = 1;
  70. HANG;
  71. for (;;) {
  72. // nothing?
  73. Userspace::sleep(1 * TICKS_PER_SECOND);
  74. }
  75. }
  76. system_t system;
  77. void banner()
  78. {
  79. kprintf("\n");
  80. vga_set_attr(0x0a);
  81. kprintf(" _____ _ _ \n");
  82. vga_set_attr(0x0b);
  83. kprintf("| __|___ ___| |_ ___ ___| |_ \n");
  84. vga_set_attr(0x0c);
  85. kprintf("| | | -_| _| . | -_| _| _|\n");
  86. vga_set_attr(0x0d);
  87. kprintf("|_____|___|_| |___|___|_| |_| \n");
  88. vga_set_attr(0x07);
  89. kprintf("\n");
  90. }
  91. static void init_stage2() NORETURN;
  92. static void init_stage2()
  93. {
  94. kprintf("init stage2...\n");
  95. Syscall::initialize();
  96. auto keyboard = make<Keyboard>();
  97. extern void panel_main();
  98. new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0);
  99. //new Task(led_disco, "led-disco", IPC::Handle::Any, Task::Ring0);
  100. Disk::initialize();
  101. #ifdef TEST_VFS
  102. auto vfs = make<VirtualFileSystem>();
  103. auto dev_zero = make<ZeroDevice>();
  104. vfs->registerCharacterDevice(1, 3, *dev_zero);
  105. auto dev_null = make<NullDevice>();
  106. vfs->registerCharacterDevice(1, 5, *dev_zero);
  107. auto dev_full = make<FullDevice>();
  108. vfs->registerCharacterDevice(1, 7, *dev_full);
  109. auto dev_random = make<RandomDevice>();
  110. vfs->registerCharacterDevice(1, 8, *dev_random);
  111. vfs->registerCharacterDevice(85, 1, *keyboard);
  112. auto dev_hd0 = IDEDiskDevice::create();
  113. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  114. e2fs->initialize();
  115. vfs->mountRoot(e2fs.copyRef());
  116. auto procfs = ProcFileSystem::create();
  117. procfs->initialize();
  118. vfs->mount(procfs.copyRef(), "/proc");
  119. {
  120. auto motdFile = vfs->open("/motd.txt");
  121. ASSERT(motdFile);
  122. auto motdData = motdFile->readEntireFile();
  123. for (unsigned i = 0; i < motdData.size(); ++i) {
  124. kprintf("%c", motdData[i]);
  125. }
  126. }
  127. #endif
  128. #ifdef TEST_CRASHY_USER_PROCESSES
  129. new Task(user_main, "user", IPC::Handle::UserTask, Task::Ring3);
  130. #endif
  131. #ifdef TEST_ELF_LOADER
  132. {
  133. auto testExecutable = vfs->open("/bin/id");
  134. ASSERT(testExecutable);
  135. auto testExecutableData = testExecutable->readEntireFile();
  136. ASSERT(testExecutableData);
  137. ExecSpace space;
  138. space.loadELF(move(testExecutableData));
  139. auto* elf_entry = space.symbolPtr("_start");
  140. ASSERT(elf_entry);
  141. typedef int (*MainFunctionPtr)(void);
  142. kprintf("elf_entry: %p\n", elf_entry);
  143. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  144. kprintf("it returned %d\n", rc);
  145. }
  146. #endif
  147. auto* shTask = Task::create("/bin/sh", (uid_t)100, (gid_t)100);
  148. //new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0);
  149. //new Task(syscall_test_main, "syscall_test", IPC::Handle::MotdTask, Task::Ring3);
  150. kprintf("init stage2 is done!\n");
  151. #if 0
  152. // It would be nice to exit this process, but right now it instantiates all kinds of things.
  153. // At the very least it needs to be made sure those things stick around as appropriate.
  154. DO_SYSCALL_A1(Syscall::PosixExit, 413);
  155. kprintf("uh, we're still going after calling sys$exit...\n");
  156. HANG;
  157. #endif
  158. for (;;) {
  159. asm("hlt");
  160. }
  161. }
  162. void init()
  163. {
  164. cli();
  165. kmalloc_init();
  166. vga_init();
  167. auto console = make<Console>();
  168. PIC::initialize();
  169. gdt_init();
  170. idt_init();
  171. MemoryManager::initialize();
  172. VirtualFileSystem::initializeGlobals();
  173. StringImpl::initializeGlobals();
  174. PIT::initialize();
  175. memset(&system, 0, sizeof(system));
  176. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  177. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  178. kprintf("%u kB base memory\n", base_memory);
  179. kprintf("%u kB extended memory\n", ext_memory);
  180. Task::initialize();
  181. auto* init2 = new Task(init_stage2, "init", IPC::Handle::InitTask, Task::Ring0);
  182. scheduleNewTask();
  183. sti();
  184. // This now becomes the idle task :^)
  185. for (;;) {
  186. asm("hlt");
  187. }
  188. }