github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/sys_windows_arm.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 "time_windows.h"
     9  
    10  // Note: For system ABI, R0-R3 are args, R4-R11 are callee-save.
    11  
    12  // void runtime·asmstdcall(void *c);
    13  TEXT runtime·asmstdcall(SB),NOSPLIT|NOFRAME,$0
    14  	MOVM.DB.W [R4, R5, R14], (R13)	// push {r4, r5, lr}
    15  	MOVW	R0, R4			// put libcall * in r4
    16  	MOVW	R13, R5			// save stack pointer in r5
    17  
    18  	// SetLastError(0)
    19  	MOVW	$0, R0
    20  	MRC	15, 0, R1, C13, C0, 2
    21  	MOVW	R0, 0x34(R1)
    22  
    23  	MOVW	8(R4), R12	// libcall->args
    24  
    25  	// Do we have more than 4 arguments?
    26  	MOVW	4(R4), R0	// libcall->n
    27  	SUB.S	$4, R0, R2
    28  	BLE	loadregs
    29  
    30  	// Reserve stack space for remaining args
    31  	SUB	R2<<2, R13
    32  	BIC	$0x7, R13	// alignment for ABI
    33  
    34  	// R0: count of arguments
    35  	// R1:
    36  	// R2: loop counter, from 0 to (n-4)
    37  	// R3: scratch
    38  	// R4: pointer to libcall struct
    39  	// R12: libcall->args
    40  	MOVW	$0, R2
    41  stackargs:
    42  	ADD	$4, R2, R3		// r3 = args[4 + i]
    43  	MOVW	R3<<2(R12), R3
    44  	MOVW	R3, R2<<2(R13)		// stack[i] = r3
    45  
    46  	ADD	$1, R2			// i++
    47  	SUB	$4, R0, R3		// while (i < (n - 4))
    48  	CMP	R3, R2
    49  	BLT	stackargs
    50  
    51  loadregs:
    52  	CMP	$3, R0
    53  	MOVW.GT 12(R12), R3
    54  
    55  	CMP	$2, R0
    56  	MOVW.GT 8(R12), R2
    57  
    58  	CMP	$1, R0
    59  	MOVW.GT 4(R12), R1
    60  
    61  	CMP	$0, R0
    62  	MOVW.GT 0(R12), R0
    63  
    64  	BIC	$0x7, R13		// alignment for ABI
    65  	MOVW	0(R4), R12		// branch to libcall->fn
    66  	BL	(R12)
    67  
    68  	MOVW	R5, R13			// free stack space
    69  	MOVW	R0, 12(R4)		// save return value to libcall->r1
    70  	MOVW	R1, 16(R4)
    71  
    72  	// GetLastError
    73  	MRC	15, 0, R1, C13, C0, 2
    74  	MOVW	0x34(R1), R0
    75  	MOVW	R0, 20(R4)		// store in libcall->err
    76  
    77  	MOVM.IA.W (R13), [R4, R5, R15]
    78  
    79  TEXT runtime·badsignal2(SB),NOSPLIT|NOFRAME,$0
    80  	MOVM.DB.W [R4, R14], (R13)	// push {r4, lr}
    81  	MOVW	R13, R4			// save original stack pointer
    82  	SUB	$8, R13			// space for 2 variables
    83  	BIC	$0x7, R13		// alignment for ABI
    84  
    85  	// stderr
    86  	MOVW	runtime·_GetStdHandle(SB), R1
    87  	MOVW	$-12, R0
    88  	BL	(R1)
    89  
    90  	MOVW	$runtime·badsignalmsg(SB), R1	// lpBuffer
    91  	MOVW	$runtime·badsignallen(SB), R2	// lpNumberOfBytesToWrite
    92  	MOVW	(R2), R2
    93  	ADD	$0x4, R13, R3		// lpNumberOfBytesWritten
    94  	MOVW	$0, R12			// lpOverlapped
    95  	MOVW	R12, (R13)
    96  
    97  	MOVW	runtime·_WriteFile(SB), R12
    98  	BL	(R12)
    99  
   100  	// Does not return.
   101  	B	runtime·abort(SB)
   102  
   103  TEXT runtime·getlasterror(SB),NOSPLIT,$0
   104  	MRC	15, 0, R0, C13, C0, 2
   105  	MOVW	0x34(R0), R0
   106  	MOVW	R0, ret+0(FP)
   107  	RET
   108  
   109  // Called by Windows as a Vectored Exception Handler (VEH).
   110  // First argument is pointer to struct containing
   111  // exception record and context pointers.
   112  // Handler function is stored in R1
   113  // Return 0 for 'not handled', -1 for handled.
   114  // int32_t sigtramp(
   115  //     PEXCEPTION_POINTERS ExceptionInfo,
   116  //     func *GoExceptionHandler);
   117  TEXT sigtramp<>(SB),NOSPLIT|NOFRAME,$0
   118  	MOVM.DB.W [R0, R4-R11, R14], (R13)	// push {r0, r4-r11, lr} (SP-=40)
   119  	SUB	$(8+20), R13		// reserve space for g, sp, and
   120  					// parameters/retval to go call
   121  
   122  	MOVW	R0, R6			// Save param0
   123  	MOVW	R1, R7			// Save param1
   124  
   125  	BL      runtime·load_g(SB)
   126  	CMP	$0,	g		// is there a current g?
   127  	BNE	g_ok
   128  	ADD	$(8+20), R13	// free locals
   129  	MOVM.IA.W (R13), [R3, R4-R11, R14]	// pop {r3, r4-r11, lr}
   130  	MOVW	$0, R0		// continue 
   131  	BEQ	return
   132  
   133  g_ok:
   134  
   135  	// save g and SP in case of stack switch
   136  	MOVW	R13, 24(R13)
   137  	MOVW	g, 20(R13)
   138  
   139  	// do we need to switch to the g0 stack?
   140  	MOVW	g, R5			// R5 = g
   141  	MOVW	g_m(R5), R2		// R2 = m
   142  	MOVW	m_g0(R2), R4		// R4 = g0
   143  	CMP	R5, R4			// if curg == g0
   144  	BEQ	g0
   145  
   146  	// switch to g0 stack
   147  	MOVW	R4, g				// g = g0
   148  	MOVW	(g_sched+gobuf_sp)(g), R3	// R3 = g->gobuf.sp
   149  	BL      runtime·save_g(SB)
   150  
   151  	// make room for sighandler arguments
   152  	// and re-save old SP for restoring later.
   153  	// (note that the 24(R3) here must match the 24(R13) above.)
   154  	SUB	$40, R3
   155  	MOVW	R13, 24(R3)		// save old stack pointer
   156  	MOVW	R3, R13			// switch stack
   157  
   158  g0:
   159  	MOVW	0(R6), R2	// R2 = ExceptionPointers->ExceptionRecord
   160  	MOVW	4(R6), R3	// R3 = ExceptionPointers->ContextRecord
   161  
   162  	MOVW	$0, R4
   163  	MOVW	R4, 0(R13)	// No saved link register.
   164  	MOVW	R2, 4(R13)	// Move arg0 (ExceptionRecord) into position
   165  	MOVW	R3, 8(R13)	// Move arg1 (ContextRecord) into position
   166  	MOVW	R5, 12(R13)	// Move arg2 (original g) into position
   167  	BL	(R7)		// Call the goroutine
   168  	MOVW	16(R13), R4	// Fetch return value from stack
   169  
   170  	// Save system stack pointer for sigresume setup below.
   171  	// The exact value does not matter - nothing is read or written
   172  	// from this address. It just needs to be on the system stack.
   173  	MOVW	R13, R12
   174  
   175  	// switch back to original stack and g
   176  	MOVW	24(R13), R13
   177  	MOVW	20(R13), g
   178  	BL      runtime·save_g(SB)
   179  
   180  done:
   181  	MOVW	R4, R0				// move retval into position
   182  	ADD	$(8 + 20), R13			// free locals
   183  	MOVM.IA.W (R13), [R3, R4-R11, R14]	// pop {r3, r4-r11, lr}
   184  
   185  	// if return value is CONTINUE_SEARCH, do not set up control
   186  	// flow guard workaround
   187  	CMP	$0, R0
   188  	BEQ	return
   189  
   190  	// Check if we need to set up the control flow guard workaround.
   191  	// On Windows, the stack pointer in the context must lie within
   192  	// system stack limits when we resume from exception.
   193  	// Store the resume SP and PC on the g0 stack,
   194  	// and return to sigresume on the g0 stack. sigresume
   195  	// pops the saved PC and SP from the g0 stack, resuming execution
   196  	// at the desired location.
   197  	// If sigresume has already been set up by a previous exception
   198  	// handler, don't clobber the stored SP and PC on the stack.
   199  	MOVW	4(R3), R3			// PEXCEPTION_POINTERS->Context
   200  	MOVW	context_pc(R3), R2		// load PC from context record
   201  	MOVW	$sigresume<>(SB), R1
   202  	CMP	R1, R2
   203  	B.EQ	return				// do not clobber saved SP/PC
   204  
   205  	// Save resume SP and PC into R0, R1.
   206  	MOVW	context_spr(R3), R2
   207  	MOVW	R2, context_r0(R3)
   208  	MOVW	context_pc(R3), R2
   209  	MOVW	R2, context_r1(R3)
   210  
   211  	// Set up context record to return to sigresume on g0 stack
   212  	MOVW	R12, context_spr(R3)
   213  	MOVW	$sigresume<>(SB), R2
   214  	MOVW	R2, context_pc(R3)
   215  
   216  return:
   217  	B	(R14)				// return
   218  
   219  // Trampoline to resume execution from exception handler.
   220  // This is part of the control flow guard workaround.
   221  // It switches stacks and jumps to the continuation address.
   222  // R0 and R1 are set above at the end of sigtramp<>
   223  // in the context that starts executing at sigresume<>.
   224  TEXT sigresume<>(SB),NOSPLIT|NOFRAME,$0
   225  	// Important: do not smash LR,
   226  	// which is set to a live value when handling
   227  	// a signal by pushing a call to sigpanic onto the stack.
   228  	MOVW	R0, R13
   229  	B	(R1)
   230  
   231  TEXT runtime·exceptiontramp(SB),NOSPLIT|NOFRAME,$0
   232  	MOVW	$runtime·exceptionhandler(SB), R1
   233  	B	sigtramp<>(SB)
   234  
   235  TEXT runtime·firstcontinuetramp(SB),NOSPLIT|NOFRAME,$0
   236  	MOVW	$runtime·firstcontinuehandler(SB), R1
   237  	B	sigtramp<>(SB)
   238  
   239  TEXT runtime·lastcontinuetramp(SB),NOSPLIT|NOFRAME,$0
   240  	MOVW	$runtime·lastcontinuehandler(SB), R1
   241  	B	sigtramp<>(SB)
   242  
   243  GLOBL runtime·cbctxts(SB), NOPTR, $4
   244  
   245  TEXT runtime·callbackasm1(SB),NOSPLIT|NOFRAME,$0
   246  	// On entry, the trampoline in zcallback_windows_arm.s left
   247  	// the callback index in R12 (which is volatile in the C ABI).
   248  
   249  	// Push callback register arguments r0-r3. We do this first so
   250  	// they're contiguous with stack arguments.
   251  	MOVM.DB.W [R0-R3], (R13)
   252  	// Push C callee-save registers r4-r11 and lr.
   253  	MOVM.DB.W [R4-R11, R14], (R13)
   254  	SUB	$(16 + callbackArgs__size), R13	// space for locals
   255  
   256  	// Create a struct callbackArgs on our stack.
   257  	MOVW	R12, (16+callbackArgs_index)(R13)	// callback index
   258  	MOVW	$(16+callbackArgs__size+4*9)(R13), R0
   259  	MOVW	R0, (16+callbackArgs_args)(R13)		// address of args vector
   260  	MOVW	$0, R0
   261  	MOVW	R0, (16+callbackArgs_result)(R13)	// result
   262  
   263  	// Prepare for entry to Go.
   264  	BL	runtime·load_g(SB)
   265  
   266  	// Call cgocallback, which will call callbackWrap(frame).
   267  	MOVW	$0, R0
   268  	MOVW	R0, 12(R13)	// context
   269  	MOVW	$16(R13), R1	// R1 = &callbackArgs{...}
   270  	MOVW	R1, 8(R13)	// frame (address of callbackArgs)
   271  	MOVW	$·callbackWrap(SB), R1
   272  	MOVW	R1, 4(R13)	// PC of function to call
   273  	BL	runtime·cgocallback(SB)
   274  
   275  	// Get callback result.
   276  	MOVW	(16+callbackArgs_result)(R13), R0
   277  
   278  	ADD	$(16 + callbackArgs__size), R13	// free locals
   279  	MOVM.IA.W (R13), [R4-R11, R12]	// pop {r4-r11, lr=>r12}
   280  	ADD	$(4*4), R13	// skip r0-r3
   281  	B	(R12)	// return
   282  
   283  // uint32 tstart_stdcall(M *newm);
   284  TEXT runtime·tstart_stdcall(SB),NOSPLIT|NOFRAME,$0
   285  	MOVM.DB.W [R4-R11, R14], (R13)		// push {r4-r11, lr}
   286  
   287  	MOVW	m_g0(R0), g
   288  	MOVW	R0, g_m(g)
   289  	BL	runtime·save_g(SB)
   290  
   291  	// Layout new m scheduler stack on os stack.
   292  	MOVW	R13, R0
   293  	MOVW	R0, g_stack+stack_hi(g)
   294  	SUB	$(64*1024), R0
   295  	MOVW	R0, (g_stack+stack_lo)(g)
   296  	MOVW	R0, g_stackguard0(g)
   297  	MOVW	R0, g_stackguard1(g)
   298  
   299  	BL	runtime·emptyfunc(SB)	// fault if stack check is wrong
   300  	BL	runtime·mstart(SB)
   301  
   302  	// Exit the thread.
   303  	MOVW	$0, R0
   304  	MOVM.IA.W (R13), [R4-R11, R15]		// pop {r4-r11, pc}
   305  
   306  // Runs on OS stack.
   307  // duration (in -100ns units) is in dt+0(FP).
   308  // g may be nil.
   309  TEXT runtime·usleep2(SB),NOSPLIT|NOFRAME,$0-4
   310  	MOVW	dt+0(FP), R3
   311  	MOVM.DB.W [R4, R14], (R13)	// push {r4, lr}
   312  	MOVW	R13, R4			// Save SP
   313  	SUB	$8, R13			// R13 = R13 - 8
   314  	BIC	$0x7, R13		// Align SP for ABI
   315  	MOVW	$0, R1			// R1 = FALSE (alertable)
   316  	MOVW	$-1, R0			// R0 = handle
   317  	MOVW	R13, R2			// R2 = pTime
   318  	MOVW	R3, 0(R2)		// time_lo
   319  	MOVW	R0, 4(R2)		// time_hi
   320  	MOVW	runtime·_NtWaitForSingleObject(SB), R3
   321  	BL	(R3)
   322  	MOVW	R4, R13			// Restore SP
   323  	MOVM.IA.W (R13), [R4, R15]	// pop {R4, pc}
   324  
   325  // Runs on OS stack.
   326  // duration (in -100ns units) is in dt+0(FP).
   327  // g is valid.
   328  // TODO: needs to be implemented properly.
   329  TEXT runtime·usleep2HighRes(SB),NOSPLIT|NOFRAME,$0-4
   330  	B	runtime·abort(SB)
   331  
   332  // Runs on OS stack.
   333  TEXT runtime·switchtothread(SB),NOSPLIT|NOFRAME,$0
   334  	MOVM.DB.W [R4, R14], (R13)  	// push {R4, lr}
   335  	MOVW    R13, R4
   336  	BIC	$0x7, R13		// alignment for ABI
   337  	MOVW	runtime·_SwitchToThread(SB), R0
   338  	BL	(R0)
   339  	MOVW 	R4, R13			// restore stack pointer
   340  	MOVM.IA.W (R13), [R4, R15]	// pop {R4, pc}
   341  
   342  TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
   343  	B	runtime·armPublicationBarrier(SB)
   344  
   345  // never called (this is a GOARM=7 platform)
   346  TEXT runtime·read_tls_fallback(SB),NOSPLIT|NOFRAME,$0
   347  	MOVW	$0xabcd, R0
   348  	MOVW	R0, (R0)
   349  	RET
   350  
   351  TEXT runtime·nanotime1(SB),NOSPLIT|NOFRAME,$0-8
   352  	MOVW	$0, R0
   353  	MOVB	runtime·useQPCTime(SB), R0
   354  	CMP	$0, R0
   355  	BNE	useQPC
   356  	MOVW	$_INTERRUPT_TIME, R3
   357  loop:
   358  	MOVW	time_hi1(R3), R1
   359  	DMB	MB_ISH
   360  	MOVW	time_lo(R3), R0
   361  	DMB	MB_ISH
   362  	MOVW	time_hi2(R3), R2
   363  	CMP	R1, R2
   364  	BNE	loop
   365  
   366  	// wintime = R1:R0, multiply by 100
   367  	MOVW	$100, R2
   368  	MULLU	R0, R2, (R4, R3)    // R4:R3 = R1:R0 * R2
   369  	MULA	R1, R2, R4, R4
   370  
   371  	// wintime*100 = R4:R3
   372  	MOVW	R3, ret_lo+0(FP)
   373  	MOVW	R4, ret_hi+4(FP)
   374  	RET
   375  useQPC:
   376  	B	runtime·nanotimeQPC(SB)		// tail call
   377  
   378  // save_g saves the g register (R10) into thread local memory
   379  // so that we can call externally compiled
   380  // ARM code that will overwrite those registers.
   381  // NOTE: runtime.gogo assumes that R1 is preserved by this function.
   382  //       runtime.mcall assumes this function only clobbers R0 and R11.
   383  // Returns with g in R0.
   384  // Save the value in the _TEB->TlsSlots array.
   385  // Effectively implements TlsSetValue().
   386  // tls_g stores the TLS slot allocated TlsAlloc().
   387  TEXT runtime·save_g(SB),NOSPLIT|NOFRAME,$0
   388  	MRC	15, 0, R0, C13, C0, 2
   389  	ADD	$0xe10, R0
   390  	MOVW 	$runtime·tls_g(SB), R11
   391  	MOVW	(R11), R11
   392  	MOVW	g, R11<<2(R0)
   393  	MOVW	g, R0	// preserve R0 across call to setg<>
   394  	RET
   395  
   396  // load_g loads the g register from thread-local memory,
   397  // for use after calling externally compiled
   398  // ARM code that overwrote those registers.
   399  // Get the value from the _TEB->TlsSlots array.
   400  // Effectively implements TlsGetValue().
   401  TEXT runtime·load_g(SB),NOSPLIT|NOFRAME,$0
   402  	MRC	15, 0, R0, C13, C0, 2
   403  	ADD	$0xe10, R0
   404  	MOVW 	$runtime·tls_g(SB), g
   405  	MOVW	(g), g
   406  	MOVW	g<<2(R0), g
   407  	RET
   408  
   409  // This is called from rt0_go, which runs on the system stack
   410  // using the initial stack allocated by the OS.
   411  // It calls back into standard C using the BL below.
   412  // To do that, the stack pointer must be 8-byte-aligned.
   413  TEXT runtime·_initcgo(SB),NOSPLIT|NOFRAME,$0
   414  	MOVM.DB.W [R4, R14], (R13)	// push {r4, lr}
   415  
   416  	// Ensure stack is 8-byte aligned before calling C code
   417  	MOVW	R13, R4
   418  	BIC	$0x7, R13
   419  
   420  	// Allocate a TLS slot to hold g across calls to external code
   421  	MOVW 	$runtime·_TlsAlloc(SB), R0
   422  	MOVW	(R0), R0
   423  	BL	(R0)
   424  
   425  	// Assert that slot is less than 64 so we can use _TEB->TlsSlots
   426  	CMP	$64, R0
   427  	MOVW	$runtime·abort(SB), R1
   428  	BL.GE	(R1)
   429  
   430  	// Save Slot into tls_g
   431  	MOVW 	$runtime·tls_g(SB), R1
   432  	MOVW	R0, (R1)
   433  
   434  	MOVW	R4, R13
   435  	MOVM.IA.W (R13), [R4, R15]	// pop {r4, pc}
   436  
   437  // Holds the TLS Slot, which was allocated by TlsAlloc()
   438  GLOBL runtime·tls_g+0(SB), NOPTR, $4