《操作系统》的实验代码。
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.

117 lines
2.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 <kmonitor.h>
  12. void kern_init(void) __attribute__((noreturn));
  13. void grade_backtrace(void);
  14. static void lab1_switch_test(void);
  15. void
  16. kern_init(void){
  17. extern char edata[], end[];
  18. memset(edata, 0, end - edata);
  19. cons_init(); // init the console
  20. const char *message = "(THU.CST) os is loading ...";
  21. cprintf("%s\n\n", message);
  22. print_kerninfo();
  23. grade_backtrace();
  24. pmm_init(); // init physical memory management
  25. pic_init(); // init interrupt controller
  26. idt_init(); // init interrupt descriptor table
  27. clock_init(); // init clock interrupt
  28. intr_enable(); // enable irq interrupt
  29. //LAB1: CAHLLENGE 1 If you try to do it, uncomment lab1_switch_test()
  30. // user/kernel mode switch test
  31. lab1_switch_test();
  32. /* do nothing */
  33. while (1);
  34. }
  35. void __attribute__((noinline))
  36. grade_backtrace2(int arg0, int arg1, int arg2, int arg3) {
  37. mon_backtrace(0, NULL, NULL);
  38. }
  39. void __attribute__((noinline))
  40. grade_backtrace1(int arg0, int arg1) {
  41. grade_backtrace2(arg0, (int)&arg0, arg1, (int)&arg1);
  42. }
  43. void __attribute__((noinline))
  44. grade_backtrace0(int arg0, int arg1, int arg2) {
  45. grade_backtrace1(arg0, arg2);
  46. }
  47. void
  48. grade_backtrace(void) {
  49. grade_backtrace0(0, (int)kern_init, 0xffff0000);
  50. }
  51. static void
  52. lab1_print_cur_status(void) {
  53. static int round = 0;
  54. uint16_t reg1, reg2, reg3, reg4;
  55. asm volatile (
  56. "mov %%cs, %0;"
  57. "mov %%ds, %1;"
  58. "mov %%es, %2;"
  59. "mov %%ss, %3;"
  60. : "=m"(reg1), "=m"(reg2), "=m"(reg3), "=m"(reg4));
  61. cprintf("%d: @ring %d\n", round, reg1 & 3);
  62. cprintf("%d: cs = %x\n", round, reg1);
  63. cprintf("%d: ds = %x\n", round, reg2);
  64. cprintf("%d: es = %x\n", round, reg3);
  65. cprintf("%d: ss = %x\n", round, reg4);
  66. round ++;
  67. }
  68. static void
  69. lab1_switch_to_user(void) {
  70. //LAB1 CHALLENGE 1 : TODO
  71. asm volatile (
  72. "sub $0x8, %%esp \n"
  73. "int %0 \n"
  74. "movl %%ebp, %%esp"
  75. :
  76. : "i"(T_SWITCH_TOU)
  77. );
  78. }
  79. static void
  80. lab1_switch_to_kernel(void) {
  81. //LAB1 CHALLENGE 1 : TODO
  82. asm volatile (
  83. "int %0 \n"
  84. "movl %%ebp, %%esp \n"
  85. :
  86. : "i"(T_SWITCH_TOK)
  87. );
  88. }
  89. static void
  90. lab1_switch_test(void) {
  91. lab1_print_cur_status();
  92. cprintf("+++ switch to user mode +++\n");
  93. lab1_switch_to_user();
  94. lab1_print_cur_status();
  95. cprintf("+++ switch to kernel mode +++\n");
  96. lab1_switch_to_kernel();
  97. lab1_print_cur_status();
  98. }