Skip to content

Commit

Permalink
runtime: use libc for signal functions on iOS
Browse files Browse the repository at this point in the history
Also:
 - Add extra SystemStack space for darwin/arm64 just
like for darwin/arm.
 - Removed redundant stack alignment; the arm64 hardware enforces
 the 16 byte alignment.
 - Save and restore the g registers at library initialization.
 - Zero g registers since libpreinit can call libc functions
 that in turn use asmcgocall. asmcgocall requires an initialized g.
 - Change asmcgocall to work even if no g is set. The change mimics
 amd64.

Change-Id: I1b8c63b07cfec23b909c0d215b50dc229f8adbc8
Reviewed-on: https://go-review.googlesource.com/117176
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
  • Loading branch information
Elias Naur authored and randall77 committed Jun 12, 2018
1 parent ec98933 commit 021c39d
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 209 deletions.
6 changes: 0 additions & 6 deletions src/cmd/vet/all/whitelist/darwin_arm.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// darwin/arm-specific vet whitelist. See readme.txt for details.

// False positives due to comments in assembly.
// To be removed. See CL 27154.

runtime/sys_darwin_arm.s: [arm] sigfwd: use of unnamed argument 0(FP); offset 0 is fn+0(FP)


// Ok.

runtime/asm_arm.s: [arm] sigreturn: function sigreturn missing Go declaration
2 changes: 0 additions & 2 deletions src/cmd/vet/all/whitelist/darwin_arm64.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// darwin/arm64-specific vet whitelist. See readme.txt for details.

runtime/sys_darwin_arm64.s: [arm64] sigtramp: 24(RSP) should be infostyle+8(FP)
runtime/sys_darwin_arm64.s: [arm64] sigtramp: 24(RSP) should be infostyle+8(FP)
runtime/asm_arm64.s: [arm64] sigreturn: function sigreturn missing Go declaration
71 changes: 50 additions & 21 deletions src/runtime/asm_arm.s
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,28 @@ TEXT _rt0_arm_lib(SB),NOSPLIT,$104
MOVW R6, 20(R13)
MOVW R7, 24(R13)
MOVW R8, 28(R13)
MOVW R11, 32(R13)
MOVW g, 32(R13)
MOVW R11, 36(R13)

// Skip floating point registers on GOARM < 6.
MOVB runtime·goarm(SB), R11
CMP $6, R11
BLT skipfpsave
MOVD F8, (32+8*1)(R13)
MOVD F9, (32+8*2)(R13)
MOVD F10, (32+8*3)(R13)
MOVD F11, (32+8*4)(R13)
MOVD F12, (32+8*5)(R13)
MOVD F13, (32+8*6)(R13)
MOVD F14, (32+8*7)(R13)
MOVD F15, (32+8*8)(R13)
MOVD F8, (40+8*0)(R13)
MOVD F9, (40+8*1)(R13)
MOVD F10, (40+8*2)(R13)
MOVD F11, (40+8*3)(R13)
MOVD F12, (40+8*4)(R13)
MOVD F13, (40+8*5)(R13)
MOVD F14, (40+8*6)(R13)
MOVD F15, (40+8*7)(R13)
skipfpsave:
// Save argc/argv.
MOVW R0, _rt0_arm_lib_argc<>(SB)
MOVW R1, _rt0_arm_lib_argv<>(SB)

MOVW $0, g // Initialize g.

// Synchronous initialization.
CALL runtime·libpreinit(SB)

Expand All @@ -77,21 +80,22 @@ rr:
MOVB runtime·goarm(SB), R11
CMP $6, R11
BLT skipfprest
MOVD (32+8*1)(R13), F8
MOVD (32+8*2)(R13), F9
MOVD (32+8*3)(R13), F10
MOVD (32+8*4)(R13), F11
MOVD (32+8*5)(R13), F12
MOVD (32+8*6)(R13), F13
MOVD (32+8*7)(R13), F14
MOVD (32+8*8)(R13), F15
MOVD (40+8*0)(R13), F8
MOVD (40+8*1)(R13), F9
MOVD (40+8*2)(R13), F10
MOVD (40+8*3)(R13), F11
MOVD (40+8*4)(R13), F12
MOVD (40+8*5)(R13), F13
MOVD (40+8*6)(R13), F14
MOVD (40+8*7)(R13), F15
skipfprest:
MOVW 12(R13), R4
MOVW 16(R13), R5
MOVW 20(R13), R6
MOVW 24(R13), R7
MOVW 28(R13), R8
MOVW 32(R13), R11
MOVW 32(R13), g
MOVW 36(R13), R11
RET

// _rt0_arm_lib_go initializes the Go runtime.
Expand Down Expand Up @@ -582,6 +586,8 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-12
MOVW arg+4(FP), R0

MOVW R13, R2
CMP $0, g
BEQ nosave
MOVW g, R4

// Figure out if we need to switch to m->g0 stack.
Expand All @@ -590,10 +596,10 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-12
MOVW g_m(g), R8
MOVW m_gsignal(R8), R3
CMP R3, g
BEQ noswitch
BEQ nosave
MOVW m_g0(R8), R3
CMP R3, g
BEQ noswitch
BEQ nosave
BL gosave<>(SB)
MOVW R0, R5
MOVW R3, R0
Expand All @@ -602,7 +608,6 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-12
MOVW (g_sched+gobuf_sp)(g), R13

// Now on a scheduling stack (a pthread-created stack).
noswitch:
SUB $24, R13
BIC $0x7, R13 // alignment for gcc ABI
MOVW R4, 20(R13) // save old g
Expand All @@ -624,6 +629,30 @@ noswitch:
MOVW R0, ret+8(FP)
RET

nosave:
// Running on a system stack, perhaps even without a g.
// Having no g can happen during thread creation or thread teardown
// (see needm/dropm on Solaris, for example).
// This code is like the above sequence but without saving/restoring g
// and without worrying about the stack moving out from under us
// (because we're on a system stack, not a goroutine stack).
// The above code could be used directly if already on a system stack,
// but then the only path through this code would be a rare case on Solaris.
// Using this code for all "already on system stack" calls exercises it more,
// which should help keep it correct.
SUB $24, R13
BIC $0x7, R13 // alignment for gcc ABI
// save null g in case someone looks during debugging.
MOVW $0, R4
MOVW R4, 20(R13)
MOVW R2, 16(R13) // Save old stack pointer.
BL (R1)
// Restore stack pointer.
MOVW 16(R13), R2
MOVW R2, R13
MOVW R0, ret+8(FP)
RET

// cgocallback(void (*fn)(void*), void *frame, uintptr framesize, uintptr ctxt)
// Turn the fn into a Go func (by taking its address) and call
// cgocallback_gofunc.
Expand Down
34 changes: 30 additions & 4 deletions src/runtime/asm_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
MOVD arg+8(FP), R0

MOVD RSP, R2 // save original stack pointer
CMP $0, g
BEQ nosave
MOVD g, R4

// Figure out if we need to switch to m->g0 stack.
Expand All @@ -871,10 +873,12 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
MOVD g_m(g), R8
MOVD m_gsignal(R8), R3
CMP R3, g
BEQ noswitch
BEQ nosave
MOVD m_g0(R8), R3
CMP R3, g
BEQ noswitch
BEQ nosave

// Switch to system stack.
MOVD R0, R9 // gosave<> and save_g might clobber R0
BL gosave<>(SB)
MOVD R3, g
Expand All @@ -884,12 +888,10 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
MOVD R9, R0

// Now on a scheduling stack (a pthread-created stack).
noswitch:
// Save room for two of our pointers /*, plus 32 bytes of callee
// save area that lives on the caller stack. */
MOVD RSP, R13
SUB $16, R13
BIC $0xf, R13 // alignment for gcc ABI
MOVD R13, RSP
MOVD R4, 0(RSP) // save old g on stack
MOVD (g_stack+stack_hi)(R4), R4
Expand All @@ -910,6 +912,30 @@ noswitch:
MOVW R0, ret+16(FP)
RET

nosave:
// Running on a system stack, perhaps even without a g.
// Having no g can happen during thread creation or thread teardown
// (see needm/dropm on Solaris, for example).
// This code is like the above sequence but without saving/restoring g
// and without worrying about the stack moving out from under us
// (because we're on a system stack, not a goroutine stack).
// The above code could be used directly if already on a system stack,
// but then the only path through this code would be a rare case on Solaris.
// Using this code for all "already on system stack" calls exercises it more,
// which should help keep it correct.
MOVD RSP, R13
SUB $16, R13
MOVD R13, RSP
MOVD $0, R4
MOVD R4, 0(RSP) // Where above code stores g, in case someone looks during debugging.
MOVD R2, 8(RSP) // Save original stack pointer.
BL (R1)
// Restore stack pointer.
MOVD 8(RSP), R2
MOVD R2, RSP
MOVD R0, ret+16(FP)
RET

// cgocallback(void (*fn)(void*), void *frame, uintptr framesize, uintptr ctxt)
// Turn the fn into a Go func (by taking its address) and call
// cgocallback_gofunc.
Expand Down
37 changes: 21 additions & 16 deletions src/runtime/rt0_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ TEXT _rt0_arm64_darwin_lib(SB),NOSPLIT,$168
MOVD R25, 72(RSP)
MOVD R26, 80(RSP)
MOVD R27, 88(RSP)
FMOVD F8, 96(RSP)
FMOVD F9, 104(RSP)
FMOVD F10, 112(RSP)
FMOVD F11, 120(RSP)
FMOVD F12, 128(RSP)
FMOVD F13, 136(RSP)
FMOVD F14, 144(RSP)
FMOVD F15, 152(RSP)
MOVD g, 96(RSP)
FMOVD F8, 104(RSP)
FMOVD F9, 112(RSP)
FMOVD F10, 120(RSP)
FMOVD F11, 128(RSP)
FMOVD F12, 136(RSP)
FMOVD F13, 144(RSP)
FMOVD F14, 152(RSP)
FMOVD F15, 160(RSP)

MOVD R0, _rt0_arm64_darwin_lib_argc<>(SB)
MOVD R1, _rt0_arm64_darwin_lib_argv<>(SB)

MOVD $0, g // initialize g to nil

// Synchronous initialization.
MOVD $runtime·libpreinit(SB), R4
BL (R4)
Expand All @@ -58,14 +61,16 @@ TEXT _rt0_arm64_darwin_lib(SB),NOSPLIT,$168
MOVD 72(RSP), R25
MOVD 80(RSP), R26
MOVD 88(RSP), R27
FMOVD 96(RSP), F8
FMOVD 104(RSP), F9
FMOVD 112(RSP), F10
FMOVD 120(RSP), F11
FMOVD 128(RSP), F12
FMOVD 136(RSP), F13
FMOVD 144(RSP), F14
FMOVD 152(RSP), F15
MOVD 96(RSP), g
FMOVD 104(RSP), F8
FMOVD 112(RSP), F9
FMOVD 120(RSP), F10
FMOVD 128(RSP), F11
FMOVD 136(RSP), F12
FMOVD 144(RSP), F13
FMOVD 152(RSP), F14
FMOVD 160(RSP), F15

RET

TEXT _rt0_arm64_darwin_lib_go(SB),NOSPLIT,$0
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ const (
// StackSystem is a number of additional bytes to add
// to each stack below the usual guard area for OS-specific
// purposes like signal handling. Used on Windows, Plan 9,
// and Darwin/ARM because they do not use a separate stack.
_StackSystem = sys.GoosWindows*512*sys.PtrSize + sys.GoosPlan9*512 + sys.GoosDarwin*sys.GoarchArm*1024
// and iOS because they do not use a separate stack.
_StackSystem = sys.GoosWindows*512*sys.PtrSize + sys.GoosPlan9*512 + sys.GoosDarwin*sys.GoarchArm*1024 + sys.GoosDarwin*sys.GoarchArm64*1024

// The minimum size of stack used by Go code
_StackMin = 2048
Expand Down
Loading

0 comments on commit 021c39d

Please sign in to comment.