fault_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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 <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/openprom.h>
  26. #include <asm/oplib.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/asi.h>
  29. #include <asm/lsu.h>
  30. #include <asm/sections.h>
  31. #include <asm/mmu_context.h>
  32. int show_unhandled_signals = 1;
  33. static inline __kprobes int notify_page_fault(struct pt_regs *regs)
  34. {
  35. int ret = 0;
  36. /* kprobe_running() needs smp_processor_id() */
  37. if (kprobes_built_in() && !user_mode(regs)) {
  38. preempt_disable();
  39. if (kprobe_running() && kprobe_fault_handler(regs, 0))
  40. ret = 1;
  41. preempt_enable();
  42. }
  43. return ret;
  44. }
  45. static void __kprobes unhandled_fault(unsigned long address,
  46. struct task_struct *tsk,
  47. struct pt_regs *regs)
  48. {
  49. if ((unsigned long) address < PAGE_SIZE) {
  50. printk(KERN_ALERT "Unable to handle kernel NULL "
  51. "pointer dereference\n");
  52. } else {
  53. printk(KERN_ALERT "Unable to handle kernel paging request "
  54. "at virtual address %016lx\n", (unsigned long)address);
  55. }
  56. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
  57. (tsk->mm ?
  58. CTX_HWBITS(tsk->mm->context) :
  59. CTX_HWBITS(tsk->active_mm->context)));
  60. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
  61. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  62. (unsigned long) tsk->active_mm->pgd));
  63. die_if_kernel("Oops", regs);
  64. }
  65. static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
  66. {
  67. printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
  68. regs->tpc);
  69. printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
  70. printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
  71. printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
  72. dump_stack();
  73. unhandled_fault(regs->tpc, current, regs);
  74. }
  75. /*
  76. * We now make sure that mmap_sem is held in all paths that call
  77. * this. Additionally, to prevent kswapd from ripping ptes from
  78. * under us, raise interrupts around the time that we look at the
  79. * pte, kswapd will have to wait to get his smp ipi response from
  80. * us. vmtruncate likewise. This saves us having to get pte lock.
  81. */
  82. static unsigned int get_user_insn(unsigned long tpc)
  83. {
  84. pgd_t *pgdp = pgd_offset(current->mm, tpc);
  85. pud_t *pudp;
  86. pmd_t *pmdp;
  87. pte_t *ptep, pte;
  88. unsigned long pa;
  89. u32 insn = 0;
  90. if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
  91. goto out;
  92. pudp = pud_offset(pgdp, tpc);
  93. if (pud_none(*pudp) || unlikely(pud_bad(*pudp)))
  94. goto out;
  95. /* This disables preemption for us as well. */
  96. local_irq_disable();
  97. pmdp = pmd_offset(pudp, tpc);
  98. if (pmd_none(*pmdp) || unlikely(pmd_bad(*pmdp)))
  99. goto out_irq_enable;
  100. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  101. if (pmd_trans_huge(*pmdp)) {
  102. if (pmd_trans_splitting(*pmdp))
  103. goto out_irq_enable;
  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. extern int handle_ldf_stq(u32, struct pt_regs *);
  174. extern int handle_ld_nf(u32, struct pt_regs *);
  175. static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
  176. {
  177. if (!insn) {
  178. if (!regs->tpc || (regs->tpc & 0x3))
  179. return 0;
  180. if (regs->tstate & TSTATE_PRIV) {
  181. insn = *(unsigned int *) regs->tpc;
  182. } else {
  183. insn = get_user_insn(regs->tpc);
  184. }
  185. }
  186. return insn;
  187. }
  188. static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
  189. int fault_code, unsigned int insn,
  190. unsigned long address)
  191. {
  192. unsigned char asi = ASI_P;
  193. if ((!insn) && (regs->tstate & TSTATE_PRIV))
  194. goto cannot_handle;
  195. /* If user insn could be read (thus insn is zero), that
  196. * is fine. We will just gun down the process with a signal
  197. * in that case.
  198. */
  199. if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
  200. (insn & 0xc0800000) == 0xc0800000) {
  201. if (insn & 0x2000)
  202. asi = (regs->tstate >> 24);
  203. else
  204. asi = (insn >> 5);
  205. if ((asi & 0xf2) == 0x82) {
  206. if (insn & 0x1000000) {
  207. handle_ldf_stq(insn, regs);
  208. } else {
  209. /* This was a non-faulting load. Just clear the
  210. * destination register(s) and continue with the next
  211. * instruction. -jj
  212. */
  213. handle_ld_nf(insn, regs);
  214. }
  215. return;
  216. }
  217. }
  218. /* Is this in ex_table? */
  219. if (regs->tstate & TSTATE_PRIV) {
  220. const struct exception_table_entry *entry;
  221. entry = search_exception_tables(regs->tpc);
  222. if (entry) {
  223. regs->tpc = entry->fixup;
  224. regs->tnpc = regs->tpc + 4;
  225. return;
  226. }
  227. } else {
  228. /* The si_code was set to make clear whether
  229. * this was a SEGV_MAPERR or SEGV_ACCERR fault.
  230. */
  231. do_fault_siginfo(si_code, SIGSEGV, regs, address, insn, fault_code);
  232. return;
  233. }
  234. cannot_handle:
  235. unhandled_fault (address, current, regs);
  236. }
  237. static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
  238. {
  239. static int times;
  240. if (times++ < 10)
  241. printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
  242. "64-bit TPC [%lx]\n",
  243. current->comm, current->pid,
  244. regs->tpc);
  245. show_regs(regs);
  246. }
  247. asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
  248. {
  249. enum ctx_state prev_state = exception_enter();
  250. struct mm_struct *mm = current->mm;
  251. struct vm_area_struct *vma;
  252. unsigned int insn = 0;
  253. int si_code, fault_code, fault;
  254. unsigned long address, mm_rss;
  255. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  256. fault_code = get_thread_fault_code();
  257. if (notify_page_fault(regs))
  258. goto exit_exception;
  259. si_code = SEGV_MAPERR;
  260. address = current_thread_info()->fault_address;
  261. if ((fault_code & FAULT_CODE_ITLB) &&
  262. (fault_code & FAULT_CODE_DTLB))
  263. BUG();
  264. if (test_thread_flag(TIF_32BIT)) {
  265. if (!(regs->tstate & TSTATE_PRIV)) {
  266. if (unlikely((regs->tpc >> 32) != 0)) {
  267. bogus_32bit_fault_tpc(regs);
  268. goto intr_or_no_mm;
  269. }
  270. }
  271. if (unlikely((address >> 32) != 0))
  272. goto intr_or_no_mm;
  273. }
  274. if (regs->tstate & TSTATE_PRIV) {
  275. unsigned long tpc = regs->tpc;
  276. /* Sanity check the PC. */
  277. if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
  278. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  279. /* Valid, no problems... */
  280. } else {
  281. bad_kernel_pc(regs, address);
  282. goto exit_exception;
  283. }
  284. } else
  285. flags |= FAULT_FLAG_USER;
  286. /*
  287. * If we're in an interrupt or have no user
  288. * context, we must not take the fault..
  289. */
  290. if (in_atomic() || !mm)
  291. goto intr_or_no_mm;
  292. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  293. if (!down_read_trylock(&mm->mmap_sem)) {
  294. if ((regs->tstate & TSTATE_PRIV) &&
  295. !search_exception_tables(regs->tpc)) {
  296. insn = get_fault_insn(regs, insn);
  297. goto handle_kernel_fault;
  298. }
  299. retry:
  300. down_read(&mm->mmap_sem);
  301. }
  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. BUG_ON(address != regs->tpc);
  363. BUG_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(mm, 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_SIGBUS)
  390. goto do_sigbus;
  391. BUG();
  392. }
  393. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  394. if (fault & VM_FAULT_MAJOR) {
  395. current->maj_flt++;
  396. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  397. 1, regs, address);
  398. } else {
  399. current->min_flt++;
  400. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  401. 1, regs, address);
  402. }
  403. if (fault & VM_FAULT_RETRY) {
  404. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  405. flags |= FAULT_FLAG_TRIED;
  406. /* No need to up_read(&mm->mmap_sem) as we would
  407. * have already released it in __lock_page_or_retry
  408. * in mm/filemap.c.
  409. */
  410. goto retry;
  411. }
  412. }
  413. up_read(&mm->mmap_sem);
  414. mm_rss = get_mm_rss(mm);
  415. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  416. mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  417. #endif
  418. if (unlikely(mm_rss >
  419. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  420. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  421. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  422. mm_rss = mm->context.huge_pte_count;
  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. }