init.cpp 5.8 KB

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