init.cpp 4.2 KB

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