/arch/um/kernel/trap_kern.c

https://bitbucket.org/evzijst/gittest · C · 251 lines · 206 code · 27 blank · 18 comment · 50 complexity · 4f76b16707c31ea9fa59cee994688d3d MD5 · raw file

  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. unsigned long segv(unsigned long address, unsigned long ip, int is_write,
  125. int is_user, void *sc)
  126. {
  127. struct siginfo si;
  128. void *catcher;
  129. int err;
  130. if(!is_user && (address >= start_vm) && (address < end_vm)){
  131. flush_tlb_kernel_vm();
  132. return(0);
  133. }
  134. else if(check_remapped_addr(address & PAGE_MASK, is_write))
  135. return(0);
  136. else if(current->mm == NULL)
  137. panic("Segfault with no mm");
  138. err = handle_page_fault(address, ip, is_write, is_user, &si.si_code);
  139. catcher = current->thread.fault_catcher;
  140. if(!err)
  141. return(0);
  142. else if(catcher != NULL){
  143. current->thread.fault_addr = (void *) address;
  144. do_longjmp(catcher, 1);
  145. }
  146. else if(current->thread.fault_addr != NULL)
  147. panic("fault_addr set but no fault catcher");
  148. else if(arch_fixup(ip, sc))
  149. return(0);
  150. if(!is_user)
  151. panic("Kernel mode fault at addr 0x%lx, ip 0x%lx",
  152. address, ip);
  153. if(err == -EACCES){
  154. si.si_signo = SIGBUS;
  155. si.si_errno = 0;
  156. si.si_code = BUS_ADRERR;
  157. si.si_addr = (void *)address;
  158. force_sig_info(SIGBUS, &si, current);
  159. }
  160. else if(err == -ENOMEM){
  161. printk("VM: killing process %s\n", current->comm);
  162. do_exit(SIGKILL);
  163. }
  164. else {
  165. si.si_signo = SIGSEGV;
  166. si.si_addr = (void *) address;
  167. current->thread.cr2 = address;
  168. current->thread.err = is_write;
  169. force_sig_info(SIGSEGV, &si, current);
  170. }
  171. return(0);
  172. }
  173. void bad_segv(unsigned long address, unsigned long ip, int is_write)
  174. {
  175. struct siginfo si;
  176. si.si_signo = SIGSEGV;
  177. si.si_code = SEGV_ACCERR;
  178. si.si_addr = (void *) address;
  179. current->thread.cr2 = address;
  180. current->thread.err = is_write;
  181. force_sig_info(SIGSEGV, &si, current);
  182. }
  183. void relay_signal(int sig, union uml_pt_regs *regs)
  184. {
  185. if(arch_handle_signal(sig, regs)) return;
  186. if(!UPT_IS_USER(regs))
  187. panic("Kernel mode signal %d", sig);
  188. force_sig(sig, current);
  189. }
  190. void bus_handler(int sig, union uml_pt_regs *regs)
  191. {
  192. if(current->thread.fault_catcher != NULL)
  193. do_longjmp(current->thread.fault_catcher, 1);
  194. else relay_signal(sig, regs);
  195. }
  196. void winch(int sig, union uml_pt_regs *regs)
  197. {
  198. do_IRQ(WINCH_IRQ, regs);
  199. }
  200. void trap_init(void)
  201. {
  202. }
  203. DEFINE_SPINLOCK(trap_lock);
  204. static int trap_index = 0;
  205. int next_trap_index(int limit)
  206. {
  207. int ret;
  208. spin_lock(&trap_lock);
  209. ret = trap_index;
  210. if(++trap_index == limit)
  211. trap_index = 0;
  212. spin_unlock(&trap_lock);
  213. return(ret);
  214. }
  215. /*
  216. * Overrides for Emacs so that we follow Linus's tabbing style.
  217. * Emacs will notice this stuff at the end of the file and automatically
  218. * adjust the settings for this buffer only. This must remain at the end
  219. * of the file.
  220. * ---------------------------------------------------------------------------
  221. * Local variables:
  222. * c-file-style: "linux"
  223. * End:
  224. */