176769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui/*
276769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * Copyright (C) 2015 The Android Open Source Project
376769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui *
476769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * Licensed under the Apache License, Version 2.0 (the "License");
576769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * you may not use this file except in compliance with the License.
676769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * You may obtain a copy of the License at
776769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui *
876769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui *      http://www.apache.org/licenses/LICENSE-2.0
976769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui *
1076769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * Unless required by applicable law or agreed to in writing, software
1176769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * distributed under the License is distributed on an "AS IS" BASIS,
1276769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1376769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * See the License for the specific language governing permissions and
1476769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui * limitations under the License.
1576769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui */
1676769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
1776769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#ifndef SIMPLE_PERF_PERF_REGS_H_
1876769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#define SIMPLE_PERF_PERF_REGS_H_
1976769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
208ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#if defined(USE_BIONIC_UAPI_HEADERS)
218ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#include <uapi/asm-x86/asm/perf_regs.h>
228ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#include <uapi/asm-arm/asm/perf_regs.h>
238ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#define perf_event_arm_regs perf_event_arm64_regs
248ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#include <uapi/asm-arm64/asm/perf_regs.h>
258ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#else
2676769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#include <asm-x86/asm/perf_regs.h>
2776769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#include <asm-arm/asm/perf_regs.h>
2876769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#define perf_event_arm_regs perf_event_arm64_regs
2976769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#include <asm-arm64/asm/perf_regs.h>
308ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui#endif
318ca8ae80383c481fcc5f5e5ce7377be9983822a2Yabin Cui
3276769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#include <stdint.h>
3376769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#include <string>
343c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#include <vector>
3576769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
3676769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cuienum ArchType {
3776769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui  ARCH_X86_32,
3876769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui  ARCH_X86_64,
3976769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui  ARCH_ARM,
4076769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui  ARCH_ARM64,
41ca7b9e71ad24c851de462bae455cc54542071adfYabin Cui  ARCH_UNSUPPORTED,
4276769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui};
4376769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
443c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cuiconstexpr ArchType GetBuildArch() {
453c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#if defined(__i386__)
463c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  return ARCH_X86_32;
473c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#elif defined(__x86_64__)
483c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  return ARCH_X86_64;
493c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#elif defined(__aarch64__)
503c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  return ARCH_ARM64;
513c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#elif defined(__arm__)
523c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  return ARCH_ARM;
533c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#else
543c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  return ARCH_UNSUPPORTED;
553c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui#endif
563c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui}
573c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui
58e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin CuiArchType GetArchType(const std::string& arch);
59e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cuiuint64_t GetSupportedRegMask(ArchType arch);
60e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cuistd::string GetRegName(size_t regno, ArchType arch);
6176769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
62e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cuiclass ScopedCurrentArch {
63e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui public:
64e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  ScopedCurrentArch(ArchType arch) : saved_arch(current_arch) {
65e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui    current_arch = arch;
66e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  }
67e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  ~ScopedCurrentArch() {
68e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui    current_arch = saved_arch;
69e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  }
70e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  static ArchType GetCurrentArch() {
71e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui    return current_arch;
72e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  }
7376769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
74e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui private:
75e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  ArchType saved_arch;
76e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui  static ArchType current_arch;
77e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cui};
7876769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui
793c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cuistruct RegSet {
803c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  uint64_t valid_mask;
813c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui  uint64_t data[64];
823c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui};
833c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui
843c8c21345478816dd0c70c096090b564c91bd9d2Yabin CuiRegSet CreateRegSet(uint64_t valid_mask, const std::vector<uint64_t>& valid_regs);
853c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui
863c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cuibool GetRegValue(const RegSet& regs, size_t regno, uint64_t* value);
87e435e7d8706ff234fdc69e9a9424fb2b08806658Yabin Cuibool GetSpRegValue(const RegSet& regs, ArchType arch, uint64_t* value);
883c8c21345478816dd0c70c096090b564c91bd9d2Yabin Cui
8976769e502d8f0ebf5d2c81b00246727fb0a59925Yabin Cui#endif  // SIMPLE_PERF_PERF_REGS_H_
90