Branch data Line data Source code
1 : : /* x86 stack sample register handling, pieces common to x86-64 and i386.
2 : : Copyright (C) 2025 Red Hat, Inc.
3 : : This file is part of elfutils.
4 : :
5 : : This file is free software; you can redistribute it and/or modify
6 : : it under the terms of either
7 : :
8 : : * the GNU Lesser General Public License as published by the Free
9 : : Software Foundation; either version 3 of the License, or (at
10 : : your option) any later version
11 : :
12 : : or
13 : :
14 : : * the GNU General Public License as published by the Free
15 : : Software Foundation; either version 2 of the License, or (at
16 : : your option) any later version
17 : :
18 : : or both in parallel, as here.
19 : :
20 : : elfutils is distributed in the hope that it will be useful, but
21 : : WITHOUT ANY WARRANTY; without even the implied warranty of
22 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 : : General Public License for more details.
24 : :
25 : : You should have received copies of the GNU General Public License and
26 : : the GNU Lesser General Public License along with this program. If
27 : : not, see <http://www.gnu.org/licenses/>. */
28 : :
29 : : static bool
30 : 0 : x86_sample_sp_pc (const Dwarf_Word *regs, uint32_t n_regs,
31 : : const int *regs_mapping, uint32_t n_regs_mapping,
32 : : Dwarf_Word *sp, uint sp_index /* into dwarf_regs */,
33 : : Dwarf_Word *pc, uint pc_index /* into dwarf_regs */)
34 : : {
35 [ # # ]: 0 : if (sp != NULL) *sp = 0;
36 [ # # ]: 0 : if (pc != NULL) *pc = 0;
37 : : #if !defined(__x86_64__)
38 : : (void)regs;
39 : : (void)n_regs;
40 : : (void)regs_mapping;
41 : : (void)n_regs_mapping;
42 : : return false;
43 : : #else /* __x86_64__ */
44 : : /* TODO: Register locations could be cached and rechecked on a
45 : : fastpath without needing to loop? */
46 : 0 : int j, need_sp = (sp != NULL), need_pc = (pc != NULL);
47 [ # # # # ]: 0 : for (j = 0; (need_sp || need_pc) && n_regs_mapping > (uint32_t)j; j++)
48 : : {
49 [ # # ]: 0 : if (n_regs < (uint32_t)j) break;
50 [ # # # # ]: 0 : if (need_sp && regs_mapping[j] == (int)sp_index)
51 : : {
52 : 0 : *sp = regs[j]; need_sp = false;
53 : : }
54 [ # # # # ]: 0 : if (need_pc && regs_mapping[j] == (int)pc_index)
55 : : {
56 : 0 : *pc = regs[j]; need_pc = false;
57 : : }
58 : : }
59 : 0 : return (!need_sp && !need_pc);
60 : : #endif
61 : : }
62 : :
63 : : static bool
64 : 0 : x86_sample_perf_regs_mapping (Ebl *ebl,
65 : : uint64_t perf_regs_mask, uint32_t abi,
66 : : const int **regs_mapping,
67 : : size_t *n_regs_mapping)
68 : : {
69 [ # # # # ]: 0 : if (perf_regs_mask != 0 && ebl->cached_perf_regs_mask == perf_regs_mask)
70 : : {
71 : 0 : *regs_mapping = ebl->cached_regs_mapping;
72 : 0 : *n_regs_mapping = ebl->cached_n_regs_mapping;
73 : 0 : return true;
74 : : }
75 : :
76 : : /* The following facts are needed to translate x86 registers correctly:
77 : : - perf register order seen in linux arch/x86/include/uapi/asm/perf_regs.h
78 : : The registers array is built in the same order as the enum!
79 : : (See the code in tools/perf/util/intel-pt.c intel_pt_add_gp_regs().)
80 : : - EBL PERF_FRAME_REGS_MASK specifies all registers except segment and
81 : : flags. However, regs_mask might be a different set of registers.
82 : : Again, regs_mask bits are in asm/perf_regs.h enum order.
83 : : - dwarf register order seen in elfutils backends/{x86_64,i386}_initreg.c
84 : : (matching pt_regs struct in linux arch/x86/include/asm/ptrace.h)
85 : : and it's a fairly different register order!
86 : :
87 : : For comparison, you can study codereview.qt-project.org/gitweb?p=qt-creator/perfparser.git;a=blob;f=app/perfregisterinfo.cpp;hb=HEAD
88 : : and follow the code which uses those tables of magic numbers.
89 : : But it's better to follow original sources of truth for this. */
90 : :
91 : 0 : bool is_abi32 = (abi == PERF_SAMPLE_REGS_ABI_32);
92 : :
93 : : /* Locations of dwarf_regs in the perf_event_x86_regs enum order,
94 : : not the regs[] array (which will include a subset of the regs): */
95 : 0 : static const int regs_i386[] = {0, 2, 3, 1, 7/*sp*/, 6, 4, 5, 8/*ip*/};
96 : 0 : static const int regs_x86_64[] = {0, 3, 2, 1, 4, 5, 6, 7/*sp*/,
97 : : 16/*r8 after flags+segment*/, 17, 18, 19, 20, 21, 22, 23,
98 : : 8/*ip*/};
99 [ # # ]: 0 : const int *dwarf_to_perf = is_abi32 ? regs_i386 : regs_x86_64;
100 : :
101 : : /* Count bits and allocate regs_mapping: */
102 : 0 : int j, k, kmax, count; uint64_t bit;
103 : 0 : for (k = 0, kmax = -1, count = 0, bit = 1;
104 [ # # ]: 0 : k < PERF_REG_X86_64_MAX; k++, bit <<= 1)
105 : : {
106 [ # # ]: 0 : if ((bit & perf_regs_mask)) {
107 : 0 : count++;
108 : 0 : kmax = k;
109 : : }
110 : : }
111 : 0 : ebl->cached_perf_regs_mask = perf_regs_mask;
112 : 0 : ebl->cached_regs_mapping = (int *)calloc (count, sizeof(int));
113 : 0 : ebl->cached_n_regs_mapping = count;
114 : :
115 : : /* Locations of perf_regs in the regs[] array, according to
116 : : perf_regs_mask: */
117 : 0 : int perf_to_regs[PERF_REG_X86_64_MAX];
118 : 0 : uint64_t expected_mask = is_abi32 ?
119 [ # # ]: 0 : PERF_FRAME_REGISTERS_I386 : PERF_FRAME_REGISTERS_X86_64;
120 [ # # ]: 0 : for (j = 0, k = 0, bit = 1; k <= kmax; k++, bit <<= 1)
121 : : {
122 [ # # # # ]: 0 : if ((bit & expected_mask) && (bit & perf_regs_mask))
123 : : {
124 : 0 : perf_to_regs[k] = j;
125 : 0 : j++;
126 : : }
127 : : else
128 : : {
129 : 0 : perf_to_regs[k] = -1;
130 : : }
131 : : }
132 [ # # ]: 0 : if (j > (int)ebl->cached_n_regs_mapping)
133 : : return false;
134 : :
135 : : /* Locations of perf_regs in the dwarf_regs array, according to
136 : : perf_regs_mask and perf_to_regs[]: */
137 [ # # ]: 0 : for (size_t i = 0; i < ebl->frame_nregs; i++)
138 : : {
139 : 0 : k = dwarf_to_perf[i];
140 : 0 : j = perf_to_regs[k];
141 [ # # ]: 0 : if (j < 0) continue;
142 : 0 : ebl->cached_regs_mapping[j] = i;
143 : : }
144 : :
145 : 0 : *regs_mapping = ebl->cached_regs_mapping;
146 : 0 : *n_regs_mapping = ebl->cached_n_regs_mapping;
147 : 0 : return true;
148 : : }
|