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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "go_asm.h"
#include "textflag.h"
#include "funcdata.h"

// func armcas(ptr *int32, old, new int32) bool
// Atomically:
//	if *ptr == old {
//		*ptr = new
//		return true
//	} else {
//		return false
//	}
//
// To implement 路cas in sys_$GOOS_arm.s
// using the native instructions, use:
//
//	TEXT 路cas(SB),NOSPLIT,$0
//		B	路armcas(SB)
//
TEXT 路armcas(SB),NOSPLIT,$0-13
	MOVW	ptr+0(FP), R1
	MOVW	old+4(FP), R2
	MOVW	new+8(FP), R3
casl:
	LDREX	(R1), R0
	CMP	R0, R2
	BNE	casfail

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$0, R11
	BEQ	2(PC)
#endif
	DMB	MB_ISHST

	STREX	R3, (R1), R0
	CMP	$0, R0
	BNE	casl
	MOVW	$1, R0

#ifndef GOARM_7
	CMP	$0, R11
	BEQ	2(PC)
#endif
	DMB	MB_ISH

	MOVB	R0, ret+12(FP)
	RET
casfail:
	MOVW	$0, R0
	MOVB	R0, ret+12(FP)
	RET

// stubs

TEXT 路Loadp(SB),NOSPLIT|NOFRAME,$0-8
	B	路Load(SB)

TEXT 路LoadAcq(SB),NOSPLIT|NOFRAME,$0-8
	B	路Load(SB)

TEXT 路LoadAcquintptr(SB),NOSPLIT|NOFRAME,$0-8
	B 	路Load(SB)

TEXT 路Casint32(SB),NOSPLIT,$0-13
	B	路Cas(SB)

TEXT 路Casint64(SB),NOSPLIT,$-4-21
	B	路Cas64(SB)

TEXT 路Casuintptr(SB),NOSPLIT,$0-13
	B	路Cas(SB)

TEXT 路Casp1(SB),NOSPLIT,$0-13
	B	路Cas(SB)

TEXT 路CasRel(SB),NOSPLIT,$0-13
	B	路Cas(SB)

TEXT 路Loadint32(SB),NOSPLIT,$0-8
	B	路Load(SB)

TEXT 路Loadint64(SB),NOSPLIT,$-4-12
	B	路Load64(SB)

TEXT 路Loaduintptr(SB),NOSPLIT,$0-8
	B	路Load(SB)

TEXT 路Loaduint(SB),NOSPLIT,$0-8
	B	路Load(SB)

TEXT 路Storeint32(SB),NOSPLIT,$0-8
	B	路Store(SB)

TEXT 路Storeint64(SB),NOSPLIT,$0-12
	B	路Store64(SB)

TEXT 路Storeuintptr(SB),NOSPLIT,$0-8
	B	路Store(SB)

TEXT 路StorepNoWB(SB),NOSPLIT,$0-8
	B	路Store(SB)

TEXT 路StoreRel(SB),NOSPLIT,$0-8
	B	路Store(SB)

TEXT 路StoreReluintptr(SB),NOSPLIT,$0-8
	B	路Store(SB)

TEXT 路Xaddint32(SB),NOSPLIT,$0-12
	B	路Xadd(SB)

TEXT 路Xaddint64(SB),NOSPLIT,$-4-20
	B	路Xadd64(SB)

TEXT 路Xadduintptr(SB),NOSPLIT,$0-12
	B	路Xadd(SB)

TEXT 路Xchgint32(SB),NOSPLIT,$0-12
	B	路Xchg(SB)

TEXT 路Xchgint64(SB),NOSPLIT,$-4-20
	B	路Xchg64(SB)

// 64-bit atomics
// The native ARM implementations use LDREXD/STREXD, which are
// available on ARMv6k or later. We use them only on ARMv7.
// On older ARM, we use Go implementations which simulate 64-bit
// atomics with locks.
TEXT armCas64<>(SB),NOSPLIT,$0-21
	// addr is already in R1
	MOVW	old_lo+4(FP), R2
	MOVW	old_hi+8(FP), R3
	MOVW	new_lo+12(FP), R4
	MOVW	new_hi+16(FP), R5
cas64loop:
	LDREXD	(R1), R6	// loads R6 and R7
	CMP	R2, R6
	BNE	cas64fail
	CMP	R3, R7
	BNE	cas64fail

	DMB	MB_ISHST

	STREXD	R4, (R1), R0	// stores R4 and R5
	CMP	$0, R0
	BNE	cas64loop
	MOVW	$1, R0

	DMB	MB_ISH

	MOVBU	R0, swapped+20(FP)
	RET
cas64fail:
	MOVW	$0, R0
	MOVBU	R0, swapped+20(FP)
	RET

TEXT armXadd64<>(SB),NOSPLIT,$0-20
	// addr is already in R1
	MOVW	delta_lo+4(FP), R2
	MOVW	delta_hi+8(FP), R3

add64loop:
	LDREXD	(R1), R4	// loads R4 and R5
	ADD.S	R2, R4
	ADC	R3, R5

	DMB	MB_ISHST

	STREXD	R4, (R1), R0	// stores R4 and R5
	CMP	$0, R0
	BNE	add64loop

	DMB	MB_ISH

	MOVW	R4, new_lo+12(FP)
	MOVW	R5, new_hi+16(FP)
	RET

TEXT armXchg64<>(SB),NOSPLIT,$0-20
	// addr is already in R1
	MOVW	new_lo+4(FP), R2
	MOVW	new_hi+8(FP), R3

swap64loop:
	LDREXD	(R1), R4	// loads R4 and R5

	DMB	MB_ISHST

	STREXD	R2, (R1), R0	// stores R2 and R3
	CMP	$0, R0
	BNE	swap64loop

	DMB	MB_ISH

	MOVW	R4, old_lo+12(FP)
	MOVW	R5, old_hi+16(FP)
	RET

TEXT armLoad64<>(SB),NOSPLIT,$0-12
	// addr is already in R1

	LDREXD	(R1), R2	// loads R2 and R3
	DMB	MB_ISH

	MOVW	R2, val_lo+4(FP)
	MOVW	R3, val_hi+8(FP)
	RET

TEXT armStore64<>(SB),NOSPLIT,$0-12
	// addr is already in R1
	MOVW	val_lo+4(FP), R2
	MOVW	val_hi+8(FP), R3

store64loop:
	LDREXD	(R1), R4	// loads R4 and R5

	DMB	MB_ISHST

	STREXD	R2, (R1), R0	// stores R2 and R3
	CMP	$0, R0
	BNE	store64loop

	DMB	MB_ISH
	RET

TEXT armAnd8<>(SB),NOSPLIT,$0-5
	// addr is already in R1
	MOVB	v+4(FP), R2

and8loop:
	LDREXB	(R1), R6

	DMB	MB_ISHST

	AND 	R2, R6
	STREXB	R6, (R1), R0
	CMP	$0, R0
	BNE	and8loop

	DMB	MB_ISH

	RET

TEXT armOr8<>(SB),NOSPLIT,$0-5
	// addr is already in R1
	MOVB	v+4(FP), R2

or8loop:
	LDREXB	(R1), R6

	DMB	MB_ISHST

	ORR 	R2, R6
	STREXB	R6, (R1), R0
	CMP	$0, R0
	BNE	or8loop

	DMB	MB_ISH

	RET

TEXT armXchg8<>(SB),NOSPLIT,$0-9
	// addr is already in R1
	MOVB	v+4(FP), R2
xchg8loop:
	LDREXB	(R1), R6

	DMB	MB_ISHST

	STREXB	R2, (R1), R0
	CMP	$0, R0
	BNE	xchg8loop

	DMB	MB_ISH

	MOVB R6, ret+8(FP)
	RET

// The following functions all panic if their address argument isn't
// 8-byte aligned. Since we're calling back into Go code to do this,
// we have to cooperate with stack unwinding. In the normal case, the
// functions tail-call into the appropriate implementation, which
// means they must not open a frame. Hence, when they go down the
// panic path, at that point they push the LR to create a real frame
// (they don't need to pop it because panic won't return; however, we
// do need to set the SP delta back).

// Check if R1 is 8-byte aligned, panic if not.
// Clobbers R2.
#define CHECK_ALIGN \
	AND.S	$7, R1, R2 \
	BEQ 	4(PC) \
	MOVW.W	R14, -4(R13) /* prepare a real frame */ \
	BL	路panicUnaligned(SB) \
	ADD	$4, R13 /* compensate SP delta */

TEXT 路Cas64(SB),NOSPLIT,$-4-21
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1
	CHECK_ALIGN

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goCas64(SB)
#endif
	JMP	armCas64<>(SB)

TEXT 路Xadd64(SB),NOSPLIT,$-4-20
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1
	CHECK_ALIGN

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goXadd64(SB)
#endif
	JMP	armXadd64<>(SB)

TEXT 路Xchg64(SB),NOSPLIT,$-4-20
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1
	CHECK_ALIGN

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goXchg64(SB)
#endif
	JMP	armXchg64<>(SB)

TEXT 路Load64(SB),NOSPLIT,$-4-12
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1
	CHECK_ALIGN

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goLoad64(SB)
#endif
	JMP	armLoad64<>(SB)

TEXT 路Store64(SB),NOSPLIT,$-4-12
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1
	CHECK_ALIGN

#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goStore64(SB)
#endif
	JMP	armStore64<>(SB)

TEXT 路And8(SB),NOSPLIT,$-4-5
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1

// Uses STREXB/LDREXB that is armv6k or later.
// For simplicity we only enable this on armv7.
#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goAnd8(SB)
#endif
	JMP	armAnd8<>(SB)

TEXT 路Or8(SB),NOSPLIT,$-4-5
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1

// Uses STREXB/LDREXB that is armv6k or later.
// For simplicity we only enable this on armv7.
#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goOr8(SB)
#endif
	JMP	armOr8<>(SB)

TEXT 路Xchg8(SB),NOSPLIT,$-4-9
	NO_LOCAL_POINTERS
	MOVW	addr+0(FP), R1

	// Uses STREXB/LDREXB that is armv6k or later.
	// For simplicity we only enable this on armv7.
#ifndef GOARM_7
	MOVB	internal鈭昪pu路ARM+const_offsetARMHasV7Atomics(SB), R11
	CMP	$1, R11
	BEQ	2(PC)
	JMP	路goXchg8(SB)
#endif
	JMP	armXchg8<>(SB)