fault_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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;
  247. vm_fault_t fault;
  248. unsigned long address, mm_rss;
  249. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  250. fault_code = get_thread_fault_code();
  251. if (notify_page_fault(regs))
  252. goto exit_exception;
  253. si_code = SEGV_MAPERR;
  254. address = current_thread_info()->fault_address;
  255. if ((fault_code & FAULT_CODE_ITLB) &&
  256. (fault_code & FAULT_CODE_DTLB))
  257. BUG();
  258. if (test_thread_flag(TIF_32BIT)) {
  259. if (!(regs->tstate & TSTATE_PRIV)) {
  260. if (unlikely((regs->tpc >> 32) != 0)) {
  261. bogus_32bit_fault_tpc(regs);
  262. goto intr_or_no_mm;
  263. }
  264. }
  265. if (unlikely((address >> 32) != 0))
  266. goto intr_or_no_mm;
  267. }
  268. if (regs->tstate & TSTATE_PRIV) {
  269. unsigned long tpc = regs->tpc;
  270. /* Sanity check the PC. */
  271. if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
  272. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  273. /* Valid, no problems... */
  274. } else {
  275. bad_kernel_pc(regs, address);
  276. goto exit_exception;
  277. }
  278. } else
  279. flags |= FAULT_FLAG_USER;
  280. /*
  281. * If we're in an interrupt or have no user
  282. * context, we must not take the fault..
  283. */
  284. if (faulthandler_disabled() || !mm)
  285. goto intr_or_no_mm;
  286. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  287. if (!down_read_trylock(&mm->mmap_sem)) {
  288. if ((regs->tstate & TSTATE_PRIV) &&
  289. !search_exception_tables(regs->tpc)) {
  290. insn = get_fault_insn(regs, insn);
  291. goto handle_kernel_fault;
  292. }
  293. retry:
  294. down_read(&mm->mmap_sem);
  295. }
  296. if (fault_code & FAULT_CODE_BAD_RA)
  297. goto do_sigbus;
  298. vma = find_vma(mm, address);
  299. if (!vma)
  300. goto bad_area;
  301. /* Pure DTLB misses do not tell us whether the fault causing
  302. * load/store/atomic was a write or not, it only says that there
  303. * was no match. So in such a case we (carefully) read the
  304. * instruction to try and figure this out. It's an optimization
  305. * so it's ok if we can't do this.
  306. *
  307. * Special hack, window spill/fill knows the exact fault type.
  308. */
  309. if (((fault_code &
  310. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  311. (vma->vm_flags & VM_WRITE) != 0) {
  312. insn = get_fault_insn(regs, 0);
  313. if (!insn)
  314. goto continue_fault;
  315. /* All loads, stores and atomics have bits 30 and 31 both set
  316. * in the instruction. Bit 21 is set in all stores, but we
  317. * have to avoid prefetches which also have bit 21 set.
  318. */
  319. if ((insn & 0xc0200000) == 0xc0200000 &&
  320. (insn & 0x01780000) != 0x01680000) {
  321. /* Don't bother updating thread struct value,
  322. * because update_mmu_cache only cares which tlb
  323. * the access came from.
  324. */
  325. fault_code |= FAULT_CODE_WRITE;
  326. }
  327. }
  328. continue_fault:
  329. if (vma->vm_start <= address)
  330. goto good_area;
  331. if (!(vma->vm_flags & VM_GROWSDOWN))
  332. goto bad_area;
  333. if (!(fault_code & FAULT_CODE_WRITE)) {
  334. /* Non-faulting loads shouldn't expand stack. */
  335. insn = get_fault_insn(regs, insn);
  336. if ((insn & 0xc0800000) == 0xc0800000) {
  337. unsigned char asi;
  338. if (insn & 0x2000)
  339. asi = (regs->tstate >> 24);
  340. else
  341. asi = (insn >> 5);
  342. if ((asi & 0xf2) == 0x82)
  343. goto bad_area;
  344. }
  345. }
  346. if (expand_stack(vma, address))
  347. goto bad_area;
  348. /*
  349. * Ok, we have a good vm_area for this memory access, so
  350. * we can handle it..
  351. */
  352. good_area:
  353. si_code = SEGV_ACCERR;
  354. /* If we took a ITLB miss on a non-executable page, catch
  355. * that here.
  356. */
  357. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  358. WARN(address != regs->tpc,
  359. "address (%lx) != regs->tpc (%lx)\n", address, regs->tpc);
  360. WARN_ON(regs->tstate & TSTATE_PRIV);
  361. goto bad_area;
  362. }
  363. if (fault_code & FAULT_CODE_WRITE) {
  364. if (!(vma->vm_flags & VM_WRITE))
  365. goto bad_area;
  366. /* Spitfire has an icache which does not snoop
  367. * processor stores. Later processors do...
  368. */
  369. if (tlb_type == spitfire &&
  370. (vma->vm_flags & VM_EXEC) != 0 &&
  371. vma->vm_file != NULL)
  372. set_thread_fault_code(fault_code |
  373. FAULT_CODE_BLKCOMMIT);
  374. flags |= FAULT_FLAG_WRITE;
  375. } else {
  376. /* Allow reads even for write-only mappings */
  377. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  378. goto bad_area;
  379. }
  380. fault = handle_mm_fault(vma, address, flags);
  381. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  382. goto exit_exception;
  383. if (unlikely(fault & VM_FAULT_ERROR)) {
  384. if (fault & VM_FAULT_OOM)
  385. goto out_of_memory;
  386. else if (fault & VM_FAULT_SIGSEGV)
  387. goto bad_area;
  388. else if (fault & VM_FAULT_SIGBUS)
  389. goto do_sigbus;
  390. BUG();
  391. }
  392. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  393. if (fault & VM_FAULT_MAJOR) {
  394. current->maj_flt++;
  395. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  396. 1, regs, address);
  397. } else {
  398. current->min_flt++;
  399. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  400. 1, regs, address);
  401. }
  402. if (fault & VM_FAULT_RETRY) {
  403. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  404. flags |= FAULT_FLAG_TRIED;
  405. /* No need to up_read(&mm->mmap_sem) as we would
  406. * have already released it in __lock_page_or_retry
  407. * in mm/filemap.c.
  408. */
  409. goto retry;
  410. }
  411. }
  412. up_read(&mm->mmap_sem);
  413. mm_rss = get_mm_rss(mm);
  414. #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
  415. mm_rss -= (mm->context.thp_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  416. #endif
  417. if (unlikely(mm_rss >
  418. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  419. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  420. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  421. mm_rss = mm->context.hugetlb_pte_count + mm->context.thp_pte_count;
  422. mm_rss *= REAL_HPAGE_PER_HPAGE;
  423. if (unlikely(mm_rss >
  424. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit)) {
  425. if (mm->context.tsb_block[MM_TSB_HUGE].tsb)
  426. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  427. else
  428. hugetlb_setup(regs);
  429. }
  430. #endif
  431. exit_exception:
  432. exception_exit(prev_state);
  433. return;
  434. /*
  435. * Something tried to access memory that isn't in our memory map..
  436. * Fix it, but check if it's kernel or user first..
  437. */
  438. bad_area:
  439. insn = get_fault_insn(regs, insn);
  440. up_read(&mm->mmap_sem);
  441. handle_kernel_fault:
  442. do_kernel_fault(regs, si_code, fault_code, insn, address);
  443. goto exit_exception;
  444. /*
  445. * We ran out of memory, or some other thing happened to us that made
  446. * us unable to handle the page fault gracefully.
  447. */
  448. out_of_memory:
  449. insn = get_fault_insn(regs, insn);
  450. up_read(&mm->mmap_sem);
  451. if (!(regs->tstate & TSTATE_PRIV)) {
  452. pagefault_out_of_memory();
  453. goto exit_exception;
  454. }
  455. goto handle_kernel_fault;
  456. intr_or_no_mm:
  457. insn = get_fault_insn(regs, 0);
  458. goto handle_kernel_fault;
  459. do_sigbus:
  460. insn = get_fault_insn(regs, insn);
  461. up_read(&mm->mmap_sem);
  462. /*
  463. * Send a sigbus, regardless of whether we were in kernel
  464. * or user mode.
  465. */
  466. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, address, insn, fault_code);
  467. /* Kernel mode? Handle exceptions or die */
  468. if (regs->tstate & TSTATE_PRIV)
  469. goto handle_kernel_fault;
  470. }