Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.5 478 lines 12 kB view raw
1/* 2 * linux/arch/unicore32/mm/fault.c 3 * 4 * Code specific to PKUnity SoC and UniCore ISA 5 * 6 * Copyright (C) 2001-2010 GUAN Xue-tao 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12#include <linux/module.h> 13#include <linux/signal.h> 14#include <linux/mm.h> 15#include <linux/hardirq.h> 16#include <linux/init.h> 17#include <linux/kprobes.h> 18#include <linux/uaccess.h> 19#include <linux/page-flags.h> 20#include <linux/sched.h> 21#include <linux/io.h> 22 23#include <asm/pgtable.h> 24#include <asm/tlbflush.h> 25 26/* 27 * Fault status register encodings. We steal bit 31 for our own purposes. 28 */ 29#define FSR_LNX_PF (1 << 31) 30 31static inline int fsr_fs(unsigned int fsr) 32{ 33 /* xyabcde will be abcde+xy */ 34 return (fsr & 31) + ((fsr & (3 << 5)) >> 5); 35} 36 37/* 38 * This is useful to dump out the page tables associated with 39 * 'addr' in mm 'mm'. 40 */ 41void show_pte(struct mm_struct *mm, unsigned long addr) 42{ 43 pgd_t *pgd; 44 45 if (!mm) 46 mm = &init_mm; 47 48 printk(KERN_ALERT "pgd = %p\n", mm->pgd); 49 pgd = pgd_offset(mm, addr); 50 printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd)); 51 52 do { 53 pmd_t *pmd; 54 pte_t *pte; 55 56 if (pgd_none(*pgd)) 57 break; 58 59 if (pgd_bad(*pgd)) { 60 printk("(bad)"); 61 break; 62 } 63 64 pmd = pmd_offset((pud_t *) pgd, addr); 65 if (PTRS_PER_PMD != 1) 66 printk(", *pmd=%08lx", pmd_val(*pmd)); 67 68 if (pmd_none(*pmd)) 69 break; 70 71 if (pmd_bad(*pmd)) { 72 printk("(bad)"); 73 break; 74 } 75 76 /* We must not map this if we have highmem enabled */ 77 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) 78 break; 79 80 pte = pte_offset_map(pmd, addr); 81 printk(", *pte=%08lx", pte_val(*pte)); 82 pte_unmap(pte); 83 } while (0); 84 85 printk("\n"); 86} 87 88/* 89 * Oops. The kernel tried to access some page that wasn't present. 90 */ 91static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, 92 unsigned int fsr, struct pt_regs *regs) 93{ 94 /* 95 * Are we prepared to handle this kernel fault? 96 */ 97 if (fixup_exception(regs)) 98 return; 99 100 /* 101 * No handler, we'll have to terminate things with extreme prejudice. 102 */ 103 bust_spinlocks(1); 104 printk(KERN_ALERT 105 "Unable to handle kernel %s at virtual address %08lx\n", 106 (addr < PAGE_SIZE) ? "NULL pointer dereference" : 107 "paging request", addr); 108 109 show_pte(mm, addr); 110 die("Oops", regs, fsr); 111 bust_spinlocks(0); 112 do_exit(SIGKILL); 113} 114 115/* 116 * Something tried to access memory that isn't in our memory map.. 117 * User mode accesses just cause a SIGSEGV 118 */ 119static void __do_user_fault(struct task_struct *tsk, unsigned long addr, 120 unsigned int fsr, unsigned int sig, int code, 121 struct pt_regs *regs) 122{ 123 struct siginfo si; 124 125 tsk->thread.address = addr; 126 tsk->thread.error_code = fsr; 127 tsk->thread.trap_no = 14; 128 si.si_signo = sig; 129 si.si_errno = 0; 130 si.si_code = code; 131 si.si_addr = (void __user *)addr; 132 force_sig_info(sig, &si, tsk); 133} 134 135void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs) 136{ 137 struct task_struct *tsk = current; 138 struct mm_struct *mm = tsk->active_mm; 139 140 /* 141 * If we are in kernel mode at this point, we 142 * have no context to handle this fault with. 143 */ 144 if (user_mode(regs)) 145 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs); 146 else 147 __do_kernel_fault(mm, addr, fsr, regs); 148} 149 150#define VM_FAULT_BADMAP 0x010000 151#define VM_FAULT_BADACCESS 0x020000 152 153/* 154 * Check that the permissions on the VMA allow for the fault which occurred. 155 * If we encountered a write fault, we must have write permission, otherwise 156 * we allow any permission. 157 */ 158static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma) 159{ 160 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC; 161 162 if (!(fsr ^ 0x12)) /* write? */ 163 mask = VM_WRITE; 164 if (fsr & FSR_LNX_PF) 165 mask = VM_EXEC; 166 167 return vma->vm_flags & mask ? false : true; 168} 169 170static int __do_pf(struct mm_struct *mm, unsigned long addr, unsigned int fsr, 171 struct task_struct *tsk) 172{ 173 struct vm_area_struct *vma; 174 int fault; 175 176 vma = find_vma(mm, addr); 177 fault = VM_FAULT_BADMAP; 178 if (unlikely(!vma)) 179 goto out; 180 if (unlikely(vma->vm_start > addr)) 181 goto check_stack; 182 183 /* 184 * Ok, we have a good vm_area for this 185 * memory access, so we can handle it. 186 */ 187good_area: 188 if (access_error(fsr, vma)) { 189 fault = VM_FAULT_BADACCESS; 190 goto out; 191 } 192 193 /* 194 * If for any reason at all we couldn't handle the fault, make 195 * sure we exit gracefully rather than endlessly redo the fault. 196 */ 197 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, 198 (!(fsr ^ 0x12)) ? FAULT_FLAG_WRITE : 0); 199 if (unlikely(fault & VM_FAULT_ERROR)) 200 return fault; 201 if (fault & VM_FAULT_MAJOR) 202 tsk->maj_flt++; 203 else 204 tsk->min_flt++; 205 return fault; 206 207check_stack: 208 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) 209 goto good_area; 210out: 211 return fault; 212} 213 214static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs) 215{ 216 struct task_struct *tsk; 217 struct mm_struct *mm; 218 int fault, sig, code; 219 220 tsk = current; 221 mm = tsk->mm; 222 223 /* 224 * If we're in an interrupt or have no user 225 * context, we must not take the fault.. 226 */ 227 if (in_atomic() || !mm) 228 goto no_context; 229 230 /* 231 * As per x86, we may deadlock here. However, since the kernel only 232 * validly references user space from well defined areas of the code, 233 * we can bug out early if this is from code which shouldn't. 234 */ 235 if (!down_read_trylock(&mm->mmap_sem)) { 236 if (!user_mode(regs) 237 && !search_exception_tables(regs->UCreg_pc)) 238 goto no_context; 239 down_read(&mm->mmap_sem); 240 } else { 241 /* 242 * The above down_read_trylock() might have succeeded in 243 * which case, we'll have missed the might_sleep() from 244 * down_read() 245 */ 246 might_sleep(); 247#ifdef CONFIG_DEBUG_VM 248 if (!user_mode(regs) && 249 !search_exception_tables(regs->UCreg_pc)) 250 goto no_context; 251#endif 252 } 253 254 fault = __do_pf(mm, addr, fsr, tsk); 255 up_read(&mm->mmap_sem); 256 257 /* 258 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR 259 */ 260 if (likely(!(fault & 261 (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS)))) 262 return 0; 263 264 if (fault & VM_FAULT_OOM) { 265 /* 266 * We ran out of memory, call the OOM killer, and return to 267 * userspace (which will retry the fault, or kill us if we 268 * got oom-killed) 269 */ 270 pagefault_out_of_memory(); 271 return 0; 272 } 273 274 /* 275 * If we are in kernel mode at this point, we 276 * have no context to handle this fault with. 277 */ 278 if (!user_mode(regs)) 279 goto no_context; 280 281 if (fault & VM_FAULT_SIGBUS) { 282 /* 283 * We had some memory, but were unable to 284 * successfully fix up this page fault. 285 */ 286 sig = SIGBUS; 287 code = BUS_ADRERR; 288 } else { 289 /* 290 * Something tried to access memory that 291 * isn't in our memory map.. 292 */ 293 sig = SIGSEGV; 294 code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR; 295 } 296 297 __do_user_fault(tsk, addr, fsr, sig, code, regs); 298 return 0; 299 300no_context: 301 __do_kernel_fault(mm, addr, fsr, regs); 302 return 0; 303} 304 305/* 306 * First Level Translation Fault Handler 307 * 308 * We enter here because the first level page table doesn't contain 309 * a valid entry for the address. 310 * 311 * If the address is in kernel space (>= TASK_SIZE), then we are 312 * probably faulting in the vmalloc() area. 313 * 314 * If the init_task's first level page tables contains the relevant 315 * entry, we copy the it to this task. If not, we send the process 316 * a signal, fixup the exception, or oops the kernel. 317 * 318 * NOTE! We MUST NOT take any locks for this case. We may be in an 319 * interrupt or a critical region, and should only copy the information 320 * from the master page table, nothing more. 321 */ 322static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) 323{ 324 unsigned int index; 325 pgd_t *pgd, *pgd_k; 326 pmd_t *pmd, *pmd_k; 327 328 if (addr < TASK_SIZE) 329 return do_pf(addr, fsr, regs); 330 331 if (user_mode(regs)) 332 goto bad_area; 333 334 index = pgd_index(addr); 335 336 pgd = cpu_get_pgd() + index; 337 pgd_k = init_mm.pgd + index; 338 339 if (pgd_none(*pgd_k)) 340 goto bad_area; 341 342 pmd_k = pmd_offset((pud_t *) pgd_k, addr); 343 pmd = pmd_offset((pud_t *) pgd, addr); 344 345 if (pmd_none(*pmd_k)) 346 goto bad_area; 347 348 set_pmd(pmd, *pmd_k); 349 flush_pmd_entry(pmd); 350 return 0; 351 352bad_area: 353 do_bad_area(addr, fsr, regs); 354 return 0; 355} 356 357/* 358 * This abort handler always returns "fault". 359 */ 360static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs) 361{ 362 return 1; 363} 364 365static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs) 366{ 367 unsigned int res1, res2; 368 369 printk("dabt exception but no error!\n"); 370 371 __asm__ __volatile__( 372 "mff %0,f0\n" 373 "mff %1,f1\n" 374 : "=r"(res1), "=r"(res2) 375 : 376 : "memory"); 377 378 printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2); 379 panic("shut up\n"); 380 return 0; 381} 382 383static struct fsr_info { 384 int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs); 385 int sig; 386 int code; 387 const char *name; 388} fsr_info[] = { 389 /* 390 * The following are the standard Unicore-I and UniCore-II aborts. 391 */ 392 { do_good, SIGBUS, 0, "no error" }, 393 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" }, 394 { do_bad, SIGBUS, BUS_OBJERR, "external exception" }, 395 { do_bad, SIGBUS, 0, "burst operation" }, 396 { do_bad, SIGBUS, 0, "unknown 00100" }, 397 { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"}, 398 { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" }, 399 { do_bad, SIGBUS, 0, "invalid pte" }, 400 { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" }, 401 { do_bad, SIGBUS, 0, "middle page miss" }, 402 { do_bad, SIGBUS, 0, "large page miss" }, 403 { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" }, 404 { do_bad, SIGBUS, 0, "unknown 01100" }, 405 { do_bad, SIGBUS, 0, "unknown 01101" }, 406 { do_bad, SIGBUS, 0, "unknown 01110" }, 407 { do_bad, SIGBUS, 0, "unknown 01111" }, 408 { do_bad, SIGBUS, 0, "addr: up 3G or IO" }, 409 { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" }, 410 { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"}, 411 { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"}, 412 { do_bad, SIGBUS, 0, "unknown 10100" }, 413 { do_bad, SIGBUS, 0, "unknown 10101" }, 414 { do_bad, SIGBUS, 0, "unknown 10110" }, 415 { do_bad, SIGBUS, 0, "unknown 10111" }, 416 { do_bad, SIGBUS, 0, "unknown 11000" }, 417 { do_bad, SIGBUS, 0, "unknown 11001" }, 418 { do_bad, SIGBUS, 0, "unknown 11010" }, 419 { do_bad, SIGBUS, 0, "unknown 11011" }, 420 { do_bad, SIGBUS, 0, "unknown 11100" }, 421 { do_bad, SIGBUS, 0, "unknown 11101" }, 422 { do_bad, SIGBUS, 0, "unknown 11110" }, 423 { do_bad, SIGBUS, 0, "unknown 11111" } 424}; 425 426void __init hook_fault_code(int nr, 427 int (*fn) (unsigned long, unsigned int, struct pt_regs *), 428 int sig, int code, const char *name) 429{ 430 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info)) 431 BUG(); 432 433 fsr_info[nr].fn = fn; 434 fsr_info[nr].sig = sig; 435 fsr_info[nr].code = code; 436 fsr_info[nr].name = name; 437} 438 439/* 440 * Dispatch a data abort to the relevant handler. 441 */ 442asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr, 443 struct pt_regs *regs) 444{ 445 const struct fsr_info *inf = fsr_info + fsr_fs(fsr); 446 struct siginfo info; 447 448 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs)) 449 return; 450 451 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n", 452 inf->name, fsr, addr); 453 454 info.si_signo = inf->sig; 455 info.si_errno = 0; 456 info.si_code = inf->code; 457 info.si_addr = (void __user *)addr; 458 uc32_notify_die("", regs, &info, fsr, 0); 459} 460 461asmlinkage void do_PrefetchAbort(unsigned long addr, 462 unsigned int ifsr, struct pt_regs *regs) 463{ 464 const struct fsr_info *inf = fsr_info + fsr_fs(ifsr); 465 struct siginfo info; 466 467 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs)) 468 return; 469 470 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n", 471 inf->name, ifsr, addr); 472 473 info.si_signo = inf->sig; 474 info.si_errno = 0; 475 info.si_code = inf->code; 476 info.si_addr = (void __user *)addr; 477 uc32_notify_die("", regs, &info, ifsr, 0); 478}