run.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #define DEBUG
  2. #include <linux/wait.h>
  3. #include <linux/ptrace.h>
  4. #include <asm/spu.h>
  5. #include <asm/spu_priv1.h>
  6. #include <asm/io.h>
  7. #include <asm/unistd.h>
  8. #include "spufs.h"
  9. /* interrupt-level stop callback function. */
  10. void spufs_stop_callback(struct spu *spu)
  11. {
  12. struct spu_context *ctx = spu->ctx;
  13. /*
  14. * It should be impossible to preempt a context while an exception
  15. * is being processed, since the context switch code is specially
  16. * coded to deal with interrupts ... But, just in case, sanity check
  17. * the context pointer. It is OK to return doing nothing since
  18. * the exception will be regenerated when the context is resumed.
  19. */
  20. if (ctx) {
  21. /* Copy exception arguments into module specific structure */
  22. ctx->csa.class_0_pending = spu->class_0_pending;
  23. ctx->csa.dsisr = spu->dsisr;
  24. ctx->csa.dar = spu->dar;
  25. /* ensure that the exception status has hit memory before a
  26. * thread waiting on the context's stop queue is woken */
  27. smp_wmb();
  28. wake_up_all(&ctx->stop_wq);
  29. }
  30. /* Clear callback arguments from spu structure */
  31. spu->class_0_pending = 0;
  32. spu->dsisr = 0;
  33. spu->dar = 0;
  34. }
  35. int spu_stopped(struct spu_context *ctx, u32 *stat)
  36. {
  37. u64 dsisr;
  38. u32 stopped;
  39. *stat = ctx->ops->status_read(ctx);
  40. if (test_bit(SPU_SCHED_NOTIFY_ACTIVE, &ctx->sched_flags))
  41. return 1;
  42. stopped = SPU_STATUS_INVALID_INSTR | SPU_STATUS_SINGLE_STEP |
  43. SPU_STATUS_STOPPED_BY_HALT | SPU_STATUS_STOPPED_BY_STOP;
  44. if (!(*stat & SPU_STATUS_RUNNING) && (*stat & stopped))
  45. return 1;
  46. dsisr = ctx->csa.dsisr;
  47. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED))
  48. return 1;
  49. if (ctx->csa.class_0_pending)
  50. return 1;
  51. return 0;
  52. }
  53. static int spu_setup_isolated(struct spu_context *ctx)
  54. {
  55. int ret;
  56. u64 __iomem *mfc_cntl;
  57. u64 sr1;
  58. u32 status;
  59. unsigned long timeout;
  60. const u32 status_loading = SPU_STATUS_RUNNING
  61. | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
  62. ret = -ENODEV;
  63. if (!isolated_loader)
  64. goto out;
  65. /*
  66. * We need to exclude userspace access to the context.
  67. *
  68. * To protect against memory access we invalidate all ptes
  69. * and make sure the pagefault handlers block on the mutex.
  70. */
  71. spu_unmap_mappings(ctx);
  72. mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
  73. /* purge the MFC DMA queue to ensure no spurious accesses before we
  74. * enter kernel mode */
  75. timeout = jiffies + HZ;
  76. out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
  77. while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
  78. != MFC_CNTL_PURGE_DMA_COMPLETE) {
  79. if (time_after(jiffies, timeout)) {
  80. printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
  81. __func__);
  82. ret = -EIO;
  83. goto out;
  84. }
  85. cond_resched();
  86. }
  87. /* put the SPE in kernel mode to allow access to the loader */
  88. sr1 = spu_mfc_sr1_get(ctx->spu);
  89. sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
  90. spu_mfc_sr1_set(ctx->spu, sr1);
  91. /* start the loader */
  92. ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
  93. ctx->ops->signal2_write(ctx,
  94. (unsigned long)isolated_loader & 0xffffffff);
  95. ctx->ops->runcntl_write(ctx,
  96. SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  97. ret = 0;
  98. timeout = jiffies + HZ;
  99. while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
  100. status_loading) {
  101. if (time_after(jiffies, timeout)) {
  102. printk(KERN_ERR "%s: timeout waiting for loader\n",
  103. __func__);
  104. ret = -EIO;
  105. goto out_drop_priv;
  106. }
  107. cond_resched();
  108. }
  109. if (!(status & SPU_STATUS_RUNNING)) {
  110. /* If isolated LOAD has failed: run SPU, we will get a stop-and
  111. * signal later. */
  112. pr_debug("%s: isolated LOAD failed\n", __func__);
  113. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  114. ret = -EACCES;
  115. goto out_drop_priv;
  116. }
  117. if (!(status & SPU_STATUS_ISOLATED_STATE)) {
  118. /* This isn't allowed by the CBEA, but check anyway */
  119. pr_debug("%s: SPU fell out of isolated mode?\n", __func__);
  120. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
  121. ret = -EINVAL;
  122. goto out_drop_priv;
  123. }
  124. out_drop_priv:
  125. /* Finished accessing the loader. Drop kernel mode */
  126. sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
  127. spu_mfc_sr1_set(ctx->spu, sr1);
  128. out:
  129. return ret;
  130. }
  131. static int spu_run_init(struct spu_context *ctx, u32 *npc)
  132. {
  133. unsigned long runcntl = SPU_RUNCNTL_RUNNABLE;
  134. int ret;
  135. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  136. /*
  137. * NOSCHED is synchronous scheduling with respect to the caller.
  138. * The caller waits for the context to be loaded.
  139. */
  140. if (ctx->flags & SPU_CREATE_NOSCHED) {
  141. if (ctx->state == SPU_STATE_SAVED) {
  142. ret = spu_activate(ctx, 0);
  143. if (ret)
  144. return ret;
  145. }
  146. }
  147. /*
  148. * Apply special setup as required.
  149. */
  150. if (ctx->flags & SPU_CREATE_ISOLATE) {
  151. if (!(ctx->ops->status_read(ctx) & SPU_STATUS_ISOLATED_STATE)) {
  152. ret = spu_setup_isolated(ctx);
  153. if (ret)
  154. return ret;
  155. }
  156. /*
  157. * If userspace has set the runcntrl register (eg, to
  158. * issue an isolated exit), we need to re-set it here
  159. */
  160. runcntl = ctx->ops->runcntl_read(ctx) &
  161. (SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
  162. if (runcntl == 0)
  163. runcntl = SPU_RUNCNTL_RUNNABLE;
  164. }
  165. if (ctx->flags & SPU_CREATE_NOSCHED) {
  166. spuctx_switch_state(ctx, SPU_UTIL_USER);
  167. ctx->ops->runcntl_write(ctx, runcntl);
  168. } else {
  169. unsigned long privcntl;
  170. if (test_thread_flag(TIF_SINGLESTEP))
  171. privcntl = SPU_PRIVCNTL_MODE_SINGLE_STEP;
  172. else
  173. privcntl = SPU_PRIVCNTL_MODE_NORMAL;
  174. ctx->ops->npc_write(ctx, *npc);
  175. ctx->ops->privcntl_write(ctx, privcntl);
  176. ctx->ops->runcntl_write(ctx, runcntl);
  177. if (ctx->state == SPU_STATE_SAVED) {
  178. ret = spu_activate(ctx, 0);
  179. if (ret)
  180. return ret;
  181. } else {
  182. spuctx_switch_state(ctx, SPU_UTIL_USER);
  183. }
  184. }
  185. set_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
  186. return 0;
  187. }
  188. static int spu_run_fini(struct spu_context *ctx, u32 *npc,
  189. u32 *status)
  190. {
  191. int ret = 0;
  192. spu_del_from_rq(ctx);
  193. *status = ctx->ops->status_read(ctx);
  194. *npc = ctx->ops->npc_read(ctx);
  195. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  196. clear_bit(SPU_SCHED_SPU_RUN, &ctx->sched_flags);
  197. spu_release(ctx);
  198. if (signal_pending(current))
  199. ret = -ERESTARTSYS;
  200. return ret;
  201. }
  202. /*
  203. * SPU syscall restarting is tricky because we violate the basic
  204. * assumption that the signal handler is running on the interrupted
  205. * thread. Here instead, the handler runs on PowerPC user space code,
  206. * while the syscall was called from the SPU.
  207. * This means we can only do a very rough approximation of POSIX
  208. * signal semantics.
  209. */
  210. static int spu_handle_restartsys(struct spu_context *ctx, long *spu_ret,
  211. unsigned int *npc)
  212. {
  213. int ret;
  214. switch (*spu_ret) {
  215. case -ERESTARTSYS:
  216. case -ERESTARTNOINTR:
  217. /*
  218. * Enter the regular syscall restarting for
  219. * sys_spu_run, then restart the SPU syscall
  220. * callback.
  221. */
  222. *npc -= 8;
  223. ret = -ERESTARTSYS;
  224. break;
  225. case -ERESTARTNOHAND:
  226. case -ERESTART_RESTARTBLOCK:
  227. /*
  228. * Restart block is too hard for now, just return -EINTR
  229. * to the SPU.
  230. * ERESTARTNOHAND comes from sys_pause, we also return
  231. * -EINTR from there.
  232. * Assume that we need to be restarted ourselves though.
  233. */
  234. *spu_ret = -EINTR;
  235. ret = -ERESTARTSYS;
  236. break;
  237. default:
  238. printk(KERN_WARNING "%s: unexpected return code %ld\n",
  239. __func__, *spu_ret);
  240. ret = 0;
  241. }
  242. return ret;
  243. }
  244. static int spu_process_callback(struct spu_context *ctx)
  245. {
  246. struct spu_syscall_block s;
  247. u32 ls_pointer, npc;
  248. void __iomem *ls;
  249. long spu_ret;
  250. int ret, ret2;
  251. /* get syscall block from local store */
  252. npc = ctx->ops->npc_read(ctx) & ~3;
  253. ls = (void __iomem *)ctx->ops->get_ls(ctx);
  254. ls_pointer = in_be32(ls + npc);
  255. if (ls_pointer > (LS_SIZE - sizeof(s)))
  256. return -EFAULT;
  257. memcpy_fromio(&s, ls + ls_pointer, sizeof(s));
  258. /* do actual syscall without pinning the spu */
  259. ret = 0;
  260. spu_ret = -ENOSYS;
  261. npc += 4;
  262. if (s.nr_ret < __NR_syscalls) {
  263. spu_release(ctx);
  264. /* do actual system call from here */
  265. spu_ret = spu_sys_callback(&s);
  266. if (spu_ret <= -ERESTARTSYS) {
  267. ret = spu_handle_restartsys(ctx, &spu_ret, &npc);
  268. }
  269. ret2 = spu_acquire(ctx);
  270. if (ret == -ERESTARTSYS)
  271. return ret;
  272. if (ret2)
  273. return -EINTR;
  274. }
  275. /* need to re-get the ls, as it may have changed when we released the
  276. * spu */
  277. ls = (void __iomem *)ctx->ops->get_ls(ctx);
  278. /* write result, jump over indirect pointer */
  279. memcpy_toio(ls + ls_pointer, &spu_ret, sizeof(spu_ret));
  280. ctx->ops->npc_write(ctx, npc);
  281. ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
  282. return ret;
  283. }
  284. long spufs_run_spu(struct spu_context *ctx, u32 *npc, u32 *event)
  285. {
  286. int ret;
  287. struct spu *spu;
  288. u32 status;
  289. if (mutex_lock_interruptible(&ctx->run_mutex))
  290. return -ERESTARTSYS;
  291. spu_enable_spu(ctx);
  292. ctx->event_return = 0;
  293. ret = spu_acquire(ctx);
  294. if (ret)
  295. goto out_unlock;
  296. spu_update_sched_info(ctx);
  297. ret = spu_run_init(ctx, npc);
  298. if (ret) {
  299. spu_release(ctx);
  300. goto out;
  301. }
  302. do {
  303. ret = spufs_wait(ctx->stop_wq, spu_stopped(ctx, &status));
  304. if (unlikely(ret)) {
  305. /*
  306. * This is nasty: we need the state_mutex for all the
  307. * bookkeeping even if the syscall was interrupted by
  308. * a signal. ewww.
  309. */
  310. mutex_lock(&ctx->state_mutex);
  311. break;
  312. }
  313. spu = ctx->spu;
  314. if (unlikely(test_and_clear_bit(SPU_SCHED_NOTIFY_ACTIVE,
  315. &ctx->sched_flags))) {
  316. if (!(status & SPU_STATUS_STOPPED_BY_STOP)) {
  317. spu_switch_notify(spu, ctx);
  318. continue;
  319. }
  320. }
  321. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  322. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  323. (status >> SPU_STOP_STATUS_SHIFT == 0x2104)) {
  324. ret = spu_process_callback(ctx);
  325. if (ret)
  326. break;
  327. status &= ~SPU_STATUS_STOPPED_BY_STOP;
  328. }
  329. ret = spufs_handle_class1(ctx);
  330. if (ret)
  331. break;
  332. ret = spufs_handle_class0(ctx);
  333. if (ret)
  334. break;
  335. if (signal_pending(current))
  336. ret = -ERESTARTSYS;
  337. } while (!ret && !(status & (SPU_STATUS_STOPPED_BY_STOP |
  338. SPU_STATUS_STOPPED_BY_HALT |
  339. SPU_STATUS_SINGLE_STEP)));
  340. spu_disable_spu(ctx);
  341. ret = spu_run_fini(ctx, npc, &status);
  342. spu_yield(ctx);
  343. if ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  344. (((status >> SPU_STOP_STATUS_SHIFT) & 0x3f00) == 0x2100))
  345. ctx->stats.libassist++;
  346. if ((ret == 0) ||
  347. ((ret == -ERESTARTSYS) &&
  348. ((status & SPU_STATUS_STOPPED_BY_HALT) ||
  349. (status & SPU_STATUS_SINGLE_STEP) ||
  350. ((status & SPU_STATUS_STOPPED_BY_STOP) &&
  351. (status >> SPU_STOP_STATUS_SHIFT != 0x2104)))))
  352. ret = status;
  353. /* Note: we don't need to force_sig SIGTRAP on single-step
  354. * since we have TIF_SINGLESTEP set, thus the kernel will do
  355. * it upon return from the syscall anyawy
  356. */
  357. if (unlikely(status & SPU_STATUS_SINGLE_STEP))
  358. ret = -ERESTARTSYS;
  359. else if (unlikely((status & SPU_STATUS_STOPPED_BY_STOP)
  360. && (status >> SPU_STOP_STATUS_SHIFT) == 0x3fff)) {
  361. force_sig(SIGTRAP, current);
  362. ret = -ERESTARTSYS;
  363. }
  364. out:
  365. *event = ctx->event_return;
  366. out_unlock:
  367. mutex_unlock(&ctx->run_mutex);
  368. return ret;
  369. }