fault_64.c 14 KB

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