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
32enum 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
38typedef struct {
39  AOTCompiledMethod* _aot;
40  CodeState _state; // State change cases: not_set->in_use, not_set->invalid
41} CodeToAMethod;
42
43class ClassLoaderData;
44
45class AOTClass {
46public:
47  ClassLoaderData* _classloader;
48};
49
50typedef 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
59typedef 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
68typedef 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
76typedef 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
90typedef struct {
91  enum { CONFIG_SIZE = 12 + 7 * 4 };
92  int _config_size;
93  int _narrowOopShift;
94  int _narrowKlassShift;
95  int _contendedPaddingWidth;
96  int _fieldsAllocationStyle;
97  int _objectAlignment;
98  int _codeSegmentSize;
99  // byte[11] array map to boolean values here
100  bool _debug_VM;
101  bool _useCompressedOops;
102  bool _useCompressedClassPointers;
103  bool _compactFields;
104  bool _useG1GC;
105  bool _useCMSGC;
106  bool _useTLAB;
107  bool _useBiasedLocking;
108  bool _tieredAOT;
109  bool _enableContended;
110  bool _restrictContended;
111  bool _omitAssertions;
112} AOTConfiguration;
113
114class 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);
128public:
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
161class 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  address _low_boundary;
195
196  bool _lib_symbols_initialized;
197
198  void adjust_boundaries(AOTCompiledMethod* method) {
199    address low = _low_boundary;
200    if (method->code_begin() < low) {
201      low = method->code_begin();
202    }
203    address high = high_boundary();
204    if (method->code_end() > high) {
205      high = method->code_end();
206    }
207    assert(_method_count > 0, "methods count should be set already");
208
209    _low_boundary = low;
210    _memory.set_high_boundary((char *)high);
211    _memory.set_high((char *)high);
212  }
213
214  void register_stubs();
215
216  void link_shared_runtime_symbols();
217  void link_stub_routines_symbols();
218  void link_os_symbols();
219  void link_graal_runtime_symbols();
220
221  void link_global_lib_symbols();
222  void link_primitive_array_klasses();
223  void publish_aot(const methodHandle& mh, AOTMethodData* method_data, int code_id);
224
225
226  AOTCompiledMethod* next_in_use_at(int index) const;
227
228  // Find klass in SystemDictionary for aot metadata.
229  static Klass* lookup_klass(const char* name, int len, const Method* method, Thread* THREAD);
230public:
231  AOTCodeHeap(AOTLib* lib);
232  virtual ~AOTCodeHeap();
233
234  address low_boundary()  const { return _low_boundary; }
235  address high_boundary() const { return (address)CodeHeap::high(); }
236
237  bool contains(const void* p) const {
238    bool result = (low_boundary() <= p) && (p < high_boundary());
239    assert(!result || (_method_count > 0), "");
240    assert(result == CodeHeap::contains(p), "");
241    return result;
242  }
243
244  bool contains_blob(const CodeBlob* blob) const {
245    return CodeHeap::contains(blob->code_begin());
246  }
247
248  AOTCompiledMethod* find_aot(address p) const;
249
250  virtual void* find_start(void* p)     const;
251  virtual CodeBlob* find_blob_unsafe(void* start) const;
252  virtual void* first() const;
253  virtual void* next(void *p) const;
254
255  AOTKlassData* find_klass(InstanceKlass* ik);
256  bool load_klass_data(instanceKlassHandle kh, Thread* thread);
257  Klass* get_klass_from_got(const char* klass_name, int klass_len, const Method* method);
258  void sweep_dependent_methods(AOTKlassData* klass_data);
259  bool is_dependent_method(Klass* dependee, AOTCompiledMethod* aot);
260
261  const char* get_name_at(int offset) {
262    return _metaspace_names + offset;
263  }
264
265  void oops_do(OopClosure* f);
266  void metadata_do(void f(Metadata*));
267  void got_metadata_do(void f(Metadata*));
268
269#ifdef ASSERT
270  bool got_contains(Metadata **p) {
271    return (p >= &_metadata_got[0] && p < &_metadata_got[_metadata_got_size]) ||
272           (p >= &_metaspace_got[0] && p < &_metaspace_got[_metaspace_got_size]);
273  }
274#endif
275
276  int dso_id() const { return _lib->id(); }
277  int aot_id() const { return _aot_id; }
278
279  int method_count() { return _method_count; }
280
281  AOTCompiledMethod* get_code_desc_at_index(int index) {
282    if (index < _method_count && _code_to_aot[index]._state == in_use) {
283        AOTCompiledMethod* m = _code_to_aot[index]._aot;
284        assert(m != NULL, "AOT method should be set");
285        if (!m->is_runtime_stub()) {
286          return m;
287        }
288    }
289    return NULL;
290  }
291
292  static Method* find_method(KlassHandle klass, Thread* thread, const char* method_name);
293
294  void cleanup_inline_caches();
295
296  DEBUG_ONLY( int verify_icholder_relocations(); )
297
298  void flush_evol_dependents_on(instanceKlassHandle dependee);
299
300  void alive_methods_do(void f(CompiledMethod* nm));
301
302#ifndef PRODUCT
303  static int klasses_seen;
304  static int aot_klasses_found;
305  static int aot_klasses_fp_miss;
306  static int aot_klasses_cl_miss;
307  static int aot_methods_found;
308
309  static void print_statistics();
310#endif
311};
312
313#endif // SHARE_VM_AOT_AOTCODEHEAP_HPP
314