trap.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if (!is_user)
  114. goto out_nosemaphore;
  115. pagefault_out_of_memory();
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(handle_page_fault);
  119. static void show_segv_info(struct uml_pt_regs *regs)
  120. {
  121. struct task_struct *tsk = current;
  122. struct faultinfo *fi = UPT_FAULTINFO(regs);
  123. if (!unhandled_signal(tsk, SIGSEGV))
  124. return;
  125. if (!printk_ratelimit())
  126. return;
  127. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
  128. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  129. tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
  130. (void *)UPT_IP(regs), (void *)UPT_SP(regs),
  131. fi->error_code);
  132. print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
  133. printk(KERN_CONT "\n");
  134. }
  135. static void bad_segv(struct faultinfo fi, unsigned long ip)
  136. {
  137. struct siginfo si;
  138. si.si_signo = SIGSEGV;
  139. si.si_code = SEGV_ACCERR;
  140. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  141. current->thread.arch.faultinfo = fi;
  142. force_sig_info(SIGSEGV, &si, current);
  143. }
  144. void fatal_sigsegv(void)
  145. {
  146. force_sigsegv(SIGSEGV, current);
  147. do_signal();
  148. /*
  149. * This is to tell gcc that we're not returning - do_signal
  150. * can, in general, return, but in this case, it's not, since
  151. * we just got a fatal SIGSEGV queued.
  152. */
  153. os_dump_core();
  154. }
  155. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  156. {
  157. struct faultinfo * fi = UPT_FAULTINFO(regs);
  158. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  159. show_segv_info(regs);
  160. bad_segv(*fi, UPT_IP(regs));
  161. return;
  162. }
  163. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  164. }
  165. /*
  166. * We give a *copy* of the faultinfo in the regs to segv.
  167. * This must be done, since nesting SEGVs could overwrite
  168. * the info in the regs. A pointer to the info then would
  169. * give us bad data!
  170. */
  171. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  172. struct uml_pt_regs *regs)
  173. {
  174. struct siginfo si;
  175. jmp_buf *catcher;
  176. int err;
  177. int is_write = FAULT_WRITE(fi);
  178. unsigned long address = FAULT_ADDRESS(fi);
  179. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  180. flush_tlb_kernel_vm();
  181. return 0;
  182. }
  183. else if (current->mm == NULL) {
  184. show_regs(container_of(regs, struct pt_regs, regs));
  185. panic("Segfault with no mm");
  186. }
  187. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  188. err = handle_page_fault(address, ip, is_write, is_user,
  189. &si.si_code);
  190. else {
  191. err = -EFAULT;
  192. /*
  193. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  194. * This code is used in __do_copy_from_user() of TT mode.
  195. * XXX tt mode is gone, so maybe this isn't needed any more
  196. */
  197. address = 0;
  198. }
  199. catcher = current->thread.fault_catcher;
  200. if (!err)
  201. return 0;
  202. else if (catcher != NULL) {
  203. current->thread.fault_addr = (void *) address;
  204. UML_LONGJMP(catcher, 1);
  205. }
  206. else if (current->thread.fault_addr != NULL)
  207. panic("fault_addr set but no fault catcher");
  208. else if (!is_user && arch_fixup(ip, regs))
  209. return 0;
  210. if (!is_user) {
  211. show_regs(container_of(regs, struct pt_regs, regs));
  212. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  213. address, ip);
  214. }
  215. show_segv_info(regs);
  216. if (err == -EACCES) {
  217. si.si_signo = SIGBUS;
  218. si.si_errno = 0;
  219. si.si_code = BUS_ADRERR;
  220. si.si_addr = (void __user *)address;
  221. current->thread.arch.faultinfo = fi;
  222. force_sig_info(SIGBUS, &si, current);
  223. } else {
  224. BUG_ON(err != -EFAULT);
  225. si.si_signo = SIGSEGV;
  226. si.si_addr = (void __user *) address;
  227. current->thread.arch.faultinfo = fi;
  228. force_sig_info(SIGSEGV, &si, current);
  229. }
  230. return 0;
  231. }
  232. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  233. {
  234. struct faultinfo *fi;
  235. struct siginfo clean_si;
  236. if (!UPT_IS_USER(regs)) {
  237. if (sig == SIGBUS)
  238. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  239. "mount likely just ran out of space\n");
  240. panic("Kernel mode signal %d", sig);
  241. }
  242. arch_examine_signal(sig, regs);
  243. memset(&clean_si, 0, sizeof(clean_si));
  244. clean_si.si_signo = si->si_signo;
  245. clean_si.si_errno = si->si_errno;
  246. clean_si.si_code = si->si_code;
  247. switch (sig) {
  248. case SIGILL:
  249. case SIGFPE:
  250. case SIGSEGV:
  251. case SIGBUS:
  252. case SIGTRAP:
  253. fi = UPT_FAULTINFO(regs);
  254. clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
  255. current->thread.arch.faultinfo = *fi;
  256. #ifdef __ARCH_SI_TRAPNO
  257. clean_si.si_trapno = si->si_trapno;
  258. #endif
  259. break;
  260. default:
  261. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
  262. sig, si->si_code);
  263. }
  264. force_sig_info(sig, &clean_si, current);
  265. }
  266. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  267. {
  268. if (current->thread.fault_catcher != NULL)
  269. UML_LONGJMP(current->thread.fault_catcher, 1);
  270. else
  271. relay_signal(sig, si, regs);
  272. }
  273. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  274. {
  275. do_IRQ(WINCH_IRQ, regs);
  276. }
  277. void trap_init(void)
  278. {
  279. }