main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "driver/multiboot2/multiboot2.h"
  19. #include "driver/acpi/acpi.h"
  20. #include "driver/keyboard/ps2_keyboard.h"
  21. #include "driver/mouse/ps2_mouse.h"
  22. #include "driver/disk/ata.h"
  23. #include "driver/pci/pci.h"
  24. #include "driver/disk/ahci/ahci.h"
  25. #include <driver/timers/rtc/rtc.h>
  26. #include <driver/timers/HPET/HPET.h>
  27. #include <driver/timers/timer.h>
  28. unsigned int *FR_address = (unsigned int *)0xb8000; //帧缓存区的地址
  29. struct memory_desc memory_management_struct = {{0}, 0};
  30. // struct Global_Memory_Descriptor memory_management_struct = {{0}, 0};
  31. void test_slab();
  32. void show_welcome()
  33. {
  34. /**
  35. * @brief 打印欢迎页面
  36. *
  37. */
  38. printk("\n\n");
  39. for (int i = 0; i < 74; ++i)
  40. printk(" ");
  41. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n");
  42. for (int i = 0; i < 74; ++i)
  43. printk(" ");
  44. printk_color(BLACK, 0x00e0ebeb, " Welcome to DragonOS ! \n");
  45. for (int i = 0; i < 74; ++i)
  46. printk(" ");
  47. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n");
  48. }
  49. // 测试内存管理单元
  50. void test_mm()
  51. {
  52. kinfo("Testing memory management unit...");
  53. struct Page *page = NULL;
  54. page = alloc_pages(ZONE_NORMAL, 63, 0);
  55. page = alloc_pages(ZONE_NORMAL, 63, 0);
  56. printk_color(ORANGE, BLACK, "4.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tmemory_management_struct.bmp+2:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1), *(memory_management_struct.bmp + 2), memory_management_struct.zones_struct->count_pages_using, memory_management_struct.zones_struct->count_pages_free);
  57. for (int i = 80; i <= 85; ++i)
  58. {
  59. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  60. i++;
  61. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  62. }
  63. for (int i = 140; i <= 145; i++)
  64. {
  65. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  66. i++;
  67. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  68. }
  69. free_pages(page, 1);
  70. printk_color(ORANGE, BLACK, "5.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tmemory_management_struct.bmp+2:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *memory_management_struct.bmp, *(memory_management_struct.bmp + 1), *(memory_management_struct.bmp + 2), memory_management_struct.zones_struct->count_pages_using, memory_management_struct.zones_struct->count_pages_free);
  71. for (int i = 75; i <= 85; i++)
  72. {
  73. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\t", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  74. i++;
  75. printk_color(INDIGO, BLACK, "page%03d attr:%#018lx address:%#018lx\n", i, (memory_management_struct.pages_struct + i)->attr, (memory_management_struct.pages_struct + i)->addr_phys);
  76. }
  77. page = alloc_pages(ZONE_UNMAPPED_IN_PGT, 63, 0);
  78. printk_color(ORANGE, BLACK, "6.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *(memory_management_struct.bmp + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), *(memory_management_struct.bmp + 1 + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_using, (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_free);
  79. free_pages(page, 1);
  80. printk_color(ORANGE, BLACK, "7.memory_management_struct.bmp:%#018lx\tmemory_management_struct.bmp+1:%#018lx\tzone_struct->count_pages_using:%d\tzone_struct->count_pages_free:%d\n", *(memory_management_struct.bmp + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), *(memory_management_struct.bmp + 1 + (page->addr_phys >> PAGE_2M_SHIFT >> 6)), (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_using, (memory_management_struct.zones_struct + ZONE_UNMAPPED_INDEX)->count_pages_free);
  81. test_slab();
  82. kinfo("Memory management module test completed!");
  83. }
  84. void test_slab()
  85. {
  86. kinfo("Testing SLAB...");
  87. kinfo("Testing kmalloc()...");
  88. for (int i = 1; i < 16; ++i)
  89. {
  90. printk_color(ORANGE, BLACK, "mem_obj_size: %ldbytes\t", kmalloc_cache_group[i].size);
  91. printk_color(ORANGE, BLACK, "bmp(before): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  92. ul *tmp = kmalloc(kmalloc_cache_group[i].size, 0);
  93. if (tmp == NULL)
  94. {
  95. kBUG("Cannot kmalloc such a memory: %ld bytes", kmalloc_cache_group[i].size);
  96. }
  97. printk_color(ORANGE, BLACK, "bmp(middle): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  98. kfree(tmp);
  99. printk_color(ORANGE, BLACK, "bmp(after): %#018lx\n", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  100. }
  101. // 测试自动扩容
  102. void *ptrs[7];
  103. for (int i = 0; i < 7; ++i)
  104. ptrs[i] = kmalloc(kmalloc_cache_group[15].size, 0);
  105. struct slab_obj *slab_obj_ptr = kmalloc_cache_group[15].cache_pool_entry;
  106. int count = 0;
  107. do
  108. {
  109. kdebug("bmp(%d): addr=%#018lx\t value=%#018lx", count, slab_obj_ptr->bmp, *slab_obj_ptr->bmp);
  110. slab_obj_ptr = container_of(list_next(&slab_obj_ptr->list), struct slab_obj, list);
  111. ++count;
  112. } while (slab_obj_ptr != kmalloc_cache_group[15].cache_pool_entry);
  113. for (int i = 0; i < 7; ++i)
  114. kfree(ptrs[i]);
  115. kinfo("SLAB test completed!");
  116. }
  117. // 初始化系统各模块
  118. void system_initialize()
  119. {
  120. // 初始化printk
  121. printk_init(8, 16);
  122. kinfo("Kernel Starting...");
  123. load_TR(10); // 加载TR寄存器
  124. ul tss_item_addr = 0x7c00;
  125. set_tss64(TSS64_Table, _stack_start, _stack_start, _stack_start, tss_item_addr,
  126. tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
  127. cpu_core_info[0].stack_start = _stack_start;
  128. cpu_core_info[0].tss_vaddr = &TSS64_Table;
  129. // 初始化中断描述符表
  130. sys_vector_init();
  131. // 初始化内存管理单元
  132. mm_init();
  133. acpi_init();
  134. for (int i = 0; i < 1e7; ++i)
  135. ;
  136. // 初始化中断模块
  137. irq_init();
  138. softirq_init();
  139. timer_init();
  140. HPET_init();
  141. smp_init();
  142. // 先初始化系统调用模块
  143. syscall_init();
  144. cpu_init();
  145. // ps2_keyboard_init();
  146. // ps2_mouse_init();
  147. // ata_init();
  148. pci_init();
  149. ahci_init();
  150. // test_slab();
  151. // test_mm();
  152. // 再初始化进程模块。顺序不能调转
  153. // sched_init();
  154. // process_init();
  155. }
  156. //操作系统内核从这里开始执行
  157. void Start_Kernel(void)
  158. {
  159. // 获取multiboot2的信息
  160. uint64_t mb2_info, mb2_magic;
  161. __asm__ __volatile__("movq %%r15, %0 \n\t"
  162. "movq %%r14, %1 \n\t"
  163. : "=r"(mb2_info), "=r"(mb2_magic)::"memory");
  164. mb2_info &= 0xffffffff;
  165. mb2_magic &= 0xffffffff;
  166. multiboot2_magic = mb2_magic;
  167. multiboot2_boot_info_addr = mb2_info+PAGE_OFFSET;
  168. system_initialize();
  169. /*
  170. uint64_t buf[100];
  171. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, 0, 1, (uint64_t)&buf, 0, 0);
  172. kdebug("buf[0]=%#010lx",(uint32_t)buf[0]);
  173. buf[0] = 0xffd3;
  174. ahci_operation.transfer(ATA_CMD_WRITE_DMA_EXT, 0, 1, (uint64_t)&buf, 0, 0);
  175. ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, 0, 1, (uint64_t)&buf, 0, 0);
  176. kdebug("buf[0]=%#010lx",(uint32_t)buf[0]);
  177. */
  178. // show_welcome();
  179. // test_mm();
  180. /*
  181. while (1)
  182. {
  183. ps2_keyboard_analyze_keycode();
  184. struct ps2_mouse_packet_3bytes packet = {0};
  185. // struct ps2_mouse_packet_4bytes packet = {0};
  186. int errcode = 0;
  187. errcode = ps2_mouse_get_packet(&packet);
  188. if (errcode == 0)
  189. {
  190. printk_color(GREEN, BLACK, " (Mouse: byte0:%d, x:%3d, y:%3d)\n", packet.byte0, packet.movement_x, packet.movement_y);
  191. // 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);
  192. }
  193. }
  194. */
  195. /*
  196. while (1)
  197. {
  198. keyboard_analyze_keycode();
  199. analyze_mousecode();
  200. }
  201. */
  202. // ipi_send_IPI(DEST_PHYSICAL, IDLE, ICR_LEVEL_DE_ASSERT, EDGE_TRIGGER, 0xc8, ICR_APIC_FIXED, ICR_No_Shorthand, true, 1); // 测试ipi
  203. int last_sec = rtc_now.second;
  204. /*
  205. while (1)
  206. {
  207. if (last_sec != rtc_now.second)
  208. {
  209. last_sec = rtc_now.second;
  210. 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);
  211. }
  212. }
  213. */
  214. while (1)
  215. hlt();
  216. }
  217. void ignore_int()
  218. {
  219. kwarn("Unknown interrupt or fault at RIP.\n");
  220. return;
  221. }