1 /*
  2  * Copyright (c) 2018, 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 #ifndef SHARE_CDS_ARCHIVEHEAPLOADER_HPP
 26 #define SHARE_CDS_ARCHIVEHEAPLOADER_HPP
 27 
 28 #include "gc/shared/gc_globals.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "memory/allStatic.hpp"
 31 #include "memory/memRegion.hpp"
 32 #include "oops/oopsHierarchy.hpp"
 33 #include "runtime/globals.hpp"
 34 #include "utilities/bitMap.hpp"
 35 #include "utilities/macros.hpp"
 36 
 37 class  FileMapInfo;
 38 struct LoadedArchiveHeapRegion;
 39 
 40 class ArchiveHeapLoader : AllStatic {
 41 public:
 42   // At runtime, the heap region in the CDS archive can be used in two different ways,
 43   // depending on the GC type:
 44   // - Mapped: (G1 only) the region is directly mapped into the Java heap
 45   // - Loaded: At VM start-up, the objects in the heap region are copied into the
 46   //           Java heap. This is easier to implement than mapping but
 47   //           slightly less efficient, as the embedded pointers need to be relocated.
 48   static bool can_use() { return can_map() || can_load(); }
 49 
 50   // Can this VM map archived heap region? Currently only G1+compressed{oops,cp}
 51   static bool can_map() {
 52     CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedClassPointers);)
 53     NOT_CDS_JAVA_HEAP(return false;)
 54   }
 55 
 56   // Can this VM load the objects from archived heap region into the heap at start-up?
 57   static bool can_load()  NOT_CDS_JAVA_HEAP_RETURN_(false);
 58   static void finish_initialization() NOT_CDS_JAVA_HEAP_RETURN;
 59   static bool is_loaded() {
 60     CDS_JAVA_HEAP_ONLY(return _is_loaded;)
 61     NOT_CDS_JAVA_HEAP(return false;)
 62   }
 63 
 64   static bool is_in_use() {
 65     return is_loaded() || is_mapped();
 66   }
 67 
 68   static ptrdiff_t mapped_heap_delta() {
 69     CDS_JAVA_HEAP_ONLY(assert(!is_loaded(), "must be"));
 70     CDS_JAVA_HEAP_ONLY(assert(_mapped_heap_relocation_initialized, "must be"));
 71     CDS_JAVA_HEAP_ONLY(return _mapped_heap_delta;)
 72     NOT_CDS_JAVA_HEAP_RETURN_(0L);
 73   }
 74 
 75   static void set_mapped() {
 76     CDS_JAVA_HEAP_ONLY(_is_mapped = true;)
 77     NOT_CDS_JAVA_HEAP_RETURN;
 78   }
 79   static bool is_mapped() {
 80     CDS_JAVA_HEAP_ONLY(return _is_mapped;)
 81     NOT_CDS_JAVA_HEAP_RETURN_(false);
 82   }
 83 
 84   // NarrowOops stored in the CDS archive may use a different encoding scheme
 85   // than CompressedOops::{base,shift} -- see FileMapInfo::map_heap_region_impl.
 86   // To decode them, do not use CompressedOops::decode_not_null. Use this
 87   // function instead.
 88   inline static oop decode_from_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
 89 
 90   // More efficient version, but works only when ArchiveHeap is mapped.
 91   inline static oop decode_from_mapped_archive(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
 92 
 93   static void patch_compressed_embedded_pointers(BitMapView bm,
 94                                                  FileMapInfo* info,
 95                                                  MemRegion region) NOT_CDS_JAVA_HEAP_RETURN;
 96 
 97   static void patch_embedded_pointers(FileMapInfo* info,
 98                                       MemRegion region, address oopmap,
 99                                       size_t oopmap_size_in_bits) NOT_CDS_JAVA_HEAP_RETURN;
100 
101   static void fixup_region() NOT_CDS_JAVA_HEAP_RETURN;
102 
103 #if INCLUDE_CDS_JAVA_HEAP
104   static void init_mapped_heap_info(address mapped_heap_bottom, ptrdiff_t delta, int dumptime_oop_shift);
105 private:
106   static bool _is_mapped;
107   static bool _is_loaded;
108 
109   // Support for loaded archived heap. These are cached values from
110   // LoadedArchiveHeapRegion's.
111   static uintptr_t _dumptime_base;
112   static uintptr_t _dumptime_top;
113   static intx _runtime_offset;
114 
115   static uintptr_t _loaded_heap_bottom;
116   static uintptr_t _loaded_heap_top;
117   static bool _loading_failed;
118 
119   // UseCompressedOops only: Used by decode_from_archive
120   static bool    _narrow_oop_base_initialized;
121   static address _narrow_oop_base;
122   static int     _narrow_oop_shift;
123 
124   // is_mapped() only: the mapped address of each region is offset by this amount from
125   // their requested address.
126   static uintptr_t _mapped_heap_bottom;
127   static ptrdiff_t _mapped_heap_delta;
128   static bool      _mapped_heap_relocation_initialized;
129 
130   static void init_narrow_oop_decoding(address base, int shift);
131   static bool init_loaded_region(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_region,
132                                  MemRegion& archive_space);
133   static bool load_heap_region_impl(FileMapInfo* mapinfo, LoadedArchiveHeapRegion* loaded_region, uintptr_t buffer);
134   static void init_loaded_heap_relocation(LoadedArchiveHeapRegion* reloc_info);
135   static void patch_native_pointers();
136   static void finish_loaded_heap();
137   static void verify_loaded_heap();
138   static void fill_failed_loaded_heap();
139 
140   static bool is_in_loaded_heap(uintptr_t o) {
141     return (_loaded_heap_bottom <= o && o < _loaded_heap_top);
142   }
143 
144   template<bool IS_MAPPED>
145   inline static oop decode_from_archive_impl(narrowOop v) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
146 
147   class PatchLoadedRegionPointers;
148   class PatchUncompressedLoadedRegionPointers;
149 
150 public:
151 
152   static bool load_heap_region(FileMapInfo* mapinfo);
153   static void assert_in_loaded_heap(uintptr_t o) {
154     assert(is_in_loaded_heap(o), "must be");
155   }
156   static oop oop_from_offset(int offset);
157 #endif // INCLUDE_CDS_JAVA_HEAP
158 
159 };
160 
161 #endif // SHARE_CDS_ARCHIVEHEAPLOADER_HPP