fault.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
  220. if (insn & 0x2000)
  221. asi = (regs->tstate >> 24);
  222. else
  223. asi = (insn >> 5);
  224. }
  225. /* Look in asi.h: All _S asis have LS bit set */
  226. if ((asi & 0x1) &&
  227. (entry = search_exception_tables(regs->tpc))) {
  228. regs->tpc = entry->fixup;
  229. regs->tnpc = regs->tpc + 4;
  230. return;
  231. }
  232. } else {
  233. /* The si_code was set to make clear whether
  234. * this was a SEGV_MAPERR or SEGV_ACCERR fault.
  235. */
  236. do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
  237. return;
  238. }
  239. cannot_handle:
  240. unhandled_fault (address, current, regs);
  241. }
  242. asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
  243. {
  244. struct mm_struct *mm = current->mm;
  245. struct vm_area_struct *vma;
  246. unsigned int insn = 0;
  247. int si_code, fault_code, fault;
  248. unsigned long address, mm_rss;
  249. fault_code = get_thread_fault_code();
  250. if (notify_page_fault(regs))
  251. return;
  252. si_code = SEGV_MAPERR;
  253. address = current_thread_info()->fault_address;
  254. if ((fault_code & FAULT_CODE_ITLB) &&
  255. (fault_code & FAULT_CODE_DTLB))
  256. BUG();
  257. if (regs->tstate & TSTATE_PRIV) {
  258. unsigned long tpc = regs->tpc;
  259. /* Sanity check the PC. */
  260. if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
  261. (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
  262. /* Valid, no problems... */
  263. } else {
  264. bad_kernel_pc(regs, address);
  265. return;
  266. }
  267. }
  268. /*
  269. * If we're in an interrupt or have no user
  270. * context, we must not take the fault..
  271. */
  272. if (in_atomic() || !mm)
  273. goto intr_or_no_mm;
  274. if (test_thread_flag(TIF_32BIT)) {
  275. if (!(regs->tstate & TSTATE_PRIV))
  276. regs->tpc &= 0xffffffff;
  277. address &= 0xffffffff;
  278. }
  279. if (!down_read_trylock(&mm->mmap_sem)) {
  280. if ((regs->tstate & TSTATE_PRIV) &&
  281. !search_exception_tables(regs->tpc)) {
  282. insn = get_fault_insn(regs, insn);
  283. goto handle_kernel_fault;
  284. }
  285. down_read(&mm->mmap_sem);
  286. }
  287. vma = find_vma(mm, address);
  288. if (!vma)
  289. goto bad_area;
  290. /* Pure DTLB misses do not tell us whether the fault causing
  291. * load/store/atomic was a write or not, it only says that there
  292. * was no match. So in such a case we (carefully) read the
  293. * instruction to try and figure this out. It's an optimization
  294. * so it's ok if we can't do this.
  295. *
  296. * Special hack, window spill/fill knows the exact fault type.
  297. */
  298. if (((fault_code &
  299. (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
  300. (vma->vm_flags & VM_WRITE) != 0) {
  301. insn = get_fault_insn(regs, 0);
  302. if (!insn)
  303. goto continue_fault;
  304. /* All loads, stores and atomics have bits 30 and 31 both set
  305. * in the instruction. Bit 21 is set in all stores, but we
  306. * have to avoid prefetches which also have bit 21 set.
  307. */
  308. if ((insn & 0xc0200000) == 0xc0200000 &&
  309. (insn & 0x01780000) != 0x01680000) {
  310. /* Don't bother updating thread struct value,
  311. * because update_mmu_cache only cares which tlb
  312. * the access came from.
  313. */
  314. fault_code |= FAULT_CODE_WRITE;
  315. }
  316. }
  317. continue_fault:
  318. if (vma->vm_start <= address)
  319. goto good_area;
  320. if (!(vma->vm_flags & VM_GROWSDOWN))
  321. goto bad_area;
  322. if (!(fault_code & FAULT_CODE_WRITE)) {
  323. /* Non-faulting loads shouldn't expand stack. */
  324. insn = get_fault_insn(regs, insn);
  325. if ((insn & 0xc0800000) == 0xc0800000) {
  326. unsigned char asi;
  327. if (insn & 0x2000)
  328. asi = (regs->tstate >> 24);
  329. else
  330. asi = (insn >> 5);
  331. if ((asi & 0xf2) == 0x82)
  332. goto bad_area;
  333. }
  334. }
  335. if (expand_stack(vma, address))
  336. goto bad_area;
  337. /*
  338. * Ok, we have a good vm_area for this memory access, so
  339. * we can handle it..
  340. */
  341. good_area:
  342. si_code = SEGV_ACCERR;
  343. /* If we took a ITLB miss on a non-executable page, catch
  344. * that here.
  345. */
  346. if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
  347. BUG_ON(address != regs->tpc);
  348. BUG_ON(regs->tstate & TSTATE_PRIV);
  349. goto bad_area;
  350. }
  351. if (fault_code & FAULT_CODE_WRITE) {
  352. if (!(vma->vm_flags & VM_WRITE))
  353. goto bad_area;
  354. /* Spitfire has an icache which does not snoop
  355. * processor stores. Later processors do...
  356. */
  357. if (tlb_type == spitfire &&
  358. (vma->vm_flags & VM_EXEC) != 0 &&
  359. vma->vm_file != NULL)
  360. set_thread_fault_code(fault_code |
  361. FAULT_CODE_BLKCOMMIT);
  362. } else {
  363. /* Allow reads even for write-only mappings */
  364. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  365. goto bad_area;
  366. }
  367. fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
  368. if (unlikely(fault & VM_FAULT_ERROR)) {
  369. if (fault & VM_FAULT_OOM)
  370. goto out_of_memory;
  371. else if (fault & VM_FAULT_SIGBUS)
  372. goto do_sigbus;
  373. BUG();
  374. }
  375. if (fault & VM_FAULT_MAJOR)
  376. current->maj_flt++;
  377. else
  378. current->min_flt++;
  379. up_read(&mm->mmap_sem);
  380. mm_rss = get_mm_rss(mm);
  381. #ifdef CONFIG_HUGETLB_PAGE
  382. mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
  383. #endif
  384. if (unlikely(mm_rss >
  385. mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
  386. tsb_grow(mm, MM_TSB_BASE, mm_rss);
  387. #ifdef CONFIG_HUGETLB_PAGE
  388. mm_rss = mm->context.huge_pte_count;
  389. if (unlikely(mm_rss >
  390. mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
  391. tsb_grow(mm, MM_TSB_HUGE, mm_rss);
  392. #endif
  393. return;
  394. /*
  395. * Something tried to access memory that isn't in our memory map..
  396. * Fix it, but check if it's kernel or user first..
  397. */
  398. bad_area:
  399. insn = get_fault_insn(regs, insn);
  400. up_read(&mm->mmap_sem);
  401. handle_kernel_fault:
  402. do_kernel_fault(regs, si_code, fault_code, insn, address);
  403. return;
  404. /*
  405. * We ran out of memory, or some other thing happened to us that made
  406. * us unable to handle the page fault gracefully.
  407. */
  408. out_of_memory:
  409. insn = get_fault_insn(regs, insn);
  410. up_read(&mm->mmap_sem);
  411. printk("VM: killing process %s\n", current->comm);
  412. if (!(regs->tstate & TSTATE_PRIV))
  413. do_group_exit(SIGKILL);
  414. goto handle_kernel_fault;
  415. intr_or_no_mm:
  416. insn = get_fault_insn(regs, 0);
  417. goto handle_kernel_fault;
  418. do_sigbus:
  419. insn = get_fault_insn(regs, insn);
  420. up_read(&mm->mmap_sem);
  421. /*
  422. * Send a sigbus, regardless of whether we were in kernel
  423. * or user mode.
  424. */
  425. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
  426. /* Kernel mode? Handle exceptions or die */
  427. if (regs->tstate & TSTATE_PRIV)
  428. goto handle_kernel_fault;
  429. }