trap_kern.c 5.0 KB

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