fault.c 11 KB

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