fault.c 11 KB

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