fault_32.c 14 KB

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