Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_INSTRUCTION_SET_H_ |
| 18 | #define ART_RUNTIME_INSTRUCTION_SET_H_ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 19 | |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 20 | #include <iosfwd> |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | #include "base/macros.h" |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 24 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | |
| 27 | enum InstructionSet { |
| 28 | kNone, |
| 29 | kArm, |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 30 | kArm64, |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 31 | kThumb2, |
Shih-wei Liao | 6edfde4 | 2012-03-01 15:49:12 -0800 | [diff] [blame] | 32 | kX86, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 33 | kX86_64, |
Shih-wei Liao | 6edfde4 | 2012-03-01 15:49:12 -0800 | [diff] [blame] | 34 | kMips |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 35 | }; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 36 | std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 37 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 38 | const char* GetInstructionSetString(InstructionSet isa); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 39 | InstructionSet GetInstructionSetFromString(const char* instruction_set); |
| 40 | |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 41 | size_t GetInstructionSetPointerSize(InstructionSet isa); |
| 42 | size_t GetInstructionSetAlignment(InstructionSet isa); |
| 43 | bool Is64BitInstructionSet(InstructionSet isa); |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 44 | size_t GetBytesPerGprSpillLocation(InstructionSet isa); |
| 45 | size_t GetBytesPerFprSpillLocation(InstructionSet isa); |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 46 | |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 47 | #if defined(__arm__) |
| 48 | static constexpr InstructionSet kRuntimeISA = kArm; |
| 49 | #elif defined(__aarch64__) |
| 50 | static constexpr InstructionSet kRuntimeISA = kArm64; |
| 51 | #elif defined(__mips__) |
| 52 | static constexpr InstructionSet kRuntimeISA = kMips; |
| 53 | #elif defined(__i386__) |
| 54 | static constexpr InstructionSet kRuntimeISA = kX86; |
| 55 | #elif defined(__x86_64__) |
| 56 | static constexpr InstructionSet kRuntimeISA = kX86_64; |
| 57 | #else |
| 58 | static constexpr InstructionSet kRuntimeISA = kNone; |
| 59 | #endif |
| 60 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 61 | enum InstructionFeatures { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 62 | kHwDiv = 0x1, // Supports hardware divide. |
| 63 | kHwLpae = 0x2, // Supports Large Physical Address Extension. |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // This is a bitmask of supported features per architecture. |
| 67 | class PACKED(4) InstructionSetFeatures { |
| 68 | public: |
| 69 | InstructionSetFeatures() : mask_(0) {} |
| 70 | explicit InstructionSetFeatures(uint32_t mask) : mask_(mask) {} |
| 71 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 72 | static InstructionSetFeatures GuessInstructionSetFeatures(); |
| 73 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 74 | bool HasDivideInstruction() const { |
| 75 | return (mask_ & kHwDiv) != 0; |
| 76 | } |
| 77 | |
| 78 | void SetHasDivideInstruction(bool v) { |
| 79 | mask_ = (mask_ & ~kHwDiv) | (v ? kHwDiv : 0); |
| 80 | } |
| 81 | |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 82 | bool HasLpae() const { |
| 83 | return (mask_ & kHwLpae) != 0; |
| 84 | } |
| 85 | |
| 86 | void SetHasLpae(bool v) { |
| 87 | mask_ = (mask_ & ~kHwLpae) | (v ? kHwLpae : 0); |
| 88 | } |
| 89 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 90 | std::string GetFeatureString() const; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 91 | |
| 92 | // Other features in here. |
| 93 | |
| 94 | bool operator==(const InstructionSetFeatures &peer) const { |
| 95 | return mask_ == peer.mask_; |
| 96 | } |
| 97 | |
| 98 | bool operator!=(const InstructionSetFeatures &peer) const { |
| 99 | return mask_ != peer.mask_; |
| 100 | } |
| 101 | |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 102 | bool operator<=(const InstructionSetFeatures &peer) const { |
| 103 | return (mask_ & peer.mask_) == mask_; |
| 104 | } |
| 105 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 106 | private: |
| 107 | uint32_t mask_; |
| 108 | }; |
| 109 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame^] | 110 | // The following definitions create return types for two word-sized entities that will be passed |
| 111 | // in registers so that memory operations for the interface trampolines can be avoided. The entities |
| 112 | // are the resolved method and the pointer to the code to be invoked. |
| 113 | // |
| 114 | // On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be |
| 115 | // uint64_t or long long int. |
| 116 | // |
| 117 | // On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two |
| 118 | // size_t-sized values. |
| 119 | // |
| 120 | // We need two operations: |
| 121 | // |
| 122 | // 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0". |
| 123 | // GetTwoWordFailureValue() will return a value that has lower part == 0. |
| 124 | // |
| 125 | // 2) A value that combines two word-sized values. |
| 126 | // GetTwoWordSuccessValue() constructs this. |
| 127 | // |
| 128 | // IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure |
| 129 | // that the object does not move or the value is updated. Simple use of this is NOT SAFE |
| 130 | // when the garbage collector can move objects concurrently. Ensure that required locks |
| 131 | // are held when using! |
| 132 | |
| 133 | #if defined(__i386__) || defined(__arm__) || defined(__mips__) |
| 134 | typedef uint64_t TwoWordReturn; |
| 135 | |
| 136 | // Encodes method_ptr==nullptr and code_ptr==nullptr |
| 137 | static inline constexpr TwoWordReturn GetTwoWordFailureValue() { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | // Use the lower 32b for the method pointer and the upper 32b for the code pointer. |
| 142 | static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
| 143 | uint32_t lo32 = static_cast<uint32_t>(lo); |
| 144 | uint64_t hi64 = static_cast<uint64_t>(hi); |
| 145 | return ((hi64 << 32) | lo32); |
| 146 | } |
| 147 | |
| 148 | #elif defined(__x86_64__) || defined(__aarch64__) |
| 149 | struct TwoWordReturn { |
| 150 | uintptr_t lo; |
| 151 | uintptr_t hi; |
| 152 | }; |
| 153 | |
| 154 | // Encodes method_ptr==nullptr. Leaves random value in code pointer. |
| 155 | static inline TwoWordReturn GetTwoWordFailureValue() { |
| 156 | TwoWordReturn ret; |
| 157 | ret.lo = 0; |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | // Write values into their respective members. |
| 162 | static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
| 163 | TwoWordReturn ret; |
| 164 | ret.lo = lo; |
| 165 | ret.hi = hi; |
| 166 | return ret; |
| 167 | } |
| 168 | #else |
| 169 | #error "Unsupported architecture" |
| 170 | #endif |
| 171 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 172 | } // namespace art |
| 173 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 174 | #endif // ART_RUNTIME_INSTRUCTION_SET_H_ |