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-x86/asm/perf_regs.h>
22 #include <uapi/asm-arm/asm/perf_regs.h>
23 #define perf_event_arm_regs perf_event_arm64_regs
24 #include <uapi/asm-arm64/asm/perf_regs.h>
25 #else
26 #include <asm-x86/asm/perf_regs.h>
27 #include <asm-arm/asm/perf_regs.h>
28 #define perf_event_arm_regs perf_event_arm64_regs
29 #include <asm-arm64/asm/perf_regs.h>
30 #endif
31
32 #include <stdint.h>
33 #include <string>
34 #include <vector>
35
36 enum ArchType {
37 ARCH_X86_32,
38 ARCH_X86_64,
39 ARCH_ARM,
40 ARCH_ARM64,
41 ARCH_UNSUPPORTED,
42 };
43
GetBuildArch()44 constexpr ArchType GetBuildArch() {
45 #if defined(__i386__)
46 return ARCH_X86_32;
47 #elif defined(__x86_64__)
48 return ARCH_X86_64;
49 #elif defined(__aarch64__)
50 return ARCH_ARM64;
51 #elif defined(__arm__)
52 return ARCH_ARM;
53 #else
54 return ARCH_UNSUPPORTED;
55 #endif
56 }
57
58 ArchType GetArchType(const std::string& arch);
59 uint64_t GetSupportedRegMask(ArchType arch);
60 std::string GetRegName(size_t regno, ArchType arch);
61
62 class ScopedCurrentArch {
63 public:
ScopedCurrentArch(ArchType arch)64 ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
65 current_arch = arch;
66 }
~ScopedCurrentArch()67 ~ScopedCurrentArch() {
68 current_arch = saved_arch;
69 }
GetCurrentArch()70 static ArchType GetCurrentArch() {
71 return current_arch;
72 }
73
74 private:
75 ArchType saved_arch;
76 static ArchType current_arch;
77 };
78
79 struct RegSet {
80 uint64_t valid_mask;
81 uint64_t data[64];
82 };
83
84 RegSet CreateRegSet(uint64_t valid_mask, const std::vector<uint64_t>& valid_regs);
85
86 bool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value);
87 bool GetSpRegValue(const RegSet& regs, ArchType arch, uint64_t* value);
88
89 #endif // SIMPLE_PERF_PERF_REGS_H_
90