Debian Codegraph @

js/src/jit/arm64/vixl/Cpu-vixl.h

raw
1 // Copyright 2014, VIXL authors
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #ifndef VIXL_CPU_AARCH64_H
28 #define VIXL_CPU_AARCH64_H
29
30 #include "jit/arm64/vixl/Cpu-Features-vixl.h"
31 #include "jit/arm64/vixl/Globals-vixl.h"
32
33 #include "jit/arm64/vixl/Instructions-vixl.h"
34
35 #ifndef VIXL_INCLUDE_TARGET_AARCH64
36 // The supporting .cc file is only compiled when the A64 target is selected.
37 // Throw an explicit error now to avoid a harder-to-debug linker error later.
38 //
39 // These helpers _could_ work on any AArch64 host, even when generating AArch32
40 // code, but we don't support this because the available features may differ
41 // between AArch32 and AArch64 on the same platform, so basing AArch32 code
42 // generation on aarch64::CPU features is probably broken.
43 #error cpu-aarch64.h requires VIXL_INCLUDE_TARGET_AARCH64 (scons target=a64).
44 #endif
45
46 namespace vixl {
47
48 // A CPU ID register, for use with CPUFeatures::kIDRegisterEmulation. Fields
49 // specific to each register are described in relevant subclasses.
50 class IDRegister {
51 protected:
52 explicit IDRegister(uint64_t value = 0) : value_(value) {}
53
54 class Field {
55 public:
56 enum Type { kUnsigned, kSigned };
57
58 explicit Field(int lsb, Type type = kUnsigned) : lsb_(lsb), type_(type) {}
59
60 static const int kMaxWidthInBits = 4;
61
62 int GetWidthInBits() const {
63 // All current ID fields have four bits.
64 return kMaxWidthInBits;
65 }
66 int GetLsb() const { return lsb_; }
67 int GetMsb() const { return lsb_ + GetWidthInBits() - 1; }
68 Type GetType() const { return type_; }
69
70 private:
71 int lsb_;
72 Type type_;
73 };
74
75 public:
76 // Extract the specified field, performing sign-extension for signed fields.
77 // This allows us to implement the 'value >= number' detection mechanism
78 // recommended by the Arm ARM, for both signed and unsigned fields.
79 int Get(Field field) const;
80
81 private:
82 uint64_t value_;
83 };
84
85 class AA64PFR0 : public IDRegister {
86 public:
87 explicit AA64PFR0(uint64_t value) : IDRegister(value) {}
88
89 CPUFeatures GetCPUFeatures() const;
90
91 private:
92 static const Field kFP;
93 static const Field kAdvSIMD;
94 static const Field kSVE;
95 static const Field kDIT;
96 };
97
98 class AA64PFR1 : public IDRegister {
99 public:
100 explicit AA64PFR1(uint64_t value) : IDRegister(value) {}
101
102 CPUFeatures GetCPUFeatures() const;
103
104 private:
105 static const Field kBT;
106 };
107
108 class AA64ISAR0 : public IDRegister {
109 public:
110 explicit AA64ISAR0(uint64_t value) : IDRegister(value) {}
111
112 CPUFeatures GetCPUFeatures() const;
113
114 private:
115 static const Field kAES;
116 static const Field kSHA1;
117 static const Field kSHA2;
118 static const Field kCRC32;
119 static const Field kAtomic;
120 static const Field kRDM;
121 static const Field kSHA3;
122 static const Field kSM3;
123 static const Field kSM4;
124 static const Field kDP;
125 static const Field kFHM;
126 static const Field kTS;
127 };
128
129 class AA64ISAR1 : public IDRegister {
130 public:
131 explicit AA64ISAR1(uint64_t value) : IDRegister(value) {}
132
133 CPUFeatures GetCPUFeatures() const;
134
135 private:
136 static const Field kDPB;
137 static const Field kAPA;
138 static const Field kAPI;
139 static const Field kJSCVT;
140 static const Field kFCMA;
141 static const Field kLRCPC;
142 static const Field kGPA;
143 static const Field kGPI;
144 static const Field kFRINTTS;
145 static const Field kSB;
146 static const Field kSPECRES;
147 };
148
149 class AA64MMFR1 : public IDRegister {
150 public:
151 explicit AA64MMFR1(uint64_t value) : IDRegister(value) {}
152
153 CPUFeatures GetCPUFeatures() const;
154
155 private:
156 static const Field kLO;
157 };
158
159 class CPU {
160 public:
161 // Initialise CPU support.
162 static void SetUp();
163
164 // Ensures the data at a given address and with a given size is the same for
165 // the I and D caches. I and D caches are not automatically coherent on ARM
166 // so this operation is required before any dynamically generated code can
167 // safely run.
168 static void EnsureIAndDCacheCoherency(void* address, size_t length);
169
170 // Flush the local instruction pipeline, forcing a reload of any instructions
171 // beyond this barrier from the icache.
172 static void FlushExecutionContext();
173
174 // Read and interpret the ID registers. This requires
175 // CPUFeatures::kIDRegisterEmulation, and therefore cannot be called on
176 // non-AArch64 platforms.
177 static CPUFeatures InferCPUFeaturesFromIDRegisters();
178
179 // Read and interpret CPUFeatures reported by the OS. Failed queries (or
180 // unsupported platforms) return an empty list. Note that this is
181 // indistinguishable from a successful query on a platform that advertises no
182 // features.
183 //
184 // Non-AArch64 hosts are considered to be unsupported platforms, and this
185 // function returns an empty list.
186 static CPUFeatures InferCPUFeaturesFromOS(
187 CPUFeatures::QueryIDRegistersOption option =
188 CPUFeatures::kQueryIDRegistersIfAvailable);
189
190 // Handle tagged pointers.
191 template <typename T>
192 static T SetPointerTag(T pointer, uint64_t tag) {
193 VIXL_ASSERT(IsUintN(kAddressTagWidth, tag));
194
195 // Use C-style casts to get static_cast behaviour for integral types (T),
196 // and reinterpret_cast behaviour for other types.
197
198 uint64_t raw = (uint64_t)pointer;
199 VIXL_STATIC_ASSERT(sizeof(pointer) == sizeof(raw));
200
201 raw = (raw & ~kAddressTagMask) | (tag << kAddressTagOffset);
202 return (T)raw;
203 }
204
205 template <typename T>
206 static uint64_t GetPointerTag(T pointer) {
207 // Use C-style casts to get static_cast behaviour for integral types (T),
208 // and reinterpret_cast behaviour for other types.
209
210 uint64_t raw = (uint64_t)pointer;
211 VIXL_STATIC_ASSERT(sizeof(pointer) == sizeof(raw));
212
213 return (raw & kAddressTagMask) >> kAddressTagOffset;
214 }
215
216 private:
217 #define VIXL_AARCH64_ID_REG_LIST(V) \
218 V(AA64PFR0) \
219 V(AA64PFR1) \
220 V(AA64ISAR0) \
221 V(AA64ISAR1) \
222 V(AA64MMFR1)
223
224 #define VIXL_READ_ID_REG(NAME) static NAME Read##NAME();
225 // On native AArch64 platforms, read the named CPU ID registers. These require
226 // CPUFeatures::kIDRegisterEmulation, and should not be called on non-AArch64
227 // platforms.
228 VIXL_AARCH64_ID_REG_LIST(VIXL_READ_ID_REG)
229 #undef VIXL_READ_ID_REG
230
231 // Return the content of the cache type register.
232 static uint32_t GetCacheType();
233
234 // I and D cache line size in bytes.
235 static unsigned icache_line_size_;
236 static unsigned dcache_line_size_;
237 };
238
239 } // namespace vixl
240
241 #endif // VIXL_CPU_AARCH64_H