github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/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  // +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 and darwin, 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  #ifdef GOOS_darwin
    26  #define TLSG_IS_VARIABLE
    27  #endif
    28  
    29  // save_g saves the g register into pthread-provided
    30  // thread-local memory, so that we can call externally compiled
    31  // ARM code that will overwrite those registers.
    32  // NOTE: runtime.gogo assumes that R1 is preserved by this function.
    33  //       runtime.mcall assumes this function only clobbers R0 and R11.
    34  // Returns with g in R0.
    35  TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0
    36  #ifdef GOOS_nacl
    37  	// nothing to do as nacl/arm does not use TLS at all.
    38  	MOVW	g, R0 // preserve R0 across call to setg<>
    39  	RET
    40  #else
    41  	// If the host does not support MRC the linker will replace it with
    42  	// a call to runtime.read_tls_fallback which jumps to __kuser_get_tls.
    43  	// The replacement function saves LR in R11 over the call to read_tls_fallback.
    44  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    45  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    46  	MOVW	runtime·tls_g(SB), R11
    47  	ADD	R11, R0
    48  	MOVW	g, 0(R0)
    49  	MOVW	g, R0 // preserve R0 across call to setg<>
    50  	RET
    51  #endif
    52  
    53  // load_g loads the g register from pthread-provided
    54  // thread-local memory, for use after calling externally compiled
    55  // ARM code that overwrote those registers.
    56  TEXT runtime·load_g(SB),NOSPLIT,$0
    57  #ifdef GOOS_nacl
    58  	// nothing to do as nacl/arm does not use TLS at all.
    59  	RET
    60  #else
    61  	// See save_g
    62  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    63  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    64  	MOVW	runtime·tls_g(SB), R11
    65  	ADD	R11, R0
    66  	MOVW	0(R0), g
    67  	RET
    68  #endif
    69  
    70  // This is called from rt0_go, which runs on the system stack
    71  // using the initial stack allocated by the OS.
    72  // It calls back into standard C using the BL (R4) below.
    73  // To do that, the stack pointer must be 8-byte-aligned
    74  // on some systems, notably FreeBSD.
    75  // The ARM ABI says the stack pointer must be 8-byte-aligned
    76  // on entry to any function, but only FreeBSD's C library seems to care.
    77  // The caller was 8-byte aligned, but we push an LR.
    78  // Declare a dummy word ($4, not $0) to make sure the
    79  // frame is 8 bytes and stays 8-byte-aligned.
    80  TEXT runtime·_initcgo(SB),NOSPLIT,$4
    81  #ifndef GOOS_nacl
    82  	// if there is an _cgo_init, call it.
    83  	MOVW	_cgo_init(SB), R4
    84  	CMP	$0, R4
    85  	B.EQ	nocgo
    86  	MRC     15, 0, R0, C13, C0, 3 	// load TLS base pointer
    87  	MOVW 	R0, R3 			// arg 3: TLS base pointer
    88  #ifdef TLSG_IS_VARIABLE
    89  	MOVW 	$runtime·tls_g(SB), R2 	// arg 2: &tls_g
    90  #else
    91          MOVW	$0, R2			// arg 2: not used when using platform tls
    92  #endif
    93  	MOVW	$setg_gcc<>(SB), R1 	// arg 1: setg
    94  	MOVW	g, R0 			// arg 0: G
    95  	BL	(R4) // will clobber R0-R3
    96  #endif
    97  nocgo:
    98  	RET
    99  
   100  // void setg_gcc(G*); set g called from gcc.
   101  TEXT setg_gcc<>(SB),NOSPLIT,$0
   102  	MOVW	R0, g
   103  	B		runtime·save_g(SB)
   104  
   105  #ifdef TLSG_IS_VARIABLE
   106  GLOBL runtime·tls_g+0(SB), NOPTR, $4
   107  #else
   108  GLOBL runtime·tls_g+0(SB), TLSBSS, $4
   109  #endif