trap.c 7.5 KB

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