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

225 lines
7.4 KiB

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