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

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