fault.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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,
  34. unsigned long dsisr, unsigned *flt)
  35. {
  36. struct vm_area_struct *vma;
  37. unsigned long is_write;
  38. int ret;
  39. #if 0
  40. if (!IS_VALID_EA(ea)) {
  41. return -EFAULT;
  42. }
  43. #endif /* XXX */
  44. if (mm == NULL) {
  45. return -EFAULT;
  46. }
  47. if (mm->pgd == NULL) {
  48. return -EFAULT;
  49. }
  50. down_read(&mm->mmap_sem);
  51. vma = find_vma(mm, ea);
  52. if (!vma)
  53. goto bad_area;
  54. if (vma->vm_start <= ea)
  55. goto good_area;
  56. if (!(vma->vm_flags & VM_GROWSDOWN))
  57. goto bad_area;
  58. if (expand_stack(vma, ea))
  59. goto bad_area;
  60. good_area:
  61. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  62. if (is_write) {
  63. if (!(vma->vm_flags & VM_WRITE))
  64. goto bad_area;
  65. } else {
  66. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  67. goto bad_area;
  68. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  69. goto bad_area;
  70. }
  71. ret = 0;
  72. *flt = handle_mm_fault(mm, vma, ea, is_write);
  73. if (unlikely(*flt & VM_FAULT_ERROR)) {
  74. if (*flt & VM_FAULT_OOM) {
  75. ret = -ENOMEM;
  76. goto bad_area;
  77. } else if (*flt & VM_FAULT_SIGBUS) {
  78. ret = -EFAULT;
  79. goto bad_area;
  80. }
  81. BUG();
  82. }
  83. if (*flt & VM_FAULT_MAJOR)
  84. current->maj_flt++;
  85. else
  86. current->min_flt++;
  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. unsigned flt = 0;
  147. int ret;
  148. /*
  149. * dar and dsisr get passed from the registers
  150. * to the spu_context, to this function, but not
  151. * back to the spu if it gets scheduled again.
  152. *
  153. * if we don't handle the fault for a saved context
  154. * in time, we can still expect to get the same fault
  155. * the immediately after the context restore.
  156. */
  157. if (ctx->state == SPU_STATE_RUNNABLE) {
  158. ea = ctx->spu->dar;
  159. dsisr = ctx->spu->dsisr;
  160. ctx->spu->dar= ctx->spu->dsisr = 0;
  161. } else {
  162. ea = ctx->csa.priv1.mfc_dar_RW;
  163. dsisr = ctx->csa.priv1.mfc_dsisr_RW;
  164. ctx->csa.priv1.mfc_dar_RW = 0;
  165. ctx->csa.priv1.mfc_dsisr_RW = 0;
  166. }
  167. if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
  168. return 0;
  169. spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
  170. pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
  171. dsisr, ctx->state);
  172. ctx->stats.hash_flt++;
  173. if (ctx->state == SPU_STATE_RUNNABLE)
  174. ctx->spu->stats.hash_flt++;
  175. /* we must not hold the lock when entering spu_handle_mm_fault */
  176. spu_release(ctx);
  177. access = (_PAGE_PRESENT | _PAGE_USER);
  178. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  179. local_irq_save(flags);
  180. ret = hash_page(ea, access, 0x300);
  181. local_irq_restore(flags);
  182. /* hashing failed, so try the actual fault handler */
  183. if (ret)
  184. ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
  185. spu_acquire(ctx);
  186. /*
  187. * If we handled the fault successfully and are in runnable
  188. * state, restart the DMA.
  189. * In case of unhandled error report the problem to user space.
  190. */
  191. if (!ret) {
  192. if (flt & VM_FAULT_MAJOR)
  193. ctx->stats.maj_flt++;
  194. else
  195. ctx->stats.min_flt++;
  196. if (ctx->state == SPU_STATE_RUNNABLE) {
  197. if (flt & VM_FAULT_MAJOR)
  198. ctx->spu->stats.maj_flt++;
  199. else
  200. ctx->spu->stats.min_flt++;
  201. }
  202. if (ctx->spu)
  203. ctx->ops->restart_dma(ctx);
  204. } else
  205. spufs_handle_dma_error(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
  206. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  207. return ret;
  208. }
  209. EXPORT_SYMBOL_GPL(spufs_handle_class1);