Skip to content

Commit 45d6bc7

Browse files
qmuntalgopherbot
authored andcommitted
runtime: unify arm64 entry point code
There is a lot of duplication in how arm64 OSes handle entry points. Do as amd64, have all the logic in a common function. Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-windows-arm64 Change-Id: I370c25c3c4b107b525aba14e9dcac34a02d9872e Reviewed-on: https://go-review.googlesource.com/c/go/+/706175 Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Quim Muntal <[email protected]>
1 parent fdea7da commit 45d6bc7

10 files changed

+111
-337
lines changed

src/runtime/asm_arm64.s

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,78 @@
77
#include "tls_arm64.h"
88
#include "funcdata.h"
99
#include "textflag.h"
10+
#include "cgo/abi_arm64.h"
11+
12+
// _rt0_arm64 is common startup code for most arm64 systems when using
13+
// internal linking. This is the entry point for the program from the
14+
// kernel for an ordinary -buildmode=exe program. The stack holds the
15+
// number of arguments and the C-style argv.
16+
TEXT _rt0_arm64(SB),NOSPLIT,$0
17+
MOVD 0(RSP), R0 // argc
18+
ADD $8, RSP, R1 // argv
19+
JMP runtime·rt0_go(SB)
20+
21+
// main is common startup code for most amd64 systems when using
22+
// external linking. The C startup code will call the symbol "main"
23+
// passing argc and argv in the usual C ABI registers R0 and R1.
24+
TEXT main(SB),NOSPLIT,$0
25+
JMP runtime·rt0_go(SB)
26+
27+
// _rt0_arm64_lib is common startup code for most arm64 systems when
28+
// using -buildmode=c-archive or -buildmode=c-shared. The linker will
29+
// arrange to invoke this function as a global constructor (for
30+
// c-archive) or when the shared library is loaded (for c-shared).
31+
// We expect argc and argv to be passed in the usual C ABI registers
32+
// R0 and R1.
33+
TEXT _rt0_arm64_lib(SB),NOSPLIT,$184
34+
// Preserve callee-save registers.
35+
SAVE_R19_TO_R28(24)
36+
SAVE_F8_TO_F15(104)
37+
38+
// Initialize g as null in case of using g later e.g. sigaction in cgo_sigaction.go
39+
MOVD ZR, g
40+
41+
MOVD R0, _rt0_arm64_lib_argc<>(SB)
42+
MOVD R1, _rt0_arm64_lib_argv<>(SB)
43+
44+
// Synchronous initialization.
45+
MOVD $runtime·libpreinit(SB), R4
46+
BL (R4)
47+
48+
// Create a new thread to do the runtime initialization and return.
49+
MOVD _cgo_sys_thread_create(SB), R4
50+
CBZ R4, nocgo
51+
MOVD $_rt0_arm64_lib_go(SB), R0
52+
MOVD $0, R1
53+
SUB $16, RSP // reserve 16 bytes for sp-8 where fp may be saved.
54+
BL (R4)
55+
ADD $16, RSP
56+
B restore
57+
58+
nocgo:
59+
MOVD $0x800000, R0 // stacksize = 8192KB
60+
MOVD $_rt0_arm64_lib_go(SB), R1
61+
MOVD R0, 8(RSP)
62+
MOVD R1, 16(RSP)
63+
MOVD $runtime·newosproc0(SB),R4
64+
BL (R4)
65+
66+
restore:
67+
// Restore callee-save registers.
68+
RESTORE_R19_TO_R28(24)
69+
RESTORE_F8_TO_F15(104)
70+
RET
71+
72+
TEXT _rt0_arm64_lib_go(SB),NOSPLIT,$0
73+
MOVD _rt0_arm64_lib_argc<>(SB), R0
74+
MOVD _rt0_arm64_lib_argv<>(SB), R1
75+
MOVD $runtime·rt0_go(SB),R4
76+
B (R4)
77+
78+
DATA _rt0_arm64_lib_argc<>(SB)/8, $0
79+
GLOBL _rt0_arm64_lib_argc<>(SB),NOPTR, $8
80+
DATA _rt0_arm64_lib_argv<>(SB)/8, $0
81+
GLOBL _rt0_arm64_lib_argv<>(SB),NOPTR, $8
1082

1183
#ifdef GOARM64_LSE
1284
DATA no_lse_msg<>+0x00(SB)/64, $"This program can only run on ARM64 processors with LSE support.\n"

src/runtime/os_windows.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,14 @@ func newosproc(mp *m) {
777777
//
778778
//go:nowritebarrierrec
779779
//go:nosplit
780-
func newosproc0(mp *m, stk unsafe.Pointer) {
781-
// TODO: this is completely broken. The args passed to newosproc0 (in asm_amd64.s)
782-
// are stacksize and function, not *m and stack.
783-
// Check os_linux.go for an implementation that might actually work.
780+
func newosproc0(stacksize uintptr, fn uintptr) {
784781
throw("bad newosproc0")
785782
}
786783

784+
//go:nosplit
785+
//go:nowritebarrierrec
786+
func libpreinit() {}
787+
787788
func exitThread(wait *atomic.Uint32) {
788789
// We should never reach exitThread on Windows because we let
789790
// the OS clean up threads.

src/runtime/rt0_android_arm64.s

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
#include "textflag.h"
66

7-
TEXT _rt0_arm64_android(SB),NOSPLIT|NOFRAME,$0
8-
MOVD $_rt0_arm64_linux(SB), R4
9-
B (R4)
7+
TEXT _rt0_arm64_android(SB),NOSPLIT,$0
8+
JMP _rt0_arm64(SB)
109

1110
// When building with -buildmode=c-shared, this symbol is called when the shared
1211
// library is loaded.
13-
TEXT _rt0_arm64_android_lib(SB),NOSPLIT|NOFRAME,$0
12+
TEXT _rt0_arm64_android_lib(SB),NOSPLIT,$0
1413
MOVW $1, R0 // argc
1514
MOVD $_rt0_arm64_android_argv(SB), R1 // **argv
16-
MOVD $_rt0_arm64_linux_lib(SB), R4
17-
B (R4)
15+
JMP _rt0_arm64_lib(SB)
1816

1917
DATA _rt0_arm64_android_argv+0x00(SB)/8,$_rt0_arm64_android_argv0(SB)
2018
DATA _rt0_arm64_android_argv+0x08(SB)/8,$0 // end argv

src/runtime/rt0_darwin_arm64.s

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,13 @@
33
// license that can be found in the LICENSE file.
44

55
#include "textflag.h"
6-
#include "cgo/abi_arm64.h"
76

8-
TEXT _rt0_arm64_darwin(SB),NOSPLIT|NOFRAME,$0
9-
MOVD $runtime·rt0_go(SB), R2
10-
BL (R2)
11-
exit:
12-
MOVD $0, R0
13-
MOVD $1, R16 // sys_exit
14-
SVC $0x80
15-
B exit
7+
TEXT _rt0_arm64_darwin(SB),NOSPLIT,$0
8+
// Darwin puts argc and argv in R0 and R1,
9+
// so there is no need to go through _rt0_arm64.
10+
JMP runtime·rt0_go(SB)
1611

1712
// When linking with -buildmode=c-archive or -buildmode=c-shared,
1813
// this symbol is called from a global initialization function.
19-
//
20-
// Note that all currently shipping darwin/arm64 platforms require
21-
// cgo and do not support c-shared.
22-
TEXT _rt0_arm64_darwin_lib(SB),NOSPLIT,$152
23-
// Preserve callee-save registers.
24-
SAVE_R19_TO_R28(8)
25-
SAVE_F8_TO_F15(88)
26-
27-
MOVD R0, _rt0_arm64_darwin_lib_argc<>(SB)
28-
MOVD R1, _rt0_arm64_darwin_lib_argv<>(SB)
29-
30-
MOVD $0, g // initialize g to nil
31-
32-
// Synchronous initialization.
33-
MOVD $runtime·libpreinit(SB), R4
34-
BL (R4)
35-
36-
// Create a new thread to do the runtime initialization and return.
37-
MOVD _cgo_sys_thread_create(SB), R4
38-
MOVD $_rt0_arm64_darwin_lib_go(SB), R0
39-
MOVD $0, R1
40-
SUB $16, RSP // reserve 16 bytes for sp-8 where fp may be saved.
41-
BL (R4)
42-
ADD $16, RSP
43-
44-
// Restore callee-save registers.
45-
RESTORE_R19_TO_R28(8)
46-
RESTORE_F8_TO_F15(88)
47-
48-
RET
49-
50-
TEXT _rt0_arm64_darwin_lib_go(SB),NOSPLIT,$0
51-
MOVD _rt0_arm64_darwin_lib_argc<>(SB), R0
52-
MOVD _rt0_arm64_darwin_lib_argv<>(SB), R1
53-
MOVD $runtime·rt0_go(SB), R4
54-
B (R4)
55-
56-
DATA _rt0_arm64_darwin_lib_argc<>(SB)/8, $0
57-
GLOBL _rt0_arm64_darwin_lib_argc<>(SB),NOPTR, $8
58-
DATA _rt0_arm64_darwin_lib_argv<>(SB)/8, $0
59-
GLOBL _rt0_arm64_darwin_lib_argv<>(SB),NOPTR, $8
60-
61-
// external linking entry point.
62-
TEXT main(SB),NOSPLIT|NOFRAME,$0
63-
JMP _rt0_arm64_darwin(SB)
14+
TEXT _rt0_arm64_darwin_lib(SB),NOSPLIT,$0
15+
JMP _rt0_arm64_lib(SB)

src/runtime/rt0_freebsd_arm64.s

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,12 @@
33
// license that can be found in the LICENSE file.
44

55
#include "textflag.h"
6-
#include "cgo/abi_arm64.h"
76

87
// On FreeBSD argc/argv are passed in R0, not RSP
9-
TEXT _rt0_arm64_freebsd(SB),NOSPLIT|NOFRAME,$0
10-
ADD $8, R0, R1 // argv
11-
MOVD 0(R0), R0 // argc
12-
BL main(SB)
8+
TEXT _rt0_arm64_freebsd(SB),NOSPLIT,$0
9+
JMP _rt0_arm64(SB)
1310

1411
// When building with -buildmode=c-shared, this symbol is called when the shared
1512
// library is loaded.
16-
TEXT _rt0_arm64_freebsd_lib(SB),NOSPLIT,$184
17-
// Preserve callee-save registers.
18-
SAVE_R19_TO_R28(24)
19-
SAVE_F8_TO_F15(104)
20-
21-
// Initialize g as null in case of using g later e.g. sigaction in cgo_sigaction.go
22-
MOVD ZR, g
23-
24-
MOVD R0, _rt0_arm64_freebsd_lib_argc<>(SB)
25-
MOVD R1, _rt0_arm64_freebsd_lib_argv<>(SB)
26-
27-
// Synchronous initialization.
28-
MOVD $runtime·libpreinit(SB), R4
29-
BL (R4)
30-
31-
// Create a new thread to do the runtime initialization and return.
32-
MOVD _cgo_sys_thread_create(SB), R4
33-
CBZ R4, nocgo
34-
MOVD $_rt0_arm64_freebsd_lib_go(SB), R0
35-
MOVD $0, R1
36-
SUB $16, RSP // reserve 16 bytes for sp-8 where fp may be saved.
37-
BL (R4)
38-
ADD $16, RSP
39-
B restore
40-
41-
nocgo:
42-
MOVD $0x800000, R0 // stacksize = 8192KB
43-
MOVD $_rt0_arm64_freebsd_lib_go(SB), R1
44-
MOVD R0, 8(RSP)
45-
MOVD R1, 16(RSP)
46-
MOVD $runtime·newosproc0(SB),R4
47-
BL (R4)
48-
49-
restore:
50-
// Restore callee-save registers.
51-
RESTORE_R19_TO_R28(24)
52-
RESTORE_F8_TO_F15(104)
53-
RET
54-
55-
TEXT _rt0_arm64_freebsd_lib_go(SB),NOSPLIT,$0
56-
MOVD _rt0_arm64_freebsd_lib_argc<>(SB), R0
57-
MOVD _rt0_arm64_freebsd_lib_argv<>(SB), R1
58-
MOVD $runtime·rt0_go(SB),R4
59-
B (R4)
60-
61-
DATA _rt0_arm64_freebsd_lib_argc<>(SB)/8, $0
62-
GLOBL _rt0_arm64_freebsd_lib_argc<>(SB),NOPTR, $8
63-
DATA _rt0_arm64_freebsd_lib_argv<>(SB)/8, $0
64-
GLOBL _rt0_arm64_freebsd_lib_argv<>(SB),NOPTR, $8
65-
66-
67-
TEXT main(SB),NOSPLIT|NOFRAME,$0
68-
MOVD $runtime·rt0_go(SB), R2
69-
BL (R2)
70-
exit:
71-
MOVD $0, R0
72-
MOVD $1, R8 // SYS_exit
73-
SVC
74-
B exit
13+
TEXT _rt0_arm64_freebsd_lib(SB),NOSPLIT,$0
14+
JMP _rt0_arm64_lib(SB)

src/runtime/rt0_ios_arm64.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ TEXT _rt0_arm64_ios(SB),NOSPLIT|NOFRAME,$0
1111

1212
// library entry point.
1313
TEXT _rt0_arm64_ios_lib(SB),NOSPLIT|NOFRAME,$0
14-
JMP _rt0_arm64_darwin_lib(SB)
14+
JMP _rt0_arm64_lib(SB)

src/runtime/rt0_linux_arm64.s

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,11 @@
33
// license that can be found in the LICENSE file.
44

55
#include "textflag.h"
6-
#include "cgo/abi_arm64.h"
76

8-
TEXT _rt0_arm64_linux(SB),NOSPLIT|NOFRAME,$0
9-
MOVD 0(RSP), R0 // argc
10-
ADD $8, RSP, R1 // argv
11-
BL main(SB)
7+
TEXT _rt0_arm64_linux(SB),NOSPLIT,$0
8+
JMP _rt0_arm64(SB)
129

1310
// When building with -buildmode=c-shared, this symbol is called when the shared
1411
// library is loaded.
15-
TEXT _rt0_arm64_linux_lib(SB),NOSPLIT,$184
16-
// Preserve callee-save registers.
17-
SAVE_R19_TO_R28(24)
18-
SAVE_F8_TO_F15(104)
19-
20-
// Initialize g as null in case of using g later e.g. sigaction in cgo_sigaction.go
21-
MOVD ZR, g
22-
23-
MOVD R0, _rt0_arm64_linux_lib_argc<>(SB)
24-
MOVD R1, _rt0_arm64_linux_lib_argv<>(SB)
25-
26-
// Synchronous initialization.
27-
MOVD $runtime·libpreinit(SB), R4
28-
BL (R4)
29-
30-
// Create a new thread to do the runtime initialization and return.
31-
MOVD _cgo_sys_thread_create(SB), R4
32-
CBZ R4, nocgo
33-
MOVD $_rt0_arm64_linux_lib_go(SB), R0
34-
MOVD $0, R1
35-
SUB $16, RSP // reserve 16 bytes for sp-8 where fp may be saved.
36-
BL (R4)
37-
ADD $16, RSP
38-
B restore
39-
40-
nocgo:
41-
MOVD $0x800000, R0 // stacksize = 8192KB
42-
MOVD $_rt0_arm64_linux_lib_go(SB), R1
43-
MOVD R0, 8(RSP)
44-
MOVD R1, 16(RSP)
45-
MOVD $runtime·newosproc0(SB),R4
46-
BL (R4)
47-
48-
restore:
49-
// Restore callee-save registers.
50-
RESTORE_R19_TO_R28(24)
51-
RESTORE_F8_TO_F15(104)
52-
RET
53-
54-
TEXT _rt0_arm64_linux_lib_go(SB),NOSPLIT,$0
55-
MOVD _rt0_arm64_linux_lib_argc<>(SB), R0
56-
MOVD _rt0_arm64_linux_lib_argv<>(SB), R1
57-
MOVD $runtime·rt0_go(SB),R4
58-
B (R4)
59-
60-
DATA _rt0_arm64_linux_lib_argc<>(SB)/8, $0
61-
GLOBL _rt0_arm64_linux_lib_argc<>(SB),NOPTR, $8
62-
DATA _rt0_arm64_linux_lib_argv<>(SB)/8, $0
63-
GLOBL _rt0_arm64_linux_lib_argv<>(SB),NOPTR, $8
64-
65-
66-
TEXT main(SB),NOSPLIT|NOFRAME,$0
67-
MOVD $runtime·rt0_go(SB), R2
68-
BL (R2)
69-
exit:
70-
MOVD $0, R0
71-
MOVD $94, R8 // sys_exit
72-
SVC
73-
B exit
12+
TEXT _rt0_arm64_linux_lib(SB),NOSPLIT,$0
13+
JMP _rt0_arm64_lib(SB)

0 commit comments

Comments
 (0)