blob: 529fa0c05f10974f8abdbf31cc4453a01e6c00bd [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Rogersb033c752011-07-20 12:22:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_INSTRUCTION_SET_H_
18#define ART_RUNTIME_INSTRUCTION_SET_H_
Ian Rogersb033c752011-07-20 12:22:35 -070019
Ian Rogersc8b306f2012-02-17 21:34:44 -080020#include <iosfwd>
Dave Allison70202782013-10-22 17:52:19 -070021#include <string>
22
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070023#include "base/logging.h" // Logging is required for FATAL in the helper functions.
Dave Allison70202782013-10-22 17:52:19 -070024#include "base/macros.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070025#include "base/value_object.h"
Andreas Gampe7cd26f32014-06-18 17:01:15 -070026#include "globals.h" // For KB.
Ian Rogersc8b306f2012-02-17 21:34:44 -080027
buzbeec143c552011-08-20 17:38:58 -070028namespace art {
29
30enum InstructionSet {
31 kNone,
32 kArm,
Serban Constantinescued8dd492014-02-11 14:15:10 +000033 kArm64,
buzbeec143c552011-08-20 17:38:58 -070034 kThumb2,
Shih-wei Liao6edfde42012-03-01 15:49:12 -080035 kX86,
Ian Rogersef7d42f2014-01-06 12:55:46 -080036 kX86_64,
Douglas Leung2db3e262014-06-25 16:02:55 -070037 kMips,
38 kMips64
buzbeec143c552011-08-20 17:38:58 -070039};
Ian Rogers8afeb852014-04-02 14:55:49 -070040std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs);
buzbeec143c552011-08-20 17:38:58 -070041
Andreas Gampe7cd26f32014-06-18 17:01:15 -070042#if defined(__arm__)
43static constexpr InstructionSet kRuntimeISA = kArm;
44#elif defined(__aarch64__)
45static constexpr InstructionSet kRuntimeISA = kArm64;
46#elif defined(__mips__)
47static constexpr InstructionSet kRuntimeISA = kMips;
48#elif defined(__i386__)
49static constexpr InstructionSet kRuntimeISA = kX86;
50#elif defined(__x86_64__)
51static constexpr InstructionSet kRuntimeISA = kX86_64;
52#else
53static constexpr InstructionSet kRuntimeISA = kNone;
54#endif
55
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070056// Architecture-specific pointer sizes
57static constexpr size_t kArmPointerSize = 4;
58static constexpr size_t kArm64PointerSize = 8;
59static constexpr size_t kMipsPointerSize = 4;
Ian Rogers02875c52014-09-25 17:36:39 -070060static constexpr size_t kMips64PointerSize = 8;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070061static constexpr size_t kX86PointerSize = 4;
62static constexpr size_t kX86_64PointerSize = 8;
63
64// ARM instruction alignment. ARM processors require code to be 4-byte aligned,
65// but ARM ELF requires 8..
66static constexpr size_t kArmAlignment = 8;
67
68// ARM64 instruction alignment. This is the recommended alignment for maximum performance.
69static constexpr size_t kArm64Alignment = 16;
70
71// MIPS instruction alignment. MIPS processors require code to be 4-byte aligned.
72// TODO: Can this be 4?
73static constexpr size_t kMipsAlignment = 8;
74
75// X86 instruction alignment. This is the recommended alignment for maximum performance.
76static constexpr size_t kX86Alignment = 16;
77
78
Brian Carlstrom2afe4942014-05-19 10:25:33 -070079const char* GetInstructionSetString(InstructionSet isa);
Andreas Gampe20c89302014-08-19 17:28:06 -070080
81// Note: Returns kNone when the string cannot be parsed to a known value.
Narayan Kamath11d9f062014-04-23 20:24:57 +010082InstructionSet GetInstructionSetFromString(const char* instruction_set);
83
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070084static inline size_t GetInstructionSetPointerSize(InstructionSet isa) {
85 switch (isa) {
86 case kArm:
87 // Fall-through.
88 case kThumb2:
89 return kArmPointerSize;
90 case kArm64:
91 return kArm64PointerSize;
92 case kX86:
93 return kX86PointerSize;
94 case kX86_64:
95 return kX86_64PointerSize;
96 case kMips:
97 return kMipsPointerSize;
Ian Rogers02875c52014-09-25 17:36:39 -070098 case kMips64:
99 return kMips64PointerSize;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700100 case kNone:
101 LOG(FATAL) << "ISA kNone does not have pointer size.";
102 return 0;
103 default:
104 LOG(FATAL) << "Unknown ISA " << isa;
105 return 0;
106 }
107}
108
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700109size_t GetInstructionSetAlignment(InstructionSet isa);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700110
111static inline bool Is64BitInstructionSet(InstructionSet isa) {
112 switch (isa) {
113 case kArm:
114 case kThumb2:
115 case kX86:
116 case kMips:
117 return false;
118
119 case kArm64:
120 case kX86_64:
Ian Rogers02875c52014-09-25 17:36:39 -0700121 case kMips64:
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700122 return true;
123
124 case kNone:
125 LOG(FATAL) << "ISA kNone does not have bit width.";
126 return 0;
127 default:
128 LOG(FATAL) << "Unknown ISA " << isa;
129 return 0;
130 }
131}
132
133static inline size_t GetBytesPerGprSpillLocation(InstructionSet isa) {
134 switch (isa) {
135 case kArm:
136 // Fall-through.
137 case kThumb2:
138 return 4;
139 case kArm64:
140 return 8;
141 case kX86:
142 return 4;
143 case kX86_64:
144 return 8;
145 case kMips:
146 return 4;
147 case kNone:
148 LOG(FATAL) << "ISA kNone does not have spills.";
149 return 0;
150 default:
151 LOG(FATAL) << "Unknown ISA " << isa;
152 return 0;
153 }
154}
155
156static inline size_t GetBytesPerFprSpillLocation(InstructionSet isa) {
157 switch (isa) {
158 case kArm:
159 // Fall-through.
160 case kThumb2:
161 return 4;
162 case kArm64:
163 return 8;
164 case kX86:
165 return 8;
166 case kX86_64:
167 return 8;
168 case kMips:
169 return 4;
170 case kNone:
171 LOG(FATAL) << "ISA kNone does not have spills.";
172 return 0;
173 default:
174 LOG(FATAL) << "Unknown ISA " << isa;
175 return 0;
176 }
177}
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700178
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700179size_t GetStackOverflowReservedBytes(InstructionSet isa);
Andreas Gampe91268c12014-04-03 17:50:24 -0700180
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700181class ArmInstructionSetFeatures;
Dave Allison70202782013-10-22 17:52:19 -0700182
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700183// Abstraction used to describe features of a different instruction sets.
184class InstructionSetFeatures {
Dave Allison70202782013-10-22 17:52:19 -0700185 public:
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700186 // Process a CPU variant string for the given ISA and create an InstructionSetFeatures.
187 static const InstructionSetFeatures* FromVariant(InstructionSet isa,
188 const std::string& variant,
189 std::string* error_msg);
Dave Allison70202782013-10-22 17:52:19 -0700190
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700191 // Parse a string of the form "div,lpae" and create an InstructionSetFeatures.
192 static const InstructionSetFeatures* FromFeatureString(InstructionSet isa,
193 const std::string& feature_list,
194 std::string* error_msg);
Ian Rogers8afeb852014-04-02 14:55:49 -0700195
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700196 // Parse a bitmap for the given isa and create an InstructionSetFeatures.
197 static const InstructionSetFeatures* FromBitmap(InstructionSet isa, uint32_t bitmap);
Dave Allison70202782013-10-22 17:52:19 -0700198
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700199 // Turn C pre-processor #defines into the equivalent instruction set features for kRuntimeISA.
200 static const InstructionSetFeatures* FromCppDefines();
Dave Allison70202782013-10-22 17:52:19 -0700201
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700202 // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
203 static const InstructionSetFeatures* FromCpuInfo();
Vladimir Marko674744e2014-04-24 15:18:26 +0100204
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700205 // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
206 // InstructionSetFeatures.
207 static const InstructionSetFeatures* FromHwcap();
Vladimir Marko674744e2014-04-24 15:18:26 +0100208
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700209 // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
210 // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
211 static const InstructionSetFeatures* FromAssembly();
Dave Allison70202782013-10-22 17:52:19 -0700212
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700213 // Are these features the same as the other given features?
214 virtual bool Equals(const InstructionSetFeatures* other) const = 0;
Dave Allison70202782013-10-22 17:52:19 -0700215
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700216 // Return the ISA these features relate to.
217 virtual InstructionSet GetInstructionSet() const = 0;
Dave Allison70202782013-10-22 17:52:19 -0700218
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700219 // Return a bitmap that represents the features. ISA specific.
220 virtual uint32_t AsBitmap() const = 0;
Dave Allison70202782013-10-22 17:52:19 -0700221
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700222 // Return a string of the form "div,lpae" or "none".
223 virtual std::string GetFeatureString() const = 0;
224
225 // Down cast this ArmInstructionFeatures.
226 const ArmInstructionSetFeatures* AsArmInstructionSetFeatures() const;
227
228 virtual ~InstructionSetFeatures() {}
229
230 protected:
231 InstructionSetFeatures() {}
Serban Constantinescu75b91132014-04-09 18:39:10 +0100232
Dave Allison70202782013-10-22 17:52:19 -0700233 private:
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700234 DISALLOW_COPY_AND_ASSIGN(InstructionSetFeatures);
235};
236std::ostream& operator<<(std::ostream& os, const InstructionSetFeatures& rhs);
237
238// Instruction set features relevant to the ARM architecture.
239class ArmInstructionSetFeatures FINAL : public InstructionSetFeatures {
240 public:
241 // Process a CPU variant string like "krait" or "cortex-a15" and create InstructionSetFeatures.
242 static const ArmInstructionSetFeatures* FromVariant(const std::string& variant,
243 std::string* error_msg);
244
245 // Parse a string of the form "div,lpae" and create an InstructionSetFeatures.
246 static const ArmInstructionSetFeatures* FromFeatureString(const std::string& feature_list,
247 std::string* error_msg);
248
249 // Parse a bitmap and create an InstructionSetFeatures.
250 static const ArmInstructionSetFeatures* FromBitmap(uint32_t bitmap);
251
252 // Turn C pre-processor #defines into the equivalent instruction set features.
253 static const ArmInstructionSetFeatures* FromCppDefines();
254
255 // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
256 static const ArmInstructionSetFeatures* FromCpuInfo();
257
258 // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
259 // InstructionSetFeatures.
260 static const ArmInstructionSetFeatures* FromHwcap();
261
262 // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
263 // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
264 static const ArmInstructionSetFeatures* FromAssembly();
265
266 bool Equals(const InstructionSetFeatures* other) const OVERRIDE;
267
268 InstructionSet GetInstructionSet() const OVERRIDE {
269 return kArm;
270 }
271
272 uint32_t AsBitmap() const OVERRIDE;
273
274 // Return a string of the form "div,lpae" or "none".
275 std::string GetFeatureString() const OVERRIDE;
276
277 // Is the divide instruction feature enabled?
278 bool HasDivideInstruction() const {
279 return has_div_;
280 }
281
282 // Is the Large Physical Address Extension (LPAE) instruction feature enabled? When true code can
283 // be used that assumes double register loads and stores (ldrd, strd) don't tear.
284 bool HasLpae() const {
285 return has_lpae_;
286 }
287
288 virtual ~ArmInstructionSetFeatures() {}
289
290 private:
291 ArmInstructionSetFeatures(bool has_lpae, bool has_div)
292 : has_lpae_(has_lpae), has_div_(has_div) {
293 }
294
295 // Bitmap positions for encoding features as a bitmap.
296 enum {
297 kDivBitfield = 1,
298 kLpaeBitfield = 2,
299 };
300
301 const bool has_lpae_;
302 const bool has_div_;
303
304 DISALLOW_COPY_AND_ASSIGN(ArmInstructionSetFeatures);
305};
306
307// A class used for instruction set features on ISAs that don't yet have any features defined.
308class UnknownInstructionSetFeatures FINAL : public InstructionSetFeatures {
309 public:
310 static const UnknownInstructionSetFeatures* Unknown(InstructionSet isa) {
311 return new UnknownInstructionSetFeatures(isa);
312 }
313
314 bool Equals(const InstructionSetFeatures* other) const OVERRIDE {
315 return isa_ == other->GetInstructionSet();
316 }
317
318 InstructionSet GetInstructionSet() const OVERRIDE {
319 return isa_;
320 }
321
322 uint32_t AsBitmap() const OVERRIDE {
323 return 0;
324 }
325
326 std::string GetFeatureString() const OVERRIDE {
327 return "none";
328 }
329
330 virtual ~UnknownInstructionSetFeatures() {}
331
332 private:
333 explicit UnknownInstructionSetFeatures(InstructionSet isa) : isa_(isa) {}
334
335 const InstructionSet isa_;
336
337 DISALLOW_COPY_AND_ASSIGN(UnknownInstructionSetFeatures);
Dave Allison70202782013-10-22 17:52:19 -0700338};
339
Andreas Gamped58342c2014-06-05 14:18:08 -0700340// The following definitions create return types for two word-sized entities that will be passed
341// in registers so that memory operations for the interface trampolines can be avoided. The entities
342// are the resolved method and the pointer to the code to be invoked.
343//
344// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
345// uint64_t or long long int.
346//
347// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two
348// size_t-sized values.
349//
350// We need two operations:
351//
352// 1) A flag value that signals failure. The assembly stubs expect the lower part to be "0".
353// GetTwoWordFailureValue() will return a value that has lower part == 0.
354//
355// 2) A value that combines two word-sized values.
356// GetTwoWordSuccessValue() constructs this.
357//
358// IMPORTANT: If you use this to transfer object pointers, it is your responsibility to ensure
359// that the object does not move or the value is updated. Simple use of this is NOT SAFE
360// when the garbage collector can move objects concurrently. Ensure that required locks
361// are held when using!
362
363#if defined(__i386__) || defined(__arm__) || defined(__mips__)
364typedef uint64_t TwoWordReturn;
365
366// Encodes method_ptr==nullptr and code_ptr==nullptr
367static inline constexpr TwoWordReturn GetTwoWordFailureValue() {
368 return 0;
369}
370
371// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
372static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
373 uint32_t lo32 = static_cast<uint32_t>(lo);
374 uint64_t hi64 = static_cast<uint64_t>(hi);
375 return ((hi64 << 32) | lo32);
376}
377
378#elif defined(__x86_64__) || defined(__aarch64__)
379struct TwoWordReturn {
380 uintptr_t lo;
381 uintptr_t hi;
382};
383
384// Encodes method_ptr==nullptr. Leaves random value in code pointer.
385static inline TwoWordReturn GetTwoWordFailureValue() {
386 TwoWordReturn ret;
387 ret.lo = 0;
388 return ret;
389}
390
391// Write values into their respective members.
392static inline TwoWordReturn GetTwoWordSuccessValue(uintptr_t hi, uintptr_t lo) {
393 TwoWordReturn ret;
394 ret.lo = lo;
395 ret.hi = hi;
396 return ret;
397}
398#else
399#error "Unsupported architecture"
400#endif
401
buzbeec143c552011-08-20 17:38:58 -0700402} // namespace art
403
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700404#endif // ART_RUNTIME_INSTRUCTION_SET_H_