1 /*
  2  * Copyright (c) 2001, 2021, 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     maxHashBits         = db.lookupLongConstant("markWord::max_hash_bits").longValue();
 53     hashBits            = db.lookupLongConstant("markWord::hash_bits").longValue();
 54     lockShift           = db.lookupLongConstant("markWord::lock_shift").longValue();
 55     ageShift            = db.lookupLongConstant("markWord::age_shift").longValue();
 56     hashShift           = db.lookupLongConstant("markWord::hash_shift").longValue();
 57     if (VM.getVM().isLP64()) {
 58       klassShift          = db.lookupLongConstant("markWord::klass_shift").longValue();
 59     }
 60     lockMask            = db.lookupLongConstant("markWord::lock_mask").longValue();
 61     lockMaskInPlace     = db.lookupLongConstant("markWord::lock_mask_in_place").longValue();
 62     ageMask             = db.lookupLongConstant("markWord::age_mask").longValue();
 63     ageMaskInPlace      = db.lookupLongConstant("markWord::age_mask_in_place").longValue();
 64     hashMask            = db.lookupLongConstant("markWord::hash_mask").longValue();
 65     hashMaskInPlace     = db.lookupLongConstant("markWord::hash_mask_in_place").longValue();
 66     lockedValue         = db.lookupLongConstant("markWord::locked_value").longValue();
 67     unlockedValue       = db.lookupLongConstant("markWord::unlocked_value").longValue();
 68     monitorValue        = db.lookupLongConstant("markWord::monitor_value").longValue();
 69     markedValue         = db.lookupLongConstant("markWord::marked_value").longValue();
 70     noHash              = db.lookupLongConstant("markWord::no_hash").longValue();
 71     noHashInPlace       = db.lookupLongConstant("markWord::no_hash_in_place").longValue();
 72     noLockInPlace       = db.lookupLongConstant("markWord::no_lock_in_place").longValue();
 73     maxAge              = db.lookupLongConstant("markWord::max_age").longValue();
 74   }
 75 
 76   // Field accessors
 77   private static CIntegerField markField;
 78 
 79   // Constants -- read from VM
 80   private static long ageBits;
 81   private static long lockBits;
 82   private static long maxHashBits;
 83   private static long hashBits;
 84 
 85   private static long lockShift;
 86   private static long ageShift;
 87   private static long hashShift;
 88   private static long klassShift;
 89 
 90   private static long lockMask;
 91   private static long lockMaskInPlace;
 92   private static long ageMask;
 93   private static long ageMaskInPlace;
 94   private static long hashMask;
 95   private static long hashMaskInPlace;
 96 
 97   private static long lockedValue;
 98   private static long unlockedValue;
 99   private static long monitorValue;
100   private static long markedValue;
101 
102   private static long noHash;
103 
104   private static long noHashInPlace;
105   private static long noLockInPlace;
106 
107   private static long maxAge;
108 
109   /* Constants in markWord used by CMS. */
110   private static long cmsShift;
111   private static long cmsMask;
112   private static long sizeShift;
113 
114   public static long getKlassShift() {
115     return klassShift;
116   }
117 
118   public Mark(Address addr) {
119     super(addr);
120   }
121 
122   public long value() {
123     return markField.getValue(addr);
124   }
125 
126   public Address valueAsAddress() {
127     return addr.getAddressAt(markField.getOffset());
128   }
129 
130   // lock accessors (note that these assume lock_shift == 0)
131   public boolean isLocked() {
132     return (Bits.maskBitsLong(value(), lockMaskInPlace) != unlockedValue);
133   }
134   public boolean isUnlocked() {
135     return (Bits.maskBitsLong(value(), lockMaskInPlace) == unlockedValue);
136   }
137   public boolean isMarked() {
138     return (Bits.maskBitsLong(value(), lockMaskInPlace) == markedValue);
139   }
140 
141   // Special temporary state of the markWord while being inflated.
142   // Code that looks at mark outside a lock need to take this into account.
143   public boolean isBeingInflated() {
144     return (value() == 0);
145   }
146 
147   // Should this header be preserved during GC?
148   public boolean mustBePreserved() {
149      return (!isUnlocked() || !hasNoHash());
150   }
151 
152   // WARNING: The following routines are used EXCLUSIVELY by
153   // synchronization functions. They are not really gc safe.
154   // They must get updated if markWord layout get changed.
155 
156   public boolean hasLocker() {
157     return ((value() & lockMaskInPlace) == lockedValue);
158   }
159   public BasicLock locker() {
160     if (Assert.ASSERTS_ENABLED) {
161       Assert.that(hasLocker(), "check");
162     }
163     return new BasicLock(valueAsAddress());
164   }
165   public boolean hasMonitor() {
166     return ((value() & monitorValue) != 0);
167   }
168   public ObjectMonitor monitor() {
169     if (Assert.ASSERTS_ENABLED) {
170       Assert.that(hasMonitor(), "check");
171     }
172     // Use xor instead of &~ to provide one extra tag-bit check.
173     Address monAddr = valueAsAddress().xorWithMask(monitorValue);
174     return new ObjectMonitor(monAddr);
175   }
176   public boolean hasDisplacedMarkHelper() {
177     return ((value() & unlockedValue) == 0);
178   }
179   public Mark displacedMarkHelper() {
180     if (Assert.ASSERTS_ENABLED) {
181       Assert.that(hasDisplacedMarkHelper(), "check");
182     }
183     Address addr = valueAsAddress().andWithMask(~monitorValue);
184     return new Mark(addr.getAddressAt(0));
185   }
186   public int age() { return (int) Bits.maskBitsLong(value() >> ageShift, ageMask); }
187 
188   // hash operations
189   public long hash() {
190     return Bits.maskBitsLong(value() >> hashShift, hashMask);
191   }
192 
193   public boolean hasNoHash() {
194     return hash() == noHash;
195   }
196 
197   public Klass getKlass() {
198     assert(VM.getVM().isCompactObjectHeadersEnabled());
199     assert(!hasMonitor());
200     return (Klass)Metadata.instantiateWrapperFor(addr.getCompKlassAddressAt(0));
201   }
202 
203   // Debugging
204   public void printOn(PrintStream tty) {
205     if (isLocked()) {
206       tty.print("locked(0x" +
207                 Long.toHexString(value()) + ")->");
208       displacedMarkHelper().printOn(tty);
209     } else {
210       if (Assert.ASSERTS_ENABLED) {
211         Assert.that(isUnlocked(), "just checking");
212       }
213       tty.print("mark(");
214       tty.print("hash " + Long.toHexString(hash()) + ",");
215       tty.print("age " + age() + ")");
216     }
217   }
218 
219   public long getSize() { return (long)(value() >> sizeShift); }
220 }