fault.c 12 KB

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