fault.c 12 KB

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