trap_kern.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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(!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. page = address & PAGE_MASK;
  57. pgd = pgd_offset(mm, page);
  58. pud = pud_offset(pgd, page);
  59. pmd = pmd_offset(pud, page);
  60. do {
  61. survive:
  62. switch (handle_mm_fault(mm, vma, address, is_write)){
  63. case VM_FAULT_MINOR:
  64. current->min_flt++;
  65. break;
  66. case VM_FAULT_MAJOR:
  67. current->maj_flt++;
  68. break;
  69. case VM_FAULT_SIGBUS:
  70. err = -EACCES;
  71. goto out;
  72. case VM_FAULT_OOM:
  73. err = -ENOMEM;
  74. goto out_of_memory;
  75. default:
  76. BUG();
  77. }
  78. pgd = pgd_offset(mm, page);
  79. pud = pud_offset(pgd, page);
  80. pmd = pmd_offset(pud, page);
  81. pte = pte_offset_kernel(pmd, page);
  82. } while(!pte_present(*pte));
  83. err = 0;
  84. *pte = pte_mkyoung(*pte);
  85. if(pte_write(*pte)) *pte = pte_mkdirty(*pte);
  86. flush_tlb_page(vma, page);
  87. out:
  88. up_read(&mm->mmap_sem);
  89. return(err);
  90. /*
  91. * We ran out of memory, or some other thing happened to us that made
  92. * us unable to handle the page fault gracefully.
  93. */
  94. out_of_memory:
  95. if (current->pid == 1) {
  96. up_read(&mm->mmap_sem);
  97. yield();
  98. down_read(&mm->mmap_sem);
  99. goto survive;
  100. }
  101. goto out;
  102. }
  103. LIST_HEAD(physmem_remappers);
  104. void register_remapper(struct remapper *info)
  105. {
  106. list_add(&info->list, &physmem_remappers);
  107. }
  108. static int check_remapped_addr(unsigned long address, int is_write)
  109. {
  110. struct remapper *remapper;
  111. struct list_head *ele;
  112. __u64 offset;
  113. int fd;
  114. fd = phys_mapping(__pa(address), &offset);
  115. if(fd == -1)
  116. return(0);
  117. list_for_each(ele, &physmem_remappers){
  118. remapper = list_entry(ele, struct remapper, list);
  119. if((*remapper->proc)(fd, address, is_write, offset))
  120. return(1);
  121. }
  122. return(0);
  123. }
  124. /*
  125. * We give a *copy* of the faultinfo in the regs to segv.
  126. * This must be done, since nesting SEGVs could overwrite
  127. * the info in the regs. A pointer to the info then would
  128. * give us bad data!
  129. */
  130. unsigned long segv(struct faultinfo fi, unsigned long ip, int is_user, void *sc)
  131. {
  132. struct siginfo si;
  133. void *catcher;
  134. int err;
  135. int is_write = FAULT_WRITE(fi);
  136. unsigned long address = FAULT_ADDRESS(fi);
  137. if(!is_user && (address >= start_vm) && (address < end_vm)){
  138. flush_tlb_kernel_vm();
  139. return(0);
  140. }
  141. else if(check_remapped_addr(address & PAGE_MASK, is_write))
  142. return(0);
  143. else if(current->mm == NULL)
  144. panic("Segfault with no mm");
  145. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  146. catcher = current->thread.fault_catcher;
  147. if(!err)
  148. return(0);
  149. else if(catcher != NULL){
  150. current->thread.fault_addr = (void *) address;
  151. do_longjmp(catcher, 1);
  152. }
  153. else if(current->thread.fault_addr != NULL)
  154. panic("fault_addr set but no fault catcher");
  155. else if(!is_user && arch_fixup(ip, sc))
  156. return(0);
  157. if(!is_user)
  158. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  159. address, ip);
  160. if(err == -EACCES){
  161. si.si_signo = SIGBUS;
  162. si.si_errno = 0;
  163. si.si_code = BUS_ADRERR;
  164. si.si_addr = (void *)address;
  165. current->thread.arch.faultinfo = fi;
  166. force_sig_info(SIGBUS, &si, current);
  167. }
  168. else if(err == -ENOMEM){
  169. printk("VM: killing process %s\n", current->comm);
  170. do_exit(SIGKILL);
  171. }
  172. else {
  173. si.si_signo = SIGSEGV;
  174. si.si_addr = (void *) address;
  175. current->thread.arch.faultinfo = fi;
  176. force_sig_info(SIGSEGV, &si, current);
  177. }
  178. return(0);
  179. }
  180. void bad_segv(struct faultinfo fi, unsigned long ip)
  181. {
  182. struct siginfo si;
  183. si.si_signo = SIGSEGV;
  184. si.si_code = SEGV_ACCERR;
  185. si.si_addr = (void *) FAULT_ADDRESS(fi);
  186. current->thread.arch.faultinfo = fi;
  187. force_sig_info(SIGSEGV, &si, current);
  188. }
  189. void relay_signal(int sig, union uml_pt_regs *regs)
  190. {
  191. if(arch_handle_signal(sig, regs)) return;
  192. if(!UPT_IS_USER(regs))
  193. panic("Kernel mode signal %d", sig);
  194. current->thread.arch.faultinfo = *UPT_FAULTINFO(regs);
  195. force_sig(sig, current);
  196. }
  197. void bus_handler(int sig, union uml_pt_regs *regs)
  198. {
  199. if(current->thread.fault_catcher != NULL)
  200. do_longjmp(current->thread.fault_catcher, 1);
  201. else relay_signal(sig, regs);
  202. }
  203. void winch(int sig, union uml_pt_regs *regs)
  204. {
  205. do_IRQ(WINCH_IRQ, regs);
  206. }
  207. void trap_init(void)
  208. {
  209. }
  210. DEFINE_SPINLOCK(trap_lock);
  211. static int trap_index = 0;
  212. int next_trap_index(int limit)
  213. {
  214. int ret;
  215. spin_lock(&trap_lock);
  216. ret = trap_index;
  217. if(++trap_index == limit)
  218. trap_index = 0;
  219. spin_unlock(&trap_lock);
  220. return(ret);
  221. }
  222. /*
  223. * Overrides for Emacs so that we follow Linus's tabbing style.
  224. * Emacs will notice this stuff at the end of the file and automatically
  225. * adjust the settings for this buffer only. This must remain at the end
  226. * of the file.
  227. * ---------------------------------------------------------------------------
  228. * Local variables:
  229. * c-file-style: "linux"
  230. * End:
  231. */