Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/stringprintf.h> |
| 22 | #include <android-base/strings.h> |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 23 | |
Yabin Cui | 257d5e6 | 2016-03-30 16:21:47 -0700 | [diff] [blame] | 24 | #include "perf_event.h" |
| 25 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 26 | ArchType ScopedCurrentArch::current_arch = ARCH_UNSUPPORTED; |
| 27 | ArchType ScopedCurrentArch::current_arch32 = ARCH_UNSUPPORTED; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 28 | |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 29 | ArchType GetArchType(const std::string& arch) { |
Than McIntosh | 46546e9 | 2015-12-29 13:56:10 -0500 | [diff] [blame] | 30 | if (arch == "x86" || arch == "i686") { |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 31 | return ARCH_X86_32; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 32 | } else if (arch == "x86_64") { |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 33 | return ARCH_X86_64; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 34 | } else if (arch == "aarch64") { |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 35 | return ARCH_ARM64; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 36 | } else if (android::base::StartsWith(arch, "arm")) { |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 37 | // 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 Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 46 | return ARCH_ARM; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 47 | } |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 48 | LOG(ERROR) << "unsupported arch: " << arch; |
| 49 | return ARCH_UNSUPPORTED; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Yabin Cui | 257d5e6 | 2016-03-30 16:21:47 -0700 | [diff] [blame] | 52 | ArchType 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 | |
| 64 | std::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 Cui | 4b6720d | 2016-03-31 14:39:19 -0700 | [diff] [blame] | 80 | // If strict_check, must have arch1 == arch2. |
| 81 | // Otherwise, allow X86_32 with X86_64, ARM with ARM64. |
| 82 | bool 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 Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 99 | uint64_t GetSupportedRegMask(ArchType arch) { |
| 100 | switch (arch) { |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 101 | case ARCH_X86_32: |
Yabin Cui | 30ae2db | 2017-06-22 13:02:29 -0700 | [diff] [blame^] | 102 | return ((1ULL << PERF_REG_X86_32_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) & |
| 103 | ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 104 | case ARCH_X86_64: |
| 105 | return (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~(1ULL << PERF_REG_X86_DS) & |
| 106 | ~(1ULL << PERF_REG_X86_ES) & ~(1ULL << PERF_REG_X86_FS) & ~(1ULL << PERF_REG_X86_GS)); |
| 107 | case ARCH_ARM: |
| 108 | return ((1ULL << PERF_REG_ARM_MAX) - 1); |
| 109 | case ARCH_ARM64: |
| 110 | return ((1ULL << PERF_REG_ARM64_MAX) - 1); |
Yabin Cui | ca7b9e7 | 2015-07-13 19:44:24 -0700 | [diff] [blame] | 111 | default: |
| 112 | return 0; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static std::unordered_map<size_t, std::string> x86_reg_map = { |
| 118 | {PERF_REG_X86_AX, "ax"}, {PERF_REG_X86_BX, "bx"}, {PERF_REG_X86_CX, "cx"}, |
| 119 | {PERF_REG_X86_DX, "dx"}, {PERF_REG_X86_SI, "si"}, {PERF_REG_X86_DI, "di"}, |
| 120 | {PERF_REG_X86_BP, "bp"}, {PERF_REG_X86_SP, "sp"}, {PERF_REG_X86_IP, "ip"}, |
| 121 | {PERF_REG_X86_FLAGS, "flags"}, {PERF_REG_X86_CS, "cs"}, {PERF_REG_X86_SS, "ss"}, |
| 122 | {PERF_REG_X86_DS, "ds"}, {PERF_REG_X86_ES, "es"}, {PERF_REG_X86_FS, "fs"}, |
| 123 | {PERF_REG_X86_GS, "gs"}, |
| 124 | }; |
| 125 | |
| 126 | static std::unordered_map<size_t, std::string> arm_reg_map = { |
| 127 | {PERF_REG_ARM_FP, "fp"}, {PERF_REG_ARM_IP, "ip"}, {PERF_REG_ARM_SP, "sp"}, |
| 128 | {PERF_REG_ARM_LR, "lr"}, {PERF_REG_ARM_PC, "pc"}, |
| 129 | }; |
| 130 | |
| 131 | static std::unordered_map<size_t, std::string> arm64_reg_map = { |
| 132 | {PERF_REG_ARM64_LR, "lr"}, {PERF_REG_ARM64_SP, "sp"}, {PERF_REG_ARM64_PC, "pc"}, |
| 133 | }; |
| 134 | |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 135 | std::string GetRegName(size_t regno, ArchType arch) { |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 136 | // Cast regno to int type to avoid -Werror=type-limits. |
| 137 | int reg = static_cast<int>(regno); |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 138 | switch (arch) { |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 139 | case ARCH_X86_64: { |
| 140 | if (reg >= PERF_REG_X86_R8 && reg <= PERF_REG_X86_R15) { |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 141 | return android::base::StringPrintf("r%d", reg - PERF_REG_X86_R8 + 8); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 142 | } |
| 143 | } // go through |
| 144 | case ARCH_X86_32: { |
| 145 | auto it = x86_reg_map.find(reg); |
| 146 | CHECK(it != x86_reg_map.end()) << "unknown reg " << reg; |
| 147 | return it->second; |
| 148 | } |
| 149 | case ARCH_ARM: { |
| 150 | if (reg >= PERF_REG_ARM_R0 && reg <= PERF_REG_ARM_R10) { |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 151 | return android::base::StringPrintf("r%d", reg - PERF_REG_ARM_R0); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 152 | } |
| 153 | auto it = arm_reg_map.find(reg); |
| 154 | CHECK(it != arm_reg_map.end()) << "unknown reg " << reg; |
| 155 | return it->second; |
| 156 | } |
| 157 | case ARCH_ARM64: { |
| 158 | if (reg >= PERF_REG_ARM64_X0 && reg <= PERF_REG_ARM64_X29) { |
Yabin Cui | ffaa912 | 2016-01-15 15:25:48 -0800 | [diff] [blame] | 159 | return android::base::StringPrintf("r%d", reg - PERF_REG_ARM64_X0); |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 160 | } |
| 161 | auto it = arm64_reg_map.find(reg); |
| 162 | CHECK(it != arm64_reg_map.end()) << "unknown reg " << reg; |
| 163 | return it->second; |
| 164 | } |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 165 | default: |
Yabin Cui | ca7b9e7 | 2015-07-13 19:44:24 -0700 | [diff] [blame] | 166 | return "unknown"; |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 167 | } |
Yabin Cui | 76769e5 | 2015-07-13 12:23:54 -0700 | [diff] [blame] | 168 | } |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 169 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 170 | RegSet CreateRegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs) { |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 171 | RegSet regs; |
| 172 | regs.valid_mask = valid_mask; |
| 173 | for (int i = 0, j = 0; i < 64; ++i) { |
| 174 | if ((valid_mask >> i) & 1) { |
| 175 | regs.data[i] = valid_regs[j++]; |
| 176 | } |
| 177 | } |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 178 | if (ScopedCurrentArch::GetCurrentArch() == ARCH_ARM64 && |
| 179 | abi == PERF_SAMPLE_REGS_ABI_32) { |
| 180 | // The kernel dumps arm64 regs, but we need arm regs. So map arm64 |
| 181 | // regs into arm regs. |
| 182 | regs.data[PERF_REG_ARM_PC] = regs.data[PERF_REG_ARM64_PC]; |
| 183 | } |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 184 | return regs; |
| 185 | } |
| 186 | |
Yabin Cui | 417df29 | 2016-11-16 12:26:13 -0800 | [diff] [blame] | 187 | void SetIpReg(ArchType arch, uint64_t ip, RegSet* regs) { |
| 188 | int regno; |
| 189 | switch (arch) { |
| 190 | case ARCH_X86_64: |
| 191 | case ARCH_X86_32: |
| 192 | regno = PERF_REG_X86_IP; |
| 193 | break; |
| 194 | case ARCH_ARM: |
| 195 | regno = PERF_REG_ARM_PC; |
| 196 | break; |
| 197 | case ARCH_ARM64: |
| 198 | regno = PERF_REG_ARM64_PC; |
| 199 | break; |
| 200 | default: |
| 201 | return; |
| 202 | } |
| 203 | regs->valid_mask |= (1ULL << regno); |
| 204 | regs->data[regno] = ip; |
| 205 | } |
| 206 | |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 207 | bool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value) { |
| 208 | CHECK_LT(regno, 64U); |
| 209 | if ((regs.valid_mask >> regno) & 1) { |
| 210 | *value = regs.data[regno]; |
| 211 | return true; |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 216 | bool GetSpRegValue(const RegSet& regs, ArchType arch, uint64_t* value) { |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 217 | size_t regno; |
Yabin Cui | 4846089 | 2016-03-18 12:30:31 -0700 | [diff] [blame] | 218 | switch (arch) { |
| 219 | case ARCH_X86_32: |
| 220 | regno = PERF_REG_X86_SP; |
| 221 | break; |
| 222 | case ARCH_X86_64: |
| 223 | regno = PERF_REG_X86_SP; |
| 224 | break; |
| 225 | case ARCH_ARM: |
| 226 | regno = PERF_REG_ARM_SP; |
| 227 | break; |
| 228 | case ARCH_ARM64: |
| 229 | regno = PERF_REG_ARM64_SP; |
| 230 | break; |
| 231 | default: |
| 232 | return false; |
| 233 | } |
Yabin Cui | 3c8c213 | 2015-08-13 20:30:20 -0700 | [diff] [blame] | 234 | return GetRegValue(regs, regno, value); |
| 235 | } |