fault.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. force_sig_fault(si_signo, si_code, (void __user *)address, tsk);
  42. }
  43. /*
  44. * This is useful to dump out the page tables associated with
  45. * 'addr' in mm 'mm'.
  46. */
  47. static void show_pte(struct mm_struct *mm, unsigned long addr)
  48. {
  49. pgd_t *pgd;
  50. if (mm) {
  51. pgd = mm->pgd;
  52. } else {
  53. pgd = get_TTB();
  54. if (unlikely(!pgd))
  55. pgd = swapper_pg_dir;
  56. }
  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. #ifdef CONFIG_SH_STORE_QUEUES
  134. #define __FAULT_ADDR_LIMIT P3_ADDR_MAX
  135. #else
  136. #define __FAULT_ADDR_LIMIT VMALLOC_END
  137. #endif
  138. /*
  139. * Handle a fault on the vmalloc or module mapping area
  140. */
  141. static noinline int vmalloc_fault(unsigned long address)
  142. {
  143. pgd_t *pgd_k;
  144. pmd_t *pmd_k;
  145. pte_t *pte_k;
  146. /* Make sure we are in vmalloc/module/P3 area: */
  147. if (!(address >= VMALLOC_START && address < __FAULT_ADDR_LIMIT))
  148. return -1;
  149. /*
  150. * Synchronize this task's top level page-table
  151. * with the 'reference' page table.
  152. *
  153. * Do _not_ use "current" here. We might be inside
  154. * an interrupt in the middle of a task switch..
  155. */
  156. pgd_k = get_TTB();
  157. pmd_k = vmalloc_sync_one(pgd_k, address);
  158. if (!pmd_k)
  159. return -1;
  160. pte_k = pte_offset_kernel(pmd_k, address);
  161. if (!pte_present(*pte_k))
  162. return -1;
  163. return 0;
  164. }
  165. static void
  166. show_fault_oops(struct pt_regs *regs, unsigned long address)
  167. {
  168. if (!oops_may_print())
  169. return;
  170. printk(KERN_ALERT "BUG: unable to handle kernel ");
  171. if (address < PAGE_SIZE)
  172. printk(KERN_CONT "NULL pointer dereference");
  173. else
  174. printk(KERN_CONT "paging request");
  175. printk(KERN_CONT " at %08lx\n", address);
  176. printk(KERN_ALERT "PC:");
  177. printk_address(regs->pc, 1);
  178. show_pte(NULL, address);
  179. }
  180. static noinline void
  181. no_context(struct pt_regs *regs, unsigned long error_code,
  182. unsigned long address)
  183. {
  184. /* Are we prepared to handle this kernel fault? */
  185. if (fixup_exception(regs))
  186. return;
  187. if (handle_trapped_io(regs, address))
  188. return;
  189. /*
  190. * Oops. The kernel tried to access some bad page. We'll have to
  191. * terminate things with extreme prejudice.
  192. */
  193. bust_spinlocks(1);
  194. show_fault_oops(regs, address);
  195. die("Oops", regs, error_code);
  196. bust_spinlocks(0);
  197. do_exit(SIGKILL);
  198. }
  199. static void
  200. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  201. unsigned long address, int si_code)
  202. {
  203. struct task_struct *tsk = current;
  204. /* User mode accesses just cause a SIGSEGV */
  205. if (user_mode(regs)) {
  206. /*
  207. * It's possible to have interrupts off here:
  208. */
  209. local_irq_enable();
  210. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  211. return;
  212. }
  213. no_context(regs, error_code, address);
  214. }
  215. static noinline void
  216. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  217. unsigned long address)
  218. {
  219. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  220. }
  221. static void
  222. __bad_area(struct pt_regs *regs, unsigned long error_code,
  223. unsigned long address, int si_code)
  224. {
  225. struct mm_struct *mm = current->mm;
  226. /*
  227. * Something tried to access memory that isn't in our memory map..
  228. * Fix it, but check if it's kernel or user first..
  229. */
  230. up_read(&mm->mmap_sem);
  231. __bad_area_nosemaphore(regs, error_code, address, si_code);
  232. }
  233. static noinline void
  234. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  235. {
  236. __bad_area(regs, error_code, address, SEGV_MAPERR);
  237. }
  238. static noinline void
  239. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  240. unsigned long address)
  241. {
  242. __bad_area(regs, error_code, address, SEGV_ACCERR);
  243. }
  244. static void
  245. do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  246. {
  247. struct task_struct *tsk = current;
  248. struct mm_struct *mm = tsk->mm;
  249. up_read(&mm->mmap_sem);
  250. /* Kernel mode? Handle exceptions or die: */
  251. if (!user_mode(regs))
  252. no_context(regs, error_code, address);
  253. force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
  254. }
  255. static noinline int
  256. mm_fault_error(struct pt_regs *regs, unsigned long error_code,
  257. unsigned long address, vm_fault_t fault)
  258. {
  259. /*
  260. * Pagefault was interrupted by SIGKILL. We have no reason to
  261. * continue pagefault.
  262. */
  263. if (fatal_signal_pending(current)) {
  264. if (!(fault & VM_FAULT_RETRY))
  265. up_read(&current->mm->mmap_sem);
  266. if (!user_mode(regs))
  267. no_context(regs, error_code, address);
  268. return 1;
  269. }
  270. if (!(fault & VM_FAULT_ERROR))
  271. return 0;
  272. if (fault & VM_FAULT_OOM) {
  273. /* Kernel mode? Handle exceptions or die: */
  274. if (!user_mode(regs)) {
  275. up_read(&current->mm->mmap_sem);
  276. no_context(regs, error_code, address);
  277. return 1;
  278. }
  279. up_read(&current->mm->mmap_sem);
  280. /*
  281. * We ran out of memory, call the OOM killer, and return the
  282. * userspace (which will retry the fault, or kill us if we got
  283. * oom-killed):
  284. */
  285. pagefault_out_of_memory();
  286. } else {
  287. if (fault & VM_FAULT_SIGBUS)
  288. do_sigbus(regs, error_code, address);
  289. else if (fault & VM_FAULT_SIGSEGV)
  290. bad_area(regs, error_code, address);
  291. else
  292. BUG();
  293. }
  294. return 1;
  295. }
  296. static inline int access_error(int error_code, struct vm_area_struct *vma)
  297. {
  298. if (error_code & FAULT_CODE_WRITE) {
  299. /* write, present and write, not present: */
  300. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  301. return 1;
  302. return 0;
  303. }
  304. /* ITLB miss on NX page */
  305. if (unlikely((error_code & FAULT_CODE_ITLB) &&
  306. !(vma->vm_flags & VM_EXEC)))
  307. return 1;
  308. /* read, not present: */
  309. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  310. return 1;
  311. return 0;
  312. }
  313. static int fault_in_kernel_space(unsigned long address)
  314. {
  315. return address >= TASK_SIZE;
  316. }
  317. /*
  318. * This routine handles page faults. It determines the address,
  319. * and the problem, and then passes it off to one of the appropriate
  320. * routines.
  321. */
  322. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  323. unsigned long error_code,
  324. unsigned long address)
  325. {
  326. unsigned long vec;
  327. struct task_struct *tsk;
  328. struct mm_struct *mm;
  329. struct vm_area_struct * vma;
  330. vm_fault_t fault;
  331. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  332. tsk = current;
  333. mm = tsk->mm;
  334. vec = lookup_exception_vector();
  335. /*
  336. * We fault-in kernel-space virtual memory on-demand. The
  337. * 'reference' page table is init_mm.pgd.
  338. *
  339. * NOTE! We MUST NOT take any locks for this case. We may
  340. * be in an interrupt or a critical region, and should
  341. * only copy the information from the master page table,
  342. * nothing more.
  343. */
  344. if (unlikely(fault_in_kernel_space(address))) {
  345. if (vmalloc_fault(address) >= 0)
  346. return;
  347. if (notify_page_fault(regs, vec))
  348. return;
  349. bad_area_nosemaphore(regs, error_code, address);
  350. return;
  351. }
  352. if (unlikely(notify_page_fault(regs, vec)))
  353. return;
  354. /* Only enable interrupts if they were on before the fault */
  355. if ((regs->sr & SR_IMASK) != SR_IMASK)
  356. local_irq_enable();
  357. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  358. /*
  359. * If we're in an interrupt, have no user context or are running
  360. * with pagefaults disabled then we must not take the fault:
  361. */
  362. if (unlikely(faulthandler_disabled() || !mm)) {
  363. bad_area_nosemaphore(regs, error_code, address);
  364. return;
  365. }
  366. retry:
  367. down_read(&mm->mmap_sem);
  368. vma = find_vma(mm, address);
  369. if (unlikely(!vma)) {
  370. bad_area(regs, error_code, address);
  371. return;
  372. }
  373. if (likely(vma->vm_start <= address))
  374. goto good_area;
  375. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  376. bad_area(regs, error_code, address);
  377. return;
  378. }
  379. if (unlikely(expand_stack(vma, address))) {
  380. bad_area(regs, error_code, address);
  381. return;
  382. }
  383. /*
  384. * Ok, we have a good vm_area for this memory access, so
  385. * we can handle it..
  386. */
  387. good_area:
  388. if (unlikely(access_error(error_code, vma))) {
  389. bad_area_access_error(regs, error_code, address);
  390. return;
  391. }
  392. set_thread_fault_code(error_code);
  393. if (user_mode(regs))
  394. flags |= FAULT_FLAG_USER;
  395. if (error_code & FAULT_CODE_WRITE)
  396. flags |= FAULT_FLAG_WRITE;
  397. /*
  398. * If for any reason at all we couldn't handle the fault,
  399. * make sure we exit gracefully rather than endlessly redo
  400. * the fault.
  401. */
  402. fault = handle_mm_fault(vma, address, flags);
  403. if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
  404. if (mm_fault_error(regs, error_code, address, fault))
  405. return;
  406. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  407. if (fault & VM_FAULT_MAJOR) {
  408. tsk->maj_flt++;
  409. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  410. regs, address);
  411. } else {
  412. tsk->min_flt++;
  413. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  414. regs, address);
  415. }
  416. if (fault & VM_FAULT_RETRY) {
  417. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  418. flags |= FAULT_FLAG_TRIED;
  419. /*
  420. * No need to up_read(&mm->mmap_sem) as we would
  421. * have already released it in __lock_page_or_retry
  422. * in mm/filemap.c.
  423. */
  424. goto retry;
  425. }
  426. }
  427. up_read(&mm->mmap_sem);
  428. }