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

112 lines
2.6 KiB

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