trap_kern.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /* Note this is constrained to return 0, -EFAULT, -EACCESS, -ENOMEM by segv(). */
  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. 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. /* Don't require VM_READ|VM_EXEC for write faults! */
  56. if(!is_write && !(vma->vm_flags & (VM_READ | VM_EXEC)))
  57. goto out;
  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, address);
  77. pud = pud_offset(pgd, address);
  78. pmd = pmd_offset(pud, address);
  79. pte = pte_offset_kernel(pmd, address);
  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, 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. 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. } else if (err == -ENOMEM) {
  143. printk("VM: killing process %s\n", current->comm);
  144. do_exit(SIGKILL);
  145. } else {
  146. BUG_ON(err != -EFAULT);
  147. si.si_signo = SIGSEGV;
  148. si.si_addr = (void *) address;
  149. current->thread.arch.faultinfo = fi;
  150. force_sig_info(SIGSEGV, &si, current);
  151. }
  152. return(0);
  153. }
  154. void bad_segv(struct faultinfo fi, unsigned long ip)
  155. {
  156. struct siginfo si;
  157. si.si_signo = SIGSEGV;
  158. si.si_code = SEGV_ACCERR;
  159. si.si_addr = (void *) FAULT_ADDRESS(fi);
  160. current->thread.arch.faultinfo = fi;
  161. force_sig_info(SIGSEGV, &si, current);
  162. }
  163. void relay_signal(int sig, union uml_pt_regs *regs)
  164. {
  165. if(arch_handle_signal(sig, regs)) return;
  166. if(!UPT_IS_USER(regs))
  167. panic("Kernel mode signal %d", sig);
  168. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  169. force_sig(sig, current);
  170. }
  171. void bus_handler(int sig, union uml_pt_regs *regs)
  172. {
  173. if(current->thread.fault_catcher != NULL)
  174. do_longjmp(current->thread.fault_catcher, 1);
  175. else relay_signal(sig, regs);
  176. }
  177. void winch(int sig, union uml_pt_regs *regs)
  178. {
  179. do_IRQ(WINCH_IRQ, regs);
  180. }
  181. void trap_init(void)
  182. {
  183. }