run.c 9.7 KB

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