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

115 lines
2.8 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 <kmonitor.h>
  16. int kern_init(void) __attribute__((noreturn));
  17. void grade_backtrace(void);
  18. static void lab1_switch_test(void);
  19. int
  20. kern_init(void) {
  21. extern char edata[], end[];
  22. memset(edata, 0, end - edata);
  23. cons_init(); // init the console
  24. const char *message = "(THU.CST) os is loading ...";
  25. cprintf("%s\n\n", message);
  26. print_kerninfo();
  27. grade_backtrace();
  28. pmm_init(); // init physical memory management
  29. pic_init(); // init interrupt controller
  30. idt_init(); // init interrupt descriptor table
  31. vmm_init(); // init virtual memory management
  32. sched_init(); // init scheduler
  33. proc_init(); // init process table
  34. ide_init(); // init ide devices
  35. swap_init(); // init swap
  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. }