trap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
  60. if (unlikely(fault & VM_FAULT_ERROR)) {
  61. if (fault & VM_FAULT_OOM) {
  62. goto out_of_memory;
  63. } else if (fault & VM_FAULT_SIGBUS) {
  64. err = -EACCES;
  65. goto out;
  66. }
  67. BUG();
  68. }
  69. if (fault & VM_FAULT_MAJOR)
  70. current->maj_flt++;
  71. else
  72. current->min_flt++;
  73. pgd = pgd_offset(mm, address);
  74. pud = pud_offset(pgd, address);
  75. pmd = pmd_offset(pud, address);
  76. pte = pte_offset_kernel(pmd, address);
  77. } while (!pte_present(*pte));
  78. err = 0;
  79. /*
  80. * The below warning was added in place of
  81. * pte_mkyoung(); if (is_write) pte_mkdirty();
  82. * If it's triggered, we'd see normally a hang here (a clean pte is
  83. * marked read-only to emulate the dirty bit).
  84. * However, the generic code can mark a PTE writable but clean on a
  85. * concurrent read fault, triggering this harmlessly. So comment it out.
  86. */
  87. #if 0
  88. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  89. #endif
  90. flush_tlb_page(vma, address);
  91. out:
  92. up_read(&mm->mmap_sem);
  93. out_nosemaphore:
  94. return err;
  95. out_of_memory:
  96. /*
  97. * We ran out of memory, call the OOM killer, and return the userspace
  98. * (which will retry the fault, or kill us if we got oom-killed).
  99. */
  100. up_read(&mm->mmap_sem);
  101. pagefault_out_of_memory();
  102. return 0;
  103. }
  104. static void bad_segv(struct faultinfo fi, unsigned long ip)
  105. {
  106. struct siginfo si;
  107. si.si_signo = SIGSEGV;
  108. si.si_code = SEGV_ACCERR;
  109. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  110. current->thread.arch.faultinfo = fi;
  111. force_sig_info(SIGSEGV, &si, current);
  112. }
  113. void fatal_sigsegv(void)
  114. {
  115. force_sigsegv(SIGSEGV, current);
  116. do_signal();
  117. /*
  118. * This is to tell gcc that we're not returning - do_signal
  119. * can, in general, return, but in this case, it's not, since
  120. * we just got a fatal SIGSEGV queued.
  121. */
  122. os_dump_core();
  123. }
  124. void segv_handler(int sig, struct uml_pt_regs *regs)
  125. {
  126. struct faultinfo * fi = UPT_FAULTINFO(regs);
  127. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  128. bad_segv(*fi, UPT_IP(regs));
  129. return;
  130. }
  131. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  132. }
  133. /*
  134. * We give a *copy* of the faultinfo in the regs to segv.
  135. * This must be done, since nesting SEGVs could overwrite
  136. * the info in the regs. A pointer to the info then would
  137. * give us bad data!
  138. */
  139. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  140. struct uml_pt_regs *regs)
  141. {
  142. struct siginfo si;
  143. jmp_buf *catcher;
  144. int err;
  145. int is_write = FAULT_WRITE(fi);
  146. unsigned long address = FAULT_ADDRESS(fi);
  147. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  148. flush_tlb_kernel_vm();
  149. return 0;
  150. }
  151. else if (current->mm == NULL) {
  152. show_regs(container_of(regs, struct pt_regs, regs));
  153. panic("Segfault with no mm");
  154. }
  155. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  156. err = handle_page_fault(address, ip, is_write, is_user,
  157. &si.si_code);
  158. else {
  159. err = -EFAULT;
  160. /*
  161. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  162. * This code is used in __do_copy_from_user() of TT mode.
  163. * XXX tt mode is gone, so maybe this isn't needed any more
  164. */
  165. address = 0;
  166. }
  167. catcher = current->thread.fault_catcher;
  168. if (!err)
  169. return 0;
  170. else if (catcher != NULL) {
  171. current->thread.fault_addr = (void *) address;
  172. UML_LONGJMP(catcher, 1);
  173. }
  174. else if (current->thread.fault_addr != NULL)
  175. panic("fault_addr set but no fault catcher");
  176. else if (!is_user && arch_fixup(ip, regs))
  177. return 0;
  178. if (!is_user) {
  179. show_regs(container_of(regs, struct pt_regs, regs));
  180. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  181. address, ip);
  182. }
  183. if (err == -EACCES) {
  184. si.si_signo = SIGBUS;
  185. si.si_errno = 0;
  186. si.si_code = BUS_ADRERR;
  187. si.si_addr = (void __user *)address;
  188. current->thread.arch.faultinfo = fi;
  189. force_sig_info(SIGBUS, &si, current);
  190. } else {
  191. BUG_ON(err != -EFAULT);
  192. si.si_signo = SIGSEGV;
  193. si.si_addr = (void __user *) address;
  194. current->thread.arch.faultinfo = fi;
  195. force_sig_info(SIGSEGV, &si, current);
  196. }
  197. return 0;
  198. }
  199. void relay_signal(int sig, struct uml_pt_regs *regs)
  200. {
  201. if (!UPT_IS_USER(regs)) {
  202. if (sig == SIGBUS)
  203. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  204. "mount likely just ran out of space\n");
  205. panic("Kernel mode signal %d", sig);
  206. }
  207. arch_examine_signal(sig, regs);
  208. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  209. force_sig(sig, current);
  210. }
  211. void bus_handler(int sig, struct uml_pt_regs *regs)
  212. {
  213. if (current->thread.fault_catcher != NULL)
  214. UML_LONGJMP(current->thread.fault_catcher, 1);
  215. else relay_signal(sig, regs);
  216. }
  217. void winch(int sig, struct uml_pt_regs *regs)
  218. {
  219. do_IRQ(WINCH_IRQ, regs);
  220. }
  221. void trap_init(void)
  222. {
  223. }