1 /*
  2  * Copyright (c) 2001, 2020, 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 package sun.jvm.hotspot.oops;
 26 
 27 import java.io.*;
 28 import java.util.*;
 29 
 30 import sun.jvm.hotspot.debugger.*;
 31 import sun.jvm.hotspot.runtime.*;
 32 import sun.jvm.hotspot.types.*;
 33 import sun.jvm.hotspot.utilities.*;
 34 import sun.jvm.hotspot.utilities.Observable;
 35 import sun.jvm.hotspot.utilities.Observer;
 36 
 37 public class Mark extends VMObject {
 38   static {
 39     VM.registerVMInitializedObserver(new Observer() {
 40         public void update(Observable o, Object data) {
 41           initialize(VM.getVM().getTypeDataBase());
 42         }
 43       });
 44   }
 45 
 46   private static synchronized void initialize(TypeDataBase db) throws WrongTypeException {
 47     Type type  = db.lookupType("oopDesc");
 48     markField  = type.getCIntegerField("_mark");
 49 
 50     ageBits             = db.lookupLongConstant("markWord::age_bits").longValue();
 51     lockBits            = db.lookupLongConstant("markWord::lock_bits").longValue();
 52     biasedLockBits      = db.lookupLongConstant("markWord::biased_lock_bits").longValue();
 53     maxHashBits         = db.lookupLongConstant("markWord::max_hash_bits").longValue();
 54     hashBits            = db.lookupLongConstant("markWord::hash_bits").longValue();
 55     lockShift           = db.lookupLongConstant("markWord::lock_shift").longValue();
 56     biasedLockShift     = db.lookupLongConstant("markWord::biased_lock_shift").longValue();
 57     ageShift            = db.lookupLongConstant("markWord::age_shift").longValue();
 58     hashShift           = db.lookupLongConstant("markWord::hash_shift").longValue();
 59     if (VM.getVM().isLP64()) {
 60       klassShift          = db.lookupLongConstant("markWord::klass_shift").longValue();
 61     }
 62     lockMask            = db.lookupLongConstant("markWord::lock_mask").longValue();
 63     lockMaskInPlace     = db.lookupLongConstant("markWord::lock_mask_in_place").longValue();
 64     biasedLockMask      = db.lookupLongConstant("markWord::biased_lock_mask").longValue();
 65     biasedLockMaskInPlace  = db.lookupLongConstant("markWord::biased_lock_mask_in_place").longValue();
 66     biasedLockBitInPlace  = db.lookupLongConstant("markWord::biased_lock_bit_in_place").longValue();
 67     ageMask             = db.lookupLongConstant("markWord::age_mask").longValue();
 68     ageMaskInPlace      = db.lookupLongConstant("markWord::age_mask_in_place").longValue();
 69     hashMask            = db.lookupLongConstant("markWord::hash_mask").longValue();
 70     hashMaskInPlace     = db.lookupLongConstant("markWord::hash_mask_in_place").longValue();
 71     biasedLockAlignment  = db.lookupLongConstant("markWord::biased_lock_alignment").longValue();
 72     lockedValue         = db.lookupLongConstant("markWord::locked_value").longValue();
 73     unlockedValue       = db.lookupLongConstant("markWord::unlocked_value").longValue();
 74     monitorValue        = db.lookupLongConstant("markWord::monitor_value").longValue();
 75     markedValue         = db.lookupLongConstant("markWord::marked_value").longValue();
 76     biasedLockPattern = db.lookupLongConstant("markWord::biased_lock_pattern").longValue();
 77     noHash              = db.lookupLongConstant("markWord::no_hash").longValue();
 78     noHashInPlace       = db.lookupLongConstant("markWord::no_hash_in_place").longValue();
 79     noLockInPlace       = db.lookupLongConstant("markWord::no_lock_in_place").longValue();
 80     maxAge              = db.lookupLongConstant("markWord::max_age").longValue();
 81   }
 82 
 83   // Field accessors
 84   private static CIntegerField markField;
 85 
 86   // Constants -- read from VM
 87   private static long ageBits;
 88   private static long lockBits;
 89   private static long biasedLockBits;
 90   private static long maxHashBits;
 91   private static long hashBits;
 92 
 93   private static long lockShift;
 94   private static long biasedLockShift;
 95   private static long ageShift;
 96   private static long hashShift;
 97   private static long klassShift;
 98 
 99   private static long lockMask;
100   private static long lockMaskInPlace;
101   private static long biasedLockMask;
102   private static long biasedLockMaskInPlace;
103   private static long biasedLockBitInPlace;
104   private static long ageMask;
105   private static long ageMaskInPlace;
106   private static long hashMask;
107   private static long hashMaskInPlace;
108   private static long biasedLockAlignment;
109 
110   private static long lockedValue;
111   private static long unlockedValue;
112   private static long monitorValue;
113   private static long markedValue;
114   private static long biasedLockPattern;
115 
116   private static long noHash;
117 
118   private static long noHashInPlace;
119   private static long noLockInPlace;
120 
121   private static long maxAge;
122 
123   /* Constants in markWord used by CMS. */
124   private static long cmsShift;
125   private static long cmsMask;
126   private static long sizeShift;
127 
128   public static long getKlassShift() {
129     return klassShift;
130   }
131 
132   public Mark(Address addr) {
133     super(addr);
134   }
135 
136   public long value() {
137     return markField.getValue(addr);
138   }
139 
140   public Address valueAsAddress() {
141     return addr.getAddressAt(markField.getOffset());
142   }
143 
144   // Biased locking accessors
145   // These must be checked by all code which calls into the
146   // ObjectSynchoronizer and other code. The biasing is not understood
147   // by the lower-level CAS-based locking code, although the runtime
148   // fixes up biased locks to be compatible with it when a bias is
149   // revoked.
150   public boolean hasBiasPattern() {
151     return (Bits.maskBitsLong(value(), biasedLockMaskInPlace) == biasedLockPattern);
152   }
153 
154   public JavaThread biasedLocker() {
155     Threads threads = VM.getVM().getThreads();
156     Address addr = valueAsAddress().andWithMask(~(biasedLockMaskInPlace & ageMaskInPlace));
157     return threads.createJavaThreadWrapper(addr);
158   }
159 
160   // Indicates that the mark gas the bias bit set but that it has not
161   // yet been biased toward a particular thread
162   public boolean isBiasedAnonymously() {
163     return hasBiasPattern() && (biasedLocker() == null);
164   }
165 
166   // lock accessors (note that these assume lock_shift == 0)
167   public boolean isLocked() {
168     return (Bits.maskBitsLong(value(), lockMaskInPlace) != unlockedValue);
169   }
170   public boolean isUnlocked() {
171     return (Bits.maskBitsLong(value(), biasedLockMaskInPlace) == unlockedValue);
172   }
173   public boolean isMarked() {
174     return (Bits.maskBitsLong(value(), lockMaskInPlace) == markedValue);
175   }
176 
177   // Special temporary state of the markWord while being inflated.
178   // Code that looks at mark outside a lock need to take this into account.
179   public boolean isBeingInflated() {
180     return (value() == 0);
181   }
182 
183   // Should this header be preserved during GC?
184   public boolean mustBePreserved() {
185      return (!isUnlocked() || !hasNoHash());
186   }
187 
188   // WARNING: The following routines are used EXCLUSIVELY by
189   // synchronization functions. They are not really gc safe.
190   // They must get updated if markWord layout get changed.
191 
192   public boolean hasLocker() {
193     return ((value() & lockMaskInPlace) == lockedValue);
194   }
195   public BasicLock locker() {
196     if (Assert.ASSERTS_ENABLED) {
197       Assert.that(hasLocker(), "check");
198     }
199     return new BasicLock(valueAsAddress());
200   }
201   public boolean hasMonitor() {
202     return ((value() & monitorValue) != 0);
203   }
204   public ObjectMonitor monitor() {
205     if (Assert.ASSERTS_ENABLED) {
206       Assert.that(hasMonitor(), "check");
207     }
208     // Use xor instead of &~ to provide one extra tag-bit check.
209     Address monAddr = valueAsAddress().xorWithMask(monitorValue);
210     return new ObjectMonitor(monAddr);
211   }
212   public boolean hasDisplacedMarkHelper() {
213     return ((value() & unlockedValue) == 0);
214   }
215   public Mark displacedMarkHelper() {
216     if (Assert.ASSERTS_ENABLED) {
217       Assert.that(hasDisplacedMarkHelper(), "check");
218     }
219     Address addr = valueAsAddress().andWithMask(~monitorValue);
220     return new Mark(addr.getAddressAt(0));
221   }
222   public int age() { return (int) Bits.maskBitsLong(value() >> ageShift, ageMask); }
223 
224   // hash operations
225   public long hash() {
226     return Bits.maskBitsLong(value() >> hashShift, hashMask);
227   }
228 
229   public boolean hasNoHash() {
230     return hash() == noHash;
231   }
232 
233   public Klass getKlass() {
234     assert(VM.getVM().isCompactObjectHeadersEnabled());
235     assert(!hasMonitor());
236     return (Klass)Metadata.instantiateWrapperFor(addr.getCompKlassAddressAt(0));
237   }
238 
239   // Debugging
240   public void printOn(PrintStream tty) {
241     if (isLocked()) {
242       tty.print("locked(0x" +
243                 Long.toHexString(value()) + ")->");
244       displacedMarkHelper().printOn(tty);
245     } else {
246       if (Assert.ASSERTS_ENABLED) {
247         Assert.that(isUnlocked(), "just checking");
248       }
249       tty.print("mark(");
250       tty.print("hash " + Long.toHexString(hash()) + ",");
251       tty.print("age " + age() + ")");
252     }
253   }
254 
255   public long getSize() { return (long)(value() >> sizeShift); }
256 }