init.cpp 5.7 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. #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. static void user_kprintf_main() NORETURN;
  90. static void user_kprintf_main()
  91. {
  92. DO_SYSCALL_A1(0x4000, 0);
  93. kprintf("This should not work!\n");
  94. HANG;
  95. for (;;) {
  96. }
  97. }
  98. system_t system;
  99. void banner()
  100. {
  101. kprintf("\n");
  102. vga_set_attr(0x0a);
  103. kprintf(" _____ _ _ \n");
  104. vga_set_attr(0x0b);
  105. kprintf("| __|___ ___| |_ ___ ___| |_ \n");
  106. vga_set_attr(0x0c);
  107. kprintf("| | | -_| _| . | -_| _| _|\n");
  108. vga_set_attr(0x0d);
  109. kprintf("|_____|___|_| |___|___|_| |_| \n");
  110. vga_set_attr(0x07);
  111. kprintf("\n");
  112. }
  113. void init()
  114. {
  115. cli();
  116. kmalloc_init();
  117. vga_init();
  118. auto console = make<Console>();
  119. PIC::initialize();
  120. gdt_init();
  121. idt_init();
  122. MemoryManager::initialize();
  123. // Anything that registers interrupts goes *after* PIC and IDT for obvious reasons.
  124. Syscall::initialize();
  125. PIT::initialize();
  126. Keyboard::initialize();
  127. Task::initialize();
  128. VirtualFileSystem::initializeGlobals();
  129. memset(&system, 0, sizeof(system));
  130. WORD base_memory = (CMOS::read(0x16) << 8) | CMOS::read(0x15);
  131. WORD ext_memory = (CMOS::read(0x18) << 8) | CMOS::read(0x17);
  132. kprintf("%u kB base memory\n", base_memory);
  133. kprintf("%u kB extended memory\n", ext_memory);
  134. extern void panel_main();
  135. new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0);
  136. //new Task(led_disco, "led-disco", IPC::Handle::Any, Task::Ring0);
  137. scheduleNewTask();
  138. banner();
  139. sti();
  140. Disk::initialize();
  141. #ifdef TEST_VFS
  142. auto vfs = make<VirtualFileSystem>();
  143. auto dev_zero = make<ZeroDevice>();
  144. vfs->registerCharacterDevice(1, 3, *dev_zero);
  145. auto dev_null = make<NullDevice>();
  146. vfs->registerCharacterDevice(1, 5, *dev_zero);
  147. auto dev_full = make<FullDevice>();
  148. vfs->registerCharacterDevice(1, 7, *dev_full);
  149. auto dev_random = make<RandomDevice>();
  150. vfs->registerCharacterDevice(1, 8, *dev_random);
  151. auto dev_hd0 = IDEDiskDevice::create();
  152. auto e2fs = Ext2FileSystem::create(dev_hd0.copyRef());
  153. e2fs->initialize();
  154. vfs->mountRoot(e2fs.copyRef());
  155. //vfs->listDirectory("/");
  156. {
  157. auto motdFile = vfs->open("/motd.txt");
  158. ASSERT(motdFile);
  159. auto motdData = motdFile->readEntireFile();
  160. for (unsigned i = 0; i < motdData.size(); ++i) {
  161. kprintf("%c", motdData[i]);
  162. }
  163. }
  164. #endif
  165. #ifdef TEST_CRASHY_USER_PROCESSES
  166. new Task(user_main, "user", IPC::Handle::UserTask, Task::Ring3);
  167. new Task(user_kprintf_main, "user_kprintf", IPC::Handle::UserTask, Task::Ring3);
  168. #endif
  169. #ifdef TEST_ELF_LOADER
  170. {
  171. auto testExecutable = vfs->open("/_hello.o");
  172. ASSERT(testExecutable);
  173. auto testExecutableData = testExecutable->readEntireFile();
  174. ASSERT(testExecutableData);
  175. ExecSpace space;
  176. space.loadELF(move(testExecutableData));
  177. auto* elf_entry = space.symbolPtr("elf_entry");
  178. ASSERT(elf_entry);
  179. typedef int (*MainFunctionPtr)(void);
  180. kprintf("elf_entry: %p\n", elf_entry);
  181. int rc = reinterpret_cast<MainFunctionPtr>(elf_entry)();
  182. kprintf("it returned %d\n", rc);
  183. }
  184. #endif
  185. //new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0);
  186. //new Task(syscall_test_main, "syscall_test", IPC::Handle::MotdTask, Task::Ring3);
  187. // The idle task will spend its eternity here for now.
  188. for (;;) {
  189. asm("hlt");
  190. }
  191. }