fault_64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. static inline __kprobes int notify_page_fault(struct pt_regs *regs)
  32. {
  33. int ret = 0;
  34. /* kprobe_running() needs smp_processor_id() */
  35. if (kprobes_built_in() && !user_mode(regs)) {
  36. preempt_disable();
  37. if (kprobe_running() && kprobe_fault_handler(regs, 0))
  38. ret = 1;
  39. preempt_enable();
  40. }
  41. return ret;
  42. }
  43. static void __kprobes unhandled_fault(unsigned long address,
  44. struct task_struct *tsk,
  45. struct pt_regs *regs)
  46. {
  47. if ((unsigned long) address < PAGE_SIZE) {
  48. printk(KERN_ALERT "Unable to handle kernel NULL "
  49. "pointer dereference\n");
  50. } else {
  51. printk(KERN_ALERT "Unable to handle kernel paging request "
  52. "at virtual address %016lx\n", (unsigned long)address);
  53. }
  54. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
  55. (tsk->mm ?
  56. CTX_HWBITS(tsk->mm->context) :
  57. CTX_HWBITS(tsk->active_mm->context)));
  58. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
  59. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  60. (unsigned long) tsk->active_mm->pgd));
  61. die_if_kernel("Oops", regs);
  62. }
  63. static void __kprobes bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
  64. {
  65. printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
  66. regs->tpc);
  67. printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
  68. printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
  69. printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
  70. dump_stack();
  71. unhandled_fault(regs->tpc, current, regs);
  72. }
  73. /*
  74. * We now make sure that mmap_sem is held in all paths that call
  75. * this. Additionally, to prevent kswapd from ripping ptes from
  76. * under us, raise interrupts around the time that we look at the
  77. * pte, kswapd will have to wait to get his smp ipi response from
  78. * us. vmtruncate likewise. This saves us having to get pte lock.
  79. */
  80. static unsigned int get_user_insn(unsigned long tpc)
  81. {
  82. pgd_t *pgdp = pgd_offset(current->mm, tpc);
  83. pud_t *pudp;
  84. pmd_t *pmdp;
  85. pte_t *ptep, pte;
  86. unsigned long pa;
  87. u32 insn = 0;
  88. unsigned long pstate;
  89. if (pgd_none(*pgdp))
  90. goto outret;
  91. pudp = pud_offset(pgdp, tpc);
  92. if (pud_none(*pudp))
  93. goto outret;
  94. pmdp = pmd_offset(pudp, tpc);
  95. if (pmd_none(*pmdp))
  96. goto outret;
  97. /* This disables preemption for us as well. */
  98. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  99. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  100. : : "r" (pstate), "i" (PSTATE_IE));
  101. ptep = pte_offset_map(pmdp, tpc);
  102. pte = *ptep;
  103. if (!pte_present(pte))
  104. goto out;
  105. pa = (pte_pfn(pte) << PAGE_SHIFT);
  106. pa += (tpc & ~PAGE_MASK);
  107. /* Use phys bypass so we don't pollute dtlb/dcache. */
  108. __asm__ __volatile__("lduwa [%1] %2, %0"
  109. : "=r" (insn)
  110. : "r" (pa), "i" (ASI_PHYS_USE_EC));
  111. out:
  112. pte_unmap(ptep);
  113. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  114. outret:
  115. return insn;
  116. }
  117. extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
  118. static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  119. unsigned int insn, int fault_code)
  120. {
  121. siginfo_t info;
  122. info.si_code = code;
  123. info.si_signo = sig;
  124. info.si_errno = 0;
  125. if (fault_code & FAULT_CODE_ITLB)
  126. info.si_addr = (void __user *) regs->tpc;
  127. else
  128. info.si_addr = (void __user *)
  129. compute_effective_address(regs, insn, 0);
  130. info.si_trapno = 0;
  131. force_sig_info(sig, &info, current);
  132. }
  133. extern int handle_ldf_stq(u32, struct pt_regs *);
  134. extern int handle_ld_nf(u32, struct pt_regs *);
  135. static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
  136. {
  137. if (!insn) {
  138. if (!regs->tpc || (regs->tpc & 0x3))
  139. return 0;
  140. if (regs->tstate & TSTATE_PRIV) {
  141. insn = *(unsigned int *) regs->tpc;
  142. } else {
  143. insn = get_user_insn(regs->tpc);
  144. }
  145. }
  146. return insn;
  147. }
  148. static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
  149. int fault_code, unsigned int insn,
  150. unsigned long address)
  151. {
  152. unsigned char asi = ASI_P;
  153. if ((!insn) && (regs->tstate & TSTATE_PRIV))
  154. goto cannot_handle;
  155. /* If user insn could be read (thus insn is zero), that
  156. * is fine. We will just gun down the process with a signal
  157. * in that case.
  158. */
  159. if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
  160. (insn & 0xc0800000) == 0xc0800000) {
  161. if (insn & 0x2000)
  162. asi = (regs->tstate >> 24);
  163. else
  164. asi = (insn >> 5);
  165. if ((asi & 0xf2) == 0x82) {
  166. if (insn & 0x1000000) {
  167. handle_ldf_stq(insn, regs);
  168. } else {
  169. /* This was a non-faulting load. Just clear the
  170. * destination register(s) and continue with the next
  171. * instruction. -jj
  172. */
  173. handle_ld_nf(insn, regs);
  174. }
  175. return;
  176. }
  177. }
  178. /* Is this in ex_table? */
  179. if (regs->tstate & TSTATE_PRIV) {
  180. const struct exception_table_entry *entry;
  181. entry = search_exception_tables(regs->tpc);
  182. if (entry) {
  183. regs->tpc = entry->fixup;
  184. regs->tnpc = regs->tpc + 4;
  185. return;
  186. }
  187. } else {
  188. /* The si_code was set to make clear whether
  189. * this was a SEGV_MAPERR or SEGV_ACCERR fault.
  190. */
  191. do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
  192. return;
  193. }
  194. cannot_handle:
  195. unhandled_fault (address, current, regs);
  196. }
  197. static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
  198. {
  199. static int times;
  200. if (times++ < 10)
  201. printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
  202. "64-bit TPC [%lx]\n",
  203. current->comm, current->pid,
  204. regs->tpc);
  205. show_regs(regs);
  206. }
  207. static void noinline __kprobes bogus_32bit_fault_address(struct pt_regs *regs,
  208. unsigned long addr)
  209. {
  210. static int times;
  211. if (times++ < 10)
  212. printk(KERN_ERR "FAULT[%s:%d]: 32-bit process "
  213. "reports 64-bit fault address [%lx]\n",
  214. current->comm, current->pid, addr);
  215. show_regs(regs);
  216. }
  217. asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
  218. {
  219. struct mm_struct *mm = current->mm;
  220. struct vm_area_struct *vma;
  221. unsigned int insn = 0;
  222. int si_code, fault_code, fault;
  223. unsigned long address, mm_rss;
  224. fault_code = get_thread_fault_code();
  225. if (notify_page_fault(regs))
  226. return;
  227. si_code = SEGV_MAPERR;
  228. address = current_thread_info()->fault_address;
  229. if ((fault_code & FAULT_CODE_ITLB) &&
  230. (fault_code & FAULT_CODE_DTLB))
  231. BUG();
  232. if (test_thread_flag(TIF_32BIT)) {
  233. if (!(regs->tstate & TSTATE_PRIV)) {
  234. if (unlikely((regs->tpc >> 32) != 0)) {
  235. bogus_32bit_fault_tpc(regs);
  236. goto intr_or_no_mm;
  237. }
  238. }
  239. if (unlikely((address >> 32) != 0)) {
  240. bogus_32bit_fault_address(regs, address);
  241. goto intr_or_no_mm;
  242. }
  243. }
  244. if (regs->tstate & TSTATE_PRIV) {
  245. unsigned long tpc = regs->tpc;
  246. /* Sanity check the PC. */
  247. if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
  248. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  249. /* Valid, no problems... */
  250. } else {
  251. bad_kernel_pc(regs, address);
  252. return;
  253. }
  254. }
  255. /*
  256. * If we're in an interrupt or have no user
  257. * context, we must not take the fault..
  258. */
  259. if (in_atomic() || !mm)
  260. goto intr_or_no_mm;
  261. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  262. if (!down_read_trylock(&mm->mmap_sem)) {
  263. if ((regs->tstate & TSTATE_PRIV) &&
  264. !search_exception_tables(regs->tpc)) {
  265. insn = get_fault_insn(regs, insn);
  266. goto handle_kernel_fault;
  267. }
  268. down_read(&mm->mmap_sem);
  269. }
  270. vma = find_vma(mm, address);
  271. if (!vma)
  272. goto bad_area;
  273. /* Pure DTLB misses do not tell us whether the fault causing
  274. * load/store/atomic was a write or not, it only says that there
  275. * was no match. So in such a case we (carefully) read the
  276. * instruction to try and figure this out. It's an optimization
  277. * so it's ok if we can't do this.
  278. *
  279. * Special hack, window spill/fill knows the exact fault type.
  280. */
  281. if (((fault_code &
  282. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  283. (vma->vm_flags & VM_WRITE) != 0) {
  284. insn = get_fault_insn(regs, 0);
  285. if (!insn)
  286. goto continue_fault;
  287. /* All loads, stores and atomics have bits 30 and 31 both set
  288. * in the instruction. Bit 21 is set in all stores, but we
  289. * have to avoid prefetches which also have bit 21 set.
  290. */
  291. if ((insn & 0xc0200000) == 0xc0200000 &&
  292. (insn & 0x01780000) != 0x01680000) {
  293. /* Don't bother updating thread struct value,
  294. * because update_mmu_cache only cares which tlb
  295. * the access came from.
  296. */
  297. fault_code |= FAULT_CODE_WRITE;
  298. }
  299. }
  300. continue_fault:
  301. if (vma->vm_start <= address)
  302. goto good_area;
  303. if (!(vma->vm_flags & VM_GROWSDOWN))
  304. goto bad_area;
  305. if (!(fault_code & FAULT_CODE_WRITE)) {
  306. /* Non-faulting loads shouldn't expand stack. */
  307. insn = get_fault_insn(regs, insn);
  308. if ((insn & 0xc0800000) == 0xc0800000) {
  309. unsigned char asi;
  310. if (insn & 0x2000)
  311. asi = (regs->tstate >> 24);
  312. else
  313. asi = (insn >> 5);
  314. if ((asi & 0xf2) == 0x82)
  315. goto bad_area;
  316. }
  317. }
  318. if (expand_stack(vma, address))
  319. goto bad_area;
  320. /*
  321. * Ok, we have a good vm_area for this memory access, so
  322. * we can handle it..
  323. */
  324. good_area:
  325. si_code = SEGV_ACCERR;
  326. /* If we took a ITLB miss on a non-executable page, catch
  327. * that here.
  328. */
  329. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  330. BUG_ON(address != regs->tpc);
  331. BUG_ON(regs->tstate & TSTATE_PRIV);
  332. goto bad_area;
  333. }
  334. if (fault_code & FAULT_CODE_WRITE) {
  335. if (!(vma->vm_flags & VM_WRITE))
  336. goto bad_area;
  337. /* Spitfire has an icache which does not snoop
  338. * processor stores. Later processors do...
  339. */
  340. if (tlb_type == spitfire &&
  341. (vma->vm_flags & VM_EXEC) != 0 &&
  342. vma->vm_file != NULL)
  343. set_thread_fault_code(fault_code |
  344. FAULT_CODE_BLKCOMMIT);
  345. } else {
  346. /* Allow reads even for write-only mappings */
  347. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  348. goto bad_area;
  349. }
  350. fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
  351. if (unlikely(fault & VM_FAULT_ERROR)) {
  352. if (fault & VM_FAULT_OOM)
  353. goto out_of_memory;
  354. else if (fault & VM_FAULT_SIGBUS)
  355. goto do_sigbus;
  356. BUG();
  357. }
  358. if (fault & VM_FAULT_MAJOR) {
  359. current->maj_flt++;
  360. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  361. regs, address);
  362. } else {
  363. current->min_flt++;
  364. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  365. regs, address);
  366. }
  367. up_read(&mm->mmap_sem);
  368. mm_rss = get_mm_rss(mm);
  369. #ifdef CONFIG_HUGETLB_PAGE
  370. mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  371. #endif
  372. if (unlikely(mm_rss >
  373. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  374. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  375. #ifdef CONFIG_HUGETLB_PAGE
  376. mm_rss = mm->context.huge_pte_count;
  377. if (unlikely(mm_rss >
  378. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
  379. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  380. #endif
  381. return;
  382. /*
  383. * Something tried to access memory that isn't in our memory map..
  384. * Fix it, but check if it's kernel or user first..
  385. */
  386. bad_area:
  387. insn = get_fault_insn(regs, insn);
  388. up_read(&mm->mmap_sem);
  389. handle_kernel_fault:
  390. do_kernel_fault(regs, si_code, fault_code, insn, address);
  391. return;
  392. /*
  393. * We ran out of memory, or some other thing happened to us that made
  394. * us unable to handle the page fault gracefully.
  395. */
  396. out_of_memory:
  397. insn = get_fault_insn(regs, insn);
  398. up_read(&mm->mmap_sem);
  399. if (!(regs->tstate & TSTATE_PRIV)) {
  400. pagefault_out_of_memory();
  401. return;
  402. }
  403. goto handle_kernel_fault;
  404. intr_or_no_mm:
  405. insn = get_fault_insn(regs, 0);
  406. goto handle_kernel_fault;
  407. do_sigbus:
  408. insn = get_fault_insn(regs, insn);
  409. up_read(&mm->mmap_sem);
  410. /*
  411. * Send a sigbus, regardless of whether we were in kernel
  412. * or user mode.
  413. */
  414. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
  415. /* Kernel mode? Handle exceptions or die */
  416. if (regs->tstate & TSTATE_PRIV)
  417. goto handle_kernel_fault;
  418. }