1 /*
   2  * Copyright (c) 2016, 2017, 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 #ifndef SHARE_VM_AOT_AOTCODEHEAP_HPP
  25 #define SHARE_VM_AOT_AOTCODEHEAP_HPP
  26 
  27 #include "aot/aotCompiledMethod.hpp"
  28 #include "classfile/symbolTable.hpp"
  29 #include "oops/metadata.hpp"
  30 #include "oops/method.hpp"
  31 
  32 enum CodeState {
  33   not_set = 0, // _aot fields is not set yet
  34   in_use  = 1, // _aot field is set to corresponding AOTCompiledMethod
  35   invalid = 2  // AOT code is invalidated because dependencies failed
  36 };
  37 
  38 typedef struct {
  39   AOTCompiledMethod* _aot;
  40   CodeState _state; // State change cases: not_set->in_use, not_set->invalid
  41 } CodeToAMethod;
  42 
  43 class ClassLoaderData;
  44 
  45 class AOTClass {
  46 public:
  47   ClassLoaderData* _classloader;
  48 };
  49 
  50 typedef struct {
  51   int _name_offset;
  52   int _code_offset;
  53   int _meta_offset;
  54   int _metadata_got_offset;
  55   int _metadata_got_size;
  56   int _code_id;
  57 } AOTMethodOffsets;
  58 
  59 typedef struct {
  60   const char* _name;
  61   address     _code;
  62   aot_metadata* _meta;
  63   jlong*      _state_adr;
  64   address     _metadata_table;
  65   int         _metadata_size;
  66 } AOTMethodData;
  67 
  68 typedef struct {
  69   int _got_index;
  70   int _class_id;
  71   int _compiled_methods_offset;
  72   int _dependent_methods_offset;
  73   uint64_t _fingerprint;
  74 } AOTKlassData;
  75 
  76 typedef struct {
  77   int _version;
  78   int _class_count;
  79   int _method_count;
  80   int _metaspace_got_size;
  81   int _metadata_got_size;
  82   int _oop_got_size;
  83   int _jvm_version_offset;
  84 
  85   enum {
  86     AOT_SHARED_VERSION = 1
  87   };
  88 } AOTHeader;
  89 
  90 typedef struct {
  91   enum { CONFIG_SIZE = 7 * jintSize + 11 };
  92   // 7 int values
  93   int _config_size;
  94   int _narrowOopShift;
  95   int _narrowKlassShift;
  96   int _contendedPaddingWidth;
  97   int _fieldsAllocationStyle;
  98   int _objectAlignment;
  99   int _codeSegmentSize;
 100   // byte[11] array map to boolean values here
 101   bool _debug_VM;
 102   bool _useCompressedOops;
 103   bool _useCompressedClassPointers;
 104   bool _compactFields;
 105   bool _useG1GC;
 106   bool _useTLAB;
 107   bool _useBiasedLocking;
 108   bool _tieredAOT;
 109   bool _enableContended;
 110   bool _restrictContended;
 111   bool _omitAssertions;
 112 } AOTConfiguration;
 113 
 114 class AOTLib : public CHeapObj<mtCode> {
 115   static bool _narrow_oop_shift_initialized;
 116   static int _narrow_oop_shift;
 117   static int _narrow_klass_shift;
 118 
 119   bool _valid;
 120   void* _dl_handle;
 121   const int _dso_id;
 122   const char* _name;
 123   // VM configuration during AOT compilation
 124   AOTConfiguration* _config;
 125   AOTHeader* _header;
 126 
 127   void handle_config_error(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 128 public:
 129   AOTLib(void* handle, const char* name, int dso_id);
 130   virtual ~AOTLib();
 131   static int  narrow_oop_shift() { return _narrow_oop_shift; }
 132   static int  narrow_klass_shift() { return _narrow_klass_shift; }
 133   static bool narrow_oop_shift_initialized() { return _narrow_oop_shift_initialized; }
 134 
 135   bool is_valid() const {
 136     return _valid;
 137   }
 138   const char* name() const {
 139     return _name;
 140   }
 141   void* dl_handle() const {
 142     return _dl_handle;
 143   }
 144   int id() const {
 145     return _dso_id;
 146   }
 147   AOTHeader* header() const {
 148     return _header;
 149   }
 150   AOTConfiguration* config() const {
 151     return _config;
 152   }
 153   void verify_config();
 154   void verify_flag(bool aot_flag, bool flag, const char* name);
 155   void verify_flag(int  aot_flag, int  flag, const char* name);
 156 
 157   address load_symbol(const char *name);
 158 };
 159 
 160 
 161 class AOTCodeHeap : public CodeHeap {
 162   AOTLib* _lib;
 163   int _aot_id;
 164 
 165   int _class_count;
 166   int _method_count;
 167   AOTClass* _classes;
 168   CodeToAMethod* _code_to_aot;
 169 
 170   address _code_space;
 171   address _code_segments;
 172   jlong*  _method_state;
 173 
 174 
 175   // Collect metaspace info: names -> address in .got section
 176   const char* _metaspace_names;
 177   address _method_metadata;
 178 
 179   address _methods_offsets;
 180   address _klasses_offsets;
 181   address _dependencies;
 182 
 183   Metadata** _metaspace_got;
 184   Metadata** _metadata_got;
 185   oop*    _oop_got;
 186 
 187   int _metaspace_got_size;
 188   int _metadata_got_size;
 189   int _oop_got_size;
 190 
 191   // Collect stubs info
 192   int* _stubs_offsets;
 193 
 194   bool _lib_symbols_initialized;
 195 
 196   void adjust_boundaries(AOTCompiledMethod* method) {
 197     char* low = (char*)method->code_begin();
 198     if (low < low_boundary()) {
 199       _memory.set_low_boundary(low);
 200       _memory.set_low(low);
 201     }
 202     char* high = (char *)method->code_end();
 203     if (high > high_boundary()) {
 204       _memory.set_high_boundary(high);
 205       _memory.set_high(high);
 206     }
 207     assert(_method_count > 0, "methods count should be set already");
 208   }
 209 
 210   void register_stubs();
 211 
 212   void link_shared_runtime_symbols();
 213   void link_stub_routines_symbols();
 214   void link_os_symbols();
 215   void link_graal_runtime_symbols();
 216 
 217   void link_global_lib_symbols();
 218   void link_primitive_array_klasses();
 219   void publish_aot(const methodHandle& mh, AOTMethodData* method_data, int code_id);
 220 
 221 
 222   AOTCompiledMethod* next_in_use_at(int index) const;
 223 
 224   // Find klass in SystemDictionary for aot metadata.
 225   static Klass* lookup_klass(const char* name, int len, const Method* method, Thread* THREAD);
 226 public:
 227   AOTCodeHeap(AOTLib* lib);
 228   virtual ~AOTCodeHeap();
 229 
 230   AOTCompiledMethod* find_aot(address p) const;
 231 
 232   virtual void* find_start(void* p)     const;
 233   virtual CodeBlob* find_blob_unsafe(void* start) const;
 234   virtual void* first() const;
 235   virtual void* next(void *p) const;
 236 
 237   AOTKlassData* find_klass(InstanceKlass* ik);
 238   bool load_klass_data(InstanceKlass* ik, Thread* thread);
 239   Klass* get_klass_from_got(const char* klass_name, int klass_len, const Method* method);
 240   void sweep_dependent_methods(AOTKlassData* klass_data);
 241   bool is_dependent_method(Klass* dependee, AOTCompiledMethod* aot);
 242 
 243   const char* get_name_at(int offset) {
 244     return _metaspace_names + offset;
 245   }
 246 
 247   void oops_do(OopClosure* f);
 248   void metadata_do(void f(Metadata*));
 249   void got_metadata_do(void f(Metadata*));
 250 
 251 #ifdef ASSERT
 252   bool got_contains(Metadata **p) {
 253     return (p >= &_metadata_got[0] && p < &_metadata_got[_metadata_got_size]) ||
 254            (p >= &_metaspace_got[0] && p < &_metaspace_got[_metaspace_got_size]);
 255   }
 256 #endif
 257 
 258   int dso_id() const { return _lib->id(); }
 259   int aot_id() const { return _aot_id; }
 260 
 261   int method_count() { return _method_count; }
 262 
 263   AOTCompiledMethod* get_code_desc_at_index(int index) {
 264     if (index < _method_count && _code_to_aot[index]._state == in_use) {
 265         AOTCompiledMethod* m = _code_to_aot[index]._aot;
 266         assert(m != NULL, "AOT method should be set");
 267         if (!m->is_runtime_stub()) {
 268           return m;
 269         }
 270     }
 271     return NULL;
 272   }
 273 
 274   static Method* find_method(Klass* klass, Thread* thread, const char* method_name);
 275 
 276   void cleanup_inline_caches();
 277 
 278   DEBUG_ONLY( int verify_icholder_relocations(); )
 279 
 280   void flush_evol_dependents_on(InstanceKlass* dependee);
 281 
 282   void alive_methods_do(void f(CompiledMethod* nm));
 283 
 284 #ifndef PRODUCT
 285   static int klasses_seen;
 286   static int aot_klasses_found;
 287   static int aot_klasses_fp_miss;
 288   static int aot_klasses_cl_miss;
 289   static int aot_methods_found;
 290 
 291   static void print_statistics();
 292 #endif
 293 };
 294 
 295 #endif // SHARE_VM_AOT_AOTCODEHEAP_HPP