fault_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. unsigned long pstate;
  90. if (pgd_none(*pgdp))
  91. goto outret;
  92. pudp = pud_offset(pgdp, tpc);
  93. if (pud_none(*pudp))
  94. goto outret;
  95. pmdp = pmd_offset(pudp, tpc);
  96. if (pmd_none(*pmdp))
  97. goto outret;
  98. /* This disables preemption for us as well. */
  99. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  100. __asm__ __volatile__("wrpr %0, %1, %%pstate"
  101. : : "r" (pstate), "i" (PSTATE_IE));
  102. ptep = pte_offset_map(pmdp, tpc);
  103. pte = *ptep;
  104. if (!pte_present(pte))
  105. goto out;
  106. pa = (pte_pfn(pte) << PAGE_SHIFT);
  107. pa += (tpc & ~PAGE_MASK);
  108. /* Use phys bypass so we don't pollute dtlb/dcache. */
  109. __asm__ __volatile__("lduwa [%1] %2, %0"
  110. : "=r" (insn)
  111. : "r" (pa), "i" (ASI_PHYS_USE_EC));
  112. out:
  113. pte_unmap(ptep);
  114. __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
  115. outret:
  116. return insn;
  117. }
  118. static inline void
  119. show_signal_msg(struct pt_regs *regs, int sig, int code,
  120. unsigned long address, struct task_struct *tsk)
  121. {
  122. if (!unhandled_signal(tsk, sig))
  123. return;
  124. if (!printk_ratelimit())
  125. return;
  126. printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
  127. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  128. tsk->comm, task_pid_nr(tsk), address,
  129. (void *)regs->tpc, (void *)regs->u_regs[UREG_I7],
  130. (void *)regs->u_regs[UREG_FP], code);
  131. print_vma_addr(KERN_CONT " in ", regs->tpc);
  132. printk(KERN_CONT "\n");
  133. }
  134. extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
  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. 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. fault_code = get_thread_fault_code();
  245. if (notify_page_fault(regs))
  246. return;
  247. si_code = SEGV_MAPERR;
  248. address = current_thread_info()->fault_address;
  249. if ((fault_code & FAULT_CODE_ITLB) &&
  250. (fault_code & FAULT_CODE_DTLB))
  251. BUG();
  252. if (test_thread_flag(TIF_32BIT)) {
  253. if (!(regs->tstate & TSTATE_PRIV)) {
  254. if (unlikely((regs->tpc >> 32) != 0)) {
  255. bogus_32bit_fault_tpc(regs);
  256. goto intr_or_no_mm;
  257. }
  258. }
  259. if (unlikely((address >> 32) != 0)) {
  260. bogus_32bit_fault_address(regs, address);
  261. goto intr_or_no_mm;
  262. }
  263. }
  264. if (regs->tstate & TSTATE_PRIV) {
  265. unsigned long tpc = regs->tpc;
  266. /* Sanity check the PC. */
  267. if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
  268. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  269. /* Valid, no problems... */
  270. } else {
  271. bad_kernel_pc(regs, address);
  272. return;
  273. }
  274. }
  275. /*
  276. * If we're in an interrupt or have no user
  277. * context, we must not take the fault..
  278. */
  279. if (in_atomic() || !mm)
  280. goto intr_or_no_mm;
  281. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  282. if (!down_read_trylock(&mm->mmap_sem)) {
  283. if ((regs->tstate & TSTATE_PRIV) &&
  284. !search_exception_tables(regs->tpc)) {
  285. insn = get_fault_insn(regs, insn);
  286. goto handle_kernel_fault;
  287. }
  288. down_read(&mm->mmap_sem);
  289. }
  290. vma = find_vma(mm, address);
  291. if (!vma)
  292. goto bad_area;
  293. /* Pure DTLB misses do not tell us whether the fault causing
  294. * load/store/atomic was a write or not, it only says that there
  295. * was no match. So in such a case we (carefully) read the
  296. * instruction to try and figure this out. It's an optimization
  297. * so it's ok if we can't do this.
  298. *
  299. * Special hack, window spill/fill knows the exact fault type.
  300. */
  301. if (((fault_code &
  302. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  303. (vma->vm_flags & VM_WRITE) != 0) {
  304. insn = get_fault_insn(regs, 0);
  305. if (!insn)
  306. goto continue_fault;
  307. /* All loads, stores and atomics have bits 30 and 31 both set
  308. * in the instruction. Bit 21 is set in all stores, but we
  309. * have to avoid prefetches which also have bit 21 set.
  310. */
  311. if ((insn & 0xc0200000) == 0xc0200000 &&
  312. (insn & 0x01780000) != 0x01680000) {
  313. /* Don't bother updating thread struct value,
  314. * because update_mmu_cache only cares which tlb
  315. * the access came from.
  316. */
  317. fault_code |= FAULT_CODE_WRITE;
  318. }
  319. }
  320. continue_fault:
  321. if (vma->vm_start <= address)
  322. goto good_area;
  323. if (!(vma->vm_flags & VM_GROWSDOWN))
  324. goto bad_area;
  325. if (!(fault_code & FAULT_CODE_WRITE)) {
  326. /* Non-faulting loads shouldn't expand stack. */
  327. insn = get_fault_insn(regs, insn);
  328. if ((insn & 0xc0800000) == 0xc0800000) {
  329. unsigned char asi;
  330. if (insn & 0x2000)
  331. asi = (regs->tstate >> 24);
  332. else
  333. asi = (insn >> 5);
  334. if ((asi & 0xf2) == 0x82)
  335. goto bad_area;
  336. }
  337. }
  338. if (expand_stack(vma, address))
  339. goto bad_area;
  340. /*
  341. * Ok, we have a good vm_area for this memory access, so
  342. * we can handle it..
  343. */
  344. good_area:
  345. si_code = SEGV_ACCERR;
  346. /* If we took a ITLB miss on a non-executable page, catch
  347. * that here.
  348. */
  349. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  350. BUG_ON(address != regs->tpc);
  351. BUG_ON(regs->tstate & TSTATE_PRIV);
  352. goto bad_area;
  353. }
  354. if (fault_code & FAULT_CODE_WRITE) {
  355. if (!(vma->vm_flags & VM_WRITE))
  356. goto bad_area;
  357. /* Spitfire has an icache which does not snoop
  358. * processor stores. Later processors do...
  359. */
  360. if (tlb_type == spitfire &&
  361. (vma->vm_flags & VM_EXEC) != 0 &&
  362. vma->vm_file != NULL)
  363. set_thread_fault_code(fault_code |
  364. FAULT_CODE_BLKCOMMIT);
  365. } else {
  366. /* Allow reads even for write-only mappings */
  367. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  368. goto bad_area;
  369. }
  370. fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
  371. if (unlikely(fault & VM_FAULT_ERROR)) {
  372. if (fault & VM_FAULT_OOM)
  373. goto out_of_memory;
  374. else if (fault & VM_FAULT_SIGBUS)
  375. goto do_sigbus;
  376. BUG();
  377. }
  378. if (fault & VM_FAULT_MAJOR) {
  379. current->maj_flt++;
  380. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  381. regs, address);
  382. } else {
  383. current->min_flt++;
  384. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  385. regs, address);
  386. }
  387. up_read(&mm->mmap_sem);
  388. mm_rss = get_mm_rss(mm);
  389. #ifdef CONFIG_HUGETLB_PAGE
  390. mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  391. #endif
  392. if (unlikely(mm_rss >
  393. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  394. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  395. #ifdef CONFIG_HUGETLB_PAGE
  396. mm_rss = mm->context.huge_pte_count;
  397. if (unlikely(mm_rss >
  398. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
  399. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  400. #endif
  401. return;
  402. /*
  403. * Something tried to access memory that isn't in our memory map..
  404. * Fix it, but check if it's kernel or user first..
  405. */
  406. bad_area:
  407. insn = get_fault_insn(regs, insn);
  408. up_read(&mm->mmap_sem);
  409. handle_kernel_fault:
  410. do_kernel_fault(regs, si_code, fault_code, insn, address);
  411. return;
  412. /*
  413. * We ran out of memory, or some other thing happened to us that made
  414. * us unable to handle the page fault gracefully.
  415. */
  416. out_of_memory:
  417. insn = get_fault_insn(regs, insn);
  418. up_read(&mm->mmap_sem);
  419. if (!(regs->tstate & TSTATE_PRIV)) {
  420. pagefault_out_of_memory();
  421. return;
  422. }
  423. goto handle_kernel_fault;
  424. intr_or_no_mm:
  425. insn = get_fault_insn(regs, 0);
  426. goto handle_kernel_fault;
  427. do_sigbus:
  428. insn = get_fault_insn(regs, insn);
  429. up_read(&mm->mmap_sem);
  430. /*
  431. * Send a sigbus, regardless of whether we were in kernel
  432. * or user mode.
  433. */
  434. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
  435. /* Kernel mode? Handle exceptions or die */
  436. if (regs->tstate & TSTATE_PRIV)
  437. goto handle_kernel_fault;
  438. }