trap_kern.c 4.7 KB

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