Skip to content

Commit 3d40895

Browse files
committed
runtime: switch openbsd/arm64 to pthreads
This switches openbsd/arm64 to thread creation via pthreads, rather than doing direct system calls. Update #36435 Change-Id: I7cf60fa954f92628e05f15d2732833a2fbdccdb9 Reviewed-on: https://go-review.googlesource.com/c/go/+/250182 Trust: Joel Sing <[email protected]> Reviewed-by: Cherry Zhang <[email protected]> Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent d95ca91 commit 3d40895

8 files changed

+195
-108
lines changed

src/runtime/defs_openbsd_arm64.go

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
_SA_RESTART = 0x2
3232
_SA_ONSTACK = 0x1
3333

34+
_PTHREAD_CREATE_DETACHED = 0x1
35+
3436
_SIGHUP = 0x1
3537
_SIGINT = 0x2
3638
_SIGQUIT = 0x3
@@ -157,3 +159,10 @@ type keventt struct {
157159
data int64
158160
udata *byte
159161
}
162+
163+
type pthread uintptr
164+
type pthreadattr uintptr
165+
type pthreadcond uintptr
166+
type pthreadcondattr uintptr
167+
type pthreadmutex uintptr
168+
type pthreadmutexattr uintptr

src/runtime/os_openbsd_libc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build openbsd,amd64
5+
// +build openbsd,amd64 openbsd,arm64
66

77
package runtime
88

src/runtime/os_openbsd_syscall.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
// +build openbsd,!amd64
6+
// +build openbsd,!arm64
67

78
package runtime
89

src/runtime/proc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ func mStackIsSystemAllocated() bool {
12241224
return true
12251225
case "openbsd":
12261226
switch GOARCH {
1227-
case "amd64":
1227+
case "amd64", "arm64":
12281228
return true
12291229
}
12301230
}

src/runtime/sys_libc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build darwin openbsd,amd64
5+
// +build darwin openbsd,amd64 openbsd,arm64
66

77
package runtime
88

src/runtime/sys_openbsd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build openbsd,amd64
5+
// +build openbsd,amd64 openbsd,arm64
66

77
package runtime
88

src/runtime/sys_openbsd_arm64.s

+177-104
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// license that can be found in the LICENSE file.
44
//
55
// System calls and other sys.stuff for arm64, OpenBSD
6-
// /usr/src/sys/kern/syscalls.master for syscall numbers.
6+
// System calls are implemented in libc/libpthread, this file
7+
// contains trampolines that convert from Go to C calling convention.
8+
// Some direct system call implementations currently remain.
79
//
810

911
#include "go_asm.h"
@@ -24,6 +26,180 @@
2426
NOOP; \
2527
NOOP
2628

29+
// mstart_stub is the first function executed on a new thread started by pthread_create.
30+
// It just does some low-level setup and then calls mstart.
31+
// Note: called with the C calling convention.
32+
TEXT runtime·mstart_stub(SB),NOSPLIT,$160
33+
// R0 points to the m.
34+
// We are already on m's g0 stack.
35+
36+
// Save callee-save registers.
37+
MOVD R19, 8(RSP)
38+
MOVD R20, 16(RSP)
39+
MOVD R21, 24(RSP)
40+
MOVD R22, 32(RSP)
41+
MOVD R23, 40(RSP)
42+
MOVD R24, 48(RSP)
43+
MOVD R25, 56(RSP)
44+
MOVD R26, 64(RSP)
45+
MOVD R27, 72(RSP)
46+
MOVD g, 80(RSP)
47+
MOVD R29, 88(RSP)
48+
FMOVD F8, 96(RSP)
49+
FMOVD F9, 104(RSP)
50+
FMOVD F10, 112(RSP)
51+
FMOVD F11, 120(RSP)
52+
FMOVD F12, 128(RSP)
53+
FMOVD F13, 136(RSP)
54+
FMOVD F14, 144(RSP)
55+
FMOVD F15, 152(RSP)
56+
57+
MOVD m_g0(R0), g
58+
BL runtime·save_g(SB)
59+
60+
BL runtime·mstart(SB)
61+
62+
// Restore callee-save registers.
63+
MOVD 8(RSP), R19
64+
MOVD 16(RSP), R20
65+
MOVD 24(RSP), R21
66+
MOVD 32(RSP), R22
67+
MOVD 40(RSP), R23
68+
MOVD 48(RSP), R24
69+
MOVD 56(RSP), R25
70+
MOVD 64(RSP), R26
71+
MOVD 72(RSP), R27
72+
MOVD 80(RSP), g
73+
MOVD 88(RSP), R29
74+
FMOVD 96(RSP), F8
75+
FMOVD 104(RSP), F9
76+
FMOVD 112(RSP), F10
77+
FMOVD 120(RSP), F11
78+
FMOVD 128(RSP), F12
79+
FMOVD 136(RSP), F13
80+
FMOVD 144(RSP), F14
81+
FMOVD 152(RSP), F15
82+
83+
// Go is all done with this OS thread.
84+
// Tell pthread everything is ok (we never join with this thread, so
85+
// the value here doesn't really matter).
86+
MOVD $0, R0
87+
88+
RET
89+
90+
TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
91+
MOVW sig+8(FP), R0
92+
MOVD info+16(FP), R1
93+
MOVD ctx+24(FP), R2
94+
MOVD fn+0(FP), R11
95+
BL (R11) // Alignment for ELF ABI?
96+
RET
97+
98+
TEXT runtime·sigtramp(SB),NOSPLIT,$192
99+
// Save callee-save registers in the case of signal forwarding.
100+
// Please refer to https://golang.org/issue/31827 .
101+
MOVD R19, 8*4(RSP)
102+
MOVD R20, 8*5(RSP)
103+
MOVD R21, 8*6(RSP)
104+
MOVD R22, 8*7(RSP)
105+
MOVD R23, 8*8(RSP)
106+
MOVD R24, 8*9(RSP)
107+
MOVD R25, 8*10(RSP)
108+
MOVD R26, 8*11(RSP)
109+
MOVD R27, 8*12(RSP)
110+
MOVD g, 8*13(RSP)
111+
MOVD R29, 8*14(RSP)
112+
FMOVD F8, 8*15(RSP)
113+
FMOVD F9, 8*16(RSP)
114+
FMOVD F10, 8*17(RSP)
115+
FMOVD F11, 8*18(RSP)
116+
FMOVD F12, 8*19(RSP)
117+
FMOVD F13, 8*20(RSP)
118+
FMOVD F14, 8*21(RSP)
119+
FMOVD F15, 8*22(RSP)
120+
121+
// If called from an external code context, g will not be set.
122+
// Save R0, since runtime·load_g will clobber it.
123+
MOVW R0, 8(RSP) // signum
124+
BL runtime·load_g(SB)
125+
126+
MOVD R1, 16(RSP)
127+
MOVD R2, 24(RSP)
128+
BL runtime·sigtrampgo(SB)
129+
130+
// Restore callee-save registers.
131+
MOVD 8*4(RSP), R19
132+
MOVD 8*5(RSP), R20
133+
MOVD 8*6(RSP), R21
134+
MOVD 8*7(RSP), R22
135+
MOVD 8*8(RSP), R23
136+
MOVD 8*9(RSP), R24
137+
MOVD 8*10(RSP), R25
138+
MOVD 8*11(RSP), R26
139+
MOVD 8*12(RSP), R27
140+
MOVD 8*13(RSP), g
141+
MOVD 8*14(RSP), R29
142+
FMOVD 8*15(RSP), F8
143+
FMOVD 8*16(RSP), F9
144+
FMOVD 8*17(RSP), F10
145+
FMOVD 8*18(RSP), F11
146+
FMOVD 8*19(RSP), F12
147+
FMOVD 8*20(RSP), F13
148+
FMOVD 8*21(RSP), F14
149+
FMOVD 8*22(RSP), F15
150+
151+
RET
152+
153+
//
154+
// These trampolines help convert from Go calling convention to C calling convention.
155+
// They should be called with asmcgocall.
156+
// A pointer to the arguments is passed in R0.
157+
// A single int32 result is returned in R0.
158+
// (For more results, make an args/results structure.)
159+
TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
160+
MOVD 0(R0), R0 // arg 1 - attr
161+
CALL libc_pthread_attr_init(SB)
162+
RET
163+
164+
TEXT runtime·pthread_attr_destroy_trampoline(SB),NOSPLIT,$0
165+
MOVD 0(R0), R0 // arg 1 - attr
166+
CALL libc_pthread_attr_destroy(SB)
167+
RET
168+
169+
TEXT runtime·pthread_attr_getstacksize_trampoline(SB),NOSPLIT,$0
170+
MOVD 8(R0), R1 // arg 2 - size
171+
MOVD 0(R0), R0 // arg 1 - attr
172+
CALL libc_pthread_attr_getstacksize(SB)
173+
RET
174+
175+
TEXT runtime·pthread_attr_setdetachstate_trampoline(SB),NOSPLIT,$0
176+
MOVD 8(R0), R1 // arg 2 - state
177+
MOVD 0(R0), R0 // arg 1 - attr
178+
CALL libc_pthread_attr_setdetachstate(SB)
179+
RET
180+
181+
TEXT runtime·pthread_create_trampoline(SB),NOSPLIT,$0
182+
MOVD 0(R0), R1 // arg 2 - attr
183+
MOVD 8(R0), R2 // arg 3 - start
184+
MOVD 16(R0), R3 // arg 4 - arg
185+
SUB $16, RSP
186+
MOVD RSP, R0 // arg 1 - &threadid (discard)
187+
CALL libc_pthread_create(SB)
188+
ADD $16, RSP
189+
RET
190+
191+
TEXT runtime·pthread_self_trampoline(SB),NOSPLIT,$0
192+
MOVD R0, R19 // pointer to args
193+
CALL libc_pthread_self(SB)
194+
MOVD R0, 0(R19) // return value
195+
RET
196+
197+
TEXT runtime·pthread_kill_trampoline(SB),NOSPLIT,$0
198+
MOVW 8(R0), R1 // arg 2 - sig
199+
MOVD 0(R0), R0 // arg 1 - thread
200+
CALL libc_pthread_kill(SB)
201+
RET
202+
27203
// Exit the entire program (like C exit)
28204
TEXT runtime·exit(SB),NOSPLIT|NOFRAME,$0
29205
MOVW code+0(FP), R0 // arg 1 - status
@@ -248,109 +424,6 @@ TEXT runtime·obsdsigprocmask(SB),NOSPLIT,$0
248424
MOVW R0, ret+8(FP)
249425
RET
250426

251-
TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
252-
MOVW sig+8(FP), R0
253-
MOVD info+16(FP), R1
254-
MOVD ctx+24(FP), R2
255-
MOVD fn+0(FP), R11
256-
BL (R11) // Alignment for ELF ABI?
257-
RET
258-
259-
TEXT runtime·sigtramp(SB),NOSPLIT,$192
260-
// Save callee-save registers in the case of signal forwarding.
261-
// Please refer to https://golang.org/issue/31827 .
262-
MOVD R19, 8*4(RSP)
263-
MOVD R20, 8*5(RSP)
264-
MOVD R21, 8*6(RSP)
265-
MOVD R22, 8*7(RSP)
266-
MOVD R23, 8*8(RSP)
267-
MOVD R24, 8*9(RSP)
268-
MOVD R25, 8*10(RSP)
269-
MOVD R26, 8*11(RSP)
270-
MOVD R27, 8*12(RSP)
271-
MOVD g, 8*13(RSP)
272-
MOVD R29, 8*14(RSP)
273-
FMOVD F8, 8*15(RSP)
274-
FMOVD F9, 8*16(RSP)
275-
FMOVD F10, 8*17(RSP)
276-
FMOVD F11, 8*18(RSP)
277-
FMOVD F12, 8*19(RSP)
278-
FMOVD F13, 8*20(RSP)
279-
FMOVD F14, 8*21(RSP)
280-
FMOVD F15, 8*22(RSP)
281-
282-
// If called from an external code context, g will not be set.
283-
// Save R0, since runtime·load_g will clobber it.
284-
MOVW R0, 8(RSP) // signum
285-
MOVB runtime·iscgo(SB), R0
286-
CMP $0, R0
287-
BEQ 2(PC)
288-
BL runtime·load_g(SB)
289-
290-
MOVD R1, 16(RSP)
291-
MOVD R2, 24(RSP)
292-
BL runtime·sigtrampgo(SB)
293-
294-
// Restore callee-save registers.
295-
MOVD 8*4(RSP), R19
296-
MOVD 8*5(RSP), R20
297-
MOVD 8*6(RSP), R21
298-
MOVD 8*7(RSP), R22
299-
MOVD 8*8(RSP), R23
300-
MOVD 8*9(RSP), R24
301-
MOVD 8*10(RSP), R25
302-
MOVD 8*11(RSP), R26
303-
MOVD 8*12(RSP), R27
304-
MOVD 8*13(RSP), g
305-
MOVD 8*14(RSP), R29
306-
FMOVD 8*15(RSP), F8
307-
FMOVD 8*16(RSP), F9
308-
FMOVD 8*17(RSP), F10
309-
FMOVD 8*18(RSP), F11
310-
FMOVD 8*19(RSP), F12
311-
FMOVD 8*20(RSP), F13
312-
FMOVD 8*21(RSP), F14
313-
FMOVD 8*22(RSP), F15
314-
315-
RET
316-
317-
// int32 tfork(void *param, uintptr psize, M *mp, G *gp, void (*fn)(void));
318-
TEXT runtime·tfork(SB),NOSPLIT,$0
319-
320-
// Copy mp, gp and fn off parent stack for use by child.
321-
MOVD mm+16(FP), R4
322-
MOVD gg+24(FP), R5
323-
MOVD fn+32(FP), R6
324-
325-
MOVD param+0(FP), R0 // arg 1 - param
326-
MOVD psize+8(FP), R1 // arg 2 - psize
327-
MOVD $8, R8 // sys___tfork
328-
INVOKE_SYSCALL
329-
330-
// Return if syscall failed.
331-
BCC 4(PC)
332-
NEG R0, R0
333-
MOVW R0, ret+40(FP)
334-
RET
335-
336-
// In parent, return.
337-
CMP $0, R0
338-
BEQ 3(PC)
339-
MOVW R0, ret+40(FP)
340-
RET
341-
342-
// Initialise m, g.
343-
MOVD R5, g
344-
MOVD R4, g_m(g)
345-
346-
// Call fn.
347-
BL (R6)
348-
349-
// fn should never return.
350-
MOVD $2, R8 // crash if reached
351-
MOVD R8, (R8)
352-
RET
353-
354427
TEXT runtime·sigaltstack(SB),NOSPLIT,$0
355428
MOVD new+0(FP), R0 // arg 1 - new sigaltstack
356429
MOVD old+8(FP), R1 // arg 2 - old sigaltstack

src/runtime/tls_arm64.s

+4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
TEXT runtime·load_g(SB),NOSPLIT,$0
1212
#ifndef TLS_darwin
13+
#ifndef GOOS_openbsd
1314
MOVB runtime·iscgo(SB), R0
1415
CBZ R0, nocgo
16+
#endif
1517
#endif
1618

1719
MRS_TPIDR_R0
@@ -27,8 +29,10 @@ nocgo:
2729

2830
TEXT runtime·save_g(SB),NOSPLIT,$0
2931
#ifndef TLS_darwin
32+
#ifndef GOOS_openbsd
3033
MOVB runtime·iscgo(SB), R0
3134
CBZ R0, nocgo
35+
#endif
3236
#endif
3337

3438
MRS_TPIDR_R0

0 commit comments

Comments
 (0)