1 /*
  2  * Copyright (c) 1997, 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 #include "precompiled.hpp"
 26 #include "runtime/globals.hpp"
 27 #include "runtime/os.hpp"
 28 #include "runtime/signature.hpp"
 29 #include "utilities/globalDefinitions.hpp"
 30 #include "utilities/powerOfTwo.hpp"
 31 
 32 // Basic error support
 33 
 34 // Info for oops within a java object.  Defaults are zero so
 35 // things will break badly if incorrectly initialized.
 36 int heapOopSize        = 0;
 37 int LogBytesPerHeapOop = 0;
 38 int LogBitsPerHeapOop  = 0;
 39 int BytesPerHeapOop    = 0;
 40 int BitsPerHeapOop     = 0;
 41 
 42 // Object alignment, in units of HeapWords.
 43 // Defaults are -1 so things will break badly if incorrectly initialized.
 44 int MinObjAlignment            = -1;
 45 int MinObjAlignmentInBytes     = -1;
 46 int MinObjAlignmentInBytesMask = 0;
 47 
 48 int LogMinObjAlignment         = -1;
 49 int LogMinObjAlignmentInBytes  = -1;
 50 
 51 // Oop encoding heap max
 52 uint64_t OopEncodingHeapMax = 0;
 53 
 54 // Something to help porters sleep at night
 55 
 56 #ifdef ASSERT
 57 BasicType char2type(int ch) {
 58   switch (ch) {
 59 #define EACH_SIG(ch, bt, ignore) \
 60     case ch: return bt;
 61     SIGNATURE_TYPES_DO(EACH_SIG, ignore)
 62 #undef EACH_SIG
 63   }
 64   return T_ILLEGAL;
 65 }
 66 
 67 extern bool signature_constants_sane();
 68 #endif //ASSERT
 69 
 70 void basic_types_init() {
 71 #ifdef ASSERT
 72 #ifdef _LP64
 73   assert(min_intx ==  (intx)CONST64(0x8000000000000000), "correct constant");
 74   assert(max_intx ==  CONST64(0x7FFFFFFFFFFFFFFF), "correct constant");
 75   assert(max_uintx == CONST64(0xFFFFFFFFFFFFFFFF), "correct constant");
 76   assert( 8 == sizeof( intx),      "wrong size for basic type");
 77   assert( 8 == sizeof( jobject),   "wrong size for basic type");
 78 #else
 79   assert(min_intx ==  (intx)0x80000000,  "correct constant");
 80   assert(max_intx ==  0x7FFFFFFF,  "correct constant");
 81   assert(max_uintx == 0xFFFFFFFF,  "correct constant");
 82   assert( 4 == sizeof( intx),      "wrong size for basic type");
 83   assert( 4 == sizeof( jobject),   "wrong size for basic type");
 84 #endif
 85   assert( (~max_juint) == 0,  "max_juint has all its bits");
 86   assert( (~max_uintx) == 0,  "max_uintx has all its bits");
 87   assert( (~max_julong) == 0, "max_julong has all its bits");
 88   assert( 1 == sizeof( jbyte),     "wrong size for basic type");
 89   assert( 2 == sizeof( jchar),     "wrong size for basic type");
 90   assert( 2 == sizeof( jshort),    "wrong size for basic type");
 91   assert( 4 == sizeof( juint),     "wrong size for basic type");
 92   assert( 4 == sizeof( jint),      "wrong size for basic type");
 93   assert( 1 == sizeof( jboolean),  "wrong size for basic type");
 94   assert( 8 == sizeof( jlong),     "wrong size for basic type");
 95   assert( 4 == sizeof( jfloat),    "wrong size for basic type");
 96   assert( 8 == sizeof( jdouble),   "wrong size for basic type");
 97   assert( 1 == sizeof( u1),        "wrong size for basic type");
 98   assert( 2 == sizeof( u2),        "wrong size for basic type");
 99   assert( 4 == sizeof( u4),        "wrong size for basic type");
100   assert(wordSize == BytesPerWord, "should be the same since they're used interchangeably");
101   assert(wordSize == HeapWordSize, "should be the same since they're also used interchangeably");
102 
103   assert(signature_constants_sane(), "");
104 
105   int num_type_chars = 0;
106   for (int i = 0; i < 99; i++) {
107     if (type2char((BasicType)i) != 0) {
108       assert(char2type(type2char((BasicType)i)) == i, "proper inverses");
109       assert(Signature::basic_type(type2char((BasicType)i)) == i, "proper inverses");
110       num_type_chars++;
111     }
112   }
113   assert(num_type_chars == 12, "must have tested the right number of mappings");
114   assert(char2type(0) == T_ILLEGAL, "correct illegality");
115 
116   {
117     for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
118       BasicType vt = (BasicType)i;
119       BasicType ft = type2field[vt];
120       switch (vt) {
121       // the following types might plausibly show up in memory layouts:
122       case T_BOOLEAN:
123       case T_BYTE:
124       case T_CHAR:
125       case T_SHORT:
126       case T_INT:
127       case T_FLOAT:
128       case T_DOUBLE:
129       case T_LONG:
130       case T_OBJECT:
131       case T_INLINE_TYPE:
132       case T_ADDRESS:     // random raw pointer
133       case T_METADATA:    // metadata pointer
134       case T_NARROWOOP:   // compressed pointer
135       case T_NARROWKLASS: // compressed klass pointer
136       case T_CONFLICT:    // might as well support a bottom type
137       case T_VOID:        // padding or other unaddressed word
138         // layout type must map to itself
139         assert(vt == ft, "");
140         break;
141       default:
142         // non-layout type must map to a (different) layout type
143         assert(vt != ft, "");
144         assert(ft == type2field[ft], "");
145       }
146       // every type must map to same-sized layout type:
147       assert(type2size[vt] == type2size[ft], "");
148     }
149   }
150   // These are assumed, e.g., when filling HeapWords with juints.
151   assert(is_power_of_2(sizeof(juint)), "juint must be power of 2");
152   assert(is_power_of_2(HeapWordSize), "HeapWordSize must be power of 2");
153   assert((size_t)HeapWordSize >= sizeof(juint),
154          "HeapWord should be at least as large as juint");
155   assert(sizeof(NULL) == sizeof(char*), "NULL must be same size as pointer");
156 #endif
157 
158   if( JavaPriority1_To_OSPriority != -1 )
159     os::java_to_os_priority[1] = JavaPriority1_To_OSPriority;
160   if( JavaPriority2_To_OSPriority != -1 )
161     os::java_to_os_priority[2] = JavaPriority2_To_OSPriority;
162   if( JavaPriority3_To_OSPriority != -1 )
163     os::java_to_os_priority[3] = JavaPriority3_To_OSPriority;
164   if( JavaPriority4_To_OSPriority != -1 )
165     os::java_to_os_priority[4] = JavaPriority4_To_OSPriority;
166   if( JavaPriority5_To_OSPriority != -1 )
167     os::java_to_os_priority[5] = JavaPriority5_To_OSPriority;
168   if( JavaPriority6_To_OSPriority != -1 )
169     os::java_to_os_priority[6] = JavaPriority6_To_OSPriority;
170   if( JavaPriority7_To_OSPriority != -1 )
171     os::java_to_os_priority[7] = JavaPriority7_To_OSPriority;
172   if( JavaPriority8_To_OSPriority != -1 )
173     os::java_to_os_priority[8] = JavaPriority8_To_OSPriority;
174   if( JavaPriority9_To_OSPriority != -1 )
175     os::java_to_os_priority[9] = JavaPriority9_To_OSPriority;
176   if(JavaPriority10_To_OSPriority != -1 )
177     os::java_to_os_priority[10] = JavaPriority10_To_OSPriority;
178 
179   // Set the size of basic types here (after argument parsing but before
180   // stub generation).
181   if (UseCompressedOops) {
182     // Size info for oops within java objects is fixed
183     heapOopSize        = jintSize;
184     LogBytesPerHeapOop = LogBytesPerInt;
185     LogBitsPerHeapOop  = LogBitsPerInt;
186     BytesPerHeapOop    = BytesPerInt;
187     BitsPerHeapOop     = BitsPerInt;
188   } else {
189     heapOopSize        = oopSize;
190     LogBytesPerHeapOop = LogBytesPerWord;
191     LogBitsPerHeapOop  = LogBitsPerWord;
192     BytesPerHeapOop    = BytesPerWord;
193     BitsPerHeapOop     = BitsPerWord;
194   }
195   _type2aelembytes[T_OBJECT] = heapOopSize;
196   _type2aelembytes[T_ARRAY]  = heapOopSize;
197   _type2aelembytes[T_INLINE_TYPE]  = heapOopSize;
198 }
199 
200 
201 // Map BasicType to signature character
202 char type2char_tab[T_CONFLICT+1] = {
203   0, 0, 0, 0,
204   JVM_SIGNATURE_BOOLEAN, JVM_SIGNATURE_CHAR,
205   JVM_SIGNATURE_FLOAT,   JVM_SIGNATURE_DOUBLE,
206   JVM_SIGNATURE_BYTE,    JVM_SIGNATURE_SHORT,
207   JVM_SIGNATURE_INT,     JVM_SIGNATURE_LONG,
208   JVM_SIGNATURE_CLASS,   JVM_SIGNATURE_ARRAY,
209   JVM_SIGNATURE_INLINE_TYPE, JVM_SIGNATURE_VOID,
210   0, 0, 0, 0, 0
211 };
212 
213 // Map BasicType to Java type name
214 const char* type2name_tab[T_CONFLICT+1] = {
215   NULL, NULL, NULL, NULL,
216   "boolean",
217   "char",
218   "float",
219   "double",
220   "byte",
221   "short",
222   "int",
223   "long",
224   "object",
225   "array",
226   "inline_type",
227   "void",
228   "*address*",
229   "*narrowoop*",
230   "*metadata*",
231   "*narrowklass*",
232   "*conflict*"
233 };
234 
235 
236 BasicType name2type(const char* name) {
237   for (int i = T_BOOLEAN; i <= T_VOID; i++) {
238     BasicType t = (BasicType)i;
239     if (type2name_tab[t] != NULL && 0 == strcmp(type2name_tab[t], name))
240       return t;
241   }
242   return T_ILLEGAL;
243 }
244 
245 // Map BasicType to size in words
246 int type2size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 0, 1, 1, 1, 1, -1};
247 
248 BasicType type2field[T_CONFLICT+1] = {
249   (BasicType)0,            // 0,
250   (BasicType)0,            // 1,
251   (BasicType)0,            // 2,
252   (BasicType)0,            // 3,
253   T_BOOLEAN,               // T_BOOLEAN  =  4,
254   T_CHAR,                  // T_CHAR     =  5,
255   T_FLOAT,                 // T_FLOAT    =  6,
256   T_DOUBLE,                // T_DOUBLE   =  7,
257   T_BYTE,                  // T_BYTE     =  8,
258   T_SHORT,                 // T_SHORT    =  9,
259   T_INT,                   // T_INT      = 10,
260   T_LONG,                  // T_LONG     = 11,
261   T_OBJECT,                // T_OBJECT   = 12,
262   T_OBJECT,                // T_ARRAY    = 13,
263   T_INLINE_TYPE,           // T_INLINE_TYPE = 14,
264   T_VOID,                  // T_VOID     = 15,
265   T_ADDRESS,               // T_ADDRESS  = 16,
266   T_NARROWOOP,             // T_NARROWOOP= 17,
267   T_METADATA,              // T_METADATA = 18,
268   T_NARROWKLASS,           // T_NARROWKLASS = 19,
269   T_CONFLICT               // T_CONFLICT = 20
270 };
271 
272 
273 BasicType type2wfield[T_CONFLICT+1] = {
274   (BasicType)0,            // 0,
275   (BasicType)0,            // 1,
276   (BasicType)0,            // 2,
277   (BasicType)0,            // 3,
278   T_INT,     // T_BOOLEAN  =  4,
279   T_INT,     // T_CHAR     =  5,
280   T_FLOAT,   // T_FLOAT    =  6,
281   T_DOUBLE,  // T_DOUBLE   =  7,
282   T_INT,     // T_BYTE     =  8,
283   T_INT,     // T_SHORT    =  9,
284   T_INT,     // T_INT      = 10,
285   T_LONG,    // T_LONG     = 11,
286   T_OBJECT,  // T_OBJECT   = 12,
287   T_OBJECT,  // T_ARRAY    = 13,
288   T_OBJECT,  // T_INLINE_TYPE = 14,
289   T_VOID,    // T_VOID     = 15,
290   T_ADDRESS, // T_ADDRESS  = 16,
291   T_NARROWOOP, // T_NARROWOOP  = 17,
292   T_METADATA,  // T_METADATA   = 18,
293   T_NARROWKLASS, // T_NARROWKLASS  = 19,
294   T_CONFLICT // T_CONFLICT = 20
295 };
296 
297 
298 int _type2aelembytes[T_CONFLICT+1] = {
299   0,                         // 0
300   0,                         // 1
301   0,                         // 2
302   0,                         // 3
303   T_BOOLEAN_aelem_bytes,     // T_BOOLEAN  =  4,
304   T_CHAR_aelem_bytes,        // T_CHAR     =  5,
305   T_FLOAT_aelem_bytes,       // T_FLOAT    =  6,
306   T_DOUBLE_aelem_bytes,      // T_DOUBLE   =  7,
307   T_BYTE_aelem_bytes,        // T_BYTE     =  8,
308   T_SHORT_aelem_bytes,       // T_SHORT    =  9,
309   T_INT_aelem_bytes,         // T_INT      = 10,
310   T_LONG_aelem_bytes,        // T_LONG     = 11,
311   T_OBJECT_aelem_bytes,      // T_OBJECT   = 12,
312   T_ARRAY_aelem_bytes,       // T_ARRAY    = 13,
313   T_INLINE_TYPE_aelem_bytes,   // T_INLINE_TYPE = 14,
314   0,                         // T_VOID     = 15,
315   T_OBJECT_aelem_bytes,      // T_ADDRESS  = 16,
316   T_NARROWOOP_aelem_bytes,   // T_NARROWOOP= 17,
317   T_OBJECT_aelem_bytes,      // T_METADATA = 18,
318   T_NARROWKLASS_aelem_bytes, // T_NARROWKLASS= 19,
319   0                          // T_CONFLICT = 20
320 };
321 
322 #ifdef ASSERT
323 int type2aelembytes(BasicType t, bool allow_address) {
324   assert(allow_address || t != T_ADDRESS, " ");
325   return _type2aelembytes[t];
326 }
327 #endif
328 
329 // Support for 64-bit integer arithmetic
330 
331 // The following code is mostly taken from JVM typedefs_md.h and system_md.c
332 
333 static const jlong high_bit   = (jlong)1 << (jlong)63;
334 static const jlong other_bits = ~high_bit;
335 
336 jlong float2long(jfloat f) {
337   jlong tmp = (jlong) f;
338   if (tmp != high_bit) {
339     return tmp;
340   } else {
341     if (g_isnan((jdouble)f)) {
342       return 0;
343     }
344     if (f < 0) {
345       return high_bit;
346     } else {
347       return other_bits;
348     }
349   }
350 }
351 
352 
353 jlong double2long(jdouble f) {
354   jlong tmp = (jlong) f;
355   if (tmp != high_bit) {
356     return tmp;
357   } else {
358     if (g_isnan(f)) {
359       return 0;
360     }
361     if (f < 0) {
362       return high_bit;
363     } else {
364       return other_bits;
365     }
366   }
367 }
368 
369 // least common multiple
370 size_t lcm(size_t a, size_t b) {
371     size_t cur, div, next;
372 
373     cur = MAX2(a, b);
374     div = MIN2(a, b);
375 
376     assert(div != 0, "lcm requires positive arguments");
377 
378 
379     while ((next = cur % div) != 0) {
380         cur = div; div = next;
381     }
382 
383 
384     julong result = julong(a) * b / div;
385     assert(result <= (size_t)max_uintx, "Integer overflow in lcm");
386 
387     return size_t(result);
388 }
389 
390 
391 // Test that nth_bit macro and friends behave as
392 // expected, even with low-precedence operators.
393 
394 STATIC_ASSERT(nth_bit(3)   == 0x8);
395 STATIC_ASSERT(nth_bit(1|2) == 0x8);
396 
397 STATIC_ASSERT(right_n_bits(3)   == 0x7);
398 STATIC_ASSERT(right_n_bits(1|2) == 0x7);