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