1 /*
   2  * Copyright (c) 2011, 2019, 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_JVMCI_JVMCICODEINSTALLER_HPP
  25 #define SHARE_JVMCI_JVMCICODEINSTALLER_HPP
  26 
  27 #include "code/debugInfoRec.hpp"
  28 #include "code/exceptionHandlerTable.hpp"
  29 #include "code/nativeInst.hpp"
  30 #include "jvmci/jvmci.hpp"
  31 #include "jvmci/jvmciEnv.hpp"
  32 
  33 #if INCLUDE_AOT
  34 class RelocBuffer : public StackObj {
  35   enum { stack_size = 1024 };
  36 public:
  37   RelocBuffer() : _size(0), _buffer(0) {}
  38   ~RelocBuffer();
  39   void ensure_size(size_t bytes);
  40   void set_size(size_t bytes);
  41   address begin() const;
  42   size_t size() const { return _size; }
  43 private:
  44   size_t _size;
  45   char _static_buffer[stack_size];
  46   char *_buffer;
  47 };
  48 
  49 class CodeInstaller;
  50 
  51 class AOTOopRecorder : public OopRecorder {
  52 public:
  53   AOTOopRecorder(CodeInstaller* code_inst, Arena* arena = NULL, bool deduplicate = false);
  54 
  55   virtual int find_index(Metadata* h);
  56   virtual int find_index(jobject h);
  57   int nr_meta_refs() const;
  58   jobject meta_element(int pos) const;
  59 
  60 private:
  61   void record_meta_ref(jobject ref, int index);
  62 
  63   GrowableArray<jobject>* _meta_refs;
  64 
  65   CodeInstaller* _code_inst;
  66 };
  67 #endif // INCLUDE_AOT
  68 
  69 class CodeMetadata {
  70 public:
  71   CodeMetadata() {}
  72 
  73   CodeBlob* get_code_blob() const { return _cb; }
  74 
  75   PcDesc* get_pc_desc() const { return _pc_desc; }
  76   int get_nr_pc_desc() const { return _nr_pc_desc; }
  77 
  78   u_char* get_scopes_desc() const { return _scopes_desc; }
  79   int get_scopes_size() const { return _nr_scopes_desc; }
  80 
  81 #if INCLUDE_AOT
  82   RelocBuffer* get_reloc_buffer() { return &_reloc_buffer; }
  83   AOTOopRecorder* get_oop_recorder() { return _oop_recorder; }
  84 #endif
  85 
  86   ExceptionHandlerTable* get_exception_table() { return _exception_table; }
  87 
  88   void set_pc_desc(PcDesc* desc, int count) {
  89     _pc_desc = desc;
  90     _nr_pc_desc = count;
  91   }
  92 
  93   void set_scopes(u_char* scopes, int size) {
  94     _scopes_desc = scopes;
  95     _nr_scopes_desc = size;
  96   }
  97 
  98 #if INCLUDE_AOT
  99   void set_oop_recorder(AOTOopRecorder* recorder) {
 100     _oop_recorder = recorder;
 101   }
 102 #endif
 103 
 104   void set_exception_table(ExceptionHandlerTable* table) {
 105     _exception_table = table;
 106   }
 107 
 108 private:
 109   CodeBlob* _cb;
 110   PcDesc* _pc_desc;
 111   int _nr_pc_desc;
 112 
 113   u_char* _scopes_desc;
 114   int _nr_scopes_desc;
 115 
 116 #if INCLUDE_AOT
 117   RelocBuffer _reloc_buffer;
 118   AOTOopRecorder* _oop_recorder;
 119 #endif
 120   ExceptionHandlerTable* _exception_table;
 121 };
 122 
 123 /*
 124  * This class handles the conversion from a InstalledCode to a CodeBlob or an nmethod.
 125  */
 126 class CodeInstaller : public StackObj {
 127   friend class JVMCIVMStructs;
 128 private:
 129   enum MarkId {
 130     VERIFIED_ENTRY                         = 1,
 131     UNVERIFIED_ENTRY                       = 2,
 132     OSR_ENTRY                              = 3,
 133     EXCEPTION_HANDLER_ENTRY                = 4,
 134     DEOPT_HANDLER_ENTRY                    = 5,
 135     INVOKEINTERFACE                        = 6,
 136     INVOKEVIRTUAL                          = 7,
 137     INVOKESTATIC                           = 8,
 138     INVOKESPECIAL                          = 9,
 139     INLINE_INVOKE                          = 10,
 140     POLL_NEAR                              = 11,
 141     POLL_RETURN_NEAR                       = 12,
 142     POLL_FAR                               = 13,
 143     POLL_RETURN_FAR                        = 14,
 144     CARD_TABLE_ADDRESS                     = 15,
 145     CARD_TABLE_SHIFT                       = 16,
 146     HEAP_TOP_ADDRESS                       = 17,
 147     HEAP_END_ADDRESS                       = 18,
 148     NARROW_KLASS_BASE_ADDRESS              = 19,
 149     NARROW_OOP_BASE_ADDRESS                = 20,
 150     CRC_TABLE_ADDRESS                      = 21,
 151     LOG_OF_HEAP_REGION_GRAIN_BYTES         = 22,
 152     INLINE_CONTIGUOUS_ALLOCATION_SUPPORTED = 23,
 153     INVOKE_INVALID                         = -1
 154   };
 155 
 156   Arena         _arena;
 157   JVMCIEnv*     _jvmci_env;
 158 
 159   JVMCIPrimitiveArray    _data_section_handle;
 160   JVMCIObjectArray       _data_section_patches_handle;
 161   JVMCIObjectArray       _sites_handle;
 162 #ifndef PRODUCT
 163   JVMCIObjectArray       _comments_handle;
 164 #endif
 165   JVMCIPrimitiveArray    _code_handle;
 166   JVMCIObject            _word_kind_handle;
 167 
 168   CodeOffsets   _offsets;
 169 
 170   jint          _code_size;
 171   jint          _total_frame_size;
 172   jint          _orig_pc_offset;
 173   jint          _parameter_count;
 174   jint          _constants_size;
 175 
 176   bool          _has_wide_vector;
 177 
 178   MarkId        _next_call_type;
 179   address       _invoke_mark_pc;
 180 
 181   CodeSection*  _instructions;
 182   CodeSection*  _constants;
 183 
 184   OopRecorder*              _oop_recorder;
 185   DebugInformationRecorder* _debug_recorder;
 186   Dependencies*             _dependencies;
 187   ExceptionHandlerTable     _exception_handler_table;
 188 
 189   bool _immutable_pic_compilation;  // Installer is called for Immutable PIC compilation.
 190 
 191   static ConstantOopWriteValue* _oop_null_scope_value;
 192   static ConstantIntValue*    _int_m1_scope_value;
 193   static ConstantIntValue*    _int_0_scope_value;
 194   static ConstantIntValue*    _int_1_scope_value;
 195   static ConstantIntValue*    _int_2_scope_value;
 196   static LocationValue*       _illegal_value;
 197 
 198   jint pd_next_offset(NativeInstruction* inst, jint pc_offset, JVMCIObject method, JVMCI_TRAPS);
 199   void pd_patch_OopConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
 200   void pd_patch_MetaspaceConstant(int pc_offset, JVMCIObject constant, JVMCI_TRAPS);
 201   void pd_patch_DataSectionReference(int pc_offset, int data_offset, JVMCI_TRAPS);
 202   void pd_relocate_ForeignCall(NativeInstruction* inst, jlong foreign_call_destination, JVMCI_TRAPS);
 203   void pd_relocate_JavaMethod(CodeBuffer &cbuf, JVMCIObject method, jint pc_offset, JVMCI_TRAPS);
 204   void pd_relocate_poll(address pc, jint mark, JVMCI_TRAPS);
 205 
 206   JVMCIObjectArray sites()                { return _sites_handle; }
 207   JVMCIPrimitiveArray code()              { return _code_handle; }
 208   JVMCIPrimitiveArray  data_section()     { return _data_section_handle; }
 209   JVMCIObjectArray data_section_patches() { return _data_section_patches_handle; }
 210 #ifndef PRODUCT
 211   JVMCIObjectArray comments()             { return _comments_handle; }
 212 #endif
 213   JVMCIObject word_kind()                 { return _word_kind_handle; }
 214 
 215 public:
 216 
 217   CodeInstaller(JVMCIEnv* jvmci_env, bool immutable_pic_compilation) : _arena(mtJVMCI), _jvmci_env(jvmci_env), _immutable_pic_compilation(immutable_pic_compilation) {}
 218 
 219 #if INCLUDE_AOT
 220   JVMCI::CodeInstallResult gather_metadata(JVMCIObject target, JVMCIObject compiled_code, CodeMetadata& metadata, JVMCI_TRAPS);
 221 #endif
 222   JVMCI::CodeInstallResult install(JVMCICompiler* compiler,
 223                                    JVMCIObject target,
 224                                    JVMCIObject compiled_code,
 225                                    CodeBlob*& cb,
 226                                    JVMCIObject installed_code,
 227                                    FailedSpeculation** failed_speculations,
 228                                    char* speculations,
 229                                    int speculations_len,
 230                                    JVMCI_TRAPS);
 231 
 232   JVMCIEnv* jvmci_env() { return _jvmci_env; }
 233   JVMCIRuntime* runtime() { return _jvmci_env->runtime(); }
 234 
 235   static address runtime_call_target_address(oop runtime_call);
 236   static VMReg get_hotspot_reg(jint jvmciRegisterNumber, JVMCI_TRAPS);
 237   static bool is_general_purpose_reg(VMReg hotspotRegister);
 238 
 239   const OopMapSet* oopMapSet() const { return _debug_recorder->_oopmaps; }
 240 
 241 protected:
 242   Location::Type get_oop_type(JVMCIObject value);
 243   ScopeValue* get_scope_value(JVMCIObject value, BasicType type, GrowableArray<ScopeValue*>* objects, ScopeValue* &second, JVMCI_TRAPS);
 244   MonitorValue* get_monitor_value(JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
 245 
 246   void* record_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
 247 #ifdef _LP64
 248   narrowKlass record_narrow_metadata_reference(CodeSection* section, address dest, JVMCIObject constant, JVMCI_TRAPS);
 249 #endif
 250 
 251   // extract the fields of the HotSpotCompiledCode
 252   void initialize_fields(JVMCIObject target, JVMCIObject compiled_code, JVMCI_TRAPS);
 253   void initialize_dependencies(JVMCIObject compiled_code, OopRecorder* oop_recorder, JVMCI_TRAPS);
 254 
 255   int estimate_stubs_size(JVMCI_TRAPS);
 256 
 257   // perform data and call relocation on the CodeBuffer
 258   JVMCI::CodeInstallResult initialize_buffer(CodeBuffer& buffer, bool check_size, JVMCI_TRAPS);
 259 
 260   void assumption_NoFinalizableSubclass(JVMCIObject assumption);
 261   void assumption_ConcreteSubtype(JVMCIObject assumption);
 262   void assumption_LeafType(JVMCIObject assumption);
 263   void assumption_ConcreteMethod(JVMCIObject assumption);
 264   void assumption_CallSiteTargetValue(JVMCIObject assumption, JVMCI_TRAPS);
 265 
 266   void site_Safepoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
 267   void site_Infopoint(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
 268   void site_Call(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
 269   void site_DataPatch(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
 270   void site_Mark(CodeBuffer& buffer, jint pc_offset, JVMCIObject site, JVMCI_TRAPS);
 271   void site_ExceptionHandler(jint pc_offset, JVMCIObject site);
 272 
 273   OopMap* create_oop_map(JVMCIObject debug_info, JVMCI_TRAPS);
 274 
 275   VMReg getVMRegFromLocation(JVMCIObject location, int total_frame_size, JVMCI_TRAPS);
 276 
 277   /**
 278    * Specifies the level of detail to record for a scope.
 279    */
 280   enum ScopeMode {
 281     // Only record a method and BCI
 282     BytecodePosition,
 283     // Record a method, bci and JVM frame state
 284     FullFrame
 285   };
 286 
 287   int map_jvmci_bci(int bci);
 288 
 289   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, bool return_oop, JVMCI_TRAPS);
 290   void record_scope(jint pc_offset, JVMCIObject debug_info, ScopeMode scope_mode, JVMCI_TRAPS) {
 291     record_scope(pc_offset, debug_info, scope_mode, false /* return_oop */, JVMCIENV);
 292   }
 293   void record_scope(jint pc_offset, JVMCIObject position, ScopeMode scope_mode, GrowableArray<ScopeValue*>* objects, bool return_oop, JVMCI_TRAPS);
 294   void record_object_value(ObjectValue* sv, JVMCIObject value, GrowableArray<ScopeValue*>* objects, JVMCI_TRAPS);
 295 
 296   GrowableArray<ScopeValue*>* record_virtual_objects(JVMCIObject debug_info, JVMCI_TRAPS);
 297 
 298   int estimateStubSpace(int static_call_stubs);
 299 };
 300 
 301 #endif // SHARE_JVMCI_JVMCICODEINSTALLER_HPP