fault_64.c 14 KB

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