Gennady Sharapov | ea2ba7d | 2006-01-08 01:01:31 -0800 | [diff] [blame] | 1 | /* |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/sched.h> |
| 8 | #include <linux/hardirq.h> |
| 9 | #include <asm/current.h> |
| 10 | #include <asm/pgtable.h> |
| 11 | #include <asm/tlbflush.h> |
Jeff Dike | eb83075 | 2007-05-06 14:51:07 -0700 | [diff] [blame] | 12 | #include "arch.h" |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 13 | #include "as-layout.h" |
| 14 | #include "kern_util.h" |
| 15 | #include "os.h" |
Gennady Sharapov | c66fdd5 | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 16 | #include "sysdep/sigcontext.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 18 | /* |
| 19 | * Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by |
| 20 | * segv(). |
| 21 | */ |
Jeff Dike | 1d3468a | 2006-07-10 04:45:13 -0700 | [diff] [blame] | 22 | int handle_page_fault(unsigned long address, unsigned long ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | int is_write, int is_user, int *code_out) |
| 24 | { |
| 25 | struct mm_struct *mm = current->mm; |
| 26 | struct vm_area_struct *vma; |
| 27 | pgd_t *pgd; |
| 28 | pud_t *pud; |
| 29 | pmd_t *pmd; |
| 30 | pte_t *pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int err = -EFAULT; |
| 32 | |
| 33 | *code_out = SEGV_MAPERR; |
Paolo 'Blaisorblade' Giarrusso | fea03cb | 2005-09-22 21:44:20 -0700 | [diff] [blame] | 34 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 35 | /* |
| 36 | * If the fault was during atomic operation, don't take the fault, just |
| 37 | * fail. |
| 38 | */ |
Paolo 'Blaisorblade' Giarrusso | fea03cb | 2005-09-22 21:44:20 -0700 | [diff] [blame] | 39 | if (in_atomic()) |
| 40 | goto out_nosemaphore; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | down_read(&mm->mmap_sem); |
| 43 | vma = find_vma(mm, address); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 44 | if (!vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | goto out; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 46 | else if (vma->vm_start <= address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | goto good_area; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 48 | else if (!(vma->vm_flags & VM_GROWSDOWN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | goto out; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 50 | else if (is_user && !ARCH_IS_STACKGROW(address)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | goto out; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 52 | else if (expand_stack(vma, address)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | goto out; |
| 54 | |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 55 | good_area: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | *code_out = SEGV_ACCERR; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 57 | if (is_write && !(vma->vm_flags & VM_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | goto out; |
Jeff Dike | 13479d5 | 2005-05-20 13:59:08 -0700 | [diff] [blame] | 59 | |
Paolo 'Blaisorblade' Giarrusso | d129f31 | 2005-09-10 19:44:57 +0200 | [diff] [blame] | 60 | /* Don't require VM_READ|VM_EXEC for write faults! */ |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 61 | if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC))) |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 62 | goto out; |
Jeff Dike | 13479d5 | 2005-05-20 13:59:08 -0700 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | do { |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 65 | int fault; |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 66 | survive: |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 67 | fault = handle_mm_fault(mm, vma, address, is_write); |
| 68 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 69 | if (fault & VM_FAULT_OOM) { |
| 70 | err = -ENOMEM; |
| 71 | goto out_of_memory; |
| 72 | } else if (fault & VM_FAULT_SIGBUS) { |
| 73 | err = -EACCES; |
| 74 | goto out; |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | BUG(); |
| 77 | } |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 78 | if (fault & VM_FAULT_MAJOR) |
| 79 | current->maj_flt++; |
| 80 | else |
| 81 | current->min_flt++; |
| 82 | |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 83 | pgd = pgd_offset(mm, address); |
| 84 | pud = pud_offset(pgd, address); |
| 85 | pmd = pmd_offset(pud, address); |
| 86 | pte = pte_offset_kernel(pmd, address); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 87 | } while (!pte_present(*pte)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | err = 0; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 89 | /* |
| 90 | * The below warning was added in place of |
Paolo 'Blaisorblade' Giarrusso | cbc24af | 2005-11-13 16:07:04 -0800 | [diff] [blame] | 91 | * pte_mkyoung(); if (is_write) pte_mkdirty(); |
| 92 | * If it's triggered, we'd see normally a hang here (a clean pte is |
| 93 | * marked read-only to emulate the dirty bit). |
| 94 | * However, the generic code can mark a PTE writable but clean on a |
| 95 | * concurrent read fault, triggering this harmlessly. So comment it out. |
| 96 | */ |
| 97 | #if 0 |
Paolo 'Blaisorblade' Giarrusso | 16b0367 | 2005-09-10 19:44:58 +0200 | [diff] [blame] | 98 | WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte))); |
Paolo 'Blaisorblade' Giarrusso | cbc24af | 2005-11-13 16:07:04 -0800 | [diff] [blame] | 99 | #endif |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 100 | flush_tlb_page(vma, address); |
| 101 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | up_read(&mm->mmap_sem); |
Paolo 'Blaisorblade' Giarrusso | fea03cb | 2005-09-22 21:44:20 -0700 | [diff] [blame] | 103 | out_nosemaphore: |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 104 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * We ran out of memory, or some other thing happened to us that made |
| 108 | * us unable to handle the page fault gracefully. |
| 109 | */ |
| 110 | out_of_memory: |
Sukadev Bhattiprolu | f400e19 | 2006-09-29 02:00:07 -0700 | [diff] [blame] | 111 | if (is_init(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | up_read(&mm->mmap_sem); |
| 113 | yield(); |
| 114 | down_read(&mm->mmap_sem); |
| 115 | goto survive; |
| 116 | } |
| 117 | goto out; |
| 118 | } |
| 119 | |
Jeff Dike | 27aa6ef | 2007-02-10 01:44:14 -0800 | [diff] [blame] | 120 | static void bad_segv(struct faultinfo fi, unsigned long ip) |
| 121 | { |
| 122 | struct siginfo si; |
| 123 | |
| 124 | si.si_signo = SIGSEGV; |
| 125 | si.si_code = SEGV_ACCERR; |
| 126 | si.si_addr = (void __user *) FAULT_ADDRESS(fi); |
| 127 | current->thread.arch.faultinfo = fi; |
| 128 | force_sig_info(SIGSEGV, &si, current); |
| 129 | } |
| 130 | |
| 131 | static void segv_handler(int sig, union uml_pt_regs *regs) |
Gennady Sharapov | c66fdd5 | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 132 | { |
| 133 | struct faultinfo * fi = UPT_FAULTINFO(regs); |
| 134 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 135 | if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) { |
Gennady Sharapov | c66fdd5 | 2006-01-08 01:01:32 -0800 | [diff] [blame] | 136 | bad_segv(*fi, UPT_IP(regs)); |
| 137 | return; |
| 138 | } |
| 139 | segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs); |
| 140 | } |
| 141 | |
Bodo Stroesser | c578455 | 2005-05-05 16:15:31 -0700 | [diff] [blame] | 142 | /* |
| 143 | * We give a *copy* of the faultinfo in the regs to segv. |
| 144 | * This must be done, since nesting SEGVs could overwrite |
| 145 | * the info in the regs. A pointer to the info then would |
| 146 | * give us bad data! |
| 147 | */ |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 148 | unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, |
| 149 | union uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct siginfo si; |
| 152 | void *catcher; |
| 153 | int err; |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 154 | int is_write = FAULT_WRITE(fi); |
| 155 | unsigned long address = FAULT_ADDRESS(fi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 157 | if (!is_user && (address >= start_vm) && (address < end_vm)) { |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 158 | flush_tlb_kernel_vm(); |
| 159 | return 0; |
| 160 | } |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 161 | else if (current->mm == NULL) { |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 162 | show_regs(container_of(regs, struct pt_regs, regs)); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 163 | panic("Segfault with no mm"); |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 164 | } |
Paolo 'Blaisorblade' Giarrusso | 546fe1c | 2005-09-22 21:44:16 -0700 | [diff] [blame] | 165 | |
Paolo 'Blaisorblade' Giarrusso | be662a1 | 2005-09-30 11:58:59 -0700 | [diff] [blame] | 166 | if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi)) |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 167 | err = handle_page_fault(address, ip, is_write, is_user, |
| 168 | &si.si_code); |
Paolo 'Blaisorblade' Giarrusso | 546fe1c | 2005-09-22 21:44:16 -0700 | [diff] [blame] | 169 | else { |
| 170 | err = -EFAULT; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 171 | /* |
| 172 | * A thread accessed NULL, we get a fault, but CR2 is invalid. |
| 173 | * This code is used in __do_copy_from_user() of TT mode. |
| 174 | * XXX tt mode is gone, so maybe this isn't needed any more |
| 175 | */ |
Paolo 'Blaisorblade' Giarrusso | 546fe1c | 2005-09-22 21:44:16 -0700 | [diff] [blame] | 176 | address = 0; |
| 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | catcher = current->thread.fault_catcher; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 180 | if (!err) |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 181 | return 0; |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 182 | else if (catcher != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | current->thread.fault_addr = (void *) address; |
| 184 | do_longjmp(catcher, 1); |
Jeff Dike | 1d3468a | 2006-07-10 04:45:13 -0700 | [diff] [blame] | 185 | } |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 186 | else if (current->thread.fault_addr != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | panic("fault_addr set but no fault catcher"); |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 188 | else if (!is_user && arch_fixup(ip, regs)) |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 189 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 191 | if (!is_user) { |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 192 | show_regs(container_of(regs, struct pt_regs, regs)); |
Jeff Dike | 1d3468a | 2006-07-10 04:45:13 -0700 | [diff] [blame] | 193 | panic("Kernel mode fault at addr 0x%lx, ip 0x%lx", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | address, ip); |
Jeff Dike | 377fad3 | 2007-05-06 14:51:25 -0700 | [diff] [blame] | 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 197 | if (err == -EACCES) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | si.si_signo = SIGBUS; |
| 199 | si.si_errno = 0; |
| 200 | si.si_code = BUS_ADRERR; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 201 | si.si_addr = (void __user *)address; |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 202 | current->thread.arch.faultinfo = fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | force_sig_info(SIGBUS, &si, current); |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 204 | } else if (err == -ENOMEM) { |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 205 | printk(KERN_INFO "VM: killing process %s\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | do_exit(SIGKILL); |
Paolo 'Blaisorblade' Giarrusso | 3b52166 | 2005-09-03 15:57:26 -0700 | [diff] [blame] | 207 | } else { |
| 208 | BUG_ON(err != -EFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | si.si_signo = SIGSEGV; |
Al Viro | 4d338e1 | 2006-03-31 02:30:15 -0800 | [diff] [blame] | 210 | si.si_addr = (void __user *) address; |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 211 | current->thread.arch.faultinfo = fi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | force_sig_info(SIGSEGV, &si, current); |
| 213 | } |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 214 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | void relay_signal(int sig, union uml_pt_regs *regs) |
| 218 | { |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 219 | if (arch_handle_signal(sig, regs)) |
Jeff Dike | 6edf428 | 2006-09-25 23:33:03 -0700 | [diff] [blame] | 220 | return; |
| 221 | |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 222 | if (!UPT_IS_USER(regs)) { |
| 223 | if (sig == SIGBUS) |
| 224 | printk(KERN_ERR "Bus error - the host /dev/shm or /tmp " |
| 225 | "mount likely just ran out of space\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | panic("Kernel mode signal %d", sig); |
Jeff Dike | 6edf428 | 2006-09-25 23:33:03 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jeff Dike | 5d86456 | 2007-05-06 14:51:24 -0700 | [diff] [blame] | 229 | current->thread.arch.faultinfo = *UPT_FAULTINFO(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | force_sig(sig, current); |
| 231 | } |
| 232 | |
Jeff Dike | 27aa6ef | 2007-02-10 01:44:14 -0800 | [diff] [blame] | 233 | static void bus_handler(int sig, union uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Jeff Dike | 4c9e138 | 2007-10-16 01:26:54 -0700 | [diff] [blame^] | 235 | if (current->thread.fault_catcher != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | do_longjmp(current->thread.fault_catcher, 1); |
| 237 | else relay_signal(sig, regs); |
| 238 | } |
| 239 | |
Jeff Dike | 27aa6ef | 2007-02-10 01:44:14 -0800 | [diff] [blame] | 240 | static void winch(int sig, union uml_pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | do_IRQ(WINCH_IRQ, regs); |
| 243 | } |
| 244 | |
Jeff Dike | 53dd2b5 | 2006-09-27 01:50:37 -0700 | [diff] [blame] | 245 | const struct kern_handlers handlinfo_kern = { |
| 246 | .relay_signal = relay_signal, |
| 247 | .winch = winch, |
| 248 | .bus_handler = bus_handler, |
| 249 | .page_fault = segv_handler, |
| 250 | .sigio_handler = sigio_handler, |
| 251 | .timer_handler = timer_handler |
| 252 | }; |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | void trap_init(void) |
| 255 | { |
| 256 | } |