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