1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 27 #include "gc/shared/memset_with_concurrent_readers.hpp" 28 #include "runtime/prefetch.inline.hpp" 29 #include "utilities/align.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 #include "utilities/macros.hpp" 33 34 // An implementation of memset, for use when there may be concurrent 35 // readers of the region being stored into. 36 // 37 // We can't use the standard library memset if it is implemented using 38 // block initializing stores. Doing so can result in concurrent readers 39 // seeing spurious zeros. 40 // 41 // We can't use the obvious C/C++ for-loop, because the compiler may 42 // recognize the idiomatic loop and optimize it into a call to the 43 // standard library memset; we've seen exactly this happen with, for 44 // example, Solaris Studio 12.3. Hence the use of inline assembly 45 // code, hiding loops from the compiler's optimizer. 46 // 47 // We don't attempt to use the standard library memset when it is safe 48 // to do so. We could conservatively do so by detecting the presence 49 // of block initializing stores (VM_Version::has_blk_init()), but the 50 // implementation provided here should be sufficient. 51 52 inline void fill_subword(void* start, void* end, int value) { 53 STATIC_ASSERT(BytesPerWord == 8); 54 assert(pointer_delta(end, start, 1) < (size_t)BytesPerWord, "precondition"); 55 // Dispatch on (end - start). 56 void* pc; 57 __asm__ volatile( 58 // offset := (7 - (end - start)) + 3 59 // 3 instructions from rdpc to DISPATCH 60 " sub %[offset], %[end], %[offset]\n\t" // offset := start - end 61 " sllx %[offset], 2, %[offset]\n\t" // scale offset for instruction size of 4 62 " add %[offset], 40, %[offset]\n\t" // offset += 10 * instruction size 63 " rd %%pc, %[pc]\n\t" // dispatch on scaled offset 64 " jmpl %[pc]+%[offset], %%g0\n\t" 65 " nop\n\t" 66 // DISPATCH: no direct reference, but without it the store block may be elided. 67 "1:\n\t" 68 " stb %[value], [%[end]-7]\n\t" // end[-7] = value 69 " stb %[value], [%[end]-6]\n\t" 70 " stb %[value], [%[end]-5]\n\t" 71 " stb %[value], [%[end]-4]\n\t" 72 " stb %[value], [%[end]-3]\n\t" 73 " stb %[value], [%[end]-2]\n\t" 74 " stb %[value], [%[end]-1]\n\t" // end[-1] = value 75 : /* only temporaries/overwritten outputs */ 76 [pc] "=&r" (pc), // temp 77 [offset] "+&r" (start) 78 : [end] "r" (end), 79 [value] "r" (value) 80 : "memory"); 81 } 82 83 void memset_with_concurrent_readers(void* to, int value, size_t size) { 84 Prefetch::write(to, 0); 85 void* end = static_cast<char*>(to) + size; 86 if (size >= (size_t)BytesPerWord) { 87 // Fill any partial word prefix. 88 uintx* aligned_to = static_cast<uintx*>(align_up(to, BytesPerWord)); 89 fill_subword(to, aligned_to, value); 90 91 // Compute fill word. 92 STATIC_ASSERT(BitsPerByte == 8); 93 STATIC_ASSERT(BitsPerWord == 64); 94 uintx xvalue = value & 0xff; 95 xvalue |= (xvalue << 8); 96 xvalue |= (xvalue << 16); 97 xvalue |= (xvalue << 32); 98 99 uintx* aligned_end = static_cast<uintx*>(align_down(end, BytesPerWord)); 100 assert(aligned_to <= aligned_end, "invariant"); 101 102 // for ( ; aligned_to < aligned_end; ++aligned_to) { 103 // *aligned_to = xvalue; 104 // } 105 uintptr_t temp; 106 __asm__ volatile( 107 // Unroll loop x8. 108 " sub %[aend], %[ato], %[temp]\n\t" 109 " cmp %[temp], 56\n\t" // cc := (aligned_end - aligned_to) > 7 words 110 " ba %%xcc, 2f\n\t" // goto TEST always 111 " sub %[aend], 56, %[temp]\n\t" // limit := aligned_end - 7 words 112 // LOOP: 113 "1:\n\t" // unrolled x8 store loop top 114 " cmp %[temp], %[ato]\n\t" // cc := limit > (next) aligned_to 115 " stx %[xvalue], [%[ato]-64]\n\t" // store 8 words, aligned_to pre-incremented 116 " stx %[xvalue], [%[ato]-56]\n\t" 117 " stx %[xvalue], [%[ato]-48]\n\t" 118 " stx %[xvalue], [%[ato]-40]\n\t" 119 " stx %[xvalue], [%[ato]-32]\n\t" 120 " stx %[xvalue], [%[ato]-24]\n\t" 121 " stx %[xvalue], [%[ato]-16]\n\t" 122 " stx %[xvalue], [%[ato]-8]\n\t" 123 // TEST: 124 "2:\n\t" 125 " bgu,a %%xcc, 1b\n\t" // goto LOOP if more than 7 words remaining 126 " add %[ato], 64, %[ato]\n\t" // aligned_to += 8, for next iteration 127 // Fill remaining < 8 full words. 128 // Dispatch on (aligned_end - aligned_to). 129 // offset := (7 - (aligned_end - aligned_to)) + 3 130 // 3 instructions from rdpc to DISPATCH 131 " sub %[ato], %[aend], %[ato]\n\t" // offset := aligned_to - aligned_end 132 " srax %[ato], 1, %[ato]\n\t" // scale offset for instruction size of 4 133 " add %[ato], 40, %[ato]\n\t" // offset += 10 * instruction size 134 " rd %%pc, %[temp]\n\t" // dispatch on scaled offset 135 " jmpl %[temp]+%[ato], %%g0\n\t" 136 " nop\n\t" 137 // DISPATCH: no direct reference, but without it the store block may be elided. 138 "3:\n\t" 139 " stx %[xvalue], [%[aend]-56]\n\t" // aligned_end[-7] = xvalue 140 " stx %[xvalue], [%[aend]-48]\n\t" 141 " stx %[xvalue], [%[aend]-40]\n\t" 142 " stx %[xvalue], [%[aend]-32]\n\t" 143 " stx %[xvalue], [%[aend]-24]\n\t" 144 " stx %[xvalue], [%[aend]-16]\n\t" 145 " stx %[xvalue], [%[aend]-8]\n\t" // aligned_end[-1] = xvalue 146 : /* only temporaries/overwritten outputs */ 147 [temp] "=&r" (temp), 148 [ato] "+&r" (aligned_to) 149 : [aend] "r" (aligned_end), 150 [xvalue] "r" (xvalue) 151 : "cc", "memory"); 152 to = aligned_end; // setup for suffix 153 } 154 // Fill any partial word suffix. Also the prefix if size < BytesPerWord. 155 fill_subword(to, end, value); 156 }