trap.c 8.2 KB

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