1 /*
   2  * Copyright (c) 2006, 2016, 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/copy.hpp"
  28 
  29 
  30 // Copy bytes; larger units are filled atomically if everything is aligned.
  31 void Copy::conjoint_memory_atomic(void* from, void* to, size_t size) {
  32   address src = (address) from;
  33   address dst = (address) to;
  34   uintptr_t bits = (uintptr_t) src | (uintptr_t) dst | (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((jlong*) src, (jlong*) dst, size / sizeof(jlong));
  46   } else if (bits % sizeof(jint) == 0) {
  47     Copy::conjoint_jints_atomic((jint*) src, (jint*) dst, size / sizeof(jint));
  48   } else if (bits % sizeof(jshort) == 0) {
  49     Copy::conjoint_jshorts_atomic((jshort*) src, (jshort*) dst, size / sizeof(jshort));
  50   } else {
  51     // Not aligned, so no need to be atomic.
  52     Copy::conjoint_jbytes((void*) src, (void*) dst, size);
  53   }
  54 }
  55 
  56 class CopySwap : AllStatic {
  57 public:
  58   /**
  59    * Copy and byte swap elements
  60    *
  61    * @param src address of source
  62    * @param dst address of destination
  63    * @param byte_count number of bytes to copy
  64    * @param elem_size size of the elements to copy-swap
  65    */
  66   static void conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size) {
  67     address src_end = src + byte_count;
  68 
  69     if (dst <= src || dst >= src_end) {
  70       do_conjoint_swap<RIGHT>(src, dst, byte_count, elem_size);
  71     } else {
  72       do_conjoint_swap<LEFT>(src, dst, byte_count, elem_size);
  73     }
  74   }
  75 
  76 private:
  77   /**
  78    * Byte swap a 16-bit value
  79    */
  80   static uint16_t byte_swap(uint16_t x) {
  81     return (x << 8) | (x >> 8);
  82   }
  83 
  84   /**
  85    * Byte swap a 32-bit value
  86    */
  87   static uint32_t byte_swap(uint32_t x) {
  88     uint16_t lo = (uint16_t)x;
  89     uint16_t hi = (uint16_t)(x >> 16);
  90 
  91     return
  92       ((uint32_t)byte_swap(lo) << 16) |
  93       ((uint32_t)byte_swap(hi));
  94   }
  95 
  96   /**
  97    * Byte swap a 64-bit value
  98    */
  99   static uint64_t byte_swap(uint64_t x) {
 100     uint32_t lo = (uint32_t)x;
 101     uint32_t hi = (uint32_t)(x >> 32);
 102 
 103     return
 104       ((uint64_t)byte_swap(lo) << 32) |
 105       ((uint64_t)byte_swap(hi));
 106   }
 107 
 108   enum CopyDirection {
 109     RIGHT, // lower -> higher address
 110     LEFT   // higher -> lower address
 111   };
 112 
 113   /**
 114    * Copy and byte swap elements
 115    *
 116    * <T> - type of element to copy
 117    * <D> - copy direction
 118    * <is_src_aligned> - true if src argument is aligned to element size
 119    * <is_dst_aligned> - true if dst argument is aligned to element size
 120    *
 121    * @param src address of source
 122    * @param dst address of destination
 123    * @param byte_count number of bytes to copy
 124    */
 125   template <typename T,CopyDirection D,bool is_src_aligned, bool is_dst_aligned>
 126   static void do_conjoint_swap(address src, address dst, size_t byte_count) {
 127     address cur_src, cur_dst;
 128 
 129     switch (D) {
 130     case RIGHT:
 131       cur_src = src;
 132       cur_dst = dst;
 133       break;
 134     case LEFT:
 135       cur_src = src + byte_count - sizeof(T);
 136       cur_dst = dst + byte_count - sizeof(T);
 137       break;
 138     }
 139 
 140     for (size_t i = 0; i < byte_count / sizeof(T); i++) {
 141       T tmp;
 142 
 143       if (is_src_aligned) {
 144         tmp = *(T*)cur_src;
 145       } else {
 146         memcpy(&tmp, cur_src, sizeof(T));
 147       }
 148 
 149       tmp = byte_swap(tmp);
 150 
 151       if (is_dst_aligned) {
 152         *(T*)cur_dst = tmp;
 153       } else {
 154         memcpy(cur_dst, &tmp, sizeof(T));
 155       }
 156 
 157       switch (D) {
 158       case RIGHT:
 159         cur_src += sizeof(T);
 160         cur_dst += sizeof(T);
 161         break;
 162       case LEFT:
 163         cur_src -= sizeof(T);
 164         cur_dst -= sizeof(T);
 165         break;
 166       }
 167     }
 168   }
 169 
 170   /**
 171    * Copy and byte swap elements
 172    *
 173    * <T> - type of element to copy
 174    * <D> - copy direction
 175    *
 176    * @param src address of source
 177    * @param dst address of destination
 178    * @param byte_count number of bytes to copy
 179    */
 180   template <typename T, CopyDirection direction>
 181   static void do_conjoint_swap(address src, address dst, size_t byte_count) {
 182     if (is_ptr_aligned(src, sizeof(T))) {
 183       if (is_ptr_aligned(dst, sizeof(T))) {
 184         do_conjoint_swap<T,direction,true,true>(src, dst, byte_count);
 185       } else {
 186         do_conjoint_swap<T,direction,true,false>(src, dst, byte_count);
 187       }
 188     } else {
 189       if (is_ptr_aligned(dst, sizeof(T))) {
 190         do_conjoint_swap<T,direction,false,true>(src, dst, byte_count);
 191       } else {
 192         do_conjoint_swap<T,direction,false,false>(src, dst, byte_count);
 193       }
 194     }
 195   }
 196 
 197 
 198   /**
 199    * Copy and byte swap elements
 200    *
 201    * <D> - copy direction
 202    *
 203    * @param src address of source
 204    * @param dst address of destination
 205    * @param byte_count number of bytes to copy
 206    * @param elem_size size of the elements to copy-swap
 207    */
 208   template <CopyDirection D>
 209   static void do_conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size) {
 210     switch (elem_size) {
 211     case 2: do_conjoint_swap<uint16_t,D>(src, dst, byte_count); break;
 212     case 4: do_conjoint_swap<uint32_t,D>(src, dst, byte_count); break;
 213     case 8: do_conjoint_swap<uint64_t,D>(src, dst, byte_count); break;
 214     default: guarantee(false, "do_conjoint_swap: Invalid elem_size %zd\n", elem_size);
 215     }
 216   }
 217 };
 218 
 219 void Copy::conjoint_swap(address src, address dst, size_t byte_count, size_t elem_size) {
 220   CopySwap::conjoint_swap(src, dst, byte_count, elem_size);
 221 }
 222 
 223 // Fill bytes; larger units are filled atomically if everything is aligned.
 224 void Copy::fill_to_memory_atomic(void* to, size_t size, jubyte value) {
 225   address dst = (address) to;
 226   uintptr_t bits = (uintptr_t) to | (uintptr_t) size;
 227   if (bits % sizeof(jlong) == 0) {
 228     jlong fill = (julong)( (jubyte)value ); // zero-extend
 229     if (fill != 0) {
 230       fill += fill << 8;
 231       fill += fill << 16;
 232       fill += fill << 32;
 233     }
 234     //Copy::fill_to_jlongs_atomic((jlong*) dst, size / sizeof(jlong));
 235     for (uintptr_t off = 0; off < size; off += sizeof(jlong)) {
 236       *(jlong*)(dst + off) = fill;
 237     }
 238   } else if (bits % sizeof(jint) == 0) {
 239     jint fill = (juint)( (jubyte)value ); // zero-extend
 240     if (fill != 0) {
 241       fill += fill << 8;
 242       fill += fill << 16;
 243     }
 244     //Copy::fill_to_jints_atomic((jint*) dst, size / sizeof(jint));
 245     for (uintptr_t off = 0; off < size; off += sizeof(jint)) {
 246       *(jint*)(dst + off) = fill;
 247     }
 248   } else if (bits % sizeof(jshort) == 0) {
 249     jshort fill = (jushort)( (jubyte)value ); // zero-extend
 250     fill += fill << 8;
 251     //Copy::fill_to_jshorts_atomic((jshort*) dst, size / sizeof(jshort));
 252     for (uintptr_t off = 0; off < size; off += sizeof(jshort)) {
 253       *(jshort*)(dst + off) = fill;
 254     }
 255   } else {
 256     // Not aligned, so no need to be atomic.
 257     Copy::fill_to_bytes(dst, size, value);
 258   }
 259 }