fault.c 14 KB

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