CELLプロセッサ--libc


libc

libc 解析 take.5 (newlib)

先日、SDK 2.0 が出ましたが、以前説明した libc の大部分が newlib にマー ジされています。また、SDK 1.1 ではアセンブラで記述されていた処理が C で 書き直されて理解しやすくなっています。

ということで、以下に中核となる send_to_ppe() 関数とサンプルとして fprintf() のソースコードを挙げておきます。(コードを追ってはいませんが、 レジスタ上にある引数のスタックへのコピーは va_start() でやっているんだと 思います)

newlib-1.14.0/newlib/libc/machine/spu/c99ppe.h

static void
send_to_ppe(int signalcode, int opcode, void *data)
{

        unsigned int    combined = ( ( opcode<<24 )&0xff000000 ) | ( ( unsigned int )data & 0x00ffffff );
        struct spe_reg128* ret = data;

        vector unsigned int stopfunc = {
                signalcode,     /* stop 0x210x*/
                (unsigned int) combined,
                0x4020007f,     /* nop */
                0x35000000      /* bi $0 */
        };

        void (*f) (void) = (void *) &stopfunc;
        asm ("sync":::"memory");
        f();
        errno = ret->slot[3];
        return;
}

newlib-1.14.0/newlib/libc/machine/spu/fprintf.c

typedef struct
{ 
  FILE * fp;
  unsigned int pad0[ 3 ];
  char* fmt;
  unsigned int pad1[ 3 ];
  va_list ap;
} c99_fprintf_t;

int
fprintf(FILE * fp, _CONST char *fmt,...)
{ 
  int* ret;
  c99_fprintf_t args;
  ret = (int*) &args;
  
  args.fp = translate_fp(fp);
  args.fmt = (char*) fmt;

#ifdef _HAVE_STDC
  va_start (args.ap, args.fmt);
#else
  va_start (args.ap);
#endif
  
  send_to_ppe(SPE_C99_SIGNALCODE, SPE_C99_VFPRINTF, &args);
  
  va_end (args.ap);
  return *ret;
}

あと、SPU 用の newlib もすでに開発ツリーにマージされています。

他にもフレームワークとして、SPU code overlays や software managed cache がサンプルとして提供されていますので、この場で説明していければと思っ ています。

libc 解析 take.4 (libspe)

"PPE Serviced SPE C Library Functions" の 4 回目は libspe の処理につ いて解説します。

4.2 PPE Serviced SPE C Library Functions

  1. The SPE constructs a local store image of the input and outout parameters.
  2. The SPE creates a 32-bit message consisting of an opcode and pointer to the local store parameter image array.
  3. The SPE executes a Stop and Signal instruction.
  4. The PPE detects the Stop and Signal.
  5. The PPE invoke a specialist to service the request according to the message opcode.
  6. The PPE services the requested function.
  7. The PPE returns results in the local store image array.
  8. The PPE resumes SPE execution.
  9. The SPE returns that results back to the caller.

この内、libspe で行うのは 5., 6., 7., 8. です。

1 回目に説明した通り、spu_run システムコールの返り値として SPE の stop 命令のオペランドが返ります。また、2, 3 回目に説明した通り、SPE の stop 命令の次のアドレスに OPCODE と 引数へのアドレスが格納されており、こ れらを以下のようにして処理していきます。

  • lispe の spe_create_thread() API で生成された PPE thread の本体であ る do_spe_run() が spu_run システムコールを発行して SPE thread を実行し ています。SPE thread が stop 命令を発行すると、SPE thread が停止し、 spu_run システムコールから復帰。
  • do_spe_run() は、返り値に含まれる stop 命令のオペランド (OPCLASS) を 見ることで、対応する libc のオペクラスハンドラ ( default_c99_handler() or default_posix1_handler() ) を実行。
  • オペクラスハンドラは、stop 命令の次に書かれた OPCODE を見ることで対 応する libc のオペコードハンドラ (default_{c99,posix1}_handler_*) を実行。
  • オペコードハンドラは libc 関数を実行し、実行結果 (返り値と errno) を SPE のメモリイメージに格納。
  • do_spe_run() が spu_run システムコールを発行して SPE を再実行。

以下は do_spe_run() の関連コードを抜粋したものにコメントで解説を付け たものです。

int do_spe_run (void *ptr)
{
        :
    do {
        // SPE thread を (再) 実行
        ret = spe_run(runfd, &npc, &status);
        // ret は SPU Status Register の値が入っている
        // - 下位 16 bit [0:15] が状態を表す値
        // - 上位 14 bit [16:29] が stop 命令のオペランド
        // よって code は stop 命令のオペランドとなる
        code = ret >> 16;
            :
        if (ret < 0) {
            :
        // code は libc のハンドラの場合は以下の値を取る
        // C の関数の場合:     SPE_C99_CLASS(0x2100)
        // POSIX の関数の場合: SPE_POSIX1_CLASS(0x2101)
        } else if ((code&0xff00)==0x2100) {
            int callnum = code&0xff;
                :
            if (!handlers[callnum])
            {
                :
            }
            else
            {
                // 対象となるオペクラスハンドラを実行
                // ちなみにオペクラスハンドラは 256 個作れますが、
                // 2 つしか使ってません
                int (*handler)(void *, unsigned int);
                handler=handlers[callnum];
                rc = handler(thread_store->mem_mmap_base, npc);
                if (rc) 
                {
                    :
                }
                else
                {
                    // npc がスタック上の bi 命令を指すように変更
                    npc += 4;
                }
            }
        }
        else if (code < 0x2000)
        {
            :
        }
    // 0x21?? の場合はエラーではなく、libc のハンドラ実行依頼
    // なので SPE thread を再実行するためにループの先頭へ
    } while (((code & 0xff00) == 0x2100 || ... );
}

お次は C99 のオペクラスハンドラ。

int default_c99_handler(unsigned long *base, unsigned long offset)
{
    int op, opdata; 

    if (!base) {
	DEBUG_PRINTF("%s: mmap LS required.\n", __func__);
	return 1;
    }
    // stop 命令の次のアドレスの値から OPCODE を得る
    offset = (offset & LS_ADDR_MASK) & ~0x1;
    opdata = *((int *)((char *) base + offset));
    op = SPE_C99_OP(opdata);
    // 値のチェック
    if ((op <= 0) || (op >= SPE_C99_NR_OPCODES)) {
        DEBUG_PRINTF("%s: Unhandled type %08x\n", __func__,
                     SPE_C99_OP(opdata));
        return 1;
    }

    // オペコードハンドラ実行
    default_c99_funcs[op-1] ((char *) base, opdata);

    return 0;
}

そして getc() に対応するオペコードハンドラである default_c99_handler_getc() です。

/**
 * default_c99_handler_getc
 * @ls: base pointer to SPE local-store area.
 * @opdata: per C99 call opcode & data.
 *
 * SPE C99 library operation, per: ISO/IEC C Standard 9899:1999,
 * implementing:
 *
 *	int getc(FILE *stream);
 */
int default_c99_handler_getc(char *ls, unsigned long opdata)
{
    // 引数へのポインタ arg0 を得る
    // SPE は 128 bit 単位であるため、arg0 はこれを吸収するため
    // の構造体 spe_reg128 へのポインタとなっている
    DECL_1_ARGS();
    // 返り値と errno を格納するポインタ (arg0 と同じ) を得る
    DECL_RET();
    FILE *stream;
    int rc;

    DEBUG_PRINTF("%s\n", __func__);
    CHECK_C99_OPCODE(GETC);
    // 引数から stream を得る
    stream = get_FILE(arg0->slot[0]);
    // getc() を実行
    rc = getc(stream);
    // 返り値 rc と errno を SPE のメモリイメージに格納
    PUT_LS_RC(rc, 0, 0, errno);
    return 0;
}

getc() 実行時の SPE thread スタックはこうなります。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |
         |------------|
         |            |
         |------------|
         |    lnop    |
         |    bi $2   |
         |   opdata   |        <---- ncp
         |    stop    |
         |------------|
         |   slot[3]  |   * slot[] は各 32 bit で有効なデータがない状態
         |   slot[2]  |     を表しています
         |   slot[1]  |
         |   stream   | <---- arg0, ret
         `------------'
                        low memory

getc() 実行後に 返り値 rc と errno を格納し、do_spe_run() 内で npc を +4 した後の SPE thread スタックに以下のようになります。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |
         |------------|
         |            |
         |------------|
         |    lnop    |
         |    bi $2   |        <---- ncp
         |   opdata   |
         |    stop    |
         |------------|
         |    errno   |
         |   slot[2]  |
         |   slot[1]  |
         |     rc     | <---- arg0, ret
         `------------'
                        low memory

可変引数関数の場合も同様ですが、以下に fprintf() 実行時 (PPE では vfprintf が実行されるため、対応するオペコードハンドラは default_c99_handler_vfprintf() となる) のスタックのみ示しておきます。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |<-----. <---- input SP
         |------------|      |
         |  Reg 79    |      |
         |------------|      |
         |  Reg 78    |      |
         |------------|      |
         //   ...    //      |
         |------------|      |
         |  Reg  6    |      |
         |------------|      |
         |  Reg  5    |<--.  |
         |------------|   |  |
         |   slot[3]  |   |  |
         |   slot[2]  |   |  |
         |   slot[1]  |   |  |   
         |caller_stack|------'
         |------------|   |
         |   slot[3]  |   |
         |   slot[2]  |   |
         |   slot[1]  |   |
         |  next_arg  |---'
         |------------| 
         |   slot[3]  |
         |   slot[2]  |
         |   slot[1]  |
         |   format   | <---- arg1
         |------------|
         |   slot[3]  |
         |   slot[2]  |
         |   slot[1]  |
         |   stream   | <---- arg0, ret
         |============|
         |            |
         |------------|
         |            |
         |------------|
         |    lnop    |
         |    bi $2   |
         |   opdata   |        <---- ncp
         |    stop    |
         `------------'
                        low memory

なお、現在は公開されてませんが、spe.c 内には

register_handler(void * handler, unsigned int callnum )

なる関数があり、ユーザー独自のハンドラを登録できるようにすることが検 討されていた形跡はあります。が、libspe の API としての公開はされていませ ん。

libc 解析 take.3 (可変引数関数)

"PPE Serviced SPE C Library Functions" の 3 回目、可変引数関数の場合 について解説します。

可変引数関数の場合

以下は fprintf() を例に説明します。まずは、生成されたソースコード (fprintf.S) です。

#include "spe_c99_ops.h"
#include "spe_posix1_ops.h"

#define OPCLASS SPE_C99_CLASS
#define NAME    fprintf
#define PARMS   2
#define OPCODE  SPE_C99_VFPRINTF
#define RETVAL  1

#include "spu_syscall_va.S"

非可変引数関数の場合は spu_syscall.S をインクルードしていましたが、可 変引数関数の場合は spu_syscall_va.S をインクルードしています。

注意すべき点としては、非可変引数関数と同様に命令列をスタック上にセー ブして、このスタック上の命令が実行されること以外にも、

可変引数関数の場合、引数がどれだけあるかわからないので、引数の書いて ある可能性のあるレジスタ (つまり全ての揮発性レジスタ) を全てスタックにセー ブする必要があります。

レジスタをセーブする命令もスタック上で実行されますが、メモリ使用量を 削減するため?に命令自体の書き換えを行っています。

これらのレジスタは va_list 形式 (パラメータと呼び出し関数のスタックへ のアドレス) で管理。

というところでしょうか。では実際に逆アセ結果を見てみます。まず前半の レジスタをセーブする部分です。

00000000 :
 0:   40 fd 70 02     il      $2,-1312     // save_regs_1: の命令列へのスタック相対値をロード
 4:   24 ff c0 cf     stqd    $79,-16($1)  // 内部使用するレジスタの値を先にセーブ
 8:   24 ff 80 ce     stqd    $78,-32($1)  //
 c:   24 ff 40 cd     stqd    $77,-48($1)  //
10:   24 ff 00 cc     stqd    $76,-64($1)  //
14:   33 80 0f cf     lqr     $79,90       // save_regs_1: の命令列を $79 にロード
18:   18 00 80 cc     a       $76,$1,$2    // save_regs_1: の命令列をおくアドレスをロード
1c:   33 80 10 ce     lqr     $78,a0       // save_regs_2: の命令列を $78 にロード
20:   42 00 09 02     ila     $2,12        // 命令列 (save_regs_*) のループ回数をロード
24:   33 80 11 cd     lqr     $77,b0       // save_regs_3: の命令列を $77 にロード
28:   24 00 26 4f     stqd    $79,0($76)   // 命令列 (save_regs_1) をスタックにセーブ
2c:   24 00 a6 4d     stqd    $77,32($76)  // 命令列 (save_regs_3) をスタックにセーブ
30:   1c f0 00 cd     ai      $77,$1,-64   // レジスタをセーブするアドレスをロード
34:   00 40 00 00     sync
38:   35 20 26 4f     bisl    $79,$76      // スタック上の命令列にジャンプ
3c:   1c 04 26 cd     ai      $77,$77,16
      ...

00000090 :
90:   24 00 66 4e     stqd    $78,16($76)  // 命令列 (save_regs_2) をスタックにセーブ
94:   00 40 00 00     sync
98:   1c ff c1 02     ai      $2,$2,-1     // ループカウンタをデクリメント
9c:   1c ff 27 4e     ai      $78,$78,-4   // 命令列 (save_regs_2) のレジスタ指定値を -4

000000a0 :
a0:   24 ff e6 cb     stqd    $75,-16($77) // 4 つのレジスタをセーブ
a4:   24 ff a6 ca     stqd    $74,-32($77) // レジスタ番号はループ毎に -4 される
a8:   24 ff 66 c9     stqd    $73,-48($77) // 1 回目: 75, 74, 73, 72
ac:   24 ff 26 c8     stqd    $72,-64($77) // 2 回目: 71, 70, 69, 68

000000b0 :
b0:   1c f0 26 cd     ai      $77,$77,-64  // レジスタをセーブするアドレスを設定 (-16*4)
b4:   21 7f fb 82     brnz    $2,90        // スタック上の save_regs_1 にジャンプ
b8:   35 00 27 80     bi      $79          // 3c: にジャンプ
bc:   00 00 00 00     stop

ということで 38: のジャンプ実行時のスタックが以下だとすると、

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |        <---- input SP
         |------------|
         |  Reg 79    |
         |------------|
         |  Reg 78    |
         |------------|
         |  Reg 77    |
         |------------|
         |  Reg 76    |
         |------------|
         |            |
         |------------|
         //   ...    //
         |------------|
         |            |
         |============|
         |save_regs_3 |
         |------------|
         |            |
         |------------|
         |save_regs_1 | <---- $76
         `------------'
                        low memory

39: 時点のスタックがこうなります。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |        <---- input SP
         |------------|
         |  Reg 79    |
         |------------|
         |  Reg 78    |
         |------------|
         |  Reg 77    |
         |------------|
         |  Reg 76    |
         |------------|
         |  Reg 75    |
         |------------|
         //   ...    //
         |------------|
         |  Reg  4    |
         |------------|
         |            |
         |------------|
         |            |
         |------------|
         |            |
         |============|
         |save_regs_3 |
         |------------|
         |save_regs_2 |
         |------------|
         |save_regs_1 | <---- $76
         `------------'
                        low memory

次に後半の引数セーブと stop を実行する部分です。

3c:   1c 04 26 cd     ai      $77,$77,16    // 2 つ以上引数がある場合のアドレス調整
                                            // この場合は Reg 4 分インクリメント
40:   24 ff e6 81     stqd    $1,-16($77)   // 現在の sp (call_stack) をセーブ
44:   24 ff a6 cd     stqd    $77,-32($77)  // パラメータへのアドレス (next_arg) をセーブ
48:   24 ff 66 84     stqd    $4,-48($77)   // 引数 2 (format) をセーブ
4c:   24 ff 26 83     stqd    $3,-64($77)   // 引数 1 (stream) をセーブ
50:   1c f0 26 cd     ai      $77,$77,-64   // 引数 1 のアドレスをロード
54:   41 11 80 03     ilhu    $3,8960       // OPCODE(0x23) をロード
58:   43 ff ff 84     ila     $4,3ffff      // マスク値を生成
5c:   33 80 0c cf     lqr     $79,c0        // call_ppe_1: の命令列をロード
60:   3e c1 00 85     cwd     $5,4($1)      // マスク値を生成
64:   80 73 41 84     selb    $3,$3,$77,$4  // [24:31] OPCODE, [0:17] 引数のアドレス
68:   b9 f3 c1 85     shufb   $79,$3,$79,$5 // $3 を命令列 2 word 目に挿入
6c:   24 00 26 4f     stqd    $79,0($76)    // 命令列をスタックにセーブ
70:   00 40 00 00     sync
74:   35 20 26 02     bisl    $2,$76        // スタック上の 命令列にジャンプ
78:   34 ff 00 83     lqd     $3,-64($1)    // 返り値を $3 にロード
7c:   3f e3 01 82     shlqbyi $2,$3,12
80:   23 80 00 02     stqr    $2,0          // errno をセーブ
84:   35 00 00 00     bi      $0            // 呼び出し関数に復帰
        ...
000000c0 :
c0:   00 00 21 00     stop                  // stop OPCLASS
c4:   00 00 00 00     stop                  // ここに OPCODE + 引数のアドレス が入る
c8:   35 00 01 00     bi      $2            // 78: にジャンプ
cc:   00 20 00 00     lnop

よって、スタック上の stop OPCLASS 命令を発行して SPE が停止した時のス タックはこのようになります。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |<-----. <---- input SP
         |------------|      |
         |  Reg 79    |      |
         |------------|      |
         |  Reg 78    |      |
         |------------|      |
         //   ...    //      |
         |------------|      |
         |  Reg  6    |      |
         |------------|      |
         |  Reg  5    |<--.  |
         |------------|   |  |
         | call_stack |------'
         |------------|   |   
         |  next_arg  |---'
         |------------| 
         | format (r4)|
         |------------|
         | stream (r3)| <---- start of parameters
         |============|
         |            |
         |------------|
         |            |
         |------------|
         | call_ppe_1 | <---- $76
         `------------'
                        low memory

libc 解析 take.2 (非可変引数関数)

"PPE Serviced SPE C Library Functions" の 2 回目は、SPE 側での処理に ついて解説します。

4.2 PPE Serviced SPE C Library Functions

  1. The SPE constructs a local store image of the input and outout parameters.
  2. The SPE creates a 32-bit message consisting of an opcode and pointer to the local store parameter image array.
  3. The SPE executes a Stop and Signal instruction.
  4. The PPE detects the Stop and Signal.
  5. The PPE invoke a specialist to service the request according to the message opcode.
  6. The PPE services the requested function.
  7. The PPE returns results in the local store image array.
  8. The PPE resumes SPE execution.
  9. The SPE returns that results back to the caller.

この内、SPE で行うのは 1., 2., 3. ですが、呼び出す関数の引数の数が決 まっている場合 (非可変引数関数) と、可変の場合 (可変引数関数) の場合で処 理方法が変わります。

ソースコード

ソースコードは /opt/IBM/cell-sdk-1.1/src/lib/c/spu/ 以下にあるのです が、ほとんどのコードは mk_syscalls と syscall.def からコンパイル時に生成 されています。syscall.def の一部を抜き出してみると

#
#   CLASS             OPCODE             NAME        PARMS RETVAL   SYSCALL_TYPE
#

SPE_C99_CLASS     SPE_C99_GETC           getc          1     1      spu_syscall

SPE_C99_CLASS     SPE_C99_VFPRINTF       fprintf       2     1      spu_syscall_va

のように各関数の属性が書かれており、mk_syscall がこの属性情報をもとに 各関数用のソースファイルを生成します。で、SYSCALL_TYPE が spu_syscall の 場合は非可変引数関数、spu_syscall_va の場合は可変引数関数となります。

非可変引数関数の場合

以下は getc() を例に説明します。まずは、生成されたソースコード (getc.S) です。

#include "spe_c99_ops.h"
#include "spe_posix1_ops.h"

#define OPCLASS SPE_C99_CLASS              // C99 の関数
#define NAME    gets                       // 関数名
#define PARMS   1                          // 引数の個数
#define OPCODE  SPE_C99_GETS               // 関数の識別子 (13)
#define RETVAL  1                          // 返り値がある場合

#include "spu_syscall.S"

このように非可変引数関数の場合は spu_syscall.S をインクルードしていま す。次はオブジェクトファイルの逆アセ結果で説明をコメントとして追加してあ ります。

注意すべき点としては、stack_instr: に書かれた命令列は一旦レジスタにロー ドされ、加工された後にスタック上にセーブし、最終的にはスタック上の命令が 実行されるところです。

getc.o:     file format elf32-spu
Disassembly of section .text:

00000000 :
 0:   41 09 80 02     ilhu    $2,4864      // OPCODE(13) を $2 にロード
 4:   24 ff 40 83     stqd    $3,-48($1)   // 引数 ($3) を -48(sp) にセーブ
 8:   33 80 07 06     lqr     $6,40        // stack_instr: の命令列を $6 にロード
 c:   1c f4 00 83     ai      $3,$1,-48    // 引数をセーブするアドレスを $3 にロード
10:   43 ff ff 84     ila     $4,3ffff     // マスク値を生成
14:   80 40 c1 04     selb    $2,$2,$3,$4  // [24:31] OPCODE, [0:17] 引数のアドレス
18:   3e c1 00 85     cwd     $5,4($1)     // マスク値を生成
1c:   b0 41 81 05     shufb   $2,$2,$6,$5  // $2 を命令列 2 word 目に挿入
20:   1c f8 00 83     ai      $3,$1,-32    // 命令列をセーブするアドレスをロード
24:   24 ff 80 82     stqd    $2,-32($1)   // 命令列をスタックにセーブ
28:   00 40 00 00     sync
2c:   35 20 01 82     bisl    $2,$3        // スタック上の命令列にジャンプ
30:   34 ff 40 83     lqd     $3,-48($1)   // 返り値を $3 にロード
34:   3f e3 01 82     shlqbyi $2,$3,12
38:   23 80 00 02     stqr    $2,0         // errno をセーブ
3c:   35 00 00 00     bi      $0           // 呼び出し関数に復帰

00000040 :
40:   00 00 21 00     stop                 // stop OPCLASS
44:   00 00 00 00     stop                 // ここに OPCODE + 引数のアドレス が入る
48:   35 00 01 00     bi      $2           // 30: にジャンプ
4c:   00 20 00 00     lnop

よって、スタック上の stop OPCLASS 命令を発行して SPE が停止した時のス タックはこのようになります。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |        <---- input SP
         |------------|
         |            |
         |------------|
         |   命令列   |        <---- $3
         |------------|
         |    引数    | <---- start of parameters
         `------------'
                        low memory

ちなみに引数が 2 つの場合はスタックフレームはこんな感じです。

         |   Stack    | high memory
         |   Parms    |
         |            |
         |------------|
         |  Link Reg  |
         |------------|
         | Back Chain |        <---- input SP
         |------------|
         |            |
         |------------|
         |   命令列   |        <---- $3
         |------------|
         |   引数 2   |
         |------------|
         |   引数 1   | <---- start of parameters
         `------------'
                        low memory

ちょっと謎なのが stack_instr 以下のコードをスタック上にセーブしてから 実行しているところです。spu_syscall.S のコメントを見ると "リエントラント にするためだ" と書いてあり、たしかに OPCLASS と引数のアドレスを書くんで、 1 SPE 内に複数のスレッドがいて、スレッド切り替え時に LS の内容が変更され ないなら必要ですが、現在のようにスレッドが切り替わる場合は LS の内容も全 部入れ換えている場合は必要ないかな、、と。まあ、今後の備え?

libc 解析 take.1 (spu_run syscall)

これから数回に渡って、"PPE Serviced SPE C Library Functions" について 解説しようと思います。この処理は SPE thread, Linux kernel, libspe が密接 にからんでいますが、1 回目は Linux kernel が行う処理についてです。

この処理の詳細は libraries_SDK.pdf p.66 "4.2 PPE Serviced SPE C Library Functions" にあります。

4.2 PPE Serviced SPE C Library Functions

  1. The SPE constructs a local store image of the input and outout parameters.
  2. The SPE creates a 32-bit message consisting of an opcode and pointer to the local store parameter image array.
  3. The SPE executes a Stop and Signal instruction.
  4. The PPE detects the Stop and Signal.
  5. The PPE invoke a specialist to service the request according to the message opcode.
  6. The PPE services the requested function.
  7. The PPE returns results in the local store image array.
  8. The PPE resumes SPE execution.
  9. The SPE returns that results back to the caller.

この内、Linux kernel が行うのは 4. だけで、実際には SPE thread の実行 を開始する spu_run システムコールの内部で処理されています。spu_run に関し てはドキュメントがlinux/Documentation/filesystems/spufs.txt にあるので訳 してみますと、、

------------------------------------------------------------------------------
SPU_RUN(2)                 Linux Programmer's Manual                SPU_RUN(2)



名前
       spu_run - spu context を実行する


SYNOPSIS
       #include 

       int spu_run(int fd, unsigned int *npc, unsigned int *event);

DESCRIPTION
       The  spu_run system call is used on PowerPC machines that implement the
       Cell Broadband Engine Architecture in order to access Synergistic  Pro-
       cessor  Units  (SPUs).  It  uses the fd that was returned from spu_cre-
       ate(2) to address a specific SPU context. When the context gets  sched-
       uled  to a physical SPU, it starts execution at the instruction pointer
       passed in npc.

       spu_run システムコールは Cell Broadband Engine Architecture を実装した 
       PowerPC 上で Synergistic Processor Units (SPUs) にアクセスするために使
       用される。SPU context を特定するために spu_create(2) の返り値である fd
       を使用する。context が物理 SPU を獲得後、npc で指定された命令ポインタの
       さす命令の実行を開始する。

       Execution of SPU code happens synchronously, meaning that spu_run  does
       not  return  while the SPU is still running. If there is a need to exe-
       cute SPU code in parallel with other code on either  the  main  CPU  or
       other  SPUs,  you  need to create a new thread of execution first, e.g.
       using the pthread_create(3) call.

       SPU 命令は同期的に実行されるため、SPU 実行中は spu_run は復帰しません。
       メイン CPU や他の SPU 上のコードと並列に実行する必要のある時は、例えば 
       pthread_create(3) を使用して、最初に新しいスレッドを生成する必要があり
       ます。

       When spu_run returns, the current value of the SPU instruction  pointer
       is  written back to npc, so you can call spu_run again without updating
       the pointers.

       spu_run から復帰する時に SPU 命令ポインタの現在値が npc に書き込まれるた
       め、命令ポインタを更新することなく再度 spu_run を呼び出すことができます。

       event can be a NULL pointer or point to an extended  status  code  that
       gets  filled  when spu_run returns. It can be one of the following con-
       stants:

       event は NULL ポインタか spu_run から返る時の拡張ステータスコードが格納
       されるポインタを指定でき、その値は以下の定数にいづれかになる。

       SPE_EVENT_DMA_ALIGNMENT
              A DMA alignment error

       SPE_EVENT_SPE_DATA_SEGMENT
              A DMA segmentation error

       SPE_EVENT_SPE_DATA_STORAGE
              A DMA storage error

       If NULL is passed as the event argument, these errors will result in  a
       signal delivered to the calling process.

       event 引数に NULL が指定された場合、これらのエラーはシグナルとして呼び出
       したプロセスに配送される。


返り値
       spu_run  returns the value of the spu_status register or -1 to indicate
       an error and set errno to one of the error  codes  listed  below.   The
       spu_status  register  value  contains  a  bit  mask of status codes and
       optionally a 14 bit code returned from the stop-and-signal  instruction
       on the SPU. The bit masks for the status codes are:

       spu_run は spu_status レジスタの値か、エラーを指す -1 を返し、以下に示す
       エラーコードのいづれかを errno に格納する。spu_status レジスタの値はステ
       ータスコードのビットマップと、SPU 上で実行された stop-and-signal 命令で
       指定された 14 ビットの値を含む。ステータスコードのビットマスクは、

       0x02   SPU was stopped by stop-and-signal.

       0x04   SPU was stopped by halt.

       0x08   SPU is waiting for a channel.

       0x10   SPU is in single-step mode.

       0x20   SPU has tried to execute an invalid instruction.

       0x40   SPU has tried to access an invalid channel.

       0x3fff0000
              The  bits  masked with this value contain the code returned from
              stop-and-signal.

       There are always one or more of the lower eight bits set  or  an  error
       code is returned from spu_run.

       下位 8 ビットの内、常に 1 つ以上のビットが立ったものか、エラーコードが
       spu_run から返される。

エラー
       EAGAIN or EWOULDBLOCK
              fd is in non-blocking mode and spu_run would block.

       EBADF  fd is not a valid file descriptor.

       EFAULT npc is not a valid pointer or status is neither NULL nor a valid
              pointer.

       EINTR  A signal occured while spu_run was in progress.  The  npc  value
              has  been updated to the new program counter value if necessary.

       EINVAL fd is not a file descriptor returned from spu_create(2).

       ENOMEM Insufficient memory was available to handle a page fault result-
              ing from an MFC direct memory access.

       ENOSYS the functionality is not provided by the current system, because
              either the hardware does not provide SPUs or the spufs module is
              not loaded.


ノート
       spu_run  is  meant  to  be  used  from  libraries that implement a more
       abstract interface to SPUs, not to be used from  regular  applications.
       See  http://www.bsc.es/projects/deepcomputing/linuxoncell/ for the rec-
       ommended libraries.

       spu_run は SPU へのより抽象的なインタフェースを実装するライブラリから使
       用することが意図されており、通常のアプリケーションから使用されることは意
       図されていない。推奨するライブラリとしては
       http://www.bsc.es/projects/deepcomputing/linuxoncell/ を参照。

準拠
       This call is Linux specific and only implemented by the ppc64 architec-
       ture. Programs using this system call are not portable.

       この関数は Linux 特有であり、ppc64 アーキテクチャでのみ実装されている。
       このシステムコールを使用したプログラムはポータブルではない。

バグ
       The code does not yet fully implement all features lined out here.

       コードはここに記述した機能を全て実装していない。

筆者
       Arnd Bergmann 

関連項目
       capabilities(7), close(2), spu_create(2), spufs(7)



Linux                             2005-09-28                        SPU_RUN(2)

------------------------------------------------------------------------------

と長々と訳してみましたが、関連部分を超要約すると、"SPE thread は PPE に何かしてほしい場合は、お願いをオペランドに指定して stop 命令を発行 してね。で、PPE thread は spu_run の返り値としてお願いを受け取ることがで きるから、お願いに応じて実際の処理をしてちょ〜だい" ということです。

返り値についてもエラーでなければ CBE_Public_Registers_v10.pdf p.75 "3.4.3.4 SPU Status Register (SPU_Status)" の値がそのまま返り値となりま す。

あと、注意する点としては spu_run から復帰した時は SPE thread は必ず止 まっているということです。この後、PPE thread が spufs 経由で SPE のメモリ イメージ (/spu/*/mem) を読み書きをしますが、止まっていない場合は spufs 経 由のメモリイメージへ書き込みをしても正しく反映されません。(この処理で扱う システムコールは同期的なので当然止まっているわけですが、、)

内部的には、SPE スケジューラやコンテキストの処理がからんできて結構難 しいのですが、今回説明する処理を理解する上では spu_run システムコールの 仕様がわかっていれば十分なのでこの辺でお茶を濁しておきます。


HOME |  企業情報 |  事業内容 |  採用情報 |  社内活動 |  お問合せ |  サイトマップ
.
Copyright(c)2006 Sijam.Inc all rights Reserved
システム開発 株式会社シジャム