Text file src/runtime/sys_windows_arm64.s

     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  
    16  // Note: R0-R7 are args, R8 is indirect return value address,
    17  // R9-R15 are caller-save, R19-R29 are callee-save.
    18  //
    19  // load_g and save_g (in tls_arm64.s) clobber R27 (REGTMP) and R0.
    20  
    21  // void runtime·asmstdcall(void *c);
    22  TEXT runtime·asmstdcall(SB),NOSPLIT|NOFRAME,$0
    23  	STP.W	(R29, R30), -32(RSP)	// allocate C ABI stack frame
    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  	LDP.P	32(RSP), (R29, R30)
   100  	RET
   101  
   102  TEXT runtime·badsignal2(SB),NOSPLIT,$16-0
   103  	NO_LOCAL_POINTERS
   104  
   105  	// stderr
   106  	MOVD	runtime·_GetStdHandle(SB), R1
   107  	MOVD	$-12, R0
   108  	SUB	$16, RSP	// skip over saved frame pointer below RSP
   109  	BL	(R1)
   110  	ADD	$16, RSP
   111  
   112  	// handle in R0 already
   113  	MOVD	$runtime·badsignalmsg(SB), R1	// lpBuffer
   114  	MOVD	$runtime·badsignallen(SB), R2	// lpNumberOfBytesToWrite
   115  	MOVD	(R2), R2
   116  	MOVD	R13, R3		// lpNumberOfBytesWritten
   117  	MOVD	$0, R4			// lpOverlapped
   118  	MOVD	runtime·_WriteFile(SB), R12
   119  	SUB	$16, RSP	// skip over saved frame pointer below RSP
   120  	BL	(R12)
   121  
   122  	// Does not return.
   123  	B	runtime·abort(SB)
   124  
   125  	RET
   126  
   127  TEXT runtime·getlasterror(SB),NOSPLIT|NOFRAME,$0
   128  	MOVD	TEB_error(R18_PLATFORM), R0
   129  	MOVD	R0, ret+0(FP)
   130  	RET
   131  
   132  // Called by Windows as a Vectored Exception Handler (VEH).
   133  // First argument is pointer to struct containing
   134  // exception record and context pointers.
   135  // Handler function is stored in R1
   136  // Return 0 for 'not handled', -1 for handled.
   137  // int32_t sigtramp(
   138  //     PEXCEPTION_POINTERS ExceptionInfo,
   139  //     func *GoExceptionHandler);
   140  TEXT sigtramp<>(SB),NOSPLIT|NOFRAME,$0
   141  	// Save R0, R1 (args) as well as LR, R27, R28 (callee-save).
   142  	MOVD	R0, R5
   143  	MOVD	R1, R6
   144  	MOVD	LR, R7
   145  	MOVD	R27, R16		// saved R27 (callee-save)
   146  	MOVD	g, R17 			// saved R28 (callee-save from Windows, not really g)
   147  
   148  	BL      runtime·load_g(SB)	// smashes R0, R27, R28 (g)
   149  	CMP	$0, g			// is there a current g?
   150  	BNE	2(PC)
   151  	BL	runtime·badsignal2(SB)
   152  
   153  	// Do we need to switch to the g0 stack?
   154  	MOVD	g, R3			// R3 = oldg (for sigtramp_g0)
   155  	MOVD	g_m(g), R2		// R2 = m
   156  	MOVD	m_g0(R2), R2		// R2 = g0
   157  	CMP	g, R2			// if curg == g0
   158  	BNE	switch
   159  
   160  	// No: on g0 stack already, tail call to sigtramp_g0.
   161  	// Restore all the callee-saves so sigtramp_g0 can return to our caller.
   162  	// We also pass R2 = g0, R3 = oldg, both set above.
   163  	MOVD	R5, R0
   164  	MOVD	R6, R1
   165  	MOVD	R7, LR
   166  	MOVD	R16, R27		// restore R27
   167  	MOVD	R17, g 			// restore R28
   168  	B	sigtramp_g0<>(SB)
   169  
   170  switch:
   171  	// switch to g0 stack (but do not update g - that's sigtramp_g0's job)
   172  	MOVD	RSP, R8
   173  	MOVD	(g_sched+gobuf_sp)(R2), R4	// R4 = g->gobuf.sp
   174  	SUB	$(6*8), R4			// alloc space for saves - 2 words below SP for frame pointer, 3 for us to use, 1 for alignment
   175  	MOVD	R4, RSP				// switch to g0 stack
   176  
   177  	MOVD	$0, (0*8)(RSP)	// fake saved LR
   178  	MOVD	R7, (1*8)(RSP)	// saved LR
   179  	MOVD	R8, (2*8)(RSP)	// saved SP
   180  
   181  	MOVD	R5, R0		// original args
   182  	MOVD	R6, R1		// original args
   183  	MOVD	R16, R27
   184  	MOVD	R17, g 		// R28
   185  	BL	sigtramp_g0<>(SB)
   186  
   187  	// switch back to original stack; g already updated
   188  	MOVD	(1*8)(RSP), R7	// saved LR
   189  	MOVD	(2*8)(RSP), R8	// saved SP
   190  	MOVD	R7, LR
   191  	MOVD	R8, RSP
   192  	RET
   193  
   194  // sigtramp_g0 is running on the g0 stack, with R2 = g0, R3 = oldg.
   195  // But g itself is not set - that's R28, a callee-save register,
   196  // and it still holds the value from the Windows DLL caller.
   197  TEXT sigtramp_g0<>(SB),NOSPLIT,$128
   198  	NO_LOCAL_POINTERS
   199  
   200  	// Push C callee-save registers R19-R28. LR, FP already saved.
   201  	// These registers will occupy the upper 10 words of the frame.
   202  	SAVE_R19_TO_R28(8*7)
   203  
   204  	MOVD	0(R0), R5	// R5 = ExceptionPointers->ExceptionRecord
   205  	MOVD	8(R0), R6	// R6 = ExceptionPointers->ContextRecord
   206  	MOVD	R6, context-(11*8)(SP)
   207  
   208  	MOVD	R2, g 			// g0
   209  	BL      runtime·save_g(SB)	// smashes R0
   210  
   211  	MOVD	R5, (1*8)(RSP)	// arg0 (ExceptionRecord)
   212  	MOVD	R6, (2*8)(RSP)	// arg1 (ContextRecord)
   213  	MOVD	R3, (3*8)(RSP)	// arg2 (original g)
   214  	MOVD	R3, oldg-(12*8)(SP)
   215  	BL	(R1)
   216  	MOVD	oldg-(12*8)(SP), g
   217  	BL      runtime·save_g(SB)	// smashes R0
   218  	MOVW	(4*8)(RSP), R0	// return value (0 or -1)
   219  
   220  	// if return value is CONTINUE_SEARCH, do not set up control
   221  	// flow guard workaround
   222  	CMP	$0, R0
   223  	BEQ	return
   224  
   225  	// Check if we need to set up the control flow guard workaround.
   226  	// On Windows, the stack pointer in the context must lie within
   227  	// system stack limits when we resume from exception.
   228  	// Store the resume SP and PC in alternate registers
   229  	// and return to sigresume on the g0 stack.
   230  	// sigresume makes no use of the stack at all,
   231  	// loading SP from R0 and jumping to R1.
   232  	// Note that smashing R0 and R1 is only safe because we know sigpanic
   233  	// will not actually return to the original frame, so the registers
   234  	// are effectively dead. But this does mean we can't use the
   235  	// same mechanism for async preemption.
   236  	MOVD	context-(11*8)(SP), R6
   237  	MOVD	context_pc(R6), R2		// load PC from context record
   238  	MOVD	$sigresume<>(SB), R1
   239  
   240  	CMP	R1, R2
   241  	BEQ	return				// do not clobber saved SP/PC
   242  
   243  	// Save resume SP and PC into R0, R1.
   244  	MOVD	context_xsp(R6), R2
   245  	MOVD	R2, (context_x+0*8)(R6)
   246  	MOVD	context_pc(R6), R2
   247  	MOVD	R2, (context_x+1*8)(R6)
   248  
   249  	// Set up context record to return to sigresume on g0 stack
   250  	MOVD	RSP, R2
   251  	MOVD	R2, context_xsp(R6)
   252  	MOVD	$sigresume<>(SB), R2
   253  	MOVD	R2, context_pc(R6)
   254  
   255  return:
   256  	RESTORE_R19_TO_R28(8*7)		// smashes g
   257  	RET
   258  
   259  // Trampoline to resume execution from exception handler.
   260  // This is part of the control flow guard workaround.
   261  // It switches stacks and jumps to the continuation address.
   262  // R0 and R1 are set above at the end of sigtramp<>
   263  // in the context that starts executing at sigresume<>.
   264  TEXT sigresume<>(SB),NOSPLIT|NOFRAME,$0
   265  	// Important: do not smash LR,
   266  	// which is set to a live value when handling
   267  	// a signal by pushing a call to sigpanic onto the stack.
   268  	MOVD	R0, RSP
   269  	B	(R1)
   270  
   271  TEXT runtime·exceptiontramp(SB),NOSPLIT|NOFRAME,$0
   272  	MOVD	$runtime·exceptionhandler(SB), R1
   273  	B	sigtramp<>(SB)
   274  
   275  TEXT runtime·firstcontinuetramp(SB),NOSPLIT|NOFRAME,$0
   276  	MOVD	$runtime·firstcontinuehandler(SB), R1
   277  	B	sigtramp<>(SB)
   278  
   279  TEXT runtime·lastcontinuetramp(SB),NOSPLIT|NOFRAME,$0
   280  	MOVD	$runtime·lastcontinuehandler(SB), R1
   281  	B	sigtramp<>(SB)
   282  
   283  GLOBL runtime·cbctxts(SB), NOPTR, $4
   284  
   285  TEXT runtime·callbackasm1(SB),NOSPLIT,$208-0
   286  	NO_LOCAL_POINTERS
   287  
   288  	// On entry, the trampoline in zcallback_windows_arm64.s left
   289  	// the callback index in R12 (which is volatile in the C ABI).
   290  
   291  	// Save callback register arguments R0-R7.
   292  	// We do this at the top of the frame so they're contiguous with stack arguments.
   293  	// The 7*8 setting up R14 looks like a bug but is not: the eighth word
   294  	// is the space the assembler reserved for our caller's frame pointer,
   295  	// but we are not called from Go so that space is ours to use,
   296  	// and we must to be contiguous with the stack arguments.
   297  	MOVD	$arg0-(7*8)(SP), R14
   298  	STP	(R0, R1), (0*8)(R14)
   299  	STP	(R2, R3), (2*8)(R14)
   300  	STP	(R4, R5), (4*8)(R14)
   301  	STP	(R6, R7), (6*8)(R14)
   302  
   303  	// Push C callee-save registers R19-R28.
   304  	// LR, FP already saved.
   305  	SAVE_R19_TO_R28(8*9)
   306  
   307  	// Create a struct callbackArgs on our stack.
   308  	MOVD	$cbargs-(18*8+callbackArgs__size)(SP), R13
   309  	MOVD	R12, callbackArgs_index(R13)	// callback index
   310  	MOVD	R14, R0
   311  	MOVD	R0, callbackArgs_args(R13)		// address of args vector
   312  	MOVD	$0, R0
   313  	MOVD	R0, callbackArgs_result(R13)	// result
   314  
   315  	// Call cgocallback, which will call callbackWrap(frame).
   316  	MOVD	$·callbackWrap<ABIInternal>(SB), R0	// PC of function to call, cgocallback takes an ABIInternal entry-point
   317  	MOVD	R13, R1	// frame (&callbackArgs{...})
   318  	MOVD	$0, R2	// context
   319  	STP	(R0, R1), (1*8)(RSP)
   320  	MOVD	R2, (3*8)(RSP)
   321  	BL	runtime·cgocallback(SB)
   322  
   323  	// Get callback result.
   324  	MOVD	$cbargs-(18*8+callbackArgs__size)(SP), R13
   325  	MOVD	callbackArgs_result(R13), R0
   326  
   327  	RESTORE_R19_TO_R28(8*9)
   328  
   329  	RET
   330  
   331  // uint32 tstart_stdcall(M *newm);
   332  TEXT runtime·tstart_stdcall(SB),NOSPLIT,$96-0
   333  	SAVE_R19_TO_R28(8*3)
   334  
   335  	MOVD	m_g0(R0), g
   336  	MOVD	R0, g_m(g)
   337  	BL	runtime·save_g(SB)
   338  
   339  	// Set up stack guards for OS stack.
   340  	MOVD	RSP, R0
   341  	MOVD	R0, g_stack+stack_hi(g)
   342  	SUB	$(64*1024), R0
   343  	MOVD	R0, (g_stack+stack_lo)(g)
   344  	MOVD	R0, g_stackguard0(g)
   345  	MOVD	R0, g_stackguard1(g)
   346  
   347  	BL	runtime·emptyfunc(SB)	// fault if stack check is wrong
   348  	BL	runtime·mstart(SB)
   349  
   350  	RESTORE_R19_TO_R28(8*3)
   351  
   352  	// Exit the thread.
   353  	MOVD	$0, R0
   354  	RET
   355  
   356  // Runs on OS stack.
   357  // duration (in -100ns units) is in dt+0(FP).
   358  // g may be nil.
   359  TEXT runtime·usleep2(SB),NOSPLIT,$32-4
   360  	MOVW	dt+0(FP), R0
   361  	MOVD	$16(RSP), R2		// R2 = pTime
   362  	MOVD	R0, 0(R2)		// *pTime = -dt
   363  	MOVD	$-1, R0			// R0 = handle
   364  	MOVD	$0, R1			// R1 = FALSE (alertable)
   365  	MOVD	runtime·_NtWaitForSingleObject(SB), R3
   366  	SUB	$16, RSP	// skip over saved frame pointer below RSP
   367  	BL	(R3)
   368  	ADD	$16, RSP
   369  	RET
   370  
   371  // Runs on OS stack.
   372  // duration (in -100ns units) is in dt+0(FP).
   373  // g is valid.
   374  // TODO: needs to be implemented properly.
   375  TEXT runtime·usleep2HighRes(SB),NOSPLIT,$0-4
   376  	B	runtime·abort(SB)
   377  
   378  // Runs on OS stack.
   379  TEXT runtime·switchtothread(SB),NOSPLIT,$16-0
   380  	MOVD	runtime·_SwitchToThread(SB), R0
   381  	SUB	$16, RSP	// skip over saved frame pointer below RSP
   382  	BL	(R0)
   383  	ADD	$16, RSP
   384  	RET
   385  
   386  TEXT runtime·nanotime1(SB),NOSPLIT|NOFRAME,$0-8
   387  	MOVB	runtime·useQPCTime(SB), R0
   388  	CMP	$0, R0
   389  	BNE	useQPC
   390  	MOVD	$_INTERRUPT_TIME, R3
   391  	MOVD	time_lo(R3), R0
   392  	MOVD	$100, R1
   393  	MUL	R1, R0
   394  	MOVD	R0, ret+0(FP)
   395  	RET
   396  useQPC:
   397  	B	runtime·nanotimeQPC(SB)		// tail call
   398  
   399  // This is called from rt0_go, which runs on the system stack
   400  // using the initial stack allocated by the OS.
   401  // It calls back into standard C using the BL below.
   402  TEXT runtime·wintls(SB),NOSPLIT,$0
   403  	// Allocate a TLS slot to hold g across calls to external code
   404  	MOVD	runtime·_TlsAlloc(SB), R0
   405  	SUB	$16, RSP	// skip over saved frame pointer below RSP
   406  	BL	(R0)
   407  	ADD	$16, RSP
   408  
   409  	// Assert that slot is less than 64 so we can use _TEB->TlsSlots
   410  	CMP	$64, R0
   411  	BLT	ok
   412  	MOVD	$runtime·abort(SB), R1
   413  	BL	(R1)
   414  ok:
   415  
   416  	// Save offset from R18 into tls_g.
   417  	LSL	$3, R0
   418  	ADD	$TEB_TlsSlots, R0
   419  	MOVD	R0, runtime·tls_g(SB)
   420  	RET
   421  

View as plain text