fault.c 13 KB

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