1 /*
  2  * Copyright (c) 2021 SAP SE. All rights reserved.
  3  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
  4  *
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #include "precompiled.hpp"
 28 #include "oops/compressedKlass.hpp"
 29 #include "utilities/ostream.hpp"
 30 #include "utilities/debug.hpp"
 31 #include "runtime/globals.hpp"
 32 
 33 address CompressedKlassPointers::_base = NULL;
 34 int CompressedKlassPointers::_shift_copy = 0;
 35 
 36 // Given an address range [addr, addr+len) which the encoding is supposed to
 37 //  cover, choose base, shift and range.
 38 //  The address range is the expected range of uncompressed Klass pointers we
 39 //  will encounter (and the implicit promise that there will be no Klass
 40 //  structures outside this range).
 41 void CompressedKlassPointers::initialize(address addr, size_t len) {
 42 #ifdef _LP64
 43   assert(UseCompressedClassPointers, "Sanity");
 44 
 45   assert(len <= (size_t)KlassEncodingMetaspaceMax, "Range " SIZE_FORMAT " too large "
 46          "- cannot be contained fully in narrow Klass pointer encoding range.", len);
 47 
 48   if (UseSharedSpaces || DumpSharedSpaces) {
 49 
 50     // Special requirements if CDS is active:
 51     // Encoding base and shift must be the same between dump and run time.
 52     //   CDS takes care that the SharedBaseAddress and CompressedClassSpaceSize
 53     //   are the same. Archive size will be probably different at runtime, but
 54     //   it can only be smaller than at, never larger, since archives get
 55     //   shrunk at the end of the dump process.
 56     //   From that it follows that the range [addr, len) we are handed in at
 57     //   runtime will start at the same address then at dumptime, and its len
 58     //   may be smaller at runtime then it was at dump time.
 59     //
 60     // To be very careful here, we avoid any optimizations and just keep using
 61     //  the same address and shift value. Specifically we avoid using zero-based
 62     //  encoding. We also set the expected value range to 4G (encoding range
 63     //  cannot be larger than that).
 64 
 65     _base = addr;
 66 
 67   } else {
 68 
 69     // (Note that this case is almost not worth optimizing for. CDS is typically on.)
 70     if ((addr + len) <= (address)KlassEncodingMetaspaceMax) {
 71       _base = 0;
 72     } else {
 73       _base = addr;
 74     }
 75   }
 76 
 77   assert(is_valid_base(_base), "Address " PTR_FORMAT " was chosen as encoding base for range ["
 78                                PTR_FORMAT ", " PTR_FORMAT ") but is not a valid encoding base",
 79                                p2i(_base), p2i(addr), p2i(addr + len));
 80 
 81   // For SA
 82   _shift_copy = LogKlassAlignmentInBytes;
 83 
 84 #else
 85   ShouldNotReachHere(); // 64-bit only
 86 #endif
 87 }
 88 
 89 // 64-bit platforms define these functions on a per-platform base. They are not needed for
 90 //  32-bit (in fact, the whole setup is not needed and could be excluded from compilation,
 91 //  but that is a question for another RFE).
 92 #ifndef _LP64
 93 // Given an address p, return true if p can be used as an encoding base.
 94 //  (Some platforms have restrictions of what constitutes a valid base address).
 95 bool CompressedKlassPointers::is_valid_base(address p) {
 96   ShouldNotReachHere(); // 64-bit only
 97   return false;
 98 }
 99 
100 void CompressedKlassPointers::print_mode(outputStream* st) {
101 }
102 #endif
103