fault.c 11 KB

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