fault.c 8.7 KB

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