fault_32.c 11 KB

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