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

297 lines
9.3 KiB

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