trap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  32. *code_out = SEGV_MAPERR;
  33. /*
  34. * If the fault was during atomic operation, don't take the fault, just
  35. * fail.
  36. */
  37. if (in_atomic())
  38. goto out_nosemaphore;
  39. if (is_user)
  40. flags |= FAULT_FLAG_USER;
  41. retry:
  42. down_read(&mm->mmap_sem);
  43. vma = find_vma(mm, address);
  44. if (!vma)
  45. goto out;
  46. else if (vma->vm_start <= address)
  47. goto good_area;
  48. else if (!(vma->vm_flags & VM_GROWSDOWN))
  49. goto out;
  50. else if (is_user && !ARCH_IS_STACKGROW(address))
  51. goto out;
  52. else if (expand_stack(vma, address))
  53. goto out;
  54. good_area:
  55. *code_out = SEGV_ACCERR;
  56. if (is_write) {
  57. if (!(vma->vm_flags & VM_WRITE))
  58. goto out;
  59. flags |= FAULT_FLAG_WRITE;
  60. } else {
  61. /* Don't require VM_READ|VM_EXEC for write faults! */
  62. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  63. goto out;
  64. }
  65. do {
  66. int fault;
  67. fault = handle_mm_fault(mm, vma, address, flags);
  68. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  69. goto out_nosemaphore;
  70. if (unlikely(fault & VM_FAULT_ERROR)) {
  71. if (fault & VM_FAULT_OOM) {
  72. goto out_of_memory;
  73. } else if (fault & VM_FAULT_SIGSEGV) {
  74. goto out;
  75. } else if (fault & VM_FAULT_SIGBUS) {
  76. err = -EACCES;
  77. goto out;
  78. }
  79. BUG();
  80. }
  81. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  82. if (fault & VM_FAULT_MAJOR)
  83. current->maj_flt++;
  84. else
  85. current->min_flt++;
  86. if (fault & VM_FAULT_RETRY) {
  87. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  88. flags |= FAULT_FLAG_TRIED;
  89. goto retry;
  90. }
  91. }
  92. pgd = pgd_offset(mm, address);
  93. pud = pud_offset(pgd, address);
  94. pmd = pmd_offset(pud, address);
  95. pte = pte_offset_kernel(pmd, address);
  96. } while (!pte_present(*pte));
  97. err = 0;
  98. /*
  99. * The below warning was added in place of
  100. * pte_mkyoung(); if (is_write) pte_mkdirty();
  101. * If it's triggered, we'd see normally a hang here (a clean pte is
  102. * marked read-only to emulate the dirty bit).
  103. * However, the generic code can mark a PTE writable but clean on a
  104. * concurrent read fault, triggering this harmlessly. So comment it out.
  105. */
  106. #if 0
  107. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  108. #endif
  109. flush_tlb_page(vma, address);
  110. out:
  111. up_read(&mm->mmap_sem);
  112. out_nosemaphore:
  113. return err;
  114. out_of_memory:
  115. /*
  116. * We ran out of memory, call the OOM killer, and return the userspace
  117. * (which will retry the fault, or kill us if we got oom-killed).
  118. */
  119. up_read(&mm->mmap_sem);
  120. if (!is_user)
  121. goto out_nosemaphore;
  122. pagefault_out_of_memory();
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(handle_page_fault);
  126. static void show_segv_info(struct uml_pt_regs *regs)
  127. {
  128. struct task_struct *tsk = current;
  129. struct faultinfo *fi = UPT_FAULTINFO(regs);
  130. if (!unhandled_signal(tsk, SIGSEGV))
  131. return;
  132. if (!printk_ratelimit())
  133. return;
  134. printk("%s%s[%d]: segfault at %lx ip %p sp %p error %x",
  135. task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
  136. tsk->comm, task_pid_nr(tsk), FAULT_ADDRESS(*fi),
  137. (void *)UPT_IP(regs), (void *)UPT_SP(regs),
  138. fi->error_code);
  139. print_vma_addr(KERN_CONT " in ", UPT_IP(regs));
  140. printk(KERN_CONT "\n");
  141. }
  142. static void bad_segv(struct faultinfo fi, unsigned long ip)
  143. {
  144. struct siginfo si;
  145. si.si_signo = SIGSEGV;
  146. si.si_code = SEGV_ACCERR;
  147. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  148. current->thread.arch.faultinfo = fi;
  149. force_sig_info(SIGSEGV, &si, current);
  150. }
  151. void fatal_sigsegv(void)
  152. {
  153. force_sigsegv(SIGSEGV, current);
  154. do_signal();
  155. /*
  156. * This is to tell gcc that we're not returning - do_signal
  157. * can, in general, return, but in this case, it's not, since
  158. * we just got a fatal SIGSEGV queued.
  159. */
  160. os_dump_core();
  161. }
  162. void segv_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  163. {
  164. struct faultinfo * fi = UPT_FAULTINFO(regs);
  165. if (UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)) {
  166. show_segv_info(regs);
  167. bad_segv(*fi, UPT_IP(regs));
  168. return;
  169. }
  170. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  171. }
  172. /*
  173. * We give a *copy* of the faultinfo in the regs to segv.
  174. * This must be done, since nesting SEGVs could overwrite
  175. * the info in the regs. A pointer to the info then would
  176. * give us bad data!
  177. */
  178. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  179. struct uml_pt_regs *regs)
  180. {
  181. struct siginfo si;
  182. jmp_buf *catcher;
  183. int err;
  184. int is_write = FAULT_WRITE(fi);
  185. unsigned long address = FAULT_ADDRESS(fi);
  186. if (!is_user && regs)
  187. current->thread.segv_regs = container_of(regs, struct pt_regs, regs);
  188. if (!is_user && (address >= start_vm) && (address < end_vm)) {
  189. flush_tlb_kernel_vm();
  190. goto out;
  191. }
  192. else if (current->mm == NULL) {
  193. show_regs(container_of(regs, struct pt_regs, regs));
  194. panic("Segfault with no mm");
  195. }
  196. if (SEGV_IS_FIXABLE(&fi))
  197. err = handle_page_fault(address, ip, is_write, is_user,
  198. &si.si_code);
  199. else {
  200. err = -EFAULT;
  201. /*
  202. * A thread accessed NULL, we get a fault, but CR2 is invalid.
  203. * This code is used in __do_copy_from_user() of TT mode.
  204. * XXX tt mode is gone, so maybe this isn't needed any more
  205. */
  206. address = 0;
  207. }
  208. catcher = current->thread.fault_catcher;
  209. if (!err)
  210. goto out;
  211. else if (catcher != NULL) {
  212. current->thread.fault_addr = (void *) address;
  213. UML_LONGJMP(catcher, 1);
  214. }
  215. else if (current->thread.fault_addr != NULL)
  216. panic("fault_addr set but no fault catcher");
  217. else if (!is_user && arch_fixup(ip, regs))
  218. goto out;
  219. if (!is_user) {
  220. show_regs(container_of(regs, struct pt_regs, regs));
  221. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  222. address, ip);
  223. }
  224. show_segv_info(regs);
  225. if (err == -EACCES) {
  226. si.si_signo = SIGBUS;
  227. si.si_errno = 0;
  228. si.si_code = BUS_ADRERR;
  229. si.si_addr = (void __user *)address;
  230. current->thread.arch.faultinfo = fi;
  231. force_sig_info(SIGBUS, &si, current);
  232. } else {
  233. BUG_ON(err != -EFAULT);
  234. si.si_signo = SIGSEGV;
  235. si.si_addr = (void __user *) address;
  236. current->thread.arch.faultinfo = fi;
  237. force_sig_info(SIGSEGV, &si, current);
  238. }
  239. out:
  240. if (regs)
  241. current->thread.segv_regs = NULL;
  242. return 0;
  243. }
  244. void relay_signal(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  245. {
  246. struct faultinfo *fi;
  247. struct siginfo clean_si;
  248. if (!UPT_IS_USER(regs)) {
  249. if (sig == SIGBUS)
  250. printk(KERN_ERR "Bus error - the host /dev/shm or /tmp "
  251. "mount likely just ran out of space\n");
  252. panic("Kernel mode signal %d", sig);
  253. }
  254. arch_examine_signal(sig, regs);
  255. memset(&clean_si, 0, sizeof(clean_si));
  256. clean_si.si_signo = si->si_signo;
  257. clean_si.si_errno = si->si_errno;
  258. clean_si.si_code = si->si_code;
  259. switch (sig) {
  260. case SIGILL:
  261. case SIGFPE:
  262. case SIGSEGV:
  263. case SIGBUS:
  264. case SIGTRAP:
  265. fi = UPT_FAULTINFO(regs);
  266. clean_si.si_addr = (void __user *) FAULT_ADDRESS(*fi);
  267. current->thread.arch.faultinfo = *fi;
  268. #ifdef __ARCH_SI_TRAPNO
  269. clean_si.si_trapno = si->si_trapno;
  270. #endif
  271. break;
  272. default:
  273. printk(KERN_ERR "Attempted to relay unknown signal %d (si_code = %d)\n",
  274. sig, si->si_code);
  275. }
  276. force_sig_info(sig, &clean_si, current);
  277. }
  278. void bus_handler(int sig, struct siginfo *si, struct uml_pt_regs *regs)
  279. {
  280. if (current->thread.fault_catcher != NULL)
  281. UML_LONGJMP(current->thread.fault_catcher, 1);
  282. else
  283. relay_signal(sig, si, regs);
  284. }
  285. void winch(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  286. {
  287. do_IRQ(WINCH_IRQ, regs);
  288. }
  289. void trap_init(void)
  290. {
  291. }