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
1 /*
2 * Copyright (c) 1997, 2013, 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 */
25 #ifndef SHARE_VM_CODE_RELOCINFO_HPP
26 #define SHARE_VM_CODE_RELOCINFO_HPP
28 #include "memory/allocation.hpp"
29 #include "utilities/top.hpp"
31 class NativeMovConstReg;
33 // Types in this file:
34 // relocInfo
35 // One element of an array of halfwords encoding compressed relocations.
36 // Also, the source of relocation types (relocInfo::oop_type, ...).
37 // Relocation
38 // A flyweight object representing a single relocation.
39 // It is fully unpacked from the compressed relocation array.
40 // metadata_Relocation, ... (subclasses of Relocation)
41 // The location of some type-specific operations (metadata_addr, ...).
42 // Also, the source of relocation specs (metadata_Relocation::spec, ...).
43 // oop_Relocation, ... (subclasses of Relocation)
44 // oops in the code stream (strings, class loaders)
45 // Also, the source of relocation specs (oop_Relocation::spec, ...).
46 // RelocationHolder
47 // A ValueObj type which acts as a union holding a Relocation object.
48 // Represents a relocation spec passed into a CodeBuffer during assembly.
49 // RelocIterator
50 // A StackObj which iterates over the relocations associated with
51 // a range of code addresses. Can be used to operate a copy of code.
52 // BoundRelocation
53 // An _internal_ type shared by packers and unpackers of relocations.
54 // It pastes together a RelocationHolder with some pointers into
55 // code and relocInfo streams.
58 // Notes on relocType:
59 //
60 // These hold enough information to read or write a value embedded in
61 // the instructions of an CodeBlob. They're used to update:
62 //
63 // 1) embedded oops (isOop() == true)
64 // 2) inline caches (isIC() == true)
65 // 3) runtime calls (isRuntimeCall() == true)
66 // 4) internal word ref (isInternalWord() == true)
67 // 5) external word ref (isExternalWord() == true)
68 //
69 // when objects move (GC) or if code moves (compacting the code heap).
70 // They are also used to patch the code (if a call site must change)
71 //
72 // A relocInfo is represented in 16 bits:
73 // 4 bits indicating the relocation type
74 // 12 bits indicating the offset from the previous relocInfo address
75 //
76 // The offsets accumulate along the relocInfo stream to encode the
77 // address within the CodeBlob, which is named RelocIterator::addr().
78 // The address of a particular relocInfo always points to the first
79 // byte of the relevant instruction (and not to any of its subfields
80 // or embedded immediate constants).
81 //
82 // The offset value is scaled appropriately for the target machine.
83 // (See relocInfo_<arch>.hpp for the offset scaling.)
84 //
85 // On some machines, there may also be a "format" field which may provide
86 // additional information about the format of the instruction stream
87 // at the corresponding code address. The format value is usually zero.
88 // Any machine (such as Intel) whose instructions can sometimes contain
89 // more than one relocatable constant needs format codes to distinguish
90 // which operand goes with a given relocation.
91 //
92 // If the target machine needs N format bits, the offset has 12-N bits,
93 // the format is encoded between the offset and the type, and the
94 // relocInfo_<arch>.hpp file has manifest constants for the format codes.
95 //
96 // If the type is "data_prefix_tag" then the offset bits are further encoded,
97 // and in fact represent not a code-stream offset but some inline data.
98 // The data takes the form of a counted sequence of halfwords, which
99 // precedes the actual relocation record. (Clients never see it directly.)
100 // The interpetation of this extra data depends on the relocation type.
101 //
102 // On machines that have 32-bit immediate fields, there is usually
103 // little need for relocation "prefix" data, because the instruction stream
104 // is a perfectly reasonable place to store the value. On machines in
105 // which 32-bit values must be "split" across instructions, the relocation
106 // data is the "true" specification of the value, which is then applied
107 // to some field of the instruction (22 or 13 bits, on SPARC).
108 //
109 // Whenever the location of the CodeBlob changes, any PC-relative
110 // relocations, and any internal_word_type relocations, must be reapplied.
111 // After the GC runs, oop_type relocations must be reapplied.
112 //
113 //
114 // Here are meanings of the types:
115 //
116 // relocInfo::none -- a filler record
117 // Value: none
118 // Instruction: The corresponding code address is ignored
119 // Data: Any data prefix and format code are ignored
120 // (This means that any relocInfo can be disabled by setting
121 // its type to none. See relocInfo::remove.)
122 //
123 // relocInfo::oop_type, relocInfo::metadata_type -- a reference to an oop or meta data
124 // Value: an oop, or else the address (handle) of an oop
125 // Instruction types: memory (load), set (load address)
126 // Data: [] an oop stored in 4 bytes of instruction
127 // [n] n is the index of an oop in the CodeBlob's oop pool
128 // [[N]n l] and l is a byte offset to be applied to the oop
129 // [Nn Ll] both index and offset may be 32 bits if necessary
130 // Here is a special hack, used only by the old compiler:
131 // [[N]n 00] the value is the __address__ of the nth oop in the pool
132 // (Note that the offset allows optimal references to class variables.)
133 //
134 // relocInfo::internal_word_type -- an address within the same CodeBlob
135 // relocInfo::section_word_type -- same, but can refer to another section
136 // Value: an address in the CodeBlob's code or constants section
137 // Instruction types: memory (load), set (load address)
138 // Data: [] stored in 4 bytes of instruction
139 // [[L]l] a relative offset (see [About Offsets] below)
140 // In the case of section_word_type, the offset is relative to a section
141 // base address, and the section number (e.g., SECT_INSTS) is encoded
142 // into the low two bits of the offset L.
143 //
144 // relocInfo::external_word_type -- a fixed address in the runtime system
145 // Value: an address
146 // Instruction types: memory (load), set (load address)
147 // Data: [] stored in 4 bytes of instruction
148 // [n] the index of a "well-known" stub (usual case on RISC)
149 // [Ll] a 32-bit address
150 //
151 // relocInfo::runtime_call_type -- a fixed subroutine in the runtime system
152 // Value: an address
153 // Instruction types: PC-relative call (or a PC-relative branch)
154 // Data: [] stored in 4 bytes of instruction
155 //
156 // relocInfo::static_call_type -- a static call
157 // Value: an CodeBlob, a stub, or a fixup routine
158 // Instruction types: a call
159 // Data: []
160 // The identity of the callee is extracted from debugging information.
161 // //%note reloc_3
162 //
163 // relocInfo::virtual_call_type -- a virtual call site (which includes an inline
164 // cache)
165 // Value: an CodeBlob, a stub, the interpreter, or a fixup routine
166 // Instruction types: a call, plus some associated set-oop instructions
167 // Data: [] the associated set-oops are adjacent to the call
168 // [n] n is a relative offset to the first set-oop
169 // [[N]n l] and l is a limit within which the set-oops occur
170 // [Nn Ll] both n and l may be 32 bits if necessary
171 // The identity of the callee is extracted from debugging information.
172 //
173 // relocInfo::opt_virtual_call_type -- a virtual call site that is statically bound
174 //
175 // Same info as a static_call_type. We use a special type, so the handling of
176 // virtuals and statics are separated.
177 //
178 //
179 // The offset n points to the first set-oop. (See [About Offsets] below.)
180 // In turn, the set-oop instruction specifies or contains an oop cell devoted
181 // exclusively to the IC call, which can be patched along with the call.
182 //
183 // The locations of any other set-oops are found by searching the relocation
184 // information starting at the first set-oop, and continuing until all
185 // relocations up through l have been inspected. The value l is another
186 // relative offset. (Both n and l are relative to the call's first byte.)
187 //
188 // The limit l of the search is exclusive. However, if it points within
189 // the call (e.g., offset zero), it is adjusted to point after the call and
190 // any associated machine-specific delay slot.
191 //
192 // Since the offsets could be as wide as 32-bits, these conventions
193 // put no restrictions whatever upon code reorganization.
194 //
195 // The compiler is responsible for ensuring that transition from a clean
196 // state to a monomorphic compiled state is MP-safe. This implies that
197 // the system must respond well to intermediate states where a random
198 // subset of the set-oops has been correctly from the clean state
199 // upon entry to the VEP of the compiled method. In the case of a
200 // machine (Intel) with a single set-oop instruction, the 32-bit
201 // immediate field must not straddle a unit of memory coherence.
202 // //%note reloc_3
203 //
204 // relocInfo::static_stub_type -- an extra stub for each static_call_type
205 // Value: none
206 // Instruction types: a virtual call: { set_oop; jump; }
207 // Data: [[N]n] the offset of the associated static_call reloc
208 // This stub becomes the target of a static call which must be upgraded
209 // to a virtual call (because the callee is interpreted).
210 // See [About Offsets] below.
211 // //%note reloc_2
212 //
213 // For example:
214 //
215 // INSTRUCTIONS RELOC: TYPE PREFIX DATA
216 // ------------ ---- -----------
217 // sethi %hi(myObject), R oop_type [n(myObject)]
218 // ld [R+%lo(myObject)+fldOffset], R2 oop_type [n(myObject) fldOffset]
219 // add R2, 1, R2
220 // st R2, [R+%lo(myObject)+fldOffset] oop_type [n(myObject) fldOffset]
221 //%note reloc_1
222 //
223 // This uses 4 instruction words, 8 relocation halfwords,
224 // and an entry (which is sharable) in the CodeBlob's oop pool,
225 // for a total of 36 bytes.
226 //
227 // Note that the compiler is responsible for ensuring the "fldOffset" when
228 // added to "%lo(myObject)" does not overflow the immediate fields of the
229 // memory instructions.
230 //
231 //
232 // [About Offsets] Relative offsets are supplied to this module as
233 // positive byte offsets, but they may be internally stored scaled
234 // and/or negated, depending on what is most compact for the target
235 // system. Since the object pointed to by the offset typically
236 // precedes the relocation address, it is profitable to store
237 // these negative offsets as positive numbers, but this decision
238 // is internal to the relocation information abstractions.
239 //
241 class Relocation;
242 class CodeBuffer;
243 class CodeSection;
244 class RelocIterator;
246 class relocInfo VALUE_OBJ_CLASS_SPEC {
247 friend class RelocIterator;
248 public:
249 enum relocType {
250 none = 0, // Used when no relocation should be generated
251 oop_type = 1, // embedded oop
252 virtual_call_type = 2, // a standard inline cache call for a virtual send
253 opt_virtual_call_type = 3, // a virtual call that has been statically bound (i.e., no IC cache)
254 static_call_type = 4, // a static send
255 static_stub_type = 5, // stub-entry for static send (takes care of interpreter case)
256 runtime_call_type = 6, // call to fixed external routine
257 external_word_type = 7, // reference to fixed external address
258 internal_word_type = 8, // reference within the current code blob
259 section_word_type = 9, // internal, but a cross-section reference
260 poll_type = 10, // polling instruction for safepoints
261 poll_return_type = 11, // polling instruction for safepoints at return
262 metadata_type = 12, // metadata that used to be oops
263 yet_unused_type_1 = 13, // Still unused
264 yet_unused_type_2 = 14, // Still unused
265 data_prefix_tag = 15, // tag for a prefix (carries data arguments)
266 type_mask = 15 // A mask which selects only the above values
267 };
269 protected:
270 unsigned short _value;
272 enum RawBitsToken { RAW_BITS };
273 relocInfo(relocType type, RawBitsToken ignore, int bits)
274 : _value((type << nontype_width) + bits) { }
276 relocInfo(relocType type, RawBitsToken ignore, int off, int f)
277 : _value((type << nontype_width) + (off / (unsigned)offset_unit) + (f << offset_width)) { }
279 public:
280 // constructor
281 relocInfo(relocType type, int offset, int format = 0)
282 #ifndef ASSERT
283 {
284 (*this) = relocInfo(type, RAW_BITS, offset, format);
285 }
286 #else
287 // Put a bunch of assertions out-of-line.
288 ;
289 #endif
291 #define APPLY_TO_RELOCATIONS(visitor) \
292 visitor(oop) \
293 visitor(metadata) \
294 visitor(virtual_call) \
295 visitor(opt_virtual_call) \
296 visitor(static_call) \
297 visitor(static_stub) \
298 visitor(runtime_call) \
299 visitor(external_word) \
300 visitor(internal_word) \
301 visitor(poll) \
302 visitor(poll_return) \
303 visitor(section_word) \
306 public:
307 enum {
308 value_width = sizeof(unsigned short) * BitsPerByte,
309 type_width = 4, // == log2(type_mask+1)
310 nontype_width = value_width - type_width,
311 datalen_width = nontype_width-1,
312 datalen_tag = 1 << datalen_width, // or-ed into _value
313 datalen_limit = 1 << datalen_width,
314 datalen_mask = (1 << datalen_width)-1
315 };
317 // accessors
318 public:
319 relocType type() const { return (relocType)((unsigned)_value >> nontype_width); }
320 int format() const { return format_mask==0? 0: format_mask &
321 ((unsigned)_value >> offset_width); }
322 int addr_offset() const { assert(!is_prefix(), "must have offset");
323 return (_value & offset_mask)*offset_unit; }
325 protected:
326 const short* data() const { assert(is_datalen(), "must have data");
327 return (const short*)(this + 1); }
328 int datalen() const { assert(is_datalen(), "must have data");
329 return (_value & datalen_mask); }
330 int immediate() const { assert(is_immediate(), "must have immed");
331 return (_value & datalen_mask); }
332 public:
333 static int addr_unit() { return offset_unit; }
334 static int offset_limit() { return (1 << offset_width) * offset_unit; }
336 void set_type(relocType type);
337 void set_format(int format);
339 void remove() { set_type(none); }
341 protected:
342 bool is_none() const { return type() == none; }
343 bool is_prefix() const { return type() == data_prefix_tag; }
344 bool is_datalen() const { assert(is_prefix(), "must be prefix");
345 return (_value & datalen_tag) != 0; }
346 bool is_immediate() const { assert(is_prefix(), "must be prefix");
347 return (_value & datalen_tag) == 0; }
349 public:
350 // Occasionally records of type relocInfo::none will appear in the stream.
351 // We do not bother to filter these out, but clients should ignore them.
352 // These records serve as "filler" in three ways:
353 // - to skip large spans of unrelocated code (this is rare)
354 // - to pad out the relocInfo array to the required oop alignment
355 // - to disable old relocation information which is no longer applicable
357 inline friend relocInfo filler_relocInfo();
359 // Every non-prefix relocation may be preceded by at most one prefix,
360 // which supplies 1 or more halfwords of associated data. Conventionally,
361 // an int is represented by 0, 1, or 2 halfwords, depending on how
362 // many bits are required to represent the value. (In addition,
363 // if the sole halfword is a 10-bit unsigned number, it is made
364 // "immediate" in the prefix header word itself. This optimization
365 // is invisible outside this module.)
367 inline friend relocInfo prefix_relocInfo(int datalen = 0);
369 protected:
370 // an immediate relocInfo optimizes a prefix with one 10-bit unsigned value
371 static relocInfo immediate_relocInfo(int data0) {
372 assert(fits_into_immediate(data0), "data0 in limits");
373 return relocInfo(relocInfo::data_prefix_tag, RAW_BITS, data0);
374 }
375 static bool fits_into_immediate(int data0) {
376 return (data0 >= 0 && data0 < datalen_limit);
377 }
379 public:
380 // Support routines for compilers.
382 // This routine takes an infant relocInfo (unprefixed) and
383 // edits in its prefix, if any. It also updates dest.locs_end.
384 void initialize(CodeSection* dest, Relocation* reloc);
386 // This routine updates a prefix and returns the limit pointer.
387 // It tries to compress the prefix from 32 to 16 bits, and if
388 // successful returns a reduced "prefix_limit" pointer.
389 relocInfo* finish_prefix(short* prefix_limit);
391 // bit-packers for the data array:
393 // As it happens, the bytes within the shorts are ordered natively,
394 // but the shorts within the word are ordered big-endian.
395 // This is an arbitrary choice, made this way mainly to ease debugging.
396 static int data0_from_int(jint x) { return x >> value_width; }
397 static int data1_from_int(jint x) { return (short)x; }
398 static jint jint_from_data(short* data) {
399 return (data[0] << value_width) + (unsigned short)data[1];
400 }
402 static jint short_data_at(int n, short* data, int datalen) {
403 return datalen > n ? data[n] : 0;
404 }
406 static jint jint_data_at(int n, short* data, int datalen) {
407 return datalen > n+1 ? jint_from_data(&data[n]) : short_data_at(n, data, datalen);
408 }
410 // Update methods for relocation information
411 // (since code is dynamically patched, we also need to dynamically update the relocation info)
412 // Both methods takes old_type, so it is able to performe sanity checks on the information removed.
413 static void change_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type, relocType new_type);
414 static void remove_reloc_info_for_address(RelocIterator *itr, address pc, relocType old_type);
416 // Machine dependent stuff
417 #ifdef TARGET_ARCH_x86
418 # include "relocInfo_x86.hpp"
419 #endif
420 #ifdef TARGET_ARCH_sparc
421 # include "relocInfo_sparc.hpp"
422 #endif
423 #ifdef TARGET_ARCH_zero
424 # include "relocInfo_zero.hpp"
425 #endif
426 #ifdef TARGET_ARCH_arm
427 # include "relocInfo_arm.hpp"
428 #endif
429 #ifdef TARGET_ARCH_ppc
430 # include "relocInfo_ppc.hpp"
431 #endif
434 protected:
435 // Derived constant, based on format_width which is PD:
436 enum {
437 offset_width = nontype_width - format_width,
438 offset_mask = (1<<offset_width) - 1,
439 format_mask = (1<<format_width) - 1
440 };
441 public:
442 enum {
443 // Conservatively large estimate of maximum length (in shorts)
444 // of any relocation record.
445 // Extended format is length prefix, data words, and tag/offset suffix.
446 length_limit = 1 + 1 + (3*BytesPerWord/BytesPerShort) + 1,
447 have_format = format_width > 0
448 };
449 };
451 #define FORWARD_DECLARE_EACH_CLASS(name) \
452 class name##_Relocation;
453 APPLY_TO_RELOCATIONS(FORWARD_DECLARE_EACH_CLASS)
454 #undef FORWARD_DECLARE_EACH_CLASS
458 inline relocInfo filler_relocInfo() {
459 return relocInfo(relocInfo::none, relocInfo::offset_limit() - relocInfo::offset_unit);
460 }
462 inline relocInfo prefix_relocInfo(int datalen) {
463 assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
464 return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
465 }
468 // Holder for flyweight relocation objects.
469 // Although the flyweight subclasses are of varying sizes,
470 // the holder is "one size fits all".
471 class RelocationHolder VALUE_OBJ_CLASS_SPEC {
472 friend class Relocation;
473 friend class CodeSection;
475 private:
476 // this preallocated memory must accommodate all subclasses of Relocation
477 // (this number is assertion-checked in Relocation::operator new)
478 enum { _relocbuf_size = 5 };
479 void* _relocbuf[ _relocbuf_size ];
481 public:
482 Relocation* reloc() const { return (Relocation*) &_relocbuf[0]; }
483 inline relocInfo::relocType type() const;
485 // Add a constant offset to a relocation. Helper for class Address.
486 RelocationHolder plus(int offset) const;
488 inline RelocationHolder(); // initializes type to none
490 inline RelocationHolder(Relocation* r); // make a copy
492 static const RelocationHolder none;
493 };
495 // A RelocIterator iterates through the relocation information of a CodeBlob.
496 // It is a variable BoundRelocation which is able to take on successive
497 // values as it is advanced through a code stream.
498 // Usage:
499 // RelocIterator iter(nm);
500 // while (iter.next()) {
501 // iter.reloc()->some_operation();
502 // }
503 // or:
504 // RelocIterator iter(nm);
505 // while (iter.next()) {
506 // switch (iter.type()) {
507 // case relocInfo::oop_type :
508 // case relocInfo::ic_type :
509 // case relocInfo::prim_type :
510 // case relocInfo::uncommon_type :
511 // case relocInfo::runtime_call_type :
512 // case relocInfo::internal_word_type:
513 // case relocInfo::external_word_type:
514 // ...
515 // }
516 // }
518 class RelocIterator : public StackObj {
519 enum { SECT_LIMIT = 3 }; // must be equal to CodeBuffer::SECT_LIMIT, checked in ctor
520 friend class Relocation;
521 friend class relocInfo; // for change_reloc_info_for_address only
522 typedef relocInfo::relocType relocType;
524 private:
525 address _limit; // stop producing relocations after this _addr
526 relocInfo* _current; // the current relocation information
527 relocInfo* _end; // end marker; we're done iterating when _current == _end
528 nmethod* _code; // compiled method containing _addr
529 address _addr; // instruction to which the relocation applies
530 short _databuf; // spare buffer for compressed data
531 short* _data; // pointer to the relocation's data
532 short _datalen; // number of halfwords in _data
533 char _format; // position within the instruction
535 // Base addresses needed to compute targets of section_word_type relocs.
536 address _section_start[SECT_LIMIT];
537 address _section_end [SECT_LIMIT];
539 void set_has_current(bool b) {
540 _datalen = !b ? -1 : 0;
541 debug_only(_data = NULL);
542 }
543 void set_current(relocInfo& ri) {
544 _current = &ri;
545 set_has_current(true);
546 }
548 RelocationHolder _rh; // where the current relocation is allocated
550 relocInfo* current() const { assert(has_current(), "must have current");
551 return _current; }
553 void set_limits(address begin, address limit);
555 void advance_over_prefix(); // helper method
557 void initialize_misc();
559 void initialize(nmethod* nm, address begin, address limit);
561 RelocIterator() { initialize_misc(); }
563 public:
564 // constructor
565 RelocIterator(nmethod* nm, address begin = NULL, address limit = NULL);
566 RelocIterator(CodeSection* cb, address begin = NULL, address limit = NULL);
568 // get next reloc info, return !eos
569 bool next() {
570 _current++;
571 assert(_current <= _end, "must not overrun relocInfo");
572 if (_current == _end) {
573 set_has_current(false);
574 return false;
575 }
576 set_has_current(true);
578 if (_current->is_prefix()) {
579 advance_over_prefix();
580 assert(!current()->is_prefix(), "only one prefix at a time");
581 }
583 _addr += _current->addr_offset();
585 if (_limit != NULL && _addr >= _limit) {
586 set_has_current(false);
587 return false;
588 }
590 if (relocInfo::have_format) _format = current()->format();
591 return true;
592 }
594 // accessors
595 address limit() const { return _limit; }
596 void set_limit(address x);
597 relocType type() const { return current()->type(); }
598 int format() const { return (relocInfo::have_format) ? current()->format() : 0; }
599 address addr() const { return _addr; }
600 nmethod* code() const { return _code; }
601 short* data() const { return _data; }
602 int datalen() const { return _datalen; }
603 bool has_current() const { return _datalen >= 0; }
605 void set_addr(address addr) { _addr = addr; }
606 bool addr_in_const() const;
608 address section_start(int n) const {
609 assert(_section_start[n], "must be initialized");
610 return _section_start[n];
611 }
612 address section_end(int n) const {
613 assert(_section_end[n], "must be initialized");
614 return _section_end[n];
615 }
617 // The address points to the affected displacement part of the instruction.
618 // For RISC, this is just the whole instruction.
619 // For Intel, this is an unaligned 32-bit word.
621 // type-specific relocation accessors: oop_Relocation* oop_reloc(), etc.
622 #define EACH_TYPE(name) \
623 inline name##_Relocation* name##_reloc();
624 APPLY_TO_RELOCATIONS(EACH_TYPE)
625 #undef EACH_TYPE
626 // generic relocation accessor; switches on type to call the above
627 Relocation* reloc();
629 // CodeBlob's have relocation indexes for faster random access:
630 static int locs_and_index_size(int code_size, int locs_size);
631 // Store an index into [dest_start+dest_count..dest_end).
632 // At dest_start[0..dest_count] is the actual relocation information.
633 // Everything else up to dest_end is free space for the index.
634 static void create_index(relocInfo* dest_begin, int dest_count, relocInfo* dest_end);
636 #ifndef PRODUCT
637 public:
638 void print();
639 void print_current();
640 #endif
641 };
644 // A Relocation is a flyweight object allocated within a RelocationHolder.
645 // It represents the relocation data of relocation record.
646 // So, the RelocIterator unpacks relocInfos into Relocations.
648 class Relocation VALUE_OBJ_CLASS_SPEC {
649 friend class RelocationHolder;
650 friend class RelocIterator;
652 private:
653 static void guarantee_size();
655 // When a relocation has been created by a RelocIterator,
656 // this field is non-null. It allows the relocation to know
657 // its context, such as the address to which it applies.
658 RelocIterator* _binding;
660 protected:
661 RelocIterator* binding() const {
662 assert(_binding != NULL, "must be bound");
663 return _binding;
664 }
665 void set_binding(RelocIterator* b) {
666 assert(_binding == NULL, "must be unbound");
667 _binding = b;
668 assert(_binding != NULL, "must now be bound");
669 }
671 Relocation() {
672 _binding = NULL;
673 }
675 static RelocationHolder newHolder() {
676 return RelocationHolder();
677 }
679 public:
680 void* operator new(size_t size, const RelocationHolder& holder) throw() {
681 if (size > sizeof(holder._relocbuf)) guarantee_size();
682 assert((void* const *)holder.reloc() == &holder._relocbuf[0], "ptrs must agree");
683 return holder.reloc();
684 }
686 // make a generic relocation for a given type (if possible)
687 static RelocationHolder spec_simple(relocInfo::relocType rtype);
689 // here is the type-specific hook which writes relocation data:
690 virtual void pack_data_to(CodeSection* dest) { }
692 // here is the type-specific hook which reads (unpacks) relocation data:
693 virtual void unpack_data() {
694 assert(datalen()==0 || type()==relocInfo::none, "no data here");
695 }
697 static bool is_reloc_index(intptr_t index) {
698 return 0 < index && index < os::vm_page_size();
699 }
701 protected:
702 // Helper functions for pack_data_to() and unpack_data().
704 // Most of the compression logic is confined here.
705 // (The "immediate data" mechanism of relocInfo works independently
706 // of this stuff, and acts to further compress most 1-word data prefixes.)
708 // A variable-width int is encoded as a short if it will fit in 16 bits.
709 // The decoder looks at datalen to decide whether to unpack short or jint.
710 // Most relocation records are quite simple, containing at most two ints.
712 static bool is_short(jint x) { return x == (short)x; }
713 static short* add_short(short* p, int x) { *p++ = x; return p; }
714 static short* add_jint (short* p, jint x) {
715 *p++ = relocInfo::data0_from_int(x); *p++ = relocInfo::data1_from_int(x);
716 return p;
717 }
718 static short* add_var_int(short* p, jint x) { // add a variable-width int
719 if (is_short(x)) p = add_short(p, x);
720 else p = add_jint (p, x);
721 return p;
722 }
724 static short* pack_1_int_to(short* p, jint x0) {
725 // Format is one of: [] [x] [Xx]
726 if (x0 != 0) p = add_var_int(p, x0);
727 return p;
728 }
729 int unpack_1_int() {
730 assert(datalen() <= 2, "too much data");
731 return relocInfo::jint_data_at(0, data(), datalen());
732 }
734 // With two ints, the short form is used only if both ints are short.
735 short* pack_2_ints_to(short* p, jint x0, jint x1) {
736 // Format is one of: [] [x y?] [Xx Y?y]
737 if (x0 == 0 && x1 == 0) {
738 // no halfwords needed to store zeroes
739 } else if (is_short(x0) && is_short(x1)) {
740 // 1-2 halfwords needed to store shorts
741 p = add_short(p, x0); if (x1!=0) p = add_short(p, x1);
742 } else {
743 // 3-4 halfwords needed to store jints
744 p = add_jint(p, x0); p = add_var_int(p, x1);
745 }
746 return p;
747 }
748 void unpack_2_ints(jint& x0, jint& x1) {
749 int dlen = datalen();
750 short* dp = data();
751 if (dlen <= 2) {
752 x0 = relocInfo::short_data_at(0, dp, dlen);
753 x1 = relocInfo::short_data_at(1, dp, dlen);
754 } else {
755 assert(dlen <= 4, "too much data");
756 x0 = relocInfo::jint_data_at(0, dp, dlen);
757 x1 = relocInfo::jint_data_at(2, dp, dlen);
758 }
759 }
761 protected:
762 // platform-dependent utilities for decoding and patching instructions
763 void pd_set_data_value (address x, intptr_t off, bool verify_only = false); // a set or mem-ref
764 void pd_verify_data_value (address x, intptr_t off) { pd_set_data_value(x, off, true); }
765 address pd_call_destination (address orig_addr = NULL);
766 void pd_set_call_destination (address x);
768 // this extracts the address of an address in the code stream instead of the reloc data
769 address* pd_address_in_code ();
771 // this extracts an address from the code stream instead of the reloc data
772 address pd_get_address_from_code ();
774 // these convert from byte offsets, to scaled offsets, to addresses
775 static jint scaled_offset(address x, address base) {
776 int byte_offset = x - base;
777 int offset = -byte_offset / relocInfo::addr_unit();
778 assert(address_from_scaled_offset(offset, base) == x, "just checkin'");
779 return offset;
780 }
781 static jint scaled_offset_null_special(address x, address base) {
782 // Some relocations treat offset=0 as meaning NULL.
783 // Handle this extra convention carefully.
784 if (x == NULL) return 0;
785 assert(x != base, "offset must not be zero");
786 return scaled_offset(x, base);
787 }
788 static address address_from_scaled_offset(jint offset, address base) {
789 int byte_offset = -( offset * relocInfo::addr_unit() );
790 return base + byte_offset;
791 }
793 // these convert between indexes and addresses in the runtime system
794 static int32_t runtime_address_to_index(address runtime_address);
795 static address index_to_runtime_address(int32_t index);
797 // helpers for mapping between old and new addresses after a move or resize
798 address old_addr_for(address newa, const CodeBuffer* src, CodeBuffer* dest);
799 address new_addr_for(address olda, const CodeBuffer* src, CodeBuffer* dest);
800 void normalize_address(address& addr, const CodeSection* dest, bool allow_other_sections = false);
802 public:
803 // accessors which only make sense for a bound Relocation
804 address addr() const { return binding()->addr(); }
805 nmethod* code() const { return binding()->code(); }
806 bool addr_in_const() const { return binding()->addr_in_const(); }
807 protected:
808 short* data() const { return binding()->data(); }
809 int datalen() const { return binding()->datalen(); }
810 int format() const { return binding()->format(); }
812 public:
813 virtual relocInfo::relocType type() { return relocInfo::none; }
815 // is it a call instruction?
816 virtual bool is_call() { return false; }
818 // is it a data movement instruction?
819 virtual bool is_data() { return false; }
821 // some relocations can compute their own values
822 virtual address value();
824 // all relocations are able to reassert their values
825 virtual void set_value(address x);
827 virtual void clear_inline_cache() { }
829 // This method assumes that all virtual/static (inline) caches are cleared (since for static_call_type and
830 // ic_call_type is not always posisition dependent (depending on the state of the cache)). However, this is
831 // probably a reasonable assumption, since empty caches simplifies code reloacation.
832 virtual void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) { }
834 void print();
835 };
838 // certain inlines must be deferred until class Relocation is defined:
840 inline RelocationHolder::RelocationHolder() {
841 // initialize the vtbl, just to keep things type-safe
842 new(*this) Relocation();
843 }
846 inline RelocationHolder::RelocationHolder(Relocation* r) {
847 // wordwise copy from r (ok if it copies garbage after r)
848 for (int i = 0; i < _relocbuf_size; i++) {
849 _relocbuf[i] = ((void**)r)[i];
850 }
851 }
854 relocInfo::relocType RelocationHolder::type() const {
855 return reloc()->type();
856 }
858 // A DataRelocation always points at a memory or load-constant instruction..
859 // It is absolute on most machines, and the constant is split on RISCs.
860 // The specific subtypes are oop, external_word, and internal_word.
861 // By convention, the "value" does not include a separately reckoned "offset".
862 class DataRelocation : public Relocation {
863 public:
864 bool is_data() { return true; }
866 // both target and offset must be computed somehow from relocation data
867 virtual int offset() { return 0; }
868 address value() = 0;
869 void set_value(address x) { set_value(x, offset()); }
870 void set_value(address x, intptr_t o) {
871 if (addr_in_const())
872 *(address*)addr() = x;
873 else
874 pd_set_data_value(x, o);
875 }
876 void verify_value(address x) {
877 if (addr_in_const())
878 assert(*(address*)addr() == x, "must agree");
879 else
880 pd_verify_data_value(x, offset());
881 }
883 // The "o" (displacement) argument is relevant only to split relocations
884 // on RISC machines. In some CPUs (SPARC), the set-hi and set-lo ins'ns
885 // can encode more than 32 bits between them. This allows compilers to
886 // share set-hi instructions between addresses that differ by a small
887 // offset (e.g., different static variables in the same class).
888 // On such machines, the "x" argument to set_value on all set-lo
889 // instructions must be the same as the "x" argument for the
890 // corresponding set-hi instructions. The "o" arguments for the
891 // set-hi instructions are ignored, and must not affect the high-half
892 // immediate constant. The "o" arguments for the set-lo instructions are
893 // added into the low-half immediate constant, and must not overflow it.
894 };
896 // A CallRelocation always points at a call instruction.
897 // It is PC-relative on most machines.
898 class CallRelocation : public Relocation {
899 public:
900 bool is_call() { return true; }
902 address destination() { return pd_call_destination(); }
903 void set_destination(address x); // pd_set_call_destination
905 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
906 address value() { return destination(); }
907 void set_value(address x) { set_destination(x); }
908 };
910 class oop_Relocation : public DataRelocation {
911 relocInfo::relocType type() { return relocInfo::oop_type; }
913 public:
914 // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]
915 // an oop in the CodeBlob's oop pool
916 static RelocationHolder spec(int oop_index, int offset = 0) {
917 assert(oop_index > 0, "must be a pool-resident oop");
918 RelocationHolder rh = newHolder();
919 new(rh) oop_Relocation(oop_index, offset);
920 return rh;
921 }
922 // an oop in the instruction stream
923 static RelocationHolder spec_for_immediate() {
924 const int oop_index = 0;
925 const int offset = 0; // if you want an offset, use the oop pool
926 RelocationHolder rh = newHolder();
927 new(rh) oop_Relocation(oop_index, offset);
928 return rh;
929 }
931 private:
932 jint _oop_index; // if > 0, index into CodeBlob::oop_at
933 jint _offset; // byte offset to apply to the oop itself
935 oop_Relocation(int oop_index, int offset) {
936 _oop_index = oop_index; _offset = offset;
937 }
939 friend class RelocIterator;
940 oop_Relocation() { }
942 public:
943 int oop_index() { return _oop_index; }
944 int offset() { return _offset; }
946 // data is packed in "2_ints" format: [i o] or [Ii Oo]
947 void pack_data_to(CodeSection* dest);
948 void unpack_data();
950 void fix_oop_relocation(); // reasserts oop value
952 void verify_oop_relocation();
954 address value() { return (address) *oop_addr(); }
956 bool oop_is_immediate() { return oop_index() == 0; }
958 oop* oop_addr(); // addr or &pool[jint_data]
959 oop oop_value(); // *oop_addr
960 // Note: oop_value transparently converts Universe::non_oop_word to NULL.
961 };
964 // copy of oop_Relocation for now but may delete stuff in both/either
965 class metadata_Relocation : public DataRelocation {
966 relocInfo::relocType type() { return relocInfo::metadata_type; }
968 public:
969 // encode in one of these formats: [] [n] [n l] [Nn l] [Nn Ll]
970 // an metadata in the CodeBlob's metadata pool
971 static RelocationHolder spec(int metadata_index, int offset = 0) {
972 assert(metadata_index > 0, "must be a pool-resident metadata");
973 RelocationHolder rh = newHolder();
974 new(rh) metadata_Relocation(metadata_index, offset);
975 return rh;
976 }
977 // an metadata in the instruction stream
978 static RelocationHolder spec_for_immediate() {
979 const int metadata_index = 0;
980 const int offset = 0; // if you want an offset, use the metadata pool
981 RelocationHolder rh = newHolder();
982 new(rh) metadata_Relocation(metadata_index, offset);
983 return rh;
984 }
986 private:
987 jint _metadata_index; // if > 0, index into nmethod::metadata_at
988 jint _offset; // byte offset to apply to the metadata itself
990 metadata_Relocation(int metadata_index, int offset) {
991 _metadata_index = metadata_index; _offset = offset;
992 }
994 friend class RelocIterator;
995 metadata_Relocation() { }
997 // Fixes a Metadata pointer in the code. Most platforms embeds the
998 // Metadata pointer in the code at compile time so this is empty
999 // for them.
1000 void pd_fix_value(address x);
1002 public:
1003 int metadata_index() { return _metadata_index; }
1004 int offset() { return _offset; }
1006 // data is packed in "2_ints" format: [i o] or [Ii Oo]
1007 void pack_data_to(CodeSection* dest);
1008 void unpack_data();
1010 void fix_metadata_relocation(); // reasserts metadata value
1012 void verify_metadata_relocation();
1014 address value() { return (address) *metadata_addr(); }
1016 bool metadata_is_immediate() { return metadata_index() == 0; }
1018 Metadata** metadata_addr(); // addr or &pool[jint_data]
1019 Metadata* metadata_value(); // *metadata_addr
1020 // Note: metadata_value transparently converts Universe::non_metadata_word to NULL.
1021 };
1024 class virtual_call_Relocation : public CallRelocation {
1025 relocInfo::relocType type() { return relocInfo::virtual_call_type; }
1027 public:
1028 // "cached_value" points to the first associated set-oop.
1029 // The oop_limit helps find the last associated set-oop.
1030 // (See comments at the top of this file.)
1031 static RelocationHolder spec(address cached_value) {
1032 RelocationHolder rh = newHolder();
1033 new(rh) virtual_call_Relocation(cached_value);
1034 return rh;
1035 }
1037 virtual_call_Relocation(address cached_value) {
1038 _cached_value = cached_value;
1039 assert(cached_value != NULL, "first oop address must be specified");
1040 }
1042 private:
1043 address _cached_value; // location of set-value instruction
1045 friend class RelocIterator;
1046 virtual_call_Relocation() { }
1049 public:
1050 address cached_value();
1052 // data is packed as scaled offsets in "2_ints" format: [f l] or [Ff Ll]
1053 // oop_limit is set to 0 if the limit falls somewhere within the call.
1054 // When unpacking, a zero oop_limit is taken to refer to the end of the call.
1055 // (This has the effect of bringing in the call's delay slot on SPARC.)
1056 void pack_data_to(CodeSection* dest);
1057 void unpack_data();
1059 void clear_inline_cache();
1060 };
1063 class opt_virtual_call_Relocation : public CallRelocation {
1064 relocInfo::relocType type() { return relocInfo::opt_virtual_call_type; }
1066 public:
1067 static RelocationHolder spec() {
1068 RelocationHolder rh = newHolder();
1069 new(rh) opt_virtual_call_Relocation();
1070 return rh;
1071 }
1073 private:
1074 friend class RelocIterator;
1075 opt_virtual_call_Relocation() { }
1077 public:
1078 void clear_inline_cache();
1080 // find the matching static_stub
1081 address static_stub();
1082 };
1085 class static_call_Relocation : public CallRelocation {
1086 relocInfo::relocType type() { return relocInfo::static_call_type; }
1088 public:
1089 static RelocationHolder spec() {
1090 RelocationHolder rh = newHolder();
1091 new(rh) static_call_Relocation();
1092 return rh;
1093 }
1095 private:
1096 friend class RelocIterator;
1097 static_call_Relocation() { }
1099 public:
1100 void clear_inline_cache();
1102 // find the matching static_stub
1103 address static_stub();
1104 };
1106 class static_stub_Relocation : public Relocation {
1107 relocInfo::relocType type() { return relocInfo::static_stub_type; }
1109 public:
1110 static RelocationHolder spec(address static_call) {
1111 RelocationHolder rh = newHolder();
1112 new(rh) static_stub_Relocation(static_call);
1113 return rh;
1114 }
1116 private:
1117 address _static_call; // location of corresponding static_call
1119 static_stub_Relocation(address static_call) {
1120 _static_call = static_call;
1121 }
1123 friend class RelocIterator;
1124 static_stub_Relocation() { }
1126 public:
1127 void clear_inline_cache();
1129 address static_call() { return _static_call; }
1131 // data is packed as a scaled offset in "1_int" format: [c] or [Cc]
1132 void pack_data_to(CodeSection* dest);
1133 void unpack_data();
1134 };
1136 class runtime_call_Relocation : public CallRelocation {
1137 relocInfo::relocType type() { return relocInfo::runtime_call_type; }
1139 public:
1140 static RelocationHolder spec() {
1141 RelocationHolder rh = newHolder();
1142 new(rh) runtime_call_Relocation();
1143 return rh;
1144 }
1146 private:
1147 friend class RelocIterator;
1148 runtime_call_Relocation() { }
1150 public:
1151 };
1153 class external_word_Relocation : public DataRelocation {
1154 relocInfo::relocType type() { return relocInfo::external_word_type; }
1156 public:
1157 static RelocationHolder spec(address target) {
1158 assert(target != NULL, "must not be null");
1159 RelocationHolder rh = newHolder();
1160 new(rh) external_word_Relocation(target);
1161 return rh;
1162 }
1164 // Use this one where all 32/64 bits of the target live in the code stream.
1165 // The target must be an intptr_t, and must be absolute (not relative).
1166 static RelocationHolder spec_for_immediate() {
1167 RelocationHolder rh = newHolder();
1168 new(rh) external_word_Relocation(NULL);
1169 return rh;
1170 }
1172 // Some address looking values aren't safe to treat as relocations
1173 // and should just be treated as constants.
1174 static bool can_be_relocated(address target) {
1175 return target != NULL && !is_reloc_index((intptr_t)target);
1176 }
1178 private:
1179 address _target; // address in runtime
1181 external_word_Relocation(address target) {
1182 _target = target;
1183 }
1185 friend class RelocIterator;
1186 external_word_Relocation() { }
1188 public:
1189 // data is packed as a well-known address in "1_int" format: [a] or [Aa]
1190 // The function runtime_address_to_index is used to turn full addresses
1191 // to short indexes, if they are pre-registered by the stub mechanism.
1192 // If the "a" value is 0 (i.e., _target is NULL), the address is stored
1193 // in the code stream. See external_word_Relocation::target().
1194 void pack_data_to(CodeSection* dest);
1195 void unpack_data();
1197 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1198 address target(); // if _target==NULL, fetch addr from code stream
1199 address value() { return target(); }
1200 };
1202 class internal_word_Relocation : public DataRelocation {
1203 relocInfo::relocType type() { return relocInfo::internal_word_type; }
1205 public:
1206 static RelocationHolder spec(address target) {
1207 assert(target != NULL, "must not be null");
1208 RelocationHolder rh = newHolder();
1209 new(rh) internal_word_Relocation(target);
1210 return rh;
1211 }
1213 // use this one where all the bits of the target can fit in the code stream:
1214 static RelocationHolder spec_for_immediate() {
1215 RelocationHolder rh = newHolder();
1216 new(rh) internal_word_Relocation(NULL);
1217 return rh;
1218 }
1220 internal_word_Relocation(address target) {
1221 _target = target;
1222 _section = -1; // self-relative
1223 }
1225 protected:
1226 address _target; // address in CodeBlob
1227 int _section; // section providing base address, if any
1229 friend class RelocIterator;
1230 internal_word_Relocation() { }
1232 // bit-width of LSB field in packed offset, if section >= 0
1233 enum { section_width = 2 }; // must equal CodeBuffer::sect_bits
1235 public:
1236 // data is packed as a scaled offset in "1_int" format: [o] or [Oo]
1237 // If the "o" value is 0 (i.e., _target is NULL), the offset is stored
1238 // in the code stream. See internal_word_Relocation::target().
1239 // If _section is not -1, it is appended to the low bits of the offset.
1240 void pack_data_to(CodeSection* dest);
1241 void unpack_data();
1243 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1244 address target(); // if _target==NULL, fetch addr from code stream
1245 int section() { return _section; }
1246 address value() { return target(); }
1247 };
1249 class section_word_Relocation : public internal_word_Relocation {
1250 relocInfo::relocType type() { return relocInfo::section_word_type; }
1252 public:
1253 static RelocationHolder spec(address target, int section) {
1254 RelocationHolder rh = newHolder();
1255 new(rh) section_word_Relocation(target, section);
1256 return rh;
1257 }
1259 section_word_Relocation(address target, int section) {
1260 assert(target != NULL, "must not be null");
1261 assert(section >= 0, "must be a valid section");
1262 _target = target;
1263 _section = section;
1264 }
1266 //void pack_data_to -- inherited
1267 void unpack_data();
1269 private:
1270 friend class RelocIterator;
1271 section_word_Relocation() { }
1272 };
1275 class poll_Relocation : public Relocation {
1276 bool is_data() { return true; }
1277 relocInfo::relocType type() { return relocInfo::poll_type; }
1278 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1279 };
1281 class poll_return_Relocation : public Relocation {
1282 bool is_data() { return true; }
1283 relocInfo::relocType type() { return relocInfo::poll_return_type; }
1284 void fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest);
1285 };
1287 // We know all the xxx_Relocation classes, so now we can define these:
1288 #define EACH_CASE(name) \
1289 inline name##_Relocation* RelocIterator::name##_reloc() { \
1290 assert(type() == relocInfo::name##_type, "type must agree"); \
1291 /* The purpose of the placed "new" is to re-use the same */ \
1292 /* stack storage for each new iteration. */ \
1293 name##_Relocation* r = new(_rh) name##_Relocation(); \
1294 r->set_binding(this); \
1295 r->name##_Relocation::unpack_data(); \
1296 return r; \
1297 }
1298 APPLY_TO_RELOCATIONS(EACH_CASE);
1299 #undef EACH_CASE
1301 inline RelocIterator::RelocIterator(nmethod* nm, address begin, address limit) {
1302 initialize(nm, begin, limit);
1303 }
1305 #endif // SHARE_VM_CODE_RELOCINFO_HPP