init.cpp 5.8 KB

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