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

307 lines
9.6 KiB

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