run.c 10 KB

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