sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | namespace 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 | |
| 38 | constexpr size_t smallCutoff = 30 * 8; |
| 39 | constexpr size_t mediumCutoff = 4 * 1024; |
| 40 | |
| 41 | // This is a forwards loop so gcSafeMemmove can rely on the direction. |
| 42 | template <typename T> |
| 43 | ALWAYS_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.org | 63f8aea | 2020-05-07 19:30:28 +0000 | [diff] [blame] | 56 | #if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 57 | 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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 95 | uint64_t alignedBytes = (static_cast<uint64_t>(bytes) / 16) * 16; |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 96 | size_t offset = 0; |
| 97 | |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 98 | 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.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 101 | asm volatile( |
| 102 | "1:\t\n" |
| 103 | "cmp %x[offset], %x[alignedBytes]\t\n" |
| 104 | "b.eq 2f\t\n" |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 105 | "ldr q0, [%x[srcPtr], %x[offset]]\t\n" |
| 106 | "str q0, [%x[dstPtr], %x[offset]]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 107 | "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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 113 | "ldr d0, [%x[srcPtr], %x[offset]]\t\n" |
| 114 | "str d0, [%x[dstPtr], %x[offset]]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 115 | "add %x[offset], %x[offset], #0x8\t\n" |
| 116 | "b 2b\t\n" |
| 117 | |
| 118 | "3:\t\n" |
| 119 | |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 120 | : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [offset] "+r" (offset), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 121 | : |
| 122 | : "d0", "d1", "memory" |
| 123 | ); |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 124 | #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.org | 63f8aea | 2020-05-07 19:30:28 +0000 | [diff] [blame] | 140 | #endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 141 | #else |
| 142 | memcpy(dst, src, bytes); |
| 143 | #endif // USE(JSVALUE64) |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | ALWAYS_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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 158 | 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.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 159 | 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.org | d874433 | 2020-05-07 20:15:10 +0000 | [diff] [blame] | 169 | #if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 170 | 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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 212 | 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.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 215 | |
| 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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 221 | "ldr d0, [%x[srcPtr], %x[bytes]]\t\n" |
| 222 | "str d0, [%x[dstPtr], %x[bytes]]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 223 | "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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 228 | "ldr q0, [%x[srcPtr], %x[alignedBytes]]\t\n" |
| 229 | "str q0, [%x[dstPtr], %x[alignedBytes]]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 230 | "b 2b\t\n" |
| 231 | |
| 232 | "3:\t\n" |
| 233 | |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 234 | : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [dstPtr] "+r" (dstPtr), [srcPtr] "+r" (srcPtr) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 235 | : |
| 236 | : "d0", "d1", "memory" |
| 237 | ); |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 238 | #endif // CPU(X86_64) |
| 239 | } |
| 240 | #else |
| 241 | slowPathBackwardsMemmove(); |
commit-queue@webkit.org | d874433 | 2020-05-07 20:15:10 +0000 | [diff] [blame] | 242 | #endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 243 | #else |
| 244 | memmove(dst, src, bytes); |
| 245 | #endif // USE(JSVALUE64) |
| 246 | } |
| 247 | |
| 248 | template <typename T> |
| 249 | ALWAYS_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.org | d874433 | 2020-05-07 20:15:10 +0000 | [diff] [blame] | 254 | #if COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 255 | #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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 265 | 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.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 269 | 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.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 275 | "cmp %x[dstPtr], %x[alignedEnd]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 276 | "b.eq 4f\t\n" |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 277 | "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.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 280 | "b 2b\t\n" |
| 281 | |
| 282 | "4:\t\n" |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 283 | "cmp %x[dstPtr], %x[end]\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 284 | "b.eq 5f\t\n" |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 285 | "str d0, [%x[dstPtr]], #0x8\t\n" |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 286 | "b 4b\t\n" |
| 287 | |
| 288 | "5:\t\n" |
| 289 | |
sbarati@apple.com | ecab6ea | 2019-11-15 06:50:11 +0000 | [diff] [blame] | 290 | : [alignedBytes] "+r" (alignedBytes), [bytes] "+r" (bytes), [dstPtr] "+r" (dstPtr), [end] "+r" (end), [alignedEnd] "+r" (alignedEnd) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 291 | : |
| 292 | : "d0", "d1", "memory" |
| 293 | ); |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 294 | #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.org | d874433 | 2020-05-07 20:15:10 +0000 | [diff] [blame] | 299 | #endif // COMPILER(GCC_COMPATIBLE) && (CPU(X86_64) || CPU(ARM64)) |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 300 | #else |
commit-queue@webkit.org | 00469cb | 2020-03-05 08:54:49 +0000 | [diff] [blame] | 301 | memset(reinterpret_cast<char*>(dst), 0, bytes); |
sbarati@apple.com | 64b8449 | 2019-11-04 23:57:34 +0000 | [diff] [blame] | 302 | #endif // USE(JSVALUE64) |
| 303 | } |
| 304 | |
| 305 | } // namespace JSC |