init.cpp 5.7 KB

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