fault_64.c 13 KB

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