trap_kern.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "user_util.h"
  21. #include "kern_util.h"
  22. #include "kern.h"
  23. #include "chan_kern.h"
  24. #include "mconsole_kern.h"
  25. #include "2_5compat.h"
  26. #include "mem.h"
  27. #include "mem_kern.h"
  28. int handle_page_fault(unsigned long address, unsigned long ip,
  29. int is_write, int is_user, int *code_out)
  30. {
  31. struct mm_struct *mm = current->mm;
  32. struct vm_area_struct *vma;
  33. pgd_t *pgd;
  34. pud_t *pud;
  35. pmd_t *pmd;
  36. pte_t *pte;
  37. unsigned long page;
  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. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  57. goto out;
  58. page = address & PAGE_MASK;
  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, page);
  78. pud = pud_offset(pgd, page);
  79. pmd = pmd_offset(pud, page);
  80. pte = pte_offset_kernel(pmd, page);
  81. } while(!pte_present(*pte));
  82. err = 0;
  83. *pte = pte_mkyoung(*pte);
  84. if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
  85. flush_tlb_page(vma, page);
  86. out:
  87. up_read(&mm->mmap_sem);
  88. return(err);
  89. /*
  90. * We ran out of memory, or some other thing happened to us that made
  91. * us unable to handle the page fault gracefully.
  92. */
  93. out_of_memory:
  94. if (current->pid == 1) {
  95. up_read(&mm->mmap_sem);
  96. yield();
  97. down_read(&mm->mmap_sem);
  98. goto survive;
  99. }
  100. goto out;
  101. }
  102. /*
  103. * We give a *copy* of the faultinfo in the regs to segv.
  104. * This must be done, since nesting SEGVs could overwrite
  105. * the info in the regs. A pointer to the info then would
  106. * give us bad data!
  107. */
  108. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
  109. {
  110. struct siginfo si;
  111. void *catcher;
  112. int err;
  113. int is_write = FAULT_WRITE(fi);
  114. unsigned long address = FAULT_ADDRESS(fi);
  115. if(!is_user && (address >= start_vm) && (address < end_vm)){
  116. flush_tlb_kernel_vm();
  117. return(0);
  118. }
  119. else if(current->mm == NULL)
  120. panic("Segfault with no mm");
  121. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  122. catcher = current->thread.fault_catcher;
  123. if(!err)
  124. return(0);
  125. else if(catcher != NULL){
  126. current->thread.fault_addr = (void *) address;
  127. do_longjmp(catcher, 1);
  128. }
  129. else if(current->thread.fault_addr != NULL)
  130. panic("fault_addr set but no fault catcher");
  131. else if(!is_user && arch_fixup(ip, sc))
  132. return(0);
  133. if(!is_user)
  134. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  135. address, ip);
  136. if(err == -EACCES){
  137. si.si_signo = SIGBUS;
  138. si.si_errno = 0;
  139. si.si_code = BUS_ADRERR;
  140. si.si_addr = (void *)address;
  141. current->thread.arch.faultinfo = fi;
  142. force_sig_info(SIGBUS, &si, current);
  143. }
  144. else if(err == -ENOMEM){
  145. printk("VM: killing process %s\n", current->comm);
  146. do_exit(SIGKILL);
  147. }
  148. else {
  149. si.si_signo = SIGSEGV;
  150. si.si_addr = (void *) address;
  151. current->thread.arch.faultinfo = fi;
  152. force_sig_info(SIGSEGV, &si, current);
  153. }
  154. return(0);
  155. }
  156. void bad_segv(struct faultinfo fi, unsigned long ip)
  157. {
  158. struct siginfo si;
  159. si.si_signo = SIGSEGV;
  160. si.si_code = SEGV_ACCERR;
  161. si.si_addr = (void *) FAULT_ADDRESS(fi);
  162. current->thread.arch.faultinfo = fi;
  163. force_sig_info(SIGSEGV, &si, current);
  164. }
  165. void relay_signal(int sig, union uml_pt_regs *regs)
  166. {
  167. if(arch_handle_signal(sig, regs)) return;
  168. if(!UPT_IS_USER(regs))
  169. panic("Kernel mode signal %d", sig);
  170. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  171. force_sig(sig, current);
  172. }
  173. void bus_handler(int sig, union uml_pt_regs *regs)
  174. {
  175. if(current->thread.fault_catcher != NULL)
  176. do_longjmp(current->thread.fault_catcher, 1);
  177. else relay_signal(sig, regs);
  178. }
  179. void winch(int sig, union uml_pt_regs *regs)
  180. {
  181. do_IRQ(WINCH_IRQ, regs);
  182. }
  183. void trap_init(void)
  184. {
  185. }
  186. DEFINE_SPINLOCK(trap_lock);
  187. static int trap_index = 0;
  188. int next_trap_index(int limit)
  189. {
  190. int ret;
  191. spin_lock(&trap_lock);
  192. ret = trap_index;
  193. if(++trap_index == limit)
  194. trap_index = 0;
  195. spin_unlock(&trap_lock);
  196. return(ret);
  197. }
  198. /*
  199. * Overrides for Emacs so that we follow Linus's tabbing style.
  200. * Emacs will notice this stuff at the end of the file and automatically
  201. * adjust the settings for this buffer only. This must remain at the end
  202. * of the file.
  203. * ---------------------------------------------------------------------------
  204. * Local variables:
  205. * c-file-style: "linux"
  206. * End:
  207. */