universe.cpp revision 0:a61af66fc99e
1/*
2 * Copyright 1997-2007 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# include "incls/_precompiled.incl"
26# include "incls/_universe.cpp.incl"
27
28// Known objects
29klassOop Universe::_boolArrayKlassObj                 = NULL;
30klassOop Universe::_byteArrayKlassObj                 = NULL;
31klassOop Universe::_charArrayKlassObj                 = NULL;
32klassOop Universe::_intArrayKlassObj                  = NULL;
33klassOop Universe::_shortArrayKlassObj                = NULL;
34klassOop Universe::_longArrayKlassObj                 = NULL;
35klassOop Universe::_singleArrayKlassObj               = NULL;
36klassOop Universe::_doubleArrayKlassObj               = NULL;
37klassOop Universe::_typeArrayKlassObjs[T_VOID+1]      = { NULL /*, NULL...*/ };
38klassOop Universe::_objectArrayKlassObj               = NULL;
39klassOop Universe::_symbolKlassObj                    = NULL;
40klassOop Universe::_methodKlassObj                    = NULL;
41klassOop Universe::_constMethodKlassObj               = NULL;
42klassOop Universe::_methodDataKlassObj                = NULL;
43klassOop Universe::_klassKlassObj                     = NULL;
44klassOop Universe::_arrayKlassKlassObj                = NULL;
45klassOop Universe::_objArrayKlassKlassObj             = NULL;
46klassOop Universe::_typeArrayKlassKlassObj            = NULL;
47klassOop Universe::_instanceKlassKlassObj             = NULL;
48klassOop Universe::_constantPoolKlassObj              = NULL;
49klassOop Universe::_constantPoolCacheKlassObj         = NULL;
50klassOop Universe::_compiledICHolderKlassObj          = NULL;
51klassOop Universe::_systemObjArrayKlassObj            = NULL;
52oop Universe::_int_mirror                             =  NULL;
53oop Universe::_float_mirror                           =  NULL;
54oop Universe::_double_mirror                          =  NULL;
55oop Universe::_byte_mirror                            =  NULL;
56oop Universe::_bool_mirror                            =  NULL;
57oop Universe::_char_mirror                            =  NULL;
58oop Universe::_long_mirror                            =  NULL;
59oop Universe::_short_mirror                           =  NULL;
60oop Universe::_void_mirror                            =  NULL;
61oop Universe::_mirrors[T_VOID+1]                      =  { NULL /*, NULL...*/ };
62oop Universe::_main_thread_group                      = NULL;
63oop Universe::_system_thread_group                    = NULL;
64typeArrayOop Universe::_the_empty_byte_array          = NULL;
65typeArrayOop Universe::_the_empty_short_array         = NULL;
66typeArrayOop Universe::_the_empty_int_array           = NULL;
67objArrayOop Universe::_the_empty_system_obj_array     = NULL;
68objArrayOop Universe::_the_empty_class_klass_array    = NULL;
69objArrayOop Universe::_the_array_interfaces_array     = NULL;
70LatestMethodOopCache* Universe::_finalizer_register_cache = NULL;
71LatestMethodOopCache* Universe::_loader_addClass_cache    = NULL;
72ActiveMethodOopsCache* Universe::_reflect_invoke_cache    = NULL;
73oop Universe::_out_of_memory_error_java_heap          = NULL;
74oop Universe::_out_of_memory_error_perm_gen           = NULL;
75oop Universe::_out_of_memory_error_array_size         = NULL;
76oop Universe::_out_of_memory_error_gc_overhead_limit  = NULL;
77objArrayOop Universe::_preallocated_out_of_memory_error_array = NULL;
78volatile jint Universe::_preallocated_out_of_memory_error_avail_count = 0;
79bool Universe::_verify_in_progress                    = false;
80oop Universe::_null_ptr_exception_instance            = NULL;
81oop Universe::_arithmetic_exception_instance          = NULL;
82oop Universe::_virtual_machine_error_instance         = NULL;
83oop Universe::_vm_exception                           = NULL;
84oop Universe::_emptySymbol                            = NULL;
85
86// These variables are guarded by FullGCALot_lock.
87debug_only(objArrayOop Universe::_fullgc_alot_dummy_array = NULL;)
88debug_only(int Universe::_fullgc_alot_dummy_next      = 0;)
89
90
91// Heap
92int             Universe::_verify_count = 0;
93
94int             Universe::_base_vtable_size = 0;
95bool            Universe::_bootstrapping = false;
96bool            Universe::_fully_initialized = false;
97
98size_t          Universe::_heap_capacity_at_last_gc;
99size_t          Universe::_heap_used_at_last_gc;
100
101CollectedHeap*  Universe::_collectedHeap = NULL;
102
103
104void Universe::basic_type_classes_do(void f(klassOop)) {
105  f(boolArrayKlassObj());
106  f(byteArrayKlassObj());
107  f(charArrayKlassObj());
108  f(intArrayKlassObj());
109  f(shortArrayKlassObj());
110  f(longArrayKlassObj());
111  f(singleArrayKlassObj());
112  f(doubleArrayKlassObj());
113}
114
115
116void Universe::system_classes_do(void f(klassOop)) {
117  f(symbolKlassObj());
118  f(methodKlassObj());
119  f(constMethodKlassObj());
120  f(methodDataKlassObj());
121  f(klassKlassObj());
122  f(arrayKlassKlassObj());
123  f(objArrayKlassKlassObj());
124  f(typeArrayKlassKlassObj());
125  f(instanceKlassKlassObj());
126  f(constantPoolKlassObj());
127  f(systemObjArrayKlassObj());
128}
129
130void Universe::oops_do(OopClosure* f, bool do_all) {
131
132  f->do_oop((oop*) &_int_mirror);
133  f->do_oop((oop*) &_float_mirror);
134  f->do_oop((oop*) &_double_mirror);
135  f->do_oop((oop*) &_byte_mirror);
136  f->do_oop((oop*) &_bool_mirror);
137  f->do_oop((oop*) &_char_mirror);
138  f->do_oop((oop*) &_long_mirror);
139  f->do_oop((oop*) &_short_mirror);
140  f->do_oop((oop*) &_void_mirror);
141
142  // It's important to iterate over these guys even if they are null,
143  // since that's how shared heaps are restored.
144  for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
145    f->do_oop((oop*) &_mirrors[i]);
146  }
147  assert(_mirrors[0] == NULL && _mirrors[T_BOOLEAN - 1] == NULL, "checking");
148
149  // %%% Consider moving those "shared oops" over here with the others.
150  f->do_oop((oop*)&_boolArrayKlassObj);
151  f->do_oop((oop*)&_byteArrayKlassObj);
152  f->do_oop((oop*)&_charArrayKlassObj);
153  f->do_oop((oop*)&_intArrayKlassObj);
154  f->do_oop((oop*)&_shortArrayKlassObj);
155  f->do_oop((oop*)&_longArrayKlassObj);
156  f->do_oop((oop*)&_singleArrayKlassObj);
157  f->do_oop((oop*)&_doubleArrayKlassObj);
158  f->do_oop((oop*)&_objectArrayKlassObj);
159  {
160    for (int i = 0; i < T_VOID+1; i++) {
161      if (_typeArrayKlassObjs[i] != NULL) {
162        assert(i >= T_BOOLEAN, "checking");
163        f->do_oop((oop*)&_typeArrayKlassObjs[i]);
164      } else if (do_all) {
165        f->do_oop((oop*)&_typeArrayKlassObjs[i]);
166      }
167    }
168  }
169  f->do_oop((oop*)&_symbolKlassObj);
170  f->do_oop((oop*)&_methodKlassObj);
171  f->do_oop((oop*)&_constMethodKlassObj);
172  f->do_oop((oop*)&_methodDataKlassObj);
173  f->do_oop((oop*)&_klassKlassObj);
174  f->do_oop((oop*)&_arrayKlassKlassObj);
175  f->do_oop((oop*)&_objArrayKlassKlassObj);
176  f->do_oop((oop*)&_typeArrayKlassKlassObj);
177  f->do_oop((oop*)&_instanceKlassKlassObj);
178  f->do_oop((oop*)&_constantPoolKlassObj);
179  f->do_oop((oop*)&_constantPoolCacheKlassObj);
180  f->do_oop((oop*)&_compiledICHolderKlassObj);
181  f->do_oop((oop*)&_systemObjArrayKlassObj);
182  f->do_oop((oop*)&_the_empty_byte_array);
183  f->do_oop((oop*)&_the_empty_short_array);
184  f->do_oop((oop*)&_the_empty_int_array);
185  f->do_oop((oop*)&_the_empty_system_obj_array);
186  f->do_oop((oop*)&_the_empty_class_klass_array);
187  f->do_oop((oop*)&_the_array_interfaces_array);
188  _finalizer_register_cache->oops_do(f);
189  _loader_addClass_cache->oops_do(f);
190  _reflect_invoke_cache->oops_do(f);
191  f->do_oop((oop*)&_out_of_memory_error_java_heap);
192  f->do_oop((oop*)&_out_of_memory_error_perm_gen);
193  f->do_oop((oop*)&_out_of_memory_error_array_size);
194  f->do_oop((oop*)&_out_of_memory_error_gc_overhead_limit);
195  if (_preallocated_out_of_memory_error_array != (oop)NULL) {   // NULL when DumpSharedSpaces
196    f->do_oop((oop*)&_preallocated_out_of_memory_error_array);
197  }
198  f->do_oop((oop*)&_null_ptr_exception_instance);
199  f->do_oop((oop*)&_arithmetic_exception_instance);
200  f->do_oop((oop*)&_virtual_machine_error_instance);
201  f->do_oop((oop*)&_main_thread_group);
202  f->do_oop((oop*)&_system_thread_group);
203  f->do_oop((oop*)&_vm_exception);
204  f->do_oop((oop*)&_emptySymbol);
205  debug_only(f->do_oop((oop*)&_fullgc_alot_dummy_array);)
206}
207
208
209void Universe::check_alignment(uintx size, uintx alignment, const char* name) {
210  if (size < alignment || size % alignment != 0) {
211    ResourceMark rm;
212    stringStream st;
213    st.print("Size of %s (%ld bytes) must be aligned to %ld bytes", name, size, alignment);
214    char* error = st.as_string();
215    vm_exit_during_initialization(error);
216  }
217}
218
219
220void Universe::genesis(TRAPS) {
221  ResourceMark rm;
222  { FlagSetting fs(_bootstrapping, true);
223
224    { MutexLocker mc(Compile_lock);
225
226      // determine base vtable size; without that we cannot create the array klasses
227      compute_base_vtable_size();
228
229      if (!UseSharedSpaces) {
230        _klassKlassObj          = klassKlass::create_klass(CHECK);
231        _arrayKlassKlassObj     = arrayKlassKlass::create_klass(CHECK);
232
233        _objArrayKlassKlassObj  = objArrayKlassKlass::create_klass(CHECK);
234        _instanceKlassKlassObj  = instanceKlassKlass::create_klass(CHECK);
235        _typeArrayKlassKlassObj = typeArrayKlassKlass::create_klass(CHECK);
236
237        _symbolKlassObj         = symbolKlass::create_klass(CHECK);
238
239        _emptySymbol            = oopFactory::new_symbol("", CHECK);
240
241        _boolArrayKlassObj      = typeArrayKlass::create_klass(T_BOOLEAN, sizeof(jboolean), CHECK);
242        _charArrayKlassObj      = typeArrayKlass::create_klass(T_CHAR,    sizeof(jchar),    CHECK);
243        _singleArrayKlassObj    = typeArrayKlass::create_klass(T_FLOAT,   sizeof(jfloat),   CHECK);
244        _doubleArrayKlassObj    = typeArrayKlass::create_klass(T_DOUBLE,  sizeof(jdouble),  CHECK);
245        _byteArrayKlassObj      = typeArrayKlass::create_klass(T_BYTE,    sizeof(jbyte),    CHECK);
246        _shortArrayKlassObj     = typeArrayKlass::create_klass(T_SHORT,   sizeof(jshort),   CHECK);
247        _intArrayKlassObj       = typeArrayKlass::create_klass(T_INT,     sizeof(jint),     CHECK);
248        _longArrayKlassObj      = typeArrayKlass::create_klass(T_LONG,    sizeof(jlong),    CHECK);
249
250        _typeArrayKlassObjs[T_BOOLEAN] = _boolArrayKlassObj;
251        _typeArrayKlassObjs[T_CHAR]    = _charArrayKlassObj;
252        _typeArrayKlassObjs[T_FLOAT]   = _singleArrayKlassObj;
253        _typeArrayKlassObjs[T_DOUBLE]  = _doubleArrayKlassObj;
254        _typeArrayKlassObjs[T_BYTE]    = _byteArrayKlassObj;
255        _typeArrayKlassObjs[T_SHORT]   = _shortArrayKlassObj;
256        _typeArrayKlassObjs[T_INT]     = _intArrayKlassObj;
257        _typeArrayKlassObjs[T_LONG]    = _longArrayKlassObj;
258
259        _methodKlassObj         = methodKlass::create_klass(CHECK);
260        _constMethodKlassObj    = constMethodKlass::create_klass(CHECK);
261        _methodDataKlassObj     = methodDataKlass::create_klass(CHECK);
262        _constantPoolKlassObj       = constantPoolKlass::create_klass(CHECK);
263        _constantPoolCacheKlassObj  = constantPoolCacheKlass::create_klass(CHECK);
264
265        _compiledICHolderKlassObj   = compiledICHolderKlass::create_klass(CHECK);
266        _systemObjArrayKlassObj     = objArrayKlassKlass::cast(objArrayKlassKlassObj())->allocate_system_objArray_klass(CHECK);
267
268        _the_empty_byte_array      = oopFactory::new_permanent_byteArray(0, CHECK);
269        _the_empty_short_array      = oopFactory::new_permanent_shortArray(0, CHECK);
270        _the_empty_int_array        = oopFactory::new_permanent_intArray(0, CHECK);
271        _the_empty_system_obj_array = oopFactory::new_system_objArray(0, CHECK);
272
273        _the_array_interfaces_array = oopFactory::new_system_objArray(2, CHECK);
274        _vm_exception               = oopFactory::new_symbol("vm exception holder", CHECK);
275      } else {
276
277        FileMapInfo *mapinfo = FileMapInfo::current_info();
278        char* buffer = mapinfo->region_base(CompactingPermGenGen::md);
279        void** vtbl_list = (void**)buffer;
280        init_self_patching_vtbl_list(vtbl_list,
281                                     CompactingPermGenGen::vtbl_list_size);
282      }
283    }
284
285    vmSymbols::initialize(CHECK);
286
287    SystemDictionary::initialize(CHECK);
288
289    klassOop ok = SystemDictionary::object_klass();
290
291    if (UseSharedSpaces) {
292      // Verify shared interfaces array.
293      assert(_the_array_interfaces_array->obj_at(0) ==
294             SystemDictionary::cloneable_klass(), "u3");
295      assert(_the_array_interfaces_array->obj_at(1) ==
296             SystemDictionary::serializable_klass(), "u3");
297
298      // Verify element klass for system obj array klass
299      assert(objArrayKlass::cast(_systemObjArrayKlassObj)->element_klass() == ok, "u1");
300      assert(objArrayKlass::cast(_systemObjArrayKlassObj)->bottom_klass() == ok, "u2");
301
302      // Verify super class for the classes created above
303      assert(Klass::cast(boolArrayKlassObj()     )->super() == ok, "u3");
304      assert(Klass::cast(charArrayKlassObj()     )->super() == ok, "u3");
305      assert(Klass::cast(singleArrayKlassObj()   )->super() == ok, "u3");
306      assert(Klass::cast(doubleArrayKlassObj()   )->super() == ok, "u3");
307      assert(Klass::cast(byteArrayKlassObj()     )->super() == ok, "u3");
308      assert(Klass::cast(shortArrayKlassObj()    )->super() == ok, "u3");
309      assert(Klass::cast(intArrayKlassObj()      )->super() == ok, "u3");
310      assert(Klass::cast(longArrayKlassObj()     )->super() == ok, "u3");
311      assert(Klass::cast(constantPoolKlassObj()  )->super() == ok, "u3");
312      assert(Klass::cast(systemObjArrayKlassObj())->super() == ok, "u3");
313    } else {
314      // Set up shared interfaces array.  (Do this before supers are set up.)
315      _the_array_interfaces_array->obj_at_put(0, SystemDictionary::cloneable_klass());
316      _the_array_interfaces_array->obj_at_put(1, SystemDictionary::serializable_klass());
317
318      // Set element klass for system obj array klass
319      objArrayKlass::cast(_systemObjArrayKlassObj)->set_element_klass(ok);
320      objArrayKlass::cast(_systemObjArrayKlassObj)->set_bottom_klass(ok);
321
322      // Set super class for the classes created above
323      Klass::cast(boolArrayKlassObj()     )->initialize_supers(ok, CHECK);
324      Klass::cast(charArrayKlassObj()     )->initialize_supers(ok, CHECK);
325      Klass::cast(singleArrayKlassObj()   )->initialize_supers(ok, CHECK);
326      Klass::cast(doubleArrayKlassObj()   )->initialize_supers(ok, CHECK);
327      Klass::cast(byteArrayKlassObj()     )->initialize_supers(ok, CHECK);
328      Klass::cast(shortArrayKlassObj()    )->initialize_supers(ok, CHECK);
329      Klass::cast(intArrayKlassObj()      )->initialize_supers(ok, CHECK);
330      Klass::cast(longArrayKlassObj()     )->initialize_supers(ok, CHECK);
331      Klass::cast(constantPoolKlassObj()  )->initialize_supers(ok, CHECK);
332      Klass::cast(systemObjArrayKlassObj())->initialize_supers(ok, CHECK);
333      Klass::cast(boolArrayKlassObj()     )->set_super(ok);
334      Klass::cast(charArrayKlassObj()     )->set_super(ok);
335      Klass::cast(singleArrayKlassObj()   )->set_super(ok);
336      Klass::cast(doubleArrayKlassObj()   )->set_super(ok);
337      Klass::cast(byteArrayKlassObj()     )->set_super(ok);
338      Klass::cast(shortArrayKlassObj()    )->set_super(ok);
339      Klass::cast(intArrayKlassObj()      )->set_super(ok);
340      Klass::cast(longArrayKlassObj()     )->set_super(ok);
341      Klass::cast(constantPoolKlassObj()  )->set_super(ok);
342      Klass::cast(systemObjArrayKlassObj())->set_super(ok);
343    }
344
345    Klass::cast(boolArrayKlassObj()     )->append_to_sibling_list();
346    Klass::cast(charArrayKlassObj()     )->append_to_sibling_list();
347    Klass::cast(singleArrayKlassObj()   )->append_to_sibling_list();
348    Klass::cast(doubleArrayKlassObj()   )->append_to_sibling_list();
349    Klass::cast(byteArrayKlassObj()     )->append_to_sibling_list();
350    Klass::cast(shortArrayKlassObj()    )->append_to_sibling_list();
351    Klass::cast(intArrayKlassObj()      )->append_to_sibling_list();
352    Klass::cast(longArrayKlassObj()     )->append_to_sibling_list();
353    Klass::cast(constantPoolKlassObj()  )->append_to_sibling_list();
354    Klass::cast(systemObjArrayKlassObj())->append_to_sibling_list();
355  } // end of core bootstrapping
356
357  // Initialize _objectArrayKlass after core bootstraping to make
358  // sure the super class is set up properly for _objectArrayKlass.
359  _objectArrayKlassObj = instanceKlass::
360    cast(SystemDictionary::object_klass())->array_klass(1, CHECK);
361  // Add the class to the class hierarchy manually to make sure that
362  // its vtable is initialized after core bootstrapping is completed.
363  Klass::cast(_objectArrayKlassObj)->append_to_sibling_list();
364
365  // Compute is_jdk version flags.
366  // Only 1.3 or later has the java.lang.Shutdown class.
367  // Only 1.4 or later has the java.lang.CharSequence interface.
368  // Only 1.5 or later has the java.lang.management.MemoryUsage class.
369  if (JDK_Version::is_pre_jdk16_version()) {
370    klassOop k = SystemDictionary::resolve_or_null(vmSymbolHandles::java_lang_management_MemoryUsage(), THREAD);
371    CLEAR_PENDING_EXCEPTION; // ignore exceptions
372    if (k == NULL) {
373      k = SystemDictionary::resolve_or_null(vmSymbolHandles::java_lang_CharSequence(), THREAD);
374      CLEAR_PENDING_EXCEPTION; // ignore exceptions
375      if (k == NULL) {
376        k = SystemDictionary::resolve_or_null(vmSymbolHandles::java_lang_Shutdown(), THREAD);
377        CLEAR_PENDING_EXCEPTION; // ignore exceptions
378        if (k == NULL) {
379          JDK_Version::set_jdk12x_version();
380        } else {
381          JDK_Version::set_jdk13x_version();
382        }
383      } else {
384          JDK_Version::set_jdk14x_version();
385      }
386    } else {
387          JDK_Version::set_jdk15x_version();
388    }
389  }
390
391  #ifdef ASSERT
392  if (FullGCALot) {
393    // Allocate an array of dummy objects.
394    // We'd like these to be at the bottom of the old generation,
395    // so that when we free one and then collect,
396    // (almost) the whole heap moves
397    // and we find out if we actually update all the oops correctly.
398    // But we can't allocate directly in the old generation,
399    // so we allocate wherever, and hope that the first collection
400    // moves these objects to the bottom of the old generation.
401    // We can allocate directly in the permanent generation, so we do.
402    int size;
403    if (UseConcMarkSweepGC) {
404      warning("Using +FullGCALot with concurrent mark sweep gc "
405              "will not force all objects to relocate");
406      size = FullGCALotDummies;
407    } else {
408      size = FullGCALotDummies * 2;
409    }
410    objArrayOop    naked_array = oopFactory::new_system_objArray(size, CHECK);
411    objArrayHandle dummy_array(THREAD, naked_array);
412    int i = 0;
413    while (i < size) {
414      if (!UseConcMarkSweepGC) {
415        // Allocate dummy in old generation
416        oop dummy = instanceKlass::cast(SystemDictionary::object_klass())->allocate_instance(CHECK);
417        dummy_array->obj_at_put(i++, dummy);
418      }
419      // Allocate dummy in permanent generation
420      oop dummy = instanceKlass::cast(SystemDictionary::object_klass())->allocate_permanent_instance(CHECK);
421      dummy_array->obj_at_put(i++, dummy);
422    }
423    {
424      // Only modify the global variable inside the mutex.
425      // If we had a race to here, the other dummy_array instances
426      // and their elements just get dropped on the floor, which is fine.
427      MutexLocker ml(FullGCALot_lock);
428      if (_fullgc_alot_dummy_array == NULL) {
429        _fullgc_alot_dummy_array = dummy_array();
430      }
431    }
432    assert(i == _fullgc_alot_dummy_array->length(), "just checking");
433  }
434  #endif
435}
436
437
438static inline void add_vtable(void** list, int* n, Klass* o, int count) {
439  list[(*n)++] = *(void**)&o->vtbl_value();
440  guarantee((*n) <= count, "vtable list too small.");
441}
442
443
444void Universe::init_self_patching_vtbl_list(void** list, int count) {
445  int n = 0;
446  { klassKlass o;             add_vtable(list, &n, &o, count); }
447  { arrayKlassKlass o;        add_vtable(list, &n, &o, count); }
448  { objArrayKlassKlass o;     add_vtable(list, &n, &o, count); }
449  { instanceKlassKlass o;     add_vtable(list, &n, &o, count); }
450  { instanceKlass o;          add_vtable(list, &n, &o, count); }
451  { instanceRefKlass o;       add_vtable(list, &n, &o, count); }
452  { typeArrayKlassKlass o;    add_vtable(list, &n, &o, count); }
453  { symbolKlass o;            add_vtable(list, &n, &o, count); }
454  { typeArrayKlass o;         add_vtable(list, &n, &o, count); }
455  { methodKlass o;            add_vtable(list, &n, &o, count); }
456  { constMethodKlass o;       add_vtable(list, &n, &o, count); }
457  { constantPoolKlass o;      add_vtable(list, &n, &o, count); }
458  { constantPoolCacheKlass o; add_vtable(list, &n, &o, count); }
459  { objArrayKlass o;          add_vtable(list, &n, &o, count); }
460  { methodDataKlass o;        add_vtable(list, &n, &o, count); }
461  { compiledICHolderKlass o;  add_vtable(list, &n, &o, count); }
462}
463
464
465class FixupMirrorClosure: public ObjectClosure {
466 public:
467  void do_object(oop obj) {
468    if (obj->is_klass()) {
469      EXCEPTION_MARK;
470      KlassHandle k(THREAD, klassOop(obj));
471      // We will never reach the CATCH below since Exceptions::_throw will cause
472      // the VM to exit if an exception is thrown during initialization
473      java_lang_Class::create_mirror(k, CATCH);
474      // This call unconditionally creates a new mirror for k,
475      // and links in k's component_mirror field if k is an array.
476      // If k is an objArray, k's element type must already have
477      // a mirror.  In other words, this closure must process
478      // the component type of an objArray k before it processes k.
479      // This works because the permgen iterator presents arrays
480      // and their component types in order of creation.
481    }
482  }
483};
484
485void Universe::initialize_basic_type_mirrors(TRAPS) {
486  if (UseSharedSpaces) {
487    assert(_int_mirror != NULL, "already loaded");
488    assert(_void_mirror == _mirrors[T_VOID], "consistently loaded");
489  } else {
490
491    assert(_int_mirror==NULL, "basic type mirrors already initialized");
492    _int_mirror     =
493      java_lang_Class::create_basic_type_mirror("int",    T_INT, CHECK);
494    _float_mirror   =
495      java_lang_Class::create_basic_type_mirror("float",  T_FLOAT,   CHECK);
496    _double_mirror  =
497      java_lang_Class::create_basic_type_mirror("double", T_DOUBLE,  CHECK);
498    _byte_mirror    =
499      java_lang_Class::create_basic_type_mirror("byte",   T_BYTE, CHECK);
500    _bool_mirror    =
501      java_lang_Class::create_basic_type_mirror("boolean",T_BOOLEAN, CHECK);
502    _char_mirror    =
503      java_lang_Class::create_basic_type_mirror("char",   T_CHAR, CHECK);
504    _long_mirror    =
505      java_lang_Class::create_basic_type_mirror("long",   T_LONG, CHECK);
506    _short_mirror   =
507      java_lang_Class::create_basic_type_mirror("short",  T_SHORT,   CHECK);
508    _void_mirror    =
509      java_lang_Class::create_basic_type_mirror("void",   T_VOID, CHECK);
510
511    _mirrors[T_INT]     = _int_mirror;
512    _mirrors[T_FLOAT]   = _float_mirror;
513    _mirrors[T_DOUBLE]  = _double_mirror;
514    _mirrors[T_BYTE]    = _byte_mirror;
515    _mirrors[T_BOOLEAN] = _bool_mirror;
516    _mirrors[T_CHAR]    = _char_mirror;
517    _mirrors[T_LONG]    = _long_mirror;
518    _mirrors[T_SHORT]   = _short_mirror;
519    _mirrors[T_VOID]    = _void_mirror;
520    //_mirrors[T_OBJECT]  = instanceKlass::cast(_object_klass)->java_mirror();
521    //_mirrors[T_ARRAY]   = instanceKlass::cast(_object_klass)->java_mirror();
522  }
523}
524
525void Universe::fixup_mirrors(TRAPS) {
526  // Bootstrap problem: all classes gets a mirror (java.lang.Class instance) assigned eagerly,
527  // but we cannot do that for classes created before java.lang.Class is loaded. Here we simply
528  // walk over permanent objects created so far (mostly classes) and fixup their mirrors. Note
529  // that the number of objects allocated at this point is very small.
530  assert(SystemDictionary::class_klass_loaded(), "java.lang.Class should be loaded");
531  FixupMirrorClosure blk;
532  Universe::heap()->permanent_object_iterate(&blk);
533}
534
535
536static bool has_run_finalizers_on_exit = false;
537
538void Universe::run_finalizers_on_exit() {
539  if (has_run_finalizers_on_exit) return;
540  has_run_finalizers_on_exit = true;
541
542  // Called on VM exit. This ought to be run in a separate thread.
543  if (TraceReferenceGC) tty->print_cr("Callback to run finalizers on exit");
544  {
545    PRESERVE_EXCEPTION_MARK;
546    KlassHandle finalizer_klass(THREAD, SystemDictionary::finalizer_klass());
547    JavaValue result(T_VOID);
548    JavaCalls::call_static(
549      &result,
550      finalizer_klass,
551      vmSymbolHandles::run_finalizers_on_exit_name(),
552      vmSymbolHandles::void_method_signature(),
553      THREAD
554    );
555    // Ignore any pending exceptions
556    CLEAR_PENDING_EXCEPTION;
557  }
558}
559
560
561// initialize_vtable could cause gc if
562// 1) we specified true to initialize_vtable and
563// 2) this ran after gc was enabled
564// In case those ever change we use handles for oops
565void Universe::reinitialize_vtable_of(KlassHandle k_h, TRAPS) {
566  // init vtable of k and all subclasses
567  Klass* ko = k_h()->klass_part();
568  klassVtable* vt = ko->vtable();
569  if (vt) vt->initialize_vtable(false, CHECK);
570  if (ko->oop_is_instance()) {
571    instanceKlass* ik = (instanceKlass*)ko;
572    for (KlassHandle s_h(THREAD, ik->subklass()); s_h() != NULL; s_h = (THREAD, s_h()->klass_part()->next_sibling())) {
573      reinitialize_vtable_of(s_h, CHECK);
574    }
575  }
576}
577
578
579void initialize_itable_for_klass(klassOop k, TRAPS) {
580  instanceKlass::cast(k)->itable()->initialize_itable(false, CHECK);
581}
582
583
584void Universe::reinitialize_itables(TRAPS) {
585  SystemDictionary::classes_do(initialize_itable_for_klass, CHECK);
586
587}
588
589
590bool Universe::on_page_boundary(void* addr) {
591  return ((uintptr_t) addr) % os::vm_page_size() == 0;
592}
593
594
595bool Universe::should_fill_in_stack_trace(Handle throwable) {
596  // never attempt to fill in the stack trace of preallocated errors that do not have
597  // backtrace. These errors are kept alive forever and may be "re-used" when all
598  // preallocated errors with backtrace have been consumed. Also need to avoid
599  // a potential loop which could happen if an out of memory occurs when attempting
600  // to allocate the backtrace.
601  return ((throwable() != Universe::_out_of_memory_error_java_heap) &&
602          (throwable() != Universe::_out_of_memory_error_perm_gen)  &&
603          (throwable() != Universe::_out_of_memory_error_array_size) &&
604          (throwable() != Universe::_out_of_memory_error_gc_overhead_limit));
605}
606
607
608oop Universe::gen_out_of_memory_error(oop default_err) {
609  // generate an out of memory error:
610  // - if there is a preallocated error with backtrace available then return it wth
611  //   a filled in stack trace.
612  // - if there are no preallocated errors with backtrace available then return
613  //   an error without backtrace.
614  int next;
615  if (_preallocated_out_of_memory_error_avail_count > 0) {
616    next = (int)Atomic::add(-1, &_preallocated_out_of_memory_error_avail_count);
617    assert(next < (int)PreallocatedOutOfMemoryErrorCount, "avail count is corrupt");
618  } else {
619    next = -1;
620  }
621  if (next < 0) {
622    // all preallocated errors have been used.
623    // return default
624    return default_err;
625  } else {
626    // get the error object at the slot and set set it to NULL so that the
627    // array isn't keeping it alive anymore.
628    oop exc = preallocated_out_of_memory_errors()->obj_at(next);
629    assert(exc != NULL, "slot has been used already");
630    preallocated_out_of_memory_errors()->obj_at_put(next, NULL);
631
632    // use the message from the default error
633    oop msg = java_lang_Throwable::message(default_err);
634    assert(msg != NULL, "no message");
635    java_lang_Throwable::set_message(exc, msg);
636
637    // populate the stack trace and return it.
638    java_lang_Throwable::fill_in_stack_trace_of_preallocated_backtrace(exc);
639    return exc;
640  }
641}
642
643static intptr_t non_oop_bits = 0;
644
645void* Universe::non_oop_word() {
646  // Neither the high bits nor the low bits of this value is allowed
647  // to look like (respectively) the high or low bits of a real oop.
648  //
649  // High and low are CPU-specific notions, but low always includes
650  // the low-order bit.  Since oops are always aligned at least mod 4,
651  // setting the low-order bit will ensure that the low half of the
652  // word will never look like that of a real oop.
653  //
654  // Using the OS-supplied non-memory-address word (usually 0 or -1)
655  // will take care of the high bits, however many there are.
656
657  if (non_oop_bits == 0) {
658    non_oop_bits = (intptr_t)os::non_memory_address_word() | 1;
659  }
660
661  return (void*)non_oop_bits;
662}
663
664jint universe_init() {
665  assert(!Universe::_fully_initialized, "called after initialize_vtables");
666  guarantee(1 << LogHeapWordSize == sizeof(HeapWord),
667         "LogHeapWordSize is incorrect.");
668  guarantee(sizeof(oop) >= sizeof(HeapWord), "HeapWord larger than oop?");
669  guarantee(sizeof(oop) % sizeof(HeapWord) == 0,
670         "oop size is not not a multiple of HeapWord size");
671  TraceTime timer("Genesis", TraceStartupTime);
672  GC_locker::lock();  // do not allow gc during bootstrapping
673  JavaClasses::compute_hard_coded_offsets();
674
675  // Get map info from shared archive file.
676  if (DumpSharedSpaces)
677    UseSharedSpaces = false;
678
679  FileMapInfo* mapinfo = NULL;
680  if (UseSharedSpaces) {
681    mapinfo = NEW_C_HEAP_OBJ(FileMapInfo);
682    memset(mapinfo, 0, sizeof(FileMapInfo));
683
684    // Open the shared archive file, read and validate the header. If
685    // initialization files, shared spaces [UseSharedSpaces] are
686    // disabled and the file is closed.
687
688    if (mapinfo->initialize()) {
689      FileMapInfo::set_current_info(mapinfo);
690    } else {
691      assert(!mapinfo->is_open() && !UseSharedSpaces,
692             "archive file not closed or shared spaces not disabled.");
693    }
694  }
695
696  jint status = Universe::initialize_heap();
697  if (status != JNI_OK) {
698    return status;
699  }
700
701  // We have a heap so create the methodOop caches before
702  // CompactingPermGenGen::initialize_oops() tries to populate them.
703  Universe::_finalizer_register_cache = new LatestMethodOopCache();
704  Universe::_loader_addClass_cache    = new LatestMethodOopCache();
705  Universe::_reflect_invoke_cache     = new ActiveMethodOopsCache();
706
707  if (UseSharedSpaces) {
708
709    // Read the data structures supporting the shared spaces (shared
710    // system dictionary, symbol table, etc.).  After that, access to
711    // the file (other than the mapped regions) is no longer needed, and
712    // the file is closed. Closing the file does not affect the
713    // currently mapped regions.
714
715    CompactingPermGenGen::initialize_oops();
716    mapinfo->close();
717
718  } else {
719    SymbolTable::create_table();
720    StringTable::create_table();
721    ClassLoader::create_package_info_table();
722  }
723
724  return JNI_OK;
725}
726
727jint Universe::initialize_heap() {
728
729  if (UseParallelGC) {
730#ifndef SERIALGC
731    Universe::_collectedHeap = new ParallelScavengeHeap();
732#else  // SERIALGC
733    fatal("UseParallelGC not supported in java kernel vm.");
734#endif // SERIALGC
735
736  } else {
737    GenCollectorPolicy *gc_policy;
738
739    if (UseSerialGC) {
740      gc_policy = new MarkSweepPolicy();
741    } else if (UseConcMarkSweepGC) {
742#ifndef SERIALGC
743      if (UseAdaptiveSizePolicy) {
744        gc_policy = new ASConcurrentMarkSweepPolicy();
745      } else {
746        gc_policy = new ConcurrentMarkSweepPolicy();
747      }
748#else   // SERIALGC
749    fatal("UseConcMarkSweepGC not supported in java kernel vm.");
750#endif // SERIALGC
751    } else { // default old generation
752      gc_policy = new MarkSweepPolicy();
753    }
754
755    Universe::_collectedHeap = new GenCollectedHeap(gc_policy);
756  }
757
758  jint status = Universe::heap()->initialize();
759  if (status != JNI_OK) {
760    return status;
761  }
762
763  // We will never reach the CATCH below since Exceptions::_throw will cause
764  // the VM to exit if an exception is thrown during initialization
765
766  if (UseTLAB) {
767    assert(Universe::heap()->supports_tlab_allocation(),
768           "Should support thread-local allocation buffers");
769    ThreadLocalAllocBuffer::startup_initialization();
770  }
771  return JNI_OK;
772}
773
774// It's the caller's repsonsibility to ensure glitch-freedom
775// (if required).
776void Universe::update_heap_info_at_gc() {
777  _heap_capacity_at_last_gc = heap()->capacity();
778  _heap_used_at_last_gc     = heap()->used();
779}
780
781
782
783void universe2_init() {
784  EXCEPTION_MARK;
785  Universe::genesis(CATCH);
786  // Although we'd like to verify here that the state of the heap
787  // is good, we can't because the main thread has not yet added
788  // itself to the threads list (so, using current interfaces
789  // we can't "fill" its TLAB), unless TLABs are disabled.
790  if (VerifyBeforeGC && !UseTLAB &&
791      Universe::heap()->total_collections() >= VerifyGCStartAt) {
792     Universe::heap()->prepare_for_verify();
793     Universe::verify();   // make sure we're starting with a clean slate
794  }
795}
796
797
798// This function is defined in JVM.cpp
799extern void initialize_converter_functions();
800
801bool universe_post_init() {
802  Universe::_fully_initialized = true;
803  EXCEPTION_MARK;
804  { ResourceMark rm;
805    Interpreter::initialize();      // needed for interpreter entry points
806    if (!UseSharedSpaces) {
807      KlassHandle ok_h(THREAD, SystemDictionary::object_klass());
808      Universe::reinitialize_vtable_of(ok_h, CHECK_false);
809      Universe::reinitialize_itables(CHECK_false);
810    }
811  }
812
813  klassOop k;
814  instanceKlassHandle k_h;
815  if (!UseSharedSpaces) {
816    // Setup preallocated empty java.lang.Class array
817    Universe::_the_empty_class_klass_array = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_false);
818    // Setup preallocated OutOfMemoryError errors
819    k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_OutOfMemoryError(), true, CHECK_false);
820    k_h = instanceKlassHandle(THREAD, k);
821    Universe::_out_of_memory_error_java_heap = k_h->allocate_permanent_instance(CHECK_false);
822    Universe::_out_of_memory_error_perm_gen = k_h->allocate_permanent_instance(CHECK_false);
823    Universe::_out_of_memory_error_array_size = k_h->allocate_permanent_instance(CHECK_false);
824    Universe::_out_of_memory_error_gc_overhead_limit =
825      k_h->allocate_permanent_instance(CHECK_false);
826
827    // Setup preallocated NullPointerException
828    // (this is currently used for a cheap & dirty solution in compiler exception handling)
829    k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_NullPointerException(), true, CHECK_false);
830    Universe::_null_ptr_exception_instance = instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
831    // Setup preallocated ArithmeticException
832    // (this is currently used for a cheap & dirty solution in compiler exception handling)
833    k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_ArithmeticException(), true, CHECK_false);
834    Universe::_arithmetic_exception_instance = instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
835    // Virtual Machine Error for when we get into a situation we can't resolve
836    k = SystemDictionary::resolve_or_fail(
837      vmSymbolHandles::java_lang_VirtualMachineError(), true, CHECK_false);
838    bool linked = instanceKlass::cast(k)->link_class_or_fail(CHECK_false);
839    if (!linked) {
840      tty->print_cr("Unable to link/verify VirtualMachineError class");
841      return false; // initialization failed
842    }
843    Universe::_virtual_machine_error_instance =
844      instanceKlass::cast(k)->allocate_permanent_instance(CHECK_false);
845  }
846  if (!DumpSharedSpaces) {
847    // These are the only Java fields that are currently set during shared space dumping.
848    // We prefer to not handle this generally, so we always reinitialize these detail messages.
849    Handle msg = java_lang_String::create_from_str("Java heap space", CHECK_false);
850    java_lang_Throwable::set_message(Universe::_out_of_memory_error_java_heap, msg());
851
852    msg = java_lang_String::create_from_str("PermGen space", CHECK_false);
853    java_lang_Throwable::set_message(Universe::_out_of_memory_error_perm_gen, msg());
854
855    msg = java_lang_String::create_from_str("Requested array size exceeds VM limit", CHECK_false);
856    java_lang_Throwable::set_message(Universe::_out_of_memory_error_array_size, msg());
857
858    msg = java_lang_String::create_from_str("GC overhead limit exceeded", CHECK_false);
859    java_lang_Throwable::set_message(Universe::_out_of_memory_error_gc_overhead_limit, msg());
860
861    msg = java_lang_String::create_from_str("/ by zero", CHECK_false);
862    java_lang_Throwable::set_message(Universe::_arithmetic_exception_instance, msg());
863
864    // Setup the array of errors that have preallocated backtrace
865    k = Universe::_out_of_memory_error_java_heap->klass();
866    assert(k->klass_part()->name() == vmSymbols::java_lang_OutOfMemoryError(), "should be out of memory error");
867    k_h = instanceKlassHandle(THREAD, k);
868
869    int len = (StackTraceInThrowable) ? (int)PreallocatedOutOfMemoryErrorCount : 0;
870    Universe::_preallocated_out_of_memory_error_array = oopFactory::new_objArray(k_h(), len, CHECK_false);
871    for (int i=0; i<len; i++) {
872      oop err = k_h->allocate_permanent_instance(CHECK_false);
873      Handle err_h = Handle(THREAD, err);
874      java_lang_Throwable::allocate_backtrace(err_h, CHECK_false);
875      Universe::preallocated_out_of_memory_errors()->obj_at_put(i, err_h());
876    }
877    Universe::_preallocated_out_of_memory_error_avail_count = (jint)len;
878  }
879
880
881  // Setup static method for registering finalizers
882  // The finalizer klass must be linked before looking up the method, in
883  // case it needs to get rewritten.
884  instanceKlass::cast(SystemDictionary::finalizer_klass())->link_class(CHECK_false);
885  methodOop m = instanceKlass::cast(SystemDictionary::finalizer_klass())->find_method(
886                                  vmSymbols::register_method_name(),
887                                  vmSymbols::register_method_signature());
888  if (m == NULL || !m->is_static()) {
889    THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
890      "java.lang.ref.Finalizer.register", false);
891  }
892  Universe::_finalizer_register_cache->init(
893    SystemDictionary::finalizer_klass(), m, CHECK_false);
894
895  // Resolve on first use and initialize class.
896  // Note: No race-condition here, since a resolve will always return the same result
897
898  // Setup method for security checks
899  k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_reflect_Method(), true, CHECK_false);
900  k_h = instanceKlassHandle(THREAD, k);
901  k_h->link_class(CHECK_false);
902  m = k_h->find_method(vmSymbols::invoke_name(), vmSymbols::object_array_object_object_signature());
903  if (m == NULL || m->is_static()) {
904    THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
905      "java.lang.reflect.Method.invoke", false);
906  }
907  Universe::_reflect_invoke_cache->init(k_h(), m, CHECK_false);
908
909  // Setup method for registering loaded classes in class loader vector
910  instanceKlass::cast(SystemDictionary::classloader_klass())->link_class(CHECK_false);
911  m = instanceKlass::cast(SystemDictionary::classloader_klass())->find_method(vmSymbols::addClass_name(), vmSymbols::class_void_signature());
912  if (m == NULL || m->is_static()) {
913    THROW_MSG_(vmSymbols::java_lang_NoSuchMethodException(),
914      "java.lang.ClassLoader.addClass", false);
915  }
916  Universe::_loader_addClass_cache->init(
917    SystemDictionary::classloader_klass(), m, CHECK_false);
918
919  // The folowing is initializing converter functions for serialization in
920  // JVM.cpp. If we clean up the StrictMath code above we may want to find
921  // a better solution for this as well.
922  initialize_converter_functions();
923
924  // This needs to be done before the first scavenge/gc, since
925  // it's an input to soft ref clearing policy.
926  Universe::update_heap_info_at_gc();
927
928  // ("weak") refs processing infrastructure initialization
929  Universe::heap()->post_initialize();
930
931  GC_locker::unlock();  // allow gc after bootstrapping
932
933  MemoryService::set_universe_heap(Universe::_collectedHeap);
934  return true;
935}
936
937
938void Universe::compute_base_vtable_size() {
939  _base_vtable_size = ClassLoader::compute_Object_vtable();
940}
941
942
943// %%% The Universe::flush_foo methods belong in CodeCache.
944
945// Flushes compiled methods dependent on dependee.
946void Universe::flush_dependents_on(instanceKlassHandle dependee) {
947  assert_lock_strong(Compile_lock);
948
949  if (CodeCache::number_of_nmethods_with_dependencies() == 0) return;
950
951  // CodeCache can only be updated by a thread_in_VM and they will all be
952  // stopped dring the safepoint so CodeCache will be safe to update without
953  // holding the CodeCache_lock.
954
955  DepChange changes(dependee);
956
957  // Compute the dependent nmethods
958  if (CodeCache::mark_for_deoptimization(changes) > 0) {
959    // At least one nmethod has been marked for deoptimization
960    VM_Deoptimize op;
961    VMThread::execute(&op);
962  }
963}
964
965#ifdef HOTSWAP
966// Flushes compiled methods dependent on dependee in the evolutionary sense
967void Universe::flush_evol_dependents_on(instanceKlassHandle ev_k_h) {
968  // --- Compile_lock is not held. However we are at a safepoint.
969  assert_locked_or_safepoint(Compile_lock);
970  if (CodeCache::number_of_nmethods_with_dependencies() == 0) return;
971
972  // CodeCache can only be updated by a thread_in_VM and they will all be
973  // stopped dring the safepoint so CodeCache will be safe to update without
974  // holding the CodeCache_lock.
975
976  // Compute the dependent nmethods
977  if (CodeCache::mark_for_evol_deoptimization(ev_k_h) > 0) {
978    // At least one nmethod has been marked for deoptimization
979
980    // All this already happens inside a VM_Operation, so we'll do all the work here.
981    // Stuff copied from VM_Deoptimize and modified slightly.
982
983    // We do not want any GCs to happen while we are in the middle of this VM operation
984    ResourceMark rm;
985    DeoptimizationMarker dm;
986
987    // Deoptimize all activations depending on marked nmethods
988    Deoptimization::deoptimize_dependents();
989
990    // Make the dependent methods not entrant (in VM_Deoptimize they are made zombies)
991    CodeCache::make_marked_nmethods_not_entrant();
992  }
993}
994#endif // HOTSWAP
995
996
997// Flushes compiled methods dependent on dependee
998void Universe::flush_dependents_on_method(methodHandle m_h) {
999  // --- Compile_lock is not held. However we are at a safepoint.
1000  assert_locked_or_safepoint(Compile_lock);
1001
1002  // CodeCache can only be updated by a thread_in_VM and they will all be
1003  // stopped dring the safepoint so CodeCache will be safe to update without
1004  // holding the CodeCache_lock.
1005
1006  // Compute the dependent nmethods
1007  if (CodeCache::mark_for_deoptimization(m_h()) > 0) {
1008    // At least one nmethod has been marked for deoptimization
1009
1010    // All this already happens inside a VM_Operation, so we'll do all the work here.
1011    // Stuff copied from VM_Deoptimize and modified slightly.
1012
1013    // We do not want any GCs to happen while we are in the middle of this VM operation
1014    ResourceMark rm;
1015    DeoptimizationMarker dm;
1016
1017    // Deoptimize all activations depending on marked nmethods
1018    Deoptimization::deoptimize_dependents();
1019
1020    // Make the dependent methods not entrant (in VM_Deoptimize they are made zombies)
1021    CodeCache::make_marked_nmethods_not_entrant();
1022  }
1023}
1024
1025void Universe::print() { print_on(gclog_or_tty); }
1026
1027void Universe::print_on(outputStream* st) {
1028  st->print_cr("Heap");
1029  heap()->print_on(st);
1030}
1031
1032void Universe::print_heap_at_SIGBREAK() {
1033  if (PrintHeapAtSIGBREAK) {
1034    MutexLocker hl(Heap_lock);
1035    print_on(tty);
1036    tty->cr();
1037    tty->flush();
1038  }
1039}
1040
1041void Universe::print_heap_before_gc(outputStream* st) {
1042  st->print_cr("{Heap before GC invocations=%u (full %u):",
1043               heap()->total_collections(),
1044               heap()->total_full_collections());
1045  heap()->print_on(st);
1046}
1047
1048void Universe::print_heap_after_gc(outputStream* st) {
1049  st->print_cr("Heap after GC invocations=%u (full %u):",
1050               heap()->total_collections(),
1051               heap()->total_full_collections());
1052  heap()->print_on(st);
1053  st->print_cr("}");
1054}
1055
1056void Universe::verify(bool allow_dirty, bool silent) {
1057  if (SharedSkipVerify) {
1058    return;
1059  }
1060
1061  // The use of _verify_in_progress is a temporary work around for
1062  // 6320749.  Don't bother with a creating a class to set and clear
1063  // it since it is only used in this method and the control flow is
1064  // straight forward.
1065  _verify_in_progress = true;
1066
1067  COMPILER2_PRESENT(
1068    assert(!DerivedPointerTable::is_active(),
1069         "DPT should not be active during verification "
1070         "(of thread stacks below)");
1071  )
1072
1073  ResourceMark rm;
1074  HandleMark hm;  // Handles created during verification can be zapped
1075  _verify_count++;
1076
1077  if (!silent) gclog_or_tty->print("[Verifying ");
1078  if (!silent) gclog_or_tty->print("threads ");
1079  Threads::verify();
1080  heap()->verify(allow_dirty, silent);
1081
1082  if (!silent) gclog_or_tty->print("syms ");
1083  SymbolTable::verify();
1084  if (!silent) gclog_or_tty->print("strs ");
1085  StringTable::verify();
1086  {
1087    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1088    if (!silent) gclog_or_tty->print("zone ");
1089    CodeCache::verify();
1090  }
1091  if (!silent) gclog_or_tty->print("dict ");
1092  SystemDictionary::verify();
1093  if (!silent) gclog_or_tty->print("hand ");
1094  JNIHandles::verify();
1095  if (!silent) gclog_or_tty->print("C-heap ");
1096  os::check_heap();
1097  if (!silent) gclog_or_tty->print_cr("]");
1098
1099  _verify_in_progress = false;
1100}
1101
1102// Oop verification (see MacroAssembler::verify_oop)
1103
1104static uintptr_t _verify_oop_data[2]   = {0, (uintptr_t)-1};
1105static uintptr_t _verify_klass_data[2] = {0, (uintptr_t)-1};
1106
1107
1108static void calculate_verify_data(uintptr_t verify_data[2],
1109                                  HeapWord* low_boundary,
1110                                  HeapWord* high_boundary) {
1111  assert(low_boundary < high_boundary, "bad interval");
1112
1113  // decide which low-order bits we require to be clear:
1114  size_t alignSize = MinObjAlignmentInBytes;
1115  size_t min_object_size = oopDesc::header_size();
1116
1117  // make an inclusive limit:
1118  uintptr_t max = (uintptr_t)high_boundary - min_object_size*wordSize;
1119  uintptr_t min = (uintptr_t)low_boundary;
1120  assert(min < max, "bad interval");
1121  uintptr_t diff = max ^ min;
1122
1123  // throw away enough low-order bits to make the diff vanish
1124  uintptr_t mask = (uintptr_t)(-1);
1125  while ((mask & diff) != 0)
1126    mask <<= 1;
1127  uintptr_t bits = (min & mask);
1128  assert(bits == (max & mask), "correct mask");
1129  // check an intermediate value between min and max, just to make sure:
1130  assert(bits == ((min + (max-min)/2) & mask), "correct mask");
1131
1132  // require address alignment, too:
1133  mask |= (alignSize - 1);
1134
1135  if (!(verify_data[0] == 0 && verify_data[1] == (uintptr_t)-1)) {
1136    assert(verify_data[0] == mask && verify_data[1] == bits, "mask stability");
1137  }
1138  verify_data[0] = mask;
1139  verify_data[1] = bits;
1140}
1141
1142
1143// Oop verification (see MacroAssembler::verify_oop)
1144#ifndef PRODUCT
1145
1146uintptr_t Universe::verify_oop_mask() {
1147  MemRegion m = heap()->reserved_region();
1148  calculate_verify_data(_verify_oop_data,
1149                        m.start(),
1150                        m.end());
1151  return _verify_oop_data[0];
1152}
1153
1154
1155
1156uintptr_t Universe::verify_oop_bits() {
1157  verify_oop_mask();
1158  return _verify_oop_data[1];
1159}
1160
1161
1162uintptr_t Universe::verify_klass_mask() {
1163  /* $$$
1164  // A klass can never live in the new space.  Since the new and old
1165  // spaces can change size, we must settle for bounds-checking against
1166  // the bottom of the world, plus the smallest possible new and old
1167  // space sizes that may arise during execution.
1168  size_t min_new_size = Universe::new_size();   // in bytes
1169  size_t min_old_size = Universe::old_size();   // in bytes
1170  calculate_verify_data(_verify_klass_data,
1171          (HeapWord*)((uintptr_t)_new_gen->low_boundary + min_new_size + min_old_size),
1172          _perm_gen->high_boundary);
1173                        */
1174  // Why doesn't the above just say that klass's always live in the perm
1175  // gen?  I'll see if that seems to work...
1176  MemRegion permanent_reserved;
1177  switch (Universe::heap()->kind()) {
1178  default:
1179    // ???: What if a CollectedHeap doesn't have a permanent generation?
1180    ShouldNotReachHere();
1181    break;
1182  case CollectedHeap::GenCollectedHeap: {
1183    GenCollectedHeap* gch = (GenCollectedHeap*) Universe::heap();
1184    permanent_reserved = gch->perm_gen()->reserved();
1185    break;
1186  }
1187#ifndef SERIALGC
1188  case CollectedHeap::ParallelScavengeHeap: {
1189    ParallelScavengeHeap* psh = (ParallelScavengeHeap*) Universe::heap();
1190    permanent_reserved = psh->perm_gen()->reserved();
1191    break;
1192  }
1193#endif // SERIALGC
1194  }
1195  calculate_verify_data(_verify_klass_data,
1196                        permanent_reserved.start(),
1197                        permanent_reserved.end());
1198
1199  return _verify_klass_data[0];
1200}
1201
1202
1203
1204uintptr_t Universe::verify_klass_bits() {
1205  verify_klass_mask();
1206  return _verify_klass_data[1];
1207}
1208
1209
1210uintptr_t Universe::verify_mark_mask() {
1211  return markOopDesc::lock_mask_in_place;
1212}
1213
1214
1215
1216uintptr_t Universe::verify_mark_bits() {
1217  intptr_t mask = verify_mark_mask();
1218  intptr_t bits = (intptr_t)markOopDesc::prototype();
1219  assert((bits & ~mask) == 0, "no stray header bits");
1220  return bits;
1221}
1222#endif // PRODUCT
1223
1224
1225void Universe::compute_verify_oop_data() {
1226  verify_oop_mask();
1227  verify_oop_bits();
1228  verify_mark_mask();
1229  verify_mark_bits();
1230  verify_klass_mask();
1231  verify_klass_bits();
1232}
1233
1234
1235void CommonMethodOopCache::init(klassOop k, methodOop m, TRAPS) {
1236  if (!UseSharedSpaces) {
1237    _klass = k;
1238  }
1239#ifndef PRODUCT
1240  else {
1241    // sharing initilization should have already set up _klass
1242    assert(_klass != NULL, "just checking");
1243  }
1244#endif
1245
1246  _method_idnum = m->method_idnum();
1247  assert(_method_idnum >= 0, "sanity check");
1248}
1249
1250
1251ActiveMethodOopsCache::~ActiveMethodOopsCache() {
1252  if (_prev_methods != NULL) {
1253    for (int i = _prev_methods->length() - 1; i >= 0; i--) {
1254      jweak method_ref = _prev_methods->at(i);
1255      if (method_ref != NULL) {
1256        JNIHandles::destroy_weak_global(method_ref);
1257      }
1258    }
1259    delete _prev_methods;
1260    _prev_methods = NULL;
1261  }
1262}
1263
1264
1265void ActiveMethodOopsCache::add_previous_version(const methodOop method) {
1266  assert(Thread::current()->is_VM_thread(),
1267    "only VMThread can add previous versions");
1268
1269  if (_prev_methods == NULL) {
1270    // This is the first previous version so make some space.
1271    // Start with 2 elements under the assumption that the class
1272    // won't be redefined much.
1273    _prev_methods = new (ResourceObj::C_HEAP) GrowableArray<jweak>(2, true);
1274  }
1275
1276  // RC_TRACE macro has an embedded ResourceMark
1277  RC_TRACE(0x00000100,
1278    ("add: %s(%s): adding prev version ref for cached method @%d",
1279    method->name()->as_C_string(), method->signature()->as_C_string(),
1280    _prev_methods->length()));
1281
1282  methodHandle method_h(method);
1283  jweak method_ref = JNIHandles::make_weak_global(method_h);
1284  _prev_methods->append(method_ref);
1285
1286  // Using weak references allows previous versions of the cached
1287  // method to be GC'ed when they are no longer needed. Since the
1288  // caller is the VMThread and we are at a safepoint, this is a good
1289  // time to clear out unused weak references.
1290
1291  for (int i = _prev_methods->length() - 1; i >= 0; i--) {
1292    jweak method_ref = _prev_methods->at(i);
1293    assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
1294    if (method_ref == NULL) {
1295      _prev_methods->remove_at(i);
1296      // Since we are traversing the array backwards, we don't have to
1297      // do anything special with the index.
1298      continue;  // robustness
1299    }
1300
1301    methodOop m = (methodOop)JNIHandles::resolve(method_ref);
1302    if (m == NULL) {
1303      // this method entry has been GC'ed so remove it
1304      JNIHandles::destroy_weak_global(method_ref);
1305      _prev_methods->remove_at(i);
1306    } else {
1307      // RC_TRACE macro has an embedded ResourceMark
1308      RC_TRACE(0x00000400, ("add: %s(%s): previous cached method @%d is alive",
1309        m->name()->as_C_string(), m->signature()->as_C_string(), i));
1310    }
1311  }
1312} // end add_previous_version()
1313
1314
1315bool ActiveMethodOopsCache::is_same_method(const methodOop method) const {
1316  instanceKlass* ik = instanceKlass::cast(klass());
1317  methodOop check_method = ik->method_with_idnum(method_idnum());
1318  assert(check_method != NULL, "sanity check");
1319  if (check_method == method) {
1320    // done with the easy case
1321    return true;
1322  }
1323
1324  if (_prev_methods != NULL) {
1325    // The cached method has been redefined at least once so search
1326    // the previous versions for a match.
1327    for (int i = 0; i < _prev_methods->length(); i++) {
1328      jweak method_ref = _prev_methods->at(i);
1329      assert(method_ref != NULL, "weak method ref was unexpectedly cleared");
1330      if (method_ref == NULL) {
1331        continue;  // robustness
1332      }
1333
1334      check_method = (methodOop)JNIHandles::resolve(method_ref);
1335      if (check_method == method) {
1336        // a previous version matches
1337        return true;
1338      }
1339    }
1340  }
1341
1342  // either no previous versions or no previous version matched
1343  return false;
1344}
1345
1346
1347methodOop LatestMethodOopCache::get_methodOop() {
1348  instanceKlass* ik = instanceKlass::cast(klass());
1349  methodOop m = ik->method_with_idnum(method_idnum());
1350  assert(m != NULL, "sanity check");
1351  return m;
1352}
1353
1354
1355#ifdef ASSERT
1356// Release dummy object(s) at bottom of heap
1357bool Universe::release_fullgc_alot_dummy() {
1358  MutexLocker ml(FullGCALot_lock);
1359  if (_fullgc_alot_dummy_array != NULL) {
1360    if (_fullgc_alot_dummy_next >= _fullgc_alot_dummy_array->length()) {
1361      // No more dummies to release, release entire array instead
1362      _fullgc_alot_dummy_array = NULL;
1363      return false;
1364    }
1365    if (!UseConcMarkSweepGC) {
1366      // Release dummy at bottom of old generation
1367      _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1368    }
1369    // Release dummy at bottom of permanent generation
1370    _fullgc_alot_dummy_array->obj_at_put(_fullgc_alot_dummy_next++, NULL);
1371  }
1372  return true;
1373}
1374
1375#endif // ASSERT
1376