fault.c 12 KB

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