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

114 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. proc_init(); // init process table
  33. ide_init(); // init ide devices
  34. swap_init(); // init swap
  35. clock_init(); // init clock interrupt
  36. intr_enable(); // enable irq interrupt
  37. //LAB1: CAHLLENGE 1 If you try to do it, uncomment lab1_switch_test()
  38. // user/kernel mode switch test
  39. //lab1_switch_test();
  40. cpu_idle(); // run idle process
  41. }
  42. void __attribute__((noinline))
  43. grade_backtrace2(int arg0, int arg1, int arg2, int arg3) {
  44. mon_backtrace(0, NULL, NULL);
  45. }
  46. void __attribute__((noinline))
  47. grade_backtrace1(int arg0, int arg1) {
  48. grade_backtrace2(arg0, (int)&arg0, arg1, (int)&arg1);
  49. }
  50. void __attribute__((noinline))
  51. grade_backtrace0(int arg0, int arg1, int arg2) {
  52. grade_backtrace1(arg0, arg2);
  53. }
  54. void
  55. grade_backtrace(void) {
  56. grade_backtrace0(0, (int)kern_init, 0xffff0000);
  57. }
  58. static void
  59. lab1_print_cur_status(void) {
  60. static int round = 0;
  61. uint16_t reg1, reg2, reg3, reg4;
  62. asm volatile (
  63. "mov %%cs, %0;"
  64. "mov %%ds, %1;"
  65. "mov %%es, %2;"
  66. "mov %%ss, %3;"
  67. : "=m"(reg1), "=m"(reg2), "=m"(reg3), "=m"(reg4));
  68. cprintf("%d: @ring %d\n", round, reg1 & 3);
  69. cprintf("%d: cs = %x\n", round, reg1);
  70. cprintf("%d: ds = %x\n", round, reg2);
  71. cprintf("%d: es = %x\n", round, reg3);
  72. cprintf("%d: ss = %x\n", round, reg4);
  73. round ++;
  74. }
  75. static void
  76. lab1_switch_to_user(void) {
  77. //LAB1 CHALLENGE 1 : TODO
  78. }
  79. static void
  80. lab1_switch_to_kernel(void) {
  81. //LAB1 CHALLENGE 1 : TODO
  82. }
  83. static void
  84. lab1_switch_test(void) {
  85. lab1_print_cur_status();
  86. cprintf("+++ switch to user mode +++\n");
  87. lab1_switch_to_user();
  88. lab1_print_cur_status();
  89. cprintf("+++ switch to kernel mode +++\n");
  90. lab1_switch_to_kernel();
  91. lab1_print_cur_status();
  92. }