fault_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
  4. *
  5. * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
  6. * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
  7. */
  8. #include <asm/head.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/debug.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/mman.h>
  15. #include <linux/signal.h>
  16. #include <linux/mm.h>
  17. #include <linux/extable.h>
  18. #include <linux/init.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/kdebug.h>
  23. #include <linux/percpu.h>
  24. #include <linux/context_tracking.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/page.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/openprom.h>
  29. #include <asm/oplib.h>
  30. #include <asm/asi.h>
  31. #include <asm/lsu.h>
  32. #include <asm/sections.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/setup.h>
  35. int show_unhandled_signals = 1;
  36. static inline __kprobes int notify_page_fault(struct pt_regs *regs)
  37. {
  38. int ret = 0;
  39. /* kprobe_running() needs smp_processor_id() */
  40. if (kprobes_built_in() && !user_mode(regs)) {
  41. preempt_disable();
  42. if (kprobe_running() && kprobe_fault_handler(regs, 0))
  43. ret = 1;
  44. preempt_enable();
  45. }
  46. return ret;
  47. }
  48. static void __kprobes unhandled_fault(unsigned long address,
  49. struct task_struct *tsk,
  50. struct pt_regs *regs)
  51. {
  52. if ((unsigned long) address < PAGE_SIZE) {
  53. printk(KERN_ALERT "Unable to handle kernel NULL "
  54. "pointer dereference\n");
  55. } else {
  56. printk(KERN_ALERT "Unable to handle kernel paging request "
  57. "at virtual address %016lx\n", (unsigned long)address);
  58. }
  59. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
  60. (tsk->mm ?
  61. CTX_HWBITS(tsk->mm->context) :
  62. CTX_HWBITS(tsk->active_mm->context)));
  63. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
  64. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  65. (unsigned long) tsk->active_mm->pgd));
  66. die_if_kernel("Oops", regs);
  67. }
  68. static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
  69. {
  70. printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
  71. regs->tpc);
  72. printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
  73. printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
  74. printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
  75. dump_stack();
  76. unhandled_fault(regs->tpc, current, regs);
  77. }
  78. /*
  79. * We now make sure that mmap_sem is held in all paths that call
  80. * this. Additionally, to prevent kswapd from ripping ptes from
  81. * under us, raise interrupts around the time that we look at the
  82. * pte, kswapd will have to wait to get his smp ipi response from
  83. * us. vmtruncate likewise. This saves us having to get pte lock.
  84. */
  85. static unsigned int get_user_insn(unsigned long tpc)
  86. {
  87. pgd_t *pgdp = pgd_offset(current->mm, tpc);
  88. pud_t *pudp;
  89. pmd_t *pmdp;
  90. pte_t *ptep, pte;
  91. unsigned long pa;
  92. u32 insn = 0;
  93. if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
  94. goto out;
  95. pudp = pud_offset(pgdp, tpc);
  96. if (pud_none(*pudp) || unlikely(pud_bad(*pudp)))
  97. goto out;
  98. /* This disables preemption for us as well. */
  99. local_irq_disable();
  100. pmdp = pmd_offset(pudp, tpc);
  101. if (pmd_none(*pmdp) || unlikely(pmd_bad(*pmdp)))
  102. goto out_irq_enable;
  103. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  104. if (is_hugetlb_pmd(*pmdp)) {
  105. pa = pmd_pfn(*pmdp) << PAGE_SHIFT;
  106. pa += tpc & ~HPAGE_MASK;
  107. /* Use phys bypass so we don't pollute dtlb/dcache. */
  108. __asm__ __volatile__("lduwa [%1] %2, %0"
  109. : "=r" (insn)
  110. : "r" (pa), "i" (ASI_PHYS_USE_EC));
  111. } else
  112. #endif
  113. {
  114. ptep = pte_offset_map(pmdp, tpc);
  115. pte = *ptep;
  116. if (pte_present(pte)) {
  117. pa = (pte_pfn(pte) << PAGE_SHIFT);
  118. pa += (tpc & ~PAGE_MASK);
  119. /* Use phys bypass so we don't pollute dtlb/dcache. */
  120. __asm__ __volatile__("lduwa [%1] %2, %0"
  121. : "=r" (insn)
  122. : "r" (pa), "i" (ASI_PHYS_USE_EC));
  123. }
  124. pte_unmap(ptep);
  125. }
  126. out_irq_enable:
  127. local_irq_enable();
  128. out:
  129. return insn;
  130. }
  131. static inline void
  132. show_signal_msg(struct pt_regs *regs, int sig, int code,
  133. unsigned long address, struct task_struct *tsk)
  134. {
  135. if (!unhandled_signal(tsk, sig))
  136. return;
  137. if (!printk_ratelimit())
  138. return;
  139. printk("%s%s[%d]: segfault at %lx ip %px (rpc %px) sp %px error %x",
  140. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  141. tsk->comm, task_pid_nr(tsk), address,
  142. (void *)regs->tpc, (void *)regs->u_regs[UREG_I7],
  143. (void *)regs->u_regs[UREG_FP], code);
  144. print_vma_addr(KERN_CONT " in ", regs->tpc);
  145. printk(KERN_CONT "\n");
  146. }
  147. static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  148. unsigned long fault_addr, unsigned int insn,
  149. int fault_code)
  150. {
  151. unsigned long addr;
  152. if (fault_code & FAULT_CODE_ITLB) {
  153. addr = regs->tpc;
  154. } else {
  155. /* If we were able to probe the faulting instruction, use it
  156. * to compute a precise fault address. Otherwise use the fault
  157. * time provided address which may only have page granularity.
  158. */
  159. if (insn)
  160. addr = compute_effective_address(regs, insn, 0);
  161. else
  162. addr = fault_addr;
  163. }
  164. if (unlikely(show_unhandled_signals))
  165. show_signal_msg(regs, sig, code, addr, current);
  166. force_sig_fault(sig, code, (void __user *) addr, 0, current);
  167. }
  168. static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
  169. {
  170. if (!insn) {
  171. if (!regs->tpc || (regs->tpc & 0x3))
  172. return 0;
  173. if (regs->tstate & TSTATE_PRIV) {
  174. insn = *(unsigned int *) regs->tpc;
  175. } else {
  176. insn = get_user_insn(regs->tpc);
  177. }
  178. }
  179. return insn;
  180. }
  181. static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
  182. int fault_code, unsigned int insn,
  183. unsigned long address)
  184. {
  185. unsigned char asi = ASI_P;
  186. if ((!insn) && (regs->tstate & TSTATE_PRIV))
  187. goto cannot_handle;
  188. /* If user insn could be read (thus insn is zero), that
  189. * is fine. We will just gun down the process with a signal
  190. * in that case.
  191. */
  192. if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
  193. (insn & 0xc0800000) == 0xc0800000) {
  194. if (insn & 0x2000)
  195. asi = (regs->tstate >> 24);
  196. else
  197. asi = (insn >> 5);
  198. if ((asi & 0xf2) == 0x82) {
  199. if (insn & 0x1000000) {
  200. handle_ldf_stq(insn, regs);
  201. } else {
  202. /* This was a non-faulting load. Just clear the
  203. * destination register(s) and continue with the next
  204. * instruction. -jj
  205. */
  206. handle_ld_nf(insn, regs);
  207. }
  208. return;
  209. }
  210. }
  211. /* Is this in ex_table? */
  212. if (regs->tstate & TSTATE_PRIV) {
  213. const struct exception_table_entry *entry;
  214. entry = search_exception_tables(regs->tpc);
  215. if (entry) {
  216. regs->tpc = entry->fixup;
  217. regs->tnpc = regs->tpc + 4;
  218. return;
  219. }
  220. } else {
  221. /* The si_code was set to make clear whether
  222. * this was a SEGV_MAPERR or SEGV_ACCERR fault.
  223. */
  224. do_fault_siginfo(si_code, SIGSEGV, regs, address, insn, fault_code);
  225. return;
  226. }
  227. cannot_handle:
  228. unhandled_fault (address, current, regs);
  229. }
  230. static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
  231. {
  232. static int times;
  233. if (times++ < 10)
  234. printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
  235. "64-bit TPC [%lx]\n",
  236. current->comm, current->pid,
  237. regs->tpc);
  238. show_regs(regs);
  239. }
  240. asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
  241. {
  242. enum ctx_state prev_state = exception_enter();
  243. struct mm_struct *mm = current->mm;
  244. struct vm_area_struct *vma;
  245. unsigned int insn = 0;
  246. int si_code, fault_code, fault;
  247. unsigned long address, mm_rss;
  248. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  249. fault_code = get_thread_fault_code();
  250. if (notify_page_fault(regs))
  251. goto exit_exception;
  252. si_code = SEGV_MAPERR;
  253. address = current_thread_info()->fault_address;
  254. if ((fault_code & FAULT_CODE_ITLB) &&
  255. (fault_code & FAULT_CODE_DTLB))
  256. BUG();
  257. if (test_thread_flag(TIF_32BIT)) {
  258. if (!(regs->tstate & TSTATE_PRIV)) {
  259. if (unlikely((regs->tpc >> 32) != 0)) {
  260. bogus_32bit_fault_tpc(regs);
  261. goto intr_or_no_mm;
  262. }
  263. }
  264. if (unlikely((address >> 32) != 0))
  265. goto intr_or_no_mm;
  266. }
  267. if (regs->tstate & TSTATE_PRIV) {
  268. unsigned long tpc = regs->tpc;
  269. /* Sanity check the PC. */
  270. if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
  271. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  272. /* Valid, no problems... */
  273. } else {
  274. bad_kernel_pc(regs, address);
  275. goto exit_exception;
  276. }
  277. } else
  278. flags |= FAULT_FLAG_USER;
  279. /*
  280. * If we're in an interrupt or have no user
  281. * context, we must not take the fault..
  282. */
  283. if (faulthandler_disabled() || !mm)
  284. goto intr_or_no_mm;
  285. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  286. if (!down_read_trylock(&mm->mmap_sem)) {
  287. if ((regs->tstate & TSTATE_PRIV) &&
  288. !search_exception_tables(regs->tpc)) {
  289. insn = get_fault_insn(regs, insn);
  290. goto handle_kernel_fault;
  291. }
  292. retry:
  293. down_read(&mm->mmap_sem);
  294. }
  295. if (fault_code & FAULT_CODE_BAD_RA)
  296. goto do_sigbus;
  297. vma = find_vma(mm, address);
  298. if (!vma)
  299. goto bad_area;
  300. /* Pure DTLB misses do not tell us whether the fault causing
  301. * load/store/atomic was a write or not, it only says that there
  302. * was no match. So in such a case we (carefully) read the
  303. * instruction to try and figure this out. It's an optimization
  304. * so it's ok if we can't do this.
  305. *
  306. * Special hack, window spill/fill knows the exact fault type.
  307. */
  308. if (((fault_code &
  309. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  310. (vma->vm_flags & VM_WRITE) != 0) {
  311. insn = get_fault_insn(regs, 0);
  312. if (!insn)
  313. goto continue_fault;
  314. /* All loads, stores and atomics have bits 30 and 31 both set
  315. * in the instruction. Bit 21 is set in all stores, but we
  316. * have to avoid prefetches which also have bit 21 set.
  317. */
  318. if ((insn & 0xc0200000) == 0xc0200000 &&
  319. (insn & 0x01780000) != 0x01680000) {
  320. /* Don't bother updating thread struct value,
  321. * because update_mmu_cache only cares which tlb
  322. * the access came from.
  323. */
  324. fault_code |= FAULT_CODE_WRITE;
  325. }
  326. }
  327. continue_fault:
  328. if (vma->vm_start <= address)
  329. goto good_area;
  330. if (!(vma->vm_flags & VM_GROWSDOWN))
  331. goto bad_area;
  332. if (!(fault_code & FAULT_CODE_WRITE)) {
  333. /* Non-faulting loads shouldn't expand stack. */
  334. insn = get_fault_insn(regs, insn);
  335. if ((insn & 0xc0800000) == 0xc0800000) {
  336. unsigned char asi;
  337. if (insn & 0x2000)
  338. asi = (regs->tstate >> 24);
  339. else
  340. asi = (insn >> 5);
  341. if ((asi & 0xf2) == 0x82)
  342. goto bad_area;
  343. }
  344. }
  345. if (expand_stack(vma, address))
  346. goto bad_area;
  347. /*
  348. * Ok, we have a good vm_area for this memory access, so
  349. * we can handle it..
  350. */
  351. good_area:
  352. si_code = SEGV_ACCERR;
  353. /* If we took a ITLB miss on a non-executable page, catch
  354. * that here.
  355. */
  356. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  357. WARN(address != regs->tpc,
  358. "address (%lx) != regs->tpc (%lx)\n", address, regs->tpc);
  359. WARN_ON(regs->tstate & TSTATE_PRIV);
  360. goto bad_area;
  361. }
  362. if (fault_code & FAULT_CODE_WRITE) {
  363. if (!(vma->vm_flags & VM_WRITE))
  364. goto bad_area;
  365. /* Spitfire has an icache which does not snoop
  366. * processor stores. Later processors do...
  367. */
  368. if (tlb_type == spitfire &&
  369. (vma->vm_flags & VM_EXEC) != 0 &&
  370. vma->vm_file != NULL)
  371. set_thread_fault_code(fault_code |
  372. FAULT_CODE_BLKCOMMIT);
  373. flags |= FAULT_FLAG_WRITE;
  374. } else {
  375. /* Allow reads even for write-only mappings */
  376. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  377. goto bad_area;
  378. }
  379. fault = handle_mm_fault(vma, address, flags);
  380. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  381. goto exit_exception;
  382. if (unlikely(fault & VM_FAULT_ERROR)) {
  383. if (fault & VM_FAULT_OOM)
  384. goto out_of_memory;
  385. else if (fault & VM_FAULT_SIGSEGV)
  386. goto bad_area;
  387. else if (fault & VM_FAULT_SIGBUS)
  388. goto do_sigbus;
  389. BUG();
  390. }
  391. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  392. if (fault & VM_FAULT_MAJOR) {
  393. current->maj_flt++;
  394. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  395. 1, regs, address);
  396. } else {
  397. current->min_flt++;
  398. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  399. 1, regs, address);
  400. }
  401. if (fault & VM_FAULT_RETRY) {
  402. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  403. flags |= FAULT_FLAG_TRIED;
  404. /* No need to up_read(&mm->mmap_sem) as we would
  405. * have already released it in __lock_page_or_retry
  406. * in mm/filemap.c.
  407. */
  408. goto retry;
  409. }
  410. }
  411. up_read(&mm->mmap_sem);
  412. mm_rss = get_mm_rss(mm);
  413. #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
  414. mm_rss -= (mm->context.thp_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  415. #endif
  416. if (unlikely(mm_rss >
  417. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  418. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  419. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  420. mm_rss = mm->context.hugetlb_pte_count + mm->context.thp_pte_count;
  421. mm_rss *= REAL_HPAGE_PER_HPAGE;
  422. if (unlikely(mm_rss >
  423. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit)) {
  424. if (mm->context.tsb_block[MM_TSB_HUGE].tsb)
  425. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  426. else
  427. hugetlb_setup(regs);
  428. }
  429. #endif
  430. exit_exception:
  431. exception_exit(prev_state);
  432. return;
  433. /*
  434. * Something tried to access memory that isn't in our memory map..
  435. * Fix it, but check if it's kernel or user first..
  436. */
  437. bad_area:
  438. insn = get_fault_insn(regs, insn);
  439. up_read(&mm->mmap_sem);
  440. handle_kernel_fault:
  441. do_kernel_fault(regs, si_code, fault_code, insn, address);
  442. goto exit_exception;
  443. /*
  444. * We ran out of memory, or some other thing happened to us that made
  445. * us unable to handle the page fault gracefully.
  446. */
  447. out_of_memory:
  448. insn = get_fault_insn(regs, insn);
  449. up_read(&mm->mmap_sem);
  450. if (!(regs->tstate & TSTATE_PRIV)) {
  451. pagefault_out_of_memory();
  452. goto exit_exception;
  453. }
  454. goto handle_kernel_fault;
  455. intr_or_no_mm:
  456. insn = get_fault_insn(regs, 0);
  457. goto handle_kernel_fault;
  458. do_sigbus:
  459. insn = get_fault_insn(regs, insn);
  460. up_read(&mm->mmap_sem);
  461. /*
  462. * Send a sigbus, regardless of whether we were in kernel
  463. * or user mode.
  464. */
  465. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, address, insn, fault_code);
  466. /* Kernel mode? Handle exceptions or die */
  467. if (regs->tstate & TSTATE_PRIV)
  468. goto handle_kernel_fault;
  469. }