fault.c 14 KB

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