fault.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Low-level SPU handling
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/sched.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <asm/spu.h>
  26. #include <asm/spu_csa.h>
  27. #include "spufs.h"
  28. /*
  29. * This ought to be kept in sync with the powerpc specific do_page_fault
  30. * function. Currently, there are a few corner cases that we haven't had
  31. * to handle fortunately.
  32. */
  33. static int spu_handle_mm_fault(struct mm_struct *mm, unsigned long ea, unsigned long dsisr)
  34. {
  35. struct vm_area_struct *vma;
  36. unsigned long is_write;
  37. int ret;
  38. #if 0
  39. if (!IS_VALID_EA(ea)) {
  40. return -EFAULT;
  41. }
  42. #endif /* XXX */
  43. if (mm == NULL) {
  44. return -EFAULT;
  45. }
  46. if (mm->pgd == NULL) {
  47. return -EFAULT;
  48. }
  49. down_read(&mm->mmap_sem);
  50. vma = find_vma(mm, ea);
  51. if (!vma)
  52. goto bad_area;
  53. if (vma->vm_start <= ea)
  54. goto good_area;
  55. if (!(vma->vm_flags & VM_GROWSDOWN))
  56. goto bad_area;
  57. if (expand_stack(vma, ea))
  58. goto bad_area;
  59. good_area:
  60. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  61. if (is_write) {
  62. if (!(vma->vm_flags & VM_WRITE))
  63. goto bad_area;
  64. } else {
  65. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  66. goto bad_area;
  67. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  68. goto bad_area;
  69. }
  70. ret = 0;
  71. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  72. case VM_FAULT_MINOR:
  73. current->min_flt++;
  74. break;
  75. case VM_FAULT_MAJOR:
  76. current->maj_flt++;
  77. break;
  78. case VM_FAULT_SIGBUS:
  79. ret = -EFAULT;
  80. goto bad_area;
  81. case VM_FAULT_OOM:
  82. ret = -ENOMEM;
  83. goto bad_area;
  84. default:
  85. BUG();
  86. }
  87. up_read(&mm->mmap_sem);
  88. return ret;
  89. bad_area:
  90. up_read(&mm->mmap_sem);
  91. return -EFAULT;
  92. }
  93. static void spufs_handle_dma_error(struct spu_context *ctx,
  94. unsigned long ea, int type)
  95. {
  96. if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
  97. ctx->event_return |= type;
  98. wake_up_all(&ctx->stop_wq);
  99. } else {
  100. siginfo_t info;
  101. memset(&info, 0, sizeof(info));
  102. switch (type) {
  103. case SPE_EVENT_INVALID_DMA:
  104. info.si_signo = SIGBUS;
  105. info.si_code = BUS_OBJERR;
  106. break;
  107. case SPE_EVENT_SPE_DATA_STORAGE:
  108. info.si_signo = SIGBUS;
  109. info.si_addr = (void __user *)ea;
  110. info.si_code = BUS_ADRERR;
  111. break;
  112. case SPE_EVENT_DMA_ALIGNMENT:
  113. info.si_signo = SIGBUS;
  114. /* DAR isn't set for an alignment fault :( */
  115. info.si_code = BUS_ADRALN;
  116. break;
  117. case SPE_EVENT_SPE_ERROR:
  118. info.si_signo = SIGILL;
  119. info.si_addr = (void __user *)(unsigned long)
  120. ctx->ops->npc_read(ctx) - 4;
  121. info.si_code = ILL_ILLOPC;
  122. break;
  123. }
  124. if (info.si_signo)
  125. force_sig_info(info.si_signo, &info, current);
  126. }
  127. }
  128. void spufs_dma_callback(struct spu *spu, int type)
  129. {
  130. spufs_handle_dma_error(spu->ctx, spu->dar, type);
  131. }
  132. EXPORT_SYMBOL_GPL(spufs_dma_callback);
  133. /*
  134. * bottom half handler for page faults, we can't do this from
  135. * interrupt context, since we might need to sleep.
  136. * we also need to give up the mutex so we can get scheduled
  137. * out while waiting for the backing store.
  138. *
  139. * TODO: try calling hash_page from the interrupt handler first
  140. * in order to speed up the easy case.
  141. */
  142. int spufs_handle_class1(struct spu_context *ctx)
  143. {
  144. u64 ea, dsisr, access;
  145. unsigned long flags;
  146. int ret;
  147. /*
  148. * dar and dsisr get passed from the registers
  149. * to the spu_context, to this function, but not
  150. * back to the spu if it gets scheduled again.
  151. *
  152. * if we don't handle the fault for a saved context
  153. * in time, we can still expect to get the same fault
  154. * the immediately after the context restore.
  155. */
  156. if (ctx->state == SPU_STATE_RUNNABLE) {
  157. ea = ctx->spu->dar;
  158. dsisr = ctx->spu->dsisr;
  159. ctx->spu->dar= ctx->spu->dsisr = 0;
  160. } else {
  161. ea = ctx->csa.priv1.mfc_dar_RW;
  162. dsisr = ctx->csa.priv1.mfc_dsisr_RW;
  163. ctx->csa.priv1.mfc_dar_RW = 0;
  164. ctx->csa.priv1.mfc_dsisr_RW = 0;
  165. }
  166. if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
  167. return 0;
  168. pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
  169. dsisr, ctx->state);
  170. /* we must not hold the lock when entering spu_handle_mm_fault */
  171. spu_release(ctx);
  172. access = (_PAGE_PRESENT | _PAGE_USER);
  173. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  174. local_irq_save(flags);
  175. ret = hash_page(ea, access, 0x300);
  176. local_irq_restore(flags);
  177. /* hashing failed, so try the actual fault handler */
  178. if (ret)
  179. ret = spu_handle_mm_fault(current->mm, ea, dsisr);
  180. spu_acquire(ctx);
  181. /*
  182. * If we handled the fault successfully and are in runnable
  183. * state, restart the DMA.
  184. * In case of unhandled error report the problem to user space.
  185. */
  186. if (!ret) {
  187. if (ctx->spu)
  188. ctx->ops->restart_dma(ctx);
  189. } else
  190. spufs_handle_dma_error(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
  191. return ret;
  192. }
  193. EXPORT_SYMBOL_GPL(spufs_handle_class1);