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#if INCLUDE_ALL_GCS
35
36// An implementation of memset, for use when there may be concurrent
37// readers of the region being stored into.
38//
39// We can't use the standard library memset if it is implemented using
40// block initializing stores.  Doing so can result in concurrent readers
41// seeing spurious zeros.
42//
43// We can't use the obvious C/C++ for-loop, because the compiler may
44// recognize the idiomatic loop and optimize it into a call to the
45// standard library memset; we've seen exactly this happen with, for
46// example, Solaris Studio 12.3.  Hence the use of inline assembly
47// code, hiding loops from the compiler's optimizer.
48//
49// We don't attempt to use the standard library memset when it is safe
50// to do so.  We could conservatively do so by detecting the presence
51// of block initializing stores (VM_Version::has_blk_init()), but the
52// implementation provided here should be sufficient.
53
54inline void fill_subword(void* start, void* end, int value) {
55  STATIC_ASSERT(BytesPerWord == 8);
56  assert(pointer_delta(end, start, 1) < (size_t)BytesPerWord, "precondition");
57  // Dispatch on (end - start).
58  void* pc;
59  __asm__ volatile(
60    // offset := (7 - (end - start)) + 3
61    //   3 instructions from rdpc to DISPATCH
62    " sub %[offset], %[end], %[offset]\n\t" // offset := start - end
63    " sllx %[offset], 2, %[offset]\n\t" // scale offset for instruction size of 4
64    " add %[offset], 40, %[offset]\n\t" // offset += 10 * instruction size
65    " rd %%pc, %[pc]\n\t"               // dispatch on scaled offset
66    " jmpl %[pc]+%[offset], %%g0\n\t"
67    "  nop\n\t"
68    // DISPATCH: no direct reference, but without it the store block may be elided.
69    "1:\n\t"
70    " stb %[value], [%[end]-7]\n\t" // end[-7] = value
71    " stb %[value], [%[end]-6]\n\t"
72    " stb %[value], [%[end]-5]\n\t"
73    " stb %[value], [%[end]-4]\n\t"
74    " stb %[value], [%[end]-3]\n\t"
75    " stb %[value], [%[end]-2]\n\t"
76    " stb %[value], [%[end]-1]\n\t" // end[-1] = value
77    : /* only temporaries/overwritten outputs */
78      [pc] "=&r" (pc),               // temp
79      [offset] "+&r" (start)
80    : [end] "r" (end),
81      [value] "r" (value)
82    : "memory");
83}
84
85void memset_with_concurrent_readers(void* to, int value, size_t size) {
86  Prefetch::write(to, 0);
87  void* end = static_cast<char*>(to) + size;
88  if (size >= (size_t)BytesPerWord) {
89    // Fill any partial word prefix.
90    uintx* aligned_to = static_cast<uintx*>(align_up(to, BytesPerWord));
91    fill_subword(to, aligned_to, value);
92
93    // Compute fill word.
94    STATIC_ASSERT(BitsPerByte == 8);
95    STATIC_ASSERT(BitsPerWord == 64);
96    uintx xvalue = value & 0xff;
97    xvalue |= (xvalue << 8);
98    xvalue |= (xvalue << 16);
99    xvalue |= (xvalue << 32);
100
101    uintx* aligned_end = static_cast<uintx*>(align_down(end, BytesPerWord));
102    assert(aligned_to <= aligned_end, "invariant");
103
104    // for ( ; aligned_to < aligned_end; ++aligned_to) {
105    //   *aligned_to = xvalue;
106    // }
107    uintptr_t temp;
108    __asm__ volatile(
109      // Unroll loop x8.
110      " sub %[aend], %[ato], %[temp]\n\t"
111      " cmp %[temp], 56\n\t"           // cc := (aligned_end - aligned_to) > 7 words
112      " ba %%xcc, 2f\n\t"              // goto TEST always
113      "  sub %[aend], 56, %[temp]\n\t" // limit := aligned_end - 7 words
114      // LOOP:
115      "1:\n\t"                         // unrolled x8 store loop top
116      " cmp %[temp], %[ato]\n\t"       // cc := limit > (next) aligned_to
117      " stx %[xvalue], [%[ato]-64]\n\t" // store 8 words, aligned_to pre-incremented
118      " stx %[xvalue], [%[ato]-56]\n\t"
119      " stx %[xvalue], [%[ato]-48]\n\t"
120      " stx %[xvalue], [%[ato]-40]\n\t"
121      " stx %[xvalue], [%[ato]-32]\n\t"
122      " stx %[xvalue], [%[ato]-24]\n\t"
123      " stx %[xvalue], [%[ato]-16]\n\t"
124      " stx %[xvalue], [%[ato]-8]\n\t"
125      // TEST:
126      "2:\n\t"
127      " bgu,a %%xcc, 1b\n\t"           // goto LOOP if more than 7 words remaining
128      "  add %[ato], 64, %[ato]\n\t"   // aligned_to += 8, for next iteration
129      // Fill remaining < 8 full words.
130      // Dispatch on (aligned_end - aligned_to).
131      // offset := (7 - (aligned_end - aligned_to)) + 3
132      //   3 instructions from rdpc to DISPATCH
133      " sub %[ato], %[aend], %[ato]\n\t" // offset := aligned_to - aligned_end
134      " srax %[ato], 1, %[ato]\n\t"      // scale offset for instruction size of 4
135      " add %[ato], 40, %[ato]\n\t"      // offset += 10 * instruction size
136      " rd %%pc, %[temp]\n\t"            // dispatch on scaled offset
137      " jmpl %[temp]+%[ato], %%g0\n\t"
138      "  nop\n\t"
139      // DISPATCH: no direct reference, but without it the store block may be elided.
140      "3:\n\t"
141      " stx %[xvalue], [%[aend]-56]\n\t" // aligned_end[-7] = xvalue
142      " stx %[xvalue], [%[aend]-48]\n\t"
143      " stx %[xvalue], [%[aend]-40]\n\t"
144      " stx %[xvalue], [%[aend]-32]\n\t"
145      " stx %[xvalue], [%[aend]-24]\n\t"
146      " stx %[xvalue], [%[aend]-16]\n\t"
147      " stx %[xvalue], [%[aend]-8]\n\t"  // aligned_end[-1] = xvalue
148      : /* only temporaries/overwritten outputs */
149        [temp] "=&r" (temp),
150        [ato] "+&r" (aligned_to)
151      : [aend] "r" (aligned_end),
152        [xvalue] "r" (xvalue)
153      : "cc", "memory");
154    to = aligned_end;           // setup for suffix
155  }
156  // Fill any partial word suffix.  Also the prefix if size < BytesPerWord.
157  fill_subword(to, end, value);
158}
159
160#endif // INCLUDE_ALL_GCS
161