8b2e8b641adf7b90168b7b3ff4b3ae81e2bf4d6c
[pintos-anon] / src / userprog / exception.c
1 #include "userprog/exception.h"
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include "userprog/gdt.h"
5 #include "threads/interrupt.h"
6 #include "threads/thread.h"
7
8 /* Number of page faults processed. */
9 static long long page_fault_cnt;
10
11 static void kill (struct intr_frame *);
12 static void page_fault (struct intr_frame *);
13
14 /* Registers handlers for interrupts that can be caused by user
15    programs.
16
17    In a real Unix-like OS, most of these interrupts would be
18    passed along to the user process in the form of signals, as
19    described in [SV-386] 3-24 and 3-25, but we don't implement
20    signals.  Instead, we'll make them simply kill the user
21    process.
22
23    Page faults are an exception.  Here they are treated the same
24    way as other exceptions, but this will need to change to
25    implement virtual memory.
26
27    Refer to [IA32-v3] section 5.14 for a description of each of
28    these exceptions. */
29 void
30 exception_init (void) 
31 {
32   /* These exceptions can be raised explicitly by a user program,
33      e.g. via the INT, INT3, INTO, and BOUND instructions.  Thus,
34      we set DPL==3, meaning that user programs are allowed to
35      invoke them via these instructions. */
36   intr_register (3, 3, INTR_ON, kill, "#BP Breakpoint Exception");
37   intr_register (4, 3, INTR_ON, kill, "#OF Overflow Exception");
38   intr_register (5, 3, INTR_ON, kill, "#BR BOUND Range Exceeded Exception");
39
40   /* These exceptions have DPL==0, preventing user processes from
41      invoking them via the INT instruction.  They can still be
42      caused indirectly, e.g. #DE can be caused by dividing by
43      0.  */
44   intr_register (0, 0, INTR_ON, kill, "#DE Divide Error");
45   intr_register (1, 0, INTR_ON, kill, "#DB Debug Exception");
46   intr_register (6, 0, INTR_ON, kill, "#UD Invalid Opcode Exception");
47   intr_register (7, 0, INTR_ON, kill, "#NM Device Not Available Exception");
48   intr_register (11, 0, INTR_ON, kill, "#NP Segment Not Present");
49   intr_register (12, 0, INTR_ON, kill, "#SS Stack Fault Exception");
50   intr_register (13, 0, INTR_ON, kill, "#GP General Protection Exception");
51   intr_register (16, 0, INTR_ON, kill, "#MF x87 FPU Floating-Point Error");
52   intr_register (19, 0, INTR_ON, kill, "#XF SIMD Floating-Point Exception");
53
54   /* Most exceptions can be handled with interrupts turned on.
55      We need to disable interrupts for page faults because the
56      fault address is stored in CR2 and needs to be preserved. */
57   intr_register (14, 0, INTR_OFF, page_fault, "#PF Page-Fault Exception");
58 }
59
60 /* Prints exception statistics. */
61 void
62 exception_print_stats (void) 
63 {
64   printf ("Exception: %lld page faults\n", page_fault_cnt);
65 }
66
67 /* Handler for an exception (probably) caused by a user process. */
68 static void
69 kill (struct intr_frame *f) 
70 {
71   /* This interrupt is one (probably) caused by a user process.
72      For example, the process might have tried to access unmapped
73      virtual memory (a page fault).  For now, we simply kill the
74      user process.  Later, we'll want to handle page faults in
75      the kernel.  Real Unix-like operating systems pass most
76      exceptions back to the process via signals, but we don't
77      implement them. */
78      
79   /* The interrupt frame's code segment value tells us where the
80      exception originated. */
81   switch (f->cs)
82     {
83     case SEL_UCSEG:
84       /* User's code segment, so it's a user exception, as we
85          expected.  Kill the user process.  */
86       printf ("%s: dying due to interrupt %#04x (%s).\n",
87               thread_name (), f->vec_no, intr_name (f->vec_no));
88       intr_dump_frame (f);
89       thread_exit (); 
90
91     case SEL_KCSEG:
92       /* Kernel's code segment, which indicates a kernel bug.
93          Kernel code shouldn't throw exceptions.  (Page faults
94          may cause kernel exceptions--but they shouldn't arrive
95          here.)  Panic the kernel to make the point.  */
96       intr_dump_frame (f);
97       PANIC ("Kernel bug - unexpected interrupt in kernel"); 
98
99     default:
100       /* Some other code segment?  Shouldn't happen.  Panic the
101          kernel. */
102       printf ("Interrupt %#04x (%s) in unknown segment %04x\n",
103              f->vec_no, intr_name (f->vec_no), f->cs);
104       thread_exit ();
105     }
106 }
107
108 /* Page fault error code bits that describe the cause of the exception.  */
109 #define PF_P 0x1    /* 0: not-present page. 1: access rights violation. */
110 #define PF_W 0x2    /* 0: read, 1: write. */
111 #define PF_U 0x4    /* 0: kernel, 1: user process. */
112
113 /* Page fault handler.  This is a skeleton that must be filled in
114    to implement virtual memory.  Some solutions to project 2 may
115    also require modifying this code.
116
117    At entry, the address that faulted is in CR2 (Control Register
118    2) and information about the fault, formatted as described in
119    the PF_* macros above, is in F's error_code member.  The
120    example code here shows how to parse that information.  You
121    can find more information about both of these in the
122    description of "Interrupt 14--Page Fault Exception (#PF)" in
123    [IA32-v3] section 5.14, which is pages 5-46 to 5-49. */
124 static void
125 page_fault (struct intr_frame *f) 
126 {
127   bool not_present;  /* True: not-present page, false: writing r/o page. */
128   bool write;        /* True: access was write, false: access was read. */
129   bool user;         /* True: access by user, false: access by kernel. */
130   void *fault_addr;  /* Fault address. */
131
132   /* Obtain faulting address, the virtual address that was
133      accessed to cause the fault.  It may point to code or to
134      data.  It is not necessarily the address of the instruction
135      that caused the fault (that's f->eip).  */
136   asm ("movl %%cr2, %0" : "=r" (fault_addr));
137
138   /* Turn interrupts back on (they were only off so that we could
139      be assured of reading CR2 before it changed). */
140   intr_enable ();
141
142   /* Count page faults. */
143   page_fault_cnt++;
144
145   /* Determine cause. */
146   not_present = (f->error_code & PF_P) == 0;
147   write = (f->error_code & PF_W) != 0;
148   user = (f->error_code & PF_U) != 0;
149
150   /* To implement virtual memory, delete the rest of the function
151      body, and replace it with code that brings in the page to
152      which fault_addr refers. */
153   printf ("Page fault at %p: %s error %s page in %s context.\n",
154           fault_addr,
155           not_present ? "not present" : "rights violation",
156           write ? "writing" : "reading",
157           user ? "user" : "kernel");
158   kill (f);
159 }
160