fault.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <linux/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. fix = search_exception_tables(regs->iaoq[0]);
  127. if (fix) {
  128. struct exception_data *d;
  129. d = this_cpu_ptr(&exception_data);
  130. d->fault_ip = regs->iaoq[0];
  131. d->fault_gp = regs->gr[27];
  132. d->fault_space = regs->isr;
  133. d->fault_addr = regs->ior;
  134. regs->iaoq[0] = (unsigned long)&fix->fixup + fix->fixup;
  135. regs->iaoq[0] &= ~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. /*
  150. * Print out info about fatal segfaults, if the show_unhandled_signals
  151. * sysctl is set:
  152. */
  153. static inline void
  154. show_signal_msg(struct pt_regs *regs, unsigned long code,
  155. unsigned long address, struct task_struct *tsk,
  156. struct vm_area_struct *vma)
  157. {
  158. if (!unhandled_signal(tsk, SIGSEGV))
  159. return;
  160. if (!printk_ratelimit())
  161. return;
  162. pr_warn("\n");
  163. pr_warn("do_page_fault() command='%s' type=%lu address=0x%08lx",
  164. tsk->comm, code, address);
  165. print_vma_addr(KERN_CONT " in ", regs->iaoq[0]);
  166. if (vma)
  167. pr_warn(" vm_start = 0x%08lx, vm_end = 0x%08lx\n",
  168. vma->vm_start, vma->vm_end);
  169. show_regs(regs);
  170. }
  171. void do_page_fault(struct pt_regs *regs, unsigned long code,
  172. unsigned long address)
  173. {
  174. struct vm_area_struct *vma, *prev_vma;
  175. struct task_struct *tsk;
  176. struct mm_struct *mm;
  177. unsigned long acc_type;
  178. int fault;
  179. unsigned int flags;
  180. if (faulthandler_disabled())
  181. goto no_context;
  182. tsk = current;
  183. mm = tsk->mm;
  184. if (!mm)
  185. goto no_context;
  186. flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  187. if (user_mode(regs))
  188. flags |= FAULT_FLAG_USER;
  189. acc_type = parisc_acctyp(code, regs->iir);
  190. if (acc_type & VM_WRITE)
  191. flags |= FAULT_FLAG_WRITE;
  192. retry:
  193. down_read(&mm->mmap_sem);
  194. vma = find_vma_prev(mm, address, &prev_vma);
  195. if (!vma || address < vma->vm_start)
  196. goto check_expansion;
  197. /*
  198. * Ok, we have a good vm_area for this memory access. We still need to
  199. * check the access permissions.
  200. */
  201. good_area:
  202. if ((vma->vm_flags & acc_type) != acc_type)
  203. goto bad_area;
  204. /*
  205. * If for any reason at all we couldn't handle the fault, make
  206. * sure we exit gracefully rather than endlessly redo the
  207. * fault.
  208. */
  209. fault = handle_mm_fault(vma, address, flags);
  210. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  211. return;
  212. if (unlikely(fault & VM_FAULT_ERROR)) {
  213. /*
  214. * We hit a shared mapping outside of the file, or some
  215. * other thing happened to us that made us unable to
  216. * handle the page fault gracefully.
  217. */
  218. if (fault & VM_FAULT_OOM)
  219. goto out_of_memory;
  220. else if (fault & VM_FAULT_SIGSEGV)
  221. goto bad_area;
  222. else if (fault & VM_FAULT_SIGBUS)
  223. goto bad_area;
  224. BUG();
  225. }
  226. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  227. if (fault & VM_FAULT_MAJOR)
  228. current->maj_flt++;
  229. else
  230. current->min_flt++;
  231. if (fault & VM_FAULT_RETRY) {
  232. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  233. /*
  234. * No need to up_read(&mm->mmap_sem) as we would
  235. * have already released it in __lock_page_or_retry
  236. * in mm/filemap.c.
  237. */
  238. goto retry;
  239. }
  240. }
  241. up_read(&mm->mmap_sem);
  242. return;
  243. check_expansion:
  244. vma = prev_vma;
  245. if (vma && (expand_stack(vma, address) == 0))
  246. goto good_area;
  247. /*
  248. * Something tried to access memory that isn't in our memory map..
  249. */
  250. bad_area:
  251. up_read(&mm->mmap_sem);
  252. if (user_mode(regs)) {
  253. struct siginfo si;
  254. show_signal_msg(regs, code, address, tsk, vma);
  255. switch (code) {
  256. case 15: /* Data TLB miss fault/Data page fault */
  257. /* send SIGSEGV when outside of vma */
  258. if (!vma ||
  259. address < vma->vm_start || address > vma->vm_end) {
  260. si.si_signo = SIGSEGV;
  261. si.si_code = SEGV_MAPERR;
  262. break;
  263. }
  264. /* send SIGSEGV for wrong permissions */
  265. if ((vma->vm_flags & acc_type) != acc_type) {
  266. si.si_signo = SIGSEGV;
  267. si.si_code = SEGV_ACCERR;
  268. break;
  269. }
  270. /* probably address is outside of mapped file */
  271. /* fall through */
  272. case 17: /* NA data TLB miss / page fault */
  273. case 18: /* Unaligned access - PCXS only */
  274. si.si_signo = SIGBUS;
  275. si.si_code = (code == 18) ? BUS_ADRALN : BUS_ADRERR;
  276. break;
  277. case 16: /* Non-access instruction TLB miss fault */
  278. case 26: /* PCXL: Data memory access rights trap */
  279. default:
  280. si.si_signo = SIGSEGV;
  281. si.si_code = (code == 26) ? SEGV_ACCERR : SEGV_MAPERR;
  282. break;
  283. }
  284. si.si_errno = 0;
  285. si.si_addr = (void __user *) address;
  286. force_sig_info(si.si_signo, &si, current);
  287. return;
  288. }
  289. no_context:
  290. if (!user_mode(regs) && fixup_exception(regs)) {
  291. return;
  292. }
  293. parisc_terminate("Bad Address (null pointer deref?)", regs, code, address);
  294. out_of_memory:
  295. up_read(&mm->mmap_sem);
  296. if (!user_mode(regs))
  297. goto no_context;
  298. pagefault_out_of_memory();
  299. }