]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime/internal/syscall: new package for linux
authorMichael Pratt <mpratt@google.com>
Tue, 8 Feb 2022 21:45:14 +0000 (16:45 -0500)
committerMichael Pratt <mpratt@google.com>
Tue, 15 Feb 2022 15:40:29 +0000 (15:40 +0000)
Add a generic syscall package for use by the runtime. Eventually we'd
like to clean up system calls in the runtime to use more code generation
and be moved out of the main runtime package.

The implementations of the assembly functions are based on copies of
syscall.RawSyscall6, modified slightly for more consistency between
arches. e.g., renamed trap to num, always set syscall num register
first.

For now, this package is just the bare minimum needed for
doAllThreadsSyscall to make an arbitrary syscall.

For #51087.
For #50113.

Change-Id: Ibecb5e6303279ce15286759e1cd6a2ddc52f7c72
Reviewed-on: https://go-review.googlesource.com/c/go/+/383999
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
12 files changed:
src/cmd/compile/internal/base/base.go
src/go/build/deps_test.go
src/runtime/internal/syscall/asm_linux_386.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_amd64.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_arm.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_arm64.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_mips64x.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_mipsx.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_ppc64x.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_riscv64.s [new file with mode: 0644]
src/runtime/internal/syscall/asm_linux_s390x.s [new file with mode: 0644]
src/runtime/internal/syscall/syscall_linux.go [new file with mode: 0644]

index be6d49fac766db1d846ad5a376e4e0d8297ece0e..39ce8e66f7313ebb94b7975075b5593eadbc6750 100644 (file)
@@ -62,8 +62,9 @@ func Compiling(pkgs []string) bool {
 // at best instrumentation would cause infinite recursion.
 var NoInstrumentPkgs = []string{
        "runtime/internal/atomic",
-       "runtime/internal/sys",
        "runtime/internal/math",
+       "runtime/internal/sys",
+       "runtime/internal/syscall",
        "runtime",
        "runtime/race",
        "runtime/msan",
index 22a04ff5376fa8e9517122c067fa5abadd309f97..72465659dc24e871b06fc56f923aba2a3bf73e5c 100644 (file)
@@ -88,6 +88,7 @@ var depsRules = `
        < internal/itoa
        < internal/unsafeheader
        < runtime/internal/sys
+       < runtime/internal/syscall
        < runtime/internal/atomic
        < runtime/internal/math
        < runtime
diff --git a/src/runtime/internal/syscall/asm_linux_386.s b/src/runtime/internal/syscall/asm_linux_386.s
new file mode 100644 (file)
index 0000000..15aae4d
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// See ../sys_linux_386.s for the reason why we always use int 0x80
+// instead of the glibc-specific "CALL 0x10(GS)".
+#define INVOKE_SYSCALL INT     $0x80
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+//
+// Syscall # in AX, args in BX CX DX SI DI BP, return in AX
+TEXT ·Syscall6(SB),NOSPLIT,$0-40
+       MOVL    num+0(FP), AX   // syscall entry
+       MOVL    a1+4(FP), BX
+       MOVL    a2+8(FP), CX
+       MOVL    a3+12(FP), DX
+       MOVL    a4+16(FP), SI
+       MOVL    a5+20(FP), DI
+       MOVL    a6+24(FP), BP
+       INVOKE_SYSCALL
+       CMPL    AX, $0xfffff001
+       JLS     ok
+       MOVL    $-1, r1+28(FP)
+       MOVL    $0, r2+32(FP)
+       NEGL    AX
+       MOVL    AX, errno+36(FP)
+       RET
+ok:
+       MOVL    AX, r1+28(FP)
+       MOVL    DX, r2+32(FP)
+       MOVL    $0, errno+36(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_amd64.s b/src/runtime/internal/syscall/asm_linux_amd64.s
new file mode 100644 (file)
index 0000000..961d9bd
--- /dev/null
@@ -0,0 +1,33 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+//
+// Syscall # in AX, args in DI SI DX R10 R8 R9, return in AX DX.
+//
+// Note that this differs from "standard" ABI convention, which would pass 4th
+// arg in CX, not R10.
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOVQ    num+0(FP), AX   // syscall entry
+       MOVQ    a1+8(FP), DI
+       MOVQ    a2+16(FP), SI
+       MOVQ    a3+24(FP), DX
+       MOVQ    a4+32(FP), R10
+       MOVQ    a5+40(FP), R8
+       MOVQ    a6+48(FP), R9
+       SYSCALL
+       CMPQ    AX, $0xfffffffffffff001
+       JLS     ok
+       MOVQ    $-1, r1+56(FP)
+       MOVQ    $0, r2+64(FP)
+       NEGQ    AX
+       MOVQ    AX, errno+72(FP)
+       RET
+ok:
+       MOVQ    AX, r1+56(FP)
+       MOVQ    DX, r2+64(FP)
+       MOVQ    $0, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_arm.s b/src/runtime/internal/syscall/asm_linux_arm.s
new file mode 100644 (file)
index 0000000..dbf1826
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-40
+       MOVW    num+0(FP), R7   // syscall entry
+       MOVW    a1+4(FP), R0
+       MOVW    a2+8(FP), R1
+       MOVW    a3+12(FP), R2
+       MOVW    a4+16(FP), R3
+       MOVW    a5+20(FP), R4
+       MOVW    a6+24(FP), R5
+       SWI     $0
+       MOVW    $0xfffff001, R6
+       CMP     R6, R0
+       BLS     ok
+       MOVW    $-1, R1
+       MOVW    R1, r1+28(FP)
+       MOVW    $0, R2
+       MOVW    R2, r2+32(FP)
+       RSB     $0, R0, R0
+       MOVW    R0, errno+36(FP)
+       RET
+ok:
+       MOVW    R0, r1+28(FP)
+       MOVW    R1, r2+32(FP)
+       MOVW    $0, R0
+       MOVW    R0, errno+36(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_arm64.s b/src/runtime/internal/syscall/asm_linux_arm64.s
new file mode 100644 (file)
index 0000000..83e862f
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOVD    num+0(FP), R8   // syscall entry
+       MOVD    a1+8(FP), R0
+       MOVD    a2+16(FP), R1
+       MOVD    a3+24(FP), R2
+       MOVD    a4+32(FP), R3
+       MOVD    a5+40(FP), R4
+       MOVD    a6+48(FP), R5
+       SVC
+       CMN     $4095, R0
+       BCC     ok
+       MOVD    $-1, R4
+       MOVD    R4, r1+56(FP)
+       MOVD    ZR, r2+64(FP)
+       NEG     R0, R0
+       MOVD    R0, errno+72(FP)
+       RET
+ok:
+       MOVD    R0, r1+56(FP)
+       MOVD    R1, r2+64(FP)
+       MOVD    ZR, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_mips64x.s b/src/runtime/internal/syscall/asm_linux_mips64x.s
new file mode 100644 (file)
index 0000000..0e88a2d
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build linux && (mips64 || mips64le)
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOVV    num+0(FP), R2   // syscall entry
+       MOVV    a1+8(FP), R4
+       MOVV    a2+16(FP), R5
+       MOVV    a3+24(FP), R6
+       MOVV    a4+32(FP), R7
+       MOVV    a5+40(FP), R8
+       MOVV    a6+48(FP), R9
+       SYSCALL
+       BEQ     R7, ok
+       MOVV    $-1, R1
+       MOVV    R1, r1+56(FP)
+       MOVV    R0, r2+64(FP)
+       MOVV    R2, errno+72(FP)
+       RET
+ok:
+       MOVV    R2, r1+56(FP)
+       MOVV    R3, r2+64(FP)
+       MOVV    R0, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_mipsx.s b/src/runtime/internal/syscall/asm_linux_mipsx.s
new file mode 100644 (file)
index 0000000..050029e
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build linux && (mips || mipsle)
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+//
+// The 5th and 6th arg go at sp+16, sp+20.
+// Note that frame size of 20 means that 24 bytes gets reserved on stack.
+TEXT ·Syscall6(SB),NOSPLIT,$20-40
+       MOVW    num+0(FP), R2   // syscall entry
+       MOVW    a1+4(FP), R4
+       MOVW    a2+8(FP), R5
+       MOVW    a3+12(FP), R6
+       MOVW    a4+16(FP), R7
+       MOVW    a5+20(FP), R8
+       MOVW    a6+24(FP), R9
+       MOVW    R8, 16(R29)
+       MOVW    R9, 20(R29)
+       SYSCALL
+       BEQ     R7, ok
+       MOVW    $-1, R1
+       MOVW    R1, r1+28(FP)
+       MOVW    R0, r2+32(FP)
+       MOVW    R2, errno+36(FP)
+       RET
+ok:
+       MOVW    R2, r1+28(FP)
+       MOVW    R3, r2+32(FP)
+       MOVW    R0, errno+36(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_ppc64x.s b/src/runtime/internal/syscall/asm_linux_ppc64x.s
new file mode 100644 (file)
index 0000000..8e84638
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build linux && (ppc64 || ppc64le)
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOVD    num+0(FP), R9   // syscall entry
+       MOVD    a1+8(FP), R3
+       MOVD    a2+16(FP), R4
+       MOVD    a3+24(FP), R5
+       MOVD    a4+32(FP), R6
+       MOVD    a5+40(FP), R7
+       MOVD    a6+48(FP), R8
+       SYSCALL R9
+       BVC     ok
+       MOVD    $-1, R4
+       MOVD    R4, r1+56(FP)
+       MOVD    R0, r2+64(FP)
+       MOVD    R3, errno+72(FP)
+       RET
+ok:
+       MOVD    R3, r1+56(FP)
+       MOVD    R4, r2+64(FP)
+       MOVD    R0, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_riscv64.s b/src/runtime/internal/syscall/asm_linux_riscv64.s
new file mode 100644 (file)
index 0000000..a8652fd
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOV     num+0(FP), A7   // syscall entry
+       MOV     a1+8(FP), A0
+       MOV     a2+16(FP), A1
+       MOV     a3+24(FP), A2
+       MOV     a4+32(FP), A3
+       MOV     a5+40(FP), A4
+       MOV     a6+48(FP), A5
+       ECALL
+       MOV     $-4096, T0
+       BLTU    T0, A0, err
+       MOV     A0, r1+56(FP)
+       MOV     A1, r2+64(FP)
+       MOV     ZERO, errno+72(FP)
+       RET
+err:
+       MOV     $-1, T0
+       MOV     T0, r1+56(FP)
+       MOV     ZERO, r2+64(FP)
+       SUB     A0, ZERO, A0
+       MOV     A0, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/asm_linux_s390x.s b/src/runtime/internal/syscall/asm_linux_s390x.s
new file mode 100644 (file)
index 0000000..1b27f29
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
+TEXT ·Syscall6(SB),NOSPLIT,$0-80
+       MOVD    num+0(FP), R1   // syscall entry
+       MOVD    a1+8(FP), R2
+       MOVD    a2+16(FP), R3
+       MOVD    a3+24(FP), R4
+       MOVD    a4+32(FP), R5
+       MOVD    a5+40(FP), R6
+       MOVD    a6+48(FP), R7
+       SYSCALL
+       MOVD    $0xfffffffffffff001, R8
+       CMPUBLT R2, R8, ok
+       MOVD    $-1, r1+56(FP)
+       MOVD    $0, r2+64(FP)
+       NEG     R2, R2
+       MOVD    R2, errno+72(FP)
+       RET
+ok:
+       MOVD    R2, r1+56(FP)
+       MOVD    R3, r2+64(FP)
+       MOVD    $0, errno+72(FP)
+       RET
diff --git a/src/runtime/internal/syscall/syscall_linux.go b/src/runtime/internal/syscall/syscall_linux.go
new file mode 100644 (file)
index 0000000..06d5f21
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package syscall provides the syscall primitives required for the runtime.
+package syscall
+
+// TODO(https://go.dev/issue/51087): This package is incomplete and currently
+// only contains very minimal support for Linux.
+
+// Syscall6 calls system call number 'num' with arguments a1-6.
+func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)