| 1 |
// Copyright 2018 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 "textflag.h" |
| 8 |
#include "funcdata.h" |
| 9 |
#include "time_windows.h" |
| 10 |
#include "cgo/abi_arm64.h" |
| 11 |
|
| 12 |
// Offsets into Thread Environment Block (pointer in R18) |
| 13 |
#define TEB_error 0x68 |
| 14 |
#define TEB_TlsSlots 0x1480 |
| 15 |
#define TEB_ArbitraryPtr 0x28 |
| 16 |
|
| 17 |
// Note: R0-R7 are args, R8 is indirect return value address, |
| 18 |
// R9-R15 are caller-save, R19-R29 are callee-save. |
| 19 |
// |
| 20 |
// load_g and save_g (in tls_arm64.s) clobber R27 (REGTMP) and R0. |
| 21 |
|
| 22 |
TEXT runtime·asmstdcall_trampoline<ABIInternal>(SB),NOSPLIT,$0 |
| 23 |
B runtime·asmstdcall(SB) |
| 24 |
|
| 25 |
// void runtime·asmstdcall(void *c); |
| 26 |
TEXT runtime·asmstdcall(SB),NOSPLIT,$16 |
| 27 |
STP (R19, R20), 16(RSP) // save old R19, R20 |
| 28 |
MOVD R0, R19 // save libcall pointer |
| 29 |
MOVD RSP, R20 // save stack pointer |
| 30 |
|
| 31 |
// SetLastError(0) |
| 32 |
MOVD $0, TEB_error(R18_PLATFORM) |
| 33 |
MOVD libcall_args(R19), R12 // libcall->args |
| 34 |
|
| 35 |
// Do we have more than 8 arguments? |
| 36 |
MOVD libcall_n(R19), R0 |
| 37 |
CMP $0, R0; BEQ _0args |
| 38 |
CMP $1, R0; BEQ _1args |
| 39 |
CMP $2, R0; BEQ _2args |
| 40 |
CMP $3, R0; BEQ _3args |
| 41 |
CMP $4, R0; BEQ _4args |
| 42 |
CMP $5, R0; BEQ _5args |
| 43 |
CMP $6, R0; BEQ _6args |
| 44 |
CMP $7, R0; BEQ _7args |
| 45 |
CMP $8, R0; BEQ _8args |
| 46 |
|
| 47 |
// Reserve stack space for remaining args |
| 48 |
SUB $8, R0, R2 |
| 49 |
ADD $1, R2, R3 // make even number of words for stack alignment |
| 50 |
AND $~1, R3 |
| 51 |
LSL $3, R3 |
| 52 |
SUB R3, RSP |
| 53 |
|
| 54 |
// R4: size of stack arguments (n-8)*8 |
| 55 |
// R5: &args[8] |
| 56 |
// R6: loop counter, from 0 to (n-8)*8 |
| 57 |
// R7: scratch |
| 58 |
// R8: copy of RSP - (R2)(RSP) assembles as (R2)(ZR) |
| 59 |
SUB $8, R0, R4 |
| 60 |
LSL $3, R4 |
| 61 |
ADD $(8*8), R12, R5 |
| 62 |
MOVD $0, R6 |
| 63 |
MOVD RSP, R8 |
| 64 |
stackargs: |
| 65 |
MOVD (R6)(R5), R7 |
| 66 |
MOVD R7, (R6)(R8) |
| 67 |
ADD $8, R6 |
| 68 |
CMP R6, R4 |
| 69 |
BNE stackargs |
| 70 |
|
| 71 |
_8args: |
| 72 |
MOVD (7*8)(R12), R7 |
| 73 |
_7args: |
| 74 |
MOVD (6*8)(R12), R6 |
| 75 |
_6args: |
| 76 |
MOVD (5*8)(R12), R5 |
| 77 |
_5args: |
| 78 |
MOVD (4*8)(R12), R4 |
| 79 |
_4args: |
| 80 |
MOVD (3*8)(R12), R3 |
| 81 |
_3args: |
| 82 |
MOVD (2*8)(R12), R2 |
| 83 |
_2args: |
| 84 |
MOVD (1*8)(R12), R1 |
| 85 |
_1args: |
| 86 |
MOVD (0*8)(R12), R0 |
| 87 |
_0args: |
| 88 |
|
| 89 |
MOVD libcall_fn(R19), R12 // branch to libcall->fn |
| 90 |
BL (R12) |
| 91 |
|
| 92 |
MOVD R20, RSP // free stack space |
| 93 |
MOVD R0, libcall_r1(R19) // save return value to libcall->r1 |
| 94 |
// TODO(rsc) floating point like amd64 in libcall->r2? |
| 95 |
|
| 96 |
// GetLastError |
| 97 |
MOVD TEB_error(R18_PLATFORM), R0 |
| 98 |
MOVD R0, libcall_err(R19) |
| 99 |
|
| 100 |
// Restore callee-saved registers. |
| 101 |
LDP 16(RSP), (R19, R20) |
| 102 |
RET |
| 103 |
|
| 104 |
TEXT runtime·getlasterror(SB),NOSPLIT,$0 |
| 105 |
MOVD TEB_error(R18_PLATFORM), R0 |
| 106 |
MOVD R0, ret+0(FP) |
| 107 |
RET |
| 108 |
|
| 109 |
// Called by Windows as a Vectored Exception Handler (VEH). |
| 110 |
// R0 is pointer to struct containing |
| 111 |
// exception record and context pointers. |
| 112 |
// R1 is the kind of sigtramp function. |
| 113 |
// Return value of sigtrampgo is stored in R0. |
| 114 |
TEXT sigtramp<>(SB),NOSPLIT,$176 |
| 115 |
// Switch from the host ABI to the Go ABI, safe args and lr. |
| 116 |
MOVD R0, R5 |
| 117 |
MOVD R1, R6 |
| 118 |
MOVD LR, R7 |
| 119 |
SAVE_R19_TO_R28(8*4) |
| 120 |
SAVE_F8_TO_F15(8*14) |
| 121 |
|
| 122 |
BL runtime·load_g(SB) // Clobers R0, R27, R28 (g) |
| 123 |
|
| 124 |
MOVD R5, R0 |
| 125 |
MOVD R6, R1 |
| 126 |
// Calling ABIInternal because TLS might be nil. |
| 127 |
BL runtime·sigtrampgo<ABIInternal>(SB) |
| 128 |
// Return value is already stored in R0. |
| 129 |
|
| 130 |
// Restore callee-save registers. |
| 131 |
RESTORE_R19_TO_R28(8*4) |
| 132 |
RESTORE_F8_TO_F15(8*14) |
| 133 |
MOVD R7, LR |
| 134 |
RET |
| 135 |
|
| 136 |
// Trampoline to resume execution from exception handler. |
| 137 |
// This is part of the control flow guard workaround. |
| 138 |
// It switches stacks and jumps to the continuation address. |
| 139 |
// R0 and R1 are set above at the end of sigtrampgo |
| 140 |
// in the context that starts executing at sigresume. |
| 141 |
TEXT runtime·sigresume(SB),NOSPLIT|NOFRAME,$0 |
| 142 |
// Important: do not smash LR, |
| 143 |
// which is set to a live value when handling |
| 144 |
// a signal by pushing a call to sigpanic onto the stack. |
| 145 |
MOVD R0, RSP |
| 146 |
B (R1) |
| 147 |
|
| 148 |
TEXT runtime·exceptiontramp(SB),NOSPLIT|NOFRAME,$0 |
| 149 |
MOVD $const_callbackVEH, R1 |
| 150 |
B sigtramp<>(SB) |
| 151 |
|
| 152 |
TEXT runtime·firstcontinuetramp(SB),NOSPLIT|NOFRAME,$0 |
| 153 |
MOVD $const_callbackFirstVCH, R1 |
| 154 |
B sigtramp<>(SB) |
| 155 |
|
| 156 |
TEXT runtime·lastcontinuetramp(SB),NOSPLIT|NOFRAME,$0 |
| 157 |
MOVD $const_callbackLastVCH, R1 |
| 158 |
B sigtramp<>(SB) |
| 159 |
|
| 160 |
TEXT runtime·callbackasm1(SB),NOSPLIT,$208-0 |
| 161 |
NO_LOCAL_POINTERS |
| 162 |
|
| 163 |
// On entry, the trampoline in zcallback_windows_arm64.s left |
| 164 |
// the callback index in R12 (which is volatile in the C ABI). |
| 165 |
|
| 166 |
// Save callback register arguments R0-R7. |
| 167 |
// We do this at the top of the frame so they're contiguous with stack arguments. |
| 168 |
// The 7*8 setting up R14 looks like a bug but is not: the eighth word |
| 169 |
// is the space the assembler reserved for our caller's frame pointer, |
| 170 |
// but we are not called from Go so that space is ours to use, |
| 171 |
// and we must to be contiguous with the stack arguments. |
| 172 |
MOVD $arg0-(7*8)(SP), R14 |
| 173 |
STP (R0, R1), (0*8)(R14) |
| 174 |
STP (R2, R3), (2*8)(R14) |
| 175 |
STP (R4, R5), (4*8)(R14) |
| 176 |
STP (R6, R7), (6*8)(R14) |
| 177 |
|
| 178 |
// Push C callee-save registers R19-R28. |
| 179 |
// LR, FP already saved. |
| 180 |
SAVE_R19_TO_R28(8*9) |
| 181 |
|
| 182 |
// Create a struct callbackArgs on our stack. |
| 183 |
MOVD $cbargs-(18*8+callbackArgs__size)(SP), R13 |
| 184 |
MOVD R12, callbackArgs_index(R13) // callback index |
| 185 |
MOVD R14, R0 |
| 186 |
MOVD R0, callbackArgs_args(R13) // address of args vector |
| 187 |
MOVD $0, R0 |
| 188 |
MOVD R0, callbackArgs_result(R13) // result |
| 189 |
|
| 190 |
// Call cgocallback, which will call callbackWrap(frame). |
| 191 |
MOVD $·callbackWrap<ABIInternal>(SB), R0 // PC of function to call, cgocallback takes an ABIInternal entry-point |
| 192 |
MOVD R13, R1 // frame (&callbackArgs{...}) |
| 193 |
MOVD $0, R2 // context |
| 194 |
STP (R0, R1), (1*8)(RSP) |
| 195 |
MOVD R2, (3*8)(RSP) |
| 196 |
BL runtime·cgocallback(SB) |
| 197 |
|
| 198 |
// Get callback result. |
| 199 |
MOVD $cbargs-(18*8+callbackArgs__size)(SP), R13 |
| 200 |
MOVD callbackArgs_result(R13), R0 |
| 201 |
|
| 202 |
RESTORE_R19_TO_R28(8*9) |
| 203 |
|
| 204 |
RET |
| 205 |
|
| 206 |
// uint32 tstart_stdcall(M *newm); |
| 207 |
TEXT runtime·tstart_stdcall(SB),NOSPLIT,$96-0 |
| 208 |
SAVE_R19_TO_R28(8*3) |
| 209 |
|
| 210 |
MOVD m_g0(R0), g |
| 211 |
MOVD R0, g_m(g) |
| 212 |
BL runtime·save_g(SB) |
| 213 |
|
| 214 |
// Set up stack guards for OS stack. |
| 215 |
MOVD RSP, R0 |
| 216 |
MOVD R0, g_stack+stack_hi(g) |
| 217 |
SUB $(64*1024), R0 |
| 218 |
MOVD R0, (g_stack+stack_lo)(g) |
| 219 |
MOVD R0, g_stackguard0(g) |
| 220 |
MOVD R0, g_stackguard1(g) |
| 221 |
|
| 222 |
BL runtime·emptyfunc(SB) // fault if stack check is wrong |
| 223 |
BL runtime·mstart(SB) |
| 224 |
|
| 225 |
RESTORE_R19_TO_R28(8*3) |
| 226 |
|
| 227 |
// Exit the thread. |
| 228 |
MOVD $0, R0 |
| 229 |
RET |
| 230 |
|
| 231 |
TEXT runtime·nanotime1(SB),NOSPLIT,$0-8 |
| 232 |
MOVD $_INTERRUPT_TIME, R3 |
| 233 |
MOVD time_lo(R3), R0 |
| 234 |
MOVD $100, R1 |
| 235 |
MUL R1, R0 |
| 236 |
MOVD R0, ret+0(FP) |
| 237 |
RET |
| 238 |
|
| 239 |
// This is called from rt0_go, which runs on the system stack |
| 240 |
// using the initial stack allocated by the OS. |
| 241 |
// It calls back into standard C using the BL below. |
| 242 |
TEXT runtime·wintls(SB),NOSPLIT,$0 |
| 243 |
// Allocate a TLS slot to hold g across calls to external code |
| 244 |
MOVD runtime·_TlsAlloc(SB), R0 |
| 245 |
SUB $16, RSP // skip over saved frame pointer below RSP |
| 246 |
BL (R0) |
| 247 |
ADD $16, RSP |
| 248 |
|
| 249 |
// Assert that slot is less than 64 so we can use _TEB->TlsSlots |
| 250 |
CMP $64, R0 |
| 251 |
BLT ok |
| 252 |
// Fallback to the TEB arbitrary pointer. |
| 253 |
// TODO: don't use the arbitrary pointer (see go.dev/issue/59824) |
| 254 |
MOVD $TEB_ArbitraryPtr, R0 |
| 255 |
B settls |
| 256 |
ok: |
| 257 |
|
| 258 |
// Save offset from R18 into tls_g. |
| 259 |
LSL $3, R0 |
| 260 |
ADD $TEB_TlsSlots, R0 |
| 261 |
settls: |
| 262 |
MOVD R0, runtime·tls_g(SB) |
| 263 |
RET |