1 /*
  2  * Copyright (c) 2006, 2025, 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 "runtime/sharedRuntime.hpp"
 26 #include "utilities/align.hpp"
 27 #include "utilities/byteswap.hpp"
 28 #include "utilities/copy.hpp"
 29 
 30 
 31 // Copy bytes; larger units are filled atomically if everything is aligned.
 32 void Copy::conjoint_memory_atomic(const void* from, void* to, size_t size) {
 33   uintptr_t bits = (uintptr_t) from | (uintptr_t) to | (uintptr_t) size;
 34 
 35   // (Note:  We could improve performance by ignoring the low bits of size,
 36   // and putting a short cleanup loop after each bulk copy loop.
 37   // There are plenty of other ways to make this faster also,
 38   // and it's a slippery slope.  For now, let's keep this code simple
 39   // since the simplicity helps clarify the atomicity semantics of
 40   // this operation.  There are also CPU-specific assembly versions
 41   // which may or may not want to include such optimizations.)
 42 
 43   if (bits % sizeof(jlong) == 0) {
 44     Copy::conjoint_jlongs_atomic((const jlong*) from, (jlong*) to, size / sizeof(jlong));
 45   } else if (bits % sizeof(jint) == 0) {
 46     Copy::conjoint_jints_atomic((const jint*) from, (jint*) to, size / sizeof(jint));
 47   } else if (bits % sizeof(jshort) == 0) {
 48     Copy::conjoint_jshorts_atomic((const jshort*) from, (jshort*) to, size / sizeof(jshort));
 49   } else {
 50     // Not aligned, so no need to be atomic.
 51     Copy::conjoint_jbytes((const void*) from, (void*) to, size);
 52   }
 53 }
 54 
 55 class CopySwap : AllStatic {
 56 public:
 57   /**
 58    * Copy and optionally byte swap elements
 59    *
 60    * <swap> - true if elements should be byte swapped
 61    *
 62    * @param src address of source
 63    * @param dst address of destination
 64    * @param byte_count number of bytes to copy
 65    * @param elem_size size of the elements to copy-swap
 66    */
 67   template<bool swap>
 68   static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) {
 69     assert(src != nullptr, "address must not be null");
 70     assert(dst != nullptr, "address must not be null");
 71     assert(elem_size == 2 || elem_size == 4 || elem_size == 8,
 72            "incorrect element size: %zu", elem_size);
 73     assert(is_aligned(byte_count, elem_size),
 74            "byte_count %zu must be multiple of element size %zu", byte_count, elem_size);
 75 
 76     address src_end = (address)src + byte_count;
 77 
 78     if (dst <= src || dst >= src_end) {
 79       do_conjoint_swap<RIGHT,swap>(src, dst, byte_count, elem_size);
 80     } else {
 81       do_conjoint_swap<LEFT,swap>(src, dst, byte_count, elem_size);
 82     }
 83   }
 84 
 85 private:
 86   enum CopyDirection {
 87     RIGHT, // lower -> higher address
 88     LEFT   // higher -> lower address
 89   };
 90 
 91   /**
 92    * Copy and byte swap elements
 93    *
 94    * <T> - type of element to copy
 95    * <D> - copy direction
 96    * <is_src_aligned> - true if src argument is aligned to element size
 97    * <is_dst_aligned> - true if dst argument is aligned to element size
 98    *
 99    * @param src address of source
100    * @param dst address of destination
101    * @param byte_count number of bytes to copy
102    */
103   template <typename T, CopyDirection D, bool swap, bool is_src_aligned, bool is_dst_aligned>
104   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {
105     const char* cur_src;
106     char* cur_dst;
107 
108     switch (D) {
109     case RIGHT:
110       cur_src = (const char*)src;
111       cur_dst = (char*)dst;
112       break;
113     case LEFT:
114       cur_src = (const char*)src + byte_count - sizeof(T);
115       cur_dst = (char*)dst + byte_count - sizeof(T);
116       break;
117     }
118 
119     for (size_t i = 0; i < byte_count / sizeof(T); i++) {
120       T tmp;
121 
122       if (is_src_aligned) {
123         tmp = *(T*)cur_src;
124       } else {
125         memcpy(&tmp, cur_src, sizeof(T));
126       }
127 
128       if (swap) {
129         tmp = byteswap(tmp);
130       }
131 
132       if (is_dst_aligned) {
133         *(T*)cur_dst = tmp;
134       } else {
135         memcpy(cur_dst, &tmp, sizeof(T));
136       }
137 
138       switch (D) {
139       case RIGHT:
140         cur_src += sizeof(T);
141         cur_dst += sizeof(T);
142         break;
143       case LEFT:
144         cur_src -= sizeof(T);
145         cur_dst -= sizeof(T);
146         break;
147       }
148     }
149   }
150 
151   /**
152    * Copy and byte swap elements
153    *
154    * <T>    - type of element to copy
155    * <D>    - copy direction
156    * <swap> - true if elements should be byte swapped
157    *
158    * @param src address of source
159    * @param dst address of destination
160    * @param byte_count number of bytes to copy
161    */
162   template <typename T, CopyDirection direction, bool swap>
163   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {
164     if (is_aligned(src, sizeof(T))) {
165       if (is_aligned(dst, sizeof(T))) {
166         do_conjoint_swap<T,direction,swap,true,true>(src, dst, byte_count);
167       } else {
168         do_conjoint_swap<T,direction,swap,true,false>(src, dst, byte_count);
169       }
170     } else {
171       if (is_aligned(dst, sizeof(T))) {
172         do_conjoint_swap<T,direction,swap,false,true>(src, dst, byte_count);
173       } else {
174         do_conjoint_swap<T,direction,swap,false,false>(src, dst, byte_count);
175       }
176     }
177   }
178 
179 
180   /**
181    * Copy and byte swap elements
182    *
183    * <D>    - copy direction
184    * <swap> - true if elements should be byte swapped
185    *
186    * @param src address of source
187    * @param dst address of destination
188    * @param byte_count number of bytes to copy
189    * @param elem_size size of the elements to copy-swap
190    */
191   template <CopyDirection D, bool swap>
192   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {
193     switch (elem_size) {
194     case 2: do_conjoint_swap<uint16_t,D,swap>(src, dst, byte_count); break;
195     case 4: do_conjoint_swap<uint32_t,D,swap>(src, dst, byte_count); break;
196     case 8: do_conjoint_swap<uint64_t,D,swap>(src, dst, byte_count); break;
197     default: guarantee(false, "do_conjoint_swap: Invalid elem_size %zu\n", elem_size);
198     }
199   }
200 };
201 
202 void Copy::conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size) {
203   CopySwap::conjoint_swap_if_needed<false>(src, dst, byte_count, elem_size);
204 }
205 
206 void Copy::conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {
207   CopySwap::conjoint_swap_if_needed<true>(src, dst, byte_count, elem_size);
208 }
209 
210 // Fill bytes; larger units are filled atomically if everything is aligned.
211 void Copy::fill_to_memory_atomic(void* to, size_t size, jubyte value) {
212   address dst = (address)to;
213   uintptr_t bits = (uintptr_t)to | (uintptr_t)size;
214   if (bits % sizeof(jlong) == 0) {
215     jlong fill = (julong)((jubyte)value);  // zero-extend
216     if (fill != 0) {
217       fill += fill << 8;
218       fill += fill << 16;
219       fill += fill << 32;
220     }
221     // Copy::fill_to_jlongs_atomic((jlong*) dst, size / sizeof(jlong));
222     for (uintptr_t off = 0; off < size; off += sizeof(jlong)) {
223       *(jlong*)(dst + off) = fill;
224     }
225   } else if (bits % sizeof(jint) == 0) {
226     jint fill = (juint)((jubyte)value);  // zero-extend
227     if (fill != 0) {
228       fill += fill << 8;
229       fill += fill << 16;
230     }
231     // Copy::fill_to_jints_atomic((jint*) dst, size / sizeof(jint));
232     for (uintptr_t off = 0; off < size; off += sizeof(jint)) {
233       *(jint*)(dst + off) = fill;
234     }
235   } else if (bits % sizeof(jshort) == 0) {
236     jshort fill = (jushort)((jubyte)value);  // zero-extend
237     fill += (jshort)(fill << 8);
238     // Copy::fill_to_jshorts_atomic((jshort*) dst, size / sizeof(jshort));
239     for (uintptr_t off = 0; off < size; off += sizeof(jshort)) {
240       *(jshort*)(dst + off) = fill;
241     }
242   } else {
243     // Not aligned, so no need to be atomic.
244 #ifdef MUSL_LIBC
245     // This code is used by Unsafe and may hit the next page after truncation
246     // of mapped memory. Therefore, we use volatile to prevent compilers from
247     // replacing the loop by memset which may not trigger SIGBUS as needed
248     // (observed on Alpine Linux x86_64)
249     jbyte fill = value;
250     for (uintptr_t off = 0; off < size; off += sizeof(jbyte)) {
251       *(volatile jbyte*)(dst + off) = fill;
252     }
253 #else
254     Copy::fill_to_bytes(dst, size, value);
255 #endif
256   }
257 }