《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.8 KiB

преди 10 години
  1. #include <defs.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <console.h>
  5. #include <kdebug.h>
  6. #include <picirq.h>
  7. #include <trap.h>
  8. #include <clock.h>
  9. #include <intr.h>
  10. #include <pmm.h>
  11. #include <vmm.h>
  12. #include <ide.h>
  13. #include <swap.h>
  14. #include <proc.h>
  15. #include <fs.h>
  16. int kern_init(void) __attribute__((noreturn));
  17. static void lab1_switch_test(void);
  18. int
  19. kern_init(void) {
  20. extern char edata[], end[];
  21. memset(edata, 0, end - edata);
  22. cons_init(); // init the console
  23. const char *message = "(THU.CST) os is loading ...";
  24. cprintf("%s\n\n", message);
  25. print_kerninfo();
  26. grade_backtrace();
  27. pmm_init(); // init physical memory management
  28. pic_init(); // init interrupt controller
  29. idt_init(); // init interrupt descriptor table
  30. vmm_init(); // init virtual memory management
  31. sched_init(); // init scheduler
  32. proc_init(); // init process table
  33. ide_init(); // init ide devices
  34. swap_init(); // init swap
  35. fs_init(); // init fs
  36. clock_init(); // init clock interrupt
  37. intr_enable(); // enable irq interrupt
  38. //LAB1: CAHLLENGE 1 If you try to do it, uncomment lab1_switch_test()
  39. // user/kernel mode switch test
  40. //lab1_switch_test();
  41. cpu_idle(); // run idle process
  42. }
  43. void __attribute__((noinline))
  44. grade_backtrace2(int arg0, int arg1, int arg2, int arg3) {
  45. mon_backtrace(0, NULL, NULL);
  46. }
  47. void __attribute__((noinline))
  48. grade_backtrace1(int arg0, int arg1) {
  49. grade_backtrace2(arg0, (int)&arg0, arg1, (int)&arg1);
  50. }
  51. void __attribute__((noinline))
  52. grade_backtrace0(int arg0, int arg1, int arg2) {
  53. grade_backtrace1(arg0, arg2);
  54. }
  55. void
  56. grade_backtrace(void) {
  57. grade_backtrace0(0, (int)kern_init, 0xffff0000);
  58. }
  59. static void
  60. lab1_print_cur_status(void) {
  61. static int round = 0;
  62. uint16_t reg1, reg2, reg3, reg4;
  63. asm volatile (
  64. "mov %%cs, %0;"
  65. "mov %%ds, %1;"
  66. "mov %%es, %2;"
  67. "mov %%ss, %3;"
  68. : "=m"(reg1), "=m"(reg2), "=m"(reg3), "=m"(reg4));
  69. cprintf("%d: @ring %d\n", round, reg1 & 3);
  70. cprintf("%d: cs = %x\n", round, reg1);
  71. cprintf("%d: ds = %x\n", round, reg2);
  72. cprintf("%d: es = %x\n", round, reg3);
  73. cprintf("%d: ss = %x\n", round, reg4);
  74. round ++;
  75. }
  76. static void
  77. lab1_switch_to_user(void) {
  78. //LAB1 CHALLENGE 1 : TODO
  79. }
  80. static void
  81. lab1_switch_to_kernel(void) {
  82. //LAB1 CHALLENGE 1 : TODO
  83. }
  84. static void
  85. lab1_switch_test(void) {
  86. lab1_print_cur_status();
  87. cprintf("+++ switch to user mode +++\n");
  88. lab1_switch_to_user();
  89. lab1_print_cur_status();
  90. cprintf("+++ switch to kernel mode +++\n");
  91. lab1_switch_to_kernel();
  92. lab1_print_cur_status();
  93. }