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

View as plain text