trap_kern.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "mem.h"
  26. #include "mem_kern.h"
  27. int handle_page_fault(unsigned long address, unsigned long ip,
  28. int is_write, int is_user, int *code_out)
  29. {
  30. struct mm_struct *mm = current->mm;
  31. struct vm_area_struct *vma;
  32. pgd_t *pgd;
  33. pud_t *pud;
  34. pmd_t *pmd;
  35. pte_t *pte;
  36. unsigned long page;
  37. int err = -EFAULT;
  38. *code_out = SEGV_MAPERR;
  39. down_read(&mm->mmap_sem);
  40. vma = find_vma(mm, address);
  41. if(!vma)
  42. goto out;
  43. else if(vma->vm_start <= address)
  44. goto good_area;
  45. else if(!(vma->vm_flags & VM_GROWSDOWN))
  46. goto out;
  47. else if(is_user && !ARCH_IS_STACKGROW(address))
  48. goto out;
  49. else if(expand_stack(vma, address))
  50. goto out;
  51. good_area:
  52. *code_out = SEGV_ACCERR;
  53. if(is_write && !(vma->vm_flags & VM_WRITE))
  54. goto out;
  55. if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
  56. goto out;
  57. page = address & PAGE_MASK;
  58. do {
  59. survive:
  60. switch (handle_mm_fault(mm, vma, address, is_write)){
  61. case VM_FAULT_MINOR:
  62. current->min_flt++;
  63. break;
  64. case VM_FAULT_MAJOR:
  65. current->maj_flt++;
  66. break;
  67. case VM_FAULT_SIGBUS:
  68. err = -EACCES;
  69. goto out;
  70. case VM_FAULT_OOM:
  71. err = -ENOMEM;
  72. goto out_of_memory;
  73. default:
  74. BUG();
  75. }
  76. pgd = pgd_offset(mm, page);
  77. pud = pud_offset(pgd, page);
  78. pmd = pmd_offset(pud, page);
  79. pte = pte_offset_kernel(pmd, page);
  80. } while(!pte_present(*pte));
  81. err = 0;
  82. *pte = pte_mkyoung(*pte);
  83. if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
  84. flush_tlb_page(vma, page);
  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. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  121. catcher = current->thread.fault_catcher;
  122. if(!err)
  123. return(0);
  124. else if(catcher != NULL){
  125. current->thread.fault_addr = (void *) address;
  126. do_longjmp(catcher, 1);
  127. }
  128. else if(current->thread.fault_addr != NULL)
  129. panic("fault_addr set but no fault catcher");
  130. else if(!is_user && arch_fixup(ip, sc))
  131. return(0);
  132. if(!is_user)
  133. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  134. address, ip);
  135. if(err == -EACCES){
  136. si.si_signo = SIGBUS;
  137. si.si_errno = 0;
  138. si.si_code = BUS_ADRERR;
  139. si.si_addr = (void *)address;
  140. current->thread.arch.faultinfo = fi;
  141. force_sig_info(SIGBUS, &si, current);
  142. }
  143. else if(err == -ENOMEM){
  144. printk("VM: killing process %s\n", current->comm);
  145. do_exit(SIGKILL);
  146. }
  147. else {
  148. si.si_signo = SIGSEGV;
  149. si.si_addr = (void *) address;
  150. current->thread.arch.faultinfo = fi;
  151. force_sig_info(SIGSEGV, &si, current);
  152. }
  153. return(0);
  154. }
  155. void bad_segv(struct faultinfo fi, unsigned long ip)
  156. {
  157. struct siginfo si;
  158. si.si_signo = SIGSEGV;
  159. si.si_code = SEGV_ACCERR;
  160. si.si_addr = (void *) FAULT_ADDRESS(fi);
  161. current->thread.arch.faultinfo = fi;
  162. force_sig_info(SIGSEGV, &si, current);
  163. }
  164. void relay_signal(int sig, union uml_pt_regs *regs)
  165. {
  166. if(arch_handle_signal(sig, regs)) return;
  167. if(!UPT_IS_USER(regs))
  168. panic("Kernel mode signal %d", sig);
  169. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  170. force_sig(sig, current);
  171. }
  172. void bus_handler(int sig, union uml_pt_regs *regs)
  173. {
  174. if(current->thread.fault_catcher != NULL)
  175. do_longjmp(current->thread.fault_catcher, 1);
  176. else relay_signal(sig, regs);
  177. }
  178. void winch(int sig, union uml_pt_regs *regs)
  179. {
  180. do_IRQ(WINCH_IRQ, regs);
  181. }
  182. void trap_init(void)
  183. {
  184. }
  185. DEFINE_SPINLOCK(trap_lock);
  186. static int trap_index = 0;
  187. int next_trap_index(int limit)
  188. {
  189. int ret;
  190. spin_lock(&trap_lock);
  191. ret = trap_index;
  192. if(++trap_index == limit)
  193. trap_index = 0;
  194. spin_unlock(&trap_lock);
  195. return(ret);
  196. }
  197. /*
  198. * Overrides for Emacs so that we follow Linus's tabbing style.
  199. * Emacs will notice this stuff at the end of the file and automatically
  200. * adjust the settings for this buffer only. This must remain at the end
  201. * of the file.
  202. * ---------------------------------------------------------------------------
  203. * Local variables:
  204. * c-file-style: "linux"
  205. * End:
  206. */