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

187 lines
5.9 KiB

12 years ago
  1. #include <defs.h>
  2. #include <mmu.h>
  3. #include <memlayout.h>
  4. #include <clock.h>
  5. #include <trap.h>
  6. #include <x86.h>
  7. #include <stdio.h>
  8. #include <assert.h>
  9. #include <console.h>
  10. #include <kdebug.h>
  11. #define TICK_NUM 100
  12. static void print_ticks() {
  13. cprintf("%d ticks\n",TICK_NUM);
  14. #ifdef DEBUG_GRADE
  15. cprintf("End of Test.\n");
  16. panic("EOT: kernel seems ok.");
  17. #endif
  18. }
  19. /* *
  20. * Interrupt descriptor table:
  21. *
  22. * Must be built at run time because shifted function addresses can't
  23. * be represented in relocation records.
  24. * */
  25. static struct gatedesc idt[256] = {{0}};
  26. static struct pseudodesc idt_pd = {
  27. sizeof(idt) - 1, (uintptr_t)idt
  28. };
  29. /* idt_init - initialize IDT to each of the entry points in kern/trap/vectors.S */
  30. void
  31. idt_init(void) {
  32. /* LAB1 YOUR CODE : STEP 2 */
  33. /* (1) Where are the entry addrs of each Interrupt Service Routine (ISR)?
  34. * All ISR's entry addrs are stored in __vectors. where is uintptr_t __vectors[] ?
  35. * __vectors[] is in kern/trap/vector.S which is produced by tools/vector.c
  36. * (try "make" command in lab1, then you will find vector.S in kern/trap DIR)
  37. * You can use "extern uintptr_t __vectors[];" to define this extern variable which will be used later.
  38. * (2) Now you should setup the entries of ISR in Interrupt Description Table (IDT).
  39. * Can you see idt[256] in this file? Yes, it's IDT! you can use SETGATE macro to setup each item of IDT
  40. * (3) After setup the contents of IDT, you will let CPU know where is the IDT by using 'lidt' instruction.
  41. * You don't know the meaning of this instruction? just google it! and check the libs/x86.h to know more.
  42. * Notice: the argument of lidt is idt_pd. try to find it!
  43. */
  44. }
  45. static const char *
  46. trapname(int trapno) {
  47. static const char * const excnames[] = {
  48. "Divide error",
  49. "Debug",
  50. "Non-Maskable Interrupt",
  51. "Breakpoint",
  52. "Overflow",
  53. "BOUND Range Exceeded",
  54. "Invalid Opcode",
  55. "Device Not Available",
  56. "Double Fault",
  57. "Coprocessor Segment Overrun",
  58. "Invalid TSS",
  59. "Segment Not Present",
  60. "Stack Fault",
  61. "General Protection",
  62. "Page Fault",
  63. "(unknown trap)",
  64. "x87 FPU Floating-Point Error",
  65. "Alignment Check",
  66. "Machine-Check",
  67. "SIMD Floating-Point Exception"
  68. };
  69. if (trapno < sizeof(excnames)/sizeof(const char * const)) {
  70. return excnames[trapno];
  71. }
  72. if (trapno >= IRQ_OFFSET && trapno < IRQ_OFFSET + 16) {
  73. return "Hardware Interrupt";
  74. }
  75. return "(unknown trap)";
  76. }
  77. /* trap_in_kernel - test if trap happened in kernel */
  78. bool
  79. trap_in_kernel(struct trapframe *tf) {
  80. return (tf->tf_cs == (uint16_t)KERNEL_CS);
  81. }
  82. static const char *IA32flags[] = {
  83. "CF", NULL, "PF", NULL, "AF", NULL, "ZF", "SF",
  84. "TF", "IF", "DF", "OF", NULL, NULL, "NT", NULL,
  85. "RF", "VM", "AC", "VIF", "VIP", "ID", NULL, NULL,
  86. };
  87. void
  88. print_trapframe(struct trapframe *tf) {
  89. cprintf("trapframe at %p\n", tf);
  90. print_regs(&tf->tf_regs);
  91. cprintf(" ds 0x----%04x\n", tf->tf_ds);
  92. cprintf(" es 0x----%04x\n", tf->tf_es);
  93. cprintf(" fs 0x----%04x\n", tf->tf_fs);
  94. cprintf(" gs 0x----%04x\n", tf->tf_gs);
  95. cprintf(" trap 0x%08x %s\n", tf->tf_trapno, trapname(tf->tf_trapno));
  96. cprintf(" err 0x%08x\n", tf->tf_err);
  97. cprintf(" eip 0x%08x\n", tf->tf_eip);
  98. cprintf(" cs 0x----%04x\n", tf->tf_cs);
  99. cprintf(" flag 0x%08x ", tf->tf_eflags);
  100. int i, j;
  101. for (i = 0, j = 1; i < sizeof(IA32flags) / sizeof(IA32flags[0]); i ++, j <<= 1) {
  102. if ((tf->tf_eflags & j) && IA32flags[i] != NULL) {
  103. cprintf("%s,", IA32flags[i]);
  104. }
  105. }
  106. cprintf("IOPL=%d\n", (tf->tf_eflags & FL_IOPL_MASK) >> 12);
  107. if (!trap_in_kernel(tf)) {
  108. cprintf(" esp 0x%08x\n", tf->tf_esp);
  109. cprintf(" ss 0x----%04x\n", tf->tf_ss);
  110. }
  111. }
  112. void
  113. print_regs(struct pushregs *regs) {
  114. cprintf(" edi 0x%08x\n", regs->reg_edi);
  115. cprintf(" esi 0x%08x\n", regs->reg_esi);
  116. cprintf(" ebp 0x%08x\n", regs->reg_ebp);
  117. cprintf(" oesp 0x%08x\n", regs->reg_oesp);
  118. cprintf(" ebx 0x%08x\n", regs->reg_ebx);
  119. cprintf(" edx 0x%08x\n", regs->reg_edx);
  120. cprintf(" ecx 0x%08x\n", regs->reg_ecx);
  121. cprintf(" eax 0x%08x\n", regs->reg_eax);
  122. }
  123. /* trap_dispatch - dispatch based on what type of trap occurred */
  124. static void
  125. trap_dispatch(struct trapframe *tf) {
  126. char c;
  127. switch (tf->tf_trapno) {
  128. case IRQ_OFFSET + IRQ_TIMER:
  129. /* LAB1 YOUR CODE : STEP 3 */
  130. /* handle the timer interrupt */
  131. /* (1) After a timer interrupt, you should record this event using a global variable (increase it), such as ticks in kern/driver/clock.c
  132. * (2) Every TICK_NUM cycle, you can print some info using a funciton, such as print_ticks().
  133. * (3) Too Simple? Yes, I think so!
  134. */
  135. break;
  136. case IRQ_OFFSET + IRQ_COM1:
  137. c = cons_getc();
  138. cprintf("serial [%03d] %c\n", c, c);
  139. break;
  140. case IRQ_OFFSET + IRQ_KBD:
  141. c = cons_getc();
  142. cprintf("kbd [%03d] %c\n", c, c);
  143. break;
  144. //LAB1 CHALLENGE 1 : YOUR CODE you should modify below codes.
  145. case T_SWITCH_TOU:
  146. case T_SWITCH_TOK:
  147. panic("T_SWITCH_** ??\n");
  148. break;
  149. case IRQ_OFFSET + IRQ_IDE1:
  150. case IRQ_OFFSET + IRQ_IDE2:
  151. /* do nothing */
  152. break;
  153. default:
  154. // in kernel, it must be a mistake
  155. if ((tf->tf_cs & 3) == 0) {
  156. print_trapframe(tf);
  157. panic("unexpected trap in kernel.\n");
  158. }
  159. }
  160. }
  161. /* *
  162. * trap - handles or dispatches an exception/interrupt. if and when trap() returns,
  163. * the code in kern/trap/trapentry.S restores the old CPU state saved in the
  164. * trapframe and then uses the iret instruction to return from the exception.
  165. * */
  166. void
  167. trap(struct trapframe *tf) {
  168. // dispatch based on what type of trap occurred
  169. trap_dispatch(tf);
  170. }