blob: 845b84090482cdebe665fd41d9586dbef8f49cf9 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
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
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070021#include "heap.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070022#include "jni.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023#include "macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070024#include "oat/runtime/context.h"
25#include "trace.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070026
27#include <stdint.h>
28
29namespace art {
30
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
33class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070034class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070036class Thread;
37
Ian Rogers0399dde2012-06-06 17:09:28 -070038class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070039 public:
Ian Rogers0399dde2012-06-06 17:09:28 -070040 uint32_t NumberOfReferences() const {
41 return number_of_references_;
42 }
Elliott Hughes68e76522011-10-05 13:22:16 -070043
Ian Rogers5438ad82012-10-15 17:22:44 -070044 void SetNumberOfReferences(uint16_t number_of_references) {
Ian Rogers0399dde2012-06-06 17:09:28 -070045 number_of_references_ = number_of_references;
46 }
47
Ian Rogers5438ad82012-10-15 17:22:44 -070048 void SetNumberOfVRegs(uint16_t number_of_vregs) {
49 number_of_vregs_ = number_of_vregs;
50 }
51
Ian Rogers0399dde2012-06-06 17:09:28 -070052 uint32_t GetDexPC() const {
53 return dex_pc_;
54 }
55
56 void SetDexPC(uint32_t dex_pc) {
57 dex_pc_ = dex_pc;
58 }
59
Ian Rogers0399dde2012-06-06 17:09:28 -070060 ShadowFrame* GetLink() const {
61 return link_;
62 }
63
64 void SetLink(ShadowFrame* frame) {
65 DCHECK_NE(this, frame);
66 link_ = frame;
67 }
68
69 Object* GetReference(size_t i) const {
70 DCHECK_LT(i, number_of_references_);
71 return references_[i];
72 }
73
74 void SetReference(size_t i, Object* object) {
75 DCHECK_LT(i, number_of_references_);
76 references_[i] = object;
77 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080078
Mathieu Chartier66f19252012-09-18 08:57:04 -070079 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070080 DCHECK_NE(method_, static_cast<void*>(NULL));
81 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -070082 }
83
Mathieu Chartier66f19252012-09-18 08:57:04 -070084 void SetMethod(AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -070085 DCHECK_NE(method, static_cast<void*>(NULL));
86 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -070087 }
88
Ian Rogers0399dde2012-06-06 17:09:28 -070089 bool Contains(Object** shadow_frame_entry) const {
90 return ((&references_[0] <= shadow_frame_entry) &&
91 (shadow_frame_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070092 }
93
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070094 template <typename Visitor>
95 void VisitRoots(const Visitor& visitor) {
Ian Rogers0399dde2012-06-06 17:09:28 -070096 size_t num_refs = NumberOfReferences();
97 for (size_t j = 0; j < num_refs; j++) {
98 Object* object = GetReference(j);
99 if (object != NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700100 visitor(object, j);
Ian Rogers0399dde2012-06-06 17:09:28 -0700101 }
102 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700103 }
104
Ian Rogers0399dde2012-06-06 17:09:28 -0700105 static size_t LinkOffset() {
106 return OFFSETOF_MEMBER(ShadowFrame, link_);
107 }
108
Ian Rogers0399dde2012-06-06 17:09:28 -0700109 static size_t MethodOffset() {
110 return OFFSETOF_MEMBER(ShadowFrame, method_);
111 }
112
Ian Rogers0399dde2012-06-06 17:09:28 -0700113 static size_t DexPCOffset() {
114 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
115 }
116
Ian Rogers0399dde2012-06-06 17:09:28 -0700117 static size_t NumberOfReferencesOffset() {
118 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
119 }
120
Ian Rogers5438ad82012-10-15 17:22:44 -0700121 static size_t NumberOfVRegsOffset() {
122 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
123 }
124
Ian Rogers0399dde2012-06-06 17:09:28 -0700125 static size_t ReferencesOffset() {
126 return OFFSETOF_MEMBER(ShadowFrame, references_);
127 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700128
Ian Rogers5438ad82012-10-15 17:22:44 -0700129 size_t VRegsOffset() {
130 return ReferencesOffset() + (sizeof(Object*) * NumberOfReferences());
131 }
132
Elliott Hughes68e76522011-10-05 13:22:16 -0700133 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 // ShadowFrame should be allocated by the generated code directly.
135 // We should not create new shadow stack in the runtime support function.
136 ~ShadowFrame() {}
Elliott Hughes68e76522011-10-05 13:22:16 -0700137
Ian Rogers5438ad82012-10-15 17:22:44 -0700138 uint16_t number_of_references_;
139 uint16_t number_of_vregs_;
140 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700141 ShadowFrame* link_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700142 AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700143 uint32_t dex_pc_;
144 Object* references_[];
Elliott Hughes68e76522011-10-05 13:22:16 -0700145
Ian Rogers0399dde2012-06-06 17:09:28 -0700146 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700147};
148
Ian Rogers0399dde2012-06-06 17:09:28 -0700149// The managed stack is used to record fragments of managed code stacks. Managed code stacks
150// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
151// necessary for transitions between code using different frame layouts and transitions into native
152// code.
153class PACKED ManagedStack {
154 public:
Ian Rogersca190662012-06-26 15:45:57 -0700155 ManagedStack()
156 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700157
158 void PushManagedStackFragment(ManagedStack* fragment) {
159 // Copy this top fragment into given fragment.
160 memcpy(fragment, this, sizeof(ManagedStack));
161 // Clear this fragment, which has become the top.
162 memset(this, 0, sizeof(ManagedStack));
163 // Link our top fragment onto the given fragment.
164 link_ = fragment;
165 }
166
167 void PopManagedStackFragment(const ManagedStack& fragment) {
168 DCHECK(&fragment == link_);
169 // Copy this given fragment back to the top.
170 memcpy(this, &fragment, sizeof(ManagedStack));
171 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700172
173 ManagedStack* GetLink() const {
174 return link_;
175 }
176
Mathieu Chartier66f19252012-09-18 08:57:04 -0700177 AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700178 return top_quick_frame_;
179 }
180
Mathieu Chartier66f19252012-09-18 08:57:04 -0700181 void SetTopQuickFrame(AbstractMethod** top) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700182 top_quick_frame_ = top;
183 }
184
185 uintptr_t GetTopQuickFramePc() const {
186 return top_quick_frame_pc_;
187 }
188
189 void SetTopQuickFramePc(uintptr_t pc) {
190 top_quick_frame_pc_ = pc;
191 }
192
193 static size_t TopQuickFrameOffset() {
194 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
195 }
196
197 static size_t TopQuickFramePcOffset() {
198 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
199 }
200
201 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
202 ShadowFrame* old_frame = top_shadow_frame_;
203 top_shadow_frame_ = new_top_frame;
204 new_top_frame->SetLink(old_frame);
205 return old_frame;
206 }
207
208 ShadowFrame* PopShadowFrame() {
209 CHECK(top_shadow_frame_ != NULL);
210 ShadowFrame* frame = top_shadow_frame_;
211 top_shadow_frame_ = frame->GetLink();
212 return frame;
213 }
214
215 ShadowFrame* GetTopShadowFrame() const {
216 return top_shadow_frame_;
217 }
218
219 static size_t TopShadowFrameOffset() {
220 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
221 }
222
223 size_t NumShadowFrameReferences() const;
224
225 bool ShadowFramesContain(Object** shadow_frame_entry) const;
226
227 private:
228 ManagedStack* link_;
229 ShadowFrame* top_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700230 AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 uintptr_t top_quick_frame_pc_;
232};
233
234class StackVisitor {
235 protected:
236 StackVisitor(const ManagedStack* stack, const std::vector<TraceStackFrame>* trace_stack,
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700237 Context* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700238 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700239 : stack_start_(stack), trace_stack_(trace_stack), cur_shadow_frame_(NULL),
240 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
241 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700242
243 public:
244 virtual ~StackVisitor() {}
245
246 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700247 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700248
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700251
Mathieu Chartier66f19252012-09-18 08:57:04 -0700252 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700253 if (cur_shadow_frame_ != NULL) {
254 return cur_shadow_frame_->GetMethod();
255 } else if (cur_quick_frame_ != NULL) {
256 return *cur_quick_frame_;
257 } else {
258 return NULL;
259 }
260 }
261
262 bool IsShadowFrame() const {
263 return cur_shadow_frame_ != NULL;
264 }
265
Ian Rogers0c7abda2012-09-19 13:33:42 -0700266 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
267
268 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
269
Ian Rogers0399dde2012-06-06 17:09:28 -0700270 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
271 // Callee saves are held at the top of the frame
Mathieu Chartier66f19252012-09-18 08:57:04 -0700272 AbstractMethod* method = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -0700273 DCHECK(method != NULL);
274 byte* save_addr =
275 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
276#if defined(__i386__)
277 save_addr -= kPointerSize; // account for return address
278#endif
279 return *reinterpret_cast<uintptr_t*>(save_addr);
280 }
281
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700282 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700283 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700284 return GetNumFrames() - cur_depth_;
285 }
286
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700287 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700288 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700289 return GetFrameHeight() + 1;
290 }
291
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700293 if (num_frames_ == 0) {
294 num_frames_ = ComputeNumFrames();
295 }
296 return num_frames_;
297 }
298
Mathieu Chartier66f19252012-09-18 08:57:04 -0700299 uint32_t GetVReg(AbstractMethod* m, int vreg) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700300
Mathieu Chartier66f19252012-09-18 08:57:04 -0700301 void SetVReg(AbstractMethod* m, int vreg, uint32_t new_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700303
304 uintptr_t GetGPR(uint32_t reg) const;
305
Mathieu Chartier66f19252012-09-18 08:57:04 -0700306 uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers0ec569a2012-07-01 16:43:46 -0700307 uint32_t core_spills, uint32_t fp_spills, size_t frame_size, int vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700308 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700309 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
310 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700311 return *reinterpret_cast<uint32_t*>(vreg_addr);
312 }
313
314 uintptr_t GetReturnPc() const;
315
316 void SetReturnPc(uintptr_t new_ret_pc);
317
318 /*
319 * Return sp-relative offset for a Dalvik virtual register, compiler
320 * spill or Method* in bytes using Method*.
321 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
322 * denotes Method* and (reg <= -3) denotes a compiler temp.
323 *
324 * +------------------------+
325 * | IN[ins-1] | {Note: resides in caller's frame}
326 * | . |
327 * | IN[0] |
328 * | caller's Method* |
329 * +========================+ {Note: start of callee's frame}
330 * | core callee-save spill | {variable sized}
331 * +------------------------+
332 * | fp callee-save spill |
333 * +------------------------+
334 * | filler word | {For compatibility, if V[locals-1] used as wide
335 * +------------------------+
336 * | V[locals-1] |
337 * | V[locals-2] |
338 * | . |
339 * | . | ... (reg == 2)
340 * | V[1] | ... (reg == 1)
341 * | V[0] | ... (reg == 0) <---- "locals_start"
342 * +------------------------+
343 * | Compiler temps | ... (reg == -2)
344 * | | ... (reg == -3)
345 * | | ... (reg == -4)
346 * +------------------------+
347 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
348 * +------------------------+
349 * | OUT[outs-1] |
350 * | OUT[outs-2] |
351 * | . |
352 * | OUT[0] |
353 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
354 * +========================+
355 */
356 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700357 uint32_t core_spills, uint32_t fp_spills,
358 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700359 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
360 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
361 int num_ins = code_item->ins_size_;
362 int num_regs = code_item->registers_size_ - num_ins;
363 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
364 if (reg == -2) {
365 return 0; // Method*
366 } else if (reg <= -3) {
367 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
368 } else if (reg < num_regs) {
369 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
370 } else {
371 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
372 }
373 }
374
375 uintptr_t GetCurrentQuickFramePc() const {
376 return cur_quick_frame_pc_;
377 }
378
Mathieu Chartier66f19252012-09-18 08:57:04 -0700379 AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700380 return cur_quick_frame_;
381 }
382
383 ShadowFrame* GetCurrentShadowFrame() const {
384 return cur_shadow_frame_;
385 }
386
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700387 StackIndirectReferenceTable* GetCurrentSirt() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700388 AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700389 ++sp; // Skip Method*; SIRT comes next;
390 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
391 }
392
Ian Rogers0399dde2012-06-06 17:09:28 -0700393 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700394 size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700395
396 TraceStackFrame GetTraceStackFrame(uint32_t depth) const {
397 return trace_stack_->at(trace_stack_->size() - depth - 1);
398 }
399
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700401
402 const ManagedStack* const stack_start_;
403 const std::vector<TraceStackFrame>* const trace_stack_;
404 ShadowFrame* cur_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700405 AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700406 uintptr_t cur_quick_frame_pc_;
407 // Lazily computed, number of frames in the stack.
408 size_t num_frames_;
409 // Depth of the frame we're currently at.
410 size_t cur_depth_;
411 protected:
412 Context* const context_;
413};
414
Elliott Hughes68e76522011-10-05 13:22:16 -0700415} // namespace art
416
417#endif // ART_SRC_STACK_H_