fault.c 13 KB

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