fault.c 13 KB

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