trap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. out:
  104. mmap_read_unlock(mm);
  105. out_nosemaphore:
  106. return err;
  107. out_of_memory:
  108. /*
  109. * We ran out of memory, call the OOM killer, and return the userspace
  110. * (which will retry the fault, or kill us if we got oom-killed).
  111. */
  112. mmap_read_unlock(mm);
  113. if (!is_user)
  114. goto out_nosemaphore;
  115. pagefault_out_of_memory();
  116. return 0;
  117. }
  118. static void show_segv_info(struct uml_pt_regs *regs)
  119. {
  120. struct task_struct *tsk = current;
  121. struct faultinfo *fi = UPT_FAULTINFO(regs);
  122. if (!unhandled_signal(tsk, SIGSEGV))
  123. return;
  124. if (!printk_ratelimit())
  125. return;
  126. printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
  127. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  128. tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
  129. (void *)UPT_IP(regs), (void *)UPT_SP(regs),
  130. fi->error_code);
  131. print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
  132. printk(KERN_CONT "\n");
  133. }
  134. static void bad_segv(struct faultinfo fi, unsigned long ip)
  135. {
  136. current->thread.arch.faultinfo = fi;
  137. force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi));
  138. }
  139. void fatal_sigsegv(void)
  140. {
  141. force_fatal_sig(SIGSEGV);
  142. do_signal(&current->thread.regs);
  143. /*
  144. * This is to tell gcc that we're not returning - do_signal
  145. * can, in general, return, but in this case, it's not, since
  146. * we just got a fatal SIGSEGV queued.
  147. */
  148. os_dump_core();
  149. }
  150. /**
  151. * segv_handler() - the SIGSEGV handler
  152. * @sig: the signal number
  153. * @unused_si: the signal info struct; unused in this handler
  154. * @regs: the ptrace register information
  155. *
  156. * The handler first extracts the faultinfo from the UML ptrace regs struct.
  157. * If the userfault did not happen in an UML userspace process, bad_segv is called.
  158. * Otherwise the signal did happen in a cloned userspace process, handle it.
  159. */
  160. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  161. {
  162. struct faultinfo * fi = UPT_FAULTINFO(regs);
  163. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  164. show_segv_info(regs);
  165. bad_segv(*fi, UPT_IP(regs));
  166. return;
  167. }
  168. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  169. }
  170. /*
  171. * We give a *copy* of the faultinfo in the regs to segv.
  172. * This must be done, since nesting SEGVs could overwrite
  173. * the info in the regs. A pointer to the info then would
  174. * give us bad data!
  175. */
  176. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  177. struct uml_pt_regs *regs)
  178. {
  179. jmp_buf *catcher;
  180. int si_code;
  181. int err;
  182. int is_write = FAULT_WRITE(fi);
  183. unsigned long address = FAULT_ADDRESS(fi);
  184. if (!is_user && regs)
  185. current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
  186. if (!is_user && init_mm.context.sync_tlb_range_to) {
  187. /*
  188. * Kernel has pending updates from set_ptes that were not
  189. * flushed yet. Syncing them should fix the pagefault (if not
  190. * we'll get here again and panic).
  191. */
  192. err = um_tlb_sync(&init_mm);
  193. if (err == -ENOMEM)
  194. report_enomem();
  195. if (err)
  196. panic("Failed to sync kernel TLBs: %d", err);
  197. goto out;
  198. }
  199. else if (current->mm == NULL) {
  200. show_regs(container_of(regs, struct pt_regs, regs));
  201. panic("Segfault with no mm");
  202. }
  203. else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
  204. show_regs(container_of(regs, struct pt_regs, regs));
  205. panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
  206. address, ip);
  207. }
  208. if (SEGV_IS_FIXABLE(&fi))
  209. err = handle_page_fault(address, ip, is_write, is_user,
  210. &si_code);
  211. else {
  212. err = -EFAULT;
  213. /*
  214. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  215. * This code is used in __do_copy_from_user() of TT mode.
  216. * XXX tt mode is gone, so maybe this isn't needed any more
  217. */
  218. address = 0;
  219. }
  220. catcher = current->thread.fault_catcher;
  221. if (!err)
  222. goto out;
  223. else if (catcher != NULL) {
  224. current->thread.fault_addr = (void *) address;
  225. UML_LONGJMP(catcher, 1);
  226. }
  227. else if (current->thread.fault_addr != NULL)
  228. panic("fault_addr set but no fault catcher");
  229. else if (!is_user && arch_fixup(ip, regs))
  230. goto out;
  231. if (!is_user) {
  232. show_regs(container_of(regs, struct pt_regs, regs));
  233. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  234. address, ip);
  235. }
  236. show_segv_info(regs);
  237. if (err == -EACCES) {
  238. current->thread.arch.faultinfo = fi;
  239. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  240. } else {
  241. BUG_ON(err != -EFAULT);
  242. current->thread.arch.faultinfo = fi;
  243. force_sig_fault(SIGSEGV, si_code, (void __user *) address);
  244. }
  245. out:
  246. if (regs)
  247. current->thread.segv_regs = NULL;
  248. return 0;
  249. }
  250. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  251. {
  252. int code, err;
  253. if (!UPT_IS_USER(regs)) {
  254. if (sig == SIGBUS)
  255. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  256. "mount likely just ran out of space\n");
  257. panic("Kernel mode signal %d", sig);
  258. }
  259. arch_examine_signal(sig, regs);
  260. /* Is the signal layout for the signal known?
  261. * Signal data must be scrubbed to prevent information leaks.
  262. */
  263. code = si->si_code;
  264. err = si->si_errno;
  265. if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
  266. struct faultinfo *fi = UPT_FAULTINFO(regs);
  267. current->thread.arch.faultinfo = *fi;
  268. force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi));
  269. } else {
  270. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
  271. sig, code, err);
  272. force_sig(sig);
  273. }
  274. }
  275. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  276. {
  277. if (current->thread.fault_catcher != NULL)
  278. UML_LONGJMP(current->thread.fault_catcher, 1);
  279. else
  280. relay_signal(sig, si, regs);
  281. }
  282. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  283. {
  284. do_IRQ(WINCH_IRQ, regs);
  285. }