fault.c 13 KB

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