1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright (C) 2016-2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "CPU.h"
#include "JSCJSValue.h"
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
// We use these memory operations when modifying memory that might be scanned by the concurrent collector.
// We don't call the default operations because they're not guaranteed to store to memory in eight byte aligned
// chunks. If we happened to fall into the system's normal byte copy loop, we may see a torn JSValue in the
// concurrent collector.
constexpr size_t smallCutoff = 30 * 8;
constexpr size_t mediumCutoff = 4 * 1024;
// This is a forwards loop so gcSafeMemmove can rely on the direction.
template <typename T>
ALWAYS_INLINE void gcSafeMemcpy(T* dst, const T* src, size_t bytes)
{
static_assert(sizeof(T) == sizeof(JSValue));
RELEASE_ASSERT(bytes % 8 == 0);
#if USE(JSVALUE64)
auto slowPathForwardMemcpy = [&] {
size_t count = bytes / 8;
for (unsigned i = 0; i < count; ++i)
std::bit_cast<volatile uint64_t*>(dst)[i] = std::bit_cast<volatile uint64_t*>(src)[i];
};
#if CPU(X86_64) || CPU(ARM64)
if (bytes <= smallCutoff)
slowPathForwardMemcpy();
else if (isARM64() || bytes <= mediumCutoff) {
#if CPU(X86_64)
size_t alignedBytes = (bytes / 64) * 64;
size_t tmp;
size_t offset = 0;
asm volatile(
".balign 32\t\n"
"1:\t\n"
"cmpq %q[offset], %q[alignedBytes]\t\n"
"je 2f\t\n"
"movups (%q[src], %q[offset], 1), %%xmm0\t\n"
"movups 16(%q[src], %q[offset], 1), %%xmm1\t\n"
"movups 32(%q[src], %q[offset], 1), %%xmm2\t\n"
"movups 48(%q[src], %q[offset], 1), %%xmm3\t\n"
"movups %%xmm0, (%q[dst], %q[offset], 1)\t\n"
"movups %%xmm1, 16(%q[dst], %q[offset], 1)\t\n"
"movups %%xmm2, 32(%q[dst], %q[offset], 1)\t\n"
"movups %%xmm3, 48(%q[dst], %q[offset], 1)\t\n"
"addq $64, %q[offset]\t\n"
"jmp 1b\t\n"
"2:\t\n"
"cmpq %q[offset], %q[bytes]\t\n"
"je 3f\t\n"
"movq (%q[src], %q[offset], 1), %q[tmp]\t\n"
"movq %q[tmp], (%q[dst], %q[offset], 1)\t\n"
"addq $8, %q[offset]\t\n"
"jmp 2b\t\n"
"3:\t\n"
: [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [tmp] "+r" (tmp), [offset] "+r" (offset), [dst] "+r" (dst), [src] "+r" (src)
:
: "xmm0", "xmm1", "xmm2", "xmm3", "memory", "cc"
);
#elif CPU(ARM64)
uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 32) * 32;
uint64_t dstPtr = static_cast<uint64_t>(std::bit_cast<uintptr_t>(dst));
uint64_t srcPtr = static_cast<uint64_t>(std::bit_cast<uintptr_t>(src));
uint64_t end = dstPtr + bytes;
uint64_t alignedEnd = dstPtr + alignedBytes;
asm volatile(
"1:\t\n"
"cmp %x[dstPtr], %x[alignedEnd]\t\n"
"b.eq 2f\t\n"
"ldp q0, q1, [%x[srcPtr]], #0x20\t\n"
"stp q0, q1, [%x[dstPtr]], #0x20\t\n"
"b 1b\t\n"
"2:\t\n"
"cmp %x[dstPtr], %x[end]\t\n"
"b.eq 3f\t\n"
"ldr d0, [%x[srcPtr]], #0x8\t\n"
"str d0, [%x[dstPtr]], #0x8\t\n"
"b 2b\t\n"
"3:\t\n"
: [end] "+r" (end), [alignedEnd] "+r" (alignedEnd), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr)
:
: "d0", "d1", "memory", "cc"
);
#endif // CPU(X86_64)
} else {
RELEASE_ASSERT(isX86_64());
#if CPU(X86_64)
size_t count = bytes / 8;
asm volatile(
".balign 16\t\n"
"cld\t\n"
"rep movsq\t\n"
: "+D" (dst), "+S" (src), "+c" (count)
:
: "memory");
#endif // CPU(X86_64)
}
#else
slowPathForwardMemcpy();
#endif // CPU(X86_64) || CPU(ARM64)
#else
memcpy(dst, src, bytes);
#endif // USE(JSVALUE64)
}
template <typename T>
ALWAYS_INLINE void gcSafeMemmove(T* dst, const T* src, size_t bytes)
{
static_assert(sizeof(T) == sizeof(JSValue));
RELEASE_ASSERT(bytes % 8 == 0);
#if USE(JSVALUE64)
if (std::bit_cast<uintptr_t>(src) >= std::bit_cast<uintptr_t>(dst)) {
// This is written to do a forwards loop, so calling it is ok.
gcSafeMemcpy(dst, src, bytes);
return;
}
if ((static_cast<uint64_t>(std::bit_cast<uintptr_t>(src)) + static_cast<uint64_t>(bytes)) <= static_cast<uint64_t>(std::bit_cast<uintptr_t>(dst))) {
gcSafeMemcpy(dst, src, bytes);
return;
}
auto slowPathBackwardsMemmove = [&] {
size_t count = bytes / 8;
for (size_t i = count; i--; )
std::bit_cast<volatile uint64_t*>(dst)[i] = std::bit_cast<volatile uint64_t*>(src)[i];
};
#if CPU(X86_64) || CPU(ARM64)
if (bytes <= smallCutoff)
slowPathBackwardsMemmove();
else {
#if CPU(X86_64)
size_t alignedBytes = (bytes / 64) * 64;
size_t tail = alignedBytes;
size_t tmp;
asm volatile(
"2:\t\n"
"cmpq %q[tail], %q[bytes]\t\n"
"je 1f\t\n"
"addq $-8, %q[bytes]\t\n"
"movq (%q[src], %q[bytes], 1), %q[tmp]\t\n"
"movq %q[tmp], (%q[dst], %q[bytes], 1)\t\n"
"jmp 2b\t\n"
"1:\t\n"
"test %q[alignedBytes], %q[alignedBytes]\t\n"
"jz 3f\t\n"
".balign 32\t\n"
"100:\t\n"
"movups -64(%q[src], %q[alignedBytes], 1), %%xmm0\t\n"
"movups -48(%q[src], %q[alignedBytes], 1), %%xmm1\t\n"
"movups -32(%q[src], %q[alignedBytes], 1), %%xmm2\t\n"
"movups -16(%q[src], %q[alignedBytes], 1), %%xmm3\t\n"
"movups %%xmm0, -64(%q[dst], %q[alignedBytes], 1)\t\n"
"movups %%xmm1, -48(%q[dst], %q[alignedBytes], 1)\t\n"
"movups %%xmm2, -32(%q[dst], %q[alignedBytes], 1)\t\n"
"movups %%xmm3, -16(%q[dst], %q[alignedBytes], 1)\t\n"
"addq $-64, %q[alignedBytes]\t\n"
"jnz 100b\t\n"
"3:\t\n"
: [alignedBytes] "+r" (alignedBytes), [tail] "+r" (tail), [bytes] "+r" (bytes), [tmp] "+r" (tmp), [dst] "+r" (dst), [src] "+r" (src)
:
: "xmm0", "xmm1", "xmm2", "xmm3", "memory", "cc"
);
#elif CPU(ARM64)
uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 32) * 32;
uint64_t dstPtr = static_cast<uint64_t>(std::bit_cast<uintptr_t>(dst) + static_cast<uint64_t>(bytes));
uint64_t srcPtr = static_cast<uint64_t>(std::bit_cast<uintptr_t>(src) + static_cast<uint64_t>(bytes));
uint64_t alignedEnd = std::bit_cast<uintptr_t>(dst) + alignedBytes;
uint64_t end = std::bit_cast<uintptr_t>(dst);
asm volatile(
"1:\t\n"
"cmp %x[dstPtr], %x[alignedEnd]\t\n"
"b.eq 2f\t\n"
"ldr d0, [%x[srcPtr], #-0x8]!\t\n"
"str d0, [%x[dstPtr], #-0x8]!\t\n"
"b 1b\t\n"
"2:\t\n"
"cmp %x[dstPtr], %x[end]\t\n"
"b.eq 3f\t\n"
"ldp q0, q1, [%x[srcPtr], #-0x20]!\t\n"
"stp q0, q1, [%x[dstPtr], #-0x20]!\t\n"
"b 2b\t\n"
"3:\t\n"
: [alignedEnd] "+r" (alignedEnd), [end] "+r" (end), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr)
:
: "d0", "d1", "memory", "cc"
);
#endif // CPU(X86_64)
}
#else
slowPathBackwardsMemmove();
#endif // CPU(X86_64) || CPU(ARM64)
#else
memmove(dst, src, bytes);
#endif // USE(JSVALUE64)
}
template <typename T>
ALWAYS_INLINE void gcSafeZeroMemory(T* dst, size_t bytes)
{
static_assert(sizeof(T) == sizeof(JSValue));
RELEASE_ASSERT(bytes % 8 == 0);
#if USE(JSVALUE64)
#if CPU(X86_64)
uint64_t zero = 0;
size_t count = bytes / 8;
asm volatile (
"rep stosq\n\t"
: "+D"(dst), "+c"(count)
: "a"(zero)
: "memory"
);
#elif CPU(ARM64)
uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 64) * 64;
uint64_t dstPtr = static_cast<uint64_t>(std::bit_cast<uintptr_t>(dst));
uint64_t end = dstPtr + bytes;
uint64_t alignedEnd = dstPtr + alignedBytes;
asm volatile(
"movi d0, #0\t\n"
"movi d1, #0\t\n"
".p2align 4\t\n"
"2:\t\n"
"cmp %x[dstPtr], %x[alignedEnd]\t\n"
"b.eq 4f\t\n"
"stnp q0, q0, [%x[dstPtr]]\t\n"
"stnp q0, q0, [%x[dstPtr], #0x20]\t\n"
"add %x[dstPtr], %x[dstPtr], #0x40\t\n"
"b 2b\t\n"
"4:\t\n"
"cmp %x[dstPtr], %x[end]\t\n"
"b.eq 5f\t\n"
"str d0, [%x[dstPtr]], #0x8\t\n"
"b 4b\t\n"
"5:\t\n"
: [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [dstPtr] "+r" (dstPtr), [end] "+r" (end), [alignedEnd] "+r" (alignedEnd)
:
: "d0", "d1", "memory", "cc"
);
#else
size_t count = bytes / 8;
for (size_t i = 0; i < count; ++i)
std::bit_cast<volatile uint64_t*>(dst)[i] = 0;
#endif
#else
memset(reinterpret_cast<char*>(dst), 0, bytes);
#endif // USE(JSVALUE64)
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|