## How do `syscall.RawSyscall()` and `syscall.Syscall()` differ in Go, and when should you use each?

Barbara Streisand
Release: 2024-10-26 07:24:30
Original
743 people have browsed it

## How do `syscall.RawSyscall()` and `syscall.Syscall()` differ in Go, and when should you use each?

Detailed Analysis of Go's Syscall.RawSyscall() and Syscall.Syscall()

Parameters and Return Values of Syscall.RawSyscall()

The Syscall.RawSyscall() function in Go takes four uintptr type parameters:

  • trap: This is the syscall trap number.
  • a1, a2, and a3: These are the arguments to the syscall.

It returns two uintptr values, r1 and r2, and an Errno value:

  • r1 and r2: These are the return values of the syscall.
  • err: This is an error value indicating any issues encountered during the execution of the syscall.

Assembly Implementation of RawSyscall()

The assembly implementation of RawSyscall() for darwin/amd64 can be found at the following link:

[https://golang.org/src/pkg/syscall/asm_darwin_amd64.s?h=RawSyscall](https://golang.org/src/pkg/syscall/asm_darwin_amd64.s?h=RawSyscall)

The lines 61-80 in this file contain the following assembly code:

<code class="assembly">61  TEXT ·RawSyscall(SB),7,
62      MOVQ    16(SP), DI
63      MOVQ    24(SP), SI
64      MOVQ    32(SP), DX
65      MOVQ    , R10
66      MOVQ    , R8
67      MOVQ    , R9
68      MOVQ    8(SP), AX   // syscall entry
69      ADDQ    x2000000, AX
70      SYSCALL
71      JCC ok1
72      MOVQ    $-1, 40(SP) // r1
73      MOVQ    , 48(SP)  // r2
74      MOVQ    AX, 56(SP)  // errno
75      RET
76  ok1:
77      MOVQ    AX, 40(SP)  // r1
78      MOVQ    DX, 48(SP)  // r2
79      MOVQ    , 56(SP)  // errno
80      RET</code>
Copy after login

This code essentially performs the following tasks:

  • Moves the arguments to the appropriate registers.
  • Invokes the syscall using the SYSCALL instruction.
  • Sets the return values and errno based on the result of the syscall.

zsyscall in Filename

The "z" prefix in the filename zsyscall_darwin_amd64.go indicates that it contains system call wrappers for unprivileged processes. These wrappers handle additional tasks such as switching to a different user or thread ID before executing the syscall.

Differences Between Syscall() and RawSyscall()

The primary difference between Syscall() and RawSyscall() is that Syscall() calls runtime.entersyscall(SB) and runtime.exitsyscall(SB) before and after executing the syscall, respectively. These functions allow the runtime system to track the blocking status of goroutines. RawSyscall(), on the other hand, does not make these calls, so it does not interact with the runtime system in the same way.

Usage of Syscall() and RawSyscall()

Syscall() is typically used when you have a specific syscall number and arguments that you want to call directly. RawSyscall() is useful when you want more control over the syscall execution, such as when you need to call a syscall that is not exposed by the os package or when you need to manually handle the blocking status of the syscall.

The above is the detailed content of ## How do `syscall.RawSyscall()` and `syscall.Syscall()` differ in Go, and when should you use each?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!