Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm26/mm/fault.c |
| 3 | * |
| 4 | * Copyright (C) 1995 Linus Torvalds |
| 5 | * Modifications for ARM processor (c) 1995-2001 Russell King |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/signal.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/mman.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/proc_fs.h> |
| 23 | #include <linux/init.h> |
| 24 | |
| 25 | #include <asm/system.h> |
| 26 | #include <asm/pgtable.h> |
| 27 | #include <asm/uaccess.h> //FIXME this header may be bogusly included |
| 28 | |
| 29 | #include "fault.h" |
| 30 | |
| 31 | #define FAULT_CODE_LDRSTRPOST 0x80 |
| 32 | #define FAULT_CODE_LDRSTRPRE 0x40 |
| 33 | #define FAULT_CODE_LDRSTRREG 0x20 |
| 34 | #define FAULT_CODE_LDMSTM 0x10 |
| 35 | #define FAULT_CODE_LDCSTC 0x08 |
| 36 | #define FAULT_CODE_PREFETCH 0x04 |
| 37 | #define FAULT_CODE_WRITE 0x02 |
| 38 | #define FAULT_CODE_FORCECOW 0x01 |
| 39 | |
| 40 | #define DO_COW(m) ((m) & (FAULT_CODE_WRITE|FAULT_CODE_FORCECOW)) |
| 41 | #define READ_FAULT(m) (!((m) & FAULT_CODE_WRITE)) |
| 42 | #define DEBUG |
| 43 | /* |
| 44 | * This is useful to dump out the page tables associated with |
| 45 | * 'addr' in mm 'mm'. |
| 46 | */ |
| 47 | void show_pte(struct mm_struct *mm, unsigned long addr) |
| 48 | { |
| 49 | pgd_t *pgd; |
| 50 | |
| 51 | if (!mm) |
| 52 | mm = &init_mm; |
| 53 | |
| 54 | printk(KERN_ALERT "pgd = %p\n", mm->pgd); |
| 55 | pgd = pgd_offset(mm, addr); |
| 56 | printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd)); |
| 57 | |
| 58 | do { |
| 59 | pmd_t *pmd; |
| 60 | pte_t *pte; |
| 61 | |
| 62 | pmd = pmd_offset(pgd, addr); |
| 63 | |
| 64 | if (pmd_none(*pmd)) |
| 65 | break; |
| 66 | |
| 67 | if (pmd_bad(*pmd)) { |
| 68 | printk("(bad)"); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | /* We must not map this if we have highmem enabled */ |
| 73 | /* FIXME */ |
| 74 | pte = pte_offset_map(pmd, addr); |
| 75 | printk(", *pte=%08lx", pte_val(*pte)); |
| 76 | pte_unmap(pte); |
| 77 | } while(0); |
| 78 | |
| 79 | printk("\n"); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Oops. The kernel tried to access some page that wasn't present. |
| 84 | */ |
| 85 | static void |
| 86 | __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, |
| 87 | struct pt_regs *regs) |
| 88 | { |
| 89 | /* |
| 90 | * Are we prepared to handle this kernel fault? |
| 91 | */ |
| 92 | if (fixup_exception(regs)) |
| 93 | return; |
| 94 | |
| 95 | /* |
| 96 | * No handler, we'll have to terminate things with extreme prejudice. |
| 97 | */ |
| 98 | bust_spinlocks(1); |
| 99 | printk(KERN_ALERT |
| 100 | "Unable to handle kernel %s at virtual address %08lx\n", |
| 101 | (addr < PAGE_SIZE) ? "NULL pointer dereference" : |
| 102 | "paging request", addr); |
| 103 | |
| 104 | show_pte(mm, addr); |
| 105 | die("Oops", regs, fsr); |
| 106 | bust_spinlocks(0); |
| 107 | do_exit(SIGKILL); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Something tried to access memory that isn't in our memory map.. |
| 112 | * User mode accesses just cause a SIGSEGV |
| 113 | */ |
| 114 | static void |
| 115 | __do_user_fault(struct task_struct *tsk, unsigned long addr, |
| 116 | unsigned int fsr, int code, struct pt_regs *regs) |
| 117 | { |
| 118 | struct siginfo si; |
| 119 | |
| 120 | #ifdef CONFIG_DEBUG_USER |
| 121 | printk("%s: unhandled page fault at 0x%08lx, code 0x%03x\n", |
| 122 | tsk->comm, addr, fsr); |
| 123 | show_pte(tsk->mm, addr); |
| 124 | show_regs(regs); |
| 125 | //dump_backtrace(regs, tsk); // FIXME ARM32 dropped this - why? |
| 126 | while(1); //FIXME - hack to stop debug going nutso |
| 127 | #endif |
| 128 | |
| 129 | tsk->thread.address = addr; |
| 130 | tsk->thread.error_code = fsr; |
| 131 | tsk->thread.trap_no = 14; |
| 132 | si.si_signo = SIGSEGV; |
| 133 | si.si_errno = 0; |
| 134 | si.si_code = code; |
| 135 | si.si_addr = (void *)addr; |
| 136 | force_sig_info(SIGSEGV, &si, tsk); |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, |
| 141 | struct task_struct *tsk) |
| 142 | { |
| 143 | struct vm_area_struct *vma; |
| 144 | int fault, mask; |
| 145 | |
| 146 | vma = find_vma(mm, addr); |
| 147 | fault = -2; /* bad map area */ |
| 148 | if (!vma) |
| 149 | goto out; |
| 150 | if (vma->vm_start > addr) |
| 151 | goto check_stack; |
| 152 | |
| 153 | /* |
| 154 | * Ok, we have a good vm_area for this |
| 155 | * memory access, so we can handle it. |
| 156 | */ |
| 157 | good_area: |
| 158 | if (READ_FAULT(fsr)) /* read? */ |
| 159 | mask = VM_READ|VM_EXEC; |
| 160 | else |
| 161 | mask = VM_WRITE; |
| 162 | |
| 163 | fault = -1; /* bad access type */ |
| 164 | if (!(vma->vm_flags & mask)) |
| 165 | goto out; |
| 166 | |
| 167 | /* |
| 168 | * If for any reason at all we couldn't handle |
| 169 | * the fault, make sure we exit gracefully rather |
| 170 | * than endlessly redo the fault. |
| 171 | */ |
| 172 | survive: |
| 173 | fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, DO_COW(fsr)); |
| 174 | |
| 175 | /* |
| 176 | * Handle the "normal" cases first - successful and sigbus |
| 177 | */ |
| 178 | switch (fault) { |
Linus Torvalds | 6e34622 | 2005-08-04 08:33:38 -0700 | [diff] [blame^] | 179 | case VM_FAULT_MAJOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | tsk->maj_flt++; |
| 181 | return fault; |
Linus Torvalds | 6e34622 | 2005-08-04 08:33:38 -0700 | [diff] [blame^] | 182 | case VM_FAULT_MINOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | tsk->min_flt++; |
Linus Torvalds | 6e34622 | 2005-08-04 08:33:38 -0700 | [diff] [blame^] | 184 | case VM_FAULT_SIGBUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return fault; |
| 186 | } |
| 187 | |
| 188 | fault = -3; /* out of memory */ |
| 189 | if (tsk->pid != 1) |
| 190 | goto out; |
| 191 | |
| 192 | /* |
| 193 | * If we are out of memory for pid1, |
| 194 | * sleep for a while and retry |
| 195 | */ |
| 196 | yield(); |
| 197 | goto survive; |
| 198 | |
| 199 | check_stack: |
| 200 | if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) |
| 201 | goto good_area; |
| 202 | out: |
| 203 | return fault; |
| 204 | } |
| 205 | |
| 206 | int do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) |
| 207 | { |
| 208 | struct task_struct *tsk; |
| 209 | struct mm_struct *mm; |
| 210 | int fault; |
| 211 | |
| 212 | tsk = current; |
| 213 | mm = tsk->mm; |
| 214 | |
| 215 | /* |
| 216 | * If we're in an interrupt or have no user |
| 217 | * context, we must not take the fault.. |
| 218 | */ |
| 219 | if (in_interrupt() || !mm) |
| 220 | goto no_context; |
| 221 | |
| 222 | down_read(&mm->mmap_sem); |
| 223 | fault = __do_page_fault(mm, addr, fsr, tsk); |
| 224 | up_read(&mm->mmap_sem); |
| 225 | |
| 226 | /* |
| 227 | * Handle the "normal" case first |
| 228 | */ |
Linus Torvalds | 6e34622 | 2005-08-04 08:33:38 -0700 | [diff] [blame^] | 229 | switch (fault) { |
| 230 | case VM_FAULT_MINOR: |
| 231 | case VM_FAULT_MAJOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return 0; |
Linus Torvalds | 6e34622 | 2005-08-04 08:33:38 -0700 | [diff] [blame^] | 233 | case VM_FAULT_SIGBUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | goto do_sigbus; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * If we are in kernel mode at this point, we |
| 239 | * have no context to handle this fault with. |
| 240 | * FIXME - is this test right? |
| 241 | */ |
| 242 | if (!user_mode(regs)){ |
| 243 | goto no_context; |
| 244 | } |
| 245 | |
| 246 | if (fault == -3) { |
| 247 | /* |
| 248 | * We ran out of memory, or some other thing happened to |
| 249 | * us that made us unable to handle the page fault gracefully. |
| 250 | */ |
| 251 | printk("VM: killing process %s\n", tsk->comm); |
| 252 | do_exit(SIGKILL); |
| 253 | } |
| 254 | else{ |
| 255 | __do_user_fault(tsk, addr, fsr, fault == -1 ? SEGV_ACCERR : SEGV_MAPERR, regs); |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | |
| 261 | /* |
| 262 | * We ran out of memory, or some other thing happened to us that made |
| 263 | * us unable to handle the page fault gracefully. |
| 264 | */ |
| 265 | do_sigbus: |
| 266 | /* |
| 267 | * Send a sigbus, regardless of whether we were in kernel |
| 268 | * or user mode. |
| 269 | */ |
| 270 | tsk->thread.address = addr; //FIXME - need other bits setting? |
| 271 | tsk->thread.error_code = fsr; |
| 272 | tsk->thread.trap_no = 14; |
| 273 | force_sig(SIGBUS, tsk); |
| 274 | #ifdef CONFIG_DEBUG_USER |
| 275 | printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lx\n", |
| 276 | current->comm, addr, instruction_pointer(regs)); |
| 277 | #endif |
| 278 | |
| 279 | /* Kernel mode? Handle exceptions or die */ |
| 280 | if (user_mode(regs)) |
| 281 | return 0; |
| 282 | |
| 283 | no_context: |
| 284 | __do_kernel_fault(mm, addr, fsr, regs); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Handle a data abort. Note that we have to handle a range of addresses |
| 290 | * on ARM2/3 for ldm. If both pages are zero-mapped, then we have to force |
| 291 | * a copy-on-write. However, on the second page, we always force COW. |
| 292 | */ |
| 293 | asmlinkage void |
| 294 | do_DataAbort(unsigned long min_addr, unsigned long max_addr, int mode, struct pt_regs *regs) |
| 295 | { |
| 296 | do_page_fault(min_addr, mode, regs); |
| 297 | |
| 298 | if ((min_addr ^ max_addr) >> PAGE_SHIFT){ |
| 299 | do_page_fault(max_addr, mode | FAULT_CODE_FORCECOW, regs); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | asmlinkage int |
| 304 | do_PrefetchAbort(unsigned long addr, struct pt_regs *regs) |
| 305 | { |
| 306 | #if 0 |
| 307 | if (the memc mapping for this page exists) { |
| 308 | printk ("Page in, but got abort (undefined instruction?)\n"); |
| 309 | return 0; |
| 310 | } |
| 311 | #endif |
| 312 | do_page_fault(addr, FAULT_CODE_PREFETCH, regs); |
| 313 | return 1; |
| 314 | } |
| 315 | |