fault_32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fault.c: Page fault handlers for the Sparc.
  4. *
  5. * Copyright (C) 1995 David S. Miller ([email protected])
  6. * Copyright (C) 1996 Eddie C. Dost ([email protected])
  7. * Copyright (C) 1997 Jakub Jelinek ([email protected])
  8. */
  9. #include <asm/head.h>
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #include <linux/sched.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/mman.h>
  15. #include <linux/threads.h>
  16. #include <linux/kernel.h>
  17. #include <linux/signal.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/perf_event.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kdebug.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/extable.h>
  25. #include <asm/page.h>
  26. #include <asm/openprom.h>
  27. #include <asm/oplib.h>
  28. #include <asm/setup.h>
  29. #include <asm/smp.h>
  30. #include <asm/traps.h>
  31. #include "mm_32.h"
  32. int show_unhandled_signals = 1;
  33. static void __noreturn unhandled_fault(unsigned long address,
  34. struct task_struct *tsk,
  35. struct pt_regs *regs)
  36. {
  37. if ((unsigned long) address < PAGE_SIZE) {
  38. printk(KERN_ALERT
  39. "Unable to handle kernel NULL pointer dereference\n");
  40. } else {
  41. printk(KERN_ALERT "Unable to handle kernel paging request at virtual address %08lx\n",
  42. address);
  43. }
  44. printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
  45. (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
  46. printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
  47. (tsk->mm ? (unsigned long) tsk->mm->pgd :
  48. (unsigned long) tsk->active_mm->pgd));
  49. die_if_kernel("Oops", regs);
  50. }
  51. static inline void
  52. show_signal_msg(struct pt_regs *regs, int sig, int code,
  53. unsigned long address, struct task_struct *tsk)
  54. {
  55. if (!unhandled_signal(tsk, sig))
  56. return;
  57. if (!printk_ratelimit())
  58. return;
  59. printk("%s%s[%d]: segfault at %lx ip %px (rpc %px) sp %px error %x",
  60. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  61. tsk->comm, task_pid_nr(tsk), address,
  62. (void *)regs->pc, (void *)regs->u_regs[UREG_I7],
  63. (void *)regs->u_regs[UREG_FP], code);
  64. print_vma_addr(KERN_CONT " in ", regs->pc);
  65. printk(KERN_CONT "\n");
  66. }
  67. static void __do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  68. unsigned long addr)
  69. {
  70. if (unlikely(show_unhandled_signals))
  71. show_signal_msg(regs, sig, code,
  72. addr, current);
  73. force_sig_fault(sig, code, (void __user *) addr);
  74. }
  75. static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
  76. {
  77. unsigned int insn;
  78. if (text_fault)
  79. return regs->pc;
  80. if (regs->psr & PSR_PS)
  81. insn = *(unsigned int *) regs->pc;
  82. else
  83. __get_user(insn, (unsigned int *) regs->pc);
  84. return safe_compute_effective_address(regs, insn);
  85. }
  86. static noinline void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
  87. int text_fault)
  88. {
  89. unsigned long addr = compute_si_addr(regs, text_fault);
  90. __do_fault_siginfo(code, sig, regs, addr);
  91. }
  92. asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
  93. unsigned long address)
  94. {
  95. struct vm_area_struct *vma;
  96. struct task_struct *tsk = current;
  97. struct mm_struct *mm = tsk->mm;
  98. int from_user = !(regs->psr & PSR_PS);
  99. int code;
  100. vm_fault_t fault;
  101. unsigned int flags = FAULT_FLAG_DEFAULT;
  102. if (text_fault)
  103. address = regs->pc;
  104. /*
  105. * We fault-in kernel-space virtual memory on-demand. The
  106. * 'reference' page table is init_mm.pgd.
  107. *
  108. * NOTE! We MUST NOT take any locks for this case. We may
  109. * be in an interrupt or a critical region, and should
  110. * only copy the information from the master page table,
  111. * nothing more.
  112. */
  113. code = SEGV_MAPERR;
  114. if (address >= TASK_SIZE)
  115. goto vmalloc_fault;
  116. /*
  117. * If we're in an interrupt or have no user
  118. * context, we must not take the fault..
  119. */
  120. if (pagefault_disabled() || !mm)
  121. goto no_context;
  122. if (!from_user && address >= PAGE_OFFSET)
  123. goto no_context;
  124. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  125. retry:
  126. vma = lock_mm_and_find_vma(mm, address, regs);
  127. if (!vma)
  128. goto bad_area_nosemaphore;
  129. /*
  130. * Ok, we have a good vm_area for this memory access, so
  131. * we can handle it..
  132. */
  133. code = SEGV_ACCERR;
  134. if (write) {
  135. if (!(vma->vm_flags & VM_WRITE))
  136. goto bad_area;
  137. } else {
  138. /* Allow reads even for write-only mappings */
  139. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  140. goto bad_area;
  141. }
  142. if (from_user)
  143. flags |= FAULT_FLAG_USER;
  144. if (write)
  145. flags |= FAULT_FLAG_WRITE;
  146. /*
  147. * If for any reason at all we couldn't handle the fault,
  148. * make sure we exit gracefully rather than endlessly redo
  149. * the fault.
  150. */
  151. fault = handle_mm_fault(vma, address, flags, regs);
  152. if (fault_signal_pending(fault, regs))
  153. return;
  154. /* The fault is fully completed (including releasing mmap lock) */
  155. if (fault & VM_FAULT_COMPLETED)
  156. return;
  157. if (unlikely(fault & VM_FAULT_ERROR)) {
  158. if (fault & VM_FAULT_OOM)
  159. goto out_of_memory;
  160. else if (fault & VM_FAULT_SIGSEGV)
  161. goto bad_area;
  162. else if (fault & VM_FAULT_SIGBUS)
  163. goto do_sigbus;
  164. BUG();
  165. }
  166. if (fault & VM_FAULT_RETRY) {
  167. flags |= FAULT_FLAG_TRIED;
  168. /* No need to mmap_read_unlock(mm) as we would
  169. * have already released it in __lock_page_or_retry
  170. * in mm/filemap.c.
  171. */
  172. goto retry;
  173. }
  174. mmap_read_unlock(mm);
  175. return;
  176. /*
  177. * Something tried to access memory that isn't in our memory map..
  178. * Fix it, but check if it's kernel or user first..
  179. */
  180. bad_area:
  181. mmap_read_unlock(mm);
  182. bad_area_nosemaphore:
  183. /* User mode accesses just cause a SIGSEGV */
  184. if (from_user) {
  185. do_fault_siginfo(code, SIGSEGV, regs, text_fault);
  186. return;
  187. }
  188. /* Is this in ex_table? */
  189. no_context:
  190. if (!from_user) {
  191. const struct exception_table_entry *entry;
  192. entry = search_exception_tables(regs->pc);
  193. #ifdef DEBUG_EXCEPTIONS
  194. printk("Exception: PC<%08lx> faddr<%08lx>\n",
  195. regs->pc, address);
  196. printk("EX_TABLE: insn<%08lx> fixup<%08x>\n",
  197. regs->pc, entry->fixup);
  198. #endif
  199. regs->pc = entry->fixup;
  200. regs->npc = regs->pc + 4;
  201. return;
  202. }
  203. unhandled_fault(address, tsk, regs);
  204. /*
  205. * We ran out of memory, or some other thing happened to us that made
  206. * us unable to handle the page fault gracefully.
  207. */
  208. out_of_memory:
  209. mmap_read_unlock(mm);
  210. if (from_user) {
  211. pagefault_out_of_memory();
  212. return;
  213. }
  214. goto no_context;
  215. do_sigbus:
  216. mmap_read_unlock(mm);
  217. do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, text_fault);
  218. if (!from_user)
  219. goto no_context;
  220. vmalloc_fault:
  221. {
  222. /*
  223. * Synchronize this task's top level page-table
  224. * with the 'reference' page table.
  225. */
  226. int offset = pgd_index(address);
  227. pgd_t *pgd, *pgd_k;
  228. p4d_t *p4d, *p4d_k;
  229. pud_t *pud, *pud_k;
  230. pmd_t *pmd, *pmd_k;
  231. pgd = tsk->active_mm->pgd + offset;
  232. pgd_k = init_mm.pgd + offset;
  233. if (!pgd_present(*pgd)) {
  234. if (!pgd_present(*pgd_k))
  235. goto bad_area_nosemaphore;
  236. pgd_val(*pgd) = pgd_val(*pgd_k);
  237. return;
  238. }
  239. p4d = p4d_offset(pgd, address);
  240. pud = pud_offset(p4d, address);
  241. pmd = pmd_offset(pud, address);
  242. p4d_k = p4d_offset(pgd_k, address);
  243. pud_k = pud_offset(p4d_k, address);
  244. pmd_k = pmd_offset(pud_k, address);
  245. if (pmd_present(*pmd) || !pmd_present(*pmd_k))
  246. goto bad_area_nosemaphore;
  247. *pmd = *pmd_k;
  248. return;
  249. }
  250. }
  251. /* This always deals with user addresses. */
  252. static void force_user_fault(unsigned long address, int write)
  253. {
  254. struct vm_area_struct *vma;
  255. struct task_struct *tsk = current;
  256. struct mm_struct *mm = tsk->mm;
  257. unsigned int flags = FAULT_FLAG_USER;
  258. int code;
  259. code = SEGV_MAPERR;
  260. vma = lock_mm_and_find_vma(mm, address, regs);
  261. if (!vma)
  262. goto bad_area_nosemaphore;
  263. code = SEGV_ACCERR;
  264. if (write) {
  265. if (!(vma->vm_flags & VM_WRITE))
  266. goto bad_area;
  267. flags |= FAULT_FLAG_WRITE;
  268. } else {
  269. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  270. goto bad_area;
  271. }
  272. switch (handle_mm_fault(vma, address, flags, NULL)) {
  273. case VM_FAULT_SIGBUS:
  274. case VM_FAULT_OOM:
  275. goto do_sigbus;
  276. }
  277. mmap_read_unlock(mm);
  278. return;
  279. bad_area:
  280. mmap_read_unlock(mm);
  281. bad_area_nosemaphore:
  282. __do_fault_siginfo(code, SIGSEGV, tsk->thread.kregs, address);
  283. return;
  284. do_sigbus:
  285. mmap_read_unlock(mm);
  286. __do_fault_siginfo(BUS_ADRERR, SIGBUS, tsk->thread.kregs, address);
  287. }
  288. static void check_stack_aligned(unsigned long sp)
  289. {
  290. if (sp & 0x7UL)
  291. force_sig(SIGILL);
  292. }
  293. void window_overflow_fault(void)
  294. {
  295. unsigned long sp;
  296. sp = current_thread_info()->rwbuf_stkptrs[0];
  297. if (((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  298. force_user_fault(sp + 0x38, 1);
  299. force_user_fault(sp, 1);
  300. check_stack_aligned(sp);
  301. }
  302. void window_underflow_fault(unsigned long sp)
  303. {
  304. if (((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  305. force_user_fault(sp + 0x38, 0);
  306. force_user_fault(sp, 0);
  307. check_stack_aligned(sp);
  308. }
  309. void window_ret_fault(struct pt_regs *regs)
  310. {
  311. unsigned long sp;
  312. sp = regs->u_regs[UREG_FP];
  313. if (((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
  314. force_user_fault(sp + 0x38, 0);
  315. force_user_fault(sp, 0);
  316. check_stack_aligned(sp);
  317. }