fault.c 13 KB

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