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