Skip to content

Commit 0b321c9

Browse files
committed
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 <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 7a132d6 commit 0b321c9

12 files changed

+292
-1
lines changed

src/cmd/compile/internal/base/base.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ func Compiling(pkgs []string) bool {
6262
// at best instrumentation would cause infinite recursion.
6363
var NoInstrumentPkgs = []string{
6464
"runtime/internal/atomic",
65-
"runtime/internal/sys",
6665
"runtime/internal/math",
66+
"runtime/internal/sys",
67+
"runtime/internal/syscall",
6768
"runtime",
6869
"runtime/race",
6970
"runtime/msan",

src/go/build/deps_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ var depsRules = `
8888
< internal/itoa
8989
< internal/unsafeheader
9090
< runtime/internal/sys
91+
< runtime/internal/syscall
9192
< runtime/internal/atomic
9293
< runtime/internal/math
9394
< runtime
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// See ../sys_linux_386.s for the reason why we always use int 0x80
8+
// instead of the glibc-specific "CALL 0x10(GS)".
9+
#define INVOKE_SYSCALL INT $0x80
10+
11+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
12+
//
13+
// Syscall # in AX, args in BX CX DX SI DI BP, return in AX
14+
TEXT ·Syscall6(SB),NOSPLIT,$0-40
15+
MOVL num+0(FP), AX // syscall entry
16+
MOVL a1+4(FP), BX
17+
MOVL a2+8(FP), CX
18+
MOVL a3+12(FP), DX
19+
MOVL a4+16(FP), SI
20+
MOVL a5+20(FP), DI
21+
MOVL a6+24(FP), BP
22+
INVOKE_SYSCALL
23+
CMPL AX, $0xfffff001
24+
JLS ok
25+
MOVL $-1, r1+28(FP)
26+
MOVL $0, r2+32(FP)
27+
NEGL AX
28+
MOVL AX, errno+36(FP)
29+
RET
30+
ok:
31+
MOVL AX, r1+28(FP)
32+
MOVL DX, r2+32(FP)
33+
MOVL $0, errno+36(FP)
34+
RET
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
8+
//
9+
// Syscall # in AX, args in DI SI DX R10 R8 R9, return in AX DX.
10+
//
11+
// Note that this differs from "standard" ABI convention, which would pass 4th
12+
// arg in CX, not R10.
13+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
14+
MOVQ num+0(FP), AX // syscall entry
15+
MOVQ a1+8(FP), DI
16+
MOVQ a2+16(FP), SI
17+
MOVQ a3+24(FP), DX
18+
MOVQ a4+32(FP), R10
19+
MOVQ a5+40(FP), R8
20+
MOVQ a6+48(FP), R9
21+
SYSCALL
22+
CMPQ AX, $0xfffffffffffff001
23+
JLS ok
24+
MOVQ $-1, r1+56(FP)
25+
MOVQ $0, r2+64(FP)
26+
NEGQ AX
27+
MOVQ AX, errno+72(FP)
28+
RET
29+
ok:
30+
MOVQ AX, r1+56(FP)
31+
MOVQ DX, r2+64(FP)
32+
MOVQ $0, errno+72(FP)
33+
RET
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
8+
TEXT ·Syscall6(SB),NOSPLIT,$0-40
9+
MOVW num+0(FP), R7 // syscall entry
10+
MOVW a1+4(FP), R0
11+
MOVW a2+8(FP), R1
12+
MOVW a3+12(FP), R2
13+
MOVW a4+16(FP), R3
14+
MOVW a5+20(FP), R4
15+
MOVW a6+24(FP), R5
16+
SWI $0
17+
MOVW $0xfffff001, R6
18+
CMP R6, R0
19+
BLS ok
20+
MOVW $-1, R1
21+
MOVW R1, r1+28(FP)
22+
MOVW $0, R2
23+
MOVW R2, r2+32(FP)
24+
RSB $0, R0, R0
25+
MOVW R0, errno+36(FP)
26+
RET
27+
ok:
28+
MOVW R0, r1+28(FP)
29+
MOVW R1, r2+32(FP)
30+
MOVW $0, R0
31+
MOVW R0, errno+36(FP)
32+
RET
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
8+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
9+
MOVD num+0(FP), R8 // syscall entry
10+
MOVD a1+8(FP), R0
11+
MOVD a2+16(FP), R1
12+
MOVD a3+24(FP), R2
13+
MOVD a4+32(FP), R3
14+
MOVD a5+40(FP), R4
15+
MOVD a6+48(FP), R5
16+
SVC
17+
CMN $4095, R0
18+
BCC ok
19+
MOVD $-1, R4
20+
MOVD R4, r1+56(FP)
21+
MOVD ZR, r2+64(FP)
22+
NEG R0, R0
23+
MOVD R0, errno+72(FP)
24+
RET
25+
ok:
26+
MOVD R0, r1+56(FP)
27+
MOVD R1, r2+64(FP)
28+
MOVD ZR, errno+72(FP)
29+
RET
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && (mips64 || mips64le)
6+
7+
#include "textflag.h"
8+
9+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
10+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
11+
MOVV num+0(FP), R2 // syscall entry
12+
MOVV a1+8(FP), R4
13+
MOVV a2+16(FP), R5
14+
MOVV a3+24(FP), R6
15+
MOVV a4+32(FP), R7
16+
MOVV a5+40(FP), R8
17+
MOVV a6+48(FP), R9
18+
SYSCALL
19+
BEQ R7, ok
20+
MOVV $-1, R1
21+
MOVV R1, r1+56(FP)
22+
MOVV R0, r2+64(FP)
23+
MOVV R2, errno+72(FP)
24+
RET
25+
ok:
26+
MOVV R2, r1+56(FP)
27+
MOVV R3, r2+64(FP)
28+
MOVV R0, errno+72(FP)
29+
RET
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && (mips || mipsle)
6+
7+
#include "textflag.h"
8+
9+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
10+
//
11+
// The 5th and 6th arg go at sp+16, sp+20.
12+
// Note that frame size of 20 means that 24 bytes gets reserved on stack.
13+
TEXT ·Syscall6(SB),NOSPLIT,$20-40
14+
MOVW num+0(FP), R2 // syscall entry
15+
MOVW a1+4(FP), R4
16+
MOVW a2+8(FP), R5
17+
MOVW a3+12(FP), R6
18+
MOVW a4+16(FP), R7
19+
MOVW a5+20(FP), R8
20+
MOVW a6+24(FP), R9
21+
MOVW R8, 16(R29)
22+
MOVW R9, 20(R29)
23+
SYSCALL
24+
BEQ R7, ok
25+
MOVW $-1, R1
26+
MOVW R1, r1+28(FP)
27+
MOVW R0, r2+32(FP)
28+
MOVW R2, errno+36(FP)
29+
RET
30+
ok:
31+
MOVW R2, r1+28(FP)
32+
MOVW R3, r2+32(FP)
33+
MOVW R0, errno+36(FP)
34+
RET
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && (ppc64 || ppc64le)
6+
7+
#include "textflag.h"
8+
9+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
10+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
11+
MOVD num+0(FP), R9 // syscall entry
12+
MOVD a1+8(FP), R3
13+
MOVD a2+16(FP), R4
14+
MOVD a3+24(FP), R5
15+
MOVD a4+32(FP), R6
16+
MOVD a5+40(FP), R7
17+
MOVD a6+48(FP), R8
18+
SYSCALL R9
19+
BVC ok
20+
MOVD $-1, R4
21+
MOVD R4, r1+56(FP)
22+
MOVD R0, r2+64(FP)
23+
MOVD R3, errno+72(FP)
24+
RET
25+
ok:
26+
MOVD R3, r1+56(FP)
27+
MOVD R4, r2+64(FP)
28+
MOVD R0, errno+72(FP)
29+
RET
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
8+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
9+
MOV num+0(FP), A7 // syscall entry
10+
MOV a1+8(FP), A0
11+
MOV a2+16(FP), A1
12+
MOV a3+24(FP), A2
13+
MOV a4+32(FP), A3
14+
MOV a5+40(FP), A4
15+
MOV a6+48(FP), A5
16+
ECALL
17+
MOV $-4096, T0
18+
BLTU T0, A0, err
19+
MOV A0, r1+56(FP)
20+
MOV A1, r2+64(FP)
21+
MOV ZERO, errno+72(FP)
22+
RET
23+
err:
24+
MOV $-1, T0
25+
MOV T0, r1+56(FP)
26+
MOV ZERO, r2+64(FP)
27+
SUB A0, ZERO, A0
28+
MOV A0, errno+72(FP)
29+
RET
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)
8+
TEXT ·Syscall6(SB),NOSPLIT,$0-80
9+
MOVD num+0(FP), R1 // syscall entry
10+
MOVD a1+8(FP), R2
11+
MOVD a2+16(FP), R3
12+
MOVD a3+24(FP), R4
13+
MOVD a4+32(FP), R5
14+
MOVD a5+40(FP), R6
15+
MOVD a6+48(FP), R7
16+
SYSCALL
17+
MOVD $0xfffffffffffff001, R8
18+
CMPUBLT R2, R8, ok
19+
MOVD $-1, r1+56(FP)
20+
MOVD $0, r2+64(FP)
21+
NEG R2, R2
22+
MOVD R2, errno+72(FP)
23+
RET
24+
ok:
25+
MOVD R2, r1+56(FP)
26+
MOVD R3, r2+64(FP)
27+
MOVD $0, errno+72(FP)
28+
RET
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package syscall provides the syscall primitives required for the runtime.
6+
package syscall
7+
8+
// TODO(https://go.dev/issue/51087): This package is incomplete and currently
9+
// only contains very minimal support for Linux.
10+
11+
// Syscall6 calls system call number 'num' with arguments a1-6.
12+
func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr)

0 commit comments

Comments
 (0)