1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/signal.h>
22#include <linux/sched.h>
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/string.h>
26#include <linux/types.h>
27#include <linux/ptrace.h>
28#include <linux/mman.h>
29#include <linux/mm.h>
30#include <linux/interrupt.h>
31
32#include <asm/page.h>
33#include <asm/pgtable.h>
34#include <asm/mmu.h>
35#include <asm/mmu_context.h>
36#include <asm/system.h>
37#include <linux/uaccess.h>
38#include <asm/exceptions.h>
39
40static unsigned long pte_misses;
41static unsigned long pte_errors;
42
43
44
45
46
47static int store_updates_sp(struct pt_regs *regs)
48{
49 unsigned int inst;
50
51 if (get_user(inst, (unsigned int __user *)regs->pc))
52 return 0;
53
54 if (((inst >> 21) & 0x1f) != 1)
55 return 0;
56
57 if ((inst & 0xd0000000) == 0xd0000000)
58 return 1;
59 return 0;
60}
61
62
63
64
65
66
67
68void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
69{
70 const struct exception_table_entry *fixup;
71
72
73 fixup = search_exception_tables(regs->pc);
74 if (fixup) {
75 regs->pc = fixup->fixup;
76 return;
77 }
78
79
80 die("kernel access of bad area", regs, sig);
81}
82
83
84
85
86
87void do_page_fault(struct pt_regs *regs, unsigned long address,
88 unsigned long error_code)
89{
90 struct vm_area_struct *vma;
91 struct mm_struct *mm = current->mm;
92 siginfo_t info;
93 int code = SEGV_MAPERR;
94 int is_write = error_code & ESR_S;
95 int fault;
96
97 regs->ear = address;
98 regs->esr = error_code;
99
100
101 if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {
102 printk(KERN_WARNING "kernel task_size exceed");
103 _exception(SIGSEGV, regs, code, address);
104 }
105
106
107 if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
108 is_write = 0;
109
110 if (unlikely(in_atomic() || !mm)) {
111 if (kernel_mode(regs))
112 goto bad_area_nosemaphore;
113
114
115
116 printk(KERN_EMERG "Page fault in user mode with "
117 "in_atomic(), mm = %p\n", mm);
118 printk(KERN_EMERG "r15 = %lx MSR = %lx\n",
119 regs->r15, regs->msr);
120 die("Weird page fault", regs, SIGSEGV);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
139 if (kernel_mode(regs) && !search_exception_tables(regs->pc))
140 goto bad_area_nosemaphore;
141
142 down_read(&mm->mmap_sem);
143 }
144
145 vma = find_vma(mm, address);
146 if (unlikely(!vma))
147 goto bad_area;
148
149 if (vma->vm_start <= address)
150 goto good_area;
151
152 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
153 goto bad_area;
154
155 if (unlikely(!is_write))
156 goto bad_area;
157
158
159
160
161
162
163
164
165
166
167 if (unlikely(address + 0x100000 < vma->vm_end)) {
168
169
170 struct pt_regs *uregs = current->thread.regs;
171 if (uregs == NULL)
172 goto bad_area;
173
174
175
176
177
178
179
180
181
182
183
184
185
186 if (address + 2048 < uregs->r1
187 && (kernel_mode(regs) || !store_updates_sp(regs)))
188 goto bad_area;
189 }
190 if (expand_stack(vma, address))
191 goto bad_area;
192
193good_area:
194 code = SEGV_ACCERR;
195
196
197 if (unlikely(is_write)) {
198 if (unlikely(!(vma->vm_flags & VM_WRITE)))
199 goto bad_area;
200
201 } else {
202
203 if (unlikely(error_code & 0x08000000))
204 goto bad_area;
205 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))
206 goto bad_area;
207 }
208
209
210
211
212
213
214 fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
215 if (unlikely(fault & VM_FAULT_ERROR)) {
216 if (fault & VM_FAULT_OOM)
217 goto out_of_memory;
218 else if (fault & VM_FAULT_SIGBUS)
219 goto do_sigbus;
220 BUG();
221 }
222 if (unlikely(fault & VM_FAULT_MAJOR))
223 current->maj_flt++;
224 else
225 current->min_flt++;
226 up_read(&mm->mmap_sem);
227
228
229
230
231
232 pte_misses++;
233 return;
234
235bad_area:
236 up_read(&mm->mmap_sem);
237
238bad_area_nosemaphore:
239 pte_errors++;
240
241
242 if (user_mode(regs)) {
243 _exception(SIGSEGV, regs, code, address);
244
245
246
247
248
249 return;
250 }
251
252 bad_page_fault(regs, address, SIGSEGV);
253 return;
254
255
256
257
258
259out_of_memory:
260 up_read(&mm->mmap_sem);
261 if (!user_mode(regs))
262 bad_page_fault(regs, address, SIGKILL);
263 else
264 pagefault_out_of_memory();
265 return;
266
267do_sigbus:
268 up_read(&mm->mmap_sem);
269 if (user_mode(regs)) {
270 info.si_signo = SIGBUS;
271 info.si_errno = 0;
272 info.si_code = BUS_ADRERR;
273 info.si_addr = (void __user *)address;
274 force_sig_info(SIGBUS, &info, current);
275 return;
276 }
277 bad_page_fault(regs, address, SIGBUS);
278}
279