Skip to content

Commit 9f12e2e

Browse files
committed
runtime: implement pthread functions for darwin/arm64
They were not needed when Go only produced binaries with cgo suppport. Now that Go is about to run self-hosted on iOS we do need these. Updates #31722 Change-Id: If233aa2b31edc7b1c2dcac68974f9fba0604f9a3 Reviewed-on: https://go-review.googlesource.com/c/go/+/174300 Run-TryBot: Elias Naur <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent e0ac75d commit 9f12e2e

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

src/runtime/sys_darwin_arm64.s

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,33 +304,92 @@ TEXT runtime·sigaltstack_trampoline(SB),NOSPLIT,$0
304304
RET
305305

306306
// Thread related functions
307-
// Note: On darwin/arm64, the runtime always use runtime/cgo to
308-
// create threads, so all thread related functions will just exit with a
309-
// unique status.
310307

308+
// mstart_stub is the first function executed on a new thread started by pthread_create.
309+
// It just does some low-level setup and then calls mstart.
310+
// Note: called with the C calling convention.
311311
TEXT runtime·mstart_stub(SB),NOSPLIT,$0
312-
MOVW $44, R0
313-
BL libc_exit(SB)
312+
// R0 points to the m.
313+
// We are already on m's g0 stack.
314+
315+
// Save callee-save registers.
316+
SUB $144, RSP
317+
MOVD R19, 0(RSP)
318+
MOVD R20, 8(RSP)
319+
MOVD R21, 16(RSP)
320+
MOVD R22, 24(RSP)
321+
MOVD R23, 32(RSP)
322+
MOVD R24, 40(RSP)
323+
MOVD R25, 48(RSP)
324+
MOVD R26, 56(RSP)
325+
MOVD R27, 64(RSP)
326+
MOVD g, 72(RSP)
327+
FMOVD F8, 80(RSP)
328+
FMOVD F9, 88(RSP)
329+
FMOVD F10, 96(RSP)
330+
FMOVD F11, 104(RSP)
331+
FMOVD F12, 112(RSP)
332+
FMOVD F13, 120(RSP)
333+
FMOVD F14, 128(RSP)
334+
FMOVD F15, 136(RSP)
335+
336+
MOVD m_g0(R0), g
337+
338+
BL runtime·mstart(SB)
339+
340+
// Restore callee-save registers.
341+
MOVD 0(RSP), R19
342+
MOVD 8(RSP), R20
343+
MOVD 16(RSP), R21
344+
MOVD 24(RSP), R22
345+
MOVD 32(RSP), R23
346+
MOVD 40(RSP), R24
347+
MOVD 48(RSP), R25
348+
MOVD 56(RSP), R26
349+
MOVD 64(RSP), R27
350+
MOVD 72(RSP), g
351+
FMOVD 80(RSP), F8
352+
FMOVD 88(RSP), F9
353+
FMOVD 96(RSP), F10
354+
FMOVD 104(RSP), F11
355+
FMOVD 112(RSP), F12
356+
FMOVD 120(RSP), F13
357+
FMOVD 128(RSP), F14
358+
FMOVD 136(RSP), F15
359+
ADD $144, RSP
360+
361+
// Go is all done with this OS thread.
362+
// Tell pthread everything is ok (we never join with this thread, so
363+
// the value here doesn't really matter).
364+
MOVD $0, R0
365+
314366
RET
315367

316368
TEXT runtime·pthread_attr_init_trampoline(SB),NOSPLIT,$0
317-
MOVW $45, R0
318-
BL libc_exit(SB)
369+
MOVD 0(R0), R0 // arg 1 attr
370+
BL libc_pthread_attr_init(SB)
319371
RET
320372

321373
TEXT runtime·pthread_attr_setstacksize_trampoline(SB),NOSPLIT,$0
322-
MOVW $46, R0
323-
BL libc_exit(SB)
374+
MOVD 8(R0), R1 // arg 2 size
375+
MOVD 0(R0), R0 // arg 1 attr
376+
BL libc_pthread_attr_setstacksize(SB)
324377
RET
325378

326379
TEXT runtime·pthread_attr_setdetachstate_trampoline(SB),NOSPLIT,$0
327-
MOVW $47, R0
328-
BL libc_exit(SB)
380+
MOVD 8(R0), R1 // arg 2 state
381+
MOVD 0(R0), R0 // arg 1 attr
382+
BL libc_pthread_attr_setdetachstate(SB)
329383
RET
330384

331385
TEXT runtime·pthread_create_trampoline(SB),NOSPLIT,$0
332-
MOVW $48, R0
333-
BL libc_exit(SB)
386+
SUB $16, RSP
387+
MOVD 0(R0), R1 // arg 2 state
388+
MOVD 8(R0), R2 // arg 3 start
389+
MOVD 16(R0), R3 // arg 4 arg
390+
MOVD RSP, R0 // arg 1 &threadid (which we throw away)
391+
BL libc_pthread_create(SB)
392+
ADD $16, RSP
334393
RET
335394

336395
TEXT runtime·raise_trampoline(SB),NOSPLIT,$0

0 commit comments

Comments
 (0)