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

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