fault.c 11 KB

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