fault.c 11 KB

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