EN JA
SYSCALL(2)
SYSCALL(2) Linux Programmer's Manual SYSCALL(2)

名前

syscall -間接システムコール

書式


#define _GNU_SOURCE /* feature_test_macros(7) 参照 */
#include <unistd.h>
#include <sys/syscall.h> /* SYS_xxx の定義用 */


int syscall(int number, ...);

説明

syscall() は、システムコールを起動する小さなライブラリ関数で、 number で指定されたアセンブリ言語インターフェースのシステムコールを、指定された引き数をつけて実行する。 syscall() が役に立つのは、例えば C ライブラリにラッパー関数が存在しないシステムコールを呼び出したい場合である。
 
syscall() は、システムコールを行う前に CPU レジスタを保存し、システムコールから返った際にレジスタを復元し、エラーが発生した場合はシステムコールが返したエラーコードを errno(3) に格納する。
 
システムコールのシンボル定数は、ヘッダファイル <sys/syscall.h> に書かれている。

返り値

返り値は呼び出されたシステムコールによって定義される。一般に、返り値 0 は成功を表す。-1 はエラーを表し、エラーコードは errno に入れられる。

注意

syscall() は 4BSD で最初に登場した。

アーキテクチャ固有の要件

Each architecture ABI has its own requirements on how system call arguments are passed to the kernel. For system calls that have a glibc wrapper (e.g., most system calls), glibc handles the details of copying arguments to the right registers in a manner suitable for the architecture. However, when using syscall() to make a system call, the caller might need to handle architecture-dependent details; this requirement is most commonly encountered on certain 32-bit architectures.
 
例えば、ARM アーキテクチャの Embedded ABI (EABI) では、 ( long long などの) 64 ビット値は偶数番地のレジスタのペアに境界があっていなければならない。したがって、 glibc が提供するラッパー関数ではなく syscall() を使う場合には、 readahead() システムコールは ARM アーキテクチャの EABI では以下のようにして起動されることになる。
 


syscall(SYS_readahead, fd, 0,
(unsigned int) (offset >> 32),
(unsigned int) (offset & 0xFFFFFFFF),
count);

オフセット引き数は 64 ビットで、最初の引き数 ( fd) は r0 で渡されるので、呼び出し側では手動で 64 ビット値を分割して境界を合わせて、 64 ビット値が r2/ r3 レジスタペアで渡されるようにしなければならない。このため、 r1 (2 番目の引数 0) としてダミー値を挿入している。

 

同様のことが、 MIPS の O32 ABI、 PowerPC の 32 ビット ABI や Xtensa でも起こりうる。

 

次のシステムコールに影響がある: fadvise64_64(2), ftruncate64(2), posix_fadvise(2), pread64(2), pwrite64(2), readahead(2), sync_file_range(2), truncate64(2)

Architecture calling conventions

Every architecture has its own way of invoking and passing arguments to the kernel. The details for various architectures are listed in the two tables below.
 
The first table lists the instruction used to transition to kernel mode, (which might not be the fastest or best way to transition to the kernel, so you might have to refer to the VDSO), the register used to indicate the system call number, and the register used to return the system call result.
arch/ABI instruction syscall # retval Notes
arm/OABI swi NR - a1 NR is syscall #
arm/EABI swi 0x0 r7 r1
blackfin excpt 0x0 P0 R0
i386 int $0x80 eax eax
ia64 break 0x100000 r15 r10/r8
parisc ble 0x100(%sr2, %r0) r20 r28
sparc/32 t 0x10 g1 o0
sparc/64 t 0x6d g1 o0
x86_64 syscall rax rax

The second table shows the registers used to pass the system call arguments.

arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7
arm/OABI a1 a2 a3 a4 v1 v2 v3
arm/EABI r1 r2 r3 r4 r5 r6 r7
blackfin R0 R1 R2 R3 R4 R5 -
i386 ebx ecx edx esi edi ebp -
ia64 r11 r9 r10 r14 r15 r13 -
parisc r26 r25 r24 r23 r22 r21 -
sparc/32 o0 o1 o2 o3 o4 o5 -
sparc/64 o0 o1 o2 o3 o4 o5 -
x86_64 rdi rsi rdx r10 r8 r9 -

Note that these tables don't cover the entire calling convention—some architectures may indiscriminately clobber other registers not listed here.


#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>


int
main(int argc, char *argv[])
{
pid_t tid;


tid = syscall(SYS_gettid);
tid = syscall(SYS_tgkill, getpid(), tid);
}

関連項目

_syscall(2), intro(2), syscalls(2)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 3.51 の一部である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man-pages/ に書かれている。
2013-04-07 Linux