fault.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 (-20)
  127. #define VM_FAULT_BADACCESS (-21)
  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. /*
  160. * Handle the "normal" cases first - successful and sigbus
  161. */
  162. switch (fault) {
  163. case VM_FAULT_MAJOR:
  164. tsk->maj_flt++;
  165. return fault;
  166. case VM_FAULT_MINOR:
  167. tsk->min_flt++;
  168. case VM_FAULT_SIGBUS:
  169. return fault;
  170. }
  171. if (!is_init(tsk))
  172. goto out;
  173. /*
  174. * If we are out of memory for pid1, sleep for a while and retry
  175. */
  176. up_read(&mm->mmap_sem);
  177. yield();
  178. down_read(&mm->mmap_sem);
  179. goto survive;
  180. check_stack:
  181. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  182. goto good_area;
  183. out:
  184. return fault;
  185. }
  186. static int
  187. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  188. {
  189. struct task_struct *tsk;
  190. struct mm_struct *mm;
  191. int fault, sig, code;
  192. tsk = current;
  193. mm = tsk->mm;
  194. /*
  195. * If we're in an interrupt or have no user
  196. * context, we must not take the fault..
  197. */
  198. if (in_atomic() || !mm)
  199. goto no_context;
  200. /*
  201. * As per x86, we may deadlock here. However, since the kernel only
  202. * validly references user space from well defined areas of the code,
  203. * we can bug out early if this is from code which shouldn't.
  204. */
  205. if (!down_read_trylock(&mm->mmap_sem)) {
  206. if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
  207. goto no_context;
  208. down_read(&mm->mmap_sem);
  209. }
  210. fault = __do_page_fault(mm, addr, fsr, tsk);
  211. up_read(&mm->mmap_sem);
  212. /*
  213. * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
  214. */
  215. if (fault >= VM_FAULT_MINOR)
  216. return 0;
  217. /*
  218. * If we are in kernel mode at this point, we
  219. * have no context to handle this fault with.
  220. */
  221. if (!user_mode(regs))
  222. goto no_context;
  223. switch (fault) {
  224. case 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. case VM_FAULT_SIGBUS:
  234. /*
  235. * We had some memory, but were unable to
  236. * successfully fix up this page fault.
  237. */
  238. sig = SIGBUS;
  239. code = BUS_ADRERR;
  240. break;
  241. default:
  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. break;
  250. }
  251. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  252. return 0;
  253. no_context:
  254. __do_kernel_fault(mm, addr, fsr, regs);
  255. return 0;
  256. }
  257. /*
  258. * First Level Translation Fault Handler
  259. *
  260. * We enter here because the first level page table doesn't contain
  261. * a valid entry for the address.
  262. *
  263. * If the address is in kernel space (>= TASK_SIZE), then we are
  264. * probably faulting in the vmalloc() area.
  265. *
  266. * If the init_task's first level page tables contains the relevant
  267. * entry, we copy the it to this task. If not, we send the process
  268. * a signal, fixup the exception, or oops the kernel.
  269. *
  270. * NOTE! We MUST NOT take any locks for this case. We may be in an
  271. * interrupt or a critical region, and should only copy the information
  272. * from the master page table, nothing more.
  273. */
  274. static int
  275. do_translation_fault(unsigned long addr, unsigned int fsr,
  276. struct pt_regs *regs)
  277. {
  278. unsigned int index;
  279. pgd_t *pgd, *pgd_k;
  280. pmd_t *pmd, *pmd_k;
  281. if (addr < TASK_SIZE)
  282. return do_page_fault(addr, fsr, regs);
  283. index = pgd_index(addr);
  284. /*
  285. * FIXME: CP15 C1 is write only on ARMv3 architectures.
  286. */
  287. pgd = cpu_get_pgd() + index;
  288. pgd_k = init_mm.pgd + index;
  289. if (pgd_none(*pgd_k))
  290. goto bad_area;
  291. if (!pgd_present(*pgd))
  292. set_pgd(pgd, *pgd_k);
  293. pmd_k = pmd_offset(pgd_k, addr);
  294. pmd = pmd_offset(pgd, addr);
  295. if (pmd_none(*pmd_k))
  296. goto bad_area;
  297. copy_pmd(pmd, pmd_k);
  298. return 0;
  299. bad_area:
  300. do_bad_area(addr, fsr, regs);
  301. return 0;
  302. }
  303. /*
  304. * Some section permission faults need to be handled gracefully.
  305. * They can happen due to a __{get,put}_user during an oops.
  306. */
  307. static int
  308. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  309. {
  310. do_bad_area(addr, fsr, regs);
  311. return 0;
  312. }
  313. /*
  314. * This abort handler always returns "fault".
  315. */
  316. static int
  317. do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  318. {
  319. return 1;
  320. }
  321. static struct fsr_info {
  322. int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
  323. int sig;
  324. int code;
  325. const char *name;
  326. } fsr_info[] = {
  327. /*
  328. * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
  329. * defines these to be "precise" aborts.
  330. */
  331. { do_bad, SIGSEGV, 0, "vector exception" },
  332. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  333. { do_bad, SIGKILL, 0, "terminal exception" },
  334. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  335. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  336. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
  337. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  338. { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
  339. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  340. { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
  341. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  342. { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
  343. { do_bad, SIGBUS, 0, "external abort on translation" },
  344. { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
  345. { do_bad, SIGBUS, 0, "external abort on translation" },
  346. { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
  347. /*
  348. * The following are "imprecise" aborts, which are signalled by bit
  349. * 10 of the FSR, and may not be recoverable. These are only
  350. * supported if the CPU abort handler supports bit 10.
  351. */
  352. { do_bad, SIGBUS, 0, "unknown 16" },
  353. { do_bad, SIGBUS, 0, "unknown 17" },
  354. { do_bad, SIGBUS, 0, "unknown 18" },
  355. { do_bad, SIGBUS, 0, "unknown 19" },
  356. { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
  357. { do_bad, SIGBUS, 0, "unknown 21" },
  358. { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
  359. { do_bad, SIGBUS, 0, "unknown 23" },
  360. { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
  361. { do_bad, SIGBUS, 0, "unknown 25" },
  362. { do_bad, SIGBUS, 0, "unknown 26" },
  363. { do_bad, SIGBUS, 0, "unknown 27" },
  364. { do_bad, SIGBUS, 0, "unknown 28" },
  365. { do_bad, SIGBUS, 0, "unknown 29" },
  366. { do_bad, SIGBUS, 0, "unknown 30" },
  367. { do_bad, SIGBUS, 0, "unknown 31" }
  368. };
  369. void __init
  370. hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  371. int sig, const char *name)
  372. {
  373. if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
  374. fsr_info[nr].fn = fn;
  375. fsr_info[nr].sig = sig;
  376. fsr_info[nr].name = name;
  377. }
  378. }
  379. /*
  380. * Dispatch a data abort to the relevant handler.
  381. */
  382. asmlinkage void __exception
  383. do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  384. {
  385. const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
  386. struct siginfo info;
  387. if (!inf->fn(addr, fsr, regs))
  388. return;
  389. printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  390. inf->name, fsr, addr);
  391. info.si_signo = inf->sig;
  392. info.si_errno = 0;
  393. info.si_code = inf->code;
  394. info.si_addr = (void __user *)addr;
  395. arm_notify_die("", regs, &info, fsr, 0);
  396. }
  397. asmlinkage void __exception
  398. do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
  399. {
  400. do_translation_fault(addr, 0, regs);
  401. }