trap_kern.c 5.6 KB

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