1 /*
2  * Copyright (c) 2006, 2018, 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 #include "runtime/sharedRuntime.hpp"
27 #include "utilities/align.hpp"
28 #include "utilities/copy.hpp"
29 
30 
31 // Copy bytes; larger units are filled atomically if everything is aligned.
conjoint_memory_atomic(const void * from,void * to,size_t size)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>
conjoint_swap_if_needed(const void * src,void * dst,size_t byte_count,size_t elem_size)68   static void conjoint_swap_if_needed(const void* src, void* dst, size_t byte_count, size_t elem_size) {
69     assert(src != NULL, "address must not be NULL");
70     assert(dst != NULL, "address must not be NULL");
71     assert(elem_size == 2 || elem_size == 4 || elem_size == 8,
72            "incorrect element size: " SIZE_FORMAT, elem_size);
73     assert(is_aligned(byte_count, elem_size),
74            "byte_count " SIZE_FORMAT " must be multiple of element size " SIZE_FORMAT, 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   /**
87    * Byte swap a 16-bit value
88    */
byte_swap(uint16_t x)89   static uint16_t byte_swap(uint16_t x) {
90     return (x << 8) | (x >> 8);
91   }
92 
93   /**
94    * Byte swap a 32-bit value
95    */
byte_swap(uint32_t x)96   static uint32_t byte_swap(uint32_t x) {
97     uint16_t lo = (uint16_t)x;
98     uint16_t hi = (uint16_t)(x >> 16);
99 
100     return ((uint32_t)byte_swap(lo) << 16) | (uint32_t)byte_swap(hi);
101   }
102 
103   /**
104    * Byte swap a 64-bit value
105    */
byte_swap(uint64_t x)106   static uint64_t byte_swap(uint64_t x) {
107     uint32_t lo = (uint32_t)x;
108     uint32_t hi = (uint32_t)(x >> 32);
109 
110     return ((uint64_t)byte_swap(lo) << 32) | (uint64_t)byte_swap(hi);
111   }
112 
113   enum CopyDirection {
114     RIGHT, // lower -> higher address
115     LEFT   // higher -> lower address
116   };
117 
118   /**
119    * Copy and byte swap elements
120    *
121    * <T> - type of element to copy
122    * <D> - copy direction
123    * <is_src_aligned> - true if src argument is aligned to element size
124    * <is_dst_aligned> - true if dst argument is aligned to element size
125    *
126    * @param src address of source
127    * @param dst address of destination
128    * @param byte_count number of bytes to copy
129    */
130   template <typename T, CopyDirection D, bool swap, bool is_src_aligned, bool is_dst_aligned>
do_conjoint_swap(const void * src,void * dst,size_t byte_count)131   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {
132     const char* cur_src;
133     char* cur_dst;
134 
135     switch (D) {
136     case RIGHT:
137       cur_src = (const char*)src;
138       cur_dst = (char*)dst;
139       break;
140     case LEFT:
141       cur_src = (const char*)src + byte_count - sizeof(T);
142       cur_dst = (char*)dst + byte_count - sizeof(T);
143       break;
144     }
145 
146     for (size_t i = 0; i < byte_count / sizeof(T); i++) {
147       T tmp;
148 
149       if (is_src_aligned) {
150         tmp = *(T*)cur_src;
151       } else {
152         memcpy(&tmp, cur_src, sizeof(T));
153       }
154 
155       if (swap) {
156         tmp = byte_swap(tmp);
157       }
158 
159       if (is_dst_aligned) {
160         *(T*)cur_dst = tmp;
161       } else {
162         memcpy(cur_dst, &tmp, sizeof(T));
163       }
164 
165       switch (D) {
166       case RIGHT:
167         cur_src += sizeof(T);
168         cur_dst += sizeof(T);
169         break;
170       case LEFT:
171         cur_src -= sizeof(T);
172         cur_dst -= sizeof(T);
173         break;
174       }
175     }
176   }
177 
178   /**
179    * Copy and byte swap elements
180    *
181    * <T>    - type of element to copy
182    * <D>    - copy direction
183    * <swap> - true if elements should be byte swapped
184    *
185    * @param src address of source
186    * @param dst address of destination
187    * @param byte_count number of bytes to copy
188    */
189   template <typename T, CopyDirection direction, bool swap>
do_conjoint_swap(const void * src,void * dst,size_t byte_count)190   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count) {
191     if (is_aligned(src, sizeof(T))) {
192       if (is_aligned(dst, sizeof(T))) {
193         do_conjoint_swap<T,direction,swap,true,true>(src, dst, byte_count);
194       } else {
195         do_conjoint_swap<T,direction,swap,true,false>(src, dst, byte_count);
196       }
197     } else {
198       if (is_aligned(dst, sizeof(T))) {
199         do_conjoint_swap<T,direction,swap,false,true>(src, dst, byte_count);
200       } else {
201         do_conjoint_swap<T,direction,swap,false,false>(src, dst, byte_count);
202       }
203     }
204   }
205 
206 
207   /**
208    * Copy and byte swap elements
209    *
210    * <D>    - copy direction
211    * <swap> - true if elements should be byte swapped
212    *
213    * @param src address of source
214    * @param dst address of destination
215    * @param byte_count number of bytes to copy
216    * @param elem_size size of the elements to copy-swap
217    */
218   template <CopyDirection D, bool swap>
do_conjoint_swap(const void * src,void * dst,size_t byte_count,size_t elem_size)219   static void do_conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {
220     switch (elem_size) {
221     case 2: do_conjoint_swap<uint16_t,D,swap>(src, dst, byte_count); break;
222     case 4: do_conjoint_swap<uint32_t,D,swap>(src, dst, byte_count); break;
223     case 8: do_conjoint_swap<uint64_t,D,swap>(src, dst, byte_count); break;
224     default: guarantee(false, "do_conjoint_swap: Invalid elem_size " SIZE_FORMAT "\n", elem_size);
225     }
226   }
227 };
228 
conjoint_copy(const void * src,void * dst,size_t byte_count,size_t elem_size)229 void Copy::conjoint_copy(const void* src, void* dst, size_t byte_count, size_t elem_size) {
230   CopySwap::conjoint_swap_if_needed<false>(src, dst, byte_count, elem_size);
231 }
232 
conjoint_swap(const void * src,void * dst,size_t byte_count,size_t elem_size)233 void Copy::conjoint_swap(const void* src, void* dst, size_t byte_count, size_t elem_size) {
234   CopySwap::conjoint_swap_if_needed<true>(src, dst, byte_count, elem_size);
235 }
236 
237 // Fill bytes; larger units are filled atomically if everything is aligned.
fill_to_memory_atomic(void * to,size_t size,jubyte value)238 void Copy::fill_to_memory_atomic(void* to, size_t size, jubyte value) {
239   address dst = (address) to;
240   uintptr_t bits = (uintptr_t) to | (uintptr_t) size;
241   if (bits % sizeof(jlong) == 0) {
242     jlong fill = (julong)( (jubyte)value ); // zero-extend
243     if (fill != 0) {
244       fill += fill << 8;
245       fill += fill << 16;
246       fill += fill << 32;
247     }
248     //Copy::fill_to_jlongs_atomic((jlong*) dst, size / sizeof(jlong));
249     for (uintptr_t off = 0; off < size; off += sizeof(jlong)) {
250       *(jlong*)(dst + off) = fill;
251     }
252   } else if (bits % sizeof(jint) == 0) {
253     jint fill = (juint)( (jubyte)value ); // zero-extend
254     if (fill != 0) {
255       fill += fill << 8;
256       fill += fill << 16;
257     }
258     //Copy::fill_to_jints_atomic((jint*) dst, size / sizeof(jint));
259     for (uintptr_t off = 0; off < size; off += sizeof(jint)) {
260       *(jint*)(dst + off) = fill;
261     }
262   } else if (bits % sizeof(jshort) == 0) {
263     jshort fill = (jushort)( (jubyte)value ); // zero-extend
264     fill += fill << 8;
265     //Copy::fill_to_jshorts_atomic((jshort*) dst, size / sizeof(jshort));
266     for (uintptr_t off = 0; off < size; off += sizeof(jshort)) {
267       *(jshort*)(dst + off) = fill;
268     }
269   } else {
270     // Not aligned, so no need to be atomic.
271     Copy::fill_to_bytes(dst, size, value);
272   }
273 }
274