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.tlsg 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 	// $runtime.tlsg(SB) is a special linker symbol.
     45 	// It is the offset from the TLS base pointer to our
     46 	// thread-local storage for g.
     47 #ifdef TLSG_IS_VARIABLE
     48 	MOVW	runtimetlsg(SB), R11
     49 #else
     50 	MOVW	$runtimetlsg(SB), R11
     51 #endif
     52 	ADD	R11, R0
     53 	MOVW	g, 0(R0)
     54 	MOVW	g, R0 // preserve R0 across call to setg<>
     55 	RET
     56 
     57 // load_g loads the g register from pthread-provided
     58 // thread-local memory, for use after calling externally compiled
     59 // ARM code that overwrote those registers.
     60 TEXT runtimeload_g(SB),NOSPLIT,$0
     61 #ifdef GOOS_nacl
     62 	// nothing to do as nacl/arm does not use TLS at all.
     63 	RET
     64 #endif
     65 	// See save_g
     66 	MRC	15, 0, R0, C13, C0, 3 // fetch TLS base pointer
     67 	BIC $3, R0 // Darwin/ARM might return unaligned pointer
     68 	// $runtime.tlsg(SB) is a special linker symbol.
     69 	// It is the offset from the TLS base pointer to our
     70 	// thread-local storage for g.
     71 #ifdef TLSG_IS_VARIABLE
     72 	MOVW	runtimetlsg(SB), R11
     73 #else
     74 	MOVW	$runtimetlsg(SB), R11
     75 #endif
     76 	ADD	R11, R0
     77 	MOVW	0(R0), g
     78 	RET
     79 
     80 // This is called from rt0_go, which runs on the system stack
     81 // using the initial stack allocated by the OS.
     82 // It calls back into standard C using the BL (R4) below.
     83 // To do that, the stack pointer must be 8-byte-aligned
     84 // on some systems, notably FreeBSD.
     85 // The ARM ABI says the stack pointer must be 8-byte-aligned
     86 // on entry to any function, but only FreeBSD's C library seems to care.
     87 // The caller was 8-byte aligned, but we push an LR.
     88 // Declare a dummy word ($4, not $0) to make sure the
     89 // frame is 8 bytes and stays 8-byte-aligned.
     90 TEXT runtime_initcgo(SB),NOSPLIT,$4
     91 #ifndef GOOS_nacl
     92 	// if there is an _cgo_init, call it.
     93 	MOVW	_cgo_init(SB), R4
     94 	CMP	$0, R4
     95 	B.EQ	nocgo
     96 	MRC     15, 0, R0, C13, C0, 3 	// load TLS base pointer
     97 	MOVW 	R0, R3 			// arg 3: TLS base pointer
     98 	MOVW 	$runtimetlsg(SB), R2 	// arg 2: tlsg
     99 	MOVW	$setg_gcc<>(SB), R1 	// arg 1: setg
    100 	MOVW	g, R0 			// arg 0: G
    101 	BL	(R4) // will clobber R0-R3
    102 #endif
    103 nocgo:
    104 	RET
    105 
    106 // void setg_gcc(G*); set g called from gcc.
    107 TEXT setg_gcc<>(SB),NOSPLIT,$0
    108 	MOVW	R0, g
    109 	B		runtimesave_g(SB)
    110 
    111 #ifdef TLSG_IS_VARIABLE
    112 GLOBL runtimetlsg+0(SB), NOPTR, $4
    113 #endif
    114