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
12TEXT runtime·asmstdcall_trampoline<ABIInternal>(SB),NOSPLIT,$0
13	B	runtime·asmstdcall(SB)
14
15// void runtime·asmstdcall(void *c);
16TEXT runtime·asmstdcall(SB),NOSPLIT|NOFRAME,$0
17	MOVM.DB.W [R4, R5, R14], (R13)	// push {r4, r5, lr}
18	MOVW	R0, R4			// put libcall * in r4
19	MOVW	R13, R5			// save stack pointer in r5
20
21	// SetLastError(0)
22	MOVW	$0, R0
23	MRC	15, 0, R1, C13, C0, 2
24	MOVW	R0, 0x34(R1)
25
26	MOVW	8(R4), R12	// libcall->args
27
28	// Do we have more than 4 arguments?
29	MOVW	4(R4), R0	// libcall->n
30	SUB.S	$4, R0, R2
31	BLE	loadregs
32
33	// Reserve stack space for remaining args
34	SUB	R2<<2, R13
35	BIC	$0x7, R13	// alignment for ABI
36
37	// R0: count of arguments
38	// R1:
39	// R2: loop counter, from 0 to (n-4)
40	// R3: scratch
41	// R4: pointer to libcall struct
42	// R12: libcall->args
43	MOVW	$0, R2
44stackargs:
45	ADD	$4, R2, R3		// r3 = args[4 + i]
46	MOVW	R3<<2(R12), R3
47	MOVW	R3, R2<<2(R13)		// stack[i] = r3
48
49	ADD	$1, R2			// i++
50	SUB	$4, R0, R3		// while (i < (n - 4))
51	CMP	R3, R2
52	BLT	stackargs
53
54loadregs:
55	CMP	$3, R0
56	MOVW.GT 12(R12), R3
57
58	CMP	$2, R0
59	MOVW.GT 8(R12), R2
60
61	CMP	$1, R0
62	MOVW.GT 4(R12), R1
63
64	CMP	$0, R0
65	MOVW.GT 0(R12), R0
66
67	BIC	$0x7, R13		// alignment for ABI
68	MOVW	0(R4), R12		// branch to libcall->fn
69	BL	(R12)
70
71	MOVW	R5, R13			// free stack space
72	MOVW	R0, 12(R4)		// save return value to libcall->r1
73	MOVW	R1, 16(R4)
74
75	// GetLastError
76	MRC	15, 0, R1, C13, C0, 2
77	MOVW	0x34(R1), R0
78	MOVW	R0, 20(R4)		// store in libcall->err
79
80	MOVM.IA.W (R13), [R4, R5, R15]
81
82TEXT runtime·getlasterror(SB),NOSPLIT,$0
83	MRC	15, 0, R0, C13, C0, 2
84	MOVW	0x34(R0), R0
85	MOVW	R0, ret+0(FP)
86	RET
87
88// Called by Windows as a Vectored Exception Handler (VEH).
89// R0 is pointer to struct containing
90// exception record and context pointers.
91// R1 is the kind of sigtramp function.
92// Return value of sigtrampgo is stored in R0.
93TEXT sigtramp<>(SB),NOSPLIT|NOFRAME,$0
94	MOVM.DB.W [R4-R11, R14], (R13)	// push {r4-r11, lr} (SP-=40)
95	SUB	$(16), R13		// reserve space for parameters/retval to go call
96
97	MOVW	R0, R6			// Save param0
98	MOVW	R1, R7			// Save param1
99	BL	runtime·load_g(SB)	// Clobbers R0
100
101	MOVW	$0, R4
102	MOVW	R4, 0(R13)	// No saved link register.
103	MOVW	R6, 4(R13)	// Move arg0 into position
104	MOVW	R7, 8(R13)	// Move arg1 into position
105	BL	runtime·sigtrampgo(SB)
106	MOVW	12(R13), R0	// Fetch return value from stack
107
108	ADD	$(16), R13			// free locals
109	MOVM.IA.W (R13), [R4-R11, R14]	// pop {r4-r11, lr}
110
111	B	(R14)				// return
112
113// Trampoline to resume execution from exception handler.
114// This is part of the control flow guard workaround.
115// It switches stacks and jumps to the continuation address.
116// R0 and R1 are set above at the end of sigtrampgo
117// in the context that starts executing at sigresume.
118TEXT runtime·sigresume(SB),NOSPLIT|NOFRAME,$0
119	// Important: do not smash LR,
120	// which is set to a live value when handling
121	// a signal by pushing a call to sigpanic onto the stack.
122	MOVW	R0, R13
123	B	(R1)
124
125TEXT runtime·exceptiontramp(SB),NOSPLIT|NOFRAME,$0
126	MOVW	$const_callbackVEH, R1
127	B	sigtramp<>(SB)
128
129TEXT runtime·firstcontinuetramp(SB),NOSPLIT|NOFRAME,$0
130	MOVW	$const_callbackFirstVCH, R1
131	B	sigtramp<>(SB)
132
133TEXT runtime·lastcontinuetramp(SB),NOSPLIT|NOFRAME,$0
134	MOVW	$const_callbackLastVCH, R1
135	B	sigtramp<>(SB)
136
137TEXT runtime·callbackasm1(SB),NOSPLIT|NOFRAME,$0
138	// On entry, the trampoline in zcallback_windows_arm.s left
139	// the callback index in R12 (which is volatile in the C ABI).
140
141	// Push callback register arguments r0-r3. We do this first so
142	// they're contiguous with stack arguments.
143	MOVM.DB.W [R0-R3], (R13)
144	// Push C callee-save registers r4-r11 and lr.
145	MOVM.DB.W [R4-R11, R14], (R13)
146	SUB	$(16 + callbackArgs__size), R13	// space for locals
147
148	// Create a struct callbackArgs on our stack.
149	MOVW	R12, (16+callbackArgs_index)(R13)	// callback index
150	MOVW	$(16+callbackArgs__size+4*9)(R13), R0
151	MOVW	R0, (16+callbackArgs_args)(R13)		// address of args vector
152	MOVW	$0, R0
153	MOVW	R0, (16+callbackArgs_result)(R13)	// result
154
155	// Prepare for entry to Go.
156	BL	runtime·load_g(SB)
157
158	// Call cgocallback, which will call callbackWrap(frame).
159	MOVW	$0, R0
160	MOVW	R0, 12(R13)	// context
161	MOVW	$16(R13), R1	// R1 = &callbackArgs{...}
162	MOVW	R1, 8(R13)	// frame (address of callbackArgs)
163	MOVWcallbackWrap(SB), R1
164	MOVW	R1, 4(R13)	// PC of function to call
165	BL	runtime·cgocallback(SB)
166
167	// Get callback result.
168	MOVW	(16+callbackArgs_result)(R13), R0
169
170	ADD	$(16 + callbackArgs__size), R13	// free locals
171	MOVM.IA.W (R13), [R4-R11, R12]	// pop {r4-r11, lr=>r12}
172	ADD	$(4*4), R13	// skip r0-r3
173	B	(R12)	// return
174
175// uint32 tstart_stdcall(M *newm);
176TEXT runtime·tstart_stdcall(SB),NOSPLIT|NOFRAME,$0
177	MOVM.DB.W [R4-R11, R14], (R13)		// push {r4-r11, lr}
178
179	MOVW	m_g0(R0), g
180	MOVW	R0, g_m(g)
181	BL	runtime·save_g(SB)
182
183	// Layout new m scheduler stack on os stack.
184	MOVW	R13, R0
185	MOVW	R0, g_stack+stack_hi(g)
186	SUB	$(64*1024), R0
187	MOVW	R0, (g_stack+stack_lo)(g)
188	MOVW	R0, g_stackguard0(g)
189	MOVW	R0, g_stackguard1(g)
190
191	BL	runtime·emptyfunc(SB)	// fault if stack check is wrong
192	BL	runtime·mstart(SB)
193
194	// Exit the thread.
195	MOVW	$0, R0
196	MOVM.IA.W (R13), [R4-R11, R15]		// pop {r4-r11, pc}
197
198TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
199	B	runtime·armPublicationBarrier(SB)
200
201// never called (this is a GOARM=7 platform)
202TEXT runtime·read_tls_fallback(SB),NOSPLIT,$0
203	MOVW	$0xabcd, R0
204	MOVW	R0, (R0)
205	RET
206
207TEXT runtime·nanotime1(SB),NOSPLIT,$0-8
208loop:
209	MOVW	time_hi1(R3), R1
210	DMB	MB_ISH
211	MOVW	time_lo(R3), R0
212	DMB	MB_ISH
213	MOVW	time_hi2(R3), R2
214	CMP	R1, R2
215	BNE	loop
216
217	// wintime = R1:R0, multiply by 100
218	MOVW	$100, R2
219	MULLU	R0, R2, (R4, R3)    // R4:R3 = R1:R0 * R2
220	MULA	R1, R2, R4, R4
221
222	// wintime*100 = R4:R3
223	MOVW	R3, ret_lo+0(FP)
224	MOVW	R4, ret_hi+4(FP)
225	RET
226
227// save_g saves the g register (R10) into thread local memory
228// so that we can call externally compiled
229// ARM code that will overwrite those registers.
230// NOTE: runtime.gogo assumes that R1 is preserved by this function.
231//       runtime.mcall assumes this function only clobbers R0 and R11.
232// Returns with g in R0.
233// Save the value in the _TEB->TlsSlots array.
234// Effectively implements TlsSetValue().
235// tls_g stores the TLS slot allocated TlsAlloc().
236TEXT runtime·save_g(SB),NOSPLIT,$0
237	MRC	15, 0, R0, C13, C0, 2
238	ADD	$0xe10, R0
239	MOVW 	$runtime·tls_g(SB), R11
240	MOVW	(R11), R11
241	MOVW	g, R11<<2(R0)
242	MOVW	g, R0	// preserve R0 across call to setg<>
243	RET
244
245// load_g loads the g register from thread-local memory,
246// for use after calling externally compiled
247// ARM code that overwrote those registers.
248// Get the value from the _TEB->TlsSlots array.
249// Effectively implements TlsGetValue().
250TEXT runtime·load_g(SB),NOSPLIT,$0
251	MRC	15, 0, R0, C13, C0, 2
252	ADD	$0xe10, R0
253	MOVW 	$runtime·tls_g(SB), g
254	MOVW	(g), g
255	MOVW	g<<2(R0), g
256	RET
257
258// This is called from rt0_go, which runs on the system stack
259// using the initial stack allocated by the OS.
260// It calls back into standard C using the BL below.
261// To do that, the stack pointer must be 8-byte-aligned.
262TEXT runtime·_initcgo(SB),NOSPLIT|NOFRAME,$0
263	MOVM.DB.W [R4, R14], (R13)	// push {r4, lr}
264
265	// Ensure stack is 8-byte aligned before calling C code
266	MOVW	R13, R4
267	BIC	$0x7, R13
268
269	// Allocate a TLS slot to hold g across calls to external code
270	MOVW 	$runtime·_TlsAlloc(SB), R0
271	MOVW	(R0), R0
272	BL	(R0)
273
274	// Assert that slot is less than 64 so we can use _TEB->TlsSlots
275	CMP	$64, R0
276	MOVW	$runtime·abort(SB), R1
277	BL.GE	(R1)
278
279	// Save Slot into tls_g
280	MOVW 	$runtime·tls_g(SB), R1
281	MOVW	R0, (R1)
282
283	MOVW	R4, R13
284	MOVM.IA.W (R13), [R4, R15]	// pop {r4, pc}
285
286// Holds the TLS Slot, which was allocated by TlsAlloc()
287GLOBL runtime·tls_g+0(SB), NOPTR, $4
288