Skip to content

Commit 3af29fb

Browse files
committed
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]>
1 parent 997ed6f commit 3af29fb

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/runtime/asm_amd64.s

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
550550
// come in on the m->g0 stack already.
551551
get_tls(CX)
552552
MOVQ g(CX), R8
553+
CMPQ R8, $0
554+
JEQ nosave
553555
MOVQ g_m(R8), R8
554556
MOVQ m_g0(R8), SI
555557
MOVQ g(CX), DI
@@ -559,11 +561,11 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
559561
CMPQ SI, DI
560562
JEQ nosave
561563

564+
// Switch to system stack.
562565
MOVQ m_g0(R8), SI
563566
CALL gosave<>(SB)
564567
MOVQ SI, g(CX)
565568
MOVQ (g_sched+gobuf_sp)(SI), SP
566-
nosave:
567569

568570
// Now on a scheduling stack (a pthread-created stack).
569571
// Make sure we have enough room for 4 stack-backed fast-call
@@ -589,6 +591,29 @@ nosave:
589591
MOVL AX, ret+16(FP)
590592
RET
591593

594+
nosave:
595+
// Running on a system stack, perhaps even without a g.
596+
// Having no g can happen during thread creation or thread teardown
597+
// (see needm/dropm on Solaris, for example).
598+
// This code is like the above sequence but without saving/restoring g
599+
// and without worrying about the stack moving out from under us
600+
// (because we're on a system stack, not a goroutine stack).
601+
// The above code could be used directly if already on a system stack,
602+
// but then the only path through this code would be a rare case on Solaris.
603+
// Using this code for all "already on system stack" calls exercises it more,
604+
// which should help keep it correct.
605+
SUBQ $64, SP
606+
ANDQ $~15, SP
607+
MOVQ $0, 48(SP) // where above code stores g, in case someone looks during debugging
608+
MOVQ DX, 40(SP) // save original stack pointer
609+
MOVQ BX, DI // DI = first argument in AMD64 ABI
610+
MOVQ BX, CX // CX = first argument in Win64
611+
CALL AX
612+
MOVQ 40(SP), SI // restore original stack pointer
613+
MOVQ SI, SP
614+
MOVL AX, ret+16(FP)
615+
RET
616+
592617
// cgocallback(void (*fn)(void*), void *frame, uintptr framesize)
593618
// Turn the fn into a Go func (by taking its address) and call
594619
// cgocallback_gofunc.

0 commit comments

Comments
 (0)