fault_32.c 11 KB

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