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