trap.c 8.0 KB

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