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

240 lines
7.4 KiB

  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 2012011346 : 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. extern uintptr_t __vectors[];
  47. int i;
  48. for (i = 0; i < 256; i++) {
  49. if (i != T_SYSCALL) {
  50. SETGATE(idt[i], 0, 8, __vectors[i], 0);
  51. } else {
  52. SETGATE(idt[i], 1, 8, __vectors[i], 3);
  53. }
  54. }
  55. lidt(&idt_pd);
  56. }
  57. static const char *
  58. trapname(int trapno) {
  59. static const char * const excnames[] = {
  60. "Divide error",
  61. "Debug",
  62. "Non-Maskable Interrupt",
  63. "Breakpoint",
  64. "Overflow",
  65. "BOUND Range Exceeded",
  66. "Invalid Opcode",
  67. "Device Not Available",
  68. "Double Fault",
  69. "Coprocessor Segment Overrun",
  70. "Invalid TSS",
  71. "Segment Not Present",
  72. "Stack Fault",
  73. "General Protection",
  74. "Page Fault",
  75. "(unknown trap)",
  76. "x87 FPU Floating-Point Error",
  77. "Alignment Check",
  78. "Machine-Check",
  79. "SIMD Floating-Point Exception"
  80. };
  81. if (trapno < sizeof(excnames)/sizeof(const char * const)) {
  82. return excnames[trapno];
  83. }
  84. if (trapno >= IRQ_OFFSET && trapno < IRQ_OFFSET + 16) {
  85. return "Hardware Interrupt";
  86. }
  87. return "(unknown trap)";
  88. }
  89. /* trap_in_kernel - test if trap happened in kernel */
  90. bool
  91. trap_in_kernel(struct trapframe *tf) {
  92. return (tf->tf_cs == (uint16_t)KERNEL_CS);
  93. }
  94. static const char *IA32flags[] = {
  95. "CF", NULL, "PF", NULL, "AF", NULL, "ZF", "SF",
  96. "TF", "IF", "DF", "OF", NULL, NULL, "NT", NULL,
  97. "RF", "VM", "AC", "VIF", "VIP", "ID", NULL, NULL,
  98. };
  99. void
  100. print_trapframe(struct trapframe *tf) {
  101. cprintf("trapframe at %p\n", tf);
  102. print_regs(&tf->tf_regs);
  103. cprintf(" ds 0x----%04x\n", tf->tf_ds);
  104. cprintf(" es 0x----%04x\n", tf->tf_es);
  105. cprintf(" fs 0x----%04x\n", tf->tf_fs);
  106. cprintf(" gs 0x----%04x\n", tf->tf_gs);
  107. cprintf(" trap 0x%08x %s\n", tf->tf_trapno, trapname(tf->tf_trapno));
  108. cprintf(" err 0x%08x\n", tf->tf_err);
  109. cprintf(" eip 0x%08x\n", tf->tf_eip);
  110. cprintf(" cs 0x----%04x\n", tf->tf_cs);
  111. cprintf(" flag 0x%08x ", tf->tf_eflags);
  112. int i, j;
  113. for (i = 0, j = 1; i < sizeof(IA32flags) / sizeof(IA32flags[0]); i ++, j <<= 1) {
  114. if ((tf->tf_eflags & j) && IA32flags[i] != NULL) {
  115. cprintf("%s,", IA32flags[i]);
  116. }
  117. }
  118. cprintf("IOPL=%d\n", (tf->tf_eflags & FL_IOPL_MASK) >> 12);
  119. if (!trap_in_kernel(tf)) {
  120. cprintf(" esp 0x%08x\n", tf->tf_esp);
  121. cprintf(" ss 0x----%04x\n", tf->tf_ss);
  122. }
  123. }
  124. void
  125. print_regs(struct pushregs *regs) {
  126. cprintf(" edi 0x%08x\n", regs->reg_edi);
  127. cprintf(" esi 0x%08x\n", regs->reg_esi);
  128. cprintf(" ebp 0x%08x\n", regs->reg_ebp);
  129. cprintf(" oesp 0x%08x\n", regs->reg_oesp);
  130. cprintf(" ebx 0x%08x\n", regs->reg_ebx);
  131. cprintf(" edx 0x%08x\n", regs->reg_edx);
  132. cprintf(" ecx 0x%08x\n", regs->reg_ecx);
  133. cprintf(" eax 0x%08x\n", regs->reg_eax);
  134. }
  135. /* trap_dispatch - dispatch based on what type of trap occurred */
  136. static inline void
  137. print_pgfault(struct trapframe *tf) {
  138. /* error_code:
  139. * bit 0 == 0 means no page found, 1 means protection fault
  140. * bit 1 == 0 means read, 1 means write
  141. * bit 2 == 0 means kernel, 1 means user
  142. * */
  143. cprintf("page fault at 0x%08x: %c/%c [%s].\n", rcr2(),
  144. (tf->tf_err & 4) ? 'U' : 'K',
  145. (tf->tf_err & 2) ? 'W' : 'R',
  146. (tf->tf_err & 1) ? "protection fault" : "no page found");
  147. }
  148. //static int
  149. //pgfault_handler(struct trapframe *tf) {
  150. // extern struct mm_struct *check_mm_struct;
  151. // print_pgfault(tf);
  152. // if (check_mm_struct != NULL) {
  153. // return do_pgfault(check_mm_struct, tf->tf_err, rcr2());
  154. // }
  155. // panic("unhandled page fault.\n");
  156. //}
  157. static volatile int in_swap_tick_event = 0;
  158. extern struct mm_struct *check_mm_struct;
  159. static void
  160. trap_dispatch(struct trapframe *tf) {
  161. char c;
  162. static int count = 0;
  163. int ret;
  164. switch (tf->tf_trapno) {
  165. case T_PGFLT: //page fault
  166. panic("pgfault execption!!!\n");
  167. break;
  168. case IRQ_OFFSET + IRQ_TIMER:
  169. #if 0
  170. LAB3 : If some page replacement algorithm(such as CLOCK PRA) need tick to change the priority of pages,
  171. then you can add code here.
  172. #endif
  173. /* LAB1 2012011346 : STEP 3 */
  174. /* handle the timer interrupt */
  175. /* (1) After a timer interrupt, you should record this event using a global variable (increase it), such as ticks in kern/driver/clock.c
  176. * (2) Every TICK_NUM cycle, you can print some info using a funciton, such as print_ticks().
  177. * (3) Too Simple? Yes, I think so!
  178. */
  179. count++;
  180. if (count % 100 == 0) {
  181. print_ticks();
  182. count = 0;
  183. }
  184. break;
  185. case IRQ_OFFSET + IRQ_COM1:
  186. c = cons_getc();
  187. cprintf("serial [%03d] %c\n", c, c);
  188. break;
  189. case IRQ_OFFSET + IRQ_KBD:
  190. c = cons_getc();
  191. cprintf("kbd [%03d] %c\n", c, c);
  192. break;
  193. //LAB1 CHALLENGE 1 : 2012011346 you should modify below codes.
  194. case T_SWITCH_TOU:
  195. case T_SWITCH_TOK:
  196. panic("T_SWITCH_** ??\n");
  197. break;
  198. case IRQ_OFFSET + IRQ_IDE1:
  199. case IRQ_OFFSET + IRQ_IDE2:
  200. /* do nothing */
  201. break;
  202. default:
  203. // in kernel, it must be a mistake
  204. if ((tf->tf_cs & 3) == 0) {
  205. print_trapframe(tf);
  206. panic("unexpected trap in kernel.\n");
  207. }
  208. }
  209. }
  210. /* *
  211. * trap - handles or dispatches an exception/interrupt. if and when trap() returns,
  212. * the code in kern/trap/trapentry.S restores the old CPU state saved in the
  213. * trapframe and then uses the iret instruction to return from the exception.
  214. * */
  215. void
  216. trap(struct trapframe *tf) {
  217. // dispatch based on what type of trap occurred
  218. trap_dispatch(tf);
  219. }