Text file
src/runtime/tls_arm.s
Documentation: 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 //go:build !windows
6 // +build !windows
7
8 #include "go_asm.h"
9 #include "go_tls.h"
10 #include "funcdata.h"
11 #include "textflag.h"
12
13 // We have to resort to TLS variable to save g(R10).
14 // One reason is that external code might trigger
15 // SIGSEGV, and our runtime.sigtramp don't even know we
16 // are in external code, and will continue to use R10,
17 // this might as well result in another SIGSEGV.
18 // Note: both functions will clobber R0 and R11 and
19 // can be called from 5c ABI code.
20
21 // On android, runtime.tls_g is a normal variable.
22 // TLS offset is computed in x_cgo_inittls.
23 #ifdef GOOS_android
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 runtime·save_g(SB),NOSPLIT|NOFRAME,$0
34 // If the host does not support MRC the linker will replace it with
35 // a call to runtime.read_tls_fallback which jumps to __kuser_get_tls.
36 // The replacement function saves LR in R11 over the call to read_tls_fallback.
37 MRC 15, 0, R0, C13, C0, 3 // fetch TLS base pointer
38 BIC $3, R0 // Darwin/ARM might return unaligned pointer
39 MOVW runtime·tls_g(SB), R11
40 ADD R11, R0
41 MOVW g, 0(R0)
42 MOVW g, R0 // preserve R0 across call to setg<>
43 RET
44
45 // load_g loads the g register from pthread-provided
46 // thread-local memory, for use after calling externally compiled
47 // ARM code that overwrote those registers.
48 TEXT runtime·load_g(SB),NOSPLIT,$0
49 // See save_g
50 MRC 15, 0, R0, C13, C0, 3 // fetch TLS base pointer
51 BIC $3, R0 // Darwin/ARM might return unaligned pointer
52 MOVW runtime·tls_g(SB), R11
53 ADD R11, R0
54 MOVW 0(R0), g
55 RET
56
57 // This is called from rt0_go, which runs on the system stack
58 // using the initial stack allocated by the OS.
59 // It calls back into standard C using the BL (R4) below.
60 // To do that, the stack pointer must be 8-byte-aligned
61 // on some systems, notably FreeBSD.
62 // The ARM ABI says the stack pointer must be 8-byte-aligned
63 // on entry to any function, but only FreeBSD's C library seems to care.
64 // The caller was 8-byte aligned, but we push an LR.
65 // Declare a dummy word ($4, not $0) to make sure the
66 // frame is 8 bytes and stays 8-byte-aligned.
67 TEXT runtime·_initcgo(SB),NOSPLIT,$4
68 // if there is an _cgo_init, call it.
69 MOVW _cgo_init(SB), R4
70 CMP $0, R4
71 B.EQ nocgo
72 MRC 15, 0, R0, C13, C0, 3 // load TLS base pointer
73 MOVW R0, R3 // arg 3: TLS base pointer
74 #ifdef TLSG_IS_VARIABLE
75 MOVW $runtime·tls_g(SB), R2 // arg 2: &tls_g
76 #else
77 MOVW $0, R2 // arg 2: not used when using platform tls
78 #endif
79 MOVW $setg_gcc<>(SB), R1 // arg 1: setg
80 MOVW g, R0 // arg 0: G
81 BL (R4) // will clobber R0-R3
82 nocgo:
83 RET
84
85 // void setg_gcc(G*); set g called from gcc.
86 TEXT setg_gcc<>(SB),NOSPLIT,$0
87 MOVW R0, g
88 B runtime·save_g(SB)
89
90 #ifdef TLSG_IS_VARIABLE
91 #ifdef GOOS_android
92 // Use the free TLS_SLOT_APP slot #2 on Android Q.
93 // Earlier androids are set up in gcc_android.c.
94 DATA runtime·tls_g+0(SB)/4, $8
95 #endif
96 GLOBL runtime·tls_g+0(SB), NOPTR, $4
97 #else
98 GLOBL runtime·tls_g+0(SB), TLSBSS, $4
99 #endif
100
View as plain text