fault.c 12 KB

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