fault.c 7.0 KB

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