fault_64.c 14 KB

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