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