1 /*
   2  * Copyright (c) 2016, 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 
  84   enum {
  85     AOT_SHARED_VERSION = 1
  86   };
  87 } AOTHeader;
  88 
  89 typedef struct {
  90   enum { CONFIG_SIZE = 11 + 7 * 4 };
  91   int _config_size;
  92   int _narrowOopShift;
  93   int _narrowKlassShift;
  94   int _contendedPaddingWidth;
  95   int _fieldsAllocationStyle;
  96   int _objectAlignment;
  97   int _codeSegmentSize;
  98   // byte[11] array map to boolean values here
  99   bool _debug_VM;
 100   bool _useCompressedOops;
 101   bool _useCompressedClassPointers;
 102   bool _compactFields;
 103   bool _useG1GC;
 104   bool _useCMSGC;
 105   bool _useTLAB;
 106   bool _useBiasedLocking;
 107   bool _tieredAOT;
 108   bool _enableContended;
 109   bool _restrictContended;
 110 } AOTConfiguration;
 111 
 112 class AOTCodeHeap : public CodeHeap {
 113 private:
 114   bool _valid;
 115   void* _dl_handle;
 116   const char* _name;
 117 
 118   const int _dso_id;
 119   int _aot_id;
 120 
 121   int _class_count;
 122   int _method_count;
 123   AOTClass* _classes;
 124   CodeToAMethod* _code_to_aot;
 125 
 126   address _code_space;
 127   address _code_segments;
 128   jlong*  _method_state;
 129 
 130   // VM configuration during AOT compilation
 131   AOTConfiguration* _config;
 132 
 133   // Collect metaspace info: names -> address in .got section
 134   const char* _metaspace_names;
 135   address _method_metadata;
 136 
 137   address _methods_offsets;
 138   address _klasses_offsets;
 139   address _dependencies;
 140 
 141   Metadata** _metaspace_got;
 142   Metadata** _metadata_got;
 143   oop*    _oop_got;
 144 
 145   int _metaspace_got_size;
 146   int _metadata_got_size;
 147   int _oop_got_size;
 148 
 149   // Collect stubs info
 150   int* _stubs_offsets;
 151 
 152   address _low_boundary;
 153 
 154   bool _lib_symbols_initialized;
 155 
 156   static bool _narrow_oop_shift_initialized;
 157   static int _narrow_oop_shift;
 158   static int _narrow_klass_shift;
 159 
 160   void adjust_boundaries(AOTCompiledMethod* method) {
 161     address low = _low_boundary;
 162     if (method->code_begin() < low) {
 163       low = method->code_begin();
 164     }
 165     address high = high_boundary();
 166     if (method->code_end() > high) {
 167       high = method->code_end();
 168     }
 169     assert(_method_count > 0, "methods count should be set already");
 170 
 171     _low_boundary = low;
 172     _memory.set_high_boundary((char *)high);
 173     _memory.set_high((char *)high);
 174   }
 175 
 176   void handle_config_error(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
 177   void verify_flag(bool aot_flag, bool flag, const char* name);
 178   void verify_config(const AOTHeader* header);
 179   void register_stubs();
 180 
 181   void link_shared_runtime_symbols();
 182   void link_stub_routines_symbols();
 183   void link_os_symbols();
 184   void link_graal_runtime_symbols();
 185 
 186   void link_global_lib_symbols();
 187   void link_primitive_array_klasses();
 188   void publish_aot(instanceKlassHandle kh, methodHandle mh, AOTMethodData* method_data, int code_id);
 189 
 190   address load_symbol(const char *name);
 191 
 192   AOTCompiledMethod* next_in_use_at(int index) const;
 193 
 194   // Find klass in SystemDictionary for aot metadata.
 195   static Klass* lookup_klass(const char* name, int len, const Method* method, Thread* THREAD);
 196 public:
 197   AOTCodeHeap(void* handle, const char* name, int id);
 198   virtual ~AOTCodeHeap();
 199 
 200   address low_boundary()  const { return _low_boundary; }
 201   address high_boundary() const { return (address)CodeHeap::high(); }
 202 
 203   bool contains(const void* p) const {
 204     bool result = (low_boundary() <= p) && (p < high_boundary());
 205     assert(!result || (_method_count > 0), "");
 206     assert(result == CodeHeap::contains(p), "");
 207     return result;
 208   }
 209   AOTCompiledMethod* find_aot(address p) const;
 210 
 211   virtual void* find_start(void* p)     const;
 212   virtual CodeBlob* find_blob_unsafe(void* start) const;
 213   virtual void* first() const;
 214   virtual void* next(void *p) const;
 215 
 216   AOTKlassData* find_klass(InstanceKlass* ik);
 217   bool load_klass_data(instanceKlassHandle kh, Thread* thread);
 218   Klass* get_klass_from_got(const char* klass_name, int klass_len, const Method* method);
 219   void sweep_dependent_methods(AOTKlassData* klass_data);
 220   bool is_dependent_method(Klass* dependee, AOTCompiledMethod* aot);
 221 
 222   const char* get_name_at(int offset) {
 223     return _metaspace_names + offset;
 224   }
 225 
 226   void oops_do(OopClosure* f);
 227   void metadata_do(void f(Metadata*));
 228   void got_metadata_do(void f(Metadata*));
 229 
 230 #ifdef ASSERT
 231   bool got_contains(Metadata **p) {
 232     return (p >= &_metadata_got[0] && p < &_metadata_got[_metadata_got_size]) ||
 233            (p >= &_metaspace_got[0] && p < &_metaspace_got[_metaspace_got_size]);
 234   }
 235 #endif
 236 
 237   int dso_id() const { return _dso_id; }
 238   int aot_id() const { return _aot_id; }
 239   bool is_valid() const { return _valid; }
 240 
 241   int method_count() { return _method_count; }
 242 
 243   AOTCompiledMethod* get_code_desc_at_index(int index) {
 244     if (index < _method_count && _code_to_aot[index]._state == in_use) {
 245         AOTCompiledMethod* m = _code_to_aot[index]._aot;
 246         assert(m != NULL, "AOT method should be set");
 247         if (!m->is_runtime_stub()) {
 248           return m;
 249         }
 250     }
 251     return NULL;
 252   }
 253 
 254   static int  narrow_oop_shift() { return _narrow_oop_shift; }
 255   static int  narrow_klass_shift() { return _narrow_klass_shift; }
 256   static bool narrow_oop_shift_initialized() { return _narrow_oop_shift_initialized; }
 257   static Method* find_method(KlassHandle klass, Thread* thread, const char* method_name);
 258 
 259   void verify_flag(int  aot_flag, int  flag, const char* name);
 260 
 261   void cleanup_inline_caches();
 262 
 263   DEBUG_ONLY( int verify_icholder_relocations(); )
 264 
 265   void flush_evol_dependents_on(instanceKlassHandle dependee);
 266 
 267   void alive_methods_do(void f(CompiledMethod* nm));
 268 
 269 #ifndef PRODUCT
 270   static int klasses_seen;
 271   static int aot_klasses_found;
 272   static int aot_klasses_fp_miss;
 273   static int aot_klasses_cl_miss;
 274   static int aot_methods_found;
 275 
 276   static void print_statistics();
 277 #endif
 278 };
 279 
 280 #endif // SHARE_VM_AOT_AOTCODEHEAP_HPP