fault.c 7.0 KB

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