fault_64.c 12 KB

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