fault_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. 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", 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", 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", sizeof(*pte) * 2, (u64)pte_val(*pte));
  96. } while (0);
  97. printk("\n");
  98. }
  99. static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
  100. {
  101. unsigned index = pgd_index(address);
  102. pgd_t *pgd_k;
  103. pud_t *pud, *pud_k;
  104. pmd_t *pmd, *pmd_k;
  105. pgd += index;
  106. pgd_k = init_mm.pgd + index;
  107. if (!pgd_present(*pgd_k))
  108. return NULL;
  109. pud = pud_offset(pgd, address);
  110. pud_k = pud_offset(pgd_k, address);
  111. if (!pud_present(*pud_k))
  112. return NULL;
  113. if (!pud_present(*pud))
  114. set_pud(pud, *pud_k);
  115. pmd = pmd_offset(pud, address);
  116. pmd_k = pmd_offset(pud_k, address);
  117. if (!pmd_present(*pmd_k))
  118. return NULL;
  119. if (!pmd_present(*pmd))
  120. set_pmd(pmd, *pmd_k);
  121. else {
  122. /*
  123. * The page tables are fully synchronised so there must
  124. * be another reason for the fault. Return NULL here to
  125. * signal that we have not taken care of the fault.
  126. */
  127. BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
  128. return NULL;
  129. }
  130. return pmd_k;
  131. }
  132. /*
  133. * Handle a fault on the vmalloc or module mapping area
  134. */
  135. static noinline int vmalloc_fault(unsigned long address)
  136. {
  137. pgd_t *pgd_k;
  138. pmd_t *pmd_k;
  139. pte_t *pte_k;
  140. /* Make sure we are in vmalloc/module area: */
  141. if (!is_vmalloc_addr((void *)address))
  142. return -1;
  143. /*
  144. * Synchronize this task's top level page-table
  145. * with the 'reference' page table.
  146. *
  147. * Do _not_ use "current" here. We might be inside
  148. * an interrupt in the middle of a task switch..
  149. */
  150. pgd_k = get_TTB();
  151. pmd_k = vmalloc_sync_one(pgd_k, address);
  152. if (!pmd_k)
  153. return -1;
  154. pte_k = pte_offset_kernel(pmd_k, address);
  155. if (!pte_present(*pte_k))
  156. return -1;
  157. return 0;
  158. }
  159. static void
  160. show_fault_oops(struct pt_regs *regs, unsigned long address)
  161. {
  162. if (!oops_may_print())
  163. return;
  164. printk(KERN_ALERT "BUG: unable to handle kernel ");
  165. if (address < PAGE_SIZE)
  166. printk(KERN_CONT "NULL pointer dereference");
  167. else
  168. printk(KERN_CONT "paging request");
  169. printk(KERN_CONT " at %08lx\n", address);
  170. printk(KERN_ALERT "PC:");
  171. printk_address(regs->pc, 1);
  172. show_pte(NULL, address);
  173. }
  174. static noinline void
  175. no_context(struct pt_regs *regs, unsigned long error_code,
  176. unsigned long address)
  177. {
  178. /* Are we prepared to handle this kernel fault? */
  179. if (fixup_exception(regs))
  180. return;
  181. if (handle_trapped_io(regs, address))
  182. return;
  183. /*
  184. * Oops. The kernel tried to access some bad page. We'll have to
  185. * terminate things with extreme prejudice.
  186. */
  187. bust_spinlocks(1);
  188. show_fault_oops(regs, address);
  189. die("Oops", regs, error_code);
  190. bust_spinlocks(0);
  191. do_exit(SIGKILL);
  192. }
  193. static void
  194. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  195. unsigned long address, int si_code)
  196. {
  197. struct task_struct *tsk = current;
  198. /* User mode accesses just cause a SIGSEGV */
  199. if (user_mode(regs)) {
  200. /*
  201. * It's possible to have interrupts off here:
  202. */
  203. local_irq_enable();
  204. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  205. return;
  206. }
  207. no_context(regs, error_code, address);
  208. }
  209. static noinline void
  210. bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  211. unsigned long address)
  212. {
  213. __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
  214. }
  215. static void
  216. __bad_area(struct pt_regs *regs, unsigned long error_code,
  217. unsigned long address, int si_code)
  218. {
  219. struct mm_struct *mm = current->mm;
  220. /*
  221. * Something tried to access memory that isn't in our memory map..
  222. * Fix it, but check if it's kernel or user first..
  223. */
  224. up_read(&mm->mmap_sem);
  225. __bad_area_nosemaphore(regs, error_code, address, si_code);
  226. }
  227. static noinline void
  228. bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
  229. {
  230. __bad_area(regs, error_code, address, SEGV_MAPERR);
  231. }
  232. static noinline void
  233. bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
  234. unsigned long address)
  235. {
  236. __bad_area(regs, error_code, address, SEGV_ACCERR);
  237. }
  238. static void out_of_memory(void)
  239. {
  240. /*
  241. * We ran out of memory, call the OOM killer, and return the userspace
  242. * (which will retry the fault, or kill us if we got oom-killed):
  243. */
  244. up_read(&current->mm->mmap_sem);
  245. pagefault_out_of_memory();
  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. out_of_memory();
  283. } else {
  284. if (fault & VM_FAULT_SIGBUS)
  285. do_sigbus(regs, error_code, address);
  286. else
  287. BUG();
  288. }
  289. return 1;
  290. }
  291. static inline int access_error(int write, struct vm_area_struct *vma)
  292. {
  293. if (write) {
  294. /* write, present and write, not present: */
  295. if (unlikely(!(vma->vm_flags & VM_WRITE)))
  296. return 1;
  297. return 0;
  298. }
  299. /* read, not present: */
  300. if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
  301. return 1;
  302. return 0;
  303. }
  304. static int fault_in_kernel_space(unsigned long address)
  305. {
  306. return address >= TASK_SIZE;
  307. }
  308. /*
  309. * This routine handles page faults. It determines the address,
  310. * and the problem, and then passes it off to one of the appropriate
  311. * routines.
  312. */
  313. asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  314. unsigned long error_code,
  315. unsigned long address)
  316. {
  317. unsigned long vec;
  318. struct task_struct *tsk;
  319. struct mm_struct *mm;
  320. struct vm_area_struct * vma;
  321. int fault;
  322. int write = error_code & FAULT_CODE_WRITE;
  323. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  324. (write ? FAULT_FLAG_WRITE : 0));
  325. tsk = current;
  326. mm = tsk->mm;
  327. vec = lookup_exception_vector();
  328. /*
  329. * We fault-in kernel-space virtual memory on-demand. The
  330. * 'reference' page table is init_mm.pgd.
  331. *
  332. * NOTE! We MUST NOT take any locks for this case. We may
  333. * be in an interrupt or a critical region, and should
  334. * only copy the information from the master page table,
  335. * nothing more.
  336. */
  337. if (unlikely(fault_in_kernel_space(address))) {
  338. if (vmalloc_fault(address) >= 0)
  339. return;
  340. if (notify_page_fault(regs, vec))
  341. return;
  342. bad_area_nosemaphore(regs, error_code, address);
  343. return;
  344. }
  345. if (unlikely(notify_page_fault(regs, vec)))
  346. return;
  347. /* Only enable interrupts if they were on before the fault */
  348. if ((regs->sr & SR_IMASK) != SR_IMASK)
  349. local_irq_enable();
  350. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  351. /*
  352. * If we're in an interrupt, have no user context or are running
  353. * in an atomic region then we must not take the fault:
  354. */
  355. if (unlikely(in_atomic() || !mm)) {
  356. bad_area_nosemaphore(regs, error_code, address);
  357. return;
  358. }
  359. retry:
  360. down_read(&mm->mmap_sem);
  361. vma = find_vma(mm, address);
  362. if (unlikely(!vma)) {
  363. bad_area(regs, error_code, address);
  364. return;
  365. }
  366. if (likely(vma->vm_start <= address))
  367. goto good_area;
  368. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  369. bad_area(regs, error_code, address);
  370. return;
  371. }
  372. if (unlikely(expand_stack(vma, address))) {
  373. bad_area(regs, error_code, address);
  374. return;
  375. }
  376. /*
  377. * Ok, we have a good vm_area for this memory access, so
  378. * we can handle it..
  379. */
  380. good_area:
  381. if (unlikely(access_error(error_code, vma))) {
  382. bad_area_access_error(regs, error_code, address);
  383. return;
  384. }
  385. set_thread_fault_code(error_code);
  386. /*
  387. * If for any reason at all we couldn't handle the fault,
  388. * make sure we exit gracefully rather than endlessly redo
  389. * the fault.
  390. */
  391. fault = handle_mm_fault(mm, vma, address, flags);
  392. if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
  393. if (mm_fault_error(regs, error_code, address, fault))
  394. return;
  395. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  396. if (fault & VM_FAULT_MAJOR) {
  397. tsk->maj_flt++;
  398. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  399. regs, address);
  400. } else {
  401. tsk->min_flt++;
  402. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  403. regs, address);
  404. }
  405. if (fault & VM_FAULT_RETRY) {
  406. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  407. /*
  408. * No need to up_read(&mm->mmap_sem) as we would
  409. * have already released it in __lock_page_or_retry
  410. * in mm/filemap.c.
  411. */
  412. goto retry;
  413. }
  414. }
  415. up_read(&mm->mmap_sem);
  416. }
  417. /*
  418. * Called with interrupts disabled.
  419. */
  420. asmlinkage int __kprobes
  421. handle_tlbmiss(struct pt_regs *regs, unsigned long error_code,
  422. unsigned long address)
  423. {
  424. pgd_t *pgd;
  425. pud_t *pud;
  426. pmd_t *pmd;
  427. pte_t *pte;
  428. pte_t entry;
  429. /*
  430. * We don't take page faults for P1, P2, and parts of P4, these
  431. * are always mapped, whether it be due to legacy behaviour in
  432. * 29-bit mode, or due to PMB configuration in 32-bit mode.
  433. */
  434. if (address >= P3SEG && address < P3_ADDR_MAX) {
  435. pgd = pgd_offset_k(address);
  436. } else {
  437. if (unlikely(address >= TASK_SIZE || !current->mm))
  438. return 1;
  439. pgd = pgd_offset(current->mm, address);
  440. }
  441. pud = pud_offset(pgd, address);
  442. if (pud_none_or_clear_bad(pud))
  443. return 1;
  444. pmd = pmd_offset(pud, address);
  445. if (pmd_none_or_clear_bad(pmd))
  446. return 1;
  447. pte = pte_offset_kernel(pmd, address);
  448. entry = *pte;
  449. if (unlikely(pte_none(entry) || pte_not_present(entry)))
  450. return 1;
  451. if (unlikely(error_code && !pte_write(entry)))
  452. return 1;
  453. if (error_code)
  454. entry = pte_mkdirty(entry);
  455. entry = pte_mkyoung(entry);
  456. set_pte(pte, entry);
  457. #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
  458. /*
  459. * SH-4 does not set MMUCR.RC to the corresponding TLB entry in
  460. * the case of an initial page write exception, so we need to
  461. * flush it in order to avoid potential TLB entry duplication.
  462. */
  463. if (error_code == FAULT_CODE_INITIAL)
  464. local_flush_tlb_one(get_asid(), address & PAGE_MASK);
  465. #endif
  466. set_thread_fault_code(error_code);
  467. update_mmu_cache(NULL, address, pte);
  468. return 0;
  469. }