1/* Machine-specific calling sequence for `mcount' profiling function.  x86-64 version.
2   Copyright (C) 2002-2021 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
5   The GNU C Library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9
10   The GNU C Library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, see
17   <https://www.gnu.org/licenses/>.  */
18
19/* Assembly stub to invoke _mcount().  Compiler generated code calls
20   this stub after executing a function's prologue and without saving any
21   registers.  It is therefore necessary to preserve %rcx, %rdx, %rsi, %rdi,
22   %r8, %r9 as they may contain function arguments.  */
23
24#include <sysdep.h>
25
26ENTRY(_mcount)
27	/* Allocate space for 7 registers.  */
28	subq	$56,%rsp
29	cfi_adjust_cfa_offset (56)
30	movq	%rax,(%rsp)
31	cfi_rel_offset (rax, 0)
32	movq	%rcx,8(%rsp)
33	cfi_rel_offset (rcx, 8)
34	movq	%rdx,16(%rsp)
35	cfi_rel_offset (rdx, 16)
36	movq	%rsi,24(%rsp)
37	cfi_rel_offset (rsi, 24)
38	movq	%rdi,32(%rsp)
39	cfi_rel_offset (rdi, 32)
40	movq	%r8,40(%rsp)
41	cfi_rel_offset (r8, 40)
42	movq	%r9,48(%rsp)
43	cfi_rel_offset (r9, 48)
44
45	/* Setup parameter for __mcount_internal.  */
46	/* selfpc is the return address on the stack.  */
47	movq	56(%rsp),%rsi
48	/* Get frompc via the frame pointer.  */
49	movq	8(%rbp),%rdi
50	call C_SYMBOL_NAME(__mcount_internal)
51	/* Pop the saved registers.  Please note that `mcount' has no
52	   return value.  */
53	movq	48(%rsp),%r9
54	cfi_restore (r9)
55	movq	40(%rsp),%r8
56	cfi_restore (r8)
57	movq	32(%rsp),%rdi
58	cfi_restore (rdi)
59	movq	24(%rsp),%rsi
60	cfi_restore (rsi)
61	movq	16(%rsp),%rdx
62	cfi_restore (rdx)
63	movq	8(%rsp),%rcx
64	cfi_restore (rcx)
65	movq	(%rsp),%rax
66	cfi_restore (rax)
67	addq	$56,%rsp
68	cfi_adjust_cfa_offset (-56)
69	ret
70END(_mcount)
71
72#undef mcount
73weak_alias (_mcount, mcount)
74
75/* __fentry__ is different from _mcount in that it is called before
76   function prolog.  This means (among other things) that it has non-standard
77   stack alignment on entry: (%RSP & 0xF) == 0.  */
78
79ENTRY(__fentry__)
80	/* Allocate space for 7 registers
81	   (+8 bytes for proper stack alignment).  */
82	subq	$64,%rsp
83	cfi_adjust_cfa_offset (64)
84	movq	%rax,(%rsp)
85	cfi_rel_offset (rax, 0)
86	movq	%rcx,8(%rsp)
87	cfi_rel_offset (rcx, 8)
88	movq	%rdx,16(%rsp)
89	cfi_rel_offset (rdx, 16)
90	movq	%rsi,24(%rsp)
91	cfi_rel_offset (rsi, 24)
92	movq	%rdi,32(%rsp)
93	cfi_rel_offset (rdi, 32)
94	movq	%r8,40(%rsp)
95	cfi_rel_offset (r8, 40)
96	movq	%r9,48(%rsp)
97	cfi_rel_offset (r9, 48)
98
99	/* Setup parameter for __mcount_internal.  */
100	/* selfpc is the return address on the stack.  */
101	movq	64(%rsp),%rsi
102	/* caller is the return address above it */
103	movq	72(%rsp),%rdi
104	call C_SYMBOL_NAME(__mcount_internal)
105	/* Pop the saved registers.  Please note that `__fentry__' has no
106	   return value.  */
107	movq	48(%rsp),%r9
108	cfi_restore (r9)
109	movq	40(%rsp),%r8
110	cfi_restore (r8)
111	movq	32(%rsp),%rdi
112	cfi_restore (rdi)
113	movq	24(%rsp),%rsi
114	cfi_restore (rsi)
115	movq	16(%rsp),%rdx
116	cfi_restore (rdx)
117	movq	8(%rsp),%rcx
118	cfi_restore (rcx)
119	movq	(%rsp),%rax
120	cfi_restore (rax)
121	addq	$64,%rsp
122	cfi_adjust_cfa_offset (-64)
123	ret
124END(__fentry__)
125