main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "mm/mm.h"
  11. #include "mm/slab.h"
  12. #include "process/process.h"
  13. #include "syscall/syscall.h"
  14. #include "driver/multiboot2/multiboot2.h"
  15. #include "driver/acpi/acpi.h"
  16. #include "driver/keyboard/ps2_keyboard.h"
  17. #include "driver/mouse/ps2_mouse.h"
  18. #include "driver/disk/ata.h"
  19. #include "driver/pci/pci.h"
  20. unsigned int *FR_address = (unsigned int *)0xb8000; //帧缓存区的地址
  21. struct memory_desc memory_management_struct = {{0}, 0};
  22. // struct Global_Memory_Descriptor memory_management_struct = {{0}, 0};
  23. void test_slab();
  24. void show_welcome()
  25. {
  26. /**
  27. * @brief 打印欢迎页面
  28. *
  29. */
  30. printk("\n\n");
  31. for (int i = 0; i < 74; ++i)
  32. printk(" ");
  33. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n");
  34. for (int i = 0; i < 74; ++i)
  35. printk(" ");
  36. printk_color(BLACK, 0x00e0ebeb, " Welcome to DragonOS ! \n");
  37. for (int i = 0; i < 74; ++i)
  38. printk(" ");
  39. printk_color(0x00e0ebeb, 0x00e0ebeb, " \n\n");
  40. }
  41. // 测试内存管理单元
  42. void test_mm()
  43. {
  44. kinfo("Testing memory management unit...");
  45. struct Page *page = NULL;
  46. page = alloc_pages(ZONE_NORMAL, 63, 0);
  47. page = alloc_pages(ZONE_NORMAL, 63, 0);
  48. 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);
  49. for (int i = 80; i <= 85; ++i)
  50. {
  51. 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);
  52. i++;
  53. 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);
  54. }
  55. for (int i = 140; i <= 145; i++)
  56. {
  57. 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);
  58. i++;
  59. 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);
  60. }
  61. free_pages(page, 1);
  62. 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);
  63. for (int i = 75; i <= 85; 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. page = alloc_pages(ZONE_UNMAPPED_IN_PGT, 63, 0);
  70. 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);
  71. free_pages(page, 1);
  72. 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);
  73. test_slab();
  74. kinfo("Memory management module test completed!");
  75. }
  76. void test_slab()
  77. {
  78. kinfo("Testing SLAB...");
  79. kinfo("Testing kmalloc()...");
  80. for (int i = 1; i < 16; ++i)
  81. {
  82. printk_color(ORANGE, BLACK, "mem_obj_size: %ldbytes\t", kmalloc_cache_group[i].size);
  83. printk_color(ORANGE, BLACK, "bmp(before): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  84. ul *tmp = kmalloc(kmalloc_cache_group[i].size, 0);
  85. if (tmp == NULL)
  86. {
  87. kBUG("Cannot kmalloc such a memory: %ld bytes", kmalloc_cache_group[i].size);
  88. }
  89. printk_color(ORANGE, BLACK, "bmp(middle): %#018lx\t", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  90. kfree(tmp);
  91. printk_color(ORANGE, BLACK, "bmp(after): %#018lx\n", *kmalloc_cache_group[i].cache_pool_entry->bmp);
  92. }
  93. // 测试自动扩容
  94. void *ptrs[7];
  95. for (int i = 0; i < 7; ++i)
  96. ptrs[i] = kmalloc(kmalloc_cache_group[15].size, 0);
  97. struct slab_obj *slab_obj_ptr = kmalloc_cache_group[15].cache_pool_entry;
  98. int count = 0;
  99. do
  100. {
  101. kdebug("bmp(%d): addr=%#018lx\t value=%#018lx", count, slab_obj_ptr->bmp, *slab_obj_ptr->bmp);
  102. slab_obj_ptr = container_of(list_next(&slab_obj_ptr->list), struct slab_obj, list);
  103. ++count;
  104. } while (slab_obj_ptr != kmalloc_cache_group[15].cache_pool_entry);
  105. for (int i = 0; i < 7; ++i)
  106. kfree(ptrs[i]);
  107. kinfo("SLAB test completed!");
  108. }
  109. // 初始化系统各模块
  110. void system_initialize()
  111. {
  112. // 初始化printk
  113. printk_init(8, 16);
  114. kinfo("Kernel Starting...");
  115. load_TR(10); // 加载TR寄存器
  116. ul tss_item_addr = 0x7c00;
  117. set_TSS64(_stack_start, _stack_start, _stack_start, tss_item_addr, tss_item_addr,
  118. tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr, tss_item_addr);
  119. // 初始化中断描述符表
  120. sys_vector_init();
  121. // 初始化内存管理单元
  122. mm_init();
  123. acpi_init();
  124. // 初始化中断模块
  125. irq_init();
  126. // 先初始化系统调用模块
  127. syscall_init();
  128. cpu_init();
  129. //ps2_keyboard_init();
  130. //ps2_mouse_init();
  131. ata_init();
  132. pci_init();
  133. // test_slab();
  134. // test_mm();
  135. // 再初始化进程模块。顺序不能调转
  136. // process_init();
  137. }
  138. //操作系统内核从这里开始执行
  139. void Start_Kernel(void)
  140. {
  141. system_initialize();
  142. // show_welcome();
  143. // test_mm();
  144. /*
  145. while (1)
  146. {
  147. ps2_keyboard_analyze_keycode();
  148. struct ps2_mouse_packet_3bytes packet = {0};
  149. // struct ps2_mouse_packet_4bytes packet = {0};
  150. int errcode = 0;
  151. errcode = ps2_mouse_get_packet(&packet);
  152. if (errcode == 0)
  153. {
  154. printk_color(GREEN, BLACK, " (Mouse: byte0:%d, x:%3d, y:%3d)\n", packet.byte0, packet.movement_x, packet.movement_y);
  155. // 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);
  156. }
  157. }
  158. */
  159. /*
  160. while (1)
  161. {
  162. keyboard_analyze_keycode();
  163. analyze_mousecode();
  164. }
  165. */
  166. while (1)
  167. ;
  168. }
  169. void ignore_int()
  170. {
  171. kwarn("Unknown interrupt or fault at RIP.\n");
  172. return;
  173. }