fault_64.c 12 KB

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