trap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <linux/mm.h>
  6. #include <linux/sched/signal.h>
  7. #include <linux/hardirq.h>
  8. #include <linux/module.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/sched/debug.h>
  11. #include <asm/current.h>
  12. #include <asm/tlbflush.h>
  13. #include <arch.h>
  14. #include <as-layout.h>
  15. #include <kern_util.h>
  16. #include <os.h>
  17. #include <skas.h>
  18. /*
  19. * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by
  20. * segv().
  21. */
  22. int handle_page_fault(unsigned long address, unsigned long ip,
  23. int is_write, int is_user, int *code_out)
  24. {
  25. struct mm_struct *mm = current->mm;
  26. struct vm_area_struct *vma;
  27. pmd_t *pmd;
  28. pte_t *pte;
  29. int err = -EFAULT;
  30. unsigned int flags = FAULT_FLAG_DEFAULT;
  31. *code_out = SEGV_MAPERR;
  32. /*
  33. * If the fault was with pagefaults disabled, don't take the fault, just
  34. * fail.
  35. */
  36. if (faulthandler_disabled())
  37. goto out_nosemaphore;
  38. if (is_user)
  39. flags |= FAULT_FLAG_USER;
  40. retry:
  41. mmap_read_lock(mm);
  42. vma = find_vma(mm, address);
  43. if (!vma)
  44. goto out;
  45. if (vma->vm_start <= address)
  46. goto good_area;
  47. if (!(vma->vm_flags & VM_GROWSDOWN))
  48. goto out;
  49. if (is_user && !ARCH_IS_STACKGROW(address))
  50. goto out;
  51. vma = expand_stack(mm, address);
  52. if (!vma)
  53. goto out_nosemaphore;
  54. good_area:
  55. *code_out = SEGV_ACCERR;
  56. if (is_write) {
  57. if (!(vma->vm_flags & VM_WRITE))
  58. goto out;
  59. flags |= FAULT_FLAG_WRITE;
  60. } else {
  61. /* Don't require VM_READ|VM_EXEC for write faults! */
  62. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  63. goto out;
  64. }
  65. do {
  66. vm_fault_t fault;
  67. fault = handle_mm_fault(vma, address, flags, NULL);
  68. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  69. goto out_nosemaphore;
  70. /* The fault is fully completed (including releasing mmap lock) */
  71. if (fault & VM_FAULT_COMPLETED)
  72. return 0;
  73. if (unlikely(fault & VM_FAULT_ERROR)) {
  74. if (fault & VM_FAULT_OOM) {
  75. goto out_of_memory;
  76. } else if (fault & VM_FAULT_SIGSEGV) {
  77. goto out;
  78. } else if (fault & VM_FAULT_SIGBUS) {
  79. err = -EACCES;
  80. goto out;
  81. }
  82. BUG();
  83. }
  84. if (fault & VM_FAULT_RETRY) {
  85. flags |= FAULT_FLAG_TRIED;
  86. goto retry;
  87. }
  88. pmd = pmd_off(mm, address);
  89. pte = pte_offset_kernel(pmd, address);
  90. } while (!pte_present(*pte));
  91. err = 0;
  92. /*
  93. * The below warning was added in place of
  94. * pte_mkyoung(); if (is_write) pte_mkdirty();
  95. * If it's triggered, we'd see normally a hang here (a clean pte is
  96. * marked read-only to emulate the dirty bit).
  97. * However, the generic code can mark a PTE writable but clean on a
  98. * concurrent read fault, triggering this harmlessly. So comment it out.
  99. */
  100. #if 0
  101. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  102. #endif
  103. flush_tlb_page(vma, address);
  104. out:
  105. mmap_read_unlock(mm);
  106. out_nosemaphore:
  107. return err;
  108. out_of_memory:
  109. /*
  110. * We ran out of memory, call the OOM killer, and return the userspace
  111. * (which will retry the fault, or kill us if we got oom-killed).
  112. */
  113. mmap_read_unlock(mm);
  114. if (!is_user)
  115. goto out_nosemaphore;
  116. pagefault_out_of_memory();
  117. return 0;
  118. }
  119. static void show_segv_info(struct uml_pt_regs *regs)
  120. {
  121. struct task_struct *tsk = current;
  122. struct faultinfo *fi = UPT_FAULTINFO(regs);
  123. if (!unhandled_signal(tsk, SIGSEGV))
  124. return;
  125. if (!printk_ratelimit())
  126. return;
  127. printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
  128. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  129. tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
  130. (void *)UPT_IP(regs), (void *)UPT_SP(regs),
  131. fi->error_code);
  132. print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
  133. printk(KERN_CONT "\n");
  134. }
  135. static void bad_segv(struct faultinfo fi, unsigned long ip)
  136. {
  137. current->thread.arch.faultinfo = fi;
  138. force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi));
  139. }
  140. void fatal_sigsegv(void)
  141. {
  142. force_fatal_sig(SIGSEGV);
  143. do_signal(&current->thread.regs);
  144. /*
  145. * This is to tell gcc that we're not returning - do_signal
  146. * can, in general, return, but in this case, it's not, since
  147. * we just got a fatal SIGSEGV queued.
  148. */
  149. os_dump_core();
  150. }
  151. /**
  152. * segv_handler() - the SIGSEGV handler
  153. * @sig: the signal number
  154. * @unused_si: the signal info struct; unused in this handler
  155. * @regs: the ptrace register information
  156. *
  157. * The handler first extracts the faultinfo from the UML ptrace regs struct.
  158. * If the userfault did not happen in an UML userspace process, bad_segv is called.
  159. * Otherwise the signal did happen in a cloned userspace process, handle it.
  160. */
  161. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  162. {
  163. struct faultinfo * fi = UPT_FAULTINFO(regs);
  164. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  165. show_segv_info(regs);
  166. bad_segv(*fi, UPT_IP(regs));
  167. return;
  168. }
  169. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  170. }
  171. /*
  172. * We give a *copy* of the faultinfo in the regs to segv.
  173. * This must be done, since nesting SEGVs could overwrite
  174. * the info in the regs. A pointer to the info then would
  175. * give us bad data!
  176. */
  177. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  178. struct uml_pt_regs *regs)
  179. {
  180. jmp_buf *catcher;
  181. int si_code;
  182. int err;
  183. int is_write = FAULT_WRITE(fi);
  184. unsigned long address = FAULT_ADDRESS(fi);
  185. if (!is_user && regs)
  186. current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
  187. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  188. flush_tlb_kernel_vm();
  189. goto out;
  190. }
  191. else if (current->mm == NULL) {
  192. show_regs(container_of(regs, struct pt_regs, regs));
  193. panic("Segfault with no mm");
  194. }
  195. else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
  196. show_regs(container_of(regs, struct pt_regs, regs));
  197. panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
  198. address, ip);
  199. }
  200. if (SEGV_IS_FIXABLE(&fi))
  201. err = handle_page_fault(address, ip, is_write, is_user,
  202. &si_code);
  203. else {
  204. err = -EFAULT;
  205. /*
  206. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  207. * This code is used in __do_copy_from_user() of TT mode.
  208. * XXX tt mode is gone, so maybe this isn't needed any more
  209. */
  210. address = 0;
  211. }
  212. catcher = current->thread.fault_catcher;
  213. if (!err)
  214. goto out;
  215. else if (catcher != NULL) {
  216. current->thread.fault_addr = (void *) address;
  217. UML_LONGJMP(catcher, 1);
  218. }
  219. else if (current->thread.fault_addr != NULL)
  220. panic("fault_addr set but no fault catcher");
  221. else if (!is_user && arch_fixup(ip, regs))
  222. goto out;
  223. if (!is_user) {
  224. show_regs(container_of(regs, struct pt_regs, regs));
  225. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  226. address, ip);
  227. }
  228. show_segv_info(regs);
  229. if (err == -EACCES) {
  230. current->thread.arch.faultinfo = fi;
  231. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  232. } else {
  233. BUG_ON(err != -EFAULT);
  234. current->thread.arch.faultinfo = fi;
  235. force_sig_fault(SIGSEGV, si_code, (void __user *) address);
  236. }
  237. out:
  238. if (regs)
  239. current->thread.segv_regs = NULL;
  240. return 0;
  241. }
  242. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  243. {
  244. int code, err;
  245. if (!UPT_IS_USER(regs)) {
  246. if (sig == SIGBUS)
  247. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  248. "mount likely just ran out of space\n");
  249. panic("Kernel mode signal %d", sig);
  250. }
  251. arch_examine_signal(sig, regs);
  252. /* Is the signal layout for the signal known?
  253. * Signal data must be scrubbed to prevent information leaks.
  254. */
  255. code = si->si_code;
  256. err = si->si_errno;
  257. if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
  258. struct faultinfo *fi = UPT_FAULTINFO(regs);
  259. current->thread.arch.faultinfo = *fi;
  260. force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi));
  261. } else {
  262. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
  263. sig, code, err);
  264. force_sig(sig);
  265. }
  266. }
  267. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  268. {
  269. if (current->thread.fault_catcher != NULL)
  270. UML_LONGJMP(current->thread.fault_catcher, 1);
  271. else
  272. relay_signal(sig, si, regs);
  273. }
  274. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  275. {
  276. do_IRQ(WINCH_IRQ, regs);
  277. }