trap.c 6.1 KB

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