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

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