fault.c 7.0 KB

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