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