v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
cpu-arm64.cc
Go to the documentation of this file.
1// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// CPU specific code for arm independent of OS goes here.
6
7#if V8_TARGET_ARCH_ARM64
8
12
13#if V8_OS_DARWIN
14#include <libkern/OSCacheControl.h>
15#endif
16
17#if V8_OS_WIN
18#include <windows.h>
19#endif
20
21namespace v8 {
22namespace internal {
23
24class CacheLineSizes {
25 public:
26 CacheLineSizes() {
27#if !defined(V8_HOST_ARCH_ARM64) || defined(V8_OS_WIN) || defined(__APPLE__)
28 cache_type_register_ = 0;
29#else
30 // Copy the content of the cache type register to a core register.
31 __asm__ __volatile__("mrs %x[ctr], ctr_el0"
32 : [ctr] "=r"(cache_type_register_));
33#endif
34 }
35
36 uint32_t icache_line_size() const { return ExtractCacheLineSize(0); }
37 uint32_t dcache_line_size() const { return ExtractCacheLineSize(16); }
38
39 private:
40 uint32_t ExtractCacheLineSize(int cache_line_size_shift) const {
41 // The cache type register holds the size of cache lines in words as a
42 // power of two.
43 return 4 << ((cache_type_register_ >> cache_line_size_shift) & 0xF);
44 }
45
46 uint32_t cache_type_register_;
47};
48
49void CpuFeatures::FlushICache(void* address, size_t length) {
50#if defined(V8_HOST_ARCH_ARM64)
51#if defined(V8_OS_WIN)
52 ::FlushInstructionCache(GetCurrentProcess(), address, length);
53#elif defined(V8_OS_DARWIN)
54 sys_icache_invalidate(address, length);
55#elif defined(V8_OS_LINUX)
56 char* begin = reinterpret_cast<char*>(address);
57
58 __builtin___clear_cache(begin, begin + length);
59#else
60 // The code below assumes user space cache operations are allowed. The goal
61 // of this routine is to make sure the code generated is visible to the I
62 // side of the CPU.
63
64 uintptr_t start = reinterpret_cast<uintptr_t>(address);
65 // Sizes will be used to generate a mask big enough to cover a pointer.
66 CacheLineSizes sizes;
67 uintptr_t dsize = sizes.dcache_line_size();
68 uintptr_t isize = sizes.icache_line_size();
69 // Cache line sizes are always a power of 2.
70 DCHECK_EQ(CountSetBits(dsize, 64), 1);
71 DCHECK_EQ(CountSetBits(isize, 64), 1);
72 uintptr_t dstart = start & ~(dsize - 1);
73 uintptr_t istart = start & ~(isize - 1);
74 uintptr_t end = start + length;
75
76 __asm__ __volatile__(
77 // Clean every line of the D cache containing the target data.
78 "0: \n\t"
79 // dc : Data Cache maintenance
80 // c : Clean
81 // i : Invalidate
82 // va : by (Virtual) Address
83 // c : to the point of Coherency
84 // See ARM DDI 0406B page B2-12 for more information.
85 // We would prefer to use "cvau" (clean to the point of unification) here
86 // but we use "civac" to work around Cortex-A53 errata 819472, 826319,
87 // 827319 and 824069.
88 "dc civac, %[dline] \n\t"
89 "add %[dline], %[dline], %[dsize] \n\t"
90 "cmp %[dline], %[end] \n\t"
91 "b.lt 0b \n\t"
92 // Barrier to make sure the effect of the code above is visible to the
93 // rest of the world. dsb : Data Synchronisation Barrier
94 // ish : Inner SHareable domain
95 // The point of unification for an Inner Shareable shareability domain is
96 // the point by which the instruction and data caches of all the
97 // processors in that Inner Shareable shareability domain are guaranteed
98 // to see the same copy of a memory location. See ARM DDI 0406B page
99 // B2-12 for more information.
100 "dsb ish \n\t"
101 // Invalidate every line of the I cache containing the target data.
102 "1: \n\t"
103 // ic : instruction cache maintenance
104 // i : invalidate
105 // va : by address
106 // u : to the point of unification
107 "ic ivau, %[iline] \n\t"
108 "add %[iline], %[iline], %[isize] \n\t"
109 "cmp %[iline], %[end] \n\t"
110 "b.lt 1b \n\t"
111 // Barrier to make sure the effect of the code above is visible to the
112 // rest of the world.
113 "dsb ish \n\t"
114 // Barrier to ensure any prefetching which happened before this code is
115 // discarded.
116 // isb : Instruction Synchronisation Barrier
117 "isb \n\t"
118 : [dline] "+r"(dstart), [iline] "+r"(istart)
119 : [dsize] "r"(dsize), [isize] "r"(isize), [end] "r"(end)
120 // This code does not write to memory but without the dependency gcc might
121 // move this code before the code is generated.
122 : "cc", "memory");
123#endif // V8_OS_WIN
124#endif // V8_HOST_ARCH_ARM64
125}
126
127} // namespace internal
128} // namespace v8
129
130#endif // V8_TARGET_ARCH_ARM64
friend void V8_EXPORT_PRIVATE FlushInstructionCache(void *, size_t)
static void FlushICache(void *start, size_t size)
Definition cpu-riscv.cc:15
int start
int end
Node::Uses::const_iterator begin(const Node::Uses &uses)
Definition node.h:708
V8_EXPORT_PRIVATE int CountSetBits(uint64_t value, int width)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485