blob: d27b62a9a64baa789a85a450478afe05a6dc29ce [file] [log] [blame]
Yabin Cui76769e52015-07-13 12:23:54 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "perf_regs.h"
18
19#include <unordered_map>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080020#include <android-base/logging.h>
21#include <android-base/stringprintf.h>
22#include <android-base/strings.h>
Yabin Cui76769e52015-07-13 12:23:54 -070023
Yabin Cui257d5e62016-03-30 16:21:47 -070024#include "perf_event.h"
25
Yabin Cui417df292016-11-16 12:26:13 -080026ArchType ScopedCurrentArch::current_arch = ARCH_UNSUPPORTED;
27ArchType ScopedCurrentArch::current_arch32 = ARCH_UNSUPPORTED;
Yabin Cui76769e52015-07-13 12:23:54 -070028
Yabin Cui48460892016-03-18 12:30:31 -070029ArchType GetArchType(const std::string& arch) {
Than McIntosh46546e92015-12-29 13:56:10 -050030 if (arch == "x86" || arch == "i686") {
Yabin Cui48460892016-03-18 12:30:31 -070031 return ARCH_X86_32;
Yabin Cui76769e52015-07-13 12:23:54 -070032 } else if (arch == "x86_64") {
Yabin Cui48460892016-03-18 12:30:31 -070033 return ARCH_X86_64;
Yabin Cui76769e52015-07-13 12:23:54 -070034 } else if (arch == "aarch64") {
Yabin Cui48460892016-03-18 12:30:31 -070035 return ARCH_ARM64;
Yabin Cui76769e52015-07-13 12:23:54 -070036 } else if (android::base::StartsWith(arch, "arm")) {
Yabin Cui417df292016-11-16 12:26:13 -080037 // If arch is "armv8l", it is likely that we are using a 32-bit simpleperf
38 // binary on a aarch64 device. In this case, the profiling environment is
39 // ARCH_ARM64, because the kernel is aarch64.
40 if (arch[3] == 'v') {
41 int version = atoi(&arch[4]);
42 if (version >= 8) {
43 return ARCH_ARM64;
44 }
45 }
Yabin Cui48460892016-03-18 12:30:31 -070046 return ARCH_ARM;
Yabin Cui76769e52015-07-13 12:23:54 -070047 }
Yabin Cui48460892016-03-18 12:30:31 -070048 LOG(ERROR) << "unsupported arch: " << arch;
49 return ARCH_UNSUPPORTED;
Yabin Cui76769e52015-07-13 12:23:54 -070050}
51
Yabin Cui257d5e62016-03-30 16:21:47 -070052ArchType GetArchForAbi(ArchType machine_arch, int abi) {
53 if (abi == PERF_SAMPLE_REGS_ABI_32) {
54 if (machine_arch == ARCH_X86_64) {
55 return ARCH_X86_32;
56 }
57 if (machine_arch == ARCH_ARM64) {
58 return ARCH_ARM;
59 }
60 }
61 return machine_arch;
62}
63
64std::string GetArchString(ArchType arch) {
65 switch (arch) {
66 case ARCH_X86_32:
67 return "x86";
68 case ARCH_X86_64:
69 return "x86_64";
70 case ARCH_ARM64:
71 return "arm64";
72 case ARCH_ARM:
73 return "arm";
74 default:
75 break;
76 }
77 return "unknown";
78}
79
Yabin Cui4b6720d2016-03-31 14:39:19 -070080// If strict_check, must have arch1 == arch2.
81// Otherwise, allow X86_32 with X86_64, ARM with ARM64.
82bool IsArchTheSame(ArchType arch1, ArchType arch2, bool strict_check) {
83 if (strict_check) {
84 return arch1 == arch2;
85 }
86 switch (arch1) {
87 case ARCH_X86_32:
88 case ARCH_X86_64:
89 return arch2 == ARCH_X86_32 || arch2 == ARCH_X86_64;
90 case ARCH_ARM64:
91 case ARCH_ARM:
92 return arch2 == ARCH_ARM64 || arch2 == ARCH_ARM;
93 default:
94 break;
95 }
96 return arch1 == arch2;
97}
98
Yabin Cui48460892016-03-18 12:30:31 -070099uint64_t GetSupportedRegMask(ArchType arch) {
100 switch (arch) {
Yabin Cui76769e52015-07-13 12:23:54 -0700101 case ARCH_X86_32:
102 return ((1ULL << PERF_REG_X86_32_MAX) - 1);
103 case ARCH_X86_64:
104 return (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) &
105 ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS));
106 case ARCH_ARM:
107 return ((1ULL << PERF_REG_ARM_MAX) - 1);
108 case ARCH_ARM64:
109 return ((1ULL << PERF_REG_ARM64_MAX) - 1);
Yabin Cuica7b9e72015-07-13 19:44:24 -0700110 default:
111 return 0;
Yabin Cui76769e52015-07-13 12:23:54 -0700112 }
113 return 0;
114}
115
116static std::unordered_map<size_t, std::string> x86_reg_map = {
117 {PERF_REG_X86_AX, "ax"}, {PERF_REG_X86_BX, "bx"}, {PERF_REG_X86_CX, "cx"},
118 {PERF_REG_X86_DX, "dx"}, {PERF_REG_X86_SI, "si"}, {PERF_REG_X86_DI, "di"},
119 {PERF_REG_X86_BP, "bp"}, {PERF_REG_X86_SP, "sp"}, {PERF_REG_X86_IP, "ip"},
120 {PERF_REG_X86_FLAGS, "flags"}, {PERF_REG_X86_CS, "cs"}, {PERF_REG_X86_SS, "ss"},
121 {PERF_REG_X86_DS, "ds"}, {PERF_REG_X86_ES, "es"}, {PERF_REG_X86_FS, "fs"},
122 {PERF_REG_X86_GS, "gs"},
123};
124
125static std::unordered_map<size_t, std::string> arm_reg_map = {
126 {PERF_REG_ARM_FP, "fp"}, {PERF_REG_ARM_IP, "ip"}, {PERF_REG_ARM_SP, "sp"},
127 {PERF_REG_ARM_LR, "lr"}, {PERF_REG_ARM_PC, "pc"},
128};
129
130static std::unordered_map<size_t, std::string> arm64_reg_map = {
131 {PERF_REG_ARM64_LR, "lr"}, {PERF_REG_ARM64_SP, "sp"}, {PERF_REG_ARM64_PC, "pc"},
132};
133
Yabin Cui48460892016-03-18 12:30:31 -0700134std::string GetRegName(size_t regno, ArchType arch) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800135 // Cast regno to int type to avoid -Werror=type-limits.
136 int reg = static_cast<int>(regno);
Yabin Cui48460892016-03-18 12:30:31 -0700137 switch (arch) {
Yabin Cui76769e52015-07-13 12:23:54 -0700138 case ARCH_X86_64: {
139 if (reg >= PERF_REG_X86_R8 && reg <= PERF_REG_X86_R15) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800140 return android::base::StringPrintf("r%d", reg - PERF_REG_X86_R8 + 8);
Yabin Cui76769e52015-07-13 12:23:54 -0700141 }
142 } // go through
143 case ARCH_X86_32: {
144 auto it = x86_reg_map.find(reg);
145 CHECK(it != x86_reg_map.end()) << "unknown reg " << reg;
146 return it->second;
147 }
148 case ARCH_ARM: {
149 if (reg >= PERF_REG_ARM_R0 && reg <= PERF_REG_ARM_R10) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800150 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM_R0);
Yabin Cui76769e52015-07-13 12:23:54 -0700151 }
152 auto it = arm_reg_map.find(reg);
153 CHECK(it != arm_reg_map.end()) << "unknown reg " << reg;
154 return it->second;
155 }
156 case ARCH_ARM64: {
157 if (reg >= PERF_REG_ARM64_X0 && reg <= PERF_REG_ARM64_X29) {
Yabin Cuiffaa9122016-01-15 15:25:48 -0800158 return android::base::StringPrintf("r%d", reg - PERF_REG_ARM64_X0);
Yabin Cui76769e52015-07-13 12:23:54 -0700159 }
160 auto it = arm64_reg_map.find(reg);
161 CHECK(it != arm64_reg_map.end()) << "unknown reg " << reg;
162 return it->second;
163 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700164 default:
Yabin Cuica7b9e72015-07-13 19:44:24 -0700165 return "unknown";
Yabin Cui76769e52015-07-13 12:23:54 -0700166 }
Yabin Cui76769e52015-07-13 12:23:54 -0700167}
Yabin Cui3c8c2132015-08-13 20:30:20 -0700168
Yabin Cui417df292016-11-16 12:26:13 -0800169RegSet CreateRegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs) {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700170 RegSet regs;
171 regs.valid_mask = valid_mask;
172 for (int i = 0, j = 0; i < 64; ++i) {
173 if ((valid_mask >> i) & 1) {
174 regs.data[i] = valid_regs[j++];
175 }
176 }
Yabin Cui417df292016-11-16 12:26:13 -0800177 if (ScopedCurrentArch::GetCurrentArch() == ARCH_ARM64 &&
178 abi == PERF_SAMPLE_REGS_ABI_32) {
179 // The kernel dumps arm64 regs, but we need arm regs. So map arm64
180 // regs into arm regs.
181 regs.data[PERF_REG_ARM_PC] = regs.data[PERF_REG_ARM64_PC];
182 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700183 return regs;
184}
185
Yabin Cui417df292016-11-16 12:26:13 -0800186void SetIpReg(ArchType arch, uint64_t ip, RegSet* regs) {
187 int regno;
188 switch (arch) {
189 case ARCH_X86_64:
190 case ARCH_X86_32:
191 regno = PERF_REG_X86_IP;
192 break;
193 case ARCH_ARM:
194 regno = PERF_REG_ARM_PC;
195 break;
196 case ARCH_ARM64:
197 regno = PERF_REG_ARM64_PC;
198 break;
199 default:
200 return;
201 }
202 regs->valid_mask |= (1ULL << regno);
203 regs->data[regno] = ip;
204}
205
Yabin Cui3c8c2132015-08-13 20:30:20 -0700206bool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value) {
207 CHECK_LT(regno, 64U);
208 if ((regs.valid_mask >> regno) & 1) {
209 *value = regs.data[regno];
210 return true;
211 }
212 return false;
213}
214
Yabin Cui48460892016-03-18 12:30:31 -0700215bool GetSpRegValue(const RegSet& regs, ArchType arch, uint64_t* value) {
Yabin Cui3c8c2132015-08-13 20:30:20 -0700216 size_t regno;
Yabin Cui48460892016-03-18 12:30:31 -0700217 switch (arch) {
218 case ARCH_X86_32:
219 regno = PERF_REG_X86_SP;
220 break;
221 case ARCH_X86_64:
222 regno = PERF_REG_X86_SP;
223 break;
224 case ARCH_ARM:
225 regno = PERF_REG_ARM_SP;
226 break;
227 case ARCH_ARM64:
228 regno = PERF_REG_ARM64_SP;
229 break;
230 default:
231 return false;
232 }
Yabin Cui3c8c2132015-08-13 20:30:20 -0700233 return GetRegValue(regs, regno, value);
234}