trap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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: UML does not have exception tables. As such, this is almost a copy
  20. * of the code in mm/memory.c, only adjusting the logic to simply check whether
  21. * we are coming from the kernel instead of doing an additional lookup in the
  22. * exception table.
  23. * We can do this simplification because we never get here if the exception was
  24. * fixable.
  25. */
  26. static inline bool get_mmap_lock_carefully(struct mm_struct *mm, bool is_user)
  27. {
  28. if (likely(mmap_read_trylock(mm)))
  29. return true;
  30. if (!is_user)
  31. return false;
  32. return !mmap_read_lock_killable(mm);
  33. }
  34. static inline bool mmap_upgrade_trylock(struct mm_struct *mm)
  35. {
  36. /*
  37. * We don't have this operation yet.
  38. *
  39. * It should be easy enough to do: it's basically a
  40. * atomic_long_try_cmpxchg_acquire()
  41. * from RWSEM_READER_BIAS -> RWSEM_WRITER_LOCKED, but
  42. * it also needs the proper lockdep magic etc.
  43. */
  44. return false;
  45. }
  46. static inline bool upgrade_mmap_lock_carefully(struct mm_struct *mm, bool is_user)
  47. {
  48. mmap_read_unlock(mm);
  49. if (!is_user)
  50. return false;
  51. return !mmap_write_lock_killable(mm);
  52. }
  53. /*
  54. * Helper for page fault handling.
  55. *
  56. * This is kind of equivalend to "mmap_read_lock()" followed
  57. * by "find_extend_vma()", except it's a lot more careful about
  58. * the locking (and will drop the lock on failure).
  59. *
  60. * For example, if we have a kernel bug that causes a page
  61. * fault, we don't want to just use mmap_read_lock() to get
  62. * the mm lock, because that would deadlock if the bug were
  63. * to happen while we're holding the mm lock for writing.
  64. *
  65. * So this checks the exception tables on kernel faults in
  66. * order to only do this all for instructions that are actually
  67. * expected to fault.
  68. *
  69. * We can also actually take the mm lock for writing if we
  70. * need to extend the vma, which helps the VM layer a lot.
  71. */
  72. static struct vm_area_struct *
  73. um_lock_mm_and_find_vma(struct mm_struct *mm,
  74. unsigned long addr, bool is_user)
  75. {
  76. struct vm_area_struct *vma;
  77. if (!get_mmap_lock_carefully(mm, is_user))
  78. return NULL;
  79. vma = find_vma(mm, addr);
  80. if (likely(vma && (vma->vm_start <= addr)))
  81. return vma;
  82. /*
  83. * Well, dang. We might still be successful, but only
  84. * if we can extend a vma to do so.
  85. */
  86. if (!vma || !(vma->vm_flags & VM_GROWSDOWN)) {
  87. mmap_read_unlock(mm);
  88. return NULL;
  89. }
  90. /*
  91. * We can try to upgrade the mmap lock atomically,
  92. * in which case we can continue to use the vma
  93. * we already looked up.
  94. *
  95. * Otherwise we'll have to drop the mmap lock and
  96. * re-take it, and also look up the vma again,
  97. * re-checking it.
  98. */
  99. if (!mmap_upgrade_trylock(mm)) {
  100. if (!upgrade_mmap_lock_carefully(mm, is_user))
  101. return NULL;
  102. vma = find_vma(mm, addr);
  103. if (!vma)
  104. goto fail;
  105. if (vma->vm_start <= addr)
  106. goto success;
  107. if (!(vma->vm_flags & VM_GROWSDOWN))
  108. goto fail;
  109. }
  110. if (expand_stack_locked(vma, addr))
  111. goto fail;
  112. success:
  113. mmap_write_downgrade(mm);
  114. return vma;
  115. fail:
  116. mmap_write_unlock(mm);
  117. return NULL;
  118. }
  119. /*
  120. * Note this is constrained to return 0, -EFAULT, -EACCES, -ENOMEM by
  121. * segv().
  122. */
  123. int handle_page_fault(unsigned long address, unsigned long ip,
  124. int is_write, int is_user, int *code_out)
  125. {
  126. struct mm_struct *mm = current->mm;
  127. struct vm_area_struct *vma;
  128. pmd_t *pmd;
  129. pte_t *pte;
  130. int err = -EFAULT;
  131. unsigned int flags = FAULT_FLAG_DEFAULT;
  132. *code_out = SEGV_MAPERR;
  133. /*
  134. * If the fault was with pagefaults disabled, don't take the fault, just
  135. * fail.
  136. */
  137. if (faulthandler_disabled())
  138. goto out_nosemaphore;
  139. if (is_user)
  140. flags |= FAULT_FLAG_USER;
  141. retry:
  142. vma = um_lock_mm_and_find_vma(mm, address, is_user);
  143. if (!vma)
  144. goto out_nosemaphore;
  145. *code_out = SEGV_ACCERR;
  146. if (is_write) {
  147. if (!(vma->vm_flags & VM_WRITE))
  148. goto out;
  149. flags |= FAULT_FLAG_WRITE;
  150. } else {
  151. /* Don't require VM_READ|VM_EXEC for write faults! */
  152. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  153. goto out;
  154. }
  155. do {
  156. vm_fault_t fault;
  157. fault = handle_mm_fault(vma, address, flags, NULL);
  158. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  159. goto out_nosemaphore;
  160. /* The fault is fully completed (including releasing mmap lock) */
  161. if (fault & VM_FAULT_COMPLETED)
  162. return 0;
  163. if (unlikely(fault & VM_FAULT_ERROR)) {
  164. if (fault & VM_FAULT_OOM) {
  165. goto out_of_memory;
  166. } else if (fault & VM_FAULT_SIGSEGV) {
  167. goto out;
  168. } else if (fault & VM_FAULT_SIGBUS) {
  169. err = -EACCES;
  170. goto out;
  171. }
  172. BUG();
  173. }
  174. if (fault & VM_FAULT_RETRY) {
  175. flags |= FAULT_FLAG_TRIED;
  176. goto retry;
  177. }
  178. pmd = pmd_off(mm, address);
  179. pte = pte_offset_kernel(pmd, address);
  180. } while (!pte_present(*pte));
  181. err = 0;
  182. /*
  183. * The below warning was added in place of
  184. * pte_mkyoung(); if (is_write) pte_mkdirty();
  185. * If it's triggered, we'd see normally a hang here (a clean pte is
  186. * marked read-only to emulate the dirty bit).
  187. * However, the generic code can mark a PTE writable but clean on a
  188. * concurrent read fault, triggering this harmlessly. So comment it out.
  189. */
  190. #if 0
  191. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  192. #endif
  193. out:
  194. mmap_read_unlock(mm);
  195. out_nosemaphore:
  196. return err;
  197. out_of_memory:
  198. /*
  199. * We ran out of memory, call the OOM killer, and return the userspace
  200. * (which will retry the fault, or kill us if we got oom-killed).
  201. */
  202. mmap_read_unlock(mm);
  203. if (!is_user)
  204. goto out_nosemaphore;
  205. pagefault_out_of_memory();
  206. return 0;
  207. }
  208. static void show_segv_info(struct uml_pt_regs *regs)
  209. {
  210. struct task_struct *tsk = current;
  211. struct faultinfo *fi = UPT_FAULTINFO(regs);
  212. if (!unhandled_signal(tsk, SIGSEGV))
  213. return;
  214. if (!printk_ratelimit())
  215. return;
  216. printk("%s%s[%d]: segfault at %lx ip %px sp %px error %x",
  217. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  218. tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
  219. (void *)UPT_IP(regs), (void *)UPT_SP(regs),
  220. fi->error_code);
  221. print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
  222. printk(KERN_CONT "\n");
  223. }
  224. static void bad_segv(struct faultinfo fi, unsigned long ip)
  225. {
  226. current->thread.arch.faultinfo = fi;
  227. force_sig_fault(SIGSEGV, SEGV_ACCERR, (void __user *) FAULT_ADDRESS(fi));
  228. }
  229. void fatal_sigsegv(void)
  230. {
  231. force_fatal_sig(SIGSEGV);
  232. do_signal(&current->thread.regs);
  233. /*
  234. * This is to tell gcc that we're not returning - do_signal
  235. * can, in general, return, but in this case, it's not, since
  236. * we just got a fatal SIGSEGV queued.
  237. */
  238. os_dump_core();
  239. }
  240. /**
  241. * segv_handler() - the SIGSEGV handler
  242. * @sig: the signal number
  243. * @unused_si: the signal info struct; unused in this handler
  244. * @regs: the ptrace register information
  245. *
  246. * The handler first extracts the faultinfo from the UML ptrace regs struct.
  247. * If the userfault did not happen in an UML userspace process, bad_segv is called.
  248. * Otherwise the signal did happen in a cloned userspace process, handle it.
  249. */
  250. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  251. {
  252. struct faultinfo * fi = UPT_FAULTINFO(regs);
  253. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  254. show_segv_info(regs);
  255. bad_segv(*fi, UPT_IP(regs));
  256. return;
  257. }
  258. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  259. }
  260. /*
  261. * We give a *copy* of the faultinfo in the regs to segv.
  262. * This must be done, since nesting SEGVs could overwrite
  263. * the info in the regs. A pointer to the info then would
  264. * give us bad data!
  265. */
  266. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  267. struct uml_pt_regs *regs)
  268. {
  269. jmp_buf *catcher;
  270. int si_code;
  271. int err;
  272. int is_write = FAULT_WRITE(fi);
  273. unsigned long address = FAULT_ADDRESS(fi);
  274. if (!is_user && regs)
  275. current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
  276. if (!is_user && init_mm.context.sync_tlb_range_to) {
  277. /*
  278. * Kernel has pending updates from set_ptes that were not
  279. * flushed yet. Syncing them should fix the pagefault (if not
  280. * we'll get here again and panic).
  281. */
  282. err = um_tlb_sync(&init_mm);
  283. if (err == -ENOMEM)
  284. report_enomem();
  285. if (err)
  286. panic("Failed to sync kernel TLBs: %d", err);
  287. goto out;
  288. }
  289. else if (current->mm == NULL) {
  290. show_regs(container_of(regs, struct pt_regs, regs));
  291. panic("Segfault with no mm");
  292. }
  293. else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
  294. show_regs(container_of(regs, struct pt_regs, regs));
  295. panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
  296. address, ip);
  297. }
  298. if (SEGV_IS_FIXABLE(&fi))
  299. err = handle_page_fault(address, ip, is_write, is_user,
  300. &si_code);
  301. else {
  302. err = -EFAULT;
  303. /*
  304. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  305. * This code is used in __do_copy_from_user() of TT mode.
  306. * XXX tt mode is gone, so maybe this isn't needed any more
  307. */
  308. address = 0;
  309. }
  310. catcher = current->thread.fault_catcher;
  311. if (!err)
  312. goto out;
  313. else if (catcher != NULL) {
  314. current->thread.fault_addr = (void *) address;
  315. UML_LONGJMP(catcher, 1);
  316. }
  317. else if (current->thread.fault_addr != NULL)
  318. panic("fault_addr set but no fault catcher");
  319. else if (!is_user && arch_fixup(ip, regs))
  320. goto out;
  321. if (!is_user) {
  322. show_regs(container_of(regs, struct pt_regs, regs));
  323. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  324. address, ip);
  325. }
  326. show_segv_info(regs);
  327. if (err == -EACCES) {
  328. current->thread.arch.faultinfo = fi;
  329. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  330. } else {
  331. BUG_ON(err != -EFAULT);
  332. current->thread.arch.faultinfo = fi;
  333. force_sig_fault(SIGSEGV, si_code, (void __user *) address);
  334. }
  335. out:
  336. if (regs)
  337. current->thread.segv_regs = NULL;
  338. return 0;
  339. }
  340. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  341. {
  342. int code, err;
  343. if (!UPT_IS_USER(regs)) {
  344. if (sig == SIGBUS)
  345. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  346. "mount likely just ran out of space\n");
  347. panic("Kernel mode signal %d", sig);
  348. }
  349. arch_examine_signal(sig, regs);
  350. /* Is the signal layout for the signal known?
  351. * Signal data must be scrubbed to prevent information leaks.
  352. */
  353. code = si->si_code;
  354. err = si->si_errno;
  355. if ((err == 0) && (siginfo_layout(sig, code) == SIL_FAULT)) {
  356. struct faultinfo *fi = UPT_FAULTINFO(regs);
  357. current->thread.arch.faultinfo = *fi;
  358. force_sig_fault(sig, code, (void __user *)FAULT_ADDRESS(*fi));
  359. } else {
  360. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d) with errno %d\n",
  361. sig, code, err);
  362. force_sig(sig);
  363. }
  364. }
  365. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  366. {
  367. if (current->thread.fault_catcher != NULL)
  368. UML_LONGJMP(current->thread.fault_catcher, 1);
  369. else
  370. relay_signal(sig, si, regs);
  371. }
  372. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  373. {
  374. do_IRQ(WINCH_IRQ, regs);
  375. }