blob: 5d92632c0bf04c21e27465104a5114af1e688d2b [file] [log] [blame]
sbarati@apple.com64b84492019-11-04 23:57:34 +00001/*
2 * Copyright (C) 2016-2018 Apple Inc. 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "CPU.h"
29#include "JSCJSValue.h"
30
31namespace JSC {
32
33// We use these memory operations when modifying memory that might be scanned by the concurrent collector.
34// We don't call the default operations because they're not guaranteed to store to memory in eight byte aligned
35// chunks. If we happened to fall into the system's normal byte copy loop, we may see a torn JSValue in the
36// concurrent collector.
37
38constexpr size_t smallCutoff = 30 * 8;
39constexpr size_t mediumCutoff = 4 * 1024;
40
41// This is a forwards loop so gcSafeMemmove can rely on the direction.
42template <typename T>
43ALWAYS_INLINE void gcSafeMemcpy(T* dst, T* src, size_t bytes)
44{
45 static_assert(sizeof(T) == sizeof(JSValue));
46 RELEASE_ASSERT(bytes % 8 == 0);
47
48#if USE(JSVALUE64)
49
50 auto slowPathForwardMemcpy = [&] {
51 size_t count = bytes / 8;
52 for (unsigned i = 0; i < count; ++i)
53 bitwise_cast<volatile uint64_t*>(dst)[i] = bitwise_cast<volatile uint64_t*>(src)[i];
54 };
55
commit-queue@webkit.org63f8aea2020-05-07 19:30:28 +000056#if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +000057 if (bytes <= smallCutoff)
58 slowPathForwardMemcpy();
59 else if (isARM64() || bytes <= mediumCutoff) {
60#if CPU(X86_64)
61 size_t alignedBytes = (bytes / 64) * 64;
62 size_t tmp;
63 size_t offset = 0;
64 asm volatile(
65 ".balign 32\t\n"
66 "1:\t\n"
67 "cmpq %q[offset], %q[alignedBytes]\t\n"
68 "je 2f\t\n"
69 "movups (%q[src], %q[offset], 1), %%xmm0\t\n"
70 "movups 16(%q[src], %q[offset], 1), %%xmm1\t\n"
71 "movups 32(%q[src], %q[offset], 1), %%xmm2\t\n"
72 "movups 48(%q[src], %q[offset], 1), %%xmm3\t\n"
73 "movups %%xmm0, (%q[dst], %q[offset], 1)\t\n"
74 "movups %%xmm1, 16(%q[dst], %q[offset], 1)\t\n"
75 "movups %%xmm2, 32(%q[dst], %q[offset], 1)\t\n"
76 "movups %%xmm3, 48(%q[dst], %q[offset], 1)\t\n"
77 "addq $64, %q[offset]\t\n"
78 "jmp 1b\t\n"
79
80 "2:\t\n"
81 "cmpq %q[offset], %q[bytes]\t\n"
82 "je 3f\t\n"
83 "movq (%q[src], %q[offset], 1), %q[tmp]\t\n"
84 "movq %q[tmp], (%q[dst], %q[offset], 1)\t\n"
85 "addq $8, %q[offset]\t\n"
86 "jmp 2b\t\n"
87
88 "3:\t\n"
89
90 : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [tmp] "+r" (tmp), [offset] "+r" (offset), [dst] "+r" (dst), [src] "+r" (src)
91 :
92 : "xmm0", "xmm1", "xmm2", "xmm3", "memory", "cc"
93 );
94#elif CPU(ARM64)
sbarati@apple.comecab6ea2019-11-15 06:50:11 +000095 uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 16) * 16;
sbarati@apple.com64b84492019-11-04 23:57:34 +000096 size_t offset = 0;
97
sbarati@apple.comecab6ea2019-11-15 06:50:11 +000098 uint64_t dstPtr = static_cast<uint64_t>(bitwise_cast<uintptr_t>(dst));
99 uint64_t srcPtr = static_cast<uint64_t>(bitwise_cast<uintptr_t>(src));
100
sbarati@apple.com64b84492019-11-04 23:57:34 +0000101 asm volatile(
102 "1:\t\n"
103 "cmp %x[offset], %x[alignedBytes]\t\n"
104 "b.eq 2f\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000105 "ldr q0, [%x[srcPtr], %x[offset]]\t\n"
106 "str q0, [%x[dstPtr], %x[offset]]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000107 "add %x[offset], %x[offset], #0x10\t\n"
108 "b 1b\t\n"
109
110 "2:\t\n"
111 "cmp %x[offset], %x[bytes]\t\n"
112 "b.eq 3f\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000113 "ldr d0, [%x[srcPtr], %x[offset]]\t\n"
114 "str d0, [%x[dstPtr], %x[offset]]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000115 "add %x[offset], %x[offset], #0x8\t\n"
116 "b 2b\t\n"
117
118 "3:\t\n"
119
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000120 : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [offset] "+r" (offset), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr)
sbarati@apple.com64b84492019-11-04 23:57:34 +0000121 :
122 : "d0", "d1", "memory"
123 );
sbarati@apple.com64b84492019-11-04 23:57:34 +0000124#endif // CPU(X86_64)
125 } else {
126 RELEASE_ASSERT(isX86_64());
127#if CPU(X86_64)
128 size_t count = bytes / 8;
129 asm volatile(
130 ".balign 16\t\n"
131 "cld\t\n"
132 "rep movsq\t\n"
133 : "+D" (dst), "+S" (src), "+c" (count)
134 :
135 : "memory");
136#endif // CPU(X86_64)
137 }
138#else
139 slowPathForwardMemcpy();
commit-queue@webkit.org63f8aea2020-05-07 19:30:28 +0000140#endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +0000141#else
142 memcpy(dst, src, bytes);
143#endif // USE(JSVALUE64)
144}
145
146template <typename T>
147ALWAYS_INLINE void gcSafeMemmove(T* dst, T* src, size_t bytes)
148{
149 static_assert(sizeof(T) == sizeof(JSValue));
150 RELEASE_ASSERT(bytes % 8 == 0);
151#if USE(JSVALUE64)
152 if (bitwise_cast<uintptr_t>(src) >= bitwise_cast<uintptr_t>(dst)) {
153 // This is written to do a forwards loop, so calling it is ok.
154 gcSafeMemcpy(dst, src, bytes);
155 return;
156 }
157
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000158 if ((static_cast<uint64_t>(bitwise_cast<uintptr_t>(src)) + static_cast<uint64_t>(bytes)) <= static_cast<uint64_t>(bitwise_cast<uintptr_t>(dst))) {
sbarati@apple.com64b84492019-11-04 23:57:34 +0000159 gcSafeMemcpy(dst, src, bytes);
160 return;
161 }
162
163 auto slowPathBackwardsMemmove = [&] {
164 size_t count = bytes / 8;
165 for (size_t i = count; i--; )
166 bitwise_cast<volatile uint64_t*>(dst)[i] = bitwise_cast<volatile uint64_t*>(src)[i];
167 };
168
commit-queue@webkit.orgd8744332020-05-07 20:15:10 +0000169#if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +0000170 if (bytes <= smallCutoff)
171 slowPathBackwardsMemmove();
172 else {
173#if CPU(X86_64)
174 size_t alignedBytes = (bytes / 64) * 64;
175
176 size_t tail = alignedBytes;
177 size_t tmp;
178 asm volatile(
179 "2:\t\n"
180 "cmpq %q[tail], %q[bytes]\t\n"
181 "je 1f\t\n"
182 "addq $-8, %q[bytes]\t\n"
183 "movq (%q[src], %q[bytes], 1), %q[tmp]\t\n"
184 "movq %q[tmp], (%q[dst], %q[bytes], 1)\t\n"
185 "jmp 2b\t\n"
186
187 "1:\t\n"
188 "test %q[alignedBytes], %q[alignedBytes]\t\n"
189 "jz 3f\t\n"
190
191 ".balign 32\t\n"
192 "100:\t\n"
193
194 "movups -64(%q[src], %q[alignedBytes], 1), %%xmm0\t\n"
195 "movups -48(%q[src], %q[alignedBytes], 1), %%xmm1\t\n"
196 "movups -32(%q[src], %q[alignedBytes], 1), %%xmm2\t\n"
197 "movups -16(%q[src], %q[alignedBytes], 1), %%xmm3\t\n"
198 "movups %%xmm0, -64(%q[dst], %q[alignedBytes], 1)\t\n"
199 "movups %%xmm1, -48(%q[dst], %q[alignedBytes], 1)\t\n"
200 "movups %%xmm2, -32(%q[dst], %q[alignedBytes], 1)\t\n"
201 "movups %%xmm3, -16(%q[dst], %q[alignedBytes], 1)\t\n"
202 "addq $-64, %q[alignedBytes]\t\n"
203 "jnz 100b\t\n"
204
205 "3:\t\n"
206
207 : [alignedBytes] "+r" (alignedBytes), [tail] "+r" (tail), [bytes] "+r" (bytes), [tmp] "+r" (tmp), [dst] "+r" (dst), [src] "+r" (src)
208 :
209 : "xmm0", "xmm1", "xmm2", "xmm3", "memory", "cc"
210 );
211#elif CPU(ARM64)
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000212 uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 16) * 16;
213 uint64_t dstPtr = static_cast<uint64_t>(bitwise_cast<uintptr_t>(dst));
214 uint64_t srcPtr = static_cast<uint64_t>(bitwise_cast<uintptr_t>(src));
sbarati@apple.com64b84492019-11-04 23:57:34 +0000215
216 asm volatile(
217 "1:\t\n"
218 "cmp %x[alignedBytes], %x[bytes]\t\n"
219 "b.eq 2f\t\n"
220 "sub %x[bytes], %x[bytes], #0x8\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000221 "ldr d0, [%x[srcPtr], %x[bytes]]\t\n"
222 "str d0, [%x[dstPtr], %x[bytes]]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000223 "b 1b\t\n"
224
225 "2:\t\n"
226 "cbz %x[alignedBytes], 3f\t\n"
227 "sub %x[alignedBytes], %x[alignedBytes], #0x10\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000228 "ldr q0, [%x[srcPtr], %x[alignedBytes]]\t\n"
229 "str q0, [%x[dstPtr], %x[alignedBytes]]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000230 "b 2b\t\n"
231
232 "3:\t\n"
233
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000234 : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr)
sbarati@apple.com64b84492019-11-04 23:57:34 +0000235 :
236 : "d0", "d1", "memory"
237 );
sbarati@apple.com64b84492019-11-04 23:57:34 +0000238#endif // CPU(X86_64)
239 }
240#else
241 slowPathBackwardsMemmove();
commit-queue@webkit.orgd8744332020-05-07 20:15:10 +0000242#endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +0000243#else
244 memmove(dst, src, bytes);
245#endif // USE(JSVALUE64)
246}
247
248template <typename T>
249ALWAYS_INLINE void gcSafeZeroMemory(T* dst, size_t bytes)
250{
251 static_assert(sizeof(T) == sizeof(JSValue));
252 RELEASE_ASSERT(bytes % 8 == 0);
253#if USE(JSVALUE64)
commit-queue@webkit.orgd8744332020-05-07 20:15:10 +0000254#if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +0000255#if CPU(X86_64)
256 uint64_t zero = 0;
257 size_t count = bytes / 8;
258 asm volatile (
259 "rep stosq\n\t"
260 : "+D"(dst), "+c"(count)
261 : "a"(zero)
262 : "memory"
263 );
264#elif CPU(ARM64)
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000265 uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 64) * 64;
266 uint64_t dstPtr = static_cast<uint64_t>(bitwise_cast<uintptr_t>(dst));
267 uint64_t end = dstPtr + bytes;
268 uint64_t alignedEnd = dstPtr + alignedBytes;
sbarati@apple.com64b84492019-11-04 23:57:34 +0000269 asm volatile(
270 "movi d0, #0\t\n"
271 "movi d1, #0\t\n"
272
273 ".p2align 4\t\n"
274 "2:\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000275 "cmp %x[dstPtr], %x[alignedEnd]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000276 "b.eq 4f\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000277 "stnp q0, q0, [%x[dstPtr]]\t\n"
278 "stnp q0, q0, [%x[dstPtr], #0x20]\t\n"
279 "add %x[dstPtr], %x[dstPtr], #0x40\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000280 "b 2b\t\n"
281
282 "4:\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000283 "cmp %x[dstPtr], %x[end]\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000284 "b.eq 5f\t\n"
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000285 "str d0, [%x[dstPtr]], #0x8\t\n"
sbarati@apple.com64b84492019-11-04 23:57:34 +0000286 "b 4b\t\n"
287
288 "5:\t\n"
289
sbarati@apple.comecab6ea2019-11-15 06:50:11 +0000290 : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [dstPtr] "+r" (dstPtr), [end] "+r" (end), [alignedEnd] "+r" (alignedEnd)
sbarati@apple.com64b84492019-11-04 23:57:34 +0000291 :
292 : "d0", "d1", "memory"
293 );
sbarati@apple.com64b84492019-11-04 23:57:34 +0000294#endif // CPU(X86_64)
295#else
296 size_t count = bytes / 8;
297 for (size_t i = 0; i < count; ++i)
298 bitwise_cast<volatile uint64_t*>(dst)[i] = 0;
commit-queue@webkit.orgd8744332020-05-07 20:15:10 +0000299#endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64))
sbarati@apple.com64b84492019-11-04 23:57:34 +0000300#else
commit-queue@webkit.org00469cb2020-03-05 08:54:49 +0000301 memset(reinterpret_cast<char*>(dst), 0, bytes);
sbarati@apple.com64b84492019-11-04 23:57:34 +0000302#endif // USE(JSVALUE64)
303}
304
305} // namespace JSC