init.cpp 5.1 KB

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