...

Text file src/runtime/sys_windows_arm64.s

Documentation: runtime

     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
    22TEXT runtime·getlasterror(SB),NOSPLIT,$0
    23	MOVD	TEB_error(R18_PLATFORM), R0
    24	MOVD	R0, ret+0(FP)
    25	RET
    26
    27// Called by Windows as a Vectored Exception Handler (VEH).
    28// R0 is pointer to struct containing
    29// exception record and context pointers.
    30// R1 is the kind of sigtramp function.
    31// Return value of sigtrampgo is stored in R0.
    32TEXT sigtramp<>(SB),NOSPLIT,$176
    33	// Switch from the host ABI to the Go ABI, safe args and lr.
    34	MOVD	R0, R5
    35	MOVD	R1, R6
    36	MOVD	LR, R7
    37	SAVE_R19_TO_R28(8*4)
    38	SAVE_F8_TO_F15(8*14)
    39
    40	BL	runtime·load_g(SB)	// Clobers R0, R27, R28 (g)
    41
    42	MOVD	R5, R0
    43	MOVD	R6, R1
    44	// Calling ABIInternal because TLS might be nil.
    45	BL	runtime·sigtrampgo<ABIInternal>(SB)
    46	// Return value is already stored in R0.
    47
    48	// Restore callee-save registers.
    49	RESTORE_R19_TO_R28(8*4)
    50	RESTORE_F8_TO_F15(8*14)
    51	MOVD	R7, LR
    52	RET
    53
    54// Trampoline to resume execution from exception handler.
    55// This is part of the control flow guard workaround.
    56// It switches stacks and jumps to the continuation address.
    57// R0 and R1 are set above at the end of sigtrampgo
    58// in the context that starts executing at sigresume.
    59TEXT runtime·sigresume(SB),NOSPLIT|NOFRAME,$0
    60	// Important: do not smash LR,
    61	// which is set to a live value when handling
    62	// a signal by pushing a call to sigpanic onto the stack.
    63	MOVD	R0, RSP
    64	B	(R1)
    65
    66TEXT runtime·exceptiontramp(SB),NOSPLIT|NOFRAME,$0
    67	MOVD	$const_callbackVEH, R1
    68	B	sigtramp<>(SB)
    69
    70TEXT runtime·firstcontinuetramp(SB),NOSPLIT|NOFRAME,$0
    71	MOVD	$const_callbackFirstVCH, R1
    72	B	sigtramp<>(SB)
    73
    74TEXT runtime·lastcontinuetramp(SB),NOSPLIT|NOFRAME,$0
    75	MOVD	$const_callbackLastVCH, R1
    76	B	sigtramp<>(SB)
    77
    78TEXT runtime·callbackasm1(SB),NOSPLIT,$208-0
    79	NO_LOCAL_POINTERS
    80
    81	// On entry, the trampoline in zcallback_windows_arm64.s left
    82	// the callback index in R12 (which is volatile in the C ABI).
    83
    84	// Save callback register arguments R0-R7.
    85	// We do this at the top of the frame so they're contiguous with stack arguments.
    86	// The 7*8 setting up R14 looks like a bug but is not: the eighth word
    87	// is the space the assembler reserved for our caller's frame pointer,
    88	// but we are not called from Go so that space is ours to use,
    89	// and we must to be contiguous with the stack arguments.
    90	MOVD	$arg0-(7*8)(SP), R14
    91	STP	(R0, R1), (0*8)(R14)
    92	STP	(R2, R3), (2*8)(R14)
    93	STP	(R4, R5), (4*8)(R14)
    94	STP	(R6, R7), (6*8)(R14)
    95
    96	// Push C callee-save registers R19-R28.
    97	// LR, FP already saved.
    98	SAVE_R19_TO_R28(8*9)
    99
   100	// Create a struct callbackArgs on our stack.
   101	MOVD	$cbargs-(18*8+callbackArgs__size)(SP), R13
   102	MOVD	R12, callbackArgs_index(R13)	// callback index
   103	MOVD	R14, R0
   104	MOVD	R0, callbackArgs_args(R13)		// address of args vector
   105	MOVD	$0, R0
   106	MOVD	R0, callbackArgs_result(R13)	// result
   107
   108	// Call cgocallback, which will call callbackWrap(frame).
   109	MOVD	$·callbackWrap<ABIInternal>(SB), R0	// PC of function to call, cgocallback takes an ABIInternal entry-point
   110	MOVD	R13, R1	// frame (&callbackArgs{...})
   111	MOVD	$0, R2	// context
   112	STP	(R0, R1), (1*8)(RSP)
   113	MOVD	R2, (3*8)(RSP)
   114	BL	runtime·cgocallback(SB)
   115
   116	// Get callback result.
   117	MOVD	$cbargs-(18*8+callbackArgs__size)(SP), R13
   118	MOVD	callbackArgs_result(R13), R0
   119
   120	RESTORE_R19_TO_R28(8*9)
   121
   122	RET
   123
   124// uint32 tstart_stdcall(M *newm);
   125TEXT runtime·tstart_stdcall(SB),NOSPLIT,$96-0
   126	SAVE_R19_TO_R28(8*3)
   127
   128	MOVD	m_g0(R0), g
   129	MOVD	R0, g_m(g)
   130	BL	runtime·save_g(SB)
   131
   132	// Set up stack guards for OS stack.
   133	MOVD	RSP, R0
   134	MOVD	R0, g_stack+stack_hi(g)
   135	SUB	$(64*1024), R0
   136	MOVD	R0, (g_stack+stack_lo)(g)
   137	MOVD	R0, g_stackguard0(g)
   138	MOVD	R0, g_stackguard1(g)
   139
   140	BL	runtime·emptyfunc(SB)	// fault if stack check is wrong
   141	BL	runtime·mstart(SB)
   142
   143	RESTORE_R19_TO_R28(8*3)
   144
   145	// Exit the thread.
   146	MOVD	$0, R0
   147	RET
   148
   149TEXT runtime·nanotime1(SB),NOSPLIT,$0-8
   150	MOVD	$_INTERRUPT_TIME, R3
   151	MOVD	time_lo(R3), R0
   152	MOVD	$100, R1
   153	MUL	R1, R0
   154	MOVD	R0, ret+0(FP)
   155	RET
   156
   157// This is called from rt0_go, which runs on the system stack
   158// using the initial stack allocated by the OS.
   159// It calls back into standard C using the BL below.
   160TEXT runtime·wintls(SB),NOSPLIT,$0
   161	// Allocate a TLS slot to hold g across calls to external code
   162	MOVD	runtime·_TlsAlloc(SB), R0
   163	SUB	$16, RSP	// skip over saved frame pointer below RSP
   164	BL	(R0)
   165	ADD	$16, RSP
   166
   167	// Assert that slot is less than 64 so we can use _TEB->TlsSlots
   168	CMP	$64, R0
   169	BLT	ok
   170	// Fallback to the TEB arbitrary pointer.
   171	// TODO: don't use the arbitrary pointer (see go.dev/issue/59824)
   172	MOVD	$TEB_ArbitraryPtr, R0
   173	B	settls
   174ok:
   175
   176	// Save offset from R18 into tls_g.
   177	LSL	$3, R0
   178	ADD	$TEB_TlsSlots, R0
   179settls:
   180	MOVD	R0, runtime·tls_g(SB)
   181	RET

View as plain text