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