blob: 712dbbd650b24bc4022c31bb06f190b7afbe385b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_ARM64
8
9#include "src/arm64/utils-arm64.h"
10#include "src/assembler.h"
11
12namespace v8 {
13namespace internal {
14
15class CacheLineSizes {
16 public:
17 CacheLineSizes() {
18#ifdef USE_SIMULATOR
19 cache_type_register_ = 0;
20#else
21 // Copy the content of the cache type register to a core register.
Ben Murdochda12d292016-06-02 14:46:10 +010022 __asm__ __volatile__("mrs %x[ctr], ctr_el0" // NOLINT
Ben Murdoch097c5b22016-05-18 11:27:45 +010023 : [ctr] "=r"(cache_type_register_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024#endif
25 }
26
27 uint32_t icache_line_size() const { return ExtractCacheLineSize(0); }
28 uint32_t dcache_line_size() const { return ExtractCacheLineSize(16); }
29
30 private:
31 uint32_t ExtractCacheLineSize(int cache_line_size_shift) const {
32 // The cache type register holds the size of cache lines in words as a
33 // power of two.
34 return 4 << ((cache_type_register_ >> cache_line_size_shift) & 0xf);
35 }
36
37 uint32_t cache_type_register_;
38};
39
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040void CpuFeatures::FlushICache(void* address, size_t length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041#ifdef V8_HOST_ARCH_ARM64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 // The code below assumes user space cache operations are allowed. The goal
43 // of this routine is to make sure the code generated is visible to the I
44 // side of the CPU.
45
46 uintptr_t start = reinterpret_cast<uintptr_t>(address);
47 // Sizes will be used to generate a mask big enough to cover a pointer.
48 CacheLineSizes sizes;
49 uintptr_t dsize = sizes.dcache_line_size();
50 uintptr_t isize = sizes.icache_line_size();
51 // Cache line sizes are always a power of 2.
52 DCHECK(CountSetBits(dsize, 64) == 1);
53 DCHECK(CountSetBits(isize, 64) == 1);
54 uintptr_t dstart = start & ~(dsize - 1);
55 uintptr_t istart = start & ~(isize - 1);
56 uintptr_t end = start + length;
57
58 __asm__ __volatile__ ( // NOLINT
59 // Clean every line of the D cache containing the target data.
60 "0: \n\t"
61 // dc : Data Cache maintenance
62 // c : Clean
63 // va : by (Virtual) Address
64 // u : to the point of Unification
65 // The point of unification for a processor is the point by which the
66 // instruction and data caches are guaranteed to see the same copy of a
67 // memory location. See ARM DDI 0406B page B2-12 for more information.
68 "dc cvau, %[dline] \n\t"
69 "add %[dline], %[dline], %[dsize] \n\t"
70 "cmp %[dline], %[end] \n\t"
71 "b.lt 0b \n\t"
72 // Barrier to make sure the effect of the code above is visible to the rest
73 // of the world.
74 // dsb : Data Synchronisation Barrier
75 // ish : Inner SHareable domain
76 // The point of unification for an Inner Shareable shareability domain is
77 // the point by which the instruction and data caches of all the processors
78 // in that Inner Shareable shareability domain are guaranteed to see the
79 // same copy of a memory location. See ARM DDI 0406B page B2-12 for more
80 // information.
81 "dsb ish \n\t"
82 // Invalidate every line of the I cache containing the target data.
83 "1: \n\t"
84 // ic : instruction cache maintenance
85 // i : invalidate
86 // va : by address
87 // u : to the point of unification
88 "ic ivau, %[iline] \n\t"
89 "add %[iline], %[iline], %[isize] \n\t"
90 "cmp %[iline], %[end] \n\t"
91 "b.lt 1b \n\t"
92 // Barrier to make sure the effect of the code above is visible to the rest
93 // of the world.
94 "dsb ish \n\t"
95 // Barrier to ensure any prefetching which happened before this code is
96 // discarded.
97 // isb : Instruction Synchronisation Barrier
98 "isb \n\t"
99 : [dline] "+r" (dstart),
100 [iline] "+r" (istart)
101 : [dsize] "r" (dsize),
102 [isize] "r" (isize),
103 [end] "r" (end)
104 // This code does not write to memory but without the dependency gcc might
105 // move this code before the code is generated.
106 : "cc", "memory"
107 ); // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108#endif // V8_HOST_ARCH_ARM64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109}
110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111} // namespace internal
112} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113
114#endif // V8_TARGET_ARCH_ARM64