summaryrefslogtreecommitdiffstats
path: root/src/runtime/race_arm64.s
blob: c818345852f6b07bd5d9f4d3465ca30297495d1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright 2018 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.

//go:build race

#include "go_asm.h"
#include "funcdata.h"
#include "textflag.h"
#include "tls_arm64.h"
#include "cgo/abi_arm64.h"

// The following thunks allow calling the gcc-compiled race runtime directly
// from Go code without going all the way through cgo.
// First, it's much faster (up to 50% speedup for real Go programs).
// Second, it eliminates race-related special cases from cgocall and scheduler.
// Third, in long-term it will allow to remove cyclic runtime/race dependency on cmd/go.

// A brief recap of the arm64 calling convention.
// Arguments are passed in R0...R7, the rest is on stack.
// Callee-saved registers are: R19...R28.
// Temporary registers are: R9...R15
// SP must be 16-byte aligned.

// When calling racecalladdr, R9 is the call target address.

// The race ctx, ThreadState *thr below, is passed in R0 and loaded in racecalladdr.

// Darwin may return unaligned thread pointer. Align it. (See tls_arm64.s)
// No-op on other OSes.
#ifdef TLS_darwin
#define TP_ALIGN	AND	$~7, R0
#else
#define TP_ALIGN
#endif

// Load g from TLS. (See tls_arm64.s)
#define load_g \
	MRS_TPIDR_R0 \
	TP_ALIGN \
	MOVD    runtime·tls_g(SB), R11 \
	MOVD    (R0)(R11), g

// func runtime·raceread(addr uintptr)
// Called from instrumented code.
// Defined as ABIInternal so as to avoid introducing a wrapper,
// which would make caller's PC ineffective.
TEXT	runtime·raceread<ABIInternal>(SB), NOSPLIT, $0-8
	MOVD	R0, R1	// addr
	MOVD	LR, R2
	// void __tsan_read(ThreadState *thr, void *addr, void *pc);
	MOVD	$__tsan_read(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·RaceRead(addr uintptr)
TEXT	runtime·RaceRead(SB), NOSPLIT, $0-8
	// This needs to be a tail call, because raceread reads caller pc.
	JMP	runtime·raceread(SB)

// func runtime·racereadpc(void *addr, void *callpc, void *pc)
TEXT	runtime·racereadpc(SB), NOSPLIT, $0-24
	MOVD	addr+0(FP), R1
	MOVD	callpc+8(FP), R2
	MOVD	pc+16(FP), R3
	// void __tsan_read_pc(ThreadState *thr, void *addr, void *callpc, void *pc);
	MOVD	$__tsan_read_pc(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·racewrite(addr uintptr)
// Called from instrumented code.
// Defined as ABIInternal so as to avoid introducing a wrapper,
// which would make caller's PC ineffective.
TEXT	runtime·racewrite<ABIInternal>(SB), NOSPLIT, $0-8
	MOVD	R0, R1	// addr
	MOVD	LR, R2
	// void __tsan_write(ThreadState *thr, void *addr, void *pc);
	MOVD	$__tsan_write(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·RaceWrite(addr uintptr)
TEXT	runtime·RaceWrite(SB), NOSPLIT, $0-8
	// This needs to be a tail call, because racewrite reads caller pc.
	JMP	runtime·racewrite(SB)

// func runtime·racewritepc(void *addr, void *callpc, void *pc)
TEXT	runtime·racewritepc(SB), NOSPLIT, $0-24
	MOVD	addr+0(FP), R1
	MOVD	callpc+8(FP), R2
	MOVD	pc+16(FP), R3
	// void __tsan_write_pc(ThreadState *thr, void *addr, void *callpc, void *pc);
	MOVD	$__tsan_write_pc(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·racereadrange(addr, size uintptr)
// Called from instrumented code.
// Defined as ABIInternal so as to avoid introducing a wrapper,
// which would make caller's PC ineffective.
TEXT	runtime·racereadrange<ABIInternal>(SB), NOSPLIT, $0-16
	MOVD	R1, R2	// size
	MOVD	R0, R1	// addr
	MOVD	LR, R3
	// void __tsan_read_range(ThreadState *thr, void *addr, uintptr size, void *pc);
	MOVD	$__tsan_read_range(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·RaceReadRange(addr, size uintptr)
TEXT	runtime·RaceReadRange(SB), NOSPLIT, $0-16
	// This needs to be a tail call, because racereadrange reads caller pc.
	JMP	runtime·racereadrange(SB)

// func runtime·racereadrangepc1(void *addr, uintptr sz, void *pc)
TEXT	runtime·racereadrangepc1(SB), NOSPLIT, $0-24
	MOVD	addr+0(FP), R1
	MOVD	size+8(FP), R2
	MOVD	pc+16(FP), R3
	ADD	$4, R3	// pc is function start, tsan wants return address.
	// void __tsan_read_range(ThreadState *thr, void *addr, uintptr size, void *pc);
	MOVD	$__tsan_read_range(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·racewriterange(addr, size uintptr)
// Called from instrumented code.
// Defined as ABIInternal so as to avoid introducing a wrapper,
// which would make caller's PC ineffective.
TEXT	runtime·racewriterange<ABIInternal>(SB), NOSPLIT, $0-16
	MOVD	R1, R2	// size
	MOVD	R0, R1	// addr
	MOVD	LR, R3
	// void __tsan_write_range(ThreadState *thr, void *addr, uintptr size, void *pc);
	MOVD	$__tsan_write_range(SB), R9
	JMP	racecalladdr<>(SB)

// func runtime·RaceWriteRange(addr, size uintptr)
TEXT	runtime·RaceWriteRange(SB), NOSPLIT, $0-16
	// This needs to be a tail call, because racewriterange reads caller pc.
	JMP	runtime·racewriterange(SB)

// func runtime·racewriterangepc1(void *addr, uintptr sz, void *pc)
TEXT	runtime·racewriterangepc1(SB), NOSPLIT, $0-24
	MOVD	addr+0(FP), R1
	MOVD	size+8(FP), R2
	MOVD	pc+16(FP), R3
	ADD	$4, R3	// pc is function start, tsan wants return address.
	// void __tsan_write_range(ThreadState *thr, void *addr, uintptr size, void *pc);
	MOVD	$__tsan_write_range(SB), R9
	JMP	racecalladdr<>(SB)

// If addr (R1) is out of range, do nothing.
// Otherwise, setup goroutine context and invoke racecall. Other arguments already set.
TEXT	racecalladdr<>(SB), NOSPLIT, $0-0
	load_g
	MOVD	g_racectx(g), R0
	// Check that addr is within [arenastart, arenaend) or within [racedatastart, racedataend).
	MOVD	runtime·racearenastart(SB), R10
	CMP	R10, R1
	BLT	data
	MOVD	runtime·racearenaend(SB), R10
	CMP	R10, R1
	BLT	call
data:
	MOVD	runtime·racedatastart(SB), R10
	CMP	R10, R1
	BLT	ret
	MOVD	runtime·racedataend(SB), R10
	CMP	R10, R1
	BGT	ret
call:
	JMP	racecall<>(SB)
ret:
	RET

// func runtime·racefuncenter(pc uintptr)
// Called from instrumented code.
TEXT	runtime·racefuncenter<ABIInternal>(SB), NOSPLIT, $0-8
	MOVD	R0, R9	// callpc
	JMP	racefuncenter<>(SB)

// Common code for racefuncenter
// R9 = caller's return address
TEXT	racefuncenter<>(SB), NOSPLIT, $0-0
	load_g
	MOVD	g_racectx(g), R0	// goroutine racectx
	MOVD	R9, R1
	// void __tsan_func_enter(ThreadState *thr, void *pc);
	MOVD	$__tsan_func_enter(SB), R9
	BL	racecall<>(SB)
	RET

// func runtime·racefuncexit()
// Called from instrumented code.
TEXT	runtime·racefuncexit<ABIInternal>(SB), NOSPLIT, $0-0
	load_g
	MOVD	g_racectx(g), R0	// race context
	// void __tsan_func_exit(ThreadState *thr);
	MOVD	$__tsan_func_exit(SB), R9
	JMP	racecall<>(SB)

// Atomic operations for sync/atomic package.
// R3 = addr of arguments passed to this function, it can
// be fetched at 40(RSP) in racecallatomic after two times BL
// R0, R1, R2 set in racecallatomic

// Load
TEXT	syncatomic·LoadInt32(SB), NOSPLIT, $0-12
	GO_ARGS
	MOVD	$__tsan_go_atomic32_load(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·LoadInt64(SB), NOSPLIT, $0-16
	GO_ARGS
	MOVD	$__tsan_go_atomic64_load(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·LoadUint32(SB), NOSPLIT, $0-12
	GO_ARGS
	JMP	syncatomic·LoadInt32(SB)

TEXT	syncatomic·LoadUint64(SB), NOSPLIT, $0-16
	GO_ARGS
	JMP	syncatomic·LoadInt64(SB)

TEXT	syncatomic·LoadUintptr(SB), NOSPLIT, $0-16
	GO_ARGS
	JMP	syncatomic·LoadInt64(SB)

TEXT	syncatomic·LoadPointer(SB), NOSPLIT, $0-16
	GO_ARGS
	JMP	syncatomic·LoadInt64(SB)

// Store
TEXT	syncatomic·StoreInt32(SB), NOSPLIT, $0-12
	GO_ARGS
	MOVD	$__tsan_go_atomic32_store(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·StoreInt64(SB), NOSPLIT, $0-16
	GO_ARGS
	MOVD	$__tsan_go_atomic64_store(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·StoreUint32(SB), NOSPLIT, $0-12
	GO_ARGS
	JMP	syncatomic·StoreInt32(SB)

TEXT	syncatomic·StoreUint64(SB), NOSPLIT, $0-16
	GO_ARGS
	JMP	syncatomic·StoreInt64(SB)

TEXT	syncatomic·StoreUintptr(SB), NOSPLIT, $0-16
	GO_ARGS
	JMP	syncatomic·StoreInt64(SB)

// Swap
TEXT	syncatomic·SwapInt32(SB), NOSPLIT, $0-20
	GO_ARGS
	MOVD	$__tsan_go_atomic32_exchange(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·SwapInt64(SB), NOSPLIT, $0-24
	GO_ARGS
	MOVD	$__tsan_go_atomic64_exchange(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·SwapUint32(SB), NOSPLIT, $0-20
	GO_ARGS
	JMP	syncatomic·SwapInt32(SB)

TEXT	syncatomic·SwapUint64(SB), NOSPLIT, $0-24
	GO_ARGS
	JMP	syncatomic·SwapInt64(SB)

TEXT	syncatomic·SwapUintptr(SB), NOSPLIT, $0-24
	GO_ARGS
	JMP	syncatomic·SwapInt64(SB)

// Add
TEXT	syncatomic·AddInt32(SB), NOSPLIT, $0-20
	GO_ARGS
	MOVD	$__tsan_go_atomic32_fetch_add(SB), R9
	BL	racecallatomic<>(SB)
	MOVW	add+8(FP), R0	// convert fetch_add to add_fetch
	MOVW	ret+16(FP), R1
	ADD	R0, R1, R0
	MOVW	R0, ret+16(FP)
	RET

TEXT	syncatomic·AddInt64(SB), NOSPLIT, $0-24
	GO_ARGS
	MOVD	$__tsan_go_atomic64_fetch_add(SB), R9
	BL	racecallatomic<>(SB)
	MOVD	add+8(FP), R0	// convert fetch_add to add_fetch
	MOVD	ret+16(FP), R1
	ADD	R0, R1, R0
	MOVD	R0, ret+16(FP)
	RET

TEXT	syncatomic·AddUint32(SB), NOSPLIT, $0-20
	GO_ARGS
	JMP	syncatomic·AddInt32(SB)

TEXT	syncatomic·AddUint64(SB), NOSPLIT, $0-24
	GO_ARGS
	JMP	syncatomic·AddInt64(SB)

TEXT	syncatomic·AddUintptr(SB), NOSPLIT, $0-24
	GO_ARGS
	JMP	syncatomic·AddInt64(SB)

// CompareAndSwap
TEXT	syncatomic·CompareAndSwapInt32(SB), NOSPLIT, $0-17
	GO_ARGS
	MOVD	$__tsan_go_atomic32_compare_exchange(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·CompareAndSwapInt64(SB), NOSPLIT, $0-25
	GO_ARGS
	MOVD	$__tsan_go_atomic64_compare_exchange(SB), R9
	BL	racecallatomic<>(SB)
	RET

TEXT	syncatomic·CompareAndSwapUint32(SB), NOSPLIT, $0-17
	GO_ARGS
	JMP	syncatomic·CompareAndSwapInt32(SB)

TEXT	syncatomic·CompareAndSwapUint64(SB), NOSPLIT, $0-25
	GO_ARGS
	JMP	syncatomic·CompareAndSwapInt64(SB)

TEXT	syncatomic·CompareAndSwapUintptr(SB), NOSPLIT, $0-25
	GO_ARGS
	JMP	syncatomic·CompareAndSwapInt64(SB)

// Generic atomic operation implementation.
// R9 = addr of target function
TEXT	racecallatomic<>(SB), NOSPLIT, $0
	// Set up these registers
	// R0 = *ThreadState
	// R1 = caller pc
	// R2 = pc
	// R3 = addr of incoming arg list

	// Trigger SIGSEGV early.
	MOVD	40(RSP), R3	// 1st arg is addr. after two times BL, get it at 40(RSP)
	MOVB	(R3), R13	// segv here if addr is bad
	// Check that addr is within [arenastart, arenaend) or within [racedatastart, racedataend).
	MOVD	runtime·racearenastart(SB), R10
	CMP	R10, R3
	BLT	racecallatomic_data
	MOVD	runtime·racearenaend(SB), R10
	CMP	R10, R3
	BLT	racecallatomic_ok
racecallatomic_data:
	MOVD	runtime·racedatastart(SB), R10
	CMP	R10, R3
	BLT	racecallatomic_ignore
	MOVD	runtime·racedataend(SB), R10
	CMP	R10, R3
	BGE	racecallatomic_ignore
racecallatomic_ok:
	// Addr is within the good range, call the atomic function.
	load_g
	MOVD	g_racectx(g), R0	// goroutine context
	MOVD	16(RSP), R1	// caller pc
	MOVD	R9, R2	// pc
	ADD	$40, RSP, R3
	JMP	racecall<>(SB)	// does not return
racecallatomic_ignore:
	// Addr is outside the good range.
	// Call __tsan_go_ignore_sync_begin to ignore synchronization during the atomic op.
	// An attempt to synchronize on the address would cause crash.
	MOVD	R9, R21	// remember the original function
	MOVD	$__tsan_go_ignore_sync_begin(SB), R9
	load_g
	MOVD	g_racectx(g), R0	// goroutine context
	BL	racecall<>(SB)
	MOVD	R21, R9	// restore the original function
	// Call the atomic function.
	// racecall will call LLVM race code which might clobber R28 (g)
	load_g
	MOVD	g_racectx(g), R0	// goroutine context
	MOVD	16(RSP), R1	// caller pc
	MOVD	R9, R2	// pc
	ADD	$40, RSP, R3	// arguments
	BL	racecall<>(SB)
	// Call __tsan_go_ignore_sync_end.
	MOVD	$__tsan_go_ignore_sync_end(SB), R9
	MOVD	g_racectx(g), R0	// goroutine context
	BL	racecall<>(SB)
	RET

// func runtime·racecall(void(*f)(...), ...)
// Calls C function f from race runtime and passes up to 4 arguments to it.
// The arguments are never heap-object-preserving pointers, so we pretend there are no arguments.
TEXT	runtime·racecall(SB), NOSPLIT, $0-0
	MOVD	fn+0(FP), R9
	MOVD	arg0+8(FP), R0
	MOVD	arg1+16(FP), R1
	MOVD	arg2+24(FP), R2
	MOVD	arg3+32(FP), R3
	JMP	racecall<>(SB)

// Switches SP to g0 stack and calls (R9). Arguments already set.
// Clobbers R19, R20.
TEXT	racecall<>(SB), NOSPLIT|NOFRAME, $0-0
	MOVD	g_m(g), R10
	// Switch to g0 stack.
	MOVD	RSP, R19	// callee-saved, preserved across the CALL
	MOVD	R30, R20	// callee-saved, preserved across the CALL
	MOVD	m_g0(R10), R11
	CMP	R11, g
	BEQ	call	// already on g0
	MOVD	(g_sched+gobuf_sp)(R11), R12
	MOVD	R12, RSP
call:
	BL	R9
	MOVD	R19, RSP
	JMP	(R20)

// C->Go callback thunk that allows to call runtime·racesymbolize from C code.
// Direct Go->C race call has only switched SP, finish g->g0 switch by setting correct g.
// The overall effect of Go->C->Go call chain is similar to that of mcall.
// R0 contains command code. R1 contains command-specific context.
// See racecallback for command codes.
TEXT	runtime·racecallbackthunk(SB), NOSPLIT|NOFRAME, $0
	// Handle command raceGetProcCmd (0) here.
	// First, code below assumes that we are on curg, while raceGetProcCmd
	// can be executed on g0. Second, it is called frequently, so will
	// benefit from this fast path.
	CBNZ	R0, rest
	MOVD	g, R13
#ifdef TLS_darwin
	MOVD	R27, R12 // save R27 a.k.a. REGTMP (callee-save in C). load_g clobbers it
#endif
	load_g
#ifdef TLS_darwin
	MOVD	R12, R27
#endif
	MOVD	g_m(g), R0
	MOVD	m_p(R0), R0
	MOVD	p_raceprocctx(R0), R0
	MOVD	R0, (R1)
	MOVD	R13, g
	JMP	(LR)
rest:
	// Save callee-saved registers (Go code won't respect that).
	// 8(RSP) and 16(RSP) are for args passed through racecallback
	SUB	$176, RSP
	MOVD	LR, 0(RSP)

	SAVE_R19_TO_R28(8*3)
	SAVE_F8_TO_F15(8*13)
	MOVD	R29, (8*21)(RSP)
	// Set g = g0.
	// load_g will clobber R0, Save R0
	MOVD	R0, R13
	load_g
	// restore R0
	MOVD	R13, R0
	MOVD	g_m(g), R13
	MOVD	m_g0(R13), R14
	CMP	R14, g
	BEQ	noswitch	// branch if already on g0
	MOVD	R14, g

	MOVD	R0, 8(RSP)	// func arg
	MOVD	R1, 16(RSP)	// func arg
	BL	runtime·racecallback(SB)

	// All registers are smashed after Go code, reload.
	MOVD	g_m(g), R13
	MOVD	m_curg(R13), g	// g = m->curg
ret:
	// Restore callee-saved registers.
	MOVD	0(RSP), LR
	MOVD	(8*21)(RSP), R29
	RESTORE_F8_TO_F15(8*13)
	RESTORE_R19_TO_R28(8*3)
	ADD	$176, RSP
	JMP	(LR)

noswitch:
	// already on g0
	MOVD	R0, 8(RSP)	// func arg
	MOVD	R1, 16(RSP)	// func arg
	BL	runtime·racecallback(SB)
	JMP	ret

#ifndef TLSG_IS_VARIABLE
// tls_g, g value for each thread in TLS
GLOBL runtime·tls_g+0(SB), TLSBSS+DUPOK, $8
#endif