Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_STACK_H_ |
| 18 | #define ART_SRC_STACK_H_ |
| 19 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 20 | #include "base/macros.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 21 | #include "dex_file.h" |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 22 | #include "heap.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 23 | #include "instrumentation.h" |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 24 | #include "jni.h" |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 25 | #include "oat/runtime/context.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 26 | |
| 27 | #include <stdint.h> |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 28 | #include <string> |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 32 | class AbstractMethod; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 33 | class Context; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 34 | class Object; |
| 35 | class ShadowFrame; |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 36 | class StackIndirectReferenceTable; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | class ScopedObjectAccess; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 38 | class Thread; |
| 39 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 40 | // The kind of vreg being accessed in calls to Set/GetVReg. |
| 41 | enum VRegKind { |
| 42 | kReferenceVReg, |
| 43 | kIntVReg, |
| 44 | kFloatVReg, |
| 45 | kLongLoVReg, |
| 46 | kLongHiVReg, |
| 47 | kDoubleLoVReg, |
| 48 | kDoubleHiVReg, |
| 49 | kConstant, |
| 50 | kImpreciseConstant, |
| 51 | kUndefined, |
| 52 | }; |
| 53 | |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 54 | // ShadowFrame has 3 possible layouts: |
| 55 | // - portable - a unified array of VRegs and references. Precise references need GC maps. |
| 56 | // - interpreter - separate VRegs and reference arrays. References are in the reference array. |
| 57 | // - JNI - just VRegs, but where every VReg holds a reference. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 58 | class ShadowFrame { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 59 | public: |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 60 | // Create ShadowFrame for interpreter. |
| 61 | static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link, |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 62 | AbstractMethod* method, uint32_t dex_pc) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 63 | size_t sz = sizeof(ShadowFrame) + |
| 64 | (sizeof(uint32_t) * num_vregs) + |
| 65 | (sizeof(Object*) * num_vregs); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 66 | uint8_t* memory = new uint8_t[sz]; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 67 | ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true); |
| 68 | return sf; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 69 | } |
| 70 | ~ShadowFrame() {} |
| 71 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 72 | bool HasReferenceArray() const { |
| 73 | return (number_of_vregs_ & kHasReferenceArray) != 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 74 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 75 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 76 | uint32_t NumberOfVRegs() const { |
| 77 | return number_of_vregs_ & ~kHasReferenceArray; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 78 | } |
| 79 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 80 | void SetNumberOfVRegs(uint32_t number_of_vregs) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 81 | number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray); |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 84 | uint32_t GetDexPC() const { |
| 85 | return dex_pc_; |
| 86 | } |
| 87 | |
| 88 | void SetDexPC(uint32_t dex_pc) { |
| 89 | dex_pc_ = dex_pc; |
| 90 | } |
| 91 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 92 | ShadowFrame* GetLink() const { |
| 93 | return link_; |
| 94 | } |
| 95 | |
| 96 | void SetLink(ShadowFrame* frame) { |
| 97 | DCHECK_NE(this, frame); |
| 98 | link_ = frame; |
| 99 | } |
| 100 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 101 | int32_t GetVReg(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 102 | DCHECK_LT(i, NumberOfVRegs()); |
| 103 | const uint32_t* vreg = &vregs_[i]; |
| 104 | return *reinterpret_cast<const int32_t*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | float GetVRegFloat(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 108 | DCHECK_LT(i, NumberOfVRegs()); |
| 109 | // NOTE: Strict-aliasing? |
| 110 | const uint32_t* vreg = &vregs_[i]; |
| 111 | return *reinterpret_cast<const float*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int64_t GetVRegLong(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 115 | const uint32_t* vreg = &vregs_[i]; |
| 116 | return *reinterpret_cast<const int64_t*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | double GetVRegDouble(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 120 | const uint32_t* vreg = &vregs_[i]; |
| 121 | return *reinterpret_cast<const double*>(vreg); |
| 122 | } |
| 123 | |
| 124 | Object* GetVRegReference(size_t i) const { |
| 125 | DCHECK_LT(i, NumberOfVRegs()); |
| 126 | if (HasReferenceArray()) { |
| 127 | return References()[i]; |
| 128 | } else { |
| 129 | const uint32_t* vreg = &vregs_[i]; |
| 130 | return *reinterpret_cast<Object* const*>(vreg); |
| 131 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void SetVReg(size_t i, int32_t val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 135 | DCHECK_LT(i, NumberOfVRegs()); |
| 136 | uint32_t* vreg = &vregs_[i]; |
| 137 | *reinterpret_cast<int32_t*>(vreg) = val; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void SetVRegFloat(size_t i, float val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 141 | DCHECK_LT(i, NumberOfVRegs()); |
| 142 | uint32_t* vreg = &vregs_[i]; |
| 143 | *reinterpret_cast<float*>(vreg) = val; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void SetVRegLong(size_t i, int64_t val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 147 | uint32_t* vreg = &vregs_[i]; |
| 148 | *reinterpret_cast<int64_t*>(vreg) = val; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void SetVRegDouble(size_t i, double val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 152 | uint32_t* vreg = &vregs_[i]; |
| 153 | *reinterpret_cast<double*>(vreg) = val; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 156 | void SetVRegReference(size_t i, Object* val) { |
| 157 | DCHECK_LT(i, NumberOfVRegs()); |
| 158 | uint32_t* vreg = &vregs_[i]; |
| 159 | *reinterpret_cast<Object**>(vreg) = val; |
| 160 | if (HasReferenceArray()) { |
| 161 | References()[i] = val; |
| 162 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 165 | AbstractMethod* GetMethod() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 166 | DCHECK_NE(method_, static_cast<void*>(NULL)); |
| 167 | return method_; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 170 | void SetMethod(AbstractMethod* method) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 171 | DCHECK_NE(method, static_cast<void*>(NULL)); |
| 172 | method_ = method; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 173 | } |
| 174 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 175 | bool Contains(Object** shadow_frame_entry_obj) const { |
| 176 | if (HasReferenceArray()) { |
| 177 | return ((&References()[0] <= shadow_frame_entry_obj) && |
| 178 | (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1]))); |
| 179 | } else { |
| 180 | uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj); |
| 181 | return ((&vregs_[0] <= shadow_frame_entry) && |
| 182 | (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1]))); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 183 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 186 | static size_t LinkOffset() { |
| 187 | return OFFSETOF_MEMBER(ShadowFrame, link_); |
| 188 | } |
| 189 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 190 | static size_t MethodOffset() { |
| 191 | return OFFSETOF_MEMBER(ShadowFrame, method_); |
| 192 | } |
| 193 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 194 | static size_t DexPCOffset() { |
| 195 | return OFFSETOF_MEMBER(ShadowFrame, dex_pc_); |
| 196 | } |
| 197 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 198 | static size_t NumberOfVRegsOffset() { |
| 199 | return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_); |
| 200 | } |
| 201 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 202 | static size_t VRegsOffset() { |
| 203 | return OFFSETOF_MEMBER(ShadowFrame, vregs_); |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 206 | private: |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 207 | ShadowFrame(uint32_t num_vregs, ShadowFrame* link, AbstractMethod* method, uint32_t dex_pc, |
| 208 | bool has_reference_array) |
| 209 | : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 210 | CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray)); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 211 | if (has_reference_array) { |
| 212 | number_of_vregs_ |= kHasReferenceArray; |
| 213 | for (size_t i = 0; i < num_vregs; ++i) { |
| 214 | SetVRegReference(i, NULL); |
| 215 | } |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 216 | } else { |
| 217 | for (size_t i = 0; i < num_vregs; ++i) { |
| 218 | SetVReg(i, 0); |
| 219 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 222 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 223 | Object* const* References() const { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 224 | DCHECK(HasReferenceArray()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 225 | const uint32_t* vreg_end = &vregs_[NumberOfVRegs()]; |
| 226 | return reinterpret_cast<Object* const*>(vreg_end); |
| 227 | } |
| 228 | |
| 229 | Object** References() { |
| 230 | return const_cast<Object**>(const_cast<const ShadowFrame*>(this)->References()); |
| 231 | } |
| 232 | |
| 233 | enum ShadowFrameFlag { |
| 234 | kHasReferenceArray = 1ul << 31 |
| 235 | }; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 236 | // TODO: make the majority of these fields const. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 237 | uint32_t number_of_vregs_; |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 238 | // Link to previous shadow frame or NULL. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 239 | ShadowFrame* link_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 240 | AbstractMethod* method_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 241 | uint32_t dex_pc_; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 242 | uint32_t vregs_[0]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 243 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 244 | DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 247 | // The managed stack is used to record fragments of managed code stacks. Managed code stacks |
| 248 | // may either be shadow frames or lists of frames using fixed frame sizes. Transition records are |
| 249 | // necessary for transitions between code using different frame layouts and transitions into native |
| 250 | // code. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 251 | class PACKED(4) ManagedStack { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 252 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 253 | ManagedStack() |
| 254 | : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {} |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 255 | |
| 256 | void PushManagedStackFragment(ManagedStack* fragment) { |
| 257 | // Copy this top fragment into given fragment. |
| 258 | memcpy(fragment, this, sizeof(ManagedStack)); |
| 259 | // Clear this fragment, which has become the top. |
| 260 | memset(this, 0, sizeof(ManagedStack)); |
| 261 | // Link our top fragment onto the given fragment. |
| 262 | link_ = fragment; |
| 263 | } |
| 264 | |
| 265 | void PopManagedStackFragment(const ManagedStack& fragment) { |
| 266 | DCHECK(&fragment == link_); |
| 267 | // Copy this given fragment back to the top. |
| 268 | memcpy(this, &fragment, sizeof(ManagedStack)); |
| 269 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 270 | |
| 271 | ManagedStack* GetLink() const { |
| 272 | return link_; |
| 273 | } |
| 274 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 275 | AbstractMethod** GetTopQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 276 | return top_quick_frame_; |
| 277 | } |
| 278 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 279 | void SetTopQuickFrame(AbstractMethod** top) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 280 | top_quick_frame_ = top; |
| 281 | } |
| 282 | |
| 283 | uintptr_t GetTopQuickFramePc() const { |
| 284 | return top_quick_frame_pc_; |
| 285 | } |
| 286 | |
| 287 | void SetTopQuickFramePc(uintptr_t pc) { |
| 288 | top_quick_frame_pc_ = pc; |
| 289 | } |
| 290 | |
| 291 | static size_t TopQuickFrameOffset() { |
| 292 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_); |
| 293 | } |
| 294 | |
| 295 | static size_t TopQuickFramePcOffset() { |
| 296 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_); |
| 297 | } |
| 298 | |
| 299 | ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) { |
| 300 | ShadowFrame* old_frame = top_shadow_frame_; |
| 301 | top_shadow_frame_ = new_top_frame; |
| 302 | new_top_frame->SetLink(old_frame); |
| 303 | return old_frame; |
| 304 | } |
| 305 | |
| 306 | ShadowFrame* PopShadowFrame() { |
| 307 | CHECK(top_shadow_frame_ != NULL); |
| 308 | ShadowFrame* frame = top_shadow_frame_; |
| 309 | top_shadow_frame_ = frame->GetLink(); |
| 310 | return frame; |
| 311 | } |
| 312 | |
| 313 | ShadowFrame* GetTopShadowFrame() const { |
| 314 | return top_shadow_frame_; |
| 315 | } |
| 316 | |
| 317 | static size_t TopShadowFrameOffset() { |
| 318 | return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_); |
| 319 | } |
| 320 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 321 | size_t NumJniShadowFrameReferences() const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 322 | |
| 323 | bool ShadowFramesContain(Object** shadow_frame_entry) const; |
| 324 | |
| 325 | private: |
| 326 | ManagedStack* link_; |
| 327 | ShadowFrame* top_shadow_frame_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 328 | AbstractMethod** top_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 329 | uintptr_t top_quick_frame_pc_; |
| 330 | }; |
| 331 | |
| 332 | class StackVisitor { |
| 333 | protected: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 334 | StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 335 | |
| 336 | public: |
| 337 | virtual ~StackVisitor() {} |
| 338 | |
| 339 | // Return 'true' if we should continue to visit more frames, 'false' to stop. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 340 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 341 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | void WalkStack(bool include_transitions = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 343 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 344 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 345 | AbstractMethod* GetMethod() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 346 | if (cur_shadow_frame_ != NULL) { |
| 347 | return cur_shadow_frame_->GetMethod(); |
| 348 | } else if (cur_quick_frame_ != NULL) { |
| 349 | return *cur_quick_frame_; |
| 350 | } else { |
| 351 | return NULL; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | bool IsShadowFrame() const { |
| 356 | return cur_shadow_frame_ != NULL; |
| 357 | } |
| 358 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 359 | uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 360 | |
| 361 | size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 362 | |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 363 | uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 364 | // Callee saves are held at the top of the frame |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 365 | DCHECK(GetMethod() != NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 366 | byte* save_addr = |
| 367 | reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize); |
| 368 | #if defined(__i386__) |
| 369 | save_addr -= kPointerSize; // account for return address |
| 370 | #endif |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 371 | return reinterpret_cast<uintptr_t*>(save_addr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 374 | // Returns the height of the stack in the managed stack frames, including transitions. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 375 | size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 376 | return GetNumFrames() - cur_depth_; |
| 377 | } |
| 378 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 379 | // Returns a frame ID for JDWP use, starting from 1. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 380 | size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 381 | return GetFrameHeight() + 1; |
| 382 | } |
| 383 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 384 | size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 385 | if (num_frames_ == 0) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 386 | num_frames_ = ComputeNumFrames(thread_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 387 | } |
| 388 | return num_frames_; |
| 389 | } |
| 390 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 391 | uint32_t GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const |
| 392 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 393 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 394 | void SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 395 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 396 | |
| 397 | uintptr_t GetGPR(uint32_t reg) const; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 398 | void SetGPR(uint32_t reg, uintptr_t value); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 399 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 400 | uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item, |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 401 | uint32_t core_spills, uint32_t fp_spills, size_t frame_size, |
| 402 | uint16_t vreg) const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 403 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 404 | DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame()); |
| 405 | byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 406 | return *reinterpret_cast<uint32_t*>(vreg_addr); |
| 407 | } |
| 408 | |
| 409 | uintptr_t GetReturnPc() const; |
| 410 | |
| 411 | void SetReturnPc(uintptr_t new_ret_pc); |
| 412 | |
| 413 | /* |
| 414 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 415 | * spill or Method* in bytes using Method*. |
| 416 | * Note that (reg >= 0) refers to a Dalvik register, (reg == -2) |
| 417 | * denotes Method* and (reg <= -3) denotes a compiler temp. |
| 418 | * |
| 419 | * +------------------------+ |
| 420 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 421 | * | . | |
| 422 | * | IN[0] | |
| 423 | * | caller's Method* | |
| 424 | * +========================+ {Note: start of callee's frame} |
| 425 | * | core callee-save spill | {variable sized} |
| 426 | * +------------------------+ |
| 427 | * | fp callee-save spill | |
| 428 | * +------------------------+ |
| 429 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 430 | * +------------------------+ |
| 431 | * | V[locals-1] | |
| 432 | * | V[locals-2] | |
| 433 | * | . | |
| 434 | * | . | ... (reg == 2) |
| 435 | * | V[1] | ... (reg == 1) |
| 436 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 437 | * +------------------------+ |
| 438 | * | Compiler temps | ... (reg == -2) |
| 439 | * | | ... (reg == -3) |
| 440 | * | | ... (reg == -4) |
| 441 | * +------------------------+ |
| 442 | * | stack alignment padding| {0 to (kStackAlignWords-1) of padding} |
| 443 | * +------------------------+ |
| 444 | * | OUT[outs-1] | |
| 445 | * | OUT[outs-2] | |
| 446 | * | . | |
| 447 | * | OUT[0] | |
| 448 | * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned |
| 449 | * +========================+ |
| 450 | */ |
| 451 | static int GetVRegOffset(const DexFile::CodeItem* code_item, |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 452 | uint32_t core_spills, uint32_t fp_spills, |
| 453 | size_t frame_size, int reg) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 454 | DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U); |
| 455 | int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler. |
| 456 | int num_ins = code_item->ins_size_; |
| 457 | int num_regs = code_item->registers_size_ - num_ins; |
| 458 | int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t)); |
| 459 | if (reg == -2) { |
| 460 | return 0; // Method* |
| 461 | } else if (reg <= -3) { |
| 462 | return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp. |
| 463 | } else if (reg < num_regs) { |
| 464 | return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg. |
| 465 | } else { |
| 466 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in. |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | uintptr_t GetCurrentQuickFramePc() const { |
| 471 | return cur_quick_frame_pc_; |
| 472 | } |
| 473 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 474 | AbstractMethod** GetCurrentQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 475 | return cur_quick_frame_; |
| 476 | } |
| 477 | |
| 478 | ShadowFrame* GetCurrentShadowFrame() const { |
| 479 | return cur_shadow_frame_; |
| 480 | } |
| 481 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 482 | StackIndirectReferenceTable* GetCurrentSirt() const { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 483 | AbstractMethod** sp = GetCurrentQuickFrame(); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 484 | ++sp; // Skip Method*; SIRT comes next; |
| 485 | return reinterpret_cast<StackIndirectReferenceTable*>(sp); |
| 486 | } |
| 487 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 488 | std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 489 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 490 | static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 491 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 492 | static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 493 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 494 | private: |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 495 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 496 | InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 497 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 498 | void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 499 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame^] | 500 | Thread* const thread_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 501 | ShadowFrame* cur_shadow_frame_; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 502 | AbstractMethod** cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 503 | uintptr_t cur_quick_frame_pc_; |
| 504 | // Lazily computed, number of frames in the stack. |
| 505 | size_t num_frames_; |
| 506 | // Depth of the frame we're currently at. |
| 507 | size_t cur_depth_; |
| 508 | protected: |
| 509 | Context* const context_; |
| 510 | }; |
| 511 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 512 | class VmapTable { |
| 513 | public: |
| 514 | explicit VmapTable(const uint16_t* table) : table_(table) { |
| 515 | } |
| 516 | |
| 517 | uint16_t operator[](size_t i) const { |
| 518 | return table_[i + 1]; |
| 519 | } |
| 520 | |
| 521 | size_t size() const { |
| 522 | return table_[0]; |
| 523 | } |
| 524 | |
| 525 | // Is the dex register 'vreg' in the context or on the stack? Should not be called when the |
| 526 | // 'kind' is unknown or constant. |
| 527 | bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const { |
| 528 | DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg || |
| 529 | kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg || |
| 530 | kind == kDoubleHiVReg || kind == kImpreciseConstant); |
| 531 | vmap_offset = 0xEBAD0FF5; |
| 532 | // TODO: take advantage of the registers being ordered |
| 533 | // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values |
| 534 | // are never promoted to floating point registers. |
| 535 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 536 | bool in_floats = false; |
| 537 | for (size_t i = 0; i < size(); ++i) { |
| 538 | // Stop if we find what we are are looking for. |
| 539 | if ((table_[i + 1] == vreg) && (in_floats == is_float)) { |
| 540 | vmap_offset = i; |
| 541 | return true; |
| 542 | } |
| 543 | // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers. |
| 544 | if (table_[i + 1] == 0xffff) { |
| 545 | in_floats = true; |
| 546 | } |
| 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed |
| 552 | // by IsInContext above). If the kind is floating point then the result will be a floating point |
| 553 | // register number, otherwise it will be an integer register number. |
| 554 | uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const { |
| 555 | // Compute the register we need to load from the context. |
| 556 | DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg || |
| 557 | kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg || |
| 558 | kind == kDoubleHiVReg || kind == kImpreciseConstant); |
| 559 | // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values |
| 560 | // are never promoted to floating point registers. |
| 561 | bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); |
| 562 | uint32_t matches = 0; |
| 563 | if (is_float) { |
| 564 | while (table_[matches] != 0xffff) { |
| 565 | matches++; |
| 566 | } |
| 567 | } |
| 568 | CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask))); |
| 569 | uint32_t spill_shifts = 0; |
| 570 | while (matches != (vmap_offset + 1)) { |
| 571 | DCHECK_NE(spill_mask, 0u); |
| 572 | matches += spill_mask & 1; // Add 1 if the low bit is set |
| 573 | spill_mask >>= 1; |
| 574 | spill_shifts++; |
| 575 | } |
| 576 | spill_shifts--; // wind back one as we want the last match |
| 577 | return spill_shifts; |
| 578 | } |
| 579 | private: |
| 580 | const uint16_t* table_; |
| 581 | }; |
| 582 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 583 | } // namespace art |
| 584 | |
| 585 | #endif // ART_SRC_STACK_H_ |