fault_32.c 14 KB

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