aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/asm_riscv64.s
diff options
context:
space:
mode:
authorJoel Sing <joel@sing.id.au>2020-05-19 18:56:01 +1000
committerJoel Sing <joel@sing.id.au>2020-11-03 12:59:51 +0000
commit393f2bb067088cdbdb8d5848e6880b2ce65ddaf9 (patch)
tree91d346ce5c6d3019b935a5f5c511338556009c65 /src/runtime/asm_riscv64.s
parent974def803ee9fd03a755014dcb62d55105c846f1 (diff)
downloadgo-393f2bb067088cdbdb8d5848e6880b2ce65ddaf9.tar.gz
go-393f2bb067088cdbdb8d5848e6880b2ce65ddaf9.zip
cmd/dist,cmd/go,runtime: add support for cgo on linux/riscv64
Fixes #36641 Change-Id: I51868d83ce341d78d33b221d184c5a5110c60d14 Reviewed-on: https://go-review.googlesource.com/c/go/+/263598 Trust: Joel Sing <joel@sing.id.au> Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
Diffstat (limited to 'src/runtime/asm_riscv64.s')
-rw-r--r--src/runtime/asm_riscv64.s190
1 files changed, 178 insertions, 12 deletions
diff --git a/src/runtime/asm_riscv64.s b/src/runtime/asm_riscv64.s
index caaf42a7d0..01b42dc3de 100644
--- a/src/runtime/asm_riscv64.s
+++ b/src/runtime/asm_riscv64.s
@@ -9,10 +9,9 @@
// func rt0_go()
TEXT runtime·rt0_go(SB),NOSPLIT,$0
// X2 = stack; A0 = argc; A1 = argv
-
ADD $-24, X2
- MOV A0, 8(X2) // argc
- MOV A1, 16(X2) // argv
+ MOV A0, 8(X2) // argc
+ MOV A1, 16(X2) // argv
// create istack out of the given (operating system) stack.
// _cgo_init may update stackguard.
@@ -28,10 +27,10 @@ TEXT runtime·rt0_go(SB),NOSPLIT,$0
MOV _cgo_init(SB), T0
BEQ T0, ZERO, nocgo
- MOV ZERO, A3 // arg 3: not used
- MOV ZERO, A2 // arg 2: not used
+ MOV ZERO, A3 // arg 3: not used
+ MOV ZERO, A2 // arg 2: not used
MOV $setg_gcc<>(SB), A1 // arg 1: setg
- MOV g, A0 // arg 0: G
+ MOV g, A0 // arg 0: G
JALR RA, T0
nocgo:
@@ -313,10 +312,62 @@ TEXT runtime·gosave(SB), NOSPLIT|NOFRAME, $0-8
CALL runtime·badctxt(SB)
RET
+// Save state of caller into g->sched. Smashes X31.
+TEXT gosave<>(SB),NOSPLIT|NOFRAME,$0
+ MOV X1, (g_sched+gobuf_pc)(g)
+ MOV X2, (g_sched+gobuf_sp)(g)
+ MOV ZERO, (g_sched+gobuf_lr)(g)
+ MOV ZERO, (g_sched+gobuf_ret)(g)
+ // Assert ctxt is zero. See func save.
+ MOV (g_sched+gobuf_ctxt)(g), X31
+ BEQ ZERO, X31, 2(PC)
+ CALL runtime·badctxt(SB)
+ RET
+
// func asmcgocall(fn, arg unsafe.Pointer) int32
+// Call fn(arg) on the scheduler stack,
+// aligned appropriately for the gcc ABI.
+// See cgocall.go for more details.
TEXT ·asmcgocall(SB),NOSPLIT,$0-20
- // TODO(jsing): Add support for cgo - issue #36641.
- WORD $0 // crash
+ MOV fn+0(FP), X5
+ MOV arg+8(FP), X10
+
+ MOV X2, X8 // save original stack pointer
+ MOV g, X9
+
+ // Figure out if we need to switch to m->g0 stack.
+ // We get called to create new OS threads too, and those
+ // come in on the m->g0 stack already.
+ MOV g_m(g), X6
+ MOV m_g0(X6), X7
+ BEQ X7, g, g0
+
+ CALL gosave<>(SB)
+ MOV X7, g
+ CALL runtime·save_g(SB)
+ MOV (g_sched+gobuf_sp)(g), X2
+
+ // Now on a scheduling stack (a pthread-created stack).
+g0:
+ // Save room for two of our pointers.
+ ADD $-16, X2
+ MOV X9, 0(X2) // save old g on stack
+ MOV (g_stack+stack_hi)(X9), X9
+ SUB X8, X9, X8
+ MOV X8, 8(X2) // save depth in old g stack (can't just save SP, as stack might be copied during a callback)
+
+ JALR RA, (X5)
+
+ // Restore g, stack pointer. X10 is return value.
+ MOV 0(X2), g
+ CALL runtime·save_g(SB)
+ MOV (g_stack+stack_hi)(g), X5
+ MOV 8(X2), X6
+ SUB X6, X5, X6
+ MOV X6, X2
+
+ MOVW X10, ret+16(FP)
+ RET
// func asminit()
TEXT runtime·asminit(SB),NOSPLIT|NOFRAME,$0-0
@@ -444,6 +495,21 @@ CALLFN(·call268435456, 268435456)
CALLFN(·call536870912, 536870912)
CALLFN(·call1073741824, 1073741824)
+// Called from cgo wrappers, this function returns g->m->curg.stack.hi.
+// Must obey the gcc calling convention.
+TEXT _cgo_topofstack(SB),NOSPLIT,$8
+ // g (X27) and REG_TMP (X31) might be clobbered by load_g.
+ // X27 is callee-save in the gcc calling convention, so save it.
+ MOV g, savedX27-8(SP)
+
+ CALL runtime·load_g(SB)
+ MOV g_m(g), X5
+ MOV m_curg(X5), X5
+ MOV (g_stack+stack_hi)(X5), X10 // return value in X10
+
+ MOV savedX27-8(SP), g
+ RET
+
// func goexit(neverCallThisFunction)
// The top-most function running on a goroutine
// returns to goexit+PCQuantum.
@@ -453,11 +519,111 @@ TEXT runtime·goexit(SB),NOSPLIT|NOFRAME|TOPFRAME,$0-0
// traceback from goexit1 must hit code range of goexit
MOV ZERO, ZERO // NOP
-// cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
+// func cgocallback(fn, frame unsafe.Pointer, ctxt uintptr)
// See cgocall.go for more details.
-TEXT ·cgocallback(SB),NOSPLIT,$0-24
- // TODO(jsing): Add support for cgo - issue #36641.
- WORD $0 // crash
+TEXT ·cgocallback(SB),NOSPLIT,$24-24
+ NO_LOCAL_POINTERS
+
+ // Load m and g from thread-local storage.
+ MOVBU runtime·iscgo(SB), X5
+ BEQ ZERO, X5, nocgo
+ CALL runtime·load_g(SB)
+nocgo:
+
+ // If g is nil, Go did not create the current thread.
+ // Call needm to obtain one for temporary use.
+ // In this case, we're running on the thread stack, so there's
+ // lots of space, but the linker doesn't know. Hide the call from
+ // the linker analysis by using an indirect call.
+ BEQ ZERO, g, needm
+
+ MOV g_m(g), X5
+ MOV X5, savedm-8(SP)
+ JMP havem
+
+needm:
+ MOV g, savedm-8(SP) // g is zero, so is m.
+ MOV $runtime·needm(SB), X6
+ JALR RA, X6
+
+ // Set m->sched.sp = SP, so that if a panic happens
+ // during the function we are about to execute, it will
+ // have a valid SP to run on the g0 stack.
+ // The next few lines (after the havem label)
+ // will save this SP onto the stack and then write
+ // the same SP back to m->sched.sp. That seems redundant,
+ // but if an unrecovered panic happens, unwindm will
+ // restore the g->sched.sp from the stack location
+ // and then systemstack will try to use it. If we don't set it here,
+ // that restored SP will be uninitialized (typically 0) and
+ // will not be usable.
+ MOV g_m(g), X5
+ MOV m_g0(X5), X6
+ MOV X2, (g_sched+gobuf_sp)(X6)
+
+havem:
+ // Now there's a valid m, and we're running on its m->g0.
+ // Save current m->g0->sched.sp on stack and then set it to SP.
+ // Save current sp in m->g0->sched.sp in preparation for
+ // switch back to m->curg stack.
+ // NOTE: unwindm knows that the saved g->sched.sp is at 8(X2) aka savedsp-24(SP).
+ MOV m_g0(X5), X6
+ MOV (g_sched+gobuf_sp)(X6), X7
+ MOV X7, savedsp-24(SP) // must match frame size
+ MOV X2, (g_sched+gobuf_sp)(X6)
+
+ // Switch to m->curg stack and call runtime.cgocallbackg.
+ // Because we are taking over the execution of m->curg
+ // but *not* resuming what had been running, we need to
+ // save that information (m->curg->sched) so we can restore it.
+ // We can restore m->curg->sched.sp easily, because calling
+ // runtime.cgocallbackg leaves SP unchanged upon return.
+ // To save m->curg->sched.pc, we push it onto the curg stack and
+ // open a frame the same size as cgocallback's g0 frame.
+ // Once we switch to the curg stack, the pushed PC will appear
+ // to be the return PC of cgocallback, so that the traceback
+ // will seamlessly trace back into the earlier calls.
+ MOV m_curg(X5), g
+ CALL runtime·save_g(SB)
+ MOV (g_sched+gobuf_sp)(g), X6 // prepare stack as X6
+ MOV (g_sched+gobuf_pc)(g), X7
+ MOV X7, -(24+8)(X6) // "saved LR"; must match frame size
+ // Gather our arguments into registers.
+ MOV fn+0(FP), X7
+ MOV frame+8(FP), X8
+ MOV ctxt+16(FP), X9
+ MOV $-(24+8)(X6), X2 // switch stack; must match frame size
+ MOV X7, 8(X2)
+ MOV X8, 16(X2)
+ MOV X9, 24(X2)
+ CALL runtime·cgocallbackg(SB)
+
+ // Restore g->sched (== m->curg->sched) from saved values.
+ MOV 0(X2), X7
+ MOV X7, (g_sched+gobuf_pc)(g)
+ MOV $(24+8)(X2), X6 // must match frame size
+ MOV X6, (g_sched+gobuf_sp)(g)
+
+ // Switch back to m->g0's stack and restore m->g0->sched.sp.
+ // (Unlike m->curg, the g0 goroutine never uses sched.pc,
+ // so we do not have to restore it.)
+ MOV g_m(g), X5
+ MOV m_g0(X5), g
+ CALL runtime·save_g(SB)
+ MOV (g_sched+gobuf_sp)(g), X2
+ MOV savedsp-24(SP), X6 // must match frame size
+ MOV X6, (g_sched+gobuf_sp)(g)
+
+ // If the m on entry was nil, we called needm above to borrow an m
+ // for the duration of the call. Since the call is over, return it with dropm.
+ MOV savedm-8(SP), X5
+ BNE ZERO, X5, droppedm
+ MOV $runtime·dropm(SB), X6
+ JALR RA, X6
+droppedm:
+
+ // Done!
+ RET
TEXT runtime·breakpoint(SB),NOSPLIT|NOFRAME,$0-0
EBREAK