fault_32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/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. int show_unhandled_signals = 1;
  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. static inline void
  130. show_signal_msg(struct pt_regs *regs, int sig, int code,
  131. unsigned long address, struct task_struct *tsk)
  132. {
  133. if (!unhandled_signal(tsk, sig))
  134. return;
  135. if (!printk_ratelimit())
  136. return;
  137. printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
  138. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  139. tsk->comm, task_pid_nr(tsk), address,
  140. (void *)regs->pc, (void *)regs->u_regs[UREG_I7],
  141. (void *)regs->u_regs[UREG_FP], code);
  142. print_vma_addr(KERN_CONT " in ", regs->pc);
  143. printk(KERN_CONT "\n");
  144. }
  145. static void __do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  146. unsigned long addr)
  147. {
  148. siginfo_t info;
  149. info.si_signo = sig;
  150. info.si_code = code;
  151. info.si_errno = 0;
  152. info.si_addr = (void __user *) addr;
  153. info.si_trapno = 0;
  154. if (unlikely(show_unhandled_signals))
  155. show_signal_msg(regs, sig, info.si_code,
  156. addr, current);
  157. force_sig_info (sig, &info, current);
  158. }
  159. extern unsigned long safe_compute_effective_address(struct pt_regs *,
  160. unsigned int);
  161. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  162. {
  163. unsigned int insn;
  164. if (text_fault)
  165. return regs->pc;
  166. if (regs->psr & PSR_PS) {
  167. insn = *(unsigned int *) regs->pc;
  168. } else {
  169. __get_user(insn, (unsigned int *) regs->pc);
  170. }
  171. return safe_compute_effective_address(regs, insn);
  172. }
  173. static noinline void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  174. int text_fault)
  175. {
  176. unsigned long addr = compute_si_addr(regs, text_fault);
  177. __do_fault_siginfo(code, sig, regs, addr);
  178. }
  179. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  180. unsigned long address)
  181. {
  182. struct vm_area_struct *vma;
  183. struct task_struct *tsk = current;
  184. struct mm_struct *mm = tsk->mm;
  185. unsigned int fixup;
  186. unsigned long g2;
  187. int from_user = !(regs->psr & PSR_PS);
  188. int fault, code;
  189. if(text_fault)
  190. address = regs->pc;
  191. /*
  192. * We fault-in kernel-space virtual memory on-demand. The
  193. * 'reference' page table is init_mm.pgd.
  194. *
  195. * NOTE! We MUST NOT take any locks for this case. We may
  196. * be in an interrupt or a critical region, and should
  197. * only copy the information from the master page table,
  198. * nothing more.
  199. */
  200. code = SEGV_MAPERR;
  201. if (!ARCH_SUN4C && address >= TASK_SIZE)
  202. goto vmalloc_fault;
  203. /*
  204. * If we're in an interrupt or have no user
  205. * context, we must not take the fault..
  206. */
  207. if (in_atomic() || !mm)
  208. goto no_context;
  209. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  210. down_read(&mm->mmap_sem);
  211. /*
  212. * The kernel referencing a bad kernel pointer can lock up
  213. * a sun4c machine completely, so we must attempt recovery.
  214. */
  215. if(!from_user && address >= PAGE_OFFSET)
  216. goto bad_area;
  217. vma = find_vma(mm, address);
  218. if(!vma)
  219. goto bad_area;
  220. if(vma->vm_start <= address)
  221. goto good_area;
  222. if(!(vma->vm_flags & VM_GROWSDOWN))
  223. goto bad_area;
  224. if(expand_stack(vma, address))
  225. goto bad_area;
  226. /*
  227. * Ok, we have a good vm_area for this memory access, so
  228. * we can handle it..
  229. */
  230. good_area:
  231. code = SEGV_ACCERR;
  232. if(write) {
  233. if(!(vma->vm_flags & VM_WRITE))
  234. goto bad_area;
  235. } else {
  236. /* Allow reads even for write-only mappings */
  237. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  238. goto bad_area;
  239. }
  240. /*
  241. * If for any reason at all we couldn't handle the fault,
  242. * make sure we exit gracefully rather than endlessly redo
  243. * the fault.
  244. */
  245. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  246. if (unlikely(fault & VM_FAULT_ERROR)) {
  247. if (fault & VM_FAULT_OOM)
  248. goto out_of_memory;
  249. else if (fault & VM_FAULT_SIGBUS)
  250. goto do_sigbus;
  251. BUG();
  252. }
  253. if (fault & VM_FAULT_MAJOR) {
  254. current->maj_flt++;
  255. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
  256. } else {
  257. current->min_flt++;
  258. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
  259. }
  260. up_read(&mm->mmap_sem);
  261. return;
  262. /*
  263. * Something tried to access memory that isn't in our memory map..
  264. * Fix it, but check if it's kernel or user first..
  265. */
  266. bad_area:
  267. up_read(&mm->mmap_sem);
  268. bad_area_nosemaphore:
  269. /* User mode accesses just cause a SIGSEGV */
  270. if (from_user) {
  271. do_fault_siginfo(code, SIGSEGV, regs, text_fault);
  272. return;
  273. }
  274. /* Is this in ex_table? */
  275. no_context:
  276. g2 = regs->u_regs[UREG_G2];
  277. if (!from_user) {
  278. fixup = search_extables_range(regs->pc, &g2);
  279. if (fixup > 10) { /* Values below are reserved for other things */
  280. extern const unsigned __memset_start[];
  281. extern const unsigned __memset_end[];
  282. extern const unsigned __csum_partial_copy_start[];
  283. extern const unsigned __csum_partial_copy_end[];
  284. #ifdef DEBUG_EXCEPTIONS
  285. printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
  286. printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
  287. regs->pc, fixup, g2);
  288. #endif
  289. if ((regs->pc >= (unsigned long)__memset_start &&
  290. regs->pc < (unsigned long)__memset_end) ||
  291. (regs->pc >= (unsigned long)__csum_partial_copy_start &&
  292. regs->pc < (unsigned long)__csum_partial_copy_end)) {
  293. regs->u_regs[UREG_I4] = address;
  294. regs->u_regs[UREG_I5] = regs->pc;
  295. }
  296. regs->u_regs[UREG_G2] = g2;
  297. regs->pc = fixup;
  298. regs->npc = regs->pc + 4;
  299. return;
  300. }
  301. }
  302. unhandled_fault (address, tsk, regs);
  303. do_exit(SIGKILL);
  304. /*
  305. * We ran out of memory, or some other thing happened to us that made
  306. * us unable to handle the page fault gracefully.
  307. */
  308. out_of_memory:
  309. up_read(&mm->mmap_sem);
  310. if (from_user) {
  311. pagefault_out_of_memory();
  312. return;
  313. }
  314. goto no_context;
  315. do_sigbus:
  316. up_read(&mm->mmap_sem);
  317. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, text_fault);
  318. if (!from_user)
  319. goto no_context;
  320. vmalloc_fault:
  321. {
  322. /*
  323. * Synchronize this task's top level page-table
  324. * with the 'reference' page table.
  325. */
  326. int offset = pgd_index(address);
  327. pgd_t *pgd, *pgd_k;
  328. pmd_t *pmd, *pmd_k;
  329. pgd = tsk->active_mm->pgd + offset;
  330. pgd_k = init_mm.pgd + offset;
  331. if (!pgd_present(*pgd)) {
  332. if (!pgd_present(*pgd_k))
  333. goto bad_area_nosemaphore;
  334. pgd_val(*pgd) = pgd_val(*pgd_k);
  335. return;
  336. }
  337. pmd = pmd_offset(pgd, address);
  338. pmd_k = pmd_offset(pgd_k, address);
  339. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  340. goto bad_area_nosemaphore;
  341. *pmd = *pmd_k;
  342. return;
  343. }
  344. }
  345. asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
  346. unsigned long address)
  347. {
  348. extern void sun4c_update_mmu_cache(struct vm_area_struct *,
  349. unsigned long,pte_t *);
  350. extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
  351. struct task_struct *tsk = current;
  352. struct mm_struct *mm = tsk->mm;
  353. pgd_t *pgdp;
  354. pte_t *ptep;
  355. if (text_fault) {
  356. address = regs->pc;
  357. } else if (!write &&
  358. !(regs->psr & PSR_PS)) {
  359. unsigned int insn, __user *ip;
  360. ip = (unsigned int __user *)regs->pc;
  361. if (!get_user(insn, ip)) {
  362. if ((insn & 0xc1680000) == 0xc0680000)
  363. write = 1;
  364. }
  365. }
  366. if (!mm) {
  367. /* We are oopsing. */
  368. do_sparc_fault(regs, text_fault, write, address);
  369. BUG(); /* P3 Oops already, you bitch */
  370. }
  371. pgdp = pgd_offset(mm, address);
  372. ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
  373. if (pgd_val(*pgdp)) {
  374. if (write) {
  375. if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
  376. == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
  377. unsigned long flags;
  378. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  379. _SUN4C_PAGE_MODIFIED |
  380. _SUN4C_PAGE_VALID |
  381. _SUN4C_PAGE_DIRTY);
  382. local_irq_save(flags);
  383. if (sun4c_get_segmap(address) != invalid_segment) {
  384. sun4c_put_pte(address, pte_val(*ptep));
  385. local_irq_restore(flags);
  386. return;
  387. }
  388. local_irq_restore(flags);
  389. }
  390. } else {
  391. if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
  392. == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
  393. unsigned long flags;
  394. *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
  395. _SUN4C_PAGE_VALID);
  396. local_irq_save(flags);
  397. if (sun4c_get_segmap(address) != invalid_segment) {
  398. sun4c_put_pte(address, pte_val(*ptep));
  399. local_irq_restore(flags);
  400. return;
  401. }
  402. local_irq_restore(flags);
  403. }
  404. }
  405. }
  406. /* This conditional is 'interesting'. */
  407. if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
  408. && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
  409. /* Note: It is safe to not grab the MMAP semaphore here because
  410. * we know that update_mmu_cache() will not sleep for
  411. * any reason (at least not in the current implementation)
  412. * and therefore there is no danger of another thread getting
  413. * on the CPU and doing a shrink_mmap() on this vma.
  414. */
  415. sun4c_update_mmu_cache (find_vma(current->mm, address), address,
  416. ptep);
  417. else
  418. do_sparc_fault(regs, text_fault, write, address);
  419. }
  420. /* This always deals with user addresses. */
  421. static void force_user_fault(unsigned long address, int write)
  422. {
  423. struct vm_area_struct *vma;
  424. struct task_struct *tsk = current;
  425. struct mm_struct *mm = tsk->mm;
  426. int code;
  427. code = SEGV_MAPERR;
  428. down_read(&mm->mmap_sem);
  429. vma = find_vma(mm, address);
  430. if(!vma)
  431. goto bad_area;
  432. if(vma->vm_start <= address)
  433. goto good_area;
  434. if(!(vma->vm_flags & VM_GROWSDOWN))
  435. goto bad_area;
  436. if(expand_stack(vma, address))
  437. goto bad_area;
  438. good_area:
  439. code = SEGV_ACCERR;
  440. if(write) {
  441. if(!(vma->vm_flags & VM_WRITE))
  442. goto bad_area;
  443. } else {
  444. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  445. goto bad_area;
  446. }
  447. switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
  448. case VM_FAULT_SIGBUS:
  449. case VM_FAULT_OOM:
  450. goto do_sigbus;
  451. }
  452. up_read(&mm->mmap_sem);
  453. return;
  454. bad_area:
  455. up_read(&mm->mmap_sem);
  456. __do_fault_siginfo(code, SIGSEGV, tsk->thread.kregs, address);
  457. return;
  458. do_sigbus:
  459. up_read(&mm->mmap_sem);
  460. __do_fault_siginfo(BUS_ADRERR, SIGBUS, tsk->thread.kregs, address);
  461. }
  462. static void check_stack_aligned(unsigned long sp)
  463. {
  464. if (sp & 0x7UL)
  465. force_sig(SIGILL, current);
  466. }
  467. void window_overflow_fault(void)
  468. {
  469. unsigned long sp;
  470. sp = current_thread_info()->rwbuf_stkptrs[0];
  471. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  472. force_user_fault(sp + 0x38, 1);
  473. force_user_fault(sp, 1);
  474. check_stack_aligned(sp);
  475. }
  476. void window_underflow_fault(unsigned long sp)
  477. {
  478. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  479. force_user_fault(sp + 0x38, 0);
  480. force_user_fault(sp, 0);
  481. check_stack_aligned(sp);
  482. }
  483. void window_ret_fault(struct pt_regs *regs)
  484. {
  485. unsigned long sp;
  486. sp = regs->u_regs[UREG_FP];
  487. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  488. force_user_fault(sp + 0x38, 0);
  489. force_user_fault(sp, 0);
  490. check_stack_aligned(sp);
  491. }