fault.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. *
  7. * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
  8. * Copyright 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
  9. * Copyright 1999 Hewlett Packard Co.
  10. *
  11. */
  12. #include <linux/mm.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/traps.h>
  19. #define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
  20. /* dumped to the console via printk) */
  21. /* Various important other fields */
  22. #define bit22set(x) (x & 0x00000200)
  23. #define bits23_25set(x) (x & 0x000001c0)
  24. #define isGraphicsFlushRead(x) ((x & 0xfc003fdf) == 0x04001a80)
  25. /* extended opcode is 0x6a */
  26. #define BITSSET 0x1c0 /* for identifying LDCW */
  27. DEFINE_PER_CPU(struct exception_data, exception_data);
  28. /*
  29. * parisc_acctyp(unsigned int inst) --
  30. * Given a PA-RISC memory access instruction, determine if the
  31. * the instruction would perform a memory read or memory write
  32. * operation.
  33. *
  34. * This function assumes that the given instruction is a memory access
  35. * instruction (i.e. you should really only call it if you know that
  36. * the instruction has generated some sort of a memory access fault).
  37. *
  38. * Returns:
  39. * VM_READ if read operation
  40. * VM_WRITE if write operation
  41. * VM_EXEC if execute operation
  42. */
  43. static unsigned long
  44. parisc_acctyp(unsigned long code, unsigned int inst)
  45. {
  46. if (code == 6 || code == 16)
  47. return VM_EXEC;
  48. switch (inst & 0xf0000000) {
  49. case 0x40000000: /* load */
  50. case 0x50000000: /* new load */
  51. return VM_READ;
  52. case 0x60000000: /* store */
  53. case 0x70000000: /* new store */
  54. return VM_WRITE;
  55. case 0x20000000: /* coproc */
  56. case 0x30000000: /* coproc2 */
  57. if (bit22set(inst))
  58. return VM_WRITE;
  59. case 0x0: /* indexed/memory management */
  60. if (bit22set(inst)) {
  61. /*
  62. * Check for the 'Graphics Flush Read' instruction.
  63. * It resembles an FDC instruction, except for bits
  64. * 20 and 21. Any combination other than zero will
  65. * utilize the block mover functionality on some
  66. * older PA-RISC platforms. The case where a block
  67. * move is performed from VM to graphics IO space
  68. * should be treated as a READ.
  69. *
  70. * The significance of bits 20,21 in the FDC
  71. * instruction is:
  72. *
  73. * 00 Flush data cache (normal instruction behavior)
  74. * 01 Graphics flush write (IO space -> VM)
  75. * 10 Graphics flush read (VM -> IO space)
  76. * 11 Graphics flush read/write (VM <-> IO space)
  77. */
  78. if (isGraphicsFlushRead(inst))
  79. return VM_READ;
  80. return VM_WRITE;
  81. } else {
  82. /*
  83. * Check for LDCWX and LDCWS (semaphore instructions).
  84. * If bits 23 through 25 are all 1's it is one of
  85. * the above two instructions and is a write.
  86. *
  87. * Note: With the limited bits we are looking at,
  88. * this will also catch PROBEW and PROBEWI. However,
  89. * these should never get in here because they don't
  90. * generate exceptions of the type:
  91. * Data TLB miss fault/data page fault
  92. * Data memory protection trap
  93. */
  94. if (bits23_25set(inst) == BITSSET)
  95. return VM_WRITE;
  96. }
  97. return VM_READ; /* Default */
  98. }
  99. return VM_READ; /* Default */
  100. }
  101. #undef bit22set
  102. #undef bits23_25set
  103. #undef isGraphicsFlushRead
  104. #undef BITSSET
  105. #if 0
  106. /* This is the treewalk to find a vma which is the highest that has
  107. * a start < addr. We're using find_vma_prev instead right now, but
  108. * we might want to use this at some point in the future. Probably
  109. * not, but I want it committed to CVS so I don't lose it :-)
  110. */
  111. while (tree != vm_avl_empty) {
  112. if (tree->vm_start > addr) {
  113. tree = tree->vm_avl_left;
  114. } else {
  115. prev = tree;
  116. if (prev->vm_next == NULL)
  117. break;
  118. if (prev->vm_next->vm_start > addr)
  119. break;
  120. tree = tree->vm_avl_right;
  121. }
  122. }
  123. #endif
  124. int fixup_exception(struct pt_regs *regs)
  125. {
  126. const struct exception_table_entry *fix;
  127. /* If we only stored 32bit addresses in the exception table we can drop
  128. * out if we faulted on a 64bit address. */
  129. if ((sizeof(regs->iaoq[0]) > sizeof(fix->insn))
  130. && (regs->iaoq[0] >> 32))
  131. return 0;
  132. fix = search_exception_tables(regs->iaoq[0]);
  133. if (fix) {
  134. struct exception_data *d;
  135. d = &__get_cpu_var(exception_data);
  136. d->fault_ip = regs->iaoq[0];
  137. d->fault_space = regs->isr;
  138. d->fault_addr = regs->ior;
  139. regs->iaoq[0] = ((fix->fixup) & ~3);
  140. /*
  141. * NOTE: In some cases the faulting instruction
  142. * may be in the delay slot of a branch. We
  143. * don't want to take the branch, so we don't
  144. * increment iaoq[1], instead we set it to be
  145. * iaoq[0]+4, and clear the B bit in the PSW
  146. */
  147. regs->iaoq[1] = regs->iaoq[0] + 4;
  148. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. void do_page_fault(struct pt_regs *regs, unsigned long code,
  154. unsigned long address)
  155. {
  156. struct vm_area_struct *vma, *prev_vma;
  157. struct task_struct *tsk;
  158. struct mm_struct *mm;
  159. unsigned long acc_type;
  160. int fault;
  161. unsigned int flags;
  162. if (in_atomic())
  163. goto no_context;
  164. tsk = current;
  165. mm = tsk->mm;
  166. if (!mm)
  167. goto no_context;
  168. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  169. if (user_mode(regs))
  170. flags |= FAULT_FLAG_USER;
  171. acc_type = parisc_acctyp(code, regs->iir);
  172. if (acc_type & VM_WRITE)
  173. flags |= FAULT_FLAG_WRITE;
  174. retry:
  175. down_read(&mm->mmap_sem);
  176. vma = find_vma_prev(mm, address, &prev_vma);
  177. if (!vma || address < vma->vm_start)
  178. goto check_expansion;
  179. /*
  180. * Ok, we have a good vm_area for this memory access. We still need to
  181. * check the access permissions.
  182. */
  183. good_area:
  184. if ((vma->vm_flags & acc_type) != acc_type)
  185. goto bad_area;
  186. /*
  187. * If for any reason at all we couldn't handle the fault, make
  188. * sure we exit gracefully rather than endlessly redo the
  189. * fault.
  190. */
  191. fault = handle_mm_fault(mm, vma, address, flags);
  192. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  193. return;
  194. if (unlikely(fault & VM_FAULT_ERROR)) {
  195. /*
  196. * We hit a shared mapping outside of the file, or some
  197. * other thing happened to us that made us unable to
  198. * handle the page fault gracefully.
  199. */
  200. if (fault & VM_FAULT_OOM)
  201. goto out_of_memory;
  202. else if (fault & VM_FAULT_SIGBUS)
  203. goto bad_area;
  204. BUG();
  205. }
  206. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  207. if (fault & VM_FAULT_MAJOR)
  208. current->maj_flt++;
  209. else
  210. current->min_flt++;
  211. if (fault & VM_FAULT_RETRY) {
  212. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  213. /*
  214. * No need to up_read(&mm->mmap_sem) as we would
  215. * have already released it in __lock_page_or_retry
  216. * in mm/filemap.c.
  217. */
  218. goto retry;
  219. }
  220. }
  221. up_read(&mm->mmap_sem);
  222. return;
  223. check_expansion:
  224. vma = prev_vma;
  225. if (vma && (expand_stack(vma, address) == 0))
  226. goto good_area;
  227. /*
  228. * Something tried to access memory that isn't in our memory map..
  229. */
  230. bad_area:
  231. up_read(&mm->mmap_sem);
  232. if (user_mode(regs)) {
  233. struct siginfo si;
  234. #ifdef PRINT_USER_FAULTS
  235. printk(KERN_DEBUG "\n");
  236. printk(KERN_DEBUG "do_page_fault() pid=%d command='%s' type=%lu address=0x%08lx\n",
  237. task_pid_nr(tsk), tsk->comm, code, address);
  238. if (vma) {
  239. printk(KERN_DEBUG "vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  240. vma->vm_start, vma->vm_end);
  241. }
  242. show_regs(regs);
  243. #endif
  244. switch (code) {
  245. case 15: /* Data TLB miss fault/Data page fault */
  246. case 17: /* NA data TLB miss / page fault */
  247. case 18: /* Unaligned access - PCXS only */
  248. si.si_signo = SIGBUS;
  249. si.si_code = BUS_ADRERR;
  250. break;
  251. case 16: /* Non-access instruction TLB miss fault */
  252. case 26: /* PCXL: Data memory access rights trap */
  253. default:
  254. si.si_signo = SIGSEGV;
  255. si.si_code = SEGV_MAPERR;
  256. }
  257. si.si_errno = 0;
  258. si.si_addr = (void __user *) address;
  259. force_sig_info(si.si_signo, &si, current);
  260. return;
  261. }
  262. no_context:
  263. if (!user_mode(regs) && fixup_exception(regs)) {
  264. return;
  265. }
  266. parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
  267. out_of_memory:
  268. up_read(&mm->mmap_sem);
  269. if (!user_mode(regs))
  270. goto no_context;
  271. pagefault_out_of_memory();
  272. }