main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Created by longjin on 2022/1/20.
  3. //
  4. #include "common/glib.h"
  5. #include "common/printk.h"
  6. #include "common/kprint.h"
  7. #include "exception/gate.h"
  8. #include "exception/trap.h"
  9. #include "exception/irq.h"
  10. #include <exception/softirq.h>
  11. #include "mm/mm.h"
  12. #include "mm/slab.h"
  13. #include "process/process.h"
  14. #include "syscall/syscall.h"
  15. #include "smp/smp.h"
  16. #include <smp/ipi.h>
  17. #include <sched/sched.h>
  18. #include <filesystem/fat32/fat32.h>
  19. #include "driver/multiboot2/multiboot2.h"
  20. #include "driver/acpi/acpi.h"
  21. #include "driver/keyboard/ps2_keyboard.h"
  22. #include "driver/mouse/ps2_mouse.h"
  23. #include "driver/disk/ata.h"
  24. #include "driver/pci/pci.h"
  25. #include "driver/disk/ahci/ahci.h"
  26. #include <driver/timers/rtc/rtc.h>
  27. #include <driver/timers/HPET/HPET.h>
  28. #include <driver/timers/timer.h>
  29. #include <driver/uart/uart.h>
  30. unsigned int *FR_address = (unsigned int *)0xb8000; //帧缓存区的地址
  31. ul bsp_idt_size, bsp_gdt_size;
  32. struct memory_desc memory_management_struct = {{0}, 0};
  33. // struct Global_Memory_Descriptor memory_management_struct = {{0}, 0};
  34. void test_slab();
  35. void show_welcome()
  36. {
  37. /**
  38. * @brief 打印欢迎页面
  39. *
  40. */
  41. printk("\n\n");
  42. for (int i = 0; i < 74; ++i)
  43. printk(" ");
  44. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n");
  45. for (int i = 0; i < 74; ++i)
  46. printk(" ");
  47. printk_color(BLACK, 0x00e0ebeb, " Welcome to DragonOS ! \n");
  48. for (int i = 0; i < 74; ++i)
  49. printk(" ");
  50. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n");
  51. }
  52. struct gdtr gdtp;
  53. struct idtr idtp;
  54. void reload_gdt()
  55. {
  56. gdtp.size = bsp_gdt_size - 1;
  57. gdtp.gdt_vaddr = (ul)phys_2_virt((ul)&GDT_Table);
  58. asm volatile("lgdt (%0) \n\t" ::"r"(&gdtp)
  59. : "memory");
  60. }
  61. void reload_idt()
  62. {
  63. idtp.size = bsp_idt_size - 1;
  64. idtp.idt_vaddr = (ul)phys_2_virt((ul)&IDT_Table);
  65. // kdebug("gdtvaddr=%#018lx", p.gdt_vaddr);
  66. // kdebug("gdt size=%d", p.size);
  67. asm volatile("lidt (%0) \n\t" ::"r"(&idtp)
  68. : "memory");
  69. }
  70. // 初始化系统各模块
  71. void system_initialize()
  72. {
  73. // 初始化printk
  74. printk_init(8, 16);
  75. #ifdef DEBUG
  76. uart_init(COM1, 115200);
  77. #endif
  78. kinfo("Kernel Starting...");
  79. // 重新加载gdt和idt
  80. ul tss_item_addr = (ul)phys_2_virt(0x7c00);
  81. _stack_start = head_stack_start; // 保存init proc的栈基地址(由于之后取消了地址重映射,因此必须在这里重新保存)
  82. kdebug("_stack_start=%#018lx", _stack_start);
  83. load_TR(10); // 加载TR寄存器
  84. set_tss64((uint *)&initial_tss[0], _stack_start, _stack_start, _stack_start, tss_item_addr,
  85. tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
  86. cpu_core_info[0].stack_start = _stack_start;
  87. cpu_core_info[0].tss_vaddr = (uint64_t)&initial_tss[0];
  88. kdebug("cpu_core_info[0].tss_vaddr=%#018lx", cpu_core_info[0].tss_vaddr);
  89. kdebug("cpu_core_info[0].stack_start%#018lx", cpu_core_info[0].stack_start);
  90. // 初始化中断描述符表
  91. sys_vector_init();
  92. // 初始化内存管理单元
  93. mm_init();
  94. // =========== 重新设置initial_tss[0]的ist
  95. uchar *ptr = (uchar *)kmalloc(STACK_SIZE, 0) + STACK_SIZE;
  96. ((struct process_control_block *)(ptr - STACK_SIZE))->cpu_id = 0;
  97. initial_tss[0].ist1 = (ul)ptr;
  98. initial_tss[0].ist2 = (ul)ptr;
  99. initial_tss[0].ist3 = (ul)ptr;
  100. initial_tss[0].ist4 = (ul)ptr;
  101. initial_tss[0].ist5 = (ul)ptr;
  102. initial_tss[0].ist6 = (ul)ptr;
  103. initial_tss[0].ist7 = (ul)ptr;
  104. // ===========================
  105. acpi_init();
  106. // 初始化中断模块
  107. sched_init();
  108. irq_init();
  109. softirq_init();
  110. // 先初始化系统调用模块
  111. syscall_init();
  112. // 再初始化进程模块。顺序不能调转
  113. sched_init();
  114. timer_init();
  115. smp_init();
  116. cpu_init();
  117. // ps2_keyboard_init();
  118. // ps2_mouse_init();
  119. // ata_init();
  120. pci_init();
  121. ahci_init();
  122. fat32_init();
  123. // test_slab();
  124. // test_mm();
  125. // process_init();
  126. current_pcb->cpu_id = 0;
  127. current_pcb->preempt_count = 0;
  128. process_init();
  129. HPET_init();
  130. }
  131. //操作系统内核从这里开始执行
  132. void Start_Kernel(void)
  133. {
  134. // 获取multiboot2的信息
  135. uint64_t mb2_info, mb2_magic;
  136. __asm__ __volatile__("movq %%r15, %0 \n\t"
  137. "movq %%r14, %1 \n\t"
  138. "movq %%r13, %2 \n\t"
  139. "movq %%r12, %3 \n\t"
  140. : "=r"(mb2_info), "=r"(mb2_magic), "=r"(bsp_gdt_size), "=r"(bsp_idt_size)::"memory");
  141. reload_gdt();
  142. reload_idt();
  143. // 重新设置TSS描述符
  144. set_tss_descriptor(10, (void *)(&initial_tss[0]));
  145. mb2_info &= 0xffffffff;
  146. mb2_magic &= 0xffffffff;
  147. multiboot2_magic = (uint)mb2_magic;
  148. multiboot2_boot_info_addr = mb2_info + PAGE_OFFSET;
  149. system_initialize();
  150. int part_id = fat32_register_partition(0, 0, 0);
  151. struct fat32_Directory_t *dentry = fat32_path_walk(part_id, "a.txt", 0);
  152. if (dentry != NULL)
  153. printk_color(BLUE, BLACK, "Find a.txt\nDIR_FstClusHI:%#018lx\tDIR_FstClusLO:%#018lx\tDIR_FileSize:%#018lx\n", dentry->DIR_FstClusHI, dentry->DIR_FstClusLO, dentry->DIR_FileSize);
  154. else
  155. printk_color(BLUE, BLACK, "Can`t find file\n");
  156. dentry = fat32_path_walk(part_id, "xx/12.png", 0);
  157. if (dentry != NULL)
  158. printk_color(BLUE, BLACK, "Find xx/12.png\nDIR_FstClusHI:%#018lx\tDIR_FstClusLO:%#018lx\tDIR_FileSize:%#018lx\n", dentry->DIR_FstClusHI, dentry->DIR_FstClusLO, dentry->DIR_FileSize);
  159. else
  160. printk_color(BLUE, BLACK, "Can`t find file\n");
  161. // show_welcome();
  162. // test_mm();
  163. /*
  164. while (1)
  165. {
  166. ps2_keyboard_analyze_keycode();
  167. struct ps2_mouse_packet_3bytes packet = {0};
  168. // struct ps2_mouse_packet_4bytes packet = {0};
  169. int errcode = 0;
  170. errcode = ps2_mouse_get_packet(&packet);
  171. if (errcode == 0)
  172. {
  173. printk_color(GREEN, BLACK, " (Mouse: byte0:%d, x:%3d, y:%3d)\n", packet.byte0, packet.movement_x, packet.movement_y);
  174. // printk_color(GREEN, BLACK, " (Mouse: byte0:%d, x:%3d, y:%3d, byte3:%3d)\n", packet.byte0, packet.movement_x, packet.movement_y, (unsigned char)packet.byte3);
  175. }
  176. }
  177. */
  178. /*
  179. while (1)
  180. {
  181. keyboard_analyze_keycode();
  182. analyze_mousecode();
  183. }
  184. */
  185. // ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0xc8, ICR_APIC_FIXED, ICR_No_Shorthand, true, 1); // 测试ipi
  186. // int last_sec = rtc_now.second;
  187. /*
  188. while (1)
  189. {
  190. if (last_sec != rtc_now.second)
  191. {
  192. last_sec = rtc_now.second;
  193. kinfo("Current Time: %04d/%02d/%02d %02d:%02d:%02d", rtc_now.year, rtc_now.month, rtc_now.day, rtc_now.hour, rtc_now.minute, rtc_now.second);
  194. }
  195. }
  196. */
  197. while (1)
  198. hlt();
  199. }
  200. void ignore_int()
  201. {
  202. kwarn("Unknown interrupt or fault at RIP.\n");
  203. return;
  204. }