Skip to content

Commit

Permalink
runtime: support -buildmode=c-shared on linux/mips64le
Browse files Browse the repository at this point in the history
For golang#43264

Signed-off-by: Xiaodong Liu <liuxiaodong@loongson.cn>
  • Loading branch information
XiaodongLoong committed Aug 2, 2021
1 parent b8ca6e5 commit 889bc60
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/runtime/asm_mips64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,12 @@ TEXT ·asmcgocall(SB),NOSPLIT,$0-20
MOVV m_g0(R5), R6
BEQ R6, g, g0

MOVV R25, R15 // call to save_g clobbers R25 when buildmode=c-shared
JAL gosave_systemstack_switch<>(SB)
MOVV R6, g
JAL runtime·save_g(SB)
MOVV (g_sched+gobuf_sp)(g), R29
MOVV R15, R25

// Now on a scheduling stack (a pthread-created stack).
g0:
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/cgo/asm_mips64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ TEXT crosscall2(SB),NOSPLIT|NOFRAME,$0
MOVD F30, (8*21)(R29)
MOVD F31, (8*22)(R29)
#endif
MOVV 0(R25), R14
SRLV $16, R14, R14
MOVV $0x3c1c, R15 //crosscall2 begain with lui instruction under c-shared mode
AND R14, R15, R14
BEQ R14, R15, shared
// Initialize Go ABI environment
// prepare SB register = PC & 0xffffffff00000000
BGEZAL R0, 1(PC)
SRLV $32, R31, RSB
SLLV $32, RSB
shared:
JAL runtime·load_g(SB)

JAL runtime·cgocallback(SB)
Expand Down
26 changes: 19 additions & 7 deletions src/runtime/cgo/gcc_mips64x.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
.set noat
crosscall1:
#ifndef __mips_soft_float
daddiu $29, $29, -160
daddiu $29, $29, -168
#else
daddiu $29, $29, -96 // For soft-float, no need to make room for FP registers
daddiu $29, $29, -104 // For soft-float, no need to make room for FP registers
#endif
sd $31, 0($29)
sd $16, 8($29)
Expand Down Expand Up @@ -49,9 +49,21 @@ crosscall1:

move $20, $4 // save R4
move $1, $6
jalr $5 // call setg_gcc (clobbers R4)
jalr $20 // call fn

// buildmode=c-shared call function by jalr t9
#ifndef __mips_soft_float
sd $25, 152($29)
#else
sd $25, 88($29)
#endif
move $25, $5
jalr $25 // call setg_gcc (clobbers R4)
move $25, $20
jalr $25 // call fn
#ifndef __mips_soft_float
ld $25, 152($29)
#else
ld $25, 88($29)
#endif
ld $16, 8($29)
ld $17, 16($29)
ld $18, 24($29)
Expand All @@ -74,9 +86,9 @@ crosscall1:
#endif
ld $31, 0($29)
#ifndef __mips_soft_float
daddiu $29, $29, 160
daddiu $29, $29, 168
#else
daddiu $29, $29, 96
daddiu $29, $29, 104
#endif
jr $31

Expand Down
104 changes: 104 additions & 0 deletions src/runtime/rt0_linux_mips64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,110 @@ TEXT _main<>(SB),NOSPLIT|NOFRAME,$0
ADDV $8, R29, R5 // argv
JMP main(SB)

// When building with -buildmode=c-shared, this symbol is called when the shared
// library is loaded.
#ifdef GOMIPS64_hardfloat
TEXT _rt0_mips64le_linux_lib(SB),NOSPLIT,$232
#else
TEXT _rt0_mips64le_linux_lib(SB),NOSPLIT,$136
#endif
// Preserve callee-save registers.
MOVV R16, 24(R29)
MOVV R17, 32(R29)
MOVV R18, 40(R29)
MOVV R19, 48(R29)
MOVV R20, 56(R29)
MOVV R21, 64(R29)
MOVV R22, 72(R29)
MOVV R23, 80(R29)
MOVV RSB, 88(R29)
MOVV R29, 96(R29)
MOVV g, 104(R29)
MOVV R31, 112(R29)
#ifdef GOMIPS64_hardfloat
MOVF F20, 120(R29)
MOVF F21, 128(R29)
MOVF F22, 136(R29)
MOVF F23, 144(R29)
MOVF F24, 152(R29)
MOVF F25, 160(R29)
MOVF F26, 168(R29)
MOVF F27, 176(R29)
MOVF F28, 184(R29)
MOVF F29, 192(R29)
MOVF F30, 200(R29)
MOVF F31, 208(R29)
#endif

// Initialize g as null in case of using g later e.g. sigaction in cgo_sigaction.go
MOVV R0, g

MOVV R4, _rt0_mips64le_linux_lib_argc<>(SB)
MOVV R5, _rt0_mips64le_linux_lib_argv<>(SB)

// Synchronous initialization.
MOVV $runtime·libpreinit(SB), R1
JAL (R1)

// Create a new thread to do the runtime initialization and return.
MOVV _cgo_sys_thread_create(SB), R1
BEQ R1, nocgo
MOVV $_rt0_mips64le_linux_lib_go(SB), R4
MOVV $0, R5
// ADDV $-16, R29 // reserve 16 bytes for sp-8 where fp may be saved.
JAL (R1)
// ADDV $16, R29
JMP restore

nocgo:
MOVV $0x800000, R4 // stacksize = 8192KB
MOVV $_rt0_mips64le_linux_lib_go(SB), R5
MOVV R4, 8(R29)
MOVV R5, 16(R29)
MOVV $runtime·newosproc0(SB),R1
JAL (R1)

restore:
// Restore callee-save registers.
MOVV 24(R29), R16
MOVV 32(R29), R17
MOVV 40(R29), R18
MOVV 48(R29), R19
MOVV 56(R29), R20
MOVV 64(R29), R21
MOVV 72(R29), R22
MOVV 80(R29), R23
MOVV 88(R29), RSB
MOVV 96(R29), R29
MOVV 104(R29), g
MOVV 112(R29), R31
#ifdef GOMIPS64_hardfloat
MOVF 120(R29), F20
MOVF 128(R29), F21
MOVF 136(R29), F22
MOVF 144(R29), F23
MOVF 152(R29), F24
MOVF 160(R29), F25
MOVF 168(R29), F26
MOVF 176(R29), F27
MOVF 184(R29), F28
MOVF 192(R29), F29
MOVF 200(R29), F30
MOVF 208(R29), F31
#endif
RET

TEXT _rt0_mips64le_linux_lib_go(SB),NOSPLIT,$0
MOVV _rt0_mips64le_linux_lib_argc<>(SB), R4
MOVV _rt0_mips64le_linux_lib_argv<>(SB), R5
MOVV $runtime·rt0_go(SB),R1
JMP (R1)

DATA _rt0_mips64le_linux_lib_argc<>(SB)/8, $0
GLOBL _rt0_mips64le_linux_lib_argc<>(SB),NOPTR, $8
DATA _rt0_mips64le_linux_lib_argv<>(SB)/8, $0
GLOBL _rt0_mips64le_linux_lib_argv<>(SB),NOPTR, $8

TEXT main(SB),NOSPLIT|NOFRAME,$0
// in external linking, glibc jumps to main with argc in R4
// and argv in R5
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/tls_mips64x.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,37 @@ TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0-0
MOVB runtime·iscgo(SB), R23
BEQ R23, nocgo

// buildmode=c-shared clobbers R2,R4,R25
MOVV R31,-32(R29)
ADDV $-32, R29
MOVV R2, 8(R29)
MOVV R4, 16(R29)
MOVV R25, 24(R29)
MOVV R3, R23 // save R3
MOVV g, runtime·tls_g(SB) // TLS relocation clobbers R3
MOVV R23, R3 // restore R3
MOVV 8(R29),R2
MOVV 16(R29),R4
MOVV 24(R29),R25
MOVV 0(R29), R31
ADDV $32, R29

nocgo:
RET

TEXT runtime·load_g(SB),NOSPLIT|NOFRAME,$0-0
// buildmode=c-shared clobbers R2,R4,R25
MOVV R31,-32(R29)
ADDV $-32, R29
MOVV R2, 8(R29)
MOVV R4, 16(R29)
MOVV R25, 24(R29)
MOVV runtime·tls_g(SB), g // TLS relocation clobbers R3
MOVV 8(R29),R2
MOVV 16(R29),R4
MOVV 24(R29),R25
MOVV 0(R29), R31
ADDV $32, R29
RET

GLOBL runtime·tls_g(SB), TLSBSS, $8

0 comments on commit 889bc60

Please sign in to comment.