fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * linux/arch/arm/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Modifications for ARM processor (c) 1995-2004 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/module.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/init.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/page-flags.h>
  19. #include <linux/sched.h>
  20. #include <linux/highmem.h>
  21. #include <asm/system.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/tlbflush.h>
  24. #include "fault.h"
  25. /*
  26. * Fault status register encodings. We steal bit 31 for our own purposes.
  27. */
  28. #define FSR_LNX_PF (1 << 31)
  29. #define FSR_WRITE (1 << 11)
  30. #define FSR_FS4 (1 << 10)
  31. #define FSR_FS3_0 (15)
  32. static inline int fsr_fs(unsigned int fsr)
  33. {
  34. return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
  35. }
  36. #ifdef CONFIG_MMU
  37. #ifdef CONFIG_KPROBES
  38. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  39. {
  40. int ret = 0;
  41. if (!user_mode(regs)) {
  42. /* kprobe_running() needs smp_processor_id() */
  43. preempt_disable();
  44. if (kprobe_running() && kprobe_fault_handler(regs, fsr))
  45. ret = 1;
  46. preempt_enable();
  47. }
  48. return ret;
  49. }
  50. #else
  51. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  52. {
  53. return 0;
  54. }
  55. #endif
  56. /*
  57. * This is useful to dump out the page tables associated with
  58. * 'addr' in mm 'mm'.
  59. */
  60. void show_pte(struct mm_struct *mm, unsigned long addr)
  61. {
  62. pgd_t *pgd;
  63. if (!mm)
  64. mm = &init_mm;
  65. printk(KERN_ALERT "pgd = %p\n", mm->pgd);
  66. pgd = pgd_offset(mm, addr);
  67. printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
  68. do {
  69. pmd_t *pmd;
  70. pte_t *pte;
  71. if (pgd_none(*pgd))
  72. break;
  73. if (pgd_bad(*pgd)) {
  74. printk("(bad)");
  75. break;
  76. }
  77. pmd = pmd_offset(pgd, addr);
  78. if (PTRS_PER_PMD != 1)
  79. printk(", *pmd=%08lx", pmd_val(*pmd));
  80. if (pmd_none(*pmd))
  81. break;
  82. if (pmd_bad(*pmd)) {
  83. printk("(bad)");
  84. break;
  85. }
  86. /* We must not map this if we have highmem enabled */
  87. if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  88. break;
  89. pte = pte_offset_map(pmd, addr);
  90. printk(", *pte=%08lx", pte_val(*pte));
  91. printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
  92. pte_unmap(pte);
  93. } while(0);
  94. printk("\n");
  95. }
  96. #else /* CONFIG_MMU */
  97. void show_pte(struct mm_struct *mm, unsigned long addr)
  98. { }
  99. #endif /* CONFIG_MMU */
  100. /*
  101. * Oops. The kernel tried to access some page that wasn't present.
  102. */
  103. static void
  104. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  105. struct pt_regs *regs)
  106. {
  107. /*
  108. * Are we prepared to handle this kernel fault?
  109. */
  110. if (fixup_exception(regs))
  111. return;
  112. /*
  113. * No handler, we'll have to terminate things with extreme prejudice.
  114. */
  115. bust_spinlocks(1);
  116. printk(KERN_ALERT
  117. "Unable to handle kernel %s at virtual address %08lx\n",
  118. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  119. "paging request", addr);
  120. show_pte(mm, addr);
  121. die("Oops", regs, fsr);
  122. bust_spinlocks(0);
  123. do_exit(SIGKILL);
  124. }
  125. /*
  126. * Something tried to access memory that isn't in our memory map..
  127. * User mode accesses just cause a SIGSEGV
  128. */
  129. static void
  130. __do_user_fault(struct task_struct *tsk, unsigned long addr,
  131. unsigned int fsr, unsigned int sig, int code,
  132. struct pt_regs *regs)
  133. {
  134. struct siginfo si;
  135. #ifdef CONFIG_DEBUG_USER
  136. if (user_debug & UDBG_SEGV) {
  137. printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
  138. tsk->comm, sig, addr, fsr);
  139. show_pte(tsk->mm, addr);
  140. show_regs(regs);
  141. }
  142. #endif
  143. tsk->thread.address = addr;
  144. tsk->thread.error_code = fsr;
  145. tsk->thread.trap_no = 14;
  146. si.si_signo = sig;
  147. si.si_errno = 0;
  148. si.si_code = code;
  149. si.si_addr = (void __user *)addr;
  150. force_sig_info(sig, &si, tsk);
  151. }
  152. void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  153. {
  154. struct task_struct *tsk = current;
  155. struct mm_struct *mm = tsk->active_mm;
  156. /*
  157. * If we are in kernel mode at this point, we
  158. * have no context to handle this fault with.
  159. */
  160. if (user_mode(regs))
  161. __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
  162. else
  163. __do_kernel_fault(mm, addr, fsr, regs);
  164. }
  165. #ifdef CONFIG_MMU
  166. #define VM_FAULT_BADMAP 0x010000
  167. #define VM_FAULT_BADACCESS 0x020000
  168. /*
  169. * Check that the permissions on the VMA allow for the fault which occurred.
  170. * If we encountered a write fault, we must have write permission, otherwise
  171. * we allow any permission.
  172. */
  173. static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
  174. {
  175. unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
  176. if (fsr & FSR_WRITE)
  177. mask = VM_WRITE;
  178. if (fsr & FSR_LNX_PF)
  179. mask = VM_EXEC;
  180. return vma->vm_flags & mask ? false : true;
  181. }
  182. static int __kprobes
  183. __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  184. struct task_struct *tsk)
  185. {
  186. struct vm_area_struct *vma;
  187. int fault;
  188. vma = find_vma(mm, addr);
  189. fault = VM_FAULT_BADMAP;
  190. if (unlikely(!vma))
  191. goto out;
  192. if (unlikely(vma->vm_start > addr))
  193. goto check_stack;
  194. /*
  195. * Ok, we have a good vm_area for this
  196. * memory access, so we can handle it.
  197. */
  198. good_area:
  199. if (access_error(fsr, vma)) {
  200. fault = VM_FAULT_BADACCESS;
  201. goto out;
  202. }
  203. /*
  204. * If for any reason at all we couldn't handle the fault, make
  205. * sure we exit gracefully rather than endlessly redo the fault.
  206. */
  207. fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
  208. if (unlikely(fault & VM_FAULT_ERROR))
  209. return fault;
  210. if (fault & VM_FAULT_MAJOR)
  211. tsk->maj_flt++;
  212. else
  213. tsk->min_flt++;
  214. return fault;
  215. check_stack:
  216. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  217. goto good_area;
  218. out:
  219. return fault;
  220. }
  221. static int __kprobes
  222. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  223. {
  224. struct task_struct *tsk;
  225. struct mm_struct *mm;
  226. int fault, sig, code;
  227. if (notify_page_fault(regs, fsr))
  228. return 0;
  229. tsk = current;
  230. mm = tsk->mm;
  231. /*
  232. * If we're in an interrupt or have no user
  233. * context, we must not take the fault..
  234. */
  235. if (in_atomic() || !mm)
  236. goto no_context;
  237. /*
  238. * As per x86, we may deadlock here. However, since the kernel only
  239. * validly references user space from well defined areas of the code,
  240. * we can bug out early if this is from code which shouldn't.
  241. */
  242. if (!down_read_trylock(&mm->mmap_sem)) {
  243. if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
  244. goto no_context;
  245. down_read(&mm->mmap_sem);
  246. } else {
  247. /*
  248. * The above down_read_trylock() might have succeeded in
  249. * which case, we'll have missed the might_sleep() from
  250. * down_read()
  251. */
  252. might_sleep();
  253. #ifdef CONFIG_DEBUG_VM
  254. if (!user_mode(regs) &&
  255. !search_exception_tables(regs->ARM_pc))
  256. goto no_context;
  257. #endif
  258. }
  259. fault = __do_page_fault(mm, addr, fsr, tsk);
  260. up_read(&mm->mmap_sem);
  261. /*
  262. * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
  263. */
  264. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
  265. return 0;
  266. if (fault & VM_FAULT_OOM) {
  267. /*
  268. * We ran out of memory, call the OOM killer, and return to
  269. * userspace (which will retry the fault, or kill us if we
  270. * got oom-killed)
  271. */
  272. pagefault_out_of_memory();
  273. return 0;
  274. }
  275. /*
  276. * If we are in kernel mode at this point, we
  277. * have no context to handle this fault with.
  278. */
  279. if (!user_mode(regs))
  280. goto no_context;
  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 ?
  295. SEGV_ACCERR : SEGV_MAPERR;
  296. }
  297. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  298. return 0;
  299. no_context:
  300. __do_kernel_fault(mm, addr, fsr, regs);
  301. return 0;
  302. }
  303. #else /* CONFIG_MMU */
  304. static int
  305. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  306. {
  307. return 0;
  308. }
  309. #endif /* CONFIG_MMU */
  310. /*
  311. * First Level Translation Fault Handler
  312. *
  313. * We enter here because the first level page table doesn't contain
  314. * a valid entry for the address.
  315. *
  316. * If the address is in kernel space (>= TASK_SIZE), then we are
  317. * probably faulting in the vmalloc() area.
  318. *
  319. * If the init_task's first level page tables contains the relevant
  320. * entry, we copy the it to this task. If not, we send the process
  321. * a signal, fixup the exception, or oops the kernel.
  322. *
  323. * NOTE! We MUST NOT take any locks for this case. We may be in an
  324. * interrupt or a critical region, and should only copy the information
  325. * from the master page table, nothing more.
  326. */
  327. #ifdef CONFIG_MMU
  328. static int __kprobes
  329. do_translation_fault(unsigned long addr, unsigned int fsr,
  330. struct pt_regs *regs)
  331. {
  332. unsigned int index;
  333. pgd_t *pgd, *pgd_k;
  334. pmd_t *pmd, *pmd_k;
  335. if (addr < TASK_SIZE)
  336. return do_page_fault(addr, fsr, regs);
  337. index = pgd_index(addr);
  338. /*
  339. * FIXME: CP15 C1 is write only on ARMv3 architectures.
  340. */
  341. pgd = cpu_get_pgd() + index;
  342. pgd_k = init_mm.pgd + index;
  343. if (pgd_none(*pgd_k))
  344. goto bad_area;
  345. if (!pgd_present(*pgd))
  346. set_pgd(pgd, *pgd_k);
  347. pmd_k = pmd_offset(pgd_k, addr);
  348. pmd = pmd_offset(pgd, addr);
  349. if (pmd_none(*pmd_k))
  350. goto bad_area;
  351. copy_pmd(pmd, pmd_k);
  352. return 0;
  353. bad_area:
  354. do_bad_area(addr, fsr, regs);
  355. return 0;
  356. }
  357. #else /* CONFIG_MMU */
  358. static int
  359. do_translation_fault(unsigned long addr, unsigned int fsr,
  360. struct pt_regs *regs)
  361. {
  362. return 0;
  363. }
  364. #endif /* CONFIG_MMU */
  365. /*
  366. * Some section permission faults need to be handled gracefully.
  367. * They can happen due to a __{get,put}_user during an oops.
  368. */
  369. static int
  370. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  371. {
  372. do_bad_area(addr, fsr, regs);
  373. return 0;
  374. }
  375. /*
  376. * This abort handler always returns "fault".
  377. */
  378. static int
  379. do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  380. {
  381. return 1;
  382. }
  383. static 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 ARMv3 and ARMv4 aborts. ARMv5
  391. * defines these to be "precise" aborts.
  392. */
  393. { do_bad, SIGSEGV, 0, "vector exception" },
  394. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  395. { do_bad, SIGKILL, 0, "terminal exception" },
  396. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  397. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  398. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
  399. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  400. { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
  401. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  402. { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
  403. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  404. { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
  405. { do_bad, SIGBUS, 0, "external abort on translation" },
  406. { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
  407. { do_bad, SIGBUS, 0, "external abort on translation" },
  408. { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
  409. /*
  410. * The following are "imprecise" aborts, which are signalled by bit
  411. * 10 of the FSR, and may not be recoverable. These are only
  412. * supported if the CPU abort handler supports bit 10.
  413. */
  414. { do_bad, SIGBUS, 0, "unknown 16" },
  415. { do_bad, SIGBUS, 0, "unknown 17" },
  416. { do_bad, SIGBUS, 0, "unknown 18" },
  417. { do_bad, SIGBUS, 0, "unknown 19" },
  418. { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
  419. { do_bad, SIGBUS, 0, "unknown 21" },
  420. { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
  421. { do_bad, SIGBUS, 0, "unknown 23" },
  422. { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
  423. { do_bad, SIGBUS, 0, "unknown 25" },
  424. { do_bad, SIGBUS, 0, "unknown 26" },
  425. { do_bad, SIGBUS, 0, "unknown 27" },
  426. { do_bad, SIGBUS, 0, "unknown 28" },
  427. { do_bad, SIGBUS, 0, "unknown 29" },
  428. { do_bad, SIGBUS, 0, "unknown 30" },
  429. { do_bad, SIGBUS, 0, "unknown 31" }
  430. };
  431. void __init
  432. hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  433. int sig, const char *name)
  434. {
  435. if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
  436. fsr_info[nr].fn = fn;
  437. fsr_info[nr].sig = sig;
  438. fsr_info[nr].name = name;
  439. }
  440. }
  441. /*
  442. * Dispatch a data abort to the relevant handler.
  443. */
  444. asmlinkage void __exception
  445. do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  446. {
  447. const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
  448. struct siginfo info;
  449. if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
  450. return;
  451. printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  452. inf->name, fsr, addr);
  453. info.si_signo = inf->sig;
  454. info.si_errno = 0;
  455. info.si_code = inf->code;
  456. info.si_addr = (void __user *)addr;
  457. arm_notify_die("", regs, &info, fsr, 0);
  458. }
  459. static struct fsr_info ifsr_info[] = {
  460. { do_bad, SIGBUS, 0, "unknown 0" },
  461. { do_bad, SIGBUS, 0, "unknown 1" },
  462. { do_bad, SIGBUS, 0, "debug event" },
  463. { do_bad, SIGSEGV, SEGV_ACCERR, "section access flag fault" },
  464. { do_bad, SIGBUS, 0, "unknown 4" },
  465. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
  466. { do_bad, SIGSEGV, SEGV_ACCERR, "page access flag fault" },
  467. { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
  468. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  469. { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
  470. { do_bad, SIGBUS, 0, "unknown 10" },
  471. { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
  472. { do_bad, SIGBUS, 0, "external abort on translation" },
  473. { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
  474. { do_bad, SIGBUS, 0, "external abort on translation" },
  475. { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
  476. { do_bad, SIGBUS, 0, "unknown 16" },
  477. { do_bad, SIGBUS, 0, "unknown 17" },
  478. { do_bad, SIGBUS, 0, "unknown 18" },
  479. { do_bad, SIGBUS, 0, "unknown 19" },
  480. { do_bad, SIGBUS, 0, "unknown 20" },
  481. { do_bad, SIGBUS, 0, "unknown 21" },
  482. { do_bad, SIGBUS, 0, "unknown 22" },
  483. { do_bad, SIGBUS, 0, "unknown 23" },
  484. { do_bad, SIGBUS, 0, "unknown 24" },
  485. { do_bad, SIGBUS, 0, "unknown 25" },
  486. { do_bad, SIGBUS, 0, "unknown 26" },
  487. { do_bad, SIGBUS, 0, "unknown 27" },
  488. { do_bad, SIGBUS, 0, "unknown 28" },
  489. { do_bad, SIGBUS, 0, "unknown 29" },
  490. { do_bad, SIGBUS, 0, "unknown 30" },
  491. { do_bad, SIGBUS, 0, "unknown 31" },
  492. };
  493. asmlinkage void __exception
  494. do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
  495. {
  496. const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
  497. struct siginfo info;
  498. if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
  499. return;
  500. printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
  501. inf->name, ifsr, addr);
  502. info.si_signo = inf->sig;
  503. info.si_errno = 0;
  504. info.si_code = inf->code;
  505. info.si_addr = (void __user *)addr;
  506. arm_notify_die("", regs, &info, ifsr, 0);
  507. }