blob: 6e083e0775a9e49f76e83ce9208d9ab36d30addd [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#ifndef SIMPLE_PERF_PERF_REGS_H_
18#define SIMPLE_PERF_PERF_REGS_H_
19
Yabin Cui8ca8ae82015-07-13 21:41:05 -070020#if defined(USE_BIONIC_UAPI_HEADERS)
Yabin Cui8ca8ae82015-07-13 21:41:05 -070021#include <uapi/asm-arm/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000022#undef PERF_REG_EXTENDED_MASK
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020023#include <uapi/asm-x86/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000024#undef PERF_REG_EXTENDED_MASK
haocheng.zydb13e562022-10-12 22:57:39 +080025#include <uapi/asm-riscv/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000026#undef PERF_REG_EXTENDED_MASK
Yabin Cui8ca8ae82015-07-13 21:41:05 -070027#define perf_event_arm_regs perf_event_arm64_regs
28#include <uapi/asm-arm64/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000029#undef PERF_REG_EXTENDED_MASK
Yabin Cui8ca8ae82015-07-13 21:41:05 -070030#else
Yabin Cui76769e52015-07-13 12:23:54 -070031#include <asm-arm/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000032#undef PERF_REG_EXTENDED_MASK
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020033#include <asm-x86/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000034#undef PERF_REG_EXTENDED_MASK
haocheng.zydb13e562022-10-12 22:57:39 +080035#include <asm-riscv/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000036#undef PERF_REG_EXTENDED_MASK
Yabin Cui76769e52015-07-13 12:23:54 -070037#define perf_event_arm_regs perf_event_arm64_regs
38#include <asm-arm64/asm/perf_regs.h>
Christopher Ferris261c3e22022-12-13 01:22:22 +000039#undef PERF_REG_EXTENDED_MASK
Yabin Cui8ca8ae82015-07-13 21:41:05 -070040#endif
41
Yabin Cui76769e52015-07-13 12:23:54 -070042#include <stdint.h>
43#include <string>
Yabin Cui3c8c2132015-08-13 20:30:20 -070044#include <vector>
Yabin Cui76769e52015-07-13 12:23:54 -070045
Yabin Cui417df292016-11-16 12:26:13 -080046#include "perf_event.h"
47
Yabin Cuifaa7b922021-01-11 17:35:57 -080048namespace simpleperf {
49
Yabin Cui76769e52015-07-13 12:23:54 -070050enum ArchType {
51 ARCH_X86_32,
52 ARCH_X86_64,
53 ARCH_ARM,
54 ARCH_ARM64,
haocheng.zydb13e562022-10-12 22:57:39 +080055 ARCH_RISCV64,
Yabin Cuica7b9e72015-07-13 19:44:24 -070056 ARCH_UNSUPPORTED,
Yabin Cui76769e52015-07-13 12:23:54 -070057};
58
Yabin Cui38076912021-08-16 16:59:09 -070059constexpr ArchType GetTargetArch() {
Yabin Cui3c8c2132015-08-13 20:30:20 -070060#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;
haocheng.zydb13e562022-10-12 22:57:39 +080068#elif defined(__riscv)
69 return ARCH_RISCV64;
Yabin Cui3c8c2132015-08-13 20:30:20 -070070#else
71 return ARCH_UNSUPPORTED;
72#endif
73}
74
Yabin Cui48460892016-03-18 12:30:31 -070075ArchType GetArchType(const std::string& arch);
Yabin Cui257d5e62016-03-30 16:21:47 -070076ArchType GetArchForAbi(ArchType machine_arch, int abi);
77std::string GetArchString(ArchType arch);
Yabin Cui48460892016-03-18 12:30:31 -070078uint64_t GetSupportedRegMask(ArchType arch);
79std::string GetRegName(size_t regno, ArchType arch);
Yabin Cui76769e52015-07-13 12:23:54 -070080
Yabin Cui48460892016-03-18 12:30:31 -070081class ScopedCurrentArch {
82 public:
Chih-Hung Hsieh5674ed82016-07-12 11:35:16 -070083 explicit ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
Yabin Cui48460892016-03-18 12:30:31 -070084 current_arch = arch;
85 }
86 ~ScopedCurrentArch() {
87 current_arch = saved_arch;
88 }
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020089 static ArchType GetCurrentArch() { return current_arch; }
Yabin Cui76769e52015-07-13 12:23:54 -070090
Yabin Cui48460892016-03-18 12:30:31 -070091 private:
92 ArchType saved_arch;
93 static ArchType current_arch;
94};
Yabin Cui76769e52015-07-13 12:23:54 -070095
Yabin Cui3c8c2132015-08-13 20:30:20 -070096struct RegSet {
Yabin Cui6e75e1b2018-02-06 13:42:16 -080097 ArchType arch;
98 // For each setting bit in valid_mask, there is a valid reg value in data[].
Yabin Cui3c8c2132015-08-13 20:30:20 -070099 uint64_t valid_mask;
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800100 // Stores reg values. Values for invalid regs are 0.
Yabin Cui3c8c2132015-08-13 20:30:20 -0700101 uint64_t data[64];
Yabin Cui6e75e1b2018-02-06 13:42:16 -0800102
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;
Yabin Cui3c8c2132015-08-13 20:30:20 -0700108};
109
Yabin Cuifaa7b922021-01-11 17:35:57 -0800110} // namespace simpleperf
111
Yabin Cui76769e52015-07-13 12:23:54 -0700112#endif // SIMPLE_PERF_PERF_REGS_H_