blob: bfb0917d699dbc951df6bf964d0cdb1298ccf37c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995 Linus Torvalds
3 */
4
5#include <linux/signal.h>
6#include <linux/sched.h>
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/string.h>
10#include <linux/types.h>
11#include <linux/ptrace.h>
12#include <linux/mman.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
16#include <linux/init.h>
17#include <linux/tty.h>
18#include <linux/vt_kern.h> /* For unblank_screen() */
19#include <linux/highmem.h>
Jan Beulich28609f62007-05-02 19:27:04 +020020#include <linux/bootmem.h> /* for max_low_pfn */
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070021#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070023#include <linux/kprobes.h>
Andi Kleen11a41802006-12-07 02:14:06 +010024#include <linux/uaccess.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070025#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/desc.h>
Rusty Russell78be3702006-09-26 10:52:39 +020029#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Harvey Harrison33cb5242008-01-30 13:32:19 +010031/*
32 * Page fault error code bits
33 * bit 0 == 0 means no page found, 1 means protection fault
34 * bit 1 == 0 means read, 1 means write
35 * bit 2 == 0 means kernel, 1 means user-mode
36 * bit 3 == 1 means use of reserved bit detected
37 * bit 4 == 1 means fault was an instruction fetch
38 */
39#define PF_PROT (1<<0)
40#define PF_WRITE (1<<1)
41#define PF_USER (1<<2)
42#define PF_RSVD (1<<3)
43#define PF_INSTR (1<<4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Harvey Harrison33cb5242008-01-30 13:32:19 +010045extern void die(const char *, struct pt_regs *, long);
46
Christoph Hellwig74a0b572007-10-16 01:24:07 -070047static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthyb71b5b62006-06-26 00:25:25 -070048{
Harvey Harrison33cb5242008-01-30 13:32:19 +010049#ifdef CONFIG_KPROBES
Christoph Hellwig74a0b572007-10-16 01:24:07 -070050 int ret = 0;
Anil S Keshavamurthyb71b5b62006-06-26 00:25:25 -070051
Christoph Hellwig74a0b572007-10-16 01:24:07 -070052 /* kprobe_running() needs smp_processor_id() */
53 if (!user_mode_vm(regs)) {
54 preempt_disable();
55 if (kprobe_running() && kprobe_fault_handler(regs, 14))
56 ret = 1;
57 preempt_enable();
58 }
Anil S Keshavamurthyb71b5b62006-06-26 00:25:25 -070059
Christoph Hellwig74a0b572007-10-16 01:24:07 -070060 return ret;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070061#else
Christoph Hellwig74a0b572007-10-16 01:24:07 -070062 return 0;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070063#endif
Harvey Harrison33cb5242008-01-30 13:32:19 +010064}
Anil S Keshavamurthyb71b5b62006-06-26 00:25:25 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * Return EIP plus the CS segment base. The segment limit is also
68 * adjusted, clamped to the kernel/user address space (whichever is
69 * appropriate), and returned in *eip_limit.
70 *
71 * The segment is checked, because it might have been changed by another
72 * task between the original faulting instruction and here.
73 *
74 * If CS is no longer a valid code segment, or if EIP is beyond the
75 * limit, or if it is a kernel address when CS is not a kernel segment,
76 * then the returned value will be greater than *eip_limit.
Harvey Harrison33cb5242008-01-30 13:32:19 +010077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * This is slow, but is very rarely executed.
79 */
80static inline unsigned long get_segment_eip(struct pt_regs *regs,
81 unsigned long *eip_limit)
82{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010083 unsigned long ip = regs->ip;
84 unsigned seg = regs->cs & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 u32 seg_ar, seg_limit, base, *desc;
86
Chuck Ebbert19964fe2006-06-23 02:04:29 -070087 /* Unlikely, but must come before segment checks. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010088 if (unlikely(regs->flags & VM_MASK)) {
Chuck Ebbert19964fe2006-06-23 02:04:29 -070089 base = seg << 4;
90 *eip_limit = base + 0xffff;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010091 return base + (ip & 0xffff);
Chuck Ebbert19964fe2006-06-23 02:04:29 -070092 }
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* The standard kernel/user address space limit. */
Rusty Russell78be3702006-09-26 10:52:39 +020095 *eip_limit = user_mode(regs) ? USER_DS.seg : KERNEL_DS.seg;
Harvey Harrison33cb5242008-01-30 13:32:19 +010096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* By far the most common cases. */
Rusty Russell78be3702006-09-26 10:52:39 +020098 if (likely(SEGMENT_IS_FLAT_CODE(seg)))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010099 return ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 /* Check the segment exists, is within the current LDT/GDT size,
102 that kernel/user (ring 0..3) has the appropriate privilege,
103 that it's a code segment, and get the limit. */
104 __asm__ ("larl %3,%0; lsll %3,%1"
105 : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100106 if ((~seg_ar & 0x9800) || ip > seg_limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *eip_limit = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100108 return 1; /* So that returned ip > *eip_limit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Harvey Harrison33cb5242008-01-30 13:32:19 +0100111 /* Get the GDT/LDT descriptor base.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 When you look for races in this code remember that
113 LDT and other horrors are only used in user space. */
114 if (seg & (1<<2)) {
115 /* Must lock the LDT while reading it. */
Luiz Fernando N. Capitulinode8aacb2007-10-17 18:04:41 +0200116 mutex_lock(&current->mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 desc = current->mm->context.ldt;
118 desc = (void *)desc + (seg & ~7);
119 } else {
120 /* Must disable preemption while reading the GDT. */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100121 desc = (u32 *)get_cpu_gdt_table(get_cpu());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 desc = (void *)desc + (seg & ~7);
123 }
124
125 /* Decode the code segment base from the descriptor */
Glauber de Oliveira Costacc697852008-01-30 13:31:14 +0100126 base = get_desc_base((struct desc_struct *)desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Harvey Harrison33cb5242008-01-30 13:32:19 +0100128 if (seg & (1<<2))
Luiz Fernando N. Capitulinode8aacb2007-10-17 18:04:41 +0200129 mutex_unlock(&current->mm->context.lock);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100130 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 put_cpu();
132
133 /* Adjust EIP and segment limit, and clamp at the kernel limit.
134 It's legitimate for segments to wrap at 0xffffffff. */
135 seg_limit += base;
136 if (seg_limit < *eip_limit && seg_limit >= base)
137 *eip_limit = seg_limit;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100138 return ip + base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Harvey Harrison33cb5242008-01-30 13:32:19 +0100141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
143 * Check that here and ignore it.
144 */
145static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 unsigned long limit;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100148 unsigned char *instr = (unsigned char *)get_segment_eip(regs, &limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int scan_more = 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100150 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int i;
152
Harvey Harrison33cb5242008-01-30 13:32:19 +0100153 for (i = 0; scan_more && i < 15; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 unsigned char opcode;
155 unsigned char instr_hi;
156 unsigned char instr_lo;
157
Andi Kleen11a41802006-12-07 02:14:06 +0100158 if (instr > (unsigned char *)limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
Andi Kleen11a41802006-12-07 02:14:06 +0100160 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Harvey Harrison33cb5242008-01-30 13:32:19 +0100163 instr_hi = opcode & 0xf0;
164 instr_lo = opcode & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 instr++;
166
Harvey Harrison33cb5242008-01-30 13:32:19 +0100167 switch (instr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 case 0x20:
169 case 0x30:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100170 /*
171 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
172 * In X86_64 long mode, the CPU will signal invalid
173 * opcode if some of these prefixes are present so
174 * X86_64 will never get here anyway
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 scan_more = ((instr_lo & 7) == 0x6);
177 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100178#ifdef CONFIG_X86_64
179 case 0x40:
180 /*
181 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
182 * Need to figure out under what instruction mode the
183 * instruction was issued. Could check the LDT for lm,
184 * but for now it's good enough to assume that long
185 * mode only uses well known segments or kernel.
186 */
187 scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
188 break;
189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 case 0x60:
191 /* 0x64 thru 0x67 are valid prefixes in all modes. */
192 scan_more = (instr_lo & 0xC) == 0x4;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 case 0xF0:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100195 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 scan_more = !instr_lo || (instr_lo>>1) == 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 case 0x00:
199 /* Prefetch instruction is 0x0F0D or 0x0F18 */
200 scan_more = 0;
Andi Kleen11a41802006-12-07 02:14:06 +0100201 if (instr > (unsigned char *)limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
Andi Kleen11a41802006-12-07 02:14:06 +0100203 if (probe_kernel_address(instr, opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 prefetch = (instr_lo == 0xF) &&
206 (opcode == 0x0D || opcode == 0x18);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 default:
209 scan_more = 0;
210 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 return prefetch;
214}
215
216static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
217 unsigned long error_code)
218{
219 if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
220 boot_cpu_data.x86 >= 6)) {
221 /* Catch an obscure case of prefetch inside an NX page. */
222 if (nx_enabled && (error_code & 16))
223 return 0;
224 return __is_prefetch(regs, addr);
225 }
226 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Ingo Molnar869f96a2005-09-03 15:56:26 -0700229static noinline void force_sig_info_fault(int si_signo, int si_code,
230 unsigned long address, struct task_struct *tsk)
231{
232 siginfo_t info;
233
234 info.si_signo = si_signo;
235 info.si_errno = 0;
236 info.si_code = si_code;
237 info.si_addr = (void __user *)address;
238 force_sig_info(si_signo, &info, tsk);
239}
240
Harvey Harrison75604d72008-01-30 13:31:17 +0100241void do_invalid_op(struct pt_regs *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jan Beulich101f12a2006-03-23 02:59:45 -0800243static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
244{
245 unsigned index = pgd_index(address);
246 pgd_t *pgd_k;
247 pud_t *pud, *pud_k;
248 pmd_t *pmd, *pmd_k;
249
250 pgd += index;
251 pgd_k = init_mm.pgd + index;
252
253 if (!pgd_present(*pgd_k))
254 return NULL;
255
256 /*
257 * set_pgd(pgd, *pgd_k); here would be useless on PAE
258 * and redundant with the set_pmd() on non-PAE. As would
259 * set_pud.
260 */
261
262 pud = pud_offset(pgd, address);
263 pud_k = pud_offset(pgd_k, address);
264 if (!pud_present(*pud_k))
265 return NULL;
266
267 pmd = pmd_offset(pud, address);
268 pmd_k = pmd_offset(pud_k, address);
269 if (!pmd_present(*pmd_k))
270 return NULL;
Zachary Amsden8b14cb92007-08-21 18:30:36 -0700271 if (!pmd_present(*pmd)) {
Jan Beulich101f12a2006-03-23 02:59:45 -0800272 set_pmd(pmd, *pmd_k);
Zachary Amsden8b14cb92007-08-21 18:30:36 -0700273 arch_flush_lazy_mmu_mode();
274 } else
Jan Beulich101f12a2006-03-23 02:59:45 -0800275 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
276 return pmd_k;
277}
278
279/*
280 * Handle a fault on the vmalloc or module mapping area
281 *
282 * This assumes no large pages in there.
283 */
284static inline int vmalloc_fault(unsigned long address)
285{
286 unsigned long pgd_paddr;
287 pmd_t *pmd_k;
288 pte_t *pte_k;
289 /*
290 * Synchronize this task's top level page-table
291 * with the 'reference' page table.
292 *
293 * Do _not_ use "current" here. We might be inside
294 * an interrupt in the middle of a task switch..
295 */
296 pgd_paddr = read_cr3();
297 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
298 if (!pmd_k)
299 return -1;
300 pte_k = pte_offset_kernel(pmd_k, address);
301 if (!pte_present(*pte_k))
302 return -1;
303 return 0;
304}
305
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200306int show_unhandled_signals = 1;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * This routine handles page faults. It determines the address,
310 * and the problem, and then passes it off to one of the appropriate
311 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100313void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct task_struct *tsk;
316 struct mm_struct *mm;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100317 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 unsigned long address;
Ingo Molnar869f96a2005-09-03 15:56:26 -0700319 int write, si_code;
Nick Piggin83c54072007-07-19 01:47:05 -0700320 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200322 /*
323 * We can fault from pretty much anywhere, with unknown IRQ state.
324 */
325 trace_hardirqs_fixup();
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* get the address */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100328 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 tsk = current;
331
Ingo Molnar869f96a2005-09-03 15:56:26 -0700332 si_code = SEGV_MAPERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /*
335 * We fault-in kernel-space virtual memory on-demand. The
336 * 'reference' page table is init_mm.pgd.
337 *
338 * NOTE! We MUST NOT take any locks for this case. We may
339 * be in an interrupt or a critical region, and should
340 * only copy the information from the master page table,
341 * nothing more.
342 *
343 * This verifies that the fault happens in kernel space
344 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich101f12a2006-03-23 02:59:45 -0800345 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Jan Beulich101f12a2006-03-23 02:59:45 -0800347 if (unlikely(address >= TASK_SIZE)) {
348 if (!(error_code & 0x0000000d) && vmalloc_fault(address) >= 0)
349 return;
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700350 if (notify_page_fault(regs))
Jan Beulich101f12a2006-03-23 02:59:45 -0800351 return;
352 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Don't take the mm semaphore here. If we fixup a prefetch
354 * fault we could otherwise deadlock.
355 */
356 goto bad_area_nosemaphore;
Jan Beulich101f12a2006-03-23 02:59:45 -0800357 }
358
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700359 if (notify_page_fault(regs))
Jan Beulich101f12a2006-03-23 02:59:45 -0800360 return;
361
362 /* It's safe to allow irq's after cr2 has been saved and the vmalloc
363 fault has been handled. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100364 if (regs->flags & (X86_EFLAGS_IF|VM_MASK))
Jan Beulich101f12a2006-03-23 02:59:45 -0800365 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 mm = tsk->mm;
368
369 /*
370 * If we're in an interrupt, have no user context or are running in an
Harvey Harrison33cb5242008-01-30 13:32:19 +0100371 * atomic region then we must not take the fault.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373 if (in_atomic() || !mm)
374 goto bad_area_nosemaphore;
375
376 /* When running in the kernel we expect faults to occur only to
377 * addresses in user space. All other faults represent errors in the
Simon Arlott27b46d72007-10-20 01:13:56 +0200378 * kernel and should generate an OOPS. Unfortunately, in the case of an
Adrian Bunk80f72282006-06-30 18:27:16 +0200379 * erroneous fault occurring in a code path which already holds mmap_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * we will deadlock attempting to validate the fault against the
381 * address space. Luckily the kernel only validly references user
382 * space from well defined areas of code, which are listed in the
383 * exceptions table.
384 *
385 * As the vast majority of faults will be valid we will only perform
Simon Arlott27b46d72007-10-20 01:13:56 +0200386 * the source reference check when there is a possibility of a deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Attempt to lock the address space, if we cannot we then validate the
388 * source. If this is invalid we can skip the address space check,
389 * thus avoiding the deadlock.
390 */
391 if (!down_read_trylock(&mm->mmap_sem)) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100392 if ((error_code & PF_USER) == 0 &&
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100393 !search_exception_tables(regs->ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto bad_area_nosemaphore;
395 down_read(&mm->mmap_sem);
396 }
397
398 vma = find_vma(mm, address);
399 if (!vma)
400 goto bad_area;
401 if (vma->vm_start <= address)
402 goto good_area;
403 if (!(vma->vm_flags & VM_GROWSDOWN))
404 goto bad_area;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100405 if (error_code & PF_USER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100407 * Accessing the stack below %sp is always a bug.
Chuck Ebbert21528452006-06-23 02:04:23 -0700408 * The large cushion allows instructions like enter
409 * and pusha to work. ("enter $65535,$31" pushes
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100410 * 32 pointers and then decrements %sp by 65535.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100412 if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 goto bad_area;
414 }
415 if (expand_stack(vma, address))
416 goto bad_area;
417/*
418 * Ok, we have a good vm_area for this memory access, so
419 * we can handle it..
420 */
421good_area:
Ingo Molnar869f96a2005-09-03 15:56:26 -0700422 si_code = SEGV_ACCERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 write = 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100424 switch (error_code & (PF_PROT|PF_WRITE)) {
425 default: /* 3: write, present */
426 /* fall through */
427 case PF_WRITE: /* write, not present */
428 if (!(vma->vm_flags & VM_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto bad_area;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100430 write++;
431 break;
432 case PF_PROT: /* read, present */
433 goto bad_area;
434 case 0: /* read, not present */
435 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
436 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
439 survive:
440 /*
441 * If for any reason at all we couldn't handle the fault,
442 * make sure we exit gracefully rather than endlessly redo
443 * the fault.
444 */
Nick Piggin83c54072007-07-19 01:47:05 -0700445 fault = handle_mm_fault(mm, vma, address, write);
446 if (unlikely(fault & VM_FAULT_ERROR)) {
447 if (fault & VM_FAULT_OOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto out_of_memory;
Nick Piggin83c54072007-07-19 01:47:05 -0700449 else if (fault & VM_FAULT_SIGBUS)
450 goto do_sigbus;
451 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Nick Piggin83c54072007-07-19 01:47:05 -0700453 if (fault & VM_FAULT_MAJOR)
454 tsk->maj_flt++;
455 else
456 tsk->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /*
459 * Did it hit the DOS screen memory VA from vm86 mode?
460 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100461 if (regs->flags & VM_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
463 if (bit < 32)
464 tsk->thread.screen_bitmap |= 1 << bit;
465 }
466 up_read(&mm->mmap_sem);
467 return;
468
469/*
470 * Something tried to access memory that isn't in our memory map..
471 * Fix it, but check if it's kernel or user first..
472 */
473bad_area:
474 up_read(&mm->mmap_sem);
475
476bad_area_nosemaphore:
477 /* User mode accesses just cause a SIGSEGV */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100478 if (error_code & PF_USER) {
Steven Rostedte5e3c842007-06-06 23:34:04 -0400479 /*
480 * It's possible to have interrupts off here.
481 */
482 local_irq_enable();
483
Harvey Harrison33cb5242008-01-30 13:32:19 +0100484 /*
485 * Valid to do another page fault here because this one came
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * from user space.
487 */
488 if (is_prefetch(regs, address, error_code))
489 return;
490
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200491 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
492 printk_ratelimit()) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100493 printk("%s%s[%d]: segfault at %08lx ip %08lx "
494 "sp %08lx error %lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700495 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100496 tsk->comm, task_pid_nr(tsk), address, regs->ip,
497 regs->sp, error_code);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 tsk->thread.cr2 = address;
500 /* Kernel addresses are always protection faults */
501 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
502 tsk->thread.trap_no = 14;
Ingo Molnar869f96a2005-09-03 15:56:26 -0700503 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return;
505 }
506
507#ifdef CONFIG_X86_F00F_BUG
508 /*
509 * Pentium F0 0F C7 C8 bug workaround.
510 */
511 if (boot_cpu_data.f00f_bug) {
512 unsigned long nr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 nr = (address - idt_descr.address) >> 3;
515
516 if (nr == 6) {
517 do_invalid_op(regs, 0);
518 return;
519 }
520 }
521#endif
522
523no_context:
524 /* Are we prepared to handle this kernel fault? */
525 if (fixup_exception(regs))
526 return;
527
Harvey Harrison33cb5242008-01-30 13:32:19 +0100528 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * Valid to do another page fault here, because if this fault
Harvey Harrison33cb5242008-01-30 13:32:19 +0100530 * had been triggered by is_prefetch fixup_exception would have
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * handled it.
532 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100533 if (is_prefetch(regs, address, error_code))
534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536/*
537 * Oops. The kernel tried to access some bad page. We'll have to
538 * terminate things with extreme prejudice.
539 */
540
541 bust_spinlocks(1);
542
Andrew Mortondd287792006-03-23 03:00:57 -0800543 if (oops_may_print()) {
Jan Beulich28609f62007-05-02 19:27:04 +0200544 __typeof__(pte_val(__pte(0))) page;
545
546#ifdef CONFIG_X86_PAE
Andrew Mortondd287792006-03-23 03:00:57 -0800547 if (error_code & 16) {
548 pte_t *pte = lookup_address(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Andrew Mortondd287792006-03-23 03:00:57 -0800550 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
551 printk(KERN_CRIT "kernel tried to execute "
552 "NX-protected page - exploit attempt? "
553 "(uid: %d)\n", current->uid);
554 }
Jan Beulich28609f62007-05-02 19:27:04 +0200555#endif
Andrew Mortondd287792006-03-23 03:00:57 -0800556 if (address < PAGE_SIZE)
557 printk(KERN_ALERT "BUG: unable to handle kernel NULL "
558 "pointer dereference");
559 else
560 printk(KERN_ALERT "BUG: unable to handle kernel paging"
561 " request");
Harvey Harrison33cb5242008-01-30 13:32:19 +0100562 printk(" at virtual address %08lx\n", address);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100563 printk(KERN_ALERT "printing ip: %08lx ", regs->ip);
Jan Beulich28609f62007-05-02 19:27:04 +0200564
565 page = read_cr3();
566 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
567#ifdef CONFIG_X86_PAE
Pavel Emelyanov9aa8d712007-10-17 18:04:40 +0200568 printk("*pdpt = %016Lx ", page);
Jan Beulich28609f62007-05-02 19:27:04 +0200569 if ((page >> PAGE_SHIFT) < max_low_pfn
570 && page & _PAGE_PRESENT) {
571 page &= PAGE_MASK;
572 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
573 & (PTRS_PER_PMD - 1)];
Alexey Dobriyaneec407c2007-10-24 12:58:02 +0200574 printk(KERN_CONT "*pde = %016Lx ", page);
Jan Beulich28609f62007-05-02 19:27:04 +0200575 page &= ~_PAGE_NX;
576 }
577#else
Pavel Emelyanov9aa8d712007-10-17 18:04:40 +0200578 printk("*pde = %08lx ", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
Jan Beulich28609f62007-05-02 19:27:04 +0200580
581 /*
582 * We must not directly access the pte in the highpte
583 * case if the page table is located in highmem.
584 * And let's rather not kmap-atomic the pte, just in case
585 * it's allocated already.
586 */
587 if ((page >> PAGE_SHIFT) < max_low_pfn
Jan Beulichb1992df2007-10-19 20:35:03 +0200588 && (page & _PAGE_PRESENT)
589 && !(page & _PAGE_PSE)) {
Jan Beulich28609f62007-05-02 19:27:04 +0200590 page &= PAGE_MASK;
591 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
592 & (PTRS_PER_PTE - 1)];
Pavel Emelyanov9aa8d712007-10-17 18:04:40 +0200593 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
Jan Beulich28609f62007-05-02 19:27:04 +0200594 }
Pavel Emelyanov9aa8d712007-10-17 18:04:40 +0200595
596 printk("\n");
Jan Beulich28609f62007-05-02 19:27:04 +0200597 }
598
Alexander Nyberg4f339ec2005-06-25 14:58:27 -0700599 tsk->thread.cr2 = address;
600 tsk->thread.trap_no = 14;
601 tsk->thread.error_code = error_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 die("Oops", regs, error_code);
603 bust_spinlocks(0);
604 do_exit(SIGKILL);
605
606/*
607 * We ran out of memory, or some other thing happened to us that made
608 * us unable to handle the page fault gracefully.
609 */
610out_of_memory:
611 up_read(&mm->mmap_sem);
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700612 if (is_global_init(tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 yield();
614 down_read(&mm->mmap_sem);
615 goto survive;
616 }
617 printk("VM: killing process %s\n", tsk->comm);
618 if (error_code & 4)
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700619 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto no_context;
621
622do_sigbus:
623 up_read(&mm->mmap_sem);
624
625 /* Kernel mode? Handle exceptions or die */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100626 if (!(error_code & PF_USER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto no_context;
628
629 /* User space => ok to do another page fault */
630 if (is_prefetch(regs, address, error_code))
631 return;
632
633 tsk->thread.cr2 = address;
634 tsk->thread.error_code = error_code;
635 tsk->thread.trap_no = 14;
Ingo Molnar869f96a2005-09-03 15:56:26 -0700636 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
Jan Beulich101f12a2006-03-23 02:59:45 -0800637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Jan Beulich101f12a2006-03-23 02:59:45 -0800639void vmalloc_sync_all(void)
640{
641 /*
642 * Note that races in the updates of insync and start aren't
643 * problematic: insync can only get set bits added, and updates to
644 * start are only improving performance (without affecting correctness
645 * if undone).
646 */
647 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
648 static unsigned long start = TASK_SIZE;
649 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jeremy Fitzhardinge5311ab62007-05-02 19:27:13 +0200651 if (SHARED_KERNEL_PMD)
652 return;
653
Jan Beulich101f12a2006-03-23 02:59:45 -0800654 BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
655 for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
656 if (!test_bit(pgd_index(address), insync)) {
657 unsigned long flags;
658 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Jan Beulich101f12a2006-03-23 02:59:45 -0800660 spin_lock_irqsave(&pgd_lock, flags);
661 for (page = pgd_list; page; page =
662 (struct page *)page->index)
663 if (!vmalloc_sync_one(page_address(page),
664 address)) {
665 BUG_ON(page != pgd_list);
666 break;
667 }
668 spin_unlock_irqrestore(&pgd_lock, flags);
669 if (!page)
670 set_bit(pgd_index(address), insync);
671 }
672 if (address == start && test_bit(pgd_index(address), insync))
673 start = address + PGDIR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675}