fault.c 7.1 KB

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