#include <sys/spu.h> /* Definition of SPU_* constants */ #include <sys/syscall.h> /* Definition of SYS_* constants */ #include <unistd.h> int syscall(SYS_spu_run, int fd, uint32_t *npc, uint32_t *event);
Note: glibc provides no wrapper for spu_run(), necessitating the use of syscall(2).
SPU コードの実行は同期的 (synchronously) に行われる、つまり SPU が実行中は spu_run() は停止 (block) する。 SPU コードの実行をメイン CPU や他の SPU と並行して行う必要がある場合は、 最初に、その SPU コードを実行する新しいスレッドを、(例えば pthread_create(3) などを使って) 生成しなければならない。
spu_run() が返るときには、SPU のプログラムカウンターの現在値が npc に書き込まれる。 これにより、連続する spu_run() の呼び出しで同じ npc ポインターを使うことができる。
event 引数には、拡張ステータスコード用のバッファーを指定する。 SPU_CREATE_EVENTS_ENABLED フラグ付きで SPU コンテキストが作成されると、 spu_run() が返る前に Linux カーネルによりこのバッファーに 拡張ステータスコードが格納される。
ステータスコードには以下の定数が一つ以上入る。
NULL は event 引数として有効な値である。 この場合、イベントは呼び出し元のプロセスに報告されない。
spu_status レジスターの値は、ステータスコードと SPU の stop-and-signal 命令が返す 14 ビットのコードの ビットマスクで構成される。 後者の 14 ビットのコードはオプションである。 ステータスコードのビットマスクは下記の通りである。
spu_run() がエラーを返さなかった場合、下位 8 ビットのうち 1 つ以上は 常にセットされる。
#include <err.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
int context, fd, spu_status;
uint32_t instruction, npc;
context = syscall(SYS_spu_create, "/spu/example-context", 0, 0755);
if (context == -1)
err(EXIT_FAILURE, "spu_create");
/*
* Write a 'stop 0x1234' instruction to the SPU's
* local store memory.
*/
instruction = 0x00001234;
fd = open("/spu/example-context/mem", O_RDWR);
if (fd == -1)
err(EXIT_FAILURE, "open");
write(fd, &instruction, sizeof(instruction));
/*
* set npc to the starting instruction address of the
* SPU program. Since we wrote the instruction at the
* start of the mem file, the entry point will be 0x0.
*/
npc = 0;
spu_status = syscall(SYS_spu_run, context, &npc, NULL);
if (spu_status == -1)
err(EXIT_FAILURE, "open");
/*
* We should see a status code of 0x12340002:
* 0x00000002 (spu was stopped due to stop-and-signal)
* | 0x12340000 (the stop-and-signal code)
*/
printf("SPU Status: %#08x\n", spu_status);
exit(EXIT_SUCCESS);
}