fault.c 13 KB

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