1 /*
  2  * Copyright (c) 2002, 2021, 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 
 25 #ifndef SHARE_RUNTIME_REGISTERMAP_HPP
 26 #define SHARE_RUNTIME_REGISTERMAP_HPP
 27 
 28 #include "code/vmreg.hpp"
 29 #include "oops/stackChunkOop.hpp"
 30 #include "runtime/handles.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/macros.hpp"
 33 
 34 class JavaThread;
 35 
 36 //
 37 // RegisterMap
 38 //
 39 // A companion structure used for stack traversal. The RegisterMap contains
 40 // misc. information needed in order to do correct stack traversal of stack
 41 // frames.  Hence, it must always be passed in as an argument to
 42 // frame::sender(RegisterMap*).
 43 //
 44 // In particular,
 45 //   1) It provides access to the thread for which the stack belongs.  The
 46 //      thread object is needed in order to get sender of a deoptimized frame.
 47 //
 48 //   2) It is used to pass information from a callee frame to its caller
 49 //      frame about how the frame should be traversed.  This is used to let
 50 //      the caller frame take care of calling oops-do of out-going
 51 //      arguments, when the callee frame is not instantiated yet.  This
 52 //      happens, e.g., when a compiled frame calls into
 53 //      resolve_virtual_call.  (Hence, it is critical that the same
 54 //      RegisterMap object is used for the entire stack walk.  Normally,
 55 //      this is hidden by using the StackFrameStream.)  This is used when
 56 //      doing follow_oops and oops_do.
 57 //
 58 //   3) The RegisterMap keeps track of the values of callee-saved registers
 59 //      from frame to frame (hence, the name).  For some stack traversal the
 60 //      values of the callee-saved registers does not matter, e.g., if you
 61 //      only need the static properties such as frame type, pc, and such.
 62 //      Updating of the RegisterMap can be turned off by instantiating the
 63 //      register map as: RegisterMap map(thread, false);
 64 
 65 class RegisterMap : public StackObj {
 66  public:
 67     typedef julong LocationValidType;
 68   enum {
 69     reg_count = ConcreteRegisterImpl::number_of_registers,
 70     location_valid_type_size = sizeof(LocationValidType)*8,
 71     location_valid_size = (reg_count+location_valid_type_size-1)/location_valid_type_size
 72   };
 73  private:
 74   intptr_t*    _location[reg_count];    // Location of registers (intptr_t* looks better than address in the debugger)
 75   LocationValidType _location_valid[location_valid_size];
 76   bool        _include_argument_oops;   // Should include argument_oop marked locations for compiler
 77   JavaThread* _thread;                  // Reference to current thread
 78   stackChunkHandle _chunk;              // The current continuation stack chunk, if any
 79 
 80   bool        _update_map;              // Tells if the register map need to be
 81                                         // updated when traversing the stack
 82   bool        _process_frames;          // Should frames be processed by stack watermark barriers?
 83   bool        _walk_cont;               // whether to walk frames on a continuation stack
 84 
 85   NOT_PRODUCT(bool  _skip_missing;)
 86 
 87 #ifdef ASSERT
 88   void check_location_valid();
 89 #else
 90   void check_location_valid() {}
 91 #endif
 92 
 93  public:
 94   DEBUG_ONLY(intptr_t* _update_for_id;) // Assert that RegisterMap is not updated twice for same frame
 95   RegisterMap(JavaThread *thread, bool update_map = true, bool process_frames = true, bool walk_cont = false);
 96   RegisterMap(oop continuation, bool update_map = true);
 97   RegisterMap(const RegisterMap* map);
 98 
 99   address location(VMReg reg, intptr_t* sp) const {
100     int index = reg->value() / location_valid_type_size;
101     assert(0 <= reg->value() && reg->value() < reg_count, "range check");
102     assert(0 <= index && index < location_valid_size, "range check");
103     if (_location_valid[index] & ((LocationValidType)1 << (reg->value() % location_valid_type_size))) {
104       return (address) _location[reg->value()];
105     } else {
106       return pd_location(reg);
107     }
108   }
109 
110   address location(VMReg base_reg, int slot_idx) const {
111     if (slot_idx > 0) {
112       return pd_location(base_reg, slot_idx);
113     } else {
114       return location(base_reg, (intptr_t*)NULL);
115     }
116   }
117 
118   address trusted_location(VMReg reg) const {
119     return (address) _location[reg->value()];
120   }
121     
122   void verify(RegisterMap& other) {
123     for (int i = 0; i < reg_count; ++i) {
124       assert(_location[i] == other._location[i], "");
125     }
126   }
127 
128   void set_location(VMReg reg, address loc) {
129     int index = reg->value() / location_valid_type_size;
130     assert(0 <= reg->value() && reg->value() < reg_count, "range check");
131     assert(0 <= index && index < location_valid_size, "range check");
132     assert(_update_map, "updating map that does not need updating");
133     _location[reg->value()] = (intptr_t*) loc;
134     _location_valid[index] |= ((LocationValidType)1 << (reg->value() % location_valid_type_size));
135     check_location_valid();
136   }
137 
138   // Called by an entry frame.
139   void clear();
140 
141   bool include_argument_oops() const      { return _include_argument_oops; }
142   void set_include_argument_oops(bool f)  { _include_argument_oops = f; }
143 
144   JavaThread *thread()  const { return _thread; }
145   bool update_map()     const { return _update_map; }
146   bool process_frames() const { return _process_frames; }
147   bool walk_cont()      const { return _walk_cont; }
148 
149   bool in_cont()        const { return _chunk() != NULL; } // Whether we are currently on the hstack; if true, frames are relativized
150   oop cont() const;
151   stackChunkHandle stack_chunk() const { return _chunk; }
152   void set_stack_chunk(stackChunkOop chunk);
153 
154   const RegisterMap* as_RegisterMap() const { return this; }
155   RegisterMap* as_RegisterMap() { return this; }
156 
157   void print_on(outputStream* st) const;
158   void print() const;
159 
160 #ifndef PRODUCT
161   void set_skip_missing(bool value) { _skip_missing = value; }
162   bool should_skip_missing() const  { return _skip_missing; }
163 
164   VMReg find_register_spilled_here(void* p, intptr_t* sp);
165 #endif
166 
167   // the following contains the definition of pd_xxx methods
168 #include CPU_HEADER(registerMap)
169 
170 };
171 
172 #endif // SHARE_RUNTIME_REGISTERMAP_HPP