1/* SPDX-License-Identifier: BSD-2-Clause */
2/*
3 * Copyright (c) 2016, Linaro Limited
4 */
5
6#include <asm.S>
7
8#if defined(CFG_TA_GPROF_SUPPORT) || defined(CFG_FTRACE_SUPPORT)
9
10/*
11 * Convert return address to call site address by subtracting the size of one
12 * instruction.
13 */
14.macro adjust_pc rd, rn
15	sub	\rd, \rn, #4
16.endm
17
18/* Get instrumented function's pc value */
19.macro get_pc reg
20	ldr	\reg, [x29, #8]
21	sub	\reg, \reg, #4
22.endm
23
24/* Get instrumented function's lr address pointer */
25.macro get_lr_addr reg
26	ldr	\reg, [x29]
27	add	\reg, \reg, #8
28.endm
29
30/*
31 * void _mcount(void *return_address)
32 * @return_address: return address to instrumented function
33 *
34 * With the -pg option, the compiler inserts a call to _mcount into
35 * every function prologue.
36 * x0 contains the value of lr (x30) before the call, that is the return
37 * address to the caller of the instrumented function. The callee, i.e. the
38 * instrumented function itself, is determined from the current value of x30.
39 * Then we call:
40 *   void __mcount_internal(void *frompc, void *selfpc);
41 */
42FUNC _mcount, :
43	stp		x29, x30, [sp, #-16]!
44	mov		x29, sp
45#if defined(CFG_TA_GPROF_SUPPORT) && !defined(__KERNEL__)
46	adjust_pc	x0, x0
47	adjust_pc	x1, x30
48	bl		__mcount_internal
49#endif
50#ifdef CFG_FTRACE_SUPPORT
51	get_pc		x0
52	get_lr_addr	x1
53	bl		ftrace_enter
54#endif
55	ldp		x29, x30, [sp], #16
56	ret
57END_FUNC _mcount
58
59#ifdef CFG_FTRACE_SUPPORT
60FUNC __ftrace_return, :
61	/* Save return value regs */
62	sub		sp, sp, #64
63	stp		x0, x1, [sp]
64	stp		x2, x3, [sp, #16]
65	stp		x4, x5, [sp, #32]
66	stp		x6, x7, [sp, #48]
67
68	/* Get return address of parent func */
69	bl		ftrace_return
70	mov		x30, x0
71
72	/* Restore return value regs */
73	ldp		x0, x1, [sp]
74	ldp		x2, x3, [sp, #16]
75	ldp		x4, x5, [sp, #32]
76	ldp		x6, x7, [sp, #48]
77	add		sp, sp, #64
78
79	ret
80END_FUNC __ftrace_return
81#endif
82
83#endif /* CFG_TA_GPROF_SUPPORT || CFG_FTRACE_SUPPORT */
84
85BTI(emit_aarch64_feature_1_and     GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
86