Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #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 Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 20 | #include <linux/bootmem.h> /* for max_low_pfn */ |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 21 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 23 | #include <linux/kprobes.h> |
Andi Kleen | 11a4180 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 25 | #include <linux/kdebug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/desc.h> |
Rusty Russell | 78be370 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 29 | #include <asm/segment.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 31 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 45 | extern void die(const char *, struct pt_regs *, long); |
| 46 | |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 47 | static inline int notify_page_fault(struct pt_regs *regs) |
Anil S Keshavamurthy | b71b5b6 | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 48 | { |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 49 | #ifdef CONFIG_KPROBES |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 50 | int ret = 0; |
Anil S Keshavamurthy | b71b5b6 | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 51 | |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 52 | /* 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 Keshavamurthy | b71b5b6 | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 59 | |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 60 | return ret; |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 61 | #else |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 62 | return 0; |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 63 | #endif |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 64 | } |
Anil S Keshavamurthy | b71b5b6 | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 77 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * This is slow, but is very rarely executed. |
| 79 | */ |
| 80 | static inline unsigned long get_segment_eip(struct pt_regs *regs, |
| 81 | unsigned long *eip_limit) |
| 82 | { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 83 | unsigned long ip = regs->ip; |
| 84 | unsigned seg = regs->cs & 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | u32 seg_ar, seg_limit, base, *desc; |
| 86 | |
Chuck Ebbert | 19964fe | 2006-06-23 02:04:29 -0700 | [diff] [blame] | 87 | /* Unlikely, but must come before segment checks. */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 88 | if (unlikely(regs->flags & VM_MASK)) { |
Chuck Ebbert | 19964fe | 2006-06-23 02:04:29 -0700 | [diff] [blame] | 89 | base = seg << 4; |
| 90 | *eip_limit = base + 0xffff; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 91 | return base + (ip & 0xffff); |
Chuck Ebbert | 19964fe | 2006-06-23 02:04:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* The standard kernel/user address space limit. */ |
Rusty Russell | 78be370 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 95 | *eip_limit = user_mode(regs) ? USER_DS.seg : KERNEL_DS.seg; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* By far the most common cases. */ |
Rusty Russell | 78be370 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 98 | if (likely(SEGMENT_IS_FLAT_CODE(seg))) |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 99 | return ip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 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 Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 106 | if ((~seg_ar & 0x9800) || ip > seg_limit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | *eip_limit = 0; |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 108 | return 1; /* So that returned ip > *eip_limit. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 111 | /* Get the GDT/LDT descriptor base. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 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. Capitulino | de8aacb | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 116 | mutex_lock(¤t->mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | desc = current->mm->context.ldt; |
| 118 | desc = (void *)desc + (seg & ~7); |
| 119 | } else { |
| 120 | /* Must disable preemption while reading the GDT. */ |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 121 | desc = (u32 *)get_cpu_gdt_table(get_cpu()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | desc = (void *)desc + (seg & ~7); |
| 123 | } |
| 124 | |
| 125 | /* Decode the code segment base from the descriptor */ |
Glauber de Oliveira Costa | cc69785 | 2008-01-30 13:31:14 +0100 | [diff] [blame] | 126 | base = get_desc_base((struct desc_struct *)desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 128 | if (seg & (1<<2)) |
Luiz Fernando N. Capitulino | de8aacb | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 129 | mutex_unlock(¤t->mm->context.lock); |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 130 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 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 Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 138 | return ip + base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 141 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. |
| 143 | * Check that here and ignore it. |
| 144 | */ |
| 145 | static int __is_prefetch(struct pt_regs *regs, unsigned long addr) |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 146 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | unsigned long limit; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 148 | unsigned char *instr = (unsigned char *)get_segment_eip(regs, &limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int scan_more = 1; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 150 | int prefetch = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | int i; |
| 152 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 153 | for (i = 0; scan_more && i < 15; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | unsigned char opcode; |
| 155 | unsigned char instr_hi; |
| 156 | unsigned char instr_lo; |
| 157 | |
Andi Kleen | 11a4180 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 158 | if (instr > (unsigned char *)limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | break; |
Andi Kleen | 11a4180 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 160 | if (probe_kernel_address(instr, opcode)) |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 161 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 163 | instr_hi = opcode & 0xf0; |
| 164 | instr_lo = opcode & 0x0f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | instr++; |
| 166 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 167 | switch (instr_hi) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | case 0x20: |
| 169 | case 0x30: |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 170 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | scan_more = ((instr_lo & 7) == 0x6); |
| 177 | break; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 178 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | case 0x60: |
| 191 | /* 0x64 thru 0x67 are valid prefixes in all modes. */ |
| 192 | scan_more = (instr_lo & 0xC) == 0x4; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 193 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | case 0xF0: |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 195 | /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | scan_more = !instr_lo || (instr_lo>>1) == 1; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 197 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | case 0x00: |
| 199 | /* Prefetch instruction is 0x0F0D or 0x0F18 */ |
| 200 | scan_more = 0; |
Andi Kleen | 11a4180 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 201 | if (instr > (unsigned char *)limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | break; |
Andi Kleen | 11a4180 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 203 | if (probe_kernel_address(instr, opcode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | break; |
| 205 | prefetch = (instr_lo == 0xF) && |
| 206 | (opcode == 0x0D || opcode == 0x18); |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 207 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | default: |
| 209 | scan_more = 0; |
| 210 | break; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | return prefetch; |
| 214 | } |
| 215 | |
| 216 | static 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Ingo Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 229 | static 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 Harrison | 75604d7 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 241 | void do_invalid_op(struct pt_regs *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 243 | static 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 Amsden | 8b14cb9 | 2007-08-21 18:30:36 -0700 | [diff] [blame] | 271 | if (!pmd_present(*pmd)) { |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 272 | set_pmd(pmd, *pmd_k); |
Zachary Amsden | 8b14cb9 | 2007-08-21 18:30:36 -0700 | [diff] [blame] | 273 | arch_flush_lazy_mmu_mode(); |
| 274 | } else |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 275 | 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 | */ |
| 284 | static 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 Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 306 | int show_unhandled_signals = 1; |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Harvey Harrison | 75604d7 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 313 | void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | struct task_struct *tsk; |
| 316 | struct mm_struct *mm; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 317 | struct vm_area_struct *vma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | unsigned long address; |
Ingo Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 319 | int write, si_code; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 320 | int fault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Peter Zijlstra | 143a5d3 | 2007-10-25 14:01:10 +0200 | [diff] [blame] | 322 | /* |
| 323 | * We can fault from pretty much anywhere, with unknown IRQ state. |
| 324 | */ |
| 325 | trace_hardirqs_fixup(); |
| 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | /* get the address */ |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 328 | address = read_cr2(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | tsk = current; |
| 331 | |
Ingo Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 332 | si_code = SEGV_MAPERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 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 Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 345 | * protection error (error_code & 9) == 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | */ |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 347 | if (unlikely(address >= TASK_SIZE)) { |
| 348 | if (!(error_code & 0x0000000d) && vmalloc_fault(address) >= 0) |
| 349 | return; |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 350 | if (notify_page_fault(regs)) |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 351 | return; |
| 352 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * 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 Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 359 | if (notify_page_fault(regs)) |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 360 | 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 Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 364 | if (regs->flags & (X86_EFLAGS_IF|VM_MASK)) |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 365 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | mm = tsk->mm; |
| 368 | |
| 369 | /* |
| 370 | * If we're in an interrupt, have no user context or are running in an |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 371 | * atomic region then we must not take the fault. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | */ |
| 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 Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 378 | * kernel and should generate an OOPS. Unfortunately, in the case of an |
Adrian Bunk | 80f7228 | 2006-06-30 18:27:16 +0200 | [diff] [blame] | 379 | * erroneous fault occurring in a code path which already holds mmap_sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * 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 Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 386 | * the source reference check when there is a possibility of a deadlock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | * 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 392 | if ((error_code & PF_USER) == 0 && |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 393 | !search_exception_tables(regs->ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 405 | if (error_code & PF_USER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 407 | * Accessing the stack below %sp is always a bug. |
Chuck Ebbert | 2152845 | 2006-06-23 02:04:23 -0700 | [diff] [blame] | 408 | * The large cushion allows instructions like enter |
| 409 | * and pusha to work. ("enter $65535,$31" pushes |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 410 | * 32 pointers and then decrements %sp by 65535.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 412 | if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | 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 | */ |
| 421 | good_area: |
Ingo Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 422 | si_code = SEGV_ACCERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | write = 0; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 424 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | goto bad_area; |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 430 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 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 Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 445 | fault = handle_mm_fault(mm, vma, address, write); |
| 446 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 447 | if (fault & VM_FAULT_OOM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | goto out_of_memory; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 449 | else if (fault & VM_FAULT_SIGBUS) |
| 450 | goto do_sigbus; |
| 451 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 453 | if (fault & VM_FAULT_MAJOR) |
| 454 | tsk->maj_flt++; |
| 455 | else |
| 456 | tsk->min_flt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Did it hit the DOS screen memory VA from vm86 mode? |
| 460 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 461 | if (regs->flags & VM_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | 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 | */ |
| 473 | bad_area: |
| 474 | up_read(&mm->mmap_sem); |
| 475 | |
| 476 | bad_area_nosemaphore: |
| 477 | /* User mode accesses just cause a SIGSEGV */ |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 478 | if (error_code & PF_USER) { |
Steven Rostedt | e5e3c84 | 2007-06-06 23:34:04 -0400 | [diff] [blame] | 479 | /* |
| 480 | * It's possible to have interrupts off here. |
| 481 | */ |
| 482 | local_irq_enable(); |
| 483 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 484 | /* |
| 485 | * Valid to do another page fault here because this one came |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | * from user space. |
| 487 | */ |
| 488 | if (is_prefetch(regs, address, error_code)) |
| 489 | return; |
| 490 | |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 491 | if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) && |
| 492 | printk_ratelimit()) { |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 493 | printk("%s%s[%d]: segfault at %08lx ip %08lx " |
| 494 | "sp %08lx error %lx\n", |
Alexey Dobriyan | 19c5870 | 2007-10-18 23:40:41 -0700 | [diff] [blame] | 495 | task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 496 | tsk->comm, task_pid_nr(tsk), address, regs->ip, |
| 497 | regs->sp, error_code); |
Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | 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 Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 503 | force_sig_info_fault(SIGSEGV, si_code, address, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | nr = (address - idt_descr.address) >> 3; |
| 515 | |
| 516 | if (nr == 6) { |
| 517 | do_invalid_op(regs, 0); |
| 518 | return; |
| 519 | } |
| 520 | } |
| 521 | #endif |
| 522 | |
| 523 | no_context: |
| 524 | /* Are we prepared to handle this kernel fault? */ |
| 525 | if (fixup_exception(regs)) |
| 526 | return; |
| 527 | |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 528 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | * Valid to do another page fault here, because if this fault |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 530 | * had been triggered by is_prefetch fixup_exception would have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | * handled it. |
| 532 | */ |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 533 | if (is_prefetch(regs, address, error_code)) |
| 534 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 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 Morton | dd28779 | 2006-03-23 03:00:57 -0800 | [diff] [blame] | 543 | if (oops_may_print()) { |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 544 | __typeof__(pte_val(__pte(0))) page; |
| 545 | |
| 546 | #ifdef CONFIG_X86_PAE |
Andrew Morton | dd28779 | 2006-03-23 03:00:57 -0800 | [diff] [blame] | 547 | if (error_code & 16) { |
| 548 | pte_t *pte = lookup_address(address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Andrew Morton | dd28779 | 2006-03-23 03:00:57 -0800 | [diff] [blame] | 550 | 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 Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 555 | #endif |
Andrew Morton | dd28779 | 2006-03-23 03:00:57 -0800 | [diff] [blame] | 556 | 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 Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 562 | printk(" at virtual address %08lx\n", address); |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 563 | printk(KERN_ALERT "printing ip: %08lx ", regs->ip); |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 564 | |
| 565 | page = read_cr3(); |
| 566 | page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT]; |
| 567 | #ifdef CONFIG_X86_PAE |
Pavel Emelyanov | 9aa8d71 | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 568 | printk("*pdpt = %016Lx ", page); |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 569 | 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 Dobriyan | eec407c | 2007-10-24 12:58:02 +0200 | [diff] [blame] | 574 | printk(KERN_CONT "*pde = %016Lx ", page); |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 575 | page &= ~_PAGE_NX; |
| 576 | } |
| 577 | #else |
Pavel Emelyanov | 9aa8d71 | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 578 | printk("*pde = %08lx ", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | #endif |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 580 | |
| 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 Beulich | b1992df | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 588 | && (page & _PAGE_PRESENT) |
| 589 | && !(page & _PAGE_PSE)) { |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 590 | page &= PAGE_MASK; |
| 591 | page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT) |
| 592 | & (PTRS_PER_PTE - 1)]; |
Pavel Emelyanov | 9aa8d71 | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 593 | printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page); |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 594 | } |
Pavel Emelyanov | 9aa8d71 | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 595 | |
| 596 | printk("\n"); |
Jan Beulich | 28609f6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 597 | } |
| 598 | |
Alexander Nyberg | 4f339ec | 2005-06-25 14:58:27 -0700 | [diff] [blame] | 599 | tsk->thread.cr2 = address; |
| 600 | tsk->thread.trap_no = 14; |
| 601 | tsk->thread.error_code = error_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | 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 | */ |
| 610 | out_of_memory: |
| 611 | up_read(&mm->mmap_sem); |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 612 | if (is_global_init(tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | 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 Schmidt | dcca2bd | 2007-10-16 01:24:18 -0700 | [diff] [blame] | 619 | do_group_exit(SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | goto no_context; |
| 621 | |
| 622 | do_sigbus: |
| 623 | up_read(&mm->mmap_sem); |
| 624 | |
| 625 | /* Kernel mode? Handle exceptions or die */ |
Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame^] | 626 | if (!(error_code & PF_USER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | 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 Molnar | 869f96a | 2005-09-03 15:56:26 -0700 | [diff] [blame] | 636 | force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk); |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 639 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Jeremy Fitzhardinge | 5311ab6 | 2007-05-02 19:27:13 +0200 | [diff] [blame] | 651 | if (SHARED_KERNEL_PMD) |
| 652 | return; |
| 653 | |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 654 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Jan Beulich | 101f12a | 2006-03-23 02:59:45 -0800 | [diff] [blame] | 660 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | } |