trap.c 6.5 KB

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