fault_32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * fault.c: Page fault handlers for the Sparc.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  6. * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  7. */
  8. #include <asm/head.h>
  9. #include <linux/string.h>
  10. #include <linux/types.h>
  11. #include <linux/sched.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/mman.h>
  14. #include <linux/threads.h>
  15. #include <linux/kernel.h>
  16. #include <linux/signal.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/kdebug.h>
  23. #include <asm/system.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/memreg.h>
  27. #include <asm/openprom.h>
  28. #include <asm/oplib.h>
  29. #include <asm/smp.h>
  30. #include <asm/traps.h>
  31. #include <asm/uaccess.h>
  32. extern int prom_node_root;
  33. /* At boot time we determine these two values necessary for setting
  34. * up the segment maps and page table entries (pte's).
  35. */
  36. int num_segmaps, num_contexts;
  37. int invalid_segment;
  38. /* various Virtual Address Cache parameters we find at boot time... */
  39. int vac_size, vac_linesize, vac_do_hw_vac_flushes;
  40. int vac_entries_per_context, vac_entries_per_segment;
  41. int vac_entries_per_page;
  42. /* Return how much physical memory we have. */
  43. unsigned long probe_memory(void)
  44. {
  45. unsigned long total = 0;
  46. int i;
  47. for (i = 0; sp_banks[i].num_bytes; i++)
  48. total += sp_banks[i].num_bytes;
  49. return total;
  50. }
  51. extern void sun4c_complete_all_stores(void);
  52. /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
  53. asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
  54. unsigned long svaddr, unsigned long aerr,
  55. unsigned long avaddr)
  56. {
  57. sun4c_complete_all_stores();
  58. printk("FAULT: NMI received\n");
  59. printk("SREGS: Synchronous Error %08lx\n", serr);
  60. printk(" Synchronous Vaddr %08lx\n", svaddr);
  61. printk(" Asynchronous Error %08lx\n", aerr);
  62. printk(" Asynchronous Vaddr %08lx\n", avaddr);
  63. if (sun4c_memerr_reg)
  64. printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
  65. printk("REGISTER DUMP:\n");
  66. show_regs(regs);
  67. prom_halt();
  68. }
  69. static void unhandled_fault(unsigned long, struct task_struct *,
  70. struct pt_regs *) __attribute__ ((noreturn));
  71. static void unhandled_fault(unsigned long address, struct task_struct *tsk,
  72. struct pt_regs *regs)
  73. {
  74. if((unsigned long) address < PAGE_SIZE) {
  75. printk(KERN_ALERT
  76. "Unable to handle kernel NULL pointer dereference\n");
  77. } else {
  78. printk(KERN_ALERT "Unable to handle kernel paging request "
  79. "at virtual address %08lx\n", address);
  80. }
  81. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
  82. (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
  83. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
  84. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  85. (unsigned long) tsk->active_mm->pgd));
  86. die_if_kernel("Oops", regs);
  87. }
  88. asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
  89. unsigned long address)
  90. {
  91. struct pt_regs regs;
  92. unsigned long g2;
  93. unsigned int insn;
  94. int i;
  95. i = search_extables_range(ret_pc, &g2);
  96. switch (i) {
  97. case 3:
  98. /* load & store will be handled by fixup */
  99. return 3;
  100. case 1:
  101. /* store will be handled by fixup, load will bump out */
  102. /* for _to_ macros */
  103. insn = *((unsigned int *) pc);
  104. if ((insn >> 21) & 1)
  105. return 1;
  106. break;
  107. case 2:
  108. /* load will be handled by fixup, store will bump out */
  109. /* for _from_ macros */
  110. insn = *((unsigned int *) pc);
  111. if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
  112. return 2;
  113. break;
  114. default:
  115. break;
  116. };
  117. memset(&regs, 0, sizeof (regs));
  118. regs.pc = pc;
  119. regs.npc = pc + 4;
  120. __asm__ __volatile__(
  121. "rd %%psr, %0\n\t"
  122. "nop\n\t"
  123. "nop\n\t"
  124. "nop\n" : "=r" (regs.psr));
  125. unhandled_fault(address, current, &regs);
  126. /* Not reached */
  127. return 0;
  128. }
  129. extern unsigned long safe_compute_effective_address(struct pt_regs *,
  130. unsigned int);
  131. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  132. {
  133. unsigned int insn;
  134. if (text_fault)
  135. return regs->pc;
  136. if (regs->psr & PSR_PS) {
  137. insn = *(unsigned int *) regs->pc;
  138. } else {
  139. __get_user(insn, (unsigned int *) regs->pc);
  140. }
  141. return safe_compute_effective_address(regs, insn);
  142. }
  143. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  144. unsigned long address)
  145. {
  146. struct vm_area_struct *vma;
  147. struct task_struct *tsk = current;
  148. struct mm_struct *mm = tsk->mm;
  149. unsigned int fixup;
  150. unsigned long g2;
  151. siginfo_t info;
  152. int from_user = !(regs->psr & PSR_PS);
  153. int fault;
  154. if(text_fault)
  155. address = regs->pc;
  156. /*
  157. * We fault-in kernel-space virtual memory on-demand. The
  158. * 'reference' page table is init_mm.pgd.
  159. *
  160. * NOTE! We MUST NOT take any locks for this case. We may
  161. * be in an interrupt or a critical region, and should
  162. * only copy the information from the master page table,
  163. * nothing more.
  164. */
  165. if (!ARCH_SUN4C && address >= TASK_SIZE)
  166. goto vmalloc_fault;
  167. info.si_code = SEGV_MAPERR;
  168. /*
  169. * If we're in an interrupt or have no user
  170. * context, we must not take the fault..
  171. */
  172. if (in_atomic() || !mm)
  173. goto no_context;
  174. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  175. down_read(&mm->mmap_sem);
  176. /*
  177. * The kernel referencing a bad kernel pointer can lock up
  178. * a sun4c machine completely, so we must attempt recovery.
  179. */
  180. if(!from_user && address >= PAGE_OFFSET)
  181. goto bad_area;
  182. vma = find_vma(mm, address);
  183. if(!vma)
  184. goto bad_area;
  185. if(vma->vm_start <= address)
  186. goto good_area;
  187. if(!(vma->vm_flags & VM_GROWSDOWN))
  188. goto bad_area;
  189. if(expand_stack(vma, address))
  190. goto bad_area;
  191. /*
  192. * Ok, we have a good vm_area for this memory access, so
  193. * we can handle it..
  194. */
  195. good_area:
  196. info.si_code = SEGV_ACCERR;
  197. if(write) {
  198. if(!(vma->vm_flags & VM_WRITE))
  199. goto bad_area;
  200. } else {
  201. /* Allow reads even for write-only mappings */
  202. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  203. goto bad_area;
  204. }
  205. /*
  206. * If for any reason at all we couldn't handle the fault,
  207. * make sure we exit gracefully rather than endlessly redo
  208. * the fault.
  209. */
  210. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  211. if (unlikely(fault & VM_FAULT_ERROR)) {
  212. if (fault & VM_FAULT_OOM)
  213. goto out_of_memory;
  214. else if (fault & VM_FAULT_SIGBUS)
  215. goto do_sigbus;
  216. BUG();
  217. }
  218. if (fault & VM_FAULT_MAJOR) {
  219. current->maj_flt++;
  220. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  221. regs, address);
  222. } else {
  223. current->min_flt++;
  224. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  225. regs, address);
  226. }
  227. up_read(&mm->mmap_sem);
  228. return;
  229. /*
  230. * Something tried to access memory that isn't in our memory map..
  231. * Fix it, but check if it's kernel or user first..
  232. */
  233. bad_area:
  234. up_read(&mm->mmap_sem);
  235. bad_area_nosemaphore:
  236. /* User mode accesses just cause a SIGSEGV */
  237. if(from_user) {
  238. #if 0
  239. printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
  240. tsk->comm, tsk->pid, address, regs->pc);
  241. #endif
  242. info.si_signo = SIGSEGV;
  243. info.si_errno = 0;
  244. /* info.si_code set above to make clear whether
  245. this was a SEGV_MAPERR or SEGV_ACCERR fault. */
  246. info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
  247. info.si_trapno = 0;
  248. force_sig_info (SIGSEGV, &info, tsk);
  249. return;
  250. }
  251. /* Is this in ex_table? */
  252. no_context:
  253. g2 = regs->u_regs[UREG_G2];
  254. if (!from_user) {
  255. fixup = search_extables_range(regs->pc, &g2);
  256. if (fixup > 10) { /* Values below are reserved for other things */
  257. extern const unsigned __memset_start[];
  258. extern const unsigned __memset_end[];
  259. extern const unsigned __csum_partial_copy_start[];
  260. extern const unsigned __csum_partial_copy_end[];
  261. #ifdef DEBUG_EXCEPTIONS
  262. printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
  263. printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
  264. regs->pc, fixup, g2);
  265. #endif
  266. if ((regs->pc >= (unsigned long)__memset_start &&
  267. regs->pc < (unsigned long)__memset_end) ||
  268. (regs->pc >= (unsigned long)__csum_partial_copy_start &&
  269. regs->pc < (unsigned long)__csum_partial_copy_end)) {
  270. regs->u_regs[UREG_I4] = address;
  271. regs->u_regs[UREG_I5] = regs->pc;
  272. }
  273. regs->u_regs[UREG_G2] = g2;
  274. regs->pc = fixup;
  275. regs->npc = regs->pc + 4;
  276. return;
  277. }
  278. }
  279. unhandled_fault (address, tsk, regs);
  280. do_exit(SIGKILL);
  281. /*
  282. * We ran out of memory, or some other thing happened to us that made
  283. * us unable to handle the page fault gracefully.
  284. */
  285. out_of_memory:
  286. up_read(&mm->mmap_sem);
  287. if (from_user) {
  288. pagefault_out_of_memory();
  289. return;
  290. }
  291. goto no_context;
  292. do_sigbus:
  293. up_read(&mm->mmap_sem);
  294. info.si_signo = SIGBUS;
  295. info.si_errno = 0;
  296. info.si_code = BUS_ADRERR;
  297. info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
  298. info.si_trapno = 0;
  299. force_sig_info (SIGBUS, &info, tsk);
  300. if (!from_user)
  301. goto no_context;
  302. vmalloc_fault:
  303. {
  304. /*
  305. * Synchronize this task's top level page-table
  306. * with the 'reference' page table.
  307. */
  308. int offset = pgd_index(address);
  309. pgd_t *pgd, *pgd_k;
  310. pmd_t *pmd, *pmd_k;
  311. pgd = tsk->active_mm->pgd + offset;
  312. pgd_k = init_mm.pgd + offset;
  313. if (!pgd_present(*pgd)) {
  314. if (!pgd_present(*pgd_k))
  315. goto bad_area_nosemaphore;
  316. pgd_val(*pgd) = pgd_val(*pgd_k);
  317. return;
  318. }
  319. pmd = pmd_offset(pgd, address);
  320. pmd_k = pmd_offset(pgd_k, address);
  321. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  322. goto bad_area_nosemaphore;
  323. *pmd = *pmd_k;
  324. return;
  325. }
  326. }
  327. asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
  328. unsigned long address)
  329. {
  330. extern void sun4c_update_mmu_cache(struct vm_area_struct *,
  331. unsigned long,pte_t);
  332. extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
  333. struct task_struct *tsk = current;
  334. struct mm_struct *mm = tsk->mm;
  335. pgd_t *pgdp;
  336. pte_t *ptep;
  337. if (text_fault) {
  338. address = regs->pc;
  339. } else if (!write &&
  340. !(regs->psr & PSR_PS)) {
  341. unsigned int insn, __user *ip;
  342. ip = (unsigned int __user *)regs->pc;
  343. if (!get_user(insn, ip)) {
  344. if ((insn & 0xc1680000) == 0xc0680000)
  345. write = 1;
  346. }
  347. }
  348. if (!mm) {
  349. /* We are oopsing. */
  350. do_sparc_fault(regs, text_fault, write, address);
  351. BUG(); /* P3 Oops already, you bitch */
  352. }
  353. pgdp = pgd_offset(mm, address);
  354. ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
  355. if (pgd_val(*pgdp)) {
  356. if (write) {
  357. if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
  358. == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
  359. unsigned long flags;
  360. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  361. _SUN4C_PAGE_MODIFIED |
  362. _SUN4C_PAGE_VALID |
  363. _SUN4C_PAGE_DIRTY);
  364. local_irq_save(flags);
  365. if (sun4c_get_segmap(address) != invalid_segment) {
  366. sun4c_put_pte(address, pte_val(*ptep));
  367. local_irq_restore(flags);
  368. return;
  369. }
  370. local_irq_restore(flags);
  371. }
  372. } else {
  373. if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
  374. == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
  375. unsigned long flags;
  376. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  377. _SUN4C_PAGE_VALID);
  378. local_irq_save(flags);
  379. if (sun4c_get_segmap(address) != invalid_segment) {
  380. sun4c_put_pte(address, pte_val(*ptep));
  381. local_irq_restore(flags);
  382. return;
  383. }
  384. local_irq_restore(flags);
  385. }
  386. }
  387. }
  388. /* This conditional is 'interesting'. */
  389. if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
  390. && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
  391. /* Note: It is safe to not grab the MMAP semaphore here because
  392. * we know that update_mmu_cache() will not sleep for
  393. * any reason (at least not in the current implementation)
  394. * and therefore there is no danger of another thread getting
  395. * on the CPU and doing a shrink_mmap() on this vma.
  396. */
  397. sun4c_update_mmu_cache (find_vma(current->mm, address), address,
  398. *ptep);
  399. else
  400. do_sparc_fault(regs, text_fault, write, address);
  401. }
  402. /* This always deals with user addresses. */
  403. static void force_user_fault(unsigned long address, int write)
  404. {
  405. struct vm_area_struct *vma;
  406. struct task_struct *tsk = current;
  407. struct mm_struct *mm = tsk->mm;
  408. siginfo_t info;
  409. info.si_code = SEGV_MAPERR;
  410. #if 0
  411. printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
  412. tsk->pid, write, address);
  413. #endif
  414. down_read(&mm->mmap_sem);
  415. vma = find_vma(mm, address);
  416. if(!vma)
  417. goto bad_area;
  418. if(vma->vm_start <= address)
  419. goto good_area;
  420. if(!(vma->vm_flags & VM_GROWSDOWN))
  421. goto bad_area;
  422. if(expand_stack(vma, address))
  423. goto bad_area;
  424. good_area:
  425. info.si_code = SEGV_ACCERR;
  426. if(write) {
  427. if(!(vma->vm_flags & VM_WRITE))
  428. goto bad_area;
  429. } else {
  430. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  431. goto bad_area;
  432. }
  433. switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
  434. case VM_FAULT_SIGBUS:
  435. case VM_FAULT_OOM:
  436. goto do_sigbus;
  437. }
  438. up_read(&mm->mmap_sem);
  439. return;
  440. bad_area:
  441. up_read(&mm->mmap_sem);
  442. #if 0
  443. printk("Window whee %s [%d]: segfaults at %08lx\n",
  444. tsk->comm, tsk->pid, address);
  445. #endif
  446. info.si_signo = SIGSEGV;
  447. info.si_errno = 0;
  448. /* info.si_code set above to make clear whether
  449. this was a SEGV_MAPERR or SEGV_ACCERR fault. */
  450. info.si_addr = (void __user *) address;
  451. info.si_trapno = 0;
  452. force_sig_info (SIGSEGV, &info, tsk);
  453. return;
  454. do_sigbus:
  455. up_read(&mm->mmap_sem);
  456. info.si_signo = SIGBUS;
  457. info.si_errno = 0;
  458. info.si_code = BUS_ADRERR;
  459. info.si_addr = (void __user *) address;
  460. info.si_trapno = 0;
  461. force_sig_info (SIGBUS, &info, tsk);
  462. }
  463. void window_overflow_fault(void)
  464. {
  465. unsigned long sp;
  466. sp = current_thread_info()->rwbuf_stkptrs[0];
  467. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  468. force_user_fault(sp + 0x38, 1);
  469. force_user_fault(sp, 1);
  470. }
  471. void window_underflow_fault(unsigned long sp)
  472. {
  473. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  474. force_user_fault(sp + 0x38, 0);
  475. force_user_fault(sp, 0);
  476. }
  477. void window_ret_fault(struct pt_regs *regs)
  478. {
  479. unsigned long sp;
  480. sp = regs->u_regs[UREG_FP];
  481. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  482. force_user_fault(sp + 0x38, 0);
  483. force_user_fault(sp, 0);
  484. }