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 | |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 23 | #include "base/logging.h" // Logging is required for FATAL in the helper functions. |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame^] | 25 | #include "globals.h" // For KB. |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 26 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 27 | namespace art { |
| 28 | |
| 29 | enum InstructionSet { |
| 30 | kNone, |
| 31 | kArm, |
Serban Constantinescu | ed8dd49 | 2014-02-11 14:15:10 +0000 | [diff] [blame] | 32 | kArm64, |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 33 | kThumb2, |
Shih-wei Liao | 6edfde4 | 2012-03-01 15:49:12 -0800 | [diff] [blame] | 34 | kX86, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 35 | kX86_64, |
Shih-wei Liao | 6edfde4 | 2012-03-01 15:49:12 -0800 | [diff] [blame] | 36 | kMips |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 37 | }; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 38 | std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 39 | |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame^] | 40 | #if defined(__arm__) |
| 41 | static constexpr InstructionSet kRuntimeISA = kArm; |
| 42 | #elif defined(__aarch64__) |
| 43 | static constexpr InstructionSet kRuntimeISA = kArm64; |
| 44 | #elif defined(__mips__) |
| 45 | static constexpr InstructionSet kRuntimeISA = kMips; |
| 46 | #elif defined(__i386__) |
| 47 | static constexpr InstructionSet kRuntimeISA = kX86; |
| 48 | #elif defined(__x86_64__) |
| 49 | static constexpr InstructionSet kRuntimeISA = kX86_64; |
| 50 | #else |
| 51 | static constexpr InstructionSet kRuntimeISA = kNone; |
| 52 | #endif |
| 53 | |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 54 | // Architecture-specific pointer sizes |
| 55 | static constexpr size_t kArmPointerSize = 4; |
| 56 | static constexpr size_t kArm64PointerSize = 8; |
| 57 | static constexpr size_t kMipsPointerSize = 4; |
| 58 | static constexpr size_t kX86PointerSize = 4; |
| 59 | static constexpr size_t kX86_64PointerSize = 8; |
| 60 | |
| 61 | // ARM instruction alignment. ARM processors require code to be 4-byte aligned, |
| 62 | // but ARM ELF requires 8.. |
| 63 | static constexpr size_t kArmAlignment = 8; |
| 64 | |
| 65 | // ARM64 instruction alignment. This is the recommended alignment for maximum performance. |
| 66 | static constexpr size_t kArm64Alignment = 16; |
| 67 | |
| 68 | // MIPS instruction alignment. MIPS processors require code to be 4-byte aligned. |
| 69 | // TODO: Can this be 4? |
| 70 | static constexpr size_t kMipsAlignment = 8; |
| 71 | |
| 72 | // X86 instruction alignment. This is the recommended alignment for maximum performance. |
| 73 | static constexpr size_t kX86Alignment = 16; |
| 74 | |
| 75 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 76 | const char* GetInstructionSetString(InstructionSet isa); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 77 | InstructionSet GetInstructionSetFromString(const char* instruction_set); |
| 78 | |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 79 | static inline size_t GetInstructionSetPointerSize(InstructionSet isa) { |
| 80 | switch (isa) { |
| 81 | case kArm: |
| 82 | // Fall-through. |
| 83 | case kThumb2: |
| 84 | return kArmPointerSize; |
| 85 | case kArm64: |
| 86 | return kArm64PointerSize; |
| 87 | case kX86: |
| 88 | return kX86PointerSize; |
| 89 | case kX86_64: |
| 90 | return kX86_64PointerSize; |
| 91 | case kMips: |
| 92 | return kMipsPointerSize; |
| 93 | case kNone: |
| 94 | LOG(FATAL) << "ISA kNone does not have pointer size."; |
| 95 | return 0; |
| 96 | default: |
| 97 | LOG(FATAL) << "Unknown ISA " << isa; |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 102 | size_t GetInstructionSetAlignment(InstructionSet isa); |
Alexei Zavjalov | 41c507a | 2014-05-15 16:02:46 +0700 | [diff] [blame] | 103 | |
| 104 | static inline bool Is64BitInstructionSet(InstructionSet isa) { |
| 105 | switch (isa) { |
| 106 | case kArm: |
| 107 | case kThumb2: |
| 108 | case kX86: |
| 109 | case kMips: |
| 110 | return false; |
| 111 | |
| 112 | case kArm64: |
| 113 | case kX86_64: |
| 114 | return true; |
| 115 | |
| 116 | case kNone: |
| 117 | LOG(FATAL) << "ISA kNone does not have bit width."; |
| 118 | return 0; |
| 119 | default: |
| 120 | LOG(FATAL) << "Unknown ISA " << isa; |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) { |
| 126 | switch (isa) { |
| 127 | case kArm: |
| 128 | // Fall-through. |
| 129 | case kThumb2: |
| 130 | return 4; |
| 131 | case kArm64: |
| 132 | return 8; |
| 133 | case kX86: |
| 134 | return 4; |
| 135 | case kX86_64: |
| 136 | return 8; |
| 137 | case kMips: |
| 138 | return 4; |
| 139 | case kNone: |
| 140 | LOG(FATAL) << "ISA kNone does not have spills."; |
| 141 | return 0; |
| 142 | default: |
| 143 | LOG(FATAL) << "Unknown ISA " << isa; |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) { |
| 149 | switch (isa) { |
| 150 | case kArm: |
| 151 | // Fall-through. |
| 152 | case kThumb2: |
| 153 | return 4; |
| 154 | case kArm64: |
| 155 | return 8; |
| 156 | case kX86: |
| 157 | return 8; |
| 158 | case kX86_64: |
| 159 | return 8; |
| 160 | case kMips: |
| 161 | return 4; |
| 162 | case kNone: |
| 163 | LOG(FATAL) << "ISA kNone does not have spills."; |
| 164 | return 0; |
| 165 | default: |
| 166 | LOG(FATAL) << "Unknown ISA " << isa; |
| 167 | return 0; |
| 168 | } |
| 169 | } |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 170 | |
Andreas Gampe | 7cd26f3 | 2014-06-18 17:01:15 -0700 | [diff] [blame^] | 171 | static constexpr size_t kDefaultStackOverflowReservedBytes = 16 * KB; |
| 172 | static constexpr size_t kArmStackOverflowReservedBytes = kDefaultStackOverflowReservedBytes; |
| 173 | static constexpr size_t kMipsStackOverflowReservedBytes = kDefaultStackOverflowReservedBytes; |
| 174 | |
| 175 | // TODO: shrink reserved space, in particular for 64bit. |
| 176 | |
| 177 | // Worst-case, we would need about 2.6x the amount of x86_64 for many more registers. |
| 178 | // But this one works rather well. |
| 179 | static constexpr size_t kArm64StackOverflowReservedBytes = 32 * KB; |
| 180 | // TODO: Bumped to workaround regression (http://b/14982147) Specifically to fix: |
| 181 | // test-art-host-run-test-interpreter-018-stack-overflow |
| 182 | // test-art-host-run-test-interpreter-107-int-math2 |
| 183 | static constexpr size_t kX86StackOverflowReservedBytes = 24 * KB; |
| 184 | static constexpr size_t kX86_64StackOverflowReservedBytes = 32 * KB; |
| 185 | |
| 186 | static constexpr size_t GetStackOverflowReservedBytes(InstructionSet isa) { |
| 187 | return (isa == kArm || isa == kThumb2) ? kArmStackOverflowReservedBytes : |
| 188 | isa == kArm64 ? kArm64StackOverflowReservedBytes : |
| 189 | isa == kMips ? kMipsStackOverflowReservedBytes : |
| 190 | isa == kX86 ? kX86StackOverflowReservedBytes : |
| 191 | isa == kX86_64 ? kX86_64StackOverflowReservedBytes : |
| 192 | isa == kNone ? (LOG(FATAL) << "kNone has no stack overflow size", 0) : |
| 193 | (LOG(FATAL) << "Unknown instruction set" << isa, 0); |
| 194 | } |
| 195 | |
| 196 | static constexpr size_t kRuntimeStackOverflowReservedBytes = |
| 197 | GetStackOverflowReservedBytes(kRuntimeISA); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 198 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 199 | enum InstructionFeatures { |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 200 | kHwDiv = 0x1, // Supports hardware divide. |
| 201 | kHwLpae = 0x2, // Supports Large Physical Address Extension. |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | // This is a bitmask of supported features per architecture. |
| 205 | class PACKED(4) InstructionSetFeatures { |
| 206 | public: |
| 207 | InstructionSetFeatures() : mask_(0) {} |
| 208 | explicit InstructionSetFeatures(uint32_t mask) : mask_(mask) {} |
| 209 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 210 | static InstructionSetFeatures GuessInstructionSetFeatures(); |
| 211 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 212 | bool HasDivideInstruction() const { |
| 213 | return (mask_ & kHwDiv) != 0; |
| 214 | } |
| 215 | |
| 216 | void SetHasDivideInstruction(bool v) { |
| 217 | mask_ = (mask_ & ~kHwDiv) | (v ? kHwDiv : 0); |
| 218 | } |
| 219 | |
Vladimir Marko | 674744e | 2014-04-24 15:18:26 +0100 | [diff] [blame] | 220 | bool HasLpae() const { |
| 221 | return (mask_ & kHwLpae) != 0; |
| 222 | } |
| 223 | |
| 224 | void SetHasLpae(bool v) { |
| 225 | mask_ = (mask_ & ~kHwLpae) | (v ? kHwLpae : 0); |
| 226 | } |
| 227 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 228 | std::string GetFeatureString() const; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 229 | |
| 230 | // Other features in here. |
| 231 | |
| 232 | bool operator==(const InstructionSetFeatures &peer) const { |
| 233 | return mask_ == peer.mask_; |
| 234 | } |
| 235 | |
| 236 | bool operator!=(const InstructionSetFeatures &peer) const { |
| 237 | return mask_ != peer.mask_; |
| 238 | } |
| 239 | |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 240 | bool operator<=(const InstructionSetFeatures &peer) const { |
| 241 | return (mask_ & peer.mask_) == mask_; |
| 242 | } |
| 243 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 244 | private: |
| 245 | uint32_t mask_; |
| 246 | }; |
| 247 | |
Andreas Gampe | d58342c | 2014-06-05 14:18:08 -0700 | [diff] [blame] | 248 | // The following definitions create return types for two word-sized entities that will be passed |
| 249 | // in registers so that memory operations for the interface trampolines can be avoided. The entities |
| 250 | // are the resolved method and the pointer to the code to be invoked. |
| 251 | // |
| 252 | // On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be |
| 253 | // uint64_t or long long int. |
| 254 | // |
| 255 | // On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two |
| 256 | // size_t-sized values. |
| 257 | // |
| 258 | // We need two operations: |
| 259 | // |
| 260 | // 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0". |
| 261 | // GetTwoWordFailureValue() will return a value that has lower part == 0. |
| 262 | // |
| 263 | // 2) A value that combines two word-sized values. |
| 264 | // GetTwoWordSuccessValue() constructs this. |
| 265 | // |
| 266 | // IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure |
| 267 | // that the object does not move or the value is updated. Simple use of this is NOT SAFE |
| 268 | // when the garbage collector can move objects concurrently. Ensure that required locks |
| 269 | // are held when using! |
| 270 | |
| 271 | #if defined(__i386__) || defined(__arm__) || defined(__mips__) |
| 272 | typedef uint64_t TwoWordReturn; |
| 273 | |
| 274 | // Encodes method_ptr==nullptr and code_ptr==nullptr |
| 275 | static inline constexpr TwoWordReturn GetTwoWordFailureValue() { |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | // Use the lower 32b for the method pointer and the upper 32b for the code pointer. |
| 280 | static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
| 281 | uint32_t lo32 = static_cast<uint32_t>(lo); |
| 282 | uint64_t hi64 = static_cast<uint64_t>(hi); |
| 283 | return ((hi64 << 32) | lo32); |
| 284 | } |
| 285 | |
| 286 | #elif defined(__x86_64__) || defined(__aarch64__) |
| 287 | struct TwoWordReturn { |
| 288 | uintptr_t lo; |
| 289 | uintptr_t hi; |
| 290 | }; |
| 291 | |
| 292 | // Encodes method_ptr==nullptr. Leaves random value in code pointer. |
| 293 | static inline TwoWordReturn GetTwoWordFailureValue() { |
| 294 | TwoWordReturn ret; |
| 295 | ret.lo = 0; |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | // Write values into their respective members. |
| 300 | static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) { |
| 301 | TwoWordReturn ret; |
| 302 | ret.lo = lo; |
| 303 | ret.hi = hi; |
| 304 | return ret; |
| 305 | } |
| 306 | #else |
| 307 | #error "Unsupported architecture" |
| 308 | #endif |
| 309 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 310 | } // namespace art |
| 311 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 312 | #endif // ART_RUNTIME_INSTRUCTION_SET_H_ |