Skip to content

Commit edc3452

Browse files
committed
[release-branch.go1.5] runtime: make asmcgocall work without a g
Solaris needs to make system calls without a g, and Solaris uses asmcgocall to make system calls. I know, I know. I hope this makes CL 16915, fixing #12277, work on Solaris. Change-Id: If988dfd37f418b302da9c7096f598e5113ecea87 Reviewed-on: https://go-review.googlesource.com/17072 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Aram Hăvărneanu <[email protected]> Run-TryBot: Russ Cox <[email protected]> Reviewed-on: https://go-review.googlesource.com/17129
1 parent a28f3ef commit edc3452

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/runtime/asm_amd64.s

+26-1
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
661661
// come in on the m->g0 stack already.
662662
get_tls(CX)
663663
MOVQ g(CX), R8
664+
CMPQ R8, $0
665+
JEQ nosave
664666
MOVQ g_m(R8), R8
665667
MOVQ m_g0(R8), SI
666668
MOVQ g(CX), DI
@@ -670,11 +672,11 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
670672
CMPQ SI, DI
671673
JEQ nosave
672674

675+
// Switch to system stack.
673676
MOVQ m_g0(R8), SI
674677
CALL gosave<>(SB)
675678
MOVQ SI, g(CX)
676679
MOVQ (g_sched+gobuf_sp)(SI), SP
677-
nosave:
678680

679681
// Now on a scheduling stack (a pthread-created stack).
680682
// Make sure we have enough room for 4 stack-backed fast-call
@@ -700,6 +702,29 @@ nosave:
700702
MOVL AX, ret+16(FP)
701703
RET
702704

705+
nosave:
706+
// Running on a system stack, perhaps even without a g.
707+
// Having no g can happen during thread creation or thread teardown
708+
// (see needm/dropm on Solaris, for example).
709+
// This code is like the above sequence but without saving/restoring g
710+
// and without worrying about the stack moving out from under us
711+
// (because we're on a system stack, not a goroutine stack).
712+
// The above code could be used directly if already on a system stack,
713+
// but then the only path through this code would be a rare case on Solaris.
714+
// Using this code for all "already on system stack" calls exercises it more,
715+
// which should help keep it correct.
716+
SUBQ $64, SP
717+
ANDQ $~15, SP
718+
MOVQ $0, 48(SP) // where above code stores g, in case someone looks during debugging
719+
MOVQ DX, 40(SP) // save original stack pointer
720+
MOVQ BX, DI // DI = first argument in AMD64 ABI
721+
MOVQ BX, CX // CX = first argument in Win64
722+
CALL AX
723+
MOVQ 40(SP), SI // restore original stack pointer
724+
MOVQ SI, SP
725+
MOVL AX, ret+16(FP)
726+
RET
727+
703728
// cgocallback(void (*fn)(void*), void *frame, uintptr framesize)
704729
// Turn the fn into a Go func (by taking its address) and call
705730
// cgocallback_gofunc.

0 commit comments

Comments
 (0)