1 /* 2 * Copyright 1998-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 // CodeBlob - superclass for all entries in the CodeCache. 26 // 27 // Suptypes are: 28 // nmethod : Compiled Java methods (include method that calls to native code) 29 // RuntimeStub : Call to VM runtime methods 30 // DeoptimizationBlob : Used for deoptimizatation 31 // ExceptionBlob : Used for stack unrolling 32 // SafepointBlob : Used to handle illegal instruction exceptions 33 // 34 // 35 // Layout: 36 // - header 37 // - relocation 38 // - instruction space 39 // - data space 40 class DeoptimizationBlob; 41 42 class CodeBlob VALUE_OBJ_CLASS_SPEC { 43 44 friend class VMStructs; 45 46 private: 47 const char* _name; 48 int _size; // total size of CodeBlob in bytes 49 int _header_size; // size of header (depends on subclass) 50 int _relocation_size; // size of relocation 51 int _instructions_offset; // offset to where instructions region begins 52 int _frame_complete_offset; // instruction offsets in [0.._frame_complete_offset) have 53 // not finished setting up their frame. Beware of pc's in 54 // that range. There is a similar range(s) on returns 55 // which we don't detect. 56 int _data_offset; // offset to where data region begins 57 int _oops_offset; // offset to where embedded oop table begins (inside data) 58 int _oops_length; // number of embedded oops 59 int _frame_size; // size of stack frame 60 OopMapSet* _oop_maps; // OopMap for this CodeBlob 61 CodeComments _comments; 62 63 friend class OopRecorder; 64 65 void fix_oop_relocations(address begin, address end, bool initialize_immediates); 66 inline void initialize_immediate_oop(oop* dest, jobject handle); 67 68 public: 69 // Returns the space needed for CodeBlob 70 static unsigned int allocation_size(CodeBuffer* cb, int header_size); 71 72 // Creation 73 // a) simple CodeBlob 74 // frame_complete is the offset from the beginning of the instructions 75 // to where the frame setup (from stackwalk viewpoint) is complete. 76 CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size); 77 78 // b) full CodeBlob 79 CodeBlob( 80 const char* name, 81 CodeBuffer* cb, 82 int header_size, 83 int size, 84 int frame_complete, 85 int frame_size, 86 OopMapSet* oop_maps 87 ); 88 89 // Deletion 90 void flush(); 91 92 // Typing 93 virtual bool is_buffer_blob() const { return false; } 94 virtual bool is_nmethod() const { return false; } 95 virtual bool is_runtime_stub() const { return false; } 96 virtual bool is_deoptimization_stub() const { return false; } 97 virtual bool is_uncommon_trap_stub() const { return false; } 98 virtual bool is_exception_stub() const { return false; } 99 virtual bool is_safepoint_stub() const { return false; } 100 virtual bool is_adapter_blob() const { return false; } 101 102 virtual bool is_compiled_by_c2() const { return false; } 103 virtual bool is_compiled_by_c1() const { return false; } 104 105 // Casting 106 nmethod* as_nmethod_or_null() { return is_nmethod() ? (nmethod*) this : NULL; } 107 108 // Boundaries 109 address header_begin() const { return (address) this; } 110 address header_end() const { return ((address) this) + _header_size; }; 111 relocInfo* relocation_begin() const { return (relocInfo*) header_end(); }; 112 relocInfo* relocation_end() const { return (relocInfo*)(header_end() + _relocation_size); } 113 address instructions_begin() const { return (address) header_begin() + _instructions_offset; } 114 address instructions_end() const { return (address) header_begin() + _data_offset; } 115 address data_begin() const { return (address) header_begin() + _data_offset; } 116 address data_end() const { return (address) header_begin() + _size; } 117 oop* oops_begin() const { return (oop*) (header_begin() + _oops_offset); } 118 oop* oops_end() const { return oops_begin() + _oops_length; } 119 120 // Offsets 121 int relocation_offset() const { return _header_size; } 122 int instructions_offset() const { return _instructions_offset; } 123 int data_offset() const { return _data_offset; } 124 int oops_offset() const { return _oops_offset; } 125 126 // Sizes 127 int size() const { return _size; } 128 int header_size() const { return _header_size; } 129 int relocation_size() const { return (address) relocation_end() - (address) relocation_begin(); } 130 int instructions_size() const { return instructions_end() - instructions_begin(); } 131 int data_size() const { return data_end() - data_begin(); } 132 int oops_size() const { return (address) oops_end() - (address) oops_begin(); } 133 134 // Containment 135 bool blob_contains(address addr) const { return header_begin() <= addr && addr < data_end(); } 136 bool relocation_contains(relocInfo* addr) const{ return relocation_begin() <= addr && addr < relocation_end(); } 137 bool instructions_contains(address addr) const { return instructions_begin() <= addr && addr < instructions_end(); } 138 bool data_contains(address addr) const { return data_begin() <= addr && addr < data_end(); } 139 bool oops_contains(oop* addr) const { return oops_begin() <= addr && addr < oops_end(); } 140 bool contains(address addr) const { return instructions_contains(addr); } 141 bool is_frame_complete_at(address addr) const { return instructions_contains(addr) && 142 addr >= instructions_begin() + _frame_complete_offset; } 143 144 // Relocation support 145 void fix_oop_relocations(address begin, address end) { 146 fix_oop_relocations(begin, end, false); 147 } 148 void fix_oop_relocations() { 149 fix_oop_relocations(NULL, NULL, false); 150 } 151 relocInfo::relocType reloc_type_for_address(address pc); 152 bool is_at_poll_return(address pc); 153 bool is_at_poll_or_poll_return(address pc); 154 155 // Support for oops in scopes and relocs: 156 // Note: index 0 is reserved for null. 157 oop oop_at(int index) const { return index == 0? (oop)NULL: *oop_addr_at(index); } 158 oop* oop_addr_at(int index) const{ // for GC 159 // relocation indexes are biased by 1 (because 0 is reserved) 160 assert(index > 0 && index <= _oops_length, "must be a valid non-zero index"); 161 return &oops_begin()[index-1]; 162 } 163 164 void copy_oops(GrowableArray<jobject>* oops); 165 166 // CodeCache support: really only used by the nmethods, but in order to get 167 // asserts and certain bookkeeping to work in the CodeCache they are defined 168 // virtual here. 169 virtual bool is_zombie() const { return false; } 170 virtual bool is_locked_by_vm() const { return false; } 171 172 virtual bool is_unloaded() const { return false; } 173 virtual bool is_not_entrant() const { return false; } 174 175 // GC support 176 virtual bool is_alive() const = 0; 177 virtual void do_unloading(BoolObjectClosure* is_alive, 178 OopClosure* keep_alive, 179 bool unloading_occurred); 180 virtual void oops_do(OopClosure* f) = 0; 181 // (All CodeBlob subtypes other than NMethod currently have 182 // an empty oops_do() method. 183 184 // OopMap for frame 185 OopMapSet* oop_maps() const { return _oop_maps; } 186 void set_oop_maps(OopMapSet* p); 187 OopMap* oop_map_for_return_address(address return_address); 188 virtual void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { ShouldNotReachHere(); } 189 190 // Frame support 191 int frame_size() const { return _frame_size; } 192 void set_frame_size(int size) { _frame_size = size; } 193 194 // Returns true, if the next frame is responsible for GC'ing oops passed as arguments 195 virtual bool caller_must_gc_arguments(JavaThread* thread) const { return false; } 196 197 // Naming 198 const char* name() const { return _name; } 199 void set_name(const char* name) { _name = name; } 200 201 // Debugging 202 virtual void verify(); 203 virtual void print() const PRODUCT_RETURN; 204 virtual void print_value_on(outputStream* st) const PRODUCT_RETURN; 205 206 // Print the comment associated with offset on stream, if there is one 207 void print_block_comment(outputStream* stream, intptr_t offset) { 208 _comments.print_block_comment(stream, offset); 209 } 210 211 // Transfer ownership of comments to this CodeBlob 212 void set_comments(CodeComments& comments) { 213 _comments.assign(comments); 214 } 215 }; 216 217 218 //---------------------------------------------------------------------------------------------------- 219 // BufferBlob: used to hold non-relocatable machine code such as the interpreter, stubroutines, etc. 220 221 class BufferBlob: public CodeBlob { 222 friend class VMStructs; 223 private: 224 // Creation support 225 BufferBlob(const char* name, int size); 226 BufferBlob(const char* name, int size, CodeBuffer* cb); 227 228 void* operator new(size_t s, unsigned size); 229 230 public: 231 // Creation 232 static BufferBlob* create(const char* name, int buffer_size); 233 static BufferBlob* create(const char* name, CodeBuffer* cb); 234 235 static void free(BufferBlob* buf); 236 237 // Typing 238 bool is_buffer_blob() const { return true; } 239 bool is_adapter_blob() const; 240 241 // GC/Verification support 242 void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } 243 bool is_alive() const { return true; } 244 void do_unloading(BoolObjectClosure* is_alive, 245 OopClosure* keep_alive, 246 bool unloading_occurred) { /* do nothing */ } 247 248 void oops_do(OopClosure* f) { /* do nothing*/ } 249 250 void verify(); 251 void print() const PRODUCT_RETURN; 252 void print_value_on(outputStream* st) const PRODUCT_RETURN; 253 }; 254 255 256 //---------------------------------------------------------------------------------------------------- 257 // RuntimeStub: describes stubs used by compiled code to call a (static) C++ runtime routine 258 259 class RuntimeStub: public CodeBlob { 260 friend class VMStructs; 261 private: 262 bool _caller_must_gc_arguments; 263 264 // Creation support 265 RuntimeStub( 266 const char* name, 267 CodeBuffer* cb, 268 int size, 269 int frame_complete, 270 int frame_size, 271 OopMapSet* oop_maps, 272 bool caller_must_gc_arguments 273 ); 274 275 void* operator new(size_t s, unsigned size); 276 277 public: 278 // Creation 279 static RuntimeStub* new_runtime_stub( 280 const char* stub_name, 281 CodeBuffer* cb, 282 int frame_complete, 283 int frame_size, 284 OopMapSet* oop_maps, 285 bool caller_must_gc_arguments 286 ); 287 288 // Typing 289 bool is_runtime_stub() const { return true; } 290 291 // GC support 292 bool caller_must_gc_arguments(JavaThread* thread) const { return _caller_must_gc_arguments; } 293 294 address entry_point() { return instructions_begin(); } 295 296 // GC/Verification support 297 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* nothing to do */ } 298 bool is_alive() const { return true; } 299 void do_unloading(BoolObjectClosure* is_alive, 300 OopClosure* keep_alive, 301 bool unloading_occurred) { /* do nothing */ } 302 void oops_do(OopClosure* f) { /* do-nothing*/ } 303 304 void verify(); 305 void print() const PRODUCT_RETURN; 306 void print_value_on(outputStream* st) const PRODUCT_RETURN; 307 }; 308 309 310 //---------------------------------------------------------------------------------------------------- 311 // Super-class for all blobs that exist in only one instance. Implements default behaviour. 312 313 class SingletonBlob: public CodeBlob { 314 friend class VMStructs; 315 public: 316 SingletonBlob( 317 const char* name, 318 CodeBuffer* cb, 319 int header_size, 320 int size, 321 int frame_size, 322 OopMapSet* oop_maps 323 ) 324 : CodeBlob(name, cb, header_size, size, CodeOffsets::frame_never_safe, frame_size, oop_maps) 325 {}; 326 327 bool is_alive() const { return true; } 328 void do_unloading(BoolObjectClosure* is_alive, 329 OopClosure* keep_alive, 330 bool unloading_occurred) { /* do-nothing*/ } 331 332 void verify(); // does nothing 333 void print() const PRODUCT_RETURN; 334 void print_value_on(outputStream* st) const PRODUCT_RETURN; 335 }; 336 337 338 //---------------------------------------------------------------------------------------------------- 339 // DeoptimizationBlob 340 341 class DeoptimizationBlob: public SingletonBlob { 342 friend class VMStructs; 343 private: 344 int _unpack_offset; 345 int _unpack_with_exception; 346 int _unpack_with_reexecution; 347 348 int _unpack_with_exception_in_tls; 349 350 // Creation support 351 DeoptimizationBlob( 352 CodeBuffer* cb, 353 int size, 354 OopMapSet* oop_maps, 355 int unpack_offset, 356 int unpack_with_exception_offset, 357 int unpack_with_reexecution_offset, 358 int frame_size 359 ); 360 361 void* operator new(size_t s, unsigned size); 362 363 public: 364 // Creation 365 static DeoptimizationBlob* create( 366 CodeBuffer* cb, 367 OopMapSet* oop_maps, 368 int unpack_offset, 369 int unpack_with_exception_offset, 370 int unpack_with_reexecution_offset, 371 int frame_size 372 ); 373 374 // Typing 375 bool is_deoptimization_stub() const { return true; } 376 const DeoptimizationBlob *as_deoptimization_stub() const { return this; } 377 bool exception_address_is_unpack_entry(address pc) const { 378 address unpack_pc = unpack(); 379 return (pc == unpack_pc || (pc + frame::pc_return_offset) == unpack_pc); 380 } 381 382 383 384 385 // GC for args 386 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* Nothing to do */ } 387 388 // Iteration 389 void oops_do(OopClosure* f) {} 390 391 // Printing 392 void print_value_on(outputStream* st) const PRODUCT_RETURN; 393 394 address unpack() const { return instructions_begin() + _unpack_offset; } 395 address unpack_with_exception() const { return instructions_begin() + _unpack_with_exception; } 396 address unpack_with_reexecution() const { return instructions_begin() + _unpack_with_reexecution; } 397 398 // Alternate entry point for C1 where the exception and issuing pc 399 // are in JavaThread::_exception_oop and JavaThread::_exception_pc 400 // instead of being in registers. This is needed because C1 doesn't 401 // model exception paths in a way that keeps these registers free so 402 // there may be live values in those registers during deopt. 403 void set_unpack_with_exception_in_tls_offset(int offset) { 404 _unpack_with_exception_in_tls = offset; 405 assert(contains(instructions_begin() + _unpack_with_exception_in_tls), "must be PC inside codeblob"); 406 } 407 address unpack_with_exception_in_tls() const { return instructions_begin() + _unpack_with_exception_in_tls; } 408 }; 409 410 411 //---------------------------------------------------------------------------------------------------- 412 // UncommonTrapBlob (currently only used by Compiler 2) 413 414 #ifdef COMPILER2 415 416 class UncommonTrapBlob: public SingletonBlob { 417 friend class VMStructs; 418 private: 419 // Creation support 420 UncommonTrapBlob( 421 CodeBuffer* cb, 422 int size, 423 OopMapSet* oop_maps, 424 int frame_size 425 ); 426 427 void* operator new(size_t s, unsigned size); 428 429 public: 430 // Creation 431 static UncommonTrapBlob* create( 432 CodeBuffer* cb, 433 OopMapSet* oop_maps, 434 int frame_size 435 ); 436 437 // GC for args 438 void preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) { /* nothing to do */ } 439 440 // Typing 441 bool is_uncommon_trap_stub() const { return true; } 442 443 // Iteration 444 void oops_do(OopClosure* f) {} 445 }; 446 447 448 //---------------------------------------------------------------------------------------------------- 449 // ExceptionBlob: used for exception unwinding in compiled code (currently only used by Compiler 2) 450 451 class ExceptionBlob: public SingletonBlob { 452 friend class VMStructs; 453 private: 454 // Creation support 455 ExceptionBlob( 456 CodeBuffer* cb, 457 int size, 458 OopMapSet* oop_maps, 459 int frame_size 460 ); 461 462 void* operator new(size_t s, unsigned size); 463 464 public: 465 // Creation 466 static ExceptionBlob* create( 467 CodeBuffer* cb, 468 OopMapSet* oop_maps, 469 int frame_size 470 ); 471 472 // GC for args 473 void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } 474 475 // Typing 476 bool is_exception_stub() const { return true; } 477 478 // Iteration 479 void oops_do(OopClosure* f) {} 480 }; 481 #endif // COMPILER2 482 483 484 //---------------------------------------------------------------------------------------------------- 485 // SafepointBlob: handles illegal_instruction exceptions during a safepoint 486 487 class SafepointBlob: public SingletonBlob { 488 friend class VMStructs; 489 private: 490 // Creation support 491 SafepointBlob( 492 CodeBuffer* cb, 493 int size, 494 OopMapSet* oop_maps, 495 int frame_size 496 ); 497 498 void* operator new(size_t s, unsigned size); 499 500 public: 501 // Creation 502 static SafepointBlob* create( 503 CodeBuffer* cb, 504 OopMapSet* oop_maps, 505 int frame_size 506 ); 507 508 // GC for args 509 void preserve_callee_argument_oops(frame fr, const RegisterMap* reg_map, OopClosure* f) { /* nothing to do */ } 510 511 // Typing 512 bool is_safepoint_stub() const { return true; } 513 514 // Iteration 515 void oops_do(OopClosure* f) {} 516 };