Austin Clements | f1c4444 | 2014-12-16 17:14:00 -0500 | [diff] [blame] | 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 ppc64 ppc64le |
| 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 (R30). |
| 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 R30, |
| 16 | // this might well result in another SIGSEGV. |
| 17 | |
| 18 | // save_g saves the g register into pthread-provided |
| 19 | // thread-local memory, so that we can call externally compiled |
| 20 | // ppc64 code that will overwrite this register. |
| 21 | // |
| 22 | // If !iscgo, this is a no-op. |
Austin Clements | af7ca8d | 2014-12-16 18:34:55 -0500 | [diff] [blame] | 23 | // |
| 24 | // NOTE: setg_gcc<> assume this clobbers only R31. |
Austin Clements | f1c4444 | 2014-12-16 17:14:00 -0500 | [diff] [blame] | 25 | TEXT runtime·save_g(SB),NOSPLIT,$-8-0 |
| 26 | MOVB runtime·iscgo(SB), R31 |
| 27 | CMP R31, $0 |
| 28 | BEQ nocgo |
| 29 | |
| 30 | // $runtime.tlsg(SB) is a special linker symbol. |
| 31 | // It is the offset from the start of TLS to our |
| 32 | // thread-local storage for g. |
| 33 | MOVD $runtime·tlsg(SB), R31 |
| 34 | ADD R13, R31 |
| 35 | // The actual TLS base is 0x7000 below R13 |
| 36 | SUB $0x7000, R31 |
| 37 | |
| 38 | // Store g in TLS |
| 39 | MOVD g, 0(R31) |
| 40 | |
| 41 | nocgo: |
| 42 | RET |
| 43 | |
| 44 | // load_g loads the g register from pthread-provided |
| 45 | // thread-local memory, for use after calling externally compiled |
| 46 | // ppc64 code that overwrote those registers. |
| 47 | // |
| 48 | // This is never called directly from C code (it doesn't have to |
| 49 | // follow the C ABI), but it may be called from a C context, where the |
| 50 | // usual Go registers aren't set up. |
Austin Clements | af7ca8d | 2014-12-16 18:34:55 -0500 | [diff] [blame] | 51 | // |
| 52 | // NOTE: _cgo_topofstack assumes this only clobbers g (R30), and R31. |
Austin Clements | f1c4444 | 2014-12-16 17:14:00 -0500 | [diff] [blame] | 53 | TEXT runtime·load_g(SB),NOSPLIT,$-8-0 |
| 54 | MOVD $runtime·tlsg(SB), R31 |
| 55 | // R13 is the C ABI TLS base pointer + 0x7000 |
| 56 | ADD R13, R31 |
| 57 | SUB $0x7000, R31 |
| 58 | |
| 59 | MOVD 0(R31), g |
| 60 | RET |