fault.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. fix = search_exception_tables(regs->iaoq[0]);
  128. if (fix) {
  129. struct exception_data *d;
  130. d = &__get_cpu_var(exception_data);
  131. d->fault_ip = regs->iaoq[0];
  132. d->fault_space = regs->isr;
  133. d->fault_addr = regs->ior;
  134. regs->iaoq[0] = ((fix->fixup) & ~3);
  135. /*
  136. * NOTE: In some cases the faulting instruction
  137. * may be in the delay slot of a branch. We
  138. * don't want to take the branch, so we don't
  139. * increment iaoq[1], instead we set it to be
  140. * iaoq[0]+4, and clear the B bit in the PSW
  141. */
  142. regs->iaoq[1] = regs->iaoq[0] + 4;
  143. regs->gr[0] &= ~PSW_B; /* IPSW in gr[0] */
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. void do_page_fault(struct pt_regs *regs, unsigned long code,
  149. unsigned long address)
  150. {
  151. struct vm_area_struct *vma, *prev_vma;
  152. struct task_struct *tsk = current;
  153. struct mm_struct *mm = tsk->mm;
  154. unsigned long acc_type;
  155. int fault;
  156. if (in_atomic() || !mm)
  157. goto no_context;
  158. down_read(&mm->mmap_sem);
  159. vma = find_vma_prev(mm, address, &prev_vma);
  160. if (!vma || address < vma->vm_start)
  161. goto check_expansion;
  162. /*
  163. * Ok, we have a good vm_area for this memory access. We still need to
  164. * check the access permissions.
  165. */
  166. good_area:
  167. acc_type = parisc_acctyp(code,regs->iir);
  168. if ((vma->vm_flags & acc_type) != acc_type)
  169. goto bad_area;
  170. /*
  171. * If for any reason at all we couldn't handle the fault, make
  172. * sure we exit gracefully rather than endlessly redo the
  173. * fault.
  174. */
  175. fault = handle_mm_fault(mm, vma, address, (acc_type & VM_WRITE) ? FAULT_FLAG_WRITE : 0);
  176. if (unlikely(fault & VM_FAULT_ERROR)) {
  177. /*
  178. * We hit a shared mapping outside of the file, or some
  179. * other thing happened to us that made us unable to
  180. * handle the page fault gracefully.
  181. */
  182. if (fault & VM_FAULT_OOM)
  183. goto out_of_memory;
  184. else if (fault & VM_FAULT_SIGSEGV)
  185. goto bad_area;
  186. else if (fault & VM_FAULT_SIGBUS)
  187. goto bad_area;
  188. BUG();
  189. }
  190. if (fault & VM_FAULT_MAJOR)
  191. current->maj_flt++;
  192. else
  193. current->min_flt++;
  194. up_read(&mm->mmap_sem);
  195. return;
  196. check_expansion:
  197. vma = prev_vma;
  198. if (vma && (expand_stack(vma, address) == 0))
  199. goto good_area;
  200. /*
  201. * Something tried to access memory that isn't in our memory map..
  202. */
  203. bad_area:
  204. up_read(&mm->mmap_sem);
  205. if (user_mode(regs)) {
  206. struct siginfo si;
  207. #ifdef PRINT_USER_FAULTS
  208. printk(KERN_DEBUG "\n");
  209. printk(KERN_DEBUG "do_page_fault() pid=%d command='%s' type=%lu address=0x%08lx\n",
  210. task_pid_nr(tsk), tsk->comm, code, address);
  211. if (vma) {
  212. printk(KERN_DEBUG "vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  213. vma->vm_start, vma->vm_end);
  214. }
  215. show_regs(regs);
  216. #endif
  217. /* FIXME: actually we need to get the signo and code correct */
  218. si.si_signo = SIGSEGV;
  219. si.si_errno = 0;
  220. si.si_code = SEGV_MAPERR;
  221. si.si_addr = (void __user *) address;
  222. force_sig_info(SIGSEGV, &si, current);
  223. return;
  224. }
  225. no_context:
  226. if (!user_mode(regs) && fixup_exception(regs)) {
  227. return;
  228. }
  229. parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
  230. out_of_memory:
  231. up_read(&mm->mmap_sem);
  232. if (!user_mode(regs))
  233. goto no_context;
  234. pagefault_out_of_memory();
  235. }