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