fault.c 12 KB

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