github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/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  #include "go_asm.h"
     6  #include "go_tls.h"
     7  #include "funcdata.h"
     8  #include "textflag.h"
     9  
    10  // We have to resort to TLS variable to save g(R10).
    11  // One reason is that external code might trigger
    12  // SIGSEGV, and our runtime.sigtramp don't even know we
    13  // are in external code, and will continue to use R10,
    14  // this might as well result in another SIGSEGV.
    15  // Note: both functions will clobber R0 and R11 and
    16  // can be called from 5c ABI code.
    17  
    18  // On android and darwin, runtime.tls_g is a normal variable.
    19  // TLS offset is computed in x_cgo_inittls.
    20  #ifdef GOOS_android
    21  #define TLSG_IS_VARIABLE
    22  #endif
    23  #ifdef GOOS_darwin
    24  #define TLSG_IS_VARIABLE
    25  #endif
    26  #ifdef GOOS_windows
    27  #define TLSG_IS_VARIABLE
    28  #endif
    29  
    30  // save_g saves the g register into pthread-provided
    31  // thread-local memory, so that we can call externally compiled
    32  // ARM code that will overwrite those registers.
    33  // NOTE: runtime.gogo assumes that R1 is preserved by this function.
    34  //       runtime.mcall assumes this function only clobbers R0 and R11.
    35  // Returns with g in R0.
    36  TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0
    37  #ifdef GOOS_nacl
    38  	// nothing to do as nacl/arm does not use TLS at all.
    39  	MOVW	g, R0 // preserve R0 across call to setg<>
    40  	RET
    41  #else
    42  #ifdef GOOS_windows
    43  	// Save the value in the _TEB->TlsSlots array.
    44  	// Effectively implements TlsSetValue().
    45  	MRC	15, 0, R0, C13, C0, 2
    46  	ADD	$0xe10, R0
    47  	MOVW 	$runtime·tls_g(SB), R11
    48  	MOVW	(R11), R11
    49  	MOVW	g, R11<<2(R0)
    50  	MOVW	g, R0	// preserve R0 accross call to setg<>
    51  	RET
    52  #else
    53  	// If the host does not support MRC the linker will replace it with
    54  	// a call to runtime.read_tls_fallback which jumps to __kuser_get_tls.
    55  	// The replacement function saves LR in R11 over the call to read_tls_fallback.
    56  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    57  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    58  	MOVW	runtime·tls_g(SB), R11
    59  	ADD	R11, R0
    60  	MOVW	g, 0(R0)
    61  	MOVW	g, R0 // preserve R0 across call to setg<>
    62  	RET
    63  #endif
    64  #endif
    65  
    66  // load_g loads the g register from pthread-provided
    67  // thread-local memory, for use after calling externally compiled
    68  // ARM code that overwrote those registers.
    69  TEXT runtime·load_g(SB),NOSPLIT,$0
    70  #ifdef GOOS_nacl
    71  	// nothing to do as nacl/arm does not use TLS at all.
    72  	RET
    73  #else
    74  #ifdef GOOS_windows
    75  	// Get the value from the _TEB->TlsSlots array.
    76  	// Effectively implements TlsGetValue().
    77  	MRC	15, 0, R0, C13, C0, 2
    78  	ADD	$0xe10, R0
    79  	MOVW 	$runtime·tls_g(SB), g
    80  	MOVW	(g), g
    81  	MOVW	g<<2(R0), g
    82  	RET
    83  #else
    84  	// See save_g
    85  	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
    86  	BIC $3, R0 // Darwin/ARM might return unaligned pointer
    87  	MOVW	runtime·tls_g(SB), R11
    88  	ADD	R11, R0
    89  	MOVW	0(R0), g
    90  	RET
    91  #endif
    92  #endif
    93  
    94  // This is called from rt0_go, which runs on the system stack
    95  // using the initial stack allocated by the OS.
    96  // It calls back into standard C using the BL (R4) below.
    97  // To do that, the stack pointer must be 8-byte-aligned
    98  // on some systems, notably FreeBSD.
    99  // The ARM ABI says the stack pointer must be 8-byte-aligned
   100  // on entry to any function, but only FreeBSD's C library seems to care.
   101  // The caller was 8-byte aligned, but we push an LR.
   102  // Declare a dummy word ($4, not $0) to make sure the
   103  // frame is 8 bytes and stays 8-byte-aligned.
   104  TEXT runtime·_initcgo(SB),NOSPLIT,$4
   105  #ifdef GOOS_windows
   106  	MOVW	R13, R4
   107  	BIC	$0x7, R13
   108  	MOVW 	$runtime·_TlsAlloc(SB), R0
   109  	MOVW	(R0), R0
   110  	BL	(R0)
   111  	// Assert that slot is less than 64 so we can use _TEB->TlsSlots
   112  	CMP	$64, R0
   113  	MOVW	$runtime·abort(SB), R1
   114  	BL.GE	(R1)
   115  	MOVW 	$runtime·tls_g(SB), R1
   116  	MOVW	R0, (R1)
   117  	MOVW	R4, R13
   118  #else
   119  #ifndef GOOS_nacl
   120  	// if there is an _cgo_init, call it.
   121  	MOVW	_cgo_init(SB), R4
   122  	CMP	$0, R4
   123  	B.EQ	nocgo
   124  	MRC     15, 0, R0, C13, C0, 3 	// load TLS base pointer
   125  	MOVW 	R0, R3 			// arg 3: TLS base pointer
   126  #ifdef TLSG_IS_VARIABLE
   127  	MOVW 	$runtime·tls_g(SB), R2 	// arg 2: &tls_g
   128  #else
   129          MOVW	$0, R2			// arg 2: not used when using platform tls
   130  #endif
   131  	MOVW	$setg_gcc<>(SB), R1 	// arg 1: setg
   132  	MOVW	g, R0 			// arg 0: G
   133  	BL	(R4) // will clobber R0-R3
   134  #endif // GOOS_nacl
   135  #endif // GOOS_windows
   136  nocgo:
   137  	RET
   138  
   139  // void setg_gcc(G*); set g called from gcc.
   140  TEXT setg_gcc<>(SB),NOSPLIT,$0
   141  	MOVW	R0, g
   142  	B		runtime·save_g(SB)
   143  
   144  #ifdef TLSG_IS_VARIABLE
   145  GLOBL runtime·tls_g+0(SB), NOPTR, $4
   146  #else
   147  GLOBL runtime·tls_g+0(SB), TLSBSS, $4
   148  #endif