fault.c 12 KB

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