1 /* 2 * Copyright (c) 2012, 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_METASPACESHARED_HPP 26 #define SHARE_CDS_METASPACESHARED_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/memRegion.hpp" 30 #include "memory/virtualspace.hpp" 31 #include "oops/oop.hpp" 32 #include "utilities/macros.hpp" 33 34 class ArchiveBuilder; 35 class ArchiveHeapInfo; 36 class FileMapInfo; 37 class Method; 38 class outputStream; 39 class SerializeClosure; 40 41 template<class E> class Array; 42 template<class E> class GrowableArray; 43 44 enum MapArchiveResult { 45 MAP_ARCHIVE_SUCCESS, 46 MAP_ARCHIVE_MMAP_FAILURE, 47 MAP_ARCHIVE_OTHER_FAILURE 48 }; 49 50 class StaticArchiveBuilder; 51 52 // Class Data Sharing Support 53 class MetaspaceShared : AllStatic { 54 static ReservedSpace _symbol_rs; // used only during -Xshare:dump 55 static VirtualSpace _symbol_vs; // used only during -Xshare:dump 56 static bool _archive_loading_failed; 57 static bool _remapped_readwrite; 58 static void* _shared_metaspace_static_top; 59 static intx _relocation_delta; 60 static char* _requested_base_address; 61 static Array<Method*>* _archived_method_handle_intrinsics; 62 static bool _use_optimized_module_handling; 63 64 public: 65 enum { 66 // core archive spaces 67 rw = 0, // read-write shared space 68 ro = 1, // read-only shared space 69 bm = 2, // relocation bitmaps (freed after file mapping is finished) 70 hp = 3, // heap region 71 cc = 4, // cached code 72 num_core_region = 2, // rw and ro 73 n_regions = 5 // total number of regions 74 }; 75 76 static void prepare_for_dumping() NOT_CDS_RETURN; 77 static void preload_and_dump() NOT_CDS_RETURN; 78 #ifdef _LP64 79 static void adjust_heap_sizes_for_dumping() NOT_CDS_JAVA_HEAP_RETURN; 80 #endif 81 82 private: 83 static void preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) NOT_CDS_RETURN; 84 static void preload_classes(TRAPS) NOT_CDS_RETURN; 85 86 public: 87 static Symbol* symbol_rs_base() { 88 return (Symbol*)_symbol_rs.base(); 89 } 90 91 static void initialize_for_static_dump() NOT_CDS_RETURN; 92 static void open_static_archive() NOT_CDS_RETURN; 93 static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN; 94 static void post_initialize(TRAPS) NOT_CDS_RETURN; 95 96 static void print_on(outputStream* st); 97 98 static void set_archive_loading_failed() { 99 _archive_loading_failed = true; 100 } 101 102 static void initialize_shared_spaces() NOT_CDS_RETURN; 103 104 // Return true if given address is in the shared metaspace regions (i.e., excluding the 105 // mapped heap region.) 106 static bool is_in_shared_metaspace(const void* p) { 107 return MetaspaceObj::is_shared((const MetaspaceObj*)p); 108 } 109 110 static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN; 111 112 static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false); 113 static bool is_shared_static(void* p) NOT_CDS_RETURN_(false); 114 115 static void unrecoverable_loading_error(const char* message = nullptr); 116 static void unrecoverable_writing_error(const char* message = nullptr); 117 118 static void make_method_handle_intrinsics_shareable() NOT_CDS_RETURN; 119 static void write_method_handle_intrinsics() NOT_CDS_RETURN; 120 static Array<Method*>* archived_method_handle_intrinsics() { return _archived_method_handle_intrinsics; } 121 static void serialize(SerializeClosure* sc) NOT_CDS_RETURN; 122 123 // JVM/TI RedefineClasses() support: 124 // Remap the shared readonly space to shared readwrite, private if 125 // sharing is enabled. Simply returns true if sharing is not enabled 126 // or if the remapping has already been done by a prior call. 127 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 128 static bool remapped_readwrite() { 129 CDS_ONLY(return _remapped_readwrite); 130 NOT_CDS(return false); 131 } 132 133 static bool try_link_class(JavaThread* current, InstanceKlass* ik); 134 static void link_shared_classes(bool jcmd_request, TRAPS) NOT_CDS_RETURN; 135 static bool may_be_eagerly_linked(InstanceKlass* ik) NOT_CDS_RETURN_(false); 136 137 #if INCLUDE_CDS 138 // Alignment for the 2 core CDS regions (RW/RO) only. 139 // (Heap region alignments are decided by GC). 140 static size_t core_region_alignment(); 141 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik); 142 // print loaded classes names to file. 143 static void dump_loaded_classes(const char* file_name, TRAPS); 144 #endif 145 146 // Allocate a block of memory from the temporary "symbol" region. 147 static char* symbol_space_alloc(size_t num_bytes); 148 149 // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump. 150 // Both the base/top archives are written using this as their base address. 151 // 152 // During static dump: _requested_base_address == SharedBaseAddress. 153 // 154 // During dynamic dump: _requested_base_address is not always the same as SharedBaseAddress: 155 // - SharedBaseAddress is used for *reading the base archive*. I.e., CompactHashtable uses 156 // it to convert offsets to pointers to Symbols in the base archive. 157 // The base archive may be mapped to an OS-selected address due to ASLR. E.g., 158 // you may have SharedBaseAddress == 0x00ff123400000000. 159 // - _requested_base_address is used for *writing the output archive*. It's usually 160 // 0x800000000 (unless it was set by -XX:SharedBaseAddress during -Xshare:dump). 161 static char* requested_base_address() { 162 return _requested_base_address; 163 } 164 165 // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR. 166 static intx relocation_delta() { return _relocation_delta; } 167 168 static bool use_windows_memory_mapping() { 169 const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true)); 170 //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc. 171 return is_windows; 172 } 173 174 // Can we skip some expensive operations related to modules? 175 static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); } 176 static void disable_optimized_module_handling() { _use_optimized_module_handling = false; } 177 178 private: 179 static void read_extra_data(JavaThread* current, const char* filename) NOT_CDS_RETURN; 180 static void write_static_archive(ArchiveBuilder* builder, FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info); 181 static void fork_and_dump_final_static_archive(); 182 static FileMapInfo* open_dynamic_archive(); 183 // use_requested_addr: If true (default), attempt to map at the address the 184 static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo, 185 bool use_requested_addr); 186 static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo, 187 FileMapInfo* dynamic_mapinfo, 188 bool use_archive_base_addr, 189 ReservedSpace& total_space_rs, 190 ReservedSpace& archive_space_rs, 191 ReservedSpace& class_space_rs); 192 static void release_reserved_spaces(ReservedSpace& total_space_rs, 193 ReservedSpace& archive_space_rs, 194 ReservedSpace& class_space_rs); 195 static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs); 196 static void unmap_archive(FileMapInfo* mapinfo); 197 static void get_default_classlist(char* default_classlist, const size_t buf_size); 198 }; 199 #endif // SHARE_CDS_METASPACESHARED_HPP