trap.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. int 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. struct siginfo si;
  147. si.si_signo = SIGSEGV;
  148. si.si_code = SEGV_ACCERR;
  149. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  150. current->thread.arch.faultinfo = fi;
  151. force_sig_info(SIGSEGV, &si, current);
  152. }
  153. void fatal_sigsegv(void)
  154. {
  155. force_sigsegv(SIGSEGV, current);
  156. do_signal(&current->thread.regs);
  157. /*
  158. * This is to tell gcc that we're not returning - do_signal
  159. * can, in general, return, but in this case, it's not, since
  160. * we just got a fatal SIGSEGV queued.
  161. */
  162. os_dump_core();
  163. }
  164. /**
  165. * segv_handler() - the SIGSEGV handler
  166. * @sig: the signal number
  167. * @unused_si: the signal info struct; unused in this handler
  168. * @regs: the ptrace register information
  169. *
  170. * The handler first extracts the faultinfo from the UML ptrace regs struct.
  171. * If the userfault did not happen in an UML userspace process, bad_segv is called.
  172. * Otherwise the signal did happen in a cloned userspace process, handle it.
  173. */
  174. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  175. {
  176. struct faultinfo * fi = UPT_FAULTINFO(regs);
  177. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  178. show_segv_info(regs);
  179. bad_segv(*fi, UPT_IP(regs));
  180. return;
  181. }
  182. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  183. }
  184. /*
  185. * We give a *copy* of the faultinfo in the regs to segv.
  186. * This must be done, since nesting SEGVs could overwrite
  187. * the info in the regs. A pointer to the info then would
  188. * give us bad data!
  189. */
  190. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  191. struct uml_pt_regs *regs)
  192. {
  193. struct siginfo si;
  194. jmp_buf *catcher;
  195. int err;
  196. int is_write = FAULT_WRITE(fi);
  197. unsigned long address = FAULT_ADDRESS(fi);
  198. if (!is_user && regs)
  199. current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
  200. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  201. flush_tlb_kernel_vm();
  202. goto out;
  203. }
  204. else if (current->mm == NULL) {
  205. show_regs(container_of(regs, struct pt_regs, regs));
  206. panic("Segfault with no mm");
  207. }
  208. else if (!is_user && address > PAGE_SIZE && address < TASK_SIZE) {
  209. show_regs(container_of(regs, struct pt_regs, regs));
  210. panic("Kernel tried to access user memory at addr 0x%lx, ip 0x%lx",
  211. address, ip);
  212. }
  213. if (SEGV_IS_FIXABLE(&fi))
  214. err = handle_page_fault(address, ip, is_write, is_user,
  215. &si.si_code);
  216. else {
  217. err = -EFAULT;
  218. /*
  219. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  220. * This code is used in __do_copy_from_user() of TT mode.
  221. * XXX tt mode is gone, so maybe this isn't needed any more
  222. */
  223. address = 0;
  224. }
  225. catcher = current->thread.fault_catcher;
  226. if (!err)
  227. goto out;
  228. else if (catcher != NULL) {
  229. current->thread.fault_addr = (void *) address;
  230. UML_LONGJMP(catcher, 1);
  231. }
  232. else if (current->thread.fault_addr != NULL)
  233. panic("fault_addr set but no fault catcher");
  234. else if (!is_user && arch_fixup(ip, regs))
  235. goto out;
  236. if (!is_user) {
  237. show_regs(container_of(regs, struct pt_regs, regs));
  238. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  239. address, ip);
  240. }
  241. show_segv_info(regs);
  242. if (err == -EACCES) {
  243. si.si_signo = SIGBUS;
  244. si.si_errno = 0;
  245. si.si_code = BUS_ADRERR;
  246. si.si_addr = (void __user *)address;
  247. current->thread.arch.faultinfo = fi;
  248. force_sig_info(SIGBUS, &si, current);
  249. } else {
  250. BUG_ON(err != -EFAULT);
  251. si.si_signo = SIGSEGV;
  252. si.si_addr = (void __user *) address;
  253. current->thread.arch.faultinfo = fi;
  254. force_sig_info(SIGSEGV, &si, current);
  255. }
  256. out:
  257. if (regs)
  258. current->thread.segv_regs = NULL;
  259. return 0;
  260. }
  261. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  262. {
  263. struct faultinfo *fi;
  264. struct siginfo clean_si;
  265. if (!UPT_IS_USER(regs)) {
  266. if (sig == SIGBUS)
  267. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  268. "mount likely just ran out of space\n");
  269. panic("Kernel mode signal %d", sig);
  270. }
  271. arch_examine_signal(sig, regs);
  272. memset(&clean_si, 0, sizeof(clean_si));
  273. clean_si.si_signo = si->si_signo;
  274. clean_si.si_errno = si->si_errno;
  275. clean_si.si_code = si->si_code;
  276. switch (sig) {
  277. case SIGILL:
  278. case SIGFPE:
  279. case SIGSEGV:
  280. case SIGBUS:
  281. case SIGTRAP:
  282. fi = UPT_FAULTINFO(regs);
  283. clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
  284. current->thread.arch.faultinfo = *fi;
  285. #ifdef __ARCH_SI_TRAPNO
  286. clean_si.si_trapno = si->si_trapno;
  287. #endif
  288. break;
  289. default:
  290. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
  291. sig, si->si_code);
  292. }
  293. force_sig_info(sig, &clean_si, current);
  294. }
  295. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  296. {
  297. if (current->thread.fault_catcher != NULL)
  298. UML_LONGJMP(current->thread.fault_catcher, 1);
  299. else
  300. relay_signal(sig, si, regs);
  301. }
  302. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  303. {
  304. do_IRQ(WINCH_IRQ, regs);
  305. }
  306. void trap_init(void)
  307. {
  308. }