fault_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/page.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/openprom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/smp.h>
  27. #include <asm/traps.h>
  28. #include <asm/uaccess.h>
  29. extern int prom_node_root;
  30. int show_unhandled_signals = 1;
  31. /* At boot time we determine these two values necessary for setting
  32. * up the segment maps and page table entries (pte's).
  33. */
  34. int num_segmaps, num_contexts;
  35. int invalid_segment;
  36. /* various Virtual Address Cache parameters we find at boot time... */
  37. int vac_size, vac_linesize, vac_do_hw_vac_flushes;
  38. int vac_entries_per_context, vac_entries_per_segment;
  39. int vac_entries_per_page;
  40. /* Return how much physical memory we have. */
  41. unsigned long probe_memory(void)
  42. {
  43. unsigned long total = 0;
  44. int i;
  45. for (i = 0; sp_banks[i].num_bytes; i++)
  46. total += sp_banks[i].num_bytes;
  47. return total;
  48. }
  49. static void unhandled_fault(unsigned long, struct task_struct *,
  50. struct pt_regs *) __attribute__ ((noreturn));
  51. static void unhandled_fault(unsigned long address, struct task_struct *tsk,
  52. struct pt_regs *regs)
  53. {
  54. if((unsigned long) address < PAGE_SIZE) {
  55. printk(KERN_ALERT
  56. "Unable to handle kernel NULL pointer dereference\n");
  57. } else {
  58. printk(KERN_ALERT "Unable to handle kernel paging request "
  59. "at virtual address %08lx\n", address);
  60. }
  61. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
  62. (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
  63. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
  64. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  65. (unsigned long) tsk->active_mm->pgd));
  66. die_if_kernel("Oops", regs);
  67. }
  68. asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
  69. unsigned long address)
  70. {
  71. struct pt_regs regs;
  72. unsigned long g2;
  73. unsigned int insn;
  74. int i;
  75. i = search_extables_range(ret_pc, &g2);
  76. switch (i) {
  77. case 3:
  78. /* load & store will be handled by fixup */
  79. return 3;
  80. case 1:
  81. /* store will be handled by fixup, load will bump out */
  82. /* for _to_ macros */
  83. insn = *((unsigned int *) pc);
  84. if ((insn >> 21) & 1)
  85. return 1;
  86. break;
  87. case 2:
  88. /* load will be handled by fixup, store will bump out */
  89. /* for _from_ macros */
  90. insn = *((unsigned int *) pc);
  91. if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
  92. return 2;
  93. break;
  94. default:
  95. break;
  96. }
  97. memset(&regs, 0, sizeof (regs));
  98. regs.pc = pc;
  99. regs.npc = pc + 4;
  100. __asm__ __volatile__(
  101. "rd %%psr, %0\n\t"
  102. "nop\n\t"
  103. "nop\n\t"
  104. "nop\n" : "=r" (regs.psr));
  105. unhandled_fault(address, current, &regs);
  106. /* Not reached */
  107. return 0;
  108. }
  109. static inline void
  110. show_signal_msg(struct pt_regs *regs, int sig, int code,
  111. unsigned long address, struct task_struct *tsk)
  112. {
  113. if (!unhandled_signal(tsk, sig))
  114. return;
  115. if (!printk_ratelimit())
  116. return;
  117. printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
  118. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  119. tsk->comm, task_pid_nr(tsk), address,
  120. (void *)regs->pc, (void *)regs->u_regs[UREG_I7],
  121. (void *)regs->u_regs[UREG_FP], code);
  122. print_vma_addr(KERN_CONT " in ", regs->pc);
  123. printk(KERN_CONT "\n");
  124. }
  125. static void __do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  126. unsigned long addr)
  127. {
  128. siginfo_t info;
  129. info.si_signo = sig;
  130. info.si_code = code;
  131. info.si_errno = 0;
  132. info.si_addr = (void __user *) addr;
  133. info.si_trapno = 0;
  134. if (unlikely(show_unhandled_signals))
  135. show_signal_msg(regs, sig, info.si_code,
  136. addr, current);
  137. force_sig_info (sig, &info, current);
  138. }
  139. extern unsigned long safe_compute_effective_address(struct pt_regs *,
  140. unsigned int);
  141. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  142. {
  143. unsigned int insn;
  144. if (text_fault)
  145. return regs->pc;
  146. if (regs->psr & PSR_PS) {
  147. insn = *(unsigned int *) regs->pc;
  148. } else {
  149. __get_user(insn, (unsigned int *) regs->pc);
  150. }
  151. return safe_compute_effective_address(regs, insn);
  152. }
  153. static noinline void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  154. int text_fault)
  155. {
  156. unsigned long addr = compute_si_addr(regs, text_fault);
  157. __do_fault_siginfo(code, sig, regs, addr);
  158. }
  159. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  160. unsigned long address)
  161. {
  162. struct vm_area_struct *vma;
  163. struct task_struct *tsk = current;
  164. struct mm_struct *mm = tsk->mm;
  165. unsigned int fixup;
  166. unsigned long g2;
  167. int from_user = !(regs->psr & PSR_PS);
  168. int fault, code;
  169. unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  170. (write ? FAULT_FLAG_WRITE : 0));
  171. if(text_fault)
  172. address = regs->pc;
  173. /*
  174. * We fault-in kernel-space virtual memory on-demand. The
  175. * 'reference' page table is init_mm.pgd.
  176. *
  177. * NOTE! We MUST NOT take any locks for this case. We may
  178. * be in an interrupt or a critical region, and should
  179. * only copy the information from the master page table,
  180. * nothing more.
  181. */
  182. code = SEGV_MAPERR;
  183. if (address >= TASK_SIZE)
  184. goto vmalloc_fault;
  185. /*
  186. * If we're in an interrupt or have no user
  187. * context, we must not take the fault..
  188. */
  189. if (in_atomic() || !mm)
  190. goto no_context;
  191. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  192. retry:
  193. down_read(&mm->mmap_sem);
  194. if(!from_user && address >= PAGE_OFFSET)
  195. goto bad_area;
  196. vma = find_vma(mm, address);
  197. if(!vma)
  198. goto bad_area;
  199. if(vma->vm_start <= address)
  200. goto good_area;
  201. if(!(vma->vm_flags & VM_GROWSDOWN))
  202. goto bad_area;
  203. if(expand_stack(vma, address))
  204. goto bad_area;
  205. /*
  206. * Ok, we have a good vm_area for this memory access, so
  207. * we can handle it..
  208. */
  209. good_area:
  210. code = SEGV_ACCERR;
  211. if(write) {
  212. if(!(vma->vm_flags & VM_WRITE))
  213. goto bad_area;
  214. } else {
  215. /* Allow reads even for write-only mappings */
  216. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  217. goto bad_area;
  218. }
  219. /*
  220. * If for any reason at all we couldn't handle the fault,
  221. * make sure we exit gracefully rather than endlessly redo
  222. * the fault.
  223. */
  224. fault = handle_mm_fault(mm, vma, address, flags);
  225. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  226. return;
  227. if (unlikely(fault & VM_FAULT_ERROR)) {
  228. if (fault & VM_FAULT_OOM)
  229. goto out_of_memory;
  230. else if (fault & VM_FAULT_SIGBUS)
  231. goto do_sigbus;
  232. BUG();
  233. }
  234. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  235. if (fault & VM_FAULT_MAJOR) {
  236. current->maj_flt++;
  237. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  238. 1, regs, address);
  239. } else {
  240. current->min_flt++;
  241. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  242. 1, regs, address);
  243. }
  244. if (fault & VM_FAULT_RETRY) {
  245. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  246. /* No need to up_read(&mm->mmap_sem) as we would
  247. * have already released it in __lock_page_or_retry
  248. * in mm/filemap.c.
  249. */
  250. goto retry;
  251. }
  252. }
  253. up_read(&mm->mmap_sem);
  254. return;
  255. /*
  256. * Something tried to access memory that isn't in our memory map..
  257. * Fix it, but check if it's kernel or user first..
  258. */
  259. bad_area:
  260. up_read(&mm->mmap_sem);
  261. bad_area_nosemaphore:
  262. /* User mode accesses just cause a SIGSEGV */
  263. if (from_user) {
  264. do_fault_siginfo(code, SIGSEGV, regs, text_fault);
  265. return;
  266. }
  267. /* Is this in ex_table? */
  268. no_context:
  269. g2 = regs->u_regs[UREG_G2];
  270. if (!from_user) {
  271. fixup = search_extables_range(regs->pc, &g2);
  272. if (fixup > 10) { /* Values below are reserved for other things */
  273. extern const unsigned __memset_start[];
  274. extern const unsigned __memset_end[];
  275. extern const unsigned __csum_partial_copy_start[];
  276. extern const unsigned __csum_partial_copy_end[];
  277. #ifdef DEBUG_EXCEPTIONS
  278. printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
  279. printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
  280. regs->pc, fixup, g2);
  281. #endif
  282. if ((regs->pc >= (unsigned long)__memset_start &&
  283. regs->pc < (unsigned long)__memset_end) ||
  284. (regs->pc >= (unsigned long)__csum_partial_copy_start &&
  285. regs->pc < (unsigned long)__csum_partial_copy_end)) {
  286. regs->u_regs[UREG_I4] = address;
  287. regs->u_regs[UREG_I5] = regs->pc;
  288. }
  289. regs->u_regs[UREG_G2] = g2;
  290. regs->pc = fixup;
  291. regs->npc = regs->pc + 4;
  292. return;
  293. }
  294. }
  295. unhandled_fault (address, tsk, regs);
  296. do_exit(SIGKILL);
  297. /*
  298. * We ran out of memory, or some other thing happened to us that made
  299. * us unable to handle the page fault gracefully.
  300. */
  301. out_of_memory:
  302. up_read(&mm->mmap_sem);
  303. if (from_user) {
  304. pagefault_out_of_memory();
  305. return;
  306. }
  307. goto no_context;
  308. do_sigbus:
  309. up_read(&mm->mmap_sem);
  310. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, text_fault);
  311. if (!from_user)
  312. goto no_context;
  313. vmalloc_fault:
  314. {
  315. /*
  316. * Synchronize this task's top level page-table
  317. * with the 'reference' page table.
  318. */
  319. int offset = pgd_index(address);
  320. pgd_t *pgd, *pgd_k;
  321. pmd_t *pmd, *pmd_k;
  322. pgd = tsk->active_mm->pgd + offset;
  323. pgd_k = init_mm.pgd + offset;
  324. if (!pgd_present(*pgd)) {
  325. if (!pgd_present(*pgd_k))
  326. goto bad_area_nosemaphore;
  327. pgd_val(*pgd) = pgd_val(*pgd_k);
  328. return;
  329. }
  330. pmd = pmd_offset(pgd, address);
  331. pmd_k = pmd_offset(pgd_k, address);
  332. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  333. goto bad_area_nosemaphore;
  334. *pmd = *pmd_k;
  335. return;
  336. }
  337. }
  338. /* This always deals with user addresses. */
  339. static void force_user_fault(unsigned long address, int write)
  340. {
  341. struct vm_area_struct *vma;
  342. struct task_struct *tsk = current;
  343. struct mm_struct *mm = tsk->mm;
  344. int code;
  345. code = SEGV_MAPERR;
  346. down_read(&mm->mmap_sem);
  347. vma = find_vma(mm, address);
  348. if(!vma)
  349. goto bad_area;
  350. if(vma->vm_start <= address)
  351. goto good_area;
  352. if(!(vma->vm_flags & VM_GROWSDOWN))
  353. goto bad_area;
  354. if(expand_stack(vma, address))
  355. goto bad_area;
  356. good_area:
  357. code = SEGV_ACCERR;
  358. if(write) {
  359. if(!(vma->vm_flags & VM_WRITE))
  360. goto bad_area;
  361. } else {
  362. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  363. goto bad_area;
  364. }
  365. switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
  366. case VM_FAULT_SIGBUS:
  367. case VM_FAULT_OOM:
  368. goto do_sigbus;
  369. }
  370. up_read(&mm->mmap_sem);
  371. return;
  372. bad_area:
  373. up_read(&mm->mmap_sem);
  374. __do_fault_siginfo(code, SIGSEGV, tsk->thread.kregs, address);
  375. return;
  376. do_sigbus:
  377. up_read(&mm->mmap_sem);
  378. __do_fault_siginfo(BUS_ADRERR, SIGBUS, tsk->thread.kregs, address);
  379. }
  380. static void check_stack_aligned(unsigned long sp)
  381. {
  382. if (sp & 0x7UL)
  383. force_sig(SIGILL, current);
  384. }
  385. void window_overflow_fault(void)
  386. {
  387. unsigned long sp;
  388. sp = current_thread_info()->rwbuf_stkptrs[0];
  389. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  390. force_user_fault(sp + 0x38, 1);
  391. force_user_fault(sp, 1);
  392. check_stack_aligned(sp);
  393. }
  394. void window_underflow_fault(unsigned long sp)
  395. {
  396. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  397. force_user_fault(sp + 0x38, 0);
  398. force_user_fault(sp, 0);
  399. check_stack_aligned(sp);
  400. }
  401. void window_ret_fault(struct pt_regs *regs)
  402. {
  403. unsigned long sp;
  404. sp = regs->u_regs[UREG_FP];
  405. if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  406. force_user_fault(sp + 0x38, 0);
  407. force_user_fault(sp, 0);
  408. check_stack_aligned(sp);
  409. }