trap.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 fatal_sigsegv(void)
  120. {
  121. force_sigsegv(SIGSEGV, current);
  122. do_signal();
  123. /*
  124. * This is to tell gcc that we're not returning - do_signal
  125. * can, in general, return, but in this case, it's not, since
  126. * we just got a fatal SIGSEGV queued.
  127. */
  128. os_dump_core();
  129. }
  130. void segv_handler(int sig, struct uml_pt_regs *regs)
  131. {
  132. struct faultinfo * fi = UPT_FAULTINFO(regs);
  133. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  134. bad_segv(*fi, UPT_IP(regs));
  135. return;
  136. }
  137. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  138. }
  139. /*
  140. * We give a *copy* of the faultinfo in the regs to segv.
  141. * This must be done, since nesting SEGVs could overwrite
  142. * the info in the regs. A pointer to the info then would
  143. * give us bad data!
  144. */
  145. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  146. struct uml_pt_regs *regs)
  147. {
  148. struct siginfo si;
  149. jmp_buf *catcher;
  150. int err;
  151. int is_write = FAULT_WRITE(fi);
  152. unsigned long address = FAULT_ADDRESS(fi);
  153. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  154. flush_tlb_kernel_vm();
  155. return 0;
  156. }
  157. else if (current->mm == NULL) {
  158. show_regs(container_of(regs, struct pt_regs, regs));
  159. panic("Segfault with no mm");
  160. }
  161. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  162. err = handle_page_fault(address, ip, is_write, is_user,
  163. &si.si_code);
  164. else {
  165. err = -EFAULT;
  166. /*
  167. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  168. * This code is used in __do_copy_from_user() of TT mode.
  169. * XXX tt mode is gone, so maybe this isn't needed any more
  170. */
  171. address = 0;
  172. }
  173. catcher = current->thread.fault_catcher;
  174. if (!err)
  175. return 0;
  176. else if (catcher != NULL) {
  177. current->thread.fault_addr = (void *) address;
  178. UML_LONGJMP(catcher, 1);
  179. }
  180. else if (current->thread.fault_addr != NULL)
  181. panic("fault_addr set but no fault catcher");
  182. else if (!is_user && arch_fixup(ip, regs))
  183. return 0;
  184. if (!is_user) {
  185. show_regs(container_of(regs, struct pt_regs, regs));
  186. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  187. address, ip);
  188. }
  189. if (err == -EACCES) {
  190. si.si_signo = SIGBUS;
  191. si.si_errno = 0;
  192. si.si_code = BUS_ADRERR;
  193. si.si_addr = (void __user *)address;
  194. current->thread.arch.faultinfo = fi;
  195. force_sig_info(SIGBUS, &si, current);
  196. } else if (err == -ENOMEM) {
  197. printk(KERN_INFO "VM: killing process %s\n", current->comm);
  198. do_exit(SIGKILL);
  199. } else {
  200. BUG_ON(err != -EFAULT);
  201. si.si_signo = SIGSEGV;
  202. si.si_addr = (void __user *) address;
  203. current->thread.arch.faultinfo = fi;
  204. force_sig_info(SIGSEGV, &si, current);
  205. }
  206. return 0;
  207. }
  208. void relay_signal(int sig, struct uml_pt_regs *regs)
  209. {
  210. if (!UPT_IS_USER(regs)) {
  211. if (sig == SIGBUS)
  212. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  213. "mount likely just ran out of space\n");
  214. panic("Kernel mode signal %d", sig);
  215. }
  216. arch_examine_signal(sig, regs);
  217. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  218. force_sig(sig, current);
  219. }
  220. void bus_handler(int sig, struct uml_pt_regs *regs)
  221. {
  222. if (current->thread.fault_catcher != NULL)
  223. UML_LONGJMP(current->thread.fault_catcher, 1);
  224. else relay_signal(sig, regs);
  225. }
  226. void winch(int sig, struct uml_pt_regs *regs)
  227. {
  228. do_IRQ(WINCH_IRQ, regs);
  229. }
  230. void trap_init(void)
  231. {
  232. }