fault.c 13 KB

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