fault.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * linux/arch/arm26/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Modifications for ARM processor (c) 1995-2001 Russell King
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/init.h>
  24. #include <asm/system.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/uaccess.h> //FIXME this header may be bogusly included
  27. #include "fault.h"
  28. #define FAULT_CODE_LDRSTRPOST 0x80
  29. #define FAULT_CODE_LDRSTRPRE 0x40
  30. #define FAULT_CODE_LDRSTRREG 0x20
  31. #define FAULT_CODE_LDMSTM 0x10
  32. #define FAULT_CODE_LDCSTC 0x08
  33. #define FAULT_CODE_PREFETCH 0x04
  34. #define FAULT_CODE_WRITE 0x02
  35. #define FAULT_CODE_FORCECOW 0x01
  36. #define DO_COW(m) ((m) & (FAULT_CODE_WRITE|FAULT_CODE_FORCECOW))
  37. #define READ_FAULT(m) (!((m) & FAULT_CODE_WRITE))
  38. #define DEBUG
  39. /*
  40. * This is useful to dump out the page tables associated with
  41. * 'addr' in mm 'mm'.
  42. */
  43. void show_pte(struct mm_struct *mm, unsigned long addr)
  44. {
  45. pgd_t *pgd;
  46. if (!mm)
  47. mm = &init_mm;
  48. printk(KERN_ALERT "pgd = %p\n", mm->pgd);
  49. pgd = pgd_offset(mm, addr);
  50. printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
  51. do {
  52. pmd_t *pmd;
  53. pte_t *pte;
  54. pmd = pmd_offset(pgd, addr);
  55. if (pmd_none(*pmd))
  56. break;
  57. if (pmd_bad(*pmd)) {
  58. printk("(bad)");
  59. break;
  60. }
  61. /* We must not map this if we have highmem enabled */
  62. /* FIXME */
  63. pte = pte_offset_map(pmd, addr);
  64. printk(", *pte=%08lx", pte_val(*pte));
  65. pte_unmap(pte);
  66. } while(0);
  67. printk("\n");
  68. }
  69. /*
  70. * Oops. The kernel tried to access some page that wasn't present.
  71. */
  72. static void
  73. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  74. struct pt_regs *regs)
  75. {
  76. /*
  77. * Are we prepared to handle this kernel fault?
  78. */
  79. if (fixup_exception(regs))
  80. return;
  81. /*
  82. * No handler, we'll have to terminate things with extreme prejudice.
  83. */
  84. bust_spinlocks(1);
  85. printk(KERN_ALERT
  86. "Unable to handle kernel %s at virtual address %08lx\n",
  87. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  88. "paging request", addr);
  89. show_pte(mm, addr);
  90. die("Oops", regs, fsr);
  91. bust_spinlocks(0);
  92. do_exit(SIGKILL);
  93. }
  94. /*
  95. * Something tried to access memory that isn't in our memory map..
  96. * User mode accesses just cause a SIGSEGV
  97. */
  98. static void
  99. __do_user_fault(struct task_struct *tsk, unsigned long addr,
  100. unsigned int fsr, int code, struct pt_regs *regs)
  101. {
  102. struct siginfo si;
  103. #ifdef CONFIG_DEBUG_USER
  104. printk("%s: unhandled page fault at 0x%08lx, code 0x%03x\n",
  105. tsk->comm, addr, fsr);
  106. show_pte(tsk->mm, addr);
  107. show_regs(regs);
  108. //dump_backtrace(regs, tsk); // FIXME ARM32 dropped this - why?
  109. while(1); //FIXME - hack to stop debug going nutso
  110. #endif
  111. tsk->thread.address = addr;
  112. tsk->thread.error_code = fsr;
  113. tsk->thread.trap_no = 14;
  114. si.si_signo = SIGSEGV;
  115. si.si_errno = 0;
  116. si.si_code = code;
  117. si.si_addr = (void *)addr;
  118. force_sig_info(SIGSEGV, &si, tsk);
  119. }
  120. static int
  121. __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  122. struct task_struct *tsk)
  123. {
  124. struct vm_area_struct *vma;
  125. int fault, mask;
  126. vma = find_vma(mm, addr);
  127. fault = -2; /* bad map area */
  128. if (!vma)
  129. goto out;
  130. if (vma->vm_start > addr)
  131. goto check_stack;
  132. /*
  133. * Ok, we have a good vm_area for this
  134. * memory access, so we can handle it.
  135. */
  136. good_area:
  137. if (READ_FAULT(fsr)) /* read? */
  138. mask = VM_READ|VM_EXEC;
  139. else
  140. mask = VM_WRITE;
  141. fault = -1; /* bad access type */
  142. if (!(vma->vm_flags & mask))
  143. goto out;
  144. /*
  145. * If for any reason at all we couldn't handle
  146. * the fault, make sure we exit gracefully rather
  147. * than endlessly redo the fault.
  148. */
  149. survive:
  150. fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, DO_COW(fsr));
  151. /*
  152. * Handle the "normal" cases first - successful and sigbus
  153. */
  154. switch (fault) {
  155. case VM_FAULT_MAJOR:
  156. tsk->maj_flt++;
  157. return fault;
  158. case VM_FAULT_MINOR:
  159. tsk->min_flt++;
  160. case VM_FAULT_SIGBUS:
  161. return fault;
  162. }
  163. fault = -3; /* out of memory */
  164. if (tsk->pid != 1)
  165. goto out;
  166. /*
  167. * If we are out of memory for pid1,
  168. * sleep for a while and retry
  169. */
  170. yield();
  171. goto survive;
  172. check_stack:
  173. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  174. goto good_area;
  175. out:
  176. return fault;
  177. }
  178. int do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  179. {
  180. struct task_struct *tsk;
  181. struct mm_struct *mm;
  182. int fault;
  183. tsk = current;
  184. mm = tsk->mm;
  185. /*
  186. * If we're in an interrupt or have no user
  187. * context, we must not take the fault..
  188. */
  189. if (in_interrupt() || !mm)
  190. goto no_context;
  191. down_read(&mm->mmap_sem);
  192. fault = __do_page_fault(mm, addr, fsr, tsk);
  193. up_read(&mm->mmap_sem);
  194. /*
  195. * Handle the "normal" case first
  196. */
  197. switch (fault) {
  198. case VM_FAULT_MINOR:
  199. case VM_FAULT_MAJOR:
  200. return 0;
  201. case VM_FAULT_SIGBUS:
  202. goto do_sigbus;
  203. }
  204. /*
  205. * If we are in kernel mode at this point, we
  206. * have no context to handle this fault with.
  207. * FIXME - is this test right?
  208. */
  209. if (!user_mode(regs)){
  210. goto no_context;
  211. }
  212. if (fault == -3) {
  213. /*
  214. * We ran out of memory, or some other thing happened to
  215. * us that made us unable to handle the page fault gracefully.
  216. */
  217. printk("VM: killing process %s\n", tsk->comm);
  218. do_exit(SIGKILL);
  219. }
  220. else{
  221. __do_user_fault(tsk, addr, fsr, fault == -1 ? SEGV_ACCERR : SEGV_MAPERR, regs);
  222. }
  223. return 0;
  224. /*
  225. * We ran out of memory, or some other thing happened to us that made
  226. * us unable to handle the page fault gracefully.
  227. */
  228. do_sigbus:
  229. /*
  230. * Send a sigbus, regardless of whether we were in kernel
  231. * or user mode.
  232. */
  233. tsk->thread.address = addr; //FIXME - need other bits setting?
  234. tsk->thread.error_code = fsr;
  235. tsk->thread.trap_no = 14;
  236. force_sig(SIGBUS, tsk);
  237. #ifdef CONFIG_DEBUG_USER
  238. printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lx\n",
  239. current->comm, addr, instruction_pointer(regs));
  240. #endif
  241. /* Kernel mode? Handle exceptions or die */
  242. if (user_mode(regs))
  243. return 0;
  244. no_context:
  245. __do_kernel_fault(mm, addr, fsr, regs);
  246. return 0;
  247. }
  248. /*
  249. * Handle a data abort. Note that we have to handle a range of addresses
  250. * on ARM2/3 for ldm. If both pages are zero-mapped, then we have to force
  251. * a copy-on-write. However, on the second page, we always force COW.
  252. */
  253. asmlinkage void
  254. do_DataAbort(unsigned long min_addr, unsigned long max_addr, int mode, struct pt_regs *regs)
  255. {
  256. do_page_fault(min_addr, mode, regs);
  257. if ((min_addr ^ max_addr) >> PAGE_SHIFT){
  258. do_page_fault(max_addr, mode | FAULT_CODE_FORCECOW, regs);
  259. }
  260. }
  261. asmlinkage int
  262. do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
  263. {
  264. #if 0
  265. if (the memc mapping for this page exists) {
  266. printk ("Page in, but got abort (undefined instruction?)\n");
  267. return 0;
  268. }
  269. #endif
  270. do_page_fault(addr, FAULT_CODE_PREFETCH, regs);
  271. return 1;
  272. }