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

  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. int kern_init(void) __attribute__((noreturn));
  16. static void lab1_switch_test(void);
  17. int
  18. kern_init(void) {
  19. extern char edata[], end[];
  20. memset(edata, 0, end - edata);
  21. cons_init(); // init the console
  22. const char *message = "(THU.CST) os is loading ...";
  23. cprintf("%s\n\n", message);
  24. print_kerninfo();
  25. grade_backtrace();
  26. pmm_init(); // init physical memory management
  27. pic_init(); // init interrupt controller
  28. idt_init(); // init interrupt descriptor table
  29. vmm_init(); // init virtual memory management
  30. proc_init(); // init process table
  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. cpu_idle(); // run idle process
  39. }
  40. void __attribute__((noinline))
  41. grade_backtrace2(int arg0, int arg1, int arg2, int arg3) {
  42. mon_backtrace(0, NULL, NULL);
  43. }
  44. void __attribute__((noinline))
  45. grade_backtrace1(int arg0, int arg1) {
  46. grade_backtrace2(arg0, (int)&arg0, arg1, (int)&arg1);
  47. }
  48. void __attribute__((noinline))
  49. grade_backtrace0(int arg0, int arg1, int arg2) {
  50. grade_backtrace1(arg0, arg2);
  51. }
  52. void
  53. grade_backtrace(void) {
  54. grade_backtrace0(0, (int)kern_init, 0xffff0000);
  55. }
  56. static void
  57. lab1_print_cur_status(void) {
  58. static int round = 0;
  59. uint16_t reg1, reg2, reg3, reg4;
  60. asm volatile (
  61. "mov %%cs, %0;"
  62. "mov %%ds, %1;"
  63. "mov %%es, %2;"
  64. "mov %%ss, %3;"
  65. : "=m"(reg1), "=m"(reg2), "=m"(reg3), "=m"(reg4));
  66. cprintf("%d: @ring %d\n", round, reg1 & 3);
  67. cprintf("%d: cs = %x\n", round, reg1);
  68. cprintf("%d: ds = %x\n", round, reg2);
  69. cprintf("%d: es = %x\n", round, reg3);
  70. cprintf("%d: ss = %x\n", round, reg4);
  71. round ++;
  72. }
  73. static void
  74. lab1_switch_to_user(void) {
  75. //LAB1 CHALLENGE 1 : TODO
  76. }
  77. static void
  78. lab1_switch_to_kernel(void) {
  79. //LAB1 CHALLENGE 1 : TODO
  80. }
  81. static void
  82. lab1_switch_test(void) {
  83. lab1_print_cur_status();
  84. cprintf("+++ switch to user mode +++\n");
  85. lab1_switch_to_user();
  86. lab1_print_cur_status();
  87. cprintf("+++ switch to kernel mode +++\n");
  88. lab1_switch_to_kernel();
  89. lab1_print_cur_status();
  90. }