Skip to content

Commit

Permalink
runtime: add support for --buildmode=c-shared on loong64
Browse files Browse the repository at this point in the history
These c-shared related CLs are follow up of CLs 455016, 455017, 455018. Here we
follow the LoongArch ELF psABI v2 standard, which requires the support of the
PCALAU12I instruction.

Updates golang#53301
Updates golang#58784

Change-Id: I7f1ddbf3b2470d610f12069d147aa9b3a6a96f32
  • Loading branch information
limeidan authored and xen0n committed Mar 9, 2023
1 parent a6c4ac5 commit f843e27
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 7 deletions.
67 changes: 67 additions & 0 deletions src/runtime/cgo/abi_loong64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Macros for transitioning from the host ABI to Go ABI0.
//
// These save the frame pointer, so in general, functions that use
// these should have zero frame size to suppress the automatic frame
// pointer, though it's harmless to not do this.

#define PUSH_REGS_FLOAT_PART() \
MOVD F18, 120(R3) \
MOVD F19, 128(R3) \
MOVD F20, 136(R3) \
MOVD F21, 144(R3) \
MOVD F24, 152(R3) \
MOVD F25, 160(R3) \
MOVD F26, 168(R3) \
MOVD F27, 176(R3) \
MOVD F28, 184(R3) \
MOVD F29, 192(R3) \
MOVD F30, 200(R3) \
MOVD F31, 208(R3)

#define POP_REGS_FLOAT_PART() \
MOVD 120(R3), F18 \
MOVD 128(R3), F19 \
MOVD 136(R3), F20 \
MOVD 144(R3), F21 \
MOVD 152(R3), F24 \
MOVD 160(R3), F25 \
MOVD 168(R3), F26 \
MOVD 176(R3), F27 \
MOVD 184(R3), F28 \
MOVD 192(R3), F29 \
MOVD 200(R3), F30 \
MOVD 208(R3), F31

#define PUSH_REGS_HOST_TO_ABI0() \
MOVV R23, 24(R3) \
MOVV R24, 32(R3) \
MOVV R25, 40(R3) \
MOVV R26, 48(R3) \
MOVV R27, 56(R3) \
MOVV R28, 64(R3) \
MOVV R29, 72(R3) \
MOVV R30, 80(R3) \
MOVV R31, 88(R3) \
MOVV R3, 96(R3) \
MOVV g, 104(R3) \
MOVV R1, 112(R3) \
PUSH_REGS_FLOAT_PART()

#define POP_REGS_HOST_TO_ABI0() \
MOVV 24(R3), R23 \
MOVV 32(R3), R24 \
MOVV 40(R3), R25 \
MOVV 48(R3), R26 \
MOVV 56(R3), R27 \
MOVV 64(R3), R28 \
MOVV 72(R3), R29 \
MOVV 80(R3), R30 \
MOVV 88(R3), R31 \
MOVV 96(R3), R3 \
MOVV 104(R3), g \
MOVV 112(R3), R1 \
POP_REGS_FLOAT_PART()
49 changes: 49 additions & 0 deletions src/runtime/rt0_linux_loong64.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// license that can be found in the LICENSE file.

#include "textflag.h"
#include "cgo/abi_loong64.h"

TEXT _rt0_loong64_linux(SB),NOSPLIT,$0
JMP _main<>(SB)
Expand All @@ -16,6 +17,54 @@ TEXT _main<>(SB),NOSPLIT|NOFRAME,$0
ADDV $8, R3, R5 // argv
JMP main(SB)

// When building with -buildmode=c-shared, this symbol is called when the shared
// library is loaded.
TEXT _rt0_loong64_linux_lib(SB),NOSPLIT,$232
// Preserve callee-save registers.
PUSH_REGS_HOST_TO_ABI0()

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

MOVV R4, _rt0_loong64_linux_lib_argc<>(SB)
MOVV R5, _rt0_loong64_linux_lib_argv<>(SB)

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

// Create a new thread to do the runtime initialization and return.
MOVV _cgo_sys_thread_create(SB), R19
BEQ R19, nocgo
MOVV $_rt0_loong64_linux_lib_go(SB), R4
MOVV $0, R5
JAL (R19)
JMP restore

nocgo:
MOVV $0x800000, R4 // stacksize = 8192KB
MOVV $_rt0_loong64_linux_lib_go(SB), R5
MOVV R4, 8(R3)
MOVV R5, 16(R3)
MOVV $runtime·newosproc0(SB), R19
JAL (R19)

restore:
// Restore callee-save registers.
POP_REGS_HOST_TO_ABI0()
RET

TEXT _rt0_loong64_linux_lib_go(SB),NOSPLIT,$0
MOVV _rt0_loong64_linux_lib_argc<>(SB), R4
MOVV _rt0_loong64_linux_lib_argv<>(SB), R5
MOVV $runtime·rt0_go(SB),R19
JMP (R19)

DATA _rt0_loong64_linux_lib_argc<>(SB)/8, $0
GLOBL _rt0_loong64_linux_lib_argc<>(SB),NOPTR, $8
DATA _rt0_loong64_linux_lib_argv<>(SB)/8, $0
GLOBL _rt0_loong64_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
7 changes: 0 additions & 7 deletions src/runtime/tls_loong64.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "go_asm.h"
#include "go_tls.h"
#include "funcdata.h"
#include "textflag.h"

// If !iscgo, this is a no-op.
//
// NOTE: mcall() assumes this clobbers only R30 (REGTMP).
TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0-0
MOVB runtime·iscgo(SB), R30
BEQ R30, nocgo

MOVV g, runtime·tls_g(SB)

nocgo:
RET

Expand Down

0 comments on commit f843e27

Please sign in to comment.