trap.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <asm/current.h>
  9. #include <asm/pgtable.h>
  10. #include <asm/tlbflush.h>
  11. #include "arch.h"
  12. #include "as-layout.h"
  13. #include "kern_util.h"
  14. #include "os.h"
  15. #include "skas.h"
  16. #include "sysdep/sigcontext.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. *code_out = SEGV_MAPERR;
  32. /*
  33. * If the fault was during atomic operation, don't take the fault, just
  34. * fail.
  35. */
  36. if (in_atomic())
  37. goto out_nosemaphore;
  38. down_read(&mm->mmap_sem);
  39. vma = find_vma(mm, address);
  40. if (!vma)
  41. goto out;
  42. else if (vma->vm_start <= address)
  43. goto good_area;
  44. else if (!(vma->vm_flags & VM_GROWSDOWN))
  45. goto out;
  46. else if (is_user && !ARCH_IS_STACKGROW(address))
  47. goto out;
  48. else if (expand_stack(vma, address))
  49. goto out;
  50. good_area:
  51. *code_out = SEGV_ACCERR;
  52. if (is_write && !(vma->vm_flags & VM_WRITE))
  53. goto out;
  54. /* Don't require VM_READ|VM_EXEC for write faults! */
  55. if (!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
  56. goto out;
  57. do {
  58. int fault;
  59. survive:
  60. fault = handle_mm_fault(mm, vma, address, is_write);
  61. if (unlikely(fault & VM_FAULT_ERROR)) {
  62. if (fault & VM_FAULT_OOM) {
  63. err = -ENOMEM;
  64. goto out_of_memory;
  65. } else if (fault & VM_FAULT_SIGBUS) {
  66. err = -EACCES;
  67. goto out;
  68. }
  69. BUG();
  70. }
  71. if (fault & VM_FAULT_MAJOR)
  72. current->maj_flt++;
  73. else
  74. current->min_flt++;
  75. pgd = pgd_offset(mm, address);
  76. pud = pud_offset(pgd, address);
  77. pmd = pmd_offset(pud, address);
  78. pte = pte_offset_kernel(pmd, address);
  79. } while (!pte_present(*pte));
  80. err = 0;
  81. /*
  82. * The below warning was added in place of
  83. * pte_mkyoung(); if (is_write) pte_mkdirty();
  84. * If it's triggered, we'd see normally a hang here (a clean pte is
  85. * marked read-only to emulate the dirty bit).
  86. * However, the generic code can mark a PTE writable but clean on a
  87. * concurrent read fault, triggering this harmlessly. So comment it out.
  88. */
  89. #if 0
  90. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  91. #endif
  92. flush_tlb_page(vma, address);
  93. out:
  94. up_read(&mm->mmap_sem);
  95. out_nosemaphore:
  96. return err;
  97. /*
  98. * We ran out of memory, or some other thing happened to us that made
  99. * us unable to handle the page fault gracefully.
  100. */
  101. out_of_memory:
  102. if (is_global_init(current)) {
  103. up_read(&mm->mmap_sem);
  104. yield();
  105. down_read(&mm->mmap_sem);
  106. goto survive;
  107. }
  108. goto out;
  109. }
  110. static void bad_segv(struct faultinfo fi, unsigned long ip)
  111. {
  112. struct siginfo si;
  113. si.si_signo = SIGSEGV;
  114. si.si_code = SEGV_ACCERR;
  115. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  116. current->thread.arch.faultinfo = fi;
  117. force_sig_info(SIGSEGV, &si, current);
  118. }
  119. void segv_handler(int sig, struct uml_pt_regs *regs)
  120. {
  121. struct faultinfo * fi = UPT_FAULTINFO(regs);
  122. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  123. bad_segv(*fi, UPT_IP(regs));
  124. return;
  125. }
  126. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  127. }
  128. /*
  129. * We give a *copy* of the faultinfo in the regs to segv.
  130. * This must be done, since nesting SEGVs could overwrite
  131. * the info in the regs. A pointer to the info then would
  132. * give us bad data!
  133. */
  134. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  135. struct uml_pt_regs *regs)
  136. {
  137. struct siginfo si;
  138. jmp_buf *catcher;
  139. int err;
  140. int is_write = FAULT_WRITE(fi);
  141. unsigned long address = FAULT_ADDRESS(fi);
  142. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  143. flush_tlb_kernel_vm();
  144. return 0;
  145. }
  146. else if (current->mm == NULL) {
  147. show_regs(container_of(regs, struct pt_regs, regs));
  148. panic("Segfault with no mm");
  149. }
  150. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  151. err = handle_page_fault(address, ip, is_write, is_user,
  152. &si.si_code);
  153. else {
  154. err = -EFAULT;
  155. /*
  156. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  157. * This code is used in __do_copy_from_user() of TT mode.
  158. * XXX tt mode is gone, so maybe this isn't needed any more
  159. */
  160. address = 0;
  161. }
  162. catcher = current->thread.fault_catcher;
  163. if (!err)
  164. return 0;
  165. else if (catcher != NULL) {
  166. current->thread.fault_addr = (void *) address;
  167. UML_LONGJMP(catcher, 1);
  168. }
  169. else if (current->thread.fault_addr != NULL)
  170. panic("fault_addr set but no fault catcher");
  171. else if (!is_user && arch_fixup(ip, regs))
  172. return 0;
  173. if (!is_user) {
  174. show_regs(container_of(regs, struct pt_regs, regs));
  175. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  176. address, ip);
  177. }
  178. if (err == -EACCES) {
  179. si.si_signo = SIGBUS;
  180. si.si_errno = 0;
  181. si.si_code = BUS_ADRERR;
  182. si.si_addr = (void __user *)address;
  183. current->thread.arch.faultinfo = fi;
  184. force_sig_info(SIGBUS, &si, current);
  185. } else if (err == -ENOMEM) {
  186. printk(KERN_INFO "VM: killing process %s\n", current->comm);
  187. do_exit(SIGKILL);
  188. } else {
  189. BUG_ON(err != -EFAULT);
  190. si.si_signo = SIGSEGV;
  191. si.si_addr = (void __user *) address;
  192. current->thread.arch.faultinfo = fi;
  193. force_sig_info(SIGSEGV, &si, current);
  194. }
  195. return 0;
  196. }
  197. void relay_signal(int sig, struct uml_pt_regs *regs)
  198. {
  199. if (!UPT_IS_USER(regs)) {
  200. if (sig == SIGBUS)
  201. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  202. "mount likely just ran out of space\n");
  203. panic("Kernel mode signal %d", sig);
  204. }
  205. arch_examine_signal(sig, regs);
  206. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  207. force_sig(sig, current);
  208. }
  209. void bus_handler(int sig, struct uml_pt_regs *regs)
  210. {
  211. if (current->thread.fault_catcher != NULL)
  212. UML_LONGJMP(current->thread.fault_catcher, 1);
  213. else relay_signal(sig, regs);
  214. }
  215. void winch(int sig, struct uml_pt_regs *regs)
  216. {
  217. do_IRQ(WINCH_IRQ, regs);
  218. }
  219. void trap_init(void)
  220. {
  221. }