fault_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 <asm/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/openprom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/asi.h>
  28. #include <asm/lsu.h>
  29. #include <asm/sections.h>
  30. #include <asm/mmu_context.h>
  31. int show_unhandled_signals = 1;
  32. static inline __kprobes int notify_page_fault(struct pt_regs *regs)
  33. {
  34. int ret = 0;
  35. /* kprobe_running() needs smp_processor_id() */
  36. if (kprobes_built_in() && !user_mode(regs)) {
  37. preempt_disable();
  38. if (kprobe_running() && kprobe_fault_handler(regs, 0))
  39. ret = 1;
  40. preempt_enable();
  41. }
  42. return ret;
  43. }
  44. static void __kprobes unhandled_fault(unsigned long address,
  45. struct task_struct *tsk,
  46. struct pt_regs *regs)
  47. {
  48. if ((unsigned long) address < PAGE_SIZE) {
  49. printk(KERN_ALERT "Unable to handle kernel NULL "
  50. "pointer dereference\n");
  51. } else {
  52. printk(KERN_ALERT "Unable to handle kernel paging request "
  53. "at virtual address %016lx\n", (unsigned long)address);
  54. }
  55. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
  56. (tsk->mm ?
  57. CTX_HWBITS(tsk->mm->context) :
  58. CTX_HWBITS(tsk->active_mm->context)));
  59. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
  60. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  61. (unsigned long) tsk->active_mm->pgd));
  62. die_if_kernel("Oops", regs);
  63. }
  64. static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
  65. {
  66. printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
  67. regs->tpc);
  68. printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
  69. printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
  70. printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
  71. dump_stack();
  72. unhandled_fault(regs->tpc, current, regs);
  73. }
  74. /*
  75. * We now make sure that mmap_sem is held in all paths that call
  76. * this. Additionally, to prevent kswapd from ripping ptes from
  77. * under us, raise interrupts around the time that we look at the
  78. * pte, kswapd will have to wait to get his smp ipi response from
  79. * us. vmtruncate likewise. This saves us having to get pte lock.
  80. */
  81. static unsigned int get_user_insn(unsigned long tpc)
  82. {
  83. pgd_t *pgdp = pgd_offset(current->mm, tpc);
  84. pud_t *pudp;
  85. pmd_t *pmdp;
  86. pte_t *ptep, pte;
  87. unsigned long pa;
  88. u32 insn = 0;
  89. if (pgd_none(*pgdp) || unlikely(pgd_bad(*pgdp)))
  90. goto out;
  91. pudp = pud_offset(pgdp, tpc);
  92. if (pud_none(*pudp) || unlikely(pud_bad(*pudp)))
  93. goto out;
  94. /* This disables preemption for us as well. */
  95. local_irq_disable();
  96. pmdp = pmd_offset(pudp, tpc);
  97. if (pmd_none(*pmdp) || unlikely(pmd_bad(*pmdp)))
  98. goto out_irq_enable;
  99. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  100. if (pmd_trans_huge(*pmdp)) {
  101. if (pmd_trans_splitting(*pmdp))
  102. goto out_irq_enable;
  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. extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
  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. 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. return;
  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. return;
  282. }
  283. }
  284. /*
  285. * If we're in an interrupt or have no user
  286. * context, we must not take the fault..
  287. */
  288. if (in_atomic() || !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. vma = find_vma(mm, address);
  301. if (!vma)
  302. goto bad_area;
  303. /* Pure DTLB misses do not tell us whether the fault causing
  304. * load/store/atomic was a write or not, it only says that there
  305. * was no match. So in such a case we (carefully) read the
  306. * instruction to try and figure this out. It's an optimization
  307. * so it's ok if we can't do this.
  308. *
  309. * Special hack, window spill/fill knows the exact fault type.
  310. */
  311. if (((fault_code &
  312. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  313. (vma->vm_flags & VM_WRITE) != 0) {
  314. insn = get_fault_insn(regs, 0);
  315. if (!insn)
  316. goto continue_fault;
  317. /* All loads, stores and atomics have bits 30 and 31 both set
  318. * in the instruction. Bit 21 is set in all stores, but we
  319. * have to avoid prefetches which also have bit 21 set.
  320. */
  321. if ((insn & 0xc0200000) == 0xc0200000 &&
  322. (insn & 0x01780000) != 0x01680000) {
  323. /* Don't bother updating thread struct value,
  324. * because update_mmu_cache only cares which tlb
  325. * the access came from.
  326. */
  327. fault_code |= FAULT_CODE_WRITE;
  328. }
  329. }
  330. continue_fault:
  331. if (vma->vm_start <= address)
  332. goto good_area;
  333. if (!(vma->vm_flags & VM_GROWSDOWN))
  334. goto bad_area;
  335. if (!(fault_code & FAULT_CODE_WRITE)) {
  336. /* Non-faulting loads shouldn't expand stack. */
  337. insn = get_fault_insn(regs, insn);
  338. if ((insn & 0xc0800000) == 0xc0800000) {
  339. unsigned char asi;
  340. if (insn & 0x2000)
  341. asi = (regs->tstate >> 24);
  342. else
  343. asi = (insn >> 5);
  344. if ((asi & 0xf2) == 0x82)
  345. goto bad_area;
  346. }
  347. }
  348. if (expand_stack(vma, address))
  349. goto bad_area;
  350. /*
  351. * Ok, we have a good vm_area for this memory access, so
  352. * we can handle it..
  353. */
  354. good_area:
  355. si_code = SEGV_ACCERR;
  356. /* If we took a ITLB miss on a non-executable page, catch
  357. * that here.
  358. */
  359. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  360. BUG_ON(address != regs->tpc);
  361. BUG_ON(regs->tstate & TSTATE_PRIV);
  362. goto bad_area;
  363. }
  364. if (fault_code & FAULT_CODE_WRITE) {
  365. if (!(vma->vm_flags & VM_WRITE))
  366. goto bad_area;
  367. /* Spitfire has an icache which does not snoop
  368. * processor stores. Later processors do...
  369. */
  370. if (tlb_type == spitfire &&
  371. (vma->vm_flags & VM_EXEC) != 0 &&
  372. vma->vm_file != NULL)
  373. set_thread_fault_code(fault_code |
  374. FAULT_CODE_BLKCOMMIT);
  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. flags |= ((fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
  381. fault = handle_mm_fault(mm, vma, address, flags);
  382. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  383. return;
  384. if (unlikely(fault & VM_FAULT_ERROR)) {
  385. if (fault & VM_FAULT_OOM)
  386. goto out_of_memory;
  387. else if (fault & VM_FAULT_SIGSEGV)
  388. goto bad_area;
  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. /* 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. #ifdef CONFIG_HUGETLB_PAGE
  415. mm_rss -= (mm->context.huge_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. #ifdef CONFIG_HUGETLB_PAGE
  421. mm_rss = mm->context.huge_pte_count;
  422. if (unlikely(mm_rss >
  423. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
  424. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  425. #endif
  426. return;
  427. /*
  428. * Something tried to access memory that isn't in our memory map..
  429. * Fix it, but check if it's kernel or user first..
  430. */
  431. bad_area:
  432. insn = get_fault_insn(regs, insn);
  433. up_read(&mm->mmap_sem);
  434. handle_kernel_fault:
  435. do_kernel_fault(regs, si_code, fault_code, insn, address);
  436. return;
  437. /*
  438. * We ran out of memory, or some other thing happened to us that made
  439. * us unable to handle the page fault gracefully.
  440. */
  441. out_of_memory:
  442. insn = get_fault_insn(regs, insn);
  443. up_read(&mm->mmap_sem);
  444. if (!(regs->tstate & TSTATE_PRIV)) {
  445. pagefault_out_of_memory();
  446. return;
  447. }
  448. goto handle_kernel_fault;
  449. intr_or_no_mm:
  450. insn = get_fault_insn(regs, 0);
  451. goto handle_kernel_fault;
  452. do_sigbus:
  453. insn = get_fault_insn(regs, insn);
  454. up_read(&mm->mmap_sem);
  455. /*
  456. * Send a sigbus, regardless of whether we were in kernel
  457. * or user mode.
  458. */
  459. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, address, insn, fault_code);
  460. /* Kernel mode? Handle exceptions or die */
  461. if (regs->tstate & TSTATE_PRIV)
  462. goto handle_kernel_fault;
  463. }