1 /* 2 * Copyright (c) 1997, 2023, 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_CODE_VTABLESTUBS_HPP 26 #define SHARE_CODE_VTABLESTUBS_HPP 27 28 #include "asm/macroAssembler.hpp" 29 #include "code/vmreg.hpp" 30 #include "memory/allStatic.hpp" 31 #include "utilities/checkedCast.hpp" 32 33 // A VtableStub holds an individual code stub for a pair (vtable index, #args) for either itables or vtables 34 // There's a one-to-one relationship between a VtableStub and such a pair. 35 36 // A word on VtableStub sizing: 37 // Such a vtable/itable stub consists of the instance data 38 // and an immediately following CodeBuffer. 39 // Unfortunately, the required space for the code buffer varies, depending on 40 // the setting of compile time macros (PRODUCT, ASSERT, ...) and of command line 41 // parameters. Actual data may have an influence on the size as well. 42 // 43 // A simple approximation for the VtableStub size would be to just take a value 44 // "large enough" for all circumstances - a worst case estimate. 45 // As there can exist many stubs - and they never go away - we certainly don't 46 // want to waste more code cache space than absolutely necessary. 47 // 48 // We need a different approach which, as far as possible, should be independent 49 // from or adaptive to code size variations. These variations may be caused by 50 // changed compile time or run time switches as well as by changed emitter code. 51 // 52 // Here is the idea: 53 // For the first stub we generate, we allocate a "large enough" code buffer. 54 // Once all instructions are emitted, we know the actual size of the stub. 55 // Remembering that size allows us to allocate a tightly matching code buffer 56 // for all subsequent stubs. That covers all "static variance", i.e. all variance 57 // that is due to compile time macros, command line parameters, machine capabilities, 58 // and other influences which are immutable for the life span of the vm. 59 // 60 // Life isn't always that easy. Code size may depend on actual data, "load constant" 61 // being an example for that. All code segments with such "dynamic variance" require 62 // additional care. We need to know or estimate the worst case code size for each 63 // such segment. With that knowledge, we can maintain a "slop counter" in the 64 // platform-specific stub emitters. It accumulates the difference between worst-case 65 // and actual code size. When the stub is fully generated, the actual stub size is 66 // adjusted (increased) by the slop counter value. 67 // 68 // As a result, we allocate all but the first code buffers with the same, tightly matching size. 69 // 70 71 // VtableStubs creates the code stubs for compiled calls through vtables. 72 // There is one stub per (vtable index, args_size) pair, and the stubs are 73 // never deallocated. They don't need to be GCed because they contain no oops. 74 class VtableStub; 75 76 class VtableStubs : AllStatic { 77 public: // N must be public (some compilers need this for _table) 78 enum { 79 N = 256, // size of stub table; must be power of two 80 mask = N - 1 81 }; 82 83 static_assert(is_power_of_2((int)N), "N must be a power of 2"); 84 85 private: 86 friend class VtableStub; 87 static VtableStub* volatile _table[N]; // table of existing stubs 88 static int _vtab_stub_size; // current size estimate for vtable stub (quasi-constant) 89 static int _itab_stub_size; // current size estimate for itable stub (quasi-constant) 90 91 static VtableStub* create_vtable_stub(int vtable_index, bool caller_is_c1); 92 static VtableStub* create_itable_stub(int vtable_index, bool caller_is_c1); 93 static VtableStub* lookup (bool is_vtable_stub, int vtable_index, bool caller_is_c1); 94 static void enter (bool is_vtable_stub, int vtable_index, bool caller_is_c1, VtableStub* s); 95 static inline uint hash (bool is_vtable_stub, int vtable_index, bool caller_is_c1); 96 static address find_stub (bool is_vtable_stub, int vtable_index, bool caller_is_c1); 97 static void bookkeeping(MacroAssembler* masm, outputStream* out, VtableStub* s, 98 address npe_addr, address ame_addr, bool is_vtable_stub, 99 int index, int slop_bytes, int index_dependent_slop); 100 static int code_size_limit(bool is_vtable_stub); 101 static void check_and_set_size_limit(bool is_vtable_stub, 102 int code_size, 103 int padding); 104 105 public: 106 static address find_vtable_stub(int vtable_index, bool caller_is_c1) { return find_stub(true, vtable_index, caller_is_c1); } 107 static address find_itable_stub(int itable_index, bool caller_is_c1) { return find_stub(false, itable_index, caller_is_c1); } 108 109 static VtableStub* entry_point(address pc); // vtable stub entry point for a pc 110 static bool contains(address pc); // is pc within any stub? 111 static VtableStub* stub_containing(address pc); // stub containing pc or nullptr 112 static void initialize(); 113 static void vtable_stub_do(void f(VtableStub*)); // iterates over all vtable stubs 114 }; 115 116 117 class VtableStub { 118 private: 119 friend class VtableStubs; 120 121 static address _chunk; // For allocation 122 static address _chunk_end; // For allocation 123 static VMReg _receiver_location; // Where to find receiver 124 125 VtableStub* _next; // Pointer to next entry in hash table 126 const short _index; // vtable index 127 short _ame_offset; // Where an AbstractMethodError might occur 128 short _npe_offset; // Where a NullPointerException might occur 129 bool _is_vtable_stub; // True if vtable stub, false, is itable stub 130 bool _caller_is_c1; // True if this is for a caller compiled by C1, 131 // which doesn't scalarize parameters. 132 /* code follows here */ // The vtableStub code 133 134 void* operator new(size_t size, int code_size) throw(); 135 136 VtableStub(bool is_vtable_stub, short index, bool caller_is_c1) 137 : _next(nullptr), _index(index), _ame_offset(-1), _npe_offset(-1), 138 _is_vtable_stub(is_vtable_stub), _caller_is_c1(caller_is_c1) {} 139 VtableStub* next() const { return _next; } 140 int index() const { return _index; } 141 static VMReg receiver_location() { return _receiver_location; } 142 void set_next(VtableStub* n) { _next = n; } 143 144 public: 145 address code_begin() const { return (address)(this + 1); } 146 address code_end() const { return code_begin() + VtableStubs::code_size_limit(_is_vtable_stub); } 147 address entry_point() const { return code_begin(); } 148 static int entry_offset() { return sizeof(class VtableStub); } 149 150 bool matches(bool is_vtable_stub, int index, bool caller_is_c1) const { 151 return _index == index && _is_vtable_stub == is_vtable_stub && _caller_is_c1 == caller_is_c1; 152 } 153 bool contains(address pc) const { return code_begin() <= pc && pc < code_end(); } 154 155 private: 156 void set_exception_points(address npe_addr, address ame_addr) { 157 _npe_offset = checked_cast<short>(npe_addr - code_begin()); 158 _ame_offset = checked_cast<short>(ame_addr - code_begin()); 159 assert(is_abstract_method_error(ame_addr), "offset must be correct"); 160 assert(is_null_pointer_exception(npe_addr), "offset must be correct"); 161 assert(!is_abstract_method_error(npe_addr), "offset must be correct"); 162 assert(!is_null_pointer_exception(ame_addr), "offset must be correct"); 163 } 164 165 // platform-dependent routines 166 static int pd_code_alignment(); 167 // CNC: Removed because vtable stubs are now made with an ideal graph 168 // static bool pd_disregard_arg_size(); 169 170 static void align_chunk() { 171 uintptr_t off = (uintptr_t)( _chunk + sizeof(VtableStub) ) % pd_code_alignment(); 172 if (off != 0) _chunk += pd_code_alignment() - off; 173 } 174 175 public: 176 // Query 177 bool is_itable_stub() { return !_is_vtable_stub; } 178 bool is_vtable_stub() { return _is_vtable_stub; } 179 bool caller_is_c1() { return _caller_is_c1; } 180 bool is_abstract_method_error(address epc) { return epc == code_begin()+_ame_offset; } 181 bool is_null_pointer_exception(address epc) { return epc == code_begin()+_npe_offset; } 182 183 void print_on(outputStream* st) const; 184 void print() const; 185 186 }; 187 188 #endif // SHARE_CODE_VTABLESTUBS_HPP