trap.c 7.6 KB

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