fault.c 11 KB

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