trap_kern.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. WARN_ON(!pte_young(*pte) || (is_write && !pte_dirty(*pte)));
  91. flush_tlb_page(vma, address);
  92. out:
  93. up_read(&mm->mmap_sem);
  94. out_nosemaphore:
  95. return(err);
  96. /*
  97. * We ran out of memory, or some other thing happened to us that made
  98. * us unable to handle the page fault gracefully.
  99. */
  100. out_of_memory:
  101. if (current->pid == 1) {
  102. up_read(&mm->mmap_sem);
  103. yield();
  104. down_read(&mm->mmap_sem);
  105. goto survive;
  106. }
  107. goto out;
  108. }
  109. /*
  110. * We give a *copy* of the faultinfo in the regs to segv.
  111. * This must be done, since nesting SEGVs could overwrite
  112. * the info in the regs. A pointer to the info then would
  113. * give us bad data!
  114. */
  115. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
  116. {
  117. struct siginfo si;
  118. void *catcher;
  119. int err;
  120. int is_write = FAULT_WRITE(fi);
  121. unsigned long address = FAULT_ADDRESS(fi);
  122. if(!is_user && (address >= start_vm) && (address < end_vm)){
  123. flush_tlb_kernel_vm();
  124. return(0);
  125. }
  126. else if(current->mm == NULL)
  127. panic("Segfault with no mm");
  128. if (SEGV_IS_FIXABLE(&fi) || SEGV_MAYBE_FIXABLE(&fi))
  129. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  130. else {
  131. err = -EFAULT;
  132. /* A thread accessed NULL, we get a fault, but CR2 is invalid.
  133. * This code is used in __do_copy_from_user() of TT mode. */
  134. address = 0;
  135. }
  136. catcher = current->thread.fault_catcher;
  137. if(!err)
  138. return(0);
  139. else if(catcher != NULL){
  140. current->thread.fault_addr = (void *) address;
  141. do_longjmp(catcher, 1);
  142. }
  143. else if(current->thread.fault_addr != NULL)
  144. panic("fault_addr set but no fault catcher");
  145. else if(!is_user && arch_fixup(ip, sc))
  146. return(0);
  147. if(!is_user)
  148. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  149. address, ip);
  150. if (err == -EACCES) {
  151. si.si_signo = SIGBUS;
  152. si.si_errno = 0;
  153. si.si_code = BUS_ADRERR;
  154. si.si_addr = (void *)address;
  155. current->thread.arch.faultinfo = fi;
  156. force_sig_info(SIGBUS, &si, current);
  157. } else if (err == -ENOMEM) {
  158. printk("VM: killing process %s\n", current->comm);
  159. do_exit(SIGKILL);
  160. } else {
  161. BUG_ON(err != -EFAULT);
  162. si.si_signo = SIGSEGV;
  163. si.si_addr = (void *) address;
  164. current->thread.arch.faultinfo = fi;
  165. force_sig_info(SIGSEGV, &si, current);
  166. }
  167. return(0);
  168. }
  169. void bad_segv(struct faultinfo fi, unsigned long ip)
  170. {
  171. struct siginfo si;
  172. si.si_signo = SIGSEGV;
  173. si.si_code = SEGV_ACCERR;
  174. si.si_addr = (void *) FAULT_ADDRESS(fi);
  175. current->thread.arch.faultinfo = fi;
  176. force_sig_info(SIGSEGV, &si, current);
  177. }
  178. void relay_signal(int sig, union uml_pt_regs *regs)
  179. {
  180. if(arch_handle_signal(sig, regs)) return;
  181. if(!UPT_IS_USER(regs))
  182. panic("Kernel mode signal %d", sig);
  183. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  184. force_sig(sig, current);
  185. }
  186. void bus_handler(int sig, union uml_pt_regs *regs)
  187. {
  188. if(current->thread.fault_catcher != NULL)
  189. do_longjmp(current->thread.fault_catcher, 1);
  190. else relay_signal(sig, regs);
  191. }
  192. void winch(int sig, union uml_pt_regs *regs)
  193. {
  194. do_IRQ(WINCH_IRQ, regs);
  195. }
  196. void trap_init(void)
  197. {
  198. }