“Syscall()”在Go语言的“syscall”包中是什么意思?

huangapple go评论87阅读模式
英文:

What does "Syscall()" means in go package "syscall"?

问题

在研究我的另一个问题https://stackoverflow.com/questions/16516948/go-package-syscall-conn-read-is-non-blocking-and-cause-high-cpu-usage时,我阅读了syscall包中的源代码。

由于我在OS X 10.8.3上发现了我的上一个问题,这里是相关的源代码:

http://golang.org/src/pkg/syscall/zsyscall_darwin_amd64.go?h=Read#L898

我不知道Syscall(SYS_READ, uintptr(fd), uintptr(_p0), uintptr(len(p)))是什么意思,实际上我不理解unsafe.PointerSyscall()这样的东西。它们是如何工作的?

此外,有人能解释一下注释// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT,为什么这些东西通过不同的实现与特定平台一起工作?syscall包是如何生成这些接口的?

如果有人能解释一个与syscall相关的特定函数,比如Read(),那将有助于我更好地理解它,谢谢。

英文:

During the research of my another question https://stackoverflow.com/questions/16516948/go-package-syscall-conn-read-is-non-blocking-and-cause-high-cpu-usage, I read source code in syscall package.

Since I found my last issue on OS X 10.8.3, here is the source code related:

http://golang.org/src/pkg/syscall/zsyscall_darwin_amd64.go?h=Read#L898

I have no idea what Syscall(SYS_READ, uintptr(fd), uintptr(_p0), uintptr(len(p))) means, actually I don't understand stuffs like unsafe.Pointer & Syscall(). How they works?

Besides, can anyone explain the comment // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT, how and why these things work with specific platform by different implementations? And how syscall package generate these interfaces?

If someone can explain a specific function like Read() related with syscall could be help me understand it better, thanks.

答案1

得分: 4

Go Darwin syscall包中的Read(fd int, p []byte) (n int, err error)函数是通过进行readSYS_READ)系统调用来实现的:

> read Mac OS X Developer Tools Manual Page
>
> ssize_t read(int fildes, void *buf, size_t nbyte);
>
> Read()尝试从由描述符fildes引用的对象中读取nbyte字节的数据,并将其存储到由buf指向的缓冲区中。

Go Darwin syscall包中的Syscall函数是:

// func Syscall(trap int64, a1, a2, a3 int64) (r1, r2, err int64);
// Trap # in AX, args in DI SI DX, return in AX DX

TEXT ·Syscall(SB),7,$0
    CALL runtime·entersyscall(SB)
    MOVQ 16(SP), DI
    MOVQ 24(SP), SI
    MOVQ 32(SP), DX
    MOVQ $0, R10
    MOVQ $0, R8
    MOVQ $0, R9
    MOVQ 8(SP), AX	// syscall entry
    ADDQ $0x2000000, AX
    SYSCALL
    JCC ok
    MOVQ $-1, 40(SP)	// r1
    MOVQ $0, 48(SP)	// r2
    MOVQ AX, 56(SP)  // errno
    CALL runtime·exitsyscall(SB)
    RET
ok:
    MOVQ AX, 40(SP)	// r1
    MOVQ DX, 48(SP)	// r2
    MOVQ $0, 56(SP)	// errno
    CALL runtime·exitsyscall(SB)
    RET
英文:

The Go Darwin syscall package func Read(fd int, p \[\]byte) (n int, err error) function is making a read (SYS_READ) system call:

> read Mac OS X Developer Tools Manual Page
>
> ssize_t read(int fildes, void *buf, size_t nbyte);
>
> Read() attempts to read nbyte bytes of data from the object
> referenced by the descriptor fildes into the buffer pointed to by buf.

The Go Darwin syscall package Syscall function is:

// func Syscall(trap int64, a1, a2, a3 int64) (r1, r2, err int64);
// Trap # in AX, args in DI SI DX, return in AX DX

TEXT	·Syscall(SB),7,$0
	CALL	runtime·entersyscall(SB)
	MOVQ	16(SP), DI
	MOVQ	24(SP), SI
	MOVQ	32(SP), DX
	MOVQ	$0, R10
	MOVQ	$0, R8
	MOVQ	$0, R9
	MOVQ	8(SP), AX	// syscall entry
	ADDQ	$0x2000000, AX
	SYSCALL
	JCC	ok
	MOVQ	$-1, 40(SP)	// r1
	MOVQ	$0, 48(SP)	// r2
	MOVQ	AX, 56(SP)  // errno
	CALL	runtime·exitsyscall(SB)
	RET
ok:
	MOVQ	AX, 40(SP)	// r1
	MOVQ	DX, 48(SP)	// r2
	MOVQ	$0, 56(SP)	// errno
	CALL	runtime·exitsyscall(SB)
	RET

答案2

得分: 2

从我理解的角度来看,syscall.Syscall()只是一种调用一些特定系统API的方式。

例如,Windows API通常使用__stdcall约定,一般情况下,我们无法在Go中实现这一点,除非使用syscall.Syscall()


> func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)

trap:我们想要调用的API。

a1, a2, a3:传递给该API的参数。

r1, r2, err:该API返回的内容。有三个返回值,但我们可能只使用其中一个。


就是这样。

英文:

From what I understand, syscall.Syscall() is simply a way to call some system specific APIs.

For example, Windows APIs typically use the __stdcall convention, which in general we would not be able to achieve without syscall.Syscall() in Go.


> func Syscall(trap, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)

trap: The API we want to call.

a1, a2, a3: Arguments to that API.

r1, r2, err: Whatever that API returns. There are three, but we would probably use only one of them.


That's about it.

huangapple
  • 本文由 发表于 2013年5月13日 23:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/16525890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定