trap.c 6.5 KB

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