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

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