1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:20:04 -07:00

runtime/internal/syscall: new package for linux

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>
This commit is contained in:
Michael Pratt 2022-02-08 16:45:14 -05:00
parent 7a132d6f4e
commit 0b321c9a7c
12 changed files with 292 additions and 1 deletions

View 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",

View File

@ -88,6 +88,7 @@ var depsRules = `
< internal/itoa
< internal/unsafeheader
< runtime/internal/sys
< runtime/internal/syscall
< runtime/internal/atomic
< runtime/internal/math
< runtime

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)