fault_64.c 14 KB

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