github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/runtime/tls_arm.s (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !windows
     6  
     7  #include "go_asm.h"
     8  #include "go_tls.h"
     9  #include "funcdata.h"
    10  #include "textflag.h"
    11  
    12  // We have to resort to TLS variable to save g(R10).
    13  // One reason is that external code might trigger
    14  // SIGSEGV, and our runtime.sigtramp don't even know we
    15  // are in external code, and will continue to use R10,
    16  // this might as well result in another SIGSEGV.
    17  // Note: both functions will clobber R0 and R11 and
    18  // can be called from 5c ABI code.
    19  
    20  // On android, runtime.tls_g is a normal variable.
    21  // TLS offset is computed in x_cgo_inittls.
    22  #ifdef GOOS_android
    23  #define TLSG_IS_VARIABLE
    24  #endif
    25  
    26  // save_g saves the g register into pthread-provided
    27  // thread-local memory, so that we can call externally compiled
    28  // ARM code that will overwrite those registers.
    29  // NOTE: runtime.gogo assumes that R1 is preserved by this function.
    30  //       runtime.mcall assumes this function only clobbers R0 and R11.
    31  // Returns with g in R0.
    32  TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0
    33  	// If the host does not support MRC the linker will replace it with
    34  	// a call to runtime.read_tls_fallback which jumps to __kuser_get_tls.
    35  	// The replacement function saves LR in R11 over the call to read_tls_fallback.
    36  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    37  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    38  	MOVW	runtime·tls_g(SB), R11
    39  	ADD	R11, R0
    40  	MOVW	g, 0(R0)
    41  	MOVW	g, R0 // preserve R0 across call to setg<>
    42  	RET
    43  
    44  // load_g loads the g register from pthread-provided
    45  // thread-local memory, for use after calling externally compiled
    46  // ARM code that overwrote those registers.
    47  TEXT runtime·load_g(SB),NOSPLIT,$0
    48  	// See save_g
    49  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    50  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    51  	MOVW	runtime·tls_g(SB), R11
    52  	ADD	R11, R0
    53  	MOVW	0(R0), g
    54  	RET
    55  
    56  // This is called from rt0_go, which runs on the system stack
    57  // using the initial stack allocated by the OS.
    58  // It calls back into standard C using the BL (R4) below.
    59  // To do that, the stack pointer must be 8-byte-aligned
    60  // on some systems, notably FreeBSD.
    61  // The ARM ABI says the stack pointer must be 8-byte-aligned
    62  // on entry to any function, but only FreeBSD's C library seems to care.
    63  // The caller was 8-byte aligned, but we push an LR.
    64  // Declare a dummy word ($4, not $0) to make sure the
    65  // frame is 8 bytes and stays 8-byte-aligned.
    66  TEXT runtime·_initcgo(SB),NOSPLIT,$4
    67  	// if there is an _cgo_init, call it.
    68  	MOVW	_cgo_init(SB), R4
    69  	CMP	$0, R4
    70  	B.EQ	nocgo
    71  	MRC     15, 0, R0, C13, C0, 3 	// load TLS base pointer
    72  	MOVW 	R0, R3 			// arg 3: TLS base pointer
    73  #ifdef TLSG_IS_VARIABLE
    74  	MOVW 	$runtime·tls_g(SB), R2 	// arg 2: &tls_g
    75  #else
    76  	MOVW	$0, R2			// arg 2: not used when using platform tls
    77  #endif
    78  	MOVW	$setg_gcc<>(SB), R1 	// arg 1: setg
    79  	MOVW	g, R0 			// arg 0: G
    80  	BL	(R4) // will clobber R0-R3
    81  nocgo:
    82  	RET
    83  
    84  // void setg_gcc(G*); set g called from gcc.
    85  TEXT setg_gcc<>(SB),NOSPLIT,$0
    86  	MOVW	R0, g
    87  	B		runtime·save_g(SB)
    88  
    89  #ifdef TLSG_IS_VARIABLE
    90  #ifdef GOOS_android
    91  // Use the free TLS_SLOT_APP slot #2 on Android Q.
    92  // Earlier androids are set up in gcc_android.c.
    93  DATA runtime·tls_g+0(SB)/4, $8
    94  #endif
    95  GLOBL runtime·tls_g+0(SB), NOPTR, $4
    96  #else
    97  GLOBL runtime·tls_g+0(SB), TLSBSS, $4
    98  #endif