fault.c 13 KB

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