trap_kern.c 5.1 KB

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