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 #ifndef SIMPLE_PERF_PERF_REGS_H_
18 #define SIMPLE_PERF_PERF_REGS_H_
19
20 #if defined(USE_BIONIC_UAPI_HEADERS)
21 #include <uapi/asm-arm/asm/perf_regs.h>
22 #undef PERF_REG_EXTENDED_MASK
23 #include <uapi/asm-x86/asm/perf_regs.h>
24 #undef PERF_REG_EXTENDED_MASK
25 #include <uapi/asm-riscv/asm/perf_regs.h>
26 #undef PERF_REG_EXTENDED_MASK
27 #define perf_event_arm_regs perf_event_arm64_regs
28 #include <uapi/asm-arm64/asm/perf_regs.h>
29 #undef PERF_REG_EXTENDED_MASK
30 #else
31 #include <asm-arm/asm/perf_regs.h>
32 #undef PERF_REG_EXTENDED_MASK
33 #include <asm-x86/asm/perf_regs.h>
34 #undef PERF_REG_EXTENDED_MASK
35 #include <asm-riscv/asm/perf_regs.h>
36 #undef PERF_REG_EXTENDED_MASK
37 #define perf_event_arm_regs perf_event_arm64_regs
38 #include <asm-arm64/asm/perf_regs.h>
39 #undef PERF_REG_EXTENDED_MASK
40 #endif
41
42 #include <stdint.h>
43 #include <string>
44 #include <vector>
45
46 #include "perf_event.h"
47
48 namespace simpleperf {
49
50 enum ArchType {
51 ARCH_X86_32,
52 ARCH_X86_64,
53 ARCH_ARM,
54 ARCH_ARM64,
55 ARCH_RISCV64,
56 ARCH_UNSUPPORTED,
57 };
58
GetTargetArch()59 constexpr ArchType GetTargetArch() {
60 #if defined(__i386__)
61 return ARCH_X86_32;
62 #elif defined(__x86_64__)
63 return ARCH_X86_64;
64 #elif defined(__aarch64__)
65 return ARCH_ARM64;
66 #elif defined(__arm__)
67 return ARCH_ARM;
68 #elif defined(__riscv)
69 return ARCH_RISCV64;
70 #else
71 return ARCH_UNSUPPORTED;
72 #endif
73 }
74
75 ArchType GetArchType(const std::string& arch);
76 ArchType GetArchForAbi(ArchType machine_arch, int abi);
77 std::string GetArchString(ArchType arch);
78 uint64_t GetSupportedRegMask(ArchType arch);
79 std::string GetRegName(size_t regno, ArchType arch);
80
81 class ScopedCurrentArch {
82 public:
ScopedCurrentArch(ArchType arch)83 explicit ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
84 current_arch = arch;
85 }
~ScopedCurrentArch()86 ~ScopedCurrentArch() {
87 current_arch = saved_arch;
88 }
GetCurrentArch()89 static ArchType GetCurrentArch() { return current_arch; }
90
91 private:
92 ArchType saved_arch;
93 static ArchType current_arch;
94 };
95
96 struct RegSet {
97 ArchType arch;
98 // For each setting bit in valid_mask, there is a valid reg value in data[].
99 uint64_t valid_mask;
100 // Stores reg values. Values for invalid regs are 0.
101 uint64_t data[64];
102
103 RegSet(int abi, uint64_t valid_mask, const uint64_t* valid_regs);
104
105 bool GetRegValue(size_t regno, uint64_t* value) const;
106 bool GetSpRegValue(uint64_t* value) const;
107 bool GetIpRegValue(uint64_t* value) const;
108 };
109
110 } // namespace simpleperf
111
112 #endif // SIMPLE_PERF_PERF_REGS_H_
113