fault.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Page fault handler for SH with an MMU.
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 - 2012 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/fault.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/hardirq.h>
  17. #include <linux/kprobes.h>
  18. #include <linux/perf_event.h>
  19. #include <linux/kdebug.h>
  20. #include <asm/io_trapped.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/traps.h>
  24. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  25. {
  26. int ret = 0;
  27. if (kprobes_built_in() && !user_mode(regs)) {
  28. preempt_disable();
  29. if (kprobe_running() && kprobe_fault_handler(regs, trap))
  30. ret = 1;
  31. preempt_enable();
  32. }
  33. return ret;
  34. }
  35. static void
  36. force_sig_info_fault(int si_signo, int si_code, unsigned long address,
  37. struct task_struct *tsk)
  38. {
  39. siginfo_t info;
  40. info.si_signo = si_signo;
  41. info.si_errno = 0;
  42. info.si_code = si_code;
  43. info.si_addr = (void __user *)address;
  44. force_sig_info(si_signo, &info, tsk);
  45. }
  46. /*
  47. * This is useful to dump out the page tables associated with
  48. * 'addr' in mm 'mm'.
  49. */
  50. static void show_pte(struct mm_struct *mm, unsigned long addr)
  51. {
  52. pgd_t *pgd;
  53. if (mm)
  54. pgd = mm->pgd;
  55. else
  56. pgd = get_TTB();
  57. printk(KERN_ALERT "pgd = %p\n", pgd);
  58. pgd += pgd_index(addr);
  59. printk(KERN_ALERT "[%08lx] *pgd=%0*Lx", addr,
  60. (u32)(sizeof(*pgd) * 2), (u64)pgd_val(*pgd));
  61. do {
  62. pud_t *pud;
  63. pmd_t *pmd;
  64. pte_t *pte;
  65. if (pgd_none(*pgd))
  66. break;
  67. if (pgd_bad(*pgd)) {
  68. printk("(bad)");
  69. break;
  70. }
  71. pud = pud_offset(pgd, addr);
  72. if (PTRS_PER_PUD != 1)
  73. printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2),
  74. (u64)pud_val(*pud));
  75. if (pud_none(*pud))
  76. break;
  77. if (pud_bad(*pud)) {
  78. printk("(bad)");
  79. break;
  80. }
  81. pmd = pmd_offset(pud, addr);
  82. if (PTRS_PER_PMD != 1)
  83. printk(", *pmd=%0*Lx", (u32)(sizeof(*pmd) * 2),
  84. (u64)pmd_val(*pmd));
  85. if (pmd_none(*pmd))
  86. break;
  87. if (pmd_bad(*pmd)) {
  88. printk("(bad)");
  89. break;
  90. }
  91. /* We must not map this if we have highmem enabled */
  92. if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  93. break;
  94. pte = pte_offset_kernel(pmd, addr);
  95. printk(", *pte=%0*Lx", (u32)(sizeof(*pte) * 2),
  96. (u64)pte_val(*pte));
  97. } while (0);
  98. printk("\n");
  99. }
  100. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  101. {
  102. unsigned index = pgd_index(address);
  103. pgd_t *pgd_k;
  104. pud_t *pud, *pud_k;
  105. pmd_t *pmd, *pmd_k;
  106. pgd += index;
  107. pgd_k = init_mm.pgd + index;
  108. if (!pgd_present(*pgd_k))
  109. return NULL;
  110. pud = pud_offset(pgd, address);
  111. pud_k = pud_offset(pgd_k, address);
  112. if (!pud_present(*pud_k))
  113. return NULL;
  114. if (!pud_present(*pud))
  115. set_pud(pud, *pud_k);
  116. pmd = pmd_offset(pud, address);
  117. pmd_k = pmd_offset(pud_k, address);
  118. if (!pmd_present(*pmd_k))
  119. return NULL;
  120. if (!pmd_present(*pmd))
  121. set_pmd(pmd, *pmd_k);
  122. else {
  123. /*
  124. * The page tables are fully synchronised so there must
  125. * be another reason for the fault. Return NULL here to
  126. * signal that we have not taken care of the fault.
  127. */
  128. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  129. return NULL;
  130. }
  131. return pmd_k;
  132. }
  133. /*
  134. * Handle a fault on the vmalloc or module mapping area
  135. */
  136. static noinline int vmalloc_fault(unsigned long address)
  137. {
  138. pgd_t *pgd_k;
  139. pmd_t *pmd_k;
  140. pte_t *pte_k;
  141. /* Make sure we are in vmalloc/module area: */
  142. if (!is_vmalloc_addr((void *)address))
  143. return -1;
  144. /*
  145. * Synchronize this task's top level page-table
  146. * with the 'reference' page table.
  147. *
  148. * Do _not_ use "current" here. We might be inside
  149. * an interrupt in the middle of a task switch..
  150. */
  151. pgd_k = get_TTB();
  152. pmd_k = vmalloc_sync_one(pgd_k, address);
  153. if (!pmd_k)
  154. return -1;
  155. pte_k = pte_offset_kernel(pmd_k, address);
  156. if (!pte_present(*pte_k))
  157. return -1;
  158. return 0;
  159. }
  160. static void
  161. show_fault_oops(struct pt_regs *regs, unsigned long address)
  162. {
  163. if (!oops_may_print())
  164. return;
  165. printk(KERN_ALERT "BUG: unable to handle kernel ");
  166. if (address < PAGE_SIZE)
  167. printk(KERN_CONT "NULL pointer dereference");
  168. else
  169. printk(KERN_CONT "paging request");
  170. printk(KERN_CONT " at %08lx\n", address);
  171. printk(KERN_ALERT "PC:");
  172. printk_address(regs->pc, 1);
  173. show_pte(NULL, address);
  174. }
  175. static noinline void
  176. no_context(struct pt_regs *regs, unsigned long error_code,
  177. unsigned long address)
  178. {
  179. /* Are we prepared to handle this kernel fault? */
  180. if (fixup_exception(regs))
  181. return;
  182. if (handle_trapped_io(regs, address))
  183. return;
  184. /*
  185. * Oops. The kernel tried to access some bad page. We'll have to
  186. * terminate things with extreme prejudice.
  187. */
  188. bust_spinlocks(1);
  189. show_fault_oops(regs, address);
  190. die("Oops", regs, error_code);
  191. bust_spinlocks(0);
  192. do_exit(SIGKILL);
  193. }
  194. static void
  195. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  196. unsigned long address, int si_code)
  197. {
  198. struct task_struct *tsk = current;
  199. /* User mode accesses just cause a SIGSEGV */
  200. if (user_mode(regs)) {
  201. /*
  202. * It's possible to have interrupts off here:
  203. */
  204. local_irq_enable();
  205. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  206. return;
  207. }
  208. no_context(regs, error_code, address);
  209. }
  210. static noinline void
  211. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  212. unsigned long address)
  213. {
  214. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  215. }
  216. static void
  217. __bad_area(struct pt_regs *regs, unsigned long error_code,
  218. unsigned long address, int si_code)
  219. {
  220. struct mm_struct *mm = current->mm;
  221. /*
  222. * Something tried to access memory that isn't in our memory map..
  223. * Fix it, but check if it's kernel or user first..
  224. */
  225. up_read(&mm->mmap_sem);
  226. __bad_area_nosemaphore(regs, error_code, address, si_code);
  227. }
  228. static noinline void
  229. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  230. {
  231. __bad_area(regs, error_code, address, SEGV_MAPERR);
  232. }
  233. static noinline void
  234. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  235. unsigned long address)
  236. {
  237. __bad_area(regs, error_code, address, SEGV_ACCERR);
  238. }
  239. static void out_of_memory(void)
  240. {
  241. /*
  242. * We ran out of memory, call the OOM killer, and return the userspace
  243. * (which will retry the fault, or kill us if we got oom-killed):
  244. */
  245. up_read(&current->mm->mmap_sem);
  246. pagefault_out_of_memory();
  247. }
  248. static void
  249. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  250. {
  251. struct task_struct *tsk = current;
  252. struct mm_struct *mm = tsk->mm;
  253. up_read(&mm->mmap_sem);
  254. /* Kernel mode? Handle exceptions or die: */
  255. if (!user_mode(regs))
  256. no_context(regs, error_code, address);
  257. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  258. }
  259. static noinline int
  260. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  261. unsigned long address, unsigned int fault)
  262. {
  263. /*
  264. * Pagefault was interrupted by SIGKILL. We have no reason to
  265. * continue pagefault.
  266. */
  267. if (fatal_signal_pending(current)) {
  268. if (!(fault & VM_FAULT_RETRY))
  269. up_read(&current->mm->mmap_sem);
  270. if (!user_mode(regs))
  271. no_context(regs, error_code, address);
  272. return 1;
  273. }
  274. if (!(fault & VM_FAULT_ERROR))
  275. return 0;
  276. if (fault & VM_FAULT_OOM) {
  277. /* Kernel mode? Handle exceptions or die: */
  278. if (!user_mode(regs)) {
  279. up_read(&current->mm->mmap_sem);
  280. no_context(regs, error_code, address);
  281. return 1;
  282. }
  283. out_of_memory();
  284. } else {
  285. if (fault & VM_FAULT_SIGBUS)
  286. do_sigbus(regs, error_code, address);
  287. else
  288. BUG();
  289. }
  290. return 1;
  291. }
  292. static inline int access_error(int error_code, struct vm_area_struct *vma)
  293. {
  294. if (error_code & FAULT_CODE_WRITE) {
  295. /* write, present and write, not present: */
  296. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  297. return 1;
  298. return 0;
  299. }
  300. /* ITLB miss on NX page */
  301. if (unlikely((error_code & FAULT_CODE_ITLB) &&
  302. !(vma->vm_flags & VM_EXEC)))
  303. return 1;
  304. /* read, not present: */
  305. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  306. return 1;
  307. return 0;
  308. }
  309. static int fault_in_kernel_space(unsigned long address)
  310. {
  311. return address >= TASK_SIZE;
  312. }
  313. /*
  314. * This routine handles page faults. It determines the address,
  315. * and the problem, and then passes it off to one of the appropriate
  316. * routines.
  317. */
  318. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  319. unsigned long error_code,
  320. unsigned long address)
  321. {
  322. unsigned long vec;
  323. struct task_struct *tsk;
  324. struct mm_struct *mm;
  325. struct vm_area_struct * vma;
  326. int fault;
  327. int write = error_code & FAULT_CODE_WRITE;
  328. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  329. (write ? FAULT_FLAG_WRITE : 0));
  330. tsk = current;
  331. mm = tsk->mm;
  332. vec = lookup_exception_vector();
  333. /*
  334. * We fault-in kernel-space virtual memory on-demand. The
  335. * 'reference' page table is init_mm.pgd.
  336. *
  337. * NOTE! We MUST NOT take any locks for this case. We may
  338. * be in an interrupt or a critical region, and should
  339. * only copy the information from the master page table,
  340. * nothing more.
  341. */
  342. if (unlikely(fault_in_kernel_space(address))) {
  343. if (vmalloc_fault(address) >= 0)
  344. return;
  345. if (notify_page_fault(regs, vec))
  346. return;
  347. bad_area_nosemaphore(regs, error_code, address);
  348. return;
  349. }
  350. if (unlikely(notify_page_fault(regs, vec)))
  351. return;
  352. /* Only enable interrupts if they were on before the fault */
  353. if ((regs->sr & SR_IMASK) != SR_IMASK)
  354. local_irq_enable();
  355. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  356. /*
  357. * If we're in an interrupt, have no user context or are running
  358. * in an atomic region then we must not take the fault:
  359. */
  360. if (unlikely(in_atomic() || !mm)) {
  361. bad_area_nosemaphore(regs, error_code, address);
  362. return;
  363. }
  364. retry:
  365. down_read(&mm->mmap_sem);
  366. vma = find_vma(mm, address);
  367. if (unlikely(!vma)) {
  368. bad_area(regs, error_code, address);
  369. return;
  370. }
  371. if (likely(vma->vm_start <= address))
  372. goto good_area;
  373. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  374. bad_area(regs, error_code, address);
  375. return;
  376. }
  377. if (unlikely(expand_stack(vma, address))) {
  378. bad_area(regs, error_code, address);
  379. return;
  380. }
  381. /*
  382. * Ok, we have a good vm_area for this memory access, so
  383. * we can handle it..
  384. */
  385. good_area:
  386. if (unlikely(access_error(error_code, vma))) {
  387. bad_area_access_error(regs, error_code, address);
  388. return;
  389. }
  390. set_thread_fault_code(error_code);
  391. /*
  392. * If for any reason at all we couldn't handle the fault,
  393. * make sure we exit gracefully rather than endlessly redo
  394. * the fault.
  395. */
  396. fault = handle_mm_fault(mm, vma, address, flags);
  397. if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
  398. if (mm_fault_error(regs, error_code, address, fault))
  399. return;
  400. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  401. if (fault & VM_FAULT_MAJOR) {
  402. tsk->maj_flt++;
  403. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  404. regs, address);
  405. } else {
  406. tsk->min_flt++;
  407. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  408. regs, address);
  409. }
  410. if (fault & VM_FAULT_RETRY) {
  411. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  412. /*
  413. * No need to up_read(&mm->mmap_sem) as we would
  414. * have already released it in __lock_page_or_retry
  415. * in mm/filemap.c.
  416. */
  417. goto retry;
  418. }
  419. }
  420. up_read(&mm->mmap_sem);
  421. }