Fri, 08 Nov 2013 01:13:11 -0800
8023037: Race between ciEnv::register_method and nmethod::make_not_entrant_or_zombie
Reviewed-by: kvn, iveresov
duke@435 | 1 | /* |
coleenp@4037 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_CODE_COMPILEDIC_HPP |
stefank@2314 | 26 | #define SHARE_VM_CODE_COMPILEDIC_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "interpreter/linkResolver.hpp" |
coleenp@4037 | 29 | #include "oops/compiledICHolder.hpp" |
stefank@2314 | 30 | #ifdef TARGET_ARCH_x86 |
stefank@2314 | 31 | # include "nativeInst_x86.hpp" |
stefank@2314 | 32 | #endif |
stefank@2314 | 33 | #ifdef TARGET_ARCH_sparc |
stefank@2314 | 34 | # include "nativeInst_sparc.hpp" |
stefank@2314 | 35 | #endif |
stefank@2314 | 36 | #ifdef TARGET_ARCH_zero |
stefank@2314 | 37 | # include "nativeInst_zero.hpp" |
stefank@2314 | 38 | #endif |
bobv@2508 | 39 | #ifdef TARGET_ARCH_arm |
bobv@2508 | 40 | # include "nativeInst_arm.hpp" |
bobv@2508 | 41 | #endif |
bobv@2508 | 42 | #ifdef TARGET_ARCH_ppc |
bobv@2508 | 43 | # include "nativeInst_ppc.hpp" |
bobv@2508 | 44 | #endif |
stefank@2314 | 45 | |
duke@435 | 46 | //----------------------------------------------------------------------------- |
duke@435 | 47 | // The CompiledIC represents a compiled inline cache. |
duke@435 | 48 | // |
duke@435 | 49 | // In order to make patching of the inline cache MT-safe, we only allow the following |
duke@435 | 50 | // transitions (when not at a safepoint): |
duke@435 | 51 | // |
duke@435 | 52 | // |
duke@435 | 53 | // [1] --<-- Clean -->--- [1] |
duke@435 | 54 | // / (null) \ |
duke@435 | 55 | // / \ /-<-\ |
duke@435 | 56 | // / [2] \ / \ |
duke@435 | 57 | // Interpreted ---------> Monomorphic | [3] |
coleenp@4037 | 58 | // (CompiledICHolder*) (Klass*) | |
duke@435 | 59 | // \ / \ / |
duke@435 | 60 | // [4] \ / [4] \->-/ |
duke@435 | 61 | // \->- Megamorphic -<-/ |
coleenp@4037 | 62 | // (Method*) |
duke@435 | 63 | // |
duke@435 | 64 | // The text in paranteses () refere to the value of the inline cache receiver (mov instruction) |
duke@435 | 65 | // |
duke@435 | 66 | // The numbers in square brackets refere to the kind of transition: |
duke@435 | 67 | // [1]: Initial fixup. Receiver it found from debug information |
duke@435 | 68 | // [2]: Compilation of a method |
coleenp@4037 | 69 | // [3]: Recompilation of a method (note: only entry is changed. The Klass* must stay the same) |
duke@435 | 70 | // [4]: Inline cache miss. We go directly to megamorphic call. |
duke@435 | 71 | // |
duke@435 | 72 | // The class automatically inserts transition stubs (using the InlineCacheBuffer) when an MT-unsafe |
duke@435 | 73 | // transition is made to a stub. |
duke@435 | 74 | // |
duke@435 | 75 | class CompiledIC; |
coleenp@4037 | 76 | class ICStub; |
duke@435 | 77 | |
coleenp@4037 | 78 | class CompiledICInfo : public StackObj { |
duke@435 | 79 | private: |
duke@435 | 80 | address _entry; // entry point for call |
coleenp@4037 | 81 | void* _cached_value; // Value of cached_value (either in stub or inline cache) |
coleenp@4037 | 82 | bool _is_icholder; // Is the cached value a CompiledICHolder* |
duke@435 | 83 | bool _is_optimized; // it is an optimized virtual call (i.e., can be statically bound) |
duke@435 | 84 | bool _to_interpreter; // Call it to interpreter |
coleenp@4037 | 85 | bool _release_icholder; |
duke@435 | 86 | public: |
duke@435 | 87 | address entry() const { return _entry; } |
coleenp@4037 | 88 | Metadata* cached_metadata() const { assert(!_is_icholder, ""); return (Metadata*)_cached_value; } |
coleenp@4037 | 89 | CompiledICHolder* claim_cached_icholder() { |
coleenp@4037 | 90 | assert(_is_icholder, ""); |
coleenp@4037 | 91 | assert(_cached_value != NULL, "must be non-NULL"); |
coleenp@4037 | 92 | _release_icholder = false; |
coleenp@4037 | 93 | CompiledICHolder* icholder = (CompiledICHolder*)_cached_value; |
coleenp@4037 | 94 | icholder->claim(); |
coleenp@4037 | 95 | return icholder; |
coleenp@4037 | 96 | } |
duke@435 | 97 | bool is_optimized() const { return _is_optimized; } |
coleenp@4037 | 98 | bool to_interpreter() const { return _to_interpreter; } |
coleenp@4037 | 99 | |
coleenp@4037 | 100 | void set_compiled_entry(address entry, Klass* klass, bool is_optimized) { |
coleenp@4037 | 101 | _entry = entry; |
coleenp@4037 | 102 | _cached_value = (void*)klass; |
coleenp@4037 | 103 | _to_interpreter = false; |
coleenp@4037 | 104 | _is_icholder = false; |
coleenp@4037 | 105 | _is_optimized = is_optimized; |
coleenp@4037 | 106 | _release_icholder = false; |
coleenp@4037 | 107 | } |
coleenp@4037 | 108 | |
coleenp@4037 | 109 | void set_interpreter_entry(address entry, Method* method) { |
coleenp@4037 | 110 | _entry = entry; |
coleenp@4037 | 111 | _cached_value = (void*)method; |
coleenp@4037 | 112 | _to_interpreter = true; |
coleenp@4037 | 113 | _is_icholder = false; |
coleenp@4037 | 114 | _is_optimized = true; |
coleenp@4037 | 115 | _release_icholder = false; |
coleenp@4037 | 116 | } |
coleenp@4037 | 117 | |
coleenp@4037 | 118 | void set_icholder_entry(address entry, CompiledICHolder* icholder) { |
coleenp@4037 | 119 | _entry = entry; |
coleenp@4037 | 120 | _cached_value = (void*)icholder; |
coleenp@4037 | 121 | _to_interpreter = true; |
coleenp@4037 | 122 | _is_icholder = true; |
coleenp@4037 | 123 | _is_optimized = false; |
coleenp@4037 | 124 | _release_icholder = true; |
coleenp@4037 | 125 | } |
coleenp@4037 | 126 | |
coleenp@4037 | 127 | CompiledICInfo(): _entry(NULL), _cached_value(NULL), _is_icholder(false), |
coleenp@4037 | 128 | _to_interpreter(false), _is_optimized(false), _release_icholder(false) { |
coleenp@4037 | 129 | } |
coleenp@4037 | 130 | ~CompiledICInfo() { |
coleenp@4037 | 131 | // In rare cases the info is computed but not used, so release any |
coleenp@4037 | 132 | // CompiledICHolder* that was created |
coleenp@4037 | 133 | if (_release_icholder) { |
coleenp@4037 | 134 | assert(_is_icholder, "must be"); |
coleenp@4037 | 135 | CompiledICHolder* icholder = (CompiledICHolder*)_cached_value; |
coleenp@4037 | 136 | icholder->claim(); |
coleenp@4037 | 137 | delete icholder; |
coleenp@4037 | 138 | } |
coleenp@4037 | 139 | } |
duke@435 | 140 | }; |
duke@435 | 141 | |
duke@435 | 142 | class CompiledIC: public ResourceObj { |
duke@435 | 143 | friend class InlineCacheBuffer; |
duke@435 | 144 | friend class ICStub; |
duke@435 | 145 | |
duke@435 | 146 | |
duke@435 | 147 | private: |
duke@435 | 148 | NativeCall* _ic_call; // the call instruction |
coleenp@4037 | 149 | NativeMovConstReg* _value; // patchable value cell for this IC |
duke@435 | 150 | bool _is_optimized; // an optimized virtual call (i.e., no compiled IC) |
duke@435 | 151 | |
coleenp@4037 | 152 | CompiledIC(nmethod* nm, NativeCall* ic_call); |
coleenp@4037 | 153 | |
coleenp@4037 | 154 | static bool is_icholder_entry(address entry); |
duke@435 | 155 | |
duke@435 | 156 | // low-level inline-cache manipulation. Cannot be accessed directly, since it might not be MT-safe |
duke@435 | 157 | // to change an inline-cache. These changes the underlying inline-cache directly. They *newer* make |
duke@435 | 158 | // changes to a transition stub. |
coleenp@4037 | 159 | void internal_set_ic_destination(address entry_point, bool is_icstub, void* cache, bool is_icholder); |
coleenp@4037 | 160 | void set_ic_destination(ICStub* stub); |
coleenp@4037 | 161 | void set_ic_destination(address entry_point) { |
coleenp@4037 | 162 | assert(_is_optimized, "use set_ic_destination_and_value instead"); |
coleenp@4037 | 163 | internal_set_ic_destination(entry_point, false, NULL, false); |
coleenp@4037 | 164 | } |
coleenp@4037 | 165 | // This only for use by ICStubs where the type of the value isn't known |
coleenp@4037 | 166 | void set_ic_destination_and_value(address entry_point, void* value) { |
coleenp@4037 | 167 | internal_set_ic_destination(entry_point, false, value, is_icholder_entry(entry_point)); |
coleenp@4037 | 168 | } |
coleenp@4037 | 169 | void set_ic_destination_and_value(address entry_point, Metadata* value) { |
coleenp@4037 | 170 | internal_set_ic_destination(entry_point, false, value, false); |
coleenp@4037 | 171 | } |
coleenp@4037 | 172 | void set_ic_destination_and_value(address entry_point, CompiledICHolder* value) { |
coleenp@4037 | 173 | internal_set_ic_destination(entry_point, false, value, true); |
coleenp@4037 | 174 | } |
duke@435 | 175 | |
duke@435 | 176 | // Reads the location of the transition stub. This will fail with an assertion, if no transition stub is |
duke@435 | 177 | // associated with the inline cache. |
duke@435 | 178 | address stub_address() const; |
duke@435 | 179 | bool is_in_transition_state() const; // Use InlineCacheBuffer |
duke@435 | 180 | |
duke@435 | 181 | public: |
duke@435 | 182 | // conversion (machine PC to CompiledIC*) |
coleenp@4037 | 183 | friend CompiledIC* CompiledIC_before(nmethod* nm, address return_addr); |
coleenp@4037 | 184 | friend CompiledIC* CompiledIC_at(nmethod* nm, address call_site); |
duke@435 | 185 | friend CompiledIC* CompiledIC_at(Relocation* call_site); |
duke@435 | 186 | |
coleenp@4037 | 187 | // This is used to release CompiledICHolder*s from nmethods that |
coleenp@4037 | 188 | // are about to be freed. The callsite might contain other stale |
coleenp@4037 | 189 | // values of other kinds so it must be careful. |
coleenp@4037 | 190 | static void cleanup_call_site(virtual_call_Relocation* call_site); |
coleenp@4037 | 191 | static bool is_icholder_call_site(virtual_call_Relocation* call_site); |
coleenp@4037 | 192 | |
coleenp@4037 | 193 | // Return the cached_metadata/destination associated with this inline cache. If the cache currently points |
duke@435 | 194 | // to a transition stub, it will read the values from the transition stub. |
coleenp@4037 | 195 | void* cached_value() const; |
coleenp@4037 | 196 | CompiledICHolder* cached_icholder() const { |
coleenp@4037 | 197 | assert(is_icholder_call(), "must be"); |
coleenp@4037 | 198 | return (CompiledICHolder*) cached_value(); |
coleenp@4037 | 199 | } |
coleenp@4037 | 200 | Metadata* cached_metadata() const { |
coleenp@4037 | 201 | assert(!is_icholder_call(), "must be"); |
coleenp@4037 | 202 | return (Metadata*) cached_value(); |
coleenp@4037 | 203 | } |
coleenp@4037 | 204 | |
duke@435 | 205 | address ic_destination() const; |
duke@435 | 206 | |
duke@435 | 207 | bool is_optimized() const { return _is_optimized; } |
duke@435 | 208 | |
duke@435 | 209 | // State |
duke@435 | 210 | bool is_clean() const; |
duke@435 | 211 | bool is_megamorphic() const; |
duke@435 | 212 | bool is_call_to_compiled() const; |
duke@435 | 213 | bool is_call_to_interpreted() const; |
duke@435 | 214 | |
coleenp@4037 | 215 | bool is_icholder_call() const; |
coleenp@4037 | 216 | |
duke@435 | 217 | address end_of_call() { return _ic_call->return_address(); } |
duke@435 | 218 | |
duke@435 | 219 | // MT-safe patching of inline caches. Note: Only safe to call is_xxx when holding the CompiledIC_ock |
duke@435 | 220 | // so you are guaranteed that no patching takes place. The same goes for verify. |
duke@435 | 221 | // |
duke@435 | 222 | // Note: We do not provide any direct access to the stub code, to prevent parts of the code |
duke@435 | 223 | // to manipulate the inline cache in MT-unsafe ways. |
duke@435 | 224 | // |
duke@435 | 225 | // They all takes a TRAP argument, since they can cause a GC if the inline-cache buffer is full. |
duke@435 | 226 | // |
duke@435 | 227 | void set_to_clean(); // Can only be called during a safepoint operation |
coleenp@4037 | 228 | void set_to_monomorphic(CompiledICInfo& info); |
anoll@5762 | 229 | |
anoll@5762 | 230 | // Returns true if successful and false otherwise. The call can fail if memory |
anoll@5762 | 231 | // allocation in the code cache fails. |
anoll@5762 | 232 | bool set_to_megamorphic(CallInfo* call_info, Bytecodes::Code bytecode, TRAPS); |
duke@435 | 233 | |
duke@435 | 234 | static void compute_monomorphic_entry(methodHandle method, KlassHandle receiver_klass, |
duke@435 | 235 | bool is_optimized, bool static_bound, CompiledICInfo& info, TRAPS); |
duke@435 | 236 | |
duke@435 | 237 | // Location |
duke@435 | 238 | address instruction_address() const { return _ic_call->instruction_address(); } |
duke@435 | 239 | |
duke@435 | 240 | // Misc |
duke@435 | 241 | void print() PRODUCT_RETURN; |
duke@435 | 242 | void print_compiled_ic() PRODUCT_RETURN; |
duke@435 | 243 | void verify() PRODUCT_RETURN; |
duke@435 | 244 | }; |
duke@435 | 245 | |
coleenp@4037 | 246 | inline CompiledIC* CompiledIC_before(nmethod* nm, address return_addr) { |
coleenp@4037 | 247 | CompiledIC* c_ic = new CompiledIC(nm, nativeCall_before(return_addr)); |
duke@435 | 248 | c_ic->verify(); |
duke@435 | 249 | return c_ic; |
duke@435 | 250 | } |
duke@435 | 251 | |
coleenp@4037 | 252 | inline CompiledIC* CompiledIC_at(nmethod* nm, address call_site) { |
coleenp@4037 | 253 | CompiledIC* c_ic = new CompiledIC(nm, nativeCall_at(call_site)); |
duke@435 | 254 | c_ic->verify(); |
duke@435 | 255 | return c_ic; |
duke@435 | 256 | } |
duke@435 | 257 | |
duke@435 | 258 | inline CompiledIC* CompiledIC_at(Relocation* call_site) { |
coleenp@4037 | 259 | assert(call_site->type() == relocInfo::virtual_call_type || |
coleenp@4037 | 260 | call_site->type() == relocInfo::opt_virtual_call_type, "wrong reloc. info"); |
coleenp@4037 | 261 | CompiledIC* c_ic = new CompiledIC(call_site->code(), nativeCall_at(call_site->addr())); |
duke@435 | 262 | c_ic->verify(); |
duke@435 | 263 | return c_ic; |
duke@435 | 264 | } |
duke@435 | 265 | |
duke@435 | 266 | |
duke@435 | 267 | //----------------------------------------------------------------------------- |
duke@435 | 268 | // The CompiledStaticCall represents a call to a static method in the compiled |
duke@435 | 269 | // |
duke@435 | 270 | // Transition diagram of a static call site is somewhat simpler than for an inlined cache: |
duke@435 | 271 | // |
duke@435 | 272 | // |
duke@435 | 273 | // -----<----- Clean ----->----- |
duke@435 | 274 | // / \ |
duke@435 | 275 | // / \ |
duke@435 | 276 | // compilled code <------------> interpreted code |
duke@435 | 277 | // |
duke@435 | 278 | // Clean: Calls directly to runtime method for fixup |
duke@435 | 279 | // Compiled code: Calls directly to compiled code |
coleenp@4037 | 280 | // Interpreted code: Calls to stub that set Method* reference |
duke@435 | 281 | // |
duke@435 | 282 | // |
duke@435 | 283 | class CompiledStaticCall; |
duke@435 | 284 | |
duke@435 | 285 | class StaticCallInfo { |
duke@435 | 286 | private: |
duke@435 | 287 | address _entry; // Entrypoint |
duke@435 | 288 | methodHandle _callee; // Callee (used when calling interpreter) |
duke@435 | 289 | bool _to_interpreter; // call to interpreted method (otherwise compiled) |
duke@435 | 290 | |
duke@435 | 291 | friend class CompiledStaticCall; |
duke@435 | 292 | public: |
duke@435 | 293 | address entry() const { return _entry; } |
duke@435 | 294 | methodHandle callee() const { return _callee; } |
duke@435 | 295 | }; |
duke@435 | 296 | |
duke@435 | 297 | |
duke@435 | 298 | class CompiledStaticCall: public NativeCall { |
duke@435 | 299 | friend class CompiledIC; |
duke@435 | 300 | |
duke@435 | 301 | // Also used by CompiledIC |
duke@435 | 302 | void set_to_interpreted(methodHandle callee, address entry); |
duke@435 | 303 | bool is_optimized_virtual(); |
duke@435 | 304 | |
duke@435 | 305 | public: |
duke@435 | 306 | friend CompiledStaticCall* compiledStaticCall_before(address return_addr); |
duke@435 | 307 | friend CompiledStaticCall* compiledStaticCall_at(address native_call); |
duke@435 | 308 | friend CompiledStaticCall* compiledStaticCall_at(Relocation* call_site); |
duke@435 | 309 | |
dlong@5000 | 310 | // Code |
dlong@5000 | 311 | static void emit_to_interp_stub(CodeBuffer &cbuf); |
dlong@5000 | 312 | static int to_interp_stub_size(); |
dlong@5000 | 313 | static int reloc_to_interp_stub(); |
dlong@5000 | 314 | |
duke@435 | 315 | // State |
duke@435 | 316 | bool is_clean() const; |
duke@435 | 317 | bool is_call_to_compiled() const; |
duke@435 | 318 | bool is_call_to_interpreted() const; |
duke@435 | 319 | |
duke@435 | 320 | // Clean static call (will force resolving on next use) |
duke@435 | 321 | void set_to_clean(); |
duke@435 | 322 | |
duke@435 | 323 | // Set state. The entry must be the same, as computed by compute_entry. |
duke@435 | 324 | // Computation and setting is split up, since the actions are separate during |
duke@435 | 325 | // a OptoRuntime::resolve_xxx. |
duke@435 | 326 | void set(const StaticCallInfo& info); |
duke@435 | 327 | |
duke@435 | 328 | // Compute entry point given a method |
duke@435 | 329 | static void compute_entry(methodHandle m, StaticCallInfo& info); |
duke@435 | 330 | |
duke@435 | 331 | // Stub support |
duke@435 | 332 | address find_stub(); |
duke@435 | 333 | static void set_stub_to_clean(static_stub_Relocation* static_stub); |
duke@435 | 334 | |
duke@435 | 335 | // Misc. |
duke@435 | 336 | void print() PRODUCT_RETURN; |
duke@435 | 337 | void verify() PRODUCT_RETURN; |
duke@435 | 338 | }; |
duke@435 | 339 | |
duke@435 | 340 | |
duke@435 | 341 | inline CompiledStaticCall* compiledStaticCall_before(address return_addr) { |
duke@435 | 342 | CompiledStaticCall* st = (CompiledStaticCall*)nativeCall_before(return_addr); |
duke@435 | 343 | st->verify(); |
duke@435 | 344 | return st; |
duke@435 | 345 | } |
duke@435 | 346 | |
duke@435 | 347 | inline CompiledStaticCall* compiledStaticCall_at(address native_call) { |
duke@435 | 348 | CompiledStaticCall* st = (CompiledStaticCall*)native_call; |
duke@435 | 349 | st->verify(); |
duke@435 | 350 | return st; |
duke@435 | 351 | } |
duke@435 | 352 | |
duke@435 | 353 | inline CompiledStaticCall* compiledStaticCall_at(Relocation* call_site) { |
duke@435 | 354 | return compiledStaticCall_at(call_site->addr()); |
duke@435 | 355 | } |
stefank@2314 | 356 | |
stefank@2314 | 357 | #endif // SHARE_VM_CODE_COMPILEDIC_HPP |