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