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

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