fault.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. switch (*flt) {
  74. case VM_FAULT_MINOR:
  75. current->min_flt++;
  76. break;
  77. case VM_FAULT_MAJOR:
  78. current->maj_flt++;
  79. break;
  80. case VM_FAULT_SIGBUS:
  81. ret = -EFAULT;
  82. goto bad_area;
  83. case VM_FAULT_OOM:
  84. ret = -ENOMEM;
  85. goto bad_area;
  86. default:
  87. BUG();
  88. }
  89. up_read(&mm->mmap_sem);
  90. return ret;
  91. bad_area:
  92. up_read(&mm->mmap_sem);
  93. return -EFAULT;
  94. }
  95. static void spufs_handle_dma_error(struct spu_context *ctx,
  96. unsigned long ea, int type)
  97. {
  98. if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
  99. ctx->event_return |= type;
  100. wake_up_all(&ctx->stop_wq);
  101. } else {
  102. siginfo_t info;
  103. memset(&info, 0, sizeof(info));
  104. switch (type) {
  105. case SPE_EVENT_INVALID_DMA:
  106. info.si_signo = SIGBUS;
  107. info.si_code = BUS_OBJERR;
  108. break;
  109. case SPE_EVENT_SPE_DATA_STORAGE:
  110. info.si_signo = SIGBUS;
  111. info.si_addr = (void __user *)ea;
  112. info.si_code = BUS_ADRERR;
  113. break;
  114. case SPE_EVENT_DMA_ALIGNMENT:
  115. info.si_signo = SIGBUS;
  116. /* DAR isn't set for an alignment fault :( */
  117. info.si_code = BUS_ADRALN;
  118. break;
  119. case SPE_EVENT_SPE_ERROR:
  120. info.si_signo = SIGILL;
  121. info.si_addr = (void __user *)(unsigned long)
  122. ctx->ops->npc_read(ctx) - 4;
  123. info.si_code = ILL_ILLOPC;
  124. break;
  125. }
  126. if (info.si_signo)
  127. force_sig_info(info.si_signo, &info, current);
  128. }
  129. }
  130. void spufs_dma_callback(struct spu *spu, int type)
  131. {
  132. spufs_handle_dma_error(spu->ctx, spu->dar, type);
  133. }
  134. EXPORT_SYMBOL_GPL(spufs_dma_callback);
  135. /*
  136. * bottom half handler for page faults, we can't do this from
  137. * interrupt context, since we might need to sleep.
  138. * we also need to give up the mutex so we can get scheduled
  139. * out while waiting for the backing store.
  140. *
  141. * TODO: try calling hash_page from the interrupt handler first
  142. * in order to speed up the easy case.
  143. */
  144. int spufs_handle_class1(struct spu_context *ctx)
  145. {
  146. u64 ea, dsisr, access;
  147. unsigned long flags;
  148. unsigned flt = 0;
  149. int ret;
  150. /*
  151. * dar and dsisr get passed from the registers
  152. * to the spu_context, to this function, but not
  153. * back to the spu if it gets scheduled again.
  154. *
  155. * if we don't handle the fault for a saved context
  156. * in time, we can still expect to get the same fault
  157. * the immediately after the context restore.
  158. */
  159. if (ctx->state == SPU_STATE_RUNNABLE) {
  160. ea = ctx->spu->dar;
  161. dsisr = ctx->spu->dsisr;
  162. ctx->spu->dar= ctx->spu->dsisr = 0;
  163. } else {
  164. ea = ctx->csa.priv1.mfc_dar_RW;
  165. dsisr = ctx->csa.priv1.mfc_dsisr_RW;
  166. ctx->csa.priv1.mfc_dar_RW = 0;
  167. ctx->csa.priv1.mfc_dsisr_RW = 0;
  168. }
  169. if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
  170. return 0;
  171. spuctx_switch_state(ctx, SPUCTX_UTIL_IOWAIT);
  172. pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
  173. dsisr, ctx->state);
  174. ctx->stats.hash_flt++;
  175. if (ctx->state == SPU_STATE_RUNNABLE) {
  176. ctx->spu->stats.hash_flt++;
  177. spu_switch_state(ctx->spu, SPU_UTIL_IOWAIT);
  178. }
  179. /* we must not hold the lock when entering spu_handle_mm_fault */
  180. spu_release(ctx);
  181. access = (_PAGE_PRESENT | _PAGE_USER);
  182. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  183. local_irq_save(flags);
  184. ret = hash_page(ea, access, 0x300);
  185. local_irq_restore(flags);
  186. /* hashing failed, so try the actual fault handler */
  187. if (ret)
  188. ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
  189. spu_acquire(ctx);
  190. /*
  191. * If we handled the fault successfully and are in runnable
  192. * state, restart the DMA.
  193. * In case of unhandled error report the problem to user space.
  194. */
  195. if (!ret) {
  196. if (flt == VM_FAULT_MINOR)
  197. ctx->stats.min_flt++;
  198. else
  199. ctx->stats.maj_flt++;
  200. if (ctx->state == SPU_STATE_RUNNABLE) {
  201. if (flt == VM_FAULT_MINOR)
  202. ctx->spu->stats.min_flt++;
  203. else
  204. ctx->spu->stats.maj_flt++;
  205. }
  206. if (ctx->spu)
  207. ctx->ops->restart_dma(ctx);
  208. } else
  209. spufs_handle_dma_error(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
  210. spuctx_switch_state(ctx, SPUCTX_UTIL_SYSTEM);
  211. return ret;
  212. }
  213. EXPORT_SYMBOL_GPL(spufs_handle_class1);