1 /* 2 * Copyright (C) 2023 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 ART_RUNTIME_ARCH_RISCV64_INSTRUCTION_SET_FEATURES_RISCV64_H_ 18 #define ART_RUNTIME_ARCH_RISCV64_INSTRUCTION_SET_FEATURES_RISCV64_H_ 19 20 #include "arch/instruction_set_features.h" 21 #include "base/macros.h" 22 23 namespace art HIDDEN { 24 25 class Riscv64InstructionSetFeatures; 26 using Riscv64FeaturesUniquePtr = std::unique_ptr<const Riscv64InstructionSetFeatures>; 27 28 // Instruction set features relevant to the RISCV64 architecture. 29 class Riscv64InstructionSetFeatures final : public InstructionSetFeatures { 30 public: 31 // Bitmap positions for encoding features as a bitmap. 32 enum { 33 kExtGeneric = (1 << 0), // G extension covers the basic set IMAFD 34 kExtCompressed = (1 << 1), // C extension adds compressed instructions 35 kExtVector = (1 << 2), // V extension adds vector instructions 36 kExtZba = (1 << 3), // Zba adds address generation bit-manipulation instructions 37 kExtZbb = (1 << 4), // Zbb adds basic bit-manipulation instructions 38 kExtZbs = (1 << 5), // Zbs adds single-bit bit-manipulation instructions 39 }; 40 41 static Riscv64FeaturesUniquePtr FromVariant(const std::string& variant, std::string* error_msg); 42 43 // Parse a bitmap and create an InstructionSetFeatures. 44 static Riscv64FeaturesUniquePtr FromBitmap(uint32_t bitmap); 45 46 // Turn C pre-processor #defines into the equivalent instruction set features. 47 static Riscv64FeaturesUniquePtr FromCppDefines(); 48 49 // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures. 50 static Riscv64FeaturesUniquePtr FromCpuInfo(); 51 52 // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce 53 // InstructionSetFeatures. 54 static Riscv64FeaturesUniquePtr FromHwcap(); 55 56 // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the 57 // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo. 58 static Riscv64FeaturesUniquePtr FromAssembly(); 59 60 // Use external cpu_features library. 61 static Riscv64FeaturesUniquePtr FromCpuFeatures(); 62 63 bool Equals(const InstructionSetFeatures* other) const override; 64 GetInstructionSet()65 InstructionSet GetInstructionSet() const override { return InstructionSet::kRiscv64; } 66 67 uint32_t AsBitmap() const override; 68 69 std::string GetFeatureString() const override; 70 HasCompressed()71 bool HasCompressed() const { return (bits_ & kExtCompressed) != 0; } 72 HasVector()73 bool HasVector() const { return (bits_ & kExtVector) != 0; } 74 HasZba()75 bool HasZba() const { return (bits_ & kExtZba) != 0; } 76 HasZbb()77 bool HasZbb() const { return (bits_ & kExtZbb) != 0; } 78 HasZbs()79 bool HasZbs() const { return (bits_ & kExtZbs) != 0; } 80 ~Riscv64InstructionSetFeatures()81 virtual ~Riscv64InstructionSetFeatures() {} 82 83 protected: 84 // If `features` is empty, this method doesn't add/remove anything from the 85 // existing set of features. 86 // If `features` is not empty, this method expects it to have exactly one value 87 // which is assumed to be a complete and valid features string. In this case, the 88 // new features will override the old ones. For example, if the existing set of 89 // features were `rv64gcv_zba_zbb_zbs` but `features` is `{"rv64gcv"}`, then the 90 // new features will not have the bits set for Zba, Zbb, or Zbs. 91 std::unique_ptr<const InstructionSetFeatures> AddFeaturesFromSplitString( 92 const std::vector<std::string>& features, std::string* error_msg) const override; 93 94 private: Riscv64InstructionSetFeatures(uint32_t bits)95 explicit Riscv64InstructionSetFeatures(uint32_t bits) : InstructionSetFeatures(), bits_(bits) {} 96 97 // Extension bitmap. 98 const uint32_t bits_; 99 100 DISALLOW_COPY_AND_ASSIGN(Riscv64InstructionSetFeatures); 101 }; 102 103 } // namespace art 104 105 #endif // ART_RUNTIME_ARCH_RISCV64_INSTRUCTION_SET_FEATURES_RISCV64_H_ 106