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