trap.c 7.5 KB

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