trap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/kernel.h"
  6. #include "asm/errno.h"
  7. #include "linux/sched.h"
  8. #include "linux/mm.h"
  9. #include "linux/spinlock.h"
  10. #include "linux/init.h"
  11. #include "linux/ptrace.h"
  12. #include "asm/semaphore.h"
  13. #include "asm/pgtable.h"
  14. #include "asm/pgalloc.h"
  15. #include "asm/tlbflush.h"
  16. #include "asm/a.out.h"
  17. #include "asm/current.h"
  18. #include "asm/irq.h"
  19. #include "sysdep/sigcontext.h"
  20. #include "kern_util.h"
  21. #include "as-layout.h"
  22. #include "arch.h"
  23. #include "kern.h"
  24. #include "chan_kern.h"
  25. #include "mconsole_kern.h"
  26. #include "mem.h"
  27. #include "mem_kern.h"
  28. #include "sysdep/sigcontext.h"
  29. #include "sysdep/ptrace.h"
  30. #include "os.h"
  31. #ifdef CONFIG_MODE_SKAS
  32. #include "skas.h"
  33. #endif
  34. #include "os.h"
  35. /* Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by segv(). */
  36. int handle_page_fault(unsigned long address, unsigned long ip,
  37. int is_write, int is_user, int *code_out)
  38. {
  39. struct mm_struct *mm = current->mm;
  40. struct vm_area_struct *vma;
  41. pgd_t *pgd;
  42. pud_t *pud;
  43. pmd_t *pmd;
  44. pte_t *pte;
  45. int err = -EFAULT;
  46. *code_out = SEGV_MAPERR;
  47. /* If the fault was during atomic operation, don't take the fault, just
  48. * fail. */
  49. if (in_atomic())
  50. goto out_nosemaphore;
  51. down_read(&mm->mmap_sem);
  52. vma = find_vma(mm, address);
  53. if(!vma)
  54. goto out;
  55. else if(vma->vm_start <= address)
  56. goto good_area;
  57. else if(!(vma->vm_flags & VM_GROWSDOWN))
  58. goto out;
  59. else if(is_user && !ARCH_IS_STACKGROW(address))
  60. goto out;
  61. else if(expand_stack(vma, address))
  62. goto out;
  63. good_area:
  64. *code_out = SEGV_ACCERR;
  65. if(is_write && !(vma->vm_flags & VM_WRITE))
  66. goto out;
  67. /* Don't require VM_READ|VM_EXEC for write faults! */
  68. if(!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
  69. goto out;
  70. do {
  71. survive:
  72. switch (handle_mm_fault(mm, vma, address, is_write)){
  73. case VM_FAULT_MINOR:
  74. current->min_flt++;
  75. break;
  76. case VM_FAULT_MAJOR:
  77. current->maj_flt++;
  78. break;
  79. case VM_FAULT_SIGBUS:
  80. err = -EACCES;
  81. goto out;
  82. case VM_FAULT_OOM:
  83. err = -ENOMEM;
  84. goto out_of_memory;
  85. default:
  86. BUG();
  87. }
  88. pgd = pgd_offset(mm, address);
  89. pud = pud_offset(pgd, address);
  90. pmd = pmd_offset(pud, address);
  91. pte = pte_offset_kernel(pmd, address);
  92. } while(!pte_present(*pte));
  93. err = 0;
  94. /* The below warning was added in place of
  95. * pte_mkyoung(); if (is_write) pte_mkdirty();
  96. * If it's triggered, we'd see normally a hang here (a clean pte is
  97. * marked read-only to emulate the dirty bit).
  98. * However, the generic code can mark a PTE writable but clean on a
  99. * concurrent read fault, triggering this harmlessly. So comment it out.
  100. */
  101. #if 0
  102. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  103. #endif
  104. flush_tlb_page(vma, address);
  105. out:
  106. up_read(&mm->mmap_sem);
  107. out_nosemaphore:
  108. return(err);
  109. /*
  110. * We ran out of memory, or some other thing happened to us that made
  111. * us unable to handle the page fault gracefully.
  112. */
  113. out_of_memory:
  114. if (is_init(current)) {
  115. up_read(&mm->mmap_sem);
  116. yield();
  117. down_read(&mm->mmap_sem);
  118. goto survive;
  119. }
  120. goto out;
  121. }
  122. static void bad_segv(struct faultinfo fi, unsigned long ip)
  123. {
  124. struct siginfo si;
  125. si.si_signo = SIGSEGV;
  126. si.si_code = SEGV_ACCERR;
  127. si.si_addr = (void __user *) FAULT_ADDRESS(fi);
  128. current->thread.arch.faultinfo = fi;
  129. force_sig_info(SIGSEGV, &si, current);
  130. }
  131. static void segv_handler(int sig, union uml_pt_regs *regs)
  132. {
  133. struct faultinfo * fi = UPT_FAULTINFO(regs);
  134. if(UPT_IS_USER(regs) && !SEGV_IS_FIXABLE(fi)){
  135. bad_segv(*fi, UPT_IP(regs));
  136. return;
  137. }
  138. segv(*fi, UPT_IP(regs), UPT_IS_USER(regs), regs);
  139. }
  140. /*
  141. * We give a *copy* of the faultinfo in the regs to segv.
  142. * This must be done, since nesting SEGVs could overwrite
  143. * the info in the regs. A pointer to the info then would
  144. * give us bad data!
  145. */
  146. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user,
  147. union uml_pt_regs *regs)
  148. {
  149. struct siginfo si;
  150. void *catcher;
  151. int err;
  152. int is_write = FAULT_WRITE(fi);
  153. unsigned long address = FAULT_ADDRESS(fi);
  154. if(!is_user && (address >= start_vm) && (address < end_vm)){
  155. flush_tlb_kernel_vm();
  156. return 0;
  157. }
  158. else if(current->mm == NULL) {
  159. show_regs(container_of(regs, struct pt_regs, regs));
  160. panic("Segfault with no mm");
  161. }
  162. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  163. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  164. else {
  165. err = -EFAULT;
  166. /* A thread accessed NULL, we get a fault, but CR2 is invalid.
  167. * This code is used in __do_copy_from_user() of TT mode. */
  168. address = 0;
  169. }
  170. catcher = current->thread.fault_catcher;
  171. if(!err)
  172. return 0;
  173. else if(catcher != NULL){
  174. current->thread.fault_addr = (void *) address;
  175. do_longjmp(catcher, 1);
  176. }
  177. else if(current->thread.fault_addr != NULL)
  178. panic("fault_addr set but no fault catcher");
  179. else if(!is_user && arch_fixup(ip, regs))
  180. return 0;
  181. if(!is_user) {
  182. show_regs(container_of(regs, struct pt_regs, regs));
  183. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  184. address, ip);
  185. }
  186. if (err == -EACCES) {
  187. si.si_signo = SIGBUS;
  188. si.si_errno = 0;
  189. si.si_code = BUS_ADRERR;
  190. si.si_addr = (void __user *)address;
  191. current->thread.arch.faultinfo = fi;
  192. force_sig_info(SIGBUS, &si, current);
  193. } else if (err == -ENOMEM) {
  194. printk("VM: killing process %s\n", current->comm);
  195. do_exit(SIGKILL);
  196. } else {
  197. BUG_ON(err != -EFAULT);
  198. si.si_signo = SIGSEGV;
  199. si.si_addr = (void __user *) address;
  200. current->thread.arch.faultinfo = fi;
  201. force_sig_info(SIGSEGV, &si, current);
  202. }
  203. return 0;
  204. }
  205. void relay_signal(int sig, union uml_pt_regs *regs)
  206. {
  207. if(arch_handle_signal(sig, regs))
  208. return;
  209. if(!UPT_IS_USER(regs)){
  210. if(sig == SIGBUS)
  211. printk("Bus error - the host /dev/shm or /tmp mount "
  212. "likely just ran out of space\n");
  213. panic("Kernel mode signal %d", sig);
  214. }
  215. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  216. force_sig(sig, current);
  217. }
  218. static void bus_handler(int sig, union uml_pt_regs *regs)
  219. {
  220. if(current->thread.fault_catcher != NULL)
  221. do_longjmp(current->thread.fault_catcher, 1);
  222. else relay_signal(sig, regs);
  223. }
  224. static void winch(int sig, union uml_pt_regs *regs)
  225. {
  226. do_IRQ(WINCH_IRQ, regs);
  227. }
  228. const struct kern_handlers handlinfo_kern = {
  229. .relay_signal = relay_signal,
  230. .winch = winch,
  231. .bus_handler = bus_handler,
  232. .page_fault = segv_handler,
  233. .sigio_handler = sigio_handler,
  234. .timer_handler = timer_handler
  235. };
  236. void trap_init(void)
  237. {
  238. }