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

289 line
8.8 KiB

12 年之前
12 年之前
  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. #include <unistd.h>
  14. #include <syscall.h>
  15. #include <error.h>
  16. #include <sched.h>
  17. #include <sync.h>
  18. #define TICK_NUM 100
  19. static void print_ticks() {
  20. cprintf("%d ticks\n",TICK_NUM);
  21. #ifdef DEBUG_GRADE
  22. cprintf("End of Test.\n");
  23. panic("EOT: kernel seems ok.");
  24. #endif
  25. }
  26. /* *
  27. * Interrupt descriptor table:
  28. *
  29. * Must be built at run time because shifted function addresses can't
  30. * be represented in relocation records.
  31. * */
  32. static struct gatedesc idt[256] = {{0}};
  33. static struct pseudodesc idt_pd = {
  34. sizeof(idt) - 1, (uintptr_t)idt
  35. };
  36. /* idt_init - initialize IDT to each of the entry points in kern/trap/vectors.S */
  37. void
  38. idt_init(void) {
  39. /* LAB1 YOUR CODE : STEP 2 */
  40. /* (1) Where are the entry addrs of each Interrupt Service Routine (ISR)?
  41. * All ISR's entry addrs are stored in __vectors. where is uintptr_t __vectors[] ?
  42. * __vectors[] is in kern/trap/vector.S which is produced by tools/vector.c
  43. * (try "make" command in lab1, then you will find vector.S in kern/trap DIR)
  44. * You can use "extern uintptr_t __vectors[];" to define this extern variable which will be used later.
  45. * (2) Now you should setup the entries of ISR in Interrupt Description Table (IDT).
  46. * Can you see idt[256] in this file? Yes, it's IDT! you can use SETGATE macro to setup each item of IDT
  47. * (3) After setup the contents of IDT, you will let CPU know where is the IDT by using 'lidt' instruction.
  48. * You don't know the meaning of this instruction? just google it! and check the libs/x86.h to know more.
  49. * Notice: the argument of lidt is idt_pd. try to find it!
  50. */
  51. /* LAB5 YOUR CODE */
  52. //you should update your lab1 code (just add ONE or TWO lines of code), let user app to use syscall to get the service of ucore
  53. //so you should setup the syscall interrupt gate in here
  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. static inline void
  134. print_pgfault(struct trapframe *tf) {
  135. /* error_code:
  136. * bit 0 == 0 means no page found, 1 means protection fault
  137. * bit 1 == 0 means read, 1 means write
  138. * bit 2 == 0 means kernel, 1 means user
  139. * */
  140. cprintf("page fault at 0x%08x: %c/%c [%s].\n", rcr2(),
  141. (tf->tf_err & 4) ? 'U' : 'K',
  142. (tf->tf_err & 2) ? 'W' : 'R',
  143. (tf->tf_err & 1) ? "protection fault" : "no page found");
  144. }
  145. static int
  146. pgfault_handler(struct trapframe *tf) {
  147. extern struct mm_struct *check_mm_struct;
  148. if(check_mm_struct !=NULL) { //used for test check_swap
  149. print_pgfault(tf);
  150. }
  151. struct mm_struct *mm;
  152. if (check_mm_struct != NULL) {
  153. assert(current == idleproc);
  154. mm = check_mm_struct;
  155. }
  156. else {
  157. if (current == NULL) {
  158. print_trapframe(tf);
  159. print_pgfault(tf);
  160. panic("unhandled page fault.\n");
  161. }
  162. mm = current->mm;
  163. }
  164. return do_pgfault(mm, tf->tf_err, rcr2());
  165. }
  166. static volatile int in_swap_tick_event = 0;
  167. extern struct mm_struct *check_mm_struct;
  168. static void
  169. trap_dispatch(struct trapframe *tf) {
  170. char c;
  171. int ret=0;
  172. switch (tf->tf_trapno) {
  173. case T_PGFLT: //page fault
  174. if ((ret = pgfault_handler(tf)) != 0) {
  175. print_trapframe(tf);
  176. if (current == NULL) {
  177. panic("handle pgfault failed. ret=%d\n", ret);
  178. }
  179. else {
  180. if (trap_in_kernel(tf)) {
  181. panic("handle pgfault failed in kernel mode. ret=%d\n", ret);
  182. }
  183. cprintf("killed by kernel.\n");
  184. panic("handle user mode pgfault failed. ret=%d\n", ret);
  185. do_exit(-E_KILLED);
  186. }
  187. }
  188. break;
  189. case T_SYSCALL:
  190. syscall();
  191. break;
  192. case IRQ_OFFSET + IRQ_TIMER:
  193. #if 0
  194. LAB3 : If some page replacement algorithm(such as CLOCK PRA) need tick to change the priority of pages,
  195. then you can add code here.
  196. #endif
  197. /* LAB1 YOUR CODE : STEP 3 */
  198. /* handle the timer interrupt */
  199. /* (1) After a timer interrupt, you should record this event using a global variable (increase it), such as ticks in kern/driver/clock.c
  200. * (2) Every TICK_NUM cycle, you can print some info using a funciton, such as print_ticks().
  201. * (3) Too Simple? Yes, I think so!
  202. */
  203. /* LAB5 YOUR CODE */
  204. /* you should upate you lab1 code (just add ONE or TWO lines of code):
  205. * Every TICK_NUM cycle, you should set current process's current->need_resched = 1
  206. */
  207. break;
  208. case IRQ_OFFSET + IRQ_COM1:
  209. c = cons_getc();
  210. cprintf("serial [%03d] %c\n", c, c);
  211. break;
  212. case IRQ_OFFSET + IRQ_KBD:
  213. c = cons_getc();
  214. cprintf("kbd [%03d] %c\n", c, c);
  215. break;
  216. //LAB1 CHALLENGE 1 : YOUR CODE you should modify below codes.
  217. case T_SWITCH_TOU:
  218. case T_SWITCH_TOK:
  219. panic("T_SWITCH_** ??\n");
  220. break;
  221. case IRQ_OFFSET + IRQ_IDE1:
  222. case IRQ_OFFSET + IRQ_IDE2:
  223. /* do nothing */
  224. break;
  225. default:
  226. print_trapframe(tf);
  227. if (current != NULL) {
  228. cprintf("unhandled trap.\n");
  229. do_exit(-E_KILLED);
  230. }
  231. // in kernel, it must be a mistake
  232. panic("unexpected trap in kernel.\n");
  233. }
  234. }
  235. /* *
  236. * trap - handles or dispatches an exception/interrupt. if and when trap() returns,
  237. * the code in kern/trap/trapentry.S restores the old CPU state saved in the
  238. * trapframe and then uses the iret instruction to return from the exception.
  239. * */
  240. void
  241. trap(struct trapframe *tf) {
  242. // dispatch based on what type of trap occurred
  243. // used for previous projects
  244. if (current == NULL) {
  245. trap_dispatch(tf);
  246. }
  247. else {
  248. // keep a trapframe chain in stack
  249. struct trapframe *otf = current->tf;
  250. current->tf = tf;
  251. bool in_kernel = trap_in_kernel(tf);
  252. trap_dispatch(tf);
  253. current->tf = otf;
  254. if (!in_kernel) {
  255. if (current->flags & PF_EXITING) {
  256. do_exit(-E_KILLED);
  257. }
  258. if (current->need_resched) {
  259. schedule();
  260. }
  261. }
  262. }
  263. }