blob: 554bff47015ef5555055749bb3eb255beb75085f [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 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#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070026#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070031#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032
33namespace art {
34
35// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
36class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080037 // Number of bytes for each out register in the caller method's frame.
38 static constexpr size_t kBytesStackArgLocation = 4;
Ian Rogers848871b2013-08-05 10:56:33 -070039#if defined(__arm__)
40 // The callee save frame is pointed to by SP.
41 // | argN | |
42 // | ... | |
43 // | arg4 | |
44 // | arg3 spill | | Caller's frame
45 // | arg2 spill | |
46 // | arg1 spill | |
47 // | Method* | ---
48 // | LR |
49 // | ... | callee saves
50 // | R3 | arg3
51 // | R2 | arg2
52 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080053 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070054 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080055 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
56 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
57 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080058 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
59 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
60 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
61 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size.
62 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000063 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080064 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000065#elif defined(__aarch64__)
66 // The callee save frame is pointed to by SP.
67 // | argN | |
68 // | ... | |
69 // | arg4 | |
70 // | arg3 spill | | Caller's frame
71 // | arg2 spill | |
72 // | arg1 spill | |
73 // | Method* | ---
74 // | LR |
75 // | X28 |
76 // | : |
77 // | X19 |
78 // | X7 |
79 // | : |
80 // | X1 |
81 // | D15 |
82 // | : |
83 // | D0 |
84 // | | padding
85 // | Method* | <- sp
86 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
87 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
88 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Stuart Monteithb95a5342014-03-12 13:32:32 +000089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =16; // Offset of first FPR arg.
90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 304; // Frame size.
93 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000094 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 }
Ian Rogers848871b2013-08-05 10:56:33 -070096#elif defined(__mips__)
97 // The callee save frame is pointed to by SP.
98 // | argN | |
99 // | ... | |
100 // | arg4 | |
101 // | arg3 spill | | Caller's frame
102 // | arg2 spill | |
103 // | arg1 spill | |
104 // | Method* | ---
105 // | RA |
106 // | ... | callee saves
107 // | A3 | arg3
108 // | A2 | arg2
109 // | A1 | arg1
110 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800111 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
112 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
113 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size.
118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800120 }
Ian Rogers848871b2013-08-05 10:56:33 -0700121#elif defined(__i386__)
122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | Return |
131 // | EBP,ESI,EDI | callee saves
132 // | EBX | arg3
133 // | EDX | arg2
134 // | ECX | arg1
135 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
142 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size.
143 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000144 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800145 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800147 // The callee save frame is pointed to by SP.
148 // | argN | |
149 // | ... | |
150 // | reg. arg spills | | Caller's frame
151 // | Method* | ---
152 // | Return |
153 // | R15 | callee save
154 // | R14 | callee save
155 // | R13 | callee save
156 // | R12 | callee save
157 // | R9 | arg5
158 // | R8 | arg4
159 // | RSI/R6 | arg1
160 // | RBP/R5 | callee save
161 // | RBX/R3 | callee save
162 // | RDX/R2 | arg2
163 // | RCX/R1 | arg3
164 // | XMM7 | float arg 8
165 // | XMM6 | float arg 7
166 // | XMM5 | float arg 6
167 // | XMM4 | float arg 5
168 // | XMM3 | float arg 4
169 // | XMM2 | float arg 3
170 // | XMM1 | float arg 2
171 // | XMM0 | float arg 1
172 // | Padding |
173 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800174 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
175 static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs.
176 static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size.
181 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
182 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000183 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
186 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
187 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800188 default:
189 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
190 return 0;
191 }
192 }
Ian Rogers848871b2013-08-05 10:56:33 -0700193#else
194#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700195#endif
196
Ian Rogers936b37f2014-02-14 00:52:24 -0800197 public:
198 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
200 DCHECK((*sp)->IsCalleeSaveMethod());
201 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700203 }
204
Ian Rogers936b37f2014-02-14 00:52:24 -0800205 // For the given quick ref and args quick frame, return the caller's PC.
206 static uintptr_t GetCallingPc(mirror::ArtMethod** sp)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
208 DCHECK((*sp)->IsCalleeSaveMethod());
209 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700210 return *reinterpret_cast<uintptr_t*>(lr);
211 }
212
Brian Carlstromea46f952013-07-30 01:26:50 -0700213 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700214 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
216 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
217 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
218 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
219 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
220 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
221 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
222 is_split_long_or_double_(false) {
223 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
Ian Rogers848871b2013-08-05 10:56:33 -0700224 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
225 }
226
227 virtual ~QuickArgumentVisitor() {}
228
229 virtual void Visit() = 0;
230
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 Primitive::Type GetParamPrimitiveType() const {
232 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700233 }
234
235 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800236 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 Primitive::Type type = GetParamPrimitiveType();
238 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800239 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000240 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700242 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800243 }
244 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800245 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800246 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
247 }
248 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700249 }
250
251 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000252 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 return is_split_long_or_double_;
254 } else {
255 return false; // An optimization for when GPR and FPRs are 64bit.
256 }
Ian Rogers848871b2013-08-05 10:56:33 -0700257 }
258
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700260 return GetParamPrimitiveType() == Primitive::kPrimNot;
261 }
262
Ian Rogers936b37f2014-02-14 00:52:24 -0800263 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700264 Primitive::Type type = GetParamPrimitiveType();
265 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
266 }
267
268 uint64_t ReadSplitLongParam() const {
269 DCHECK(IsSplitLongOrDouble());
270 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
271 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
272 return (low_half & 0xffffffffULL) | (high_half << 32);
273 }
274
275 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700276 // This implementation doesn't support reg-spill area for hard float
277 // ABI targets such as x86_64 and aarch64. So, for those targets whose
278 // 'kQuickSoftFloatAbi' is 'false':
279 // (a) 'stack_args_' should point to the first method's argument
280 // (b) whatever the argument type it is, the 'stack_index_' should
281 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800282 gpr_index_ = 0;
283 fpr_index_ = 0;
284 stack_index_ = 0;
285 if (!is_static_) { // Handle this.
286 cur_type_ = Primitive::kPrimNot;
287 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700288 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700289 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
290 stack_index_++;
291 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800292 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 }
Ian Rogers848871b2013-08-05 10:56:33 -0700295 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
297 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
298 switch (cur_type_) {
299 case Primitive::kPrimNot:
300 case Primitive::kPrimBoolean:
301 case Primitive::kPrimByte:
302 case Primitive::kPrimChar:
303 case Primitive::kPrimShort:
304 case Primitive::kPrimInt:
305 is_split_long_or_double_ = false;
306 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700307 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
308 stack_index_++;
309 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800310 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800311 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 }
313 break;
314 case Primitive::kPrimFloat:
315 is_split_long_or_double_ = false;
316 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800317 if (kQuickSoftFloatAbi) {
318 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800319 gpr_index_++;
320 } else {
321 stack_index_++;
322 }
323 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800324 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800325 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800326 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700327 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800328 }
329 break;
330 case Primitive::kPrimDouble:
331 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800332 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000333 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800334 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800335 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700336 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800337 if (kBytesStackArgLocation == 4) {
338 stack_index_+= 2;
339 } else {
340 CHECK_EQ(kBytesStackArgLocation, 8U);
341 stack_index_++;
342 }
343 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700344 if (gpr_index_ < kNumQuickGprArgs) {
345 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000346 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700347 if (gpr_index_ < kNumQuickGprArgs) {
348 gpr_index_++;
349 } else if (kQuickSoftFloatAbi) {
350 stack_index_++;
351 }
352 }
353 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800358 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800359 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000360 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800361 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 }
364 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700365 }
366 if (kBytesStackArgLocation == 4) {
367 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700369 CHECK_EQ(kBytesStackArgLocation, 8U);
370 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800371 }
372 }
373 break;
374 default:
375 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
376 }
Ian Rogers848871b2013-08-05 10:56:33 -0700377 }
378 }
379
380 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
382 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800383 if (kQuickSoftFloatAbi) {
384 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000385 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
386 + GetBytesPerGprSpillLocation(kRuntimeISA) /* ArtMethod* */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700388 // For now, there is no reg-spill area for the targets with
389 // hard float ABI. So, the offset pointing to the first method's
390 // parameter ('this' for non-static methods) should be returned.
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000391 return GetBytesPerGprSpillLocation(kRuntimeISA); // Skip Method*.
Ian Rogers848871b2013-08-05 10:56:33 -0700392 }
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
394
395 const bool is_static_;
396 const char* const shorty_;
397 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
399 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
400 byte* const stack_args_; // Address of stack arguments in caller's frame.
401 uint32_t gpr_index_; // Index into spilled GPRs.
402 uint32_t fpr_index_; // Index into spilled FPRs.
403 uint32_t stack_index_; // Index into arguments on the stack.
404 // The current type of argument during VisitArguments.
405 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700406 // Does a 64bit parameter straddle the register and stack arguments?
407 bool is_split_long_or_double_;
408};
409
410// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800411class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700412 public:
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
414 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700415 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
416
Ian Rogers9758f792014-03-13 09:02:55 -0700417 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700418
419 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800420 ShadowFrame* const sf_;
421 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700422
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700423 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700424};
425
Ian Rogers9758f792014-03-13 09:02:55 -0700426void BuildQuickShadowFrameVisitor::Visit() {
427 Primitive::Type type = GetParamPrimitiveType();
428 switch (type) {
429 case Primitive::kPrimLong: // Fall-through.
430 case Primitive::kPrimDouble:
431 if (IsSplitLongOrDouble()) {
432 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
433 } else {
434 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
435 }
436 ++cur_reg_;
437 break;
438 case Primitive::kPrimNot: {
439 StackReference<mirror::Object>* stack_ref =
440 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
441 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
442 }
443 break;
444 case Primitive::kPrimBoolean: // Fall-through.
445 case Primitive::kPrimByte: // Fall-through.
446 case Primitive::kPrimChar: // Fall-through.
447 case Primitive::kPrimShort: // Fall-through.
448 case Primitive::kPrimInt: // Fall-through.
449 case Primitive::kPrimFloat:
450 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
451 break;
452 case Primitive::kPrimVoid:
453 LOG(FATAL) << "UNREACHABLE";
454 break;
455 }
456 ++cur_reg_;
457}
458
Brian Carlstromea46f952013-07-30 01:26:50 -0700459extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
460 mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
462 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
463 // frame.
464 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
465
466 if (method->IsAbstract()) {
467 ThrowAbstractMethodError(method);
468 return 0;
469 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800470 DCHECK(!method->IsNative()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700471 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
472 MethodHelper mh(method);
473 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800474 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700475 uint16_t num_regs = code_item->registers_size_;
476 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
477 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
478 method, 0, memory));
479 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700480 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 mh.GetShortyLength(),
482 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700483 shadow_frame_builder.VisitArguments();
484 // Push a transition back into managed code onto the linked list in thread.
485 ManagedStack fragment;
486 self->PushManagedStackFragment(&fragment);
487 self->PushShadowFrame(shadow_frame);
488 self->EndAssertNoThreadSuspension(old_cause);
489
490 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
491 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 StackHandleScope<1> hs(self);
493 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
494 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800495 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700496 self->PopManagedStackFragment(fragment);
497 return 0;
498 }
499 }
500
501 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
502 // Pop transition.
503 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800504 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700505 return result.GetJ();
506 }
507}
508
509// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
510// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800511class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700512 public:
Brian Carlstromea46f952013-07-30 01:26:50 -0700513 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
Ian Rogers848871b2013-08-05 10:56:33 -0700514 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa,
515 std::vector<jvalue>* args) :
516 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
517
Ian Rogers9758f792014-03-13 09:02:55 -0700518 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700519
Ian Rogers9758f792014-03-13 09:02:55 -0700520 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800521
Ian Rogers848871b2013-08-05 10:56:33 -0700522 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700523 ScopedObjectAccessUnchecked* const soa_;
524 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800525 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700526 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700527
Ian Rogers848871b2013-08-05 10:56:33 -0700528 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
529};
530
Ian Rogers9758f792014-03-13 09:02:55 -0700531void BuildQuickArgumentVisitor::Visit() {
532 jvalue val;
533 Primitive::Type type = GetParamPrimitiveType();
534 switch (type) {
535 case Primitive::kPrimNot: {
536 StackReference<mirror::Object>* stack_ref =
537 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
538 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
539 references_.push_back(std::make_pair(val.l, stack_ref));
540 break;
541 }
542 case Primitive::kPrimLong: // Fall-through.
543 case Primitive::kPrimDouble:
544 if (IsSplitLongOrDouble()) {
545 val.j = ReadSplitLongParam();
546 } else {
547 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
548 }
549 break;
550 case Primitive::kPrimBoolean: // Fall-through.
551 case Primitive::kPrimByte: // Fall-through.
552 case Primitive::kPrimChar: // Fall-through.
553 case Primitive::kPrimShort: // Fall-through.
554 case Primitive::kPrimInt: // Fall-through.
555 case Primitive::kPrimFloat:
556 val.i = *reinterpret_cast<jint*>(GetParamAddress());
557 break;
558 case Primitive::kPrimVoid:
559 LOG(FATAL) << "UNREACHABLE";
560 val.j = 0;
561 break;
562 }
563 args_->push_back(val);
564}
565
566void BuildQuickArgumentVisitor::FixupReferences() {
567 // Fixup any references which may have changed.
568 for (const auto& pair : references_) {
569 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700570 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700571 }
572}
573
Ian Rogers848871b2013-08-05 10:56:33 -0700574// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
575// which is responsible for recording callee save registers. We explicitly place into jobjects the
576// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
577// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700578extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700579 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700582 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
583 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700584 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
585 const char* old_cause =
586 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
587 // Register the top of the managed stack, making stack crawlable.
Brian Carlstromd3633d52013-08-20 21:06:26 -0700588 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700589 self->SetTopOfStack(sp, 0);
590 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700591 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
592 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700593 self->VerifyStack();
594 // Start new JNI local reference state.
595 JNIEnvExt* env = self->GetJniEnv();
596 ScopedObjectAccessUnchecked soa(env);
597 ScopedJniEnvLocalRefState env_state(env);
598 // Create local ref. copies of proxy method and the receiver.
599 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
600
601 // Placing arguments into args vector and remove the receiver.
602 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700603 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700604 std::vector<jvalue> args;
605 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
606 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700607
Ian Rogers848871b2013-08-05 10:56:33 -0700608 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700609 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700610 args.erase(args.begin());
611
612 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700614 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700615 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
616 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
617
618 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
619 // that performs allocations.
620 self->EndAssertNoThreadSuspension(old_cause);
621 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
622 rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800623 // Restore references which might have moved.
624 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700625 return result.GetJ();
626}
627
628// Read object references held in arguments from quick frames and place in a JNI local references,
629// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800630class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700631 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700632 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
633 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700634 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
635
Ian Rogers9758f792014-03-13 09:02:55 -0700636 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700637
Ian Rogers9758f792014-03-13 09:02:55 -0700638 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700639
640 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700641 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800642 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700643 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700644 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700645};
646
Ian Rogers9758f792014-03-13 09:02:55 -0700647void RememberForGcArgumentVisitor::Visit() {
648 if (IsParamAReference()) {
649 StackReference<mirror::Object>* stack_ref =
650 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
651 jobject reference =
652 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
653 references_.push_back(std::make_pair(reference, stack_ref));
654 }
655}
656
657void RememberForGcArgumentVisitor::FixupReferences() {
658 // Fixup any references which may have changed.
659 for (const auto& pair : references_) {
660 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700661 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700662 }
663}
664
665
Ian Rogers848871b2013-08-05 10:56:33 -0700666// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700667extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700668 mirror::Object* receiver,
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800669 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700670 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800671 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700672 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800673 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700674 ScopedObjectAccessUnchecked soa(env);
675 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800676 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700677
678 // Compute details about the called method (avoid GCs)
679 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700681 InvokeType invoke_type;
682 const DexFile* dex_file;
683 uint32_t dex_method_idx;
684 if (called->IsRuntimeMethod()) {
685 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
686 const DexFile::CodeItem* code;
687 {
688 MethodHelper mh(caller);
689 dex_file = &mh.GetDexFile();
690 code = mh.GetCodeItem();
691 }
692 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
693 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
694 Instruction::Code instr_code = instr->Opcode();
695 bool is_range;
696 switch (instr_code) {
697 case Instruction::INVOKE_DIRECT:
698 invoke_type = kDirect;
699 is_range = false;
700 break;
701 case Instruction::INVOKE_DIRECT_RANGE:
702 invoke_type = kDirect;
703 is_range = true;
704 break;
705 case Instruction::INVOKE_STATIC:
706 invoke_type = kStatic;
707 is_range = false;
708 break;
709 case Instruction::INVOKE_STATIC_RANGE:
710 invoke_type = kStatic;
711 is_range = true;
712 break;
713 case Instruction::INVOKE_SUPER:
714 invoke_type = kSuper;
715 is_range = false;
716 break;
717 case Instruction::INVOKE_SUPER_RANGE:
718 invoke_type = kSuper;
719 is_range = true;
720 break;
721 case Instruction::INVOKE_VIRTUAL:
722 invoke_type = kVirtual;
723 is_range = false;
724 break;
725 case Instruction::INVOKE_VIRTUAL_RANGE:
726 invoke_type = kVirtual;
727 is_range = true;
728 break;
729 case Instruction::INVOKE_INTERFACE:
730 invoke_type = kInterface;
731 is_range = false;
732 break;
733 case Instruction::INVOKE_INTERFACE_RANGE:
734 invoke_type = kInterface;
735 is_range = true;
736 break;
737 default:
738 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
739 // Avoid used uninitialized warnings.
740 invoke_type = kDirect;
741 is_range = false;
742 }
743 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
744
745 } else {
746 invoke_type = kStatic;
747 dex_file = &MethodHelper(called).GetDexFile();
748 dex_method_idx = called->GetDexMethodIndex();
749 }
750 uint32_t shorty_len;
751 const char* shorty =
752 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700753 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700754 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800755 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800756 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700757 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700758 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700759 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700760 mirror::Object* dummy = nullptr;
761 HandleWrapper<mirror::Object> h_receiver(
762 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
763 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700764 }
765 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800766 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700767 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800768 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
769 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800770 if (virtual_or_interface) {
771 // Refine called method based on receiver.
772 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700773
774 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800775 if (invoke_type == kVirtual) {
776 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
777 } else {
778 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
779 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700780
781 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
782 << PrettyTypeOf(receiver) << " "
783 << invoke_type << " " << orig_called->GetVtableIndex();
784
Ian Rogers83883d72013-10-21 21:07:24 -0700785 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
786 // of the sharpened method.
787 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100788 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700789 } else {
790 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000791 // the caller's dex file. Since we get here only if the original called was a runtime
792 // method, we've got the correct dex_file and a dex_method_idx from above.
793 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700794 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000795 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700796 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100797 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700798 }
799 }
800 }
Ian Rogers848871b2013-08-05 10:56:33 -0700801 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700802 StackHandleScope<1> hs(soa.Self());
803 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700804 linker->EnsureInitialized(called_class, true, true);
805 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800806 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700807 } else if (called_class->IsInitializing()) {
808 if (invoke_type == kStatic) {
809 // Class is still initializing, go to oat and grab code (trampoline must be left in place
810 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800811 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700812 } else {
813 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800814 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700815 }
816 } else {
817 DCHECK(called_class->IsErroneous());
818 }
819 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800820 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700821 // Fixup any locally saved objects may have moved during a GC.
822 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700823 // Place called method in callee-save frame to be placed as first argument to quick method.
824 *sp = called;
825 return code;
826}
827
Andreas Gampec147b002014-03-06 18:11:06 -0800828
829
830/*
831 * This class uses a couple of observations to unite the different calling conventions through
832 * a few constants.
833 *
834 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
835 * possible alignment.
836 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
837 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
838 * when we have to split things
839 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
840 * and we can use Int handling directly.
841 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
842 * necessary when widening. Also, widening of Ints will take place implicitly, and the
843 * extension should be compatible with Aarch64, which mandates copying the available bits
844 * into LSB and leaving the rest unspecified.
845 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
846 * the stack.
847 * 6) There is only little endian.
848 *
849 *
850 * Actual work is supposed to be done in a delegate of the template type. The interface is as
851 * follows:
852 *
853 * void PushGpr(uintptr_t): Add a value for the next GPR
854 *
855 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
856 * padding, that is, think the architecture is 32b and aligns 64b.
857 *
858 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
859 * split this if necessary. The current state will have aligned, if
860 * necessary.
861 *
862 * void PushStack(uintptr_t): Push a value to the stack.
863 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700864 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700865 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800866 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700867 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800868 *
869 */
870template <class T> class BuildGenericJniFrameStateMachine {
871 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800872#if defined(__arm__)
873 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800874 static constexpr bool kNativeSoftFloatAbi = true;
875 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800876 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
877
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800878 static constexpr size_t kRegistersNeededForLong = 2;
879 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800880 static constexpr bool kMultiRegistersAligned = true;
881 static constexpr bool kMultiRegistersWidened = false;
882 static constexpr bool kAlignLongOnStack = true;
883 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000884#elif defined(__aarch64__)
885 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
886 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
887 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
888
889 static constexpr size_t kRegistersNeededForLong = 1;
890 static constexpr size_t kRegistersNeededForDouble = 1;
891 static constexpr bool kMultiRegistersAligned = false;
892 static constexpr bool kMultiRegistersWidened = false;
893 static constexpr bool kAlignLongOnStack = false;
894 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800895#elif defined(__mips__)
896 // TODO: These are all dummy values!
897 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
898 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
899 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
900
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800901 static constexpr size_t kRegistersNeededForLong = 2;
902 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800903 static constexpr bool kMultiRegistersAligned = true;
904 static constexpr bool kMultiRegistersWidened = true;
905 static constexpr bool kAlignLongOnStack = false;
906 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800907#elif defined(__i386__)
908 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800909 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800910 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
911 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
912
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800913 static constexpr size_t kRegistersNeededForLong = 2;
914 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800915 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
916 static constexpr bool kMultiRegistersWidened = false;
917 static constexpr bool kAlignLongOnStack = false;
918 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800919#elif defined(__x86_64__)
920 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
921 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
922 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
923
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800924 static constexpr size_t kRegistersNeededForLong = 1;
925 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800926 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800927 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800928 static constexpr bool kAlignLongOnStack = false;
929 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800930#else
931#error "Unsupported architecture"
932#endif
933
Andreas Gampec147b002014-03-06 18:11:06 -0800934 public:
935 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs),
936 fpr_index_(kNumNativeFprArgs),
937 stack_entries_(0),
938 delegate_(delegate) {
939 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
940 // the next register is even; counting down is just to make the compiler happy...
941 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
942 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
943 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800944
Andreas Gampec147b002014-03-06 18:11:06 -0800945 virtual ~BuildGenericJniFrameStateMachine() {}
946
947 bool HavePointerGpr() {
948 return gpr_index_ > 0;
949 }
950
951 void AdvancePointer(void* val) {
952 if (HavePointerGpr()) {
953 gpr_index_--;
954 PushGpr(reinterpret_cast<uintptr_t>(val));
955 } else {
956 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
957 PushStack(reinterpret_cast<uintptr_t>(val));
958 gpr_index_ = 0;
959 }
960 }
961
962
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700963 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800964 return gpr_index_ > 0;
965 }
966
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700967 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
968 uintptr_t handle = PushHandle(ptr);
969 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800970 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700971 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800972 } else {
973 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700974 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800975 gpr_index_ = 0;
976 }
977 }
978
979
980 bool HaveIntGpr() {
981 return gpr_index_ > 0;
982 }
983
984 void AdvanceInt(uint32_t val) {
985 if (HaveIntGpr()) {
986 gpr_index_--;
987 PushGpr(val);
988 } else {
989 stack_entries_++;
990 PushStack(val);
991 gpr_index_ = 0;
992 }
993 }
994
995
996 bool HaveLongGpr() {
997 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
998 }
999
1000 bool LongGprNeedsPadding() {
1001 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1002 kAlignLongOnStack && // and when it needs alignment
1003 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1004 }
1005
1006 bool LongStackNeedsPadding() {
1007 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1008 kAlignLongOnStack && // and when it needs 8B alignment
1009 (stack_entries_ & 1) == 1; // counter is odd
1010 }
1011
1012 void AdvanceLong(uint64_t val) {
1013 if (HaveLongGpr()) {
1014 if (LongGprNeedsPadding()) {
1015 PushGpr(0);
1016 gpr_index_--;
1017 }
1018 if (kRegistersNeededForLong == 1) {
1019 PushGpr(static_cast<uintptr_t>(val));
1020 } else {
1021 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1022 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1023 }
1024 gpr_index_ -= kRegistersNeededForLong;
1025 } else {
1026 if (LongStackNeedsPadding()) {
1027 PushStack(0);
1028 stack_entries_++;
1029 }
1030 if (kRegistersNeededForLong == 1) {
1031 PushStack(static_cast<uintptr_t>(val));
1032 stack_entries_++;
1033 } else {
1034 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1035 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1036 stack_entries_ += 2;
1037 }
1038 gpr_index_ = 0;
1039 }
1040 }
1041
1042
1043 bool HaveFloatFpr() {
1044 return fpr_index_ > 0;
1045 }
1046
Andreas Gampec147b002014-03-06 18:11:06 -08001047 template <typename U, typename V> V convert(U in) {
1048 CHECK_LE(sizeof(U), sizeof(V));
1049 union { U u; V v; } tmp;
1050 tmp.u = in;
1051 return tmp.v;
1052 }
1053
1054 void AdvanceFloat(float val) {
1055 if (kNativeSoftFloatAbi) {
1056 AdvanceInt(convert<float, uint32_t>(val));
1057 } else {
1058 if (HaveFloatFpr()) {
1059 fpr_index_--;
1060 if (kRegistersNeededForDouble == 1) {
1061 if (kMultiRegistersWidened) {
1062 PushFpr8(convert<double, uint64_t>(val));
1063 } else {
1064 // No widening, just use the bits.
1065 PushFpr8(convert<float, uint64_t>(val));
1066 }
1067 } else {
1068 PushFpr4(val);
1069 }
1070 } else {
1071 stack_entries_++;
1072 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1073 // Need to widen before storing: Note the "double" in the template instantiation.
1074 PushStack(convert<double, uintptr_t>(val));
1075 } else {
1076 PushStack(convert<float, uintptr_t>(val));
1077 }
1078 fpr_index_ = 0;
1079 }
1080 }
1081 }
1082
1083
1084 bool HaveDoubleFpr() {
1085 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1086 }
1087
1088 bool DoubleFprNeedsPadding() {
1089 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1090 kAlignDoubleOnStack && // and when it needs alignment
1091 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1092 }
1093
1094 bool DoubleStackNeedsPadding() {
1095 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1096 kAlignDoubleOnStack && // and when it needs 8B alignment
1097 (stack_entries_ & 1) == 1; // counter is odd
1098 }
1099
1100 void AdvanceDouble(uint64_t val) {
1101 if (kNativeSoftFloatAbi) {
1102 AdvanceLong(val);
1103 } else {
1104 if (HaveDoubleFpr()) {
1105 if (DoubleFprNeedsPadding()) {
1106 PushFpr4(0);
1107 fpr_index_--;
1108 }
1109 PushFpr8(val);
1110 fpr_index_ -= kRegistersNeededForDouble;
1111 } else {
1112 if (DoubleStackNeedsPadding()) {
1113 PushStack(0);
1114 stack_entries_++;
1115 }
1116 if (kRegistersNeededForDouble == 1) {
1117 PushStack(static_cast<uintptr_t>(val));
1118 stack_entries_++;
1119 } else {
1120 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1121 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1122 stack_entries_ += 2;
1123 }
1124 fpr_index_ = 0;
1125 }
1126 }
1127 }
1128
1129 uint32_t getStackEntries() {
1130 return stack_entries_;
1131 }
1132
1133 uint32_t getNumberOfUsedGprs() {
1134 return kNumNativeGprArgs - gpr_index_;
1135 }
1136
1137 uint32_t getNumberOfUsedFprs() {
1138 return kNumNativeFprArgs - fpr_index_;
1139 }
1140
1141 private:
1142 void PushGpr(uintptr_t val) {
1143 delegate_->PushGpr(val);
1144 }
1145 void PushFpr4(float val) {
1146 delegate_->PushFpr4(val);
1147 }
1148 void PushFpr8(uint64_t val) {
1149 delegate_->PushFpr8(val);
1150 }
1151 void PushStack(uintptr_t val) {
1152 delegate_->PushStack(val);
1153 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001154 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1155 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001156 }
1157
1158 uint32_t gpr_index_; // Number of free GPRs
1159 uint32_t fpr_index_; // Number of free FPRs
1160 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1161 // extended
1162 T* delegate_; // What Push implementation gets called
1163};
1164
1165class ComputeGenericJniFrameSize FINAL {
1166 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001167 ComputeGenericJniFrameSize() : num_handle_scope_references_(0), num_stack_entries_(0) {}
Andreas Gampec147b002014-03-06 18:11:06 -08001168
Andreas Gampec147b002014-03-06 18:11:06 -08001169 uint32_t GetStackSize() {
1170 return num_stack_entries_ * sizeof(uintptr_t);
1171 }
1172
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001173 // WARNING: After this, *sp won't be pointing to the method anymore!
1174 void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001175 void* sp, HandleScope** table, uint32_t* handle_scope_entries,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001176 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr,
1177 void** code_return, size_t* overall_size)
Andreas Gampec147b002014-03-06 18:11:06 -08001178 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1179 ComputeAll(is_static, shorty, shorty_len);
1180
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001181 mirror::ArtMethod* method = **m;
1182
Andreas Gampec147b002014-03-06 18:11:06 -08001183 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
Andreas Gampec147b002014-03-06 18:11:06 -08001184
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001185 // First, fix up the layout of the callee-save frame.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001186 // We have to squeeze in the HandleScope, and relocate the method pointer.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001187
1188 // "Free" the slot for the method.
1189 sp8 += kPointerSize;
1190
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001191 // Add the HandleScope.
1192 *handle_scope_entries = num_handle_scope_references_;
1193 size_t handle_scope_size = HandleScope::GetAlignedHandleScopeSize(num_handle_scope_references_);
1194 sp8 -= handle_scope_size;
1195 *table = reinterpret_cast<HandleScope*>(sp8);
1196 (*table)->SetNumberOfReferences(num_handle_scope_references_);
Andreas Gampec147b002014-03-06 18:11:06 -08001197
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001198 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1199 sp8 -= kPointerSize;
1200 uint8_t* method_pointer = sp8;
1201 *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method;
1202 *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer);
1203
1204 // Reference cookie and padding
1205 sp8 -= 8;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001206 // Store HandleScope size
1207 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(handle_scope_size & 0xFFFFFFFF);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001208
1209 // Next comes the native call stack.
Andreas Gampec147b002014-03-06 18:11:06 -08001210 sp8 -= GetStackSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001211 // Now align the call stack below. This aligns by 16, as AArch64 seems to require.
Andreas Gampec147b002014-03-06 18:11:06 -08001212 uintptr_t mask = ~0x0F;
1213 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask);
1214 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
1215
1216 // put fprs and gprs below
1217 // Assumption is OK right now, as we have soft-float arm
1218 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs;
1219 sp8 -= fregs * sizeof(uintptr_t);
1220 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1221 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs;
1222 sp8 -= iregs * sizeof(uintptr_t);
1223 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1224
1225 // reserve space for the code pointer
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001226 sp8 -= kPointerSize;
Andreas Gampec147b002014-03-06 18:11:06 -08001227 *code_return = reinterpret_cast<void*>(sp8);
1228
1229 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001230
1231 // The new SP is stored at the end of the alloca, so it can be immediately popped
1232 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB;
1233 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer;
Andreas Gampec147b002014-03-06 18:11:06 -08001234 }
1235
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001236 void ComputeHandleScopeOffset() { } // nothing to do, static right now
Andreas Gampec147b002014-03-06 18:11:06 -08001237
1238 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len)
1239 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1240 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this);
1241
1242 // JNIEnv
1243 sm.AdvancePointer(nullptr);
1244
1245 // Class object or this as first argument
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001246 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001247
1248 for (uint32_t i = 1; i < shorty_len; ++i) {
1249 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1250 switch (cur_type_) {
1251 case Primitive::kPrimNot:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001252 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001253 break;
1254
1255 case Primitive::kPrimBoolean:
1256 case Primitive::kPrimByte:
1257 case Primitive::kPrimChar:
1258 case Primitive::kPrimShort:
1259 case Primitive::kPrimInt:
1260 sm.AdvanceInt(0);
1261 break;
1262 case Primitive::kPrimFloat:
1263 sm.AdvanceFloat(0);
1264 break;
1265 case Primitive::kPrimDouble:
1266 sm.AdvanceDouble(0);
1267 break;
1268 case Primitive::kPrimLong:
1269 sm.AdvanceLong(0);
1270 break;
1271 default:
1272 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1273 }
1274 }
1275
1276 num_stack_entries_ = sm.getStackEntries();
1277 }
1278
1279 void PushGpr(uintptr_t /* val */) {
1280 // not optimizing registers, yet
1281 }
1282
1283 void PushFpr4(float /* val */) {
1284 // not optimizing registers, yet
1285 }
1286
1287 void PushFpr8(uint64_t /* val */) {
1288 // not optimizing registers, yet
1289 }
1290
1291 void PushStack(uintptr_t /* val */) {
1292 // counting is already done in the superclass
1293 }
1294
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001295 uintptr_t PushHandle(mirror::Object* /* ptr */) {
1296 num_handle_scope_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001297 return reinterpret_cast<uintptr_t>(nullptr);
1298 }
1299
1300 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001301 uint32_t num_handle_scope_references_;
Andreas Gampec147b002014-03-06 18:11:06 -08001302 uint32_t num_stack_entries_;
1303};
1304
1305// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1306// of transitioning into native code.
1307class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001308 public:
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001309 BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty,
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001310 uint32_t shorty_len, Thread* self) :
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001311 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) {
Andreas Gampec147b002014-03-06 18:11:06 -08001312 ComputeGenericJniFrameSize fsc;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001313 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &handle_scope_, &handle_scope_expected_refs_,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001314 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_,
1315 &alloca_used_size_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001316 handle_scope_number_of_references_ = 0;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001317 cur_hs_entry_ = GetFirstHandleScopeEntry();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001318
1319 // jni environment is always first argument
Andreas Gampec147b002014-03-06 18:11:06 -08001320 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001321
1322 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001323 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001324 }
1325 }
1326
Ian Rogers9758f792014-03-13 09:02:55 -07001327 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001328
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001329 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001330
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001331 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1333 return handle_scope_->GetHandle(0).GetReference();
1334 }
1335
1336 jobject GetFirstHandleScopeJObject()
1337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001338 return handle_scope_->GetHandle(0).ToJObject();
Andreas Gampec147b002014-03-06 18:11:06 -08001339 }
1340
1341 void PushGpr(uintptr_t val) {
1342 *cur_gpr_reg_ = val;
1343 cur_gpr_reg_++;
1344 }
1345
1346 void PushFpr4(float val) {
1347 *cur_fpr_reg_ = val;
1348 cur_fpr_reg_++;
1349 }
1350
1351 void PushFpr8(uint64_t val) {
1352 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1353 *tmp = val;
1354 cur_fpr_reg_ += 2;
1355 }
1356
1357 void PushStack(uintptr_t val) {
1358 *cur_stack_arg_ = val;
1359 cur_stack_arg_++;
1360 }
1361
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001362 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001363 uintptr_t tmp;
1364 if (ref == nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001365 *cur_hs_entry_ = StackReference<mirror::Object>();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001366 tmp = reinterpret_cast<uintptr_t>(nullptr);
1367 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001368 *cur_hs_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref);
1369 tmp = reinterpret_cast<uintptr_t>(cur_hs_entry_);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001370 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001371 cur_hs_entry_++;
1372 handle_scope_number_of_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001373 return tmp;
1374 }
1375
1376 // Size of the part of the alloca that we actually need.
1377 size_t GetAllocaUsedSize() {
1378 return alloca_used_size_;
1379 }
1380
1381 void* GetCodeReturn() {
1382 return code_return_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001383 }
1384
1385 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001386 uint32_t handle_scope_number_of_references_;
1387 StackReference<mirror::Object>* cur_hs_entry_;
1388 HandleScope* handle_scope_;
1389 uint32_t handle_scope_expected_refs_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001390 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001391 uint32_t* cur_fpr_reg_;
1392 uintptr_t* cur_stack_arg_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001393 // StackReference<mirror::Object>* top_of_handle_scope_;
Andreas Gampec147b002014-03-06 18:11:06 -08001394 void* code_return_;
1395 size_t alloca_used_size_;
1396
1397 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001398
1399 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1400};
1401
Ian Rogers9758f792014-03-13 09:02:55 -07001402void BuildGenericJniFrameVisitor::Visit() {
1403 Primitive::Type type = GetParamPrimitiveType();
1404 switch (type) {
1405 case Primitive::kPrimLong: {
1406 jlong long_arg;
1407 if (IsSplitLongOrDouble()) {
1408 long_arg = ReadSplitLongParam();
1409 } else {
1410 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1411 }
1412 sm_.AdvanceLong(long_arg);
1413 break;
1414 }
1415 case Primitive::kPrimDouble: {
1416 uint64_t double_arg;
1417 if (IsSplitLongOrDouble()) {
1418 // Read into union so that we don't case to a double.
1419 double_arg = ReadSplitLongParam();
1420 } else {
1421 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1422 }
1423 sm_.AdvanceDouble(double_arg);
1424 break;
1425 }
1426 case Primitive::kPrimNot: {
1427 StackReference<mirror::Object>* stack_ref =
1428 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001429 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001430 break;
1431 }
1432 case Primitive::kPrimFloat:
1433 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1434 break;
1435 case Primitive::kPrimBoolean: // Fall-through.
1436 case Primitive::kPrimByte: // Fall-through.
1437 case Primitive::kPrimChar: // Fall-through.
1438 case Primitive::kPrimShort: // Fall-through.
1439 case Primitive::kPrimInt: // Fall-through.
1440 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1441 break;
1442 case Primitive::kPrimVoid:
1443 LOG(FATAL) << "UNREACHABLE";
1444 break;
1445 }
1446}
1447
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001448void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Ian Rogers9758f792014-03-13 09:02:55 -07001449 // Initialize padding entries.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001450 while (handle_scope_number_of_references_ < handle_scope_expected_refs_) {
1451 *cur_hs_entry_ = StackReference<mirror::Object>();
1452 cur_hs_entry_++;
1453 handle_scope_number_of_references_++;
Ian Rogers9758f792014-03-13 09:02:55 -07001454 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001455 handle_scope_->SetNumberOfReferences(handle_scope_expected_refs_);
1456 DCHECK_NE(handle_scope_expected_refs_, 0U);
1457 // Install HandleScope.
1458 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001459}
1460
Andreas Gampe90546832014-03-12 18:07:19 -07001461extern "C" void* artFindNativeMethod();
1462
Andreas Gampead615172014-04-04 16:20:13 -07001463uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1464 if (lock != nullptr) {
1465 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1466 } else {
1467 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1468 }
1469}
1470
1471void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1472 if (lock != nullptr) {
1473 JniMethodEndSynchronized(cookie, lock, self);
1474 } else {
1475 JniMethodEnd(cookie, self);
1476 }
1477}
1478
Andreas Gampec147b002014-03-06 18:11:06 -08001479/*
1480 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001481 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers.
Andreas Gampec147b002014-03-06 18:11:06 -08001482 * The final element on the stack is a pointer to the native code.
1483 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001484 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001485 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001486 *
Andreas Gampec147b002014-03-06 18:11:06 -08001487 * The return of this function denotes:
1488 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1489 * 2) An error, if the value is negative.
1490 */
1491extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001492 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001493 mirror::ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001494 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001495
1496 // run the visitor
1497 MethodHelper mh(called);
Andreas Gampec147b002014-03-06 18:11:06 -08001498
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001499 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(),
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001500 self);
1501 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001502 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001503
1504 // fix up managed-stack things in Thread
1505 self->SetTopOfStack(sp, 0);
1506
Ian Rogerse0dcd462014-03-08 15:21:04 -08001507 self->VerifyStack();
1508
Andreas Gampe90546832014-03-12 18:07:19 -07001509 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001510 uint32_t cookie;
1511 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001512 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001513 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001514 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001515 // A negative value denotes an error.
1516 return -1;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001517 }
1518 } else {
1519 cookie = JniMethodStart(self);
1520 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001521 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001522 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001523
Andreas Gampe90546832014-03-12 18:07:19 -07001524 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001525 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001526
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001527 // There are two cases for the content of nativeCode:
1528 // 1) Pointer to the native function.
1529 // 2) Pointer to the trampoline for native code binding.
1530 // In the second case, we need to execute the binding and continue with the actual native function
1531 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001532 DCHECK(nativeCode != nullptr);
1533 if (nativeCode == GetJniDlsymLookupStub()) {
1534 nativeCode = artFindNativeMethod();
1535
1536 if (nativeCode == nullptr) {
1537 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001538
1539 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001540 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Andreas Gampead615172014-04-04 16:20:13 -07001541 if (mh.GetShorty()[0] == 'L') {
1542 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1543 } else {
1544 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1545 }
1546
Andreas Gampe90546832014-03-12 18:07:19 -07001547 return -1;
1548 }
1549 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001550 }
1551
Andreas Gampe90546832014-03-12 18:07:19 -07001552 // Store the native code pointer in the stack at the right location.
Andreas Gampec147b002014-03-06 18:11:06 -08001553 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn());
Andreas Gampec147b002014-03-06 18:11:06 -08001554 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode);
1555
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001556 // 5K reserved, window_size + frame pointer used.
Andreas Gampe90546832014-03-12 18:07:19 -07001557 size_t window_size = visitor.GetAllocaUsedSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001558 return (5 * KB) - window_size - kPointerSize;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001559}
1560
1561/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001562 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563 * unlocking.
1564 */
1565extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp,
1566 jvalue result, uint64_t result_f)
1567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1568 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
1569 mirror::ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08001570 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001571
Andreas Gampead615172014-04-04 16:20:13 -07001572 jobject lock = nullptr;
1573 if (called->IsSynchronized()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001574 HandleScope* table = reinterpret_cast<HandleScope*>(
1575 reinterpret_cast<uint8_t*>(sp) + kPointerSize);
1576 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001577 }
1578
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001579 MethodHelper mh(called);
1580 char return_shorty_char = mh.GetShorty()[0];
1581
1582 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001583 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001584 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001585 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001586
1587 switch (return_shorty_char) {
1588 case 'F': // Fall-through.
1589 case 'D':
1590 return result_f;
1591 case 'Z':
1592 return result.z;
1593 case 'B':
1594 return result.b;
1595 case 'C':
1596 return result.c;
1597 case 'S':
1598 return result.s;
1599 case 'I':
1600 return result.i;
1601 case 'J':
1602 return result.j;
1603 case 'V':
1604 return 0;
1605 default:
1606 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1607 return 0;
1608 }
1609 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001610}
1611
Andreas Gampe51f76352014-05-21 08:28:48 -07001612// The following definitions create return types for two word-sized entities that will be passed
1613// in registers so that memory operations for the interface trampolines can be avoided. The entities
1614// are the resolved method and the pointer to the code to be invoked.
1615//
1616// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
1617// uint64_t or long long int. We use the upper 32b for code, and the lower 32b for the method.
1618//
1619// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two
1620// size_t-sized values.
1621//
1622// We need two operations:
1623//
1624// 1) A flag value that signals failure. The assembly stubs expect the method part to be "0".
1625// GetFailureValue() will return a value that has method == 0.
1626//
1627// 2) A value that combines a code pointer and a method pointer.
1628// GetSuccessValue() constructs this.
1629
1630#if defined(__i386__) || defined(__arm__) || defined(__mips__)
1631typedef uint64_t MethodAndCode;
1632
1633// Encodes method_ptr==nullptr and code_ptr==nullptr
1634static constexpr MethodAndCode GetFailureValue() {
1635 return 0;
1636}
1637
1638// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
1639static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1640 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1641 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1642 return ((code_uint << 32) | method_uint);
1643}
1644
1645#elif defined(__x86_64__) || defined(__aarch64__)
1646struct MethodAndCode {
1647 uintptr_t method;
1648 uintptr_t code;
1649};
1650
1651// Encodes method_ptr==nullptr. Leaves random value in code pointer.
1652static MethodAndCode GetFailureValue() {
1653 MethodAndCode ret;
1654 ret.method = 0;
1655 return ret;
1656}
1657
1658// Write values into their respective members.
1659static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1660 MethodAndCode ret;
1661 ret.method = reinterpret_cast<uintptr_t>(method);
1662 ret.code = reinterpret_cast<uintptr_t>(code);
1663 return ret;
1664}
1665#else
1666#error "Unsupported architecture"
1667#endif
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001668
1669template<InvokeType type, bool access_check>
Andreas Gampe51f76352014-05-21 08:28:48 -07001670static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1671 mirror::ArtMethod* caller_method,
1672 Thread* self, mirror::ArtMethod** sp);
1673
1674template<InvokeType type, bool access_check>
1675static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1676 mirror::ArtMethod* caller_method,
1677 Thread* self, mirror::ArtMethod** sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001678 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1679 type);
1680 if (UNLIKELY(method == nullptr)) {
1681 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1682 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1683 uint32_t shorty_len;
1684 const char* shorty =
1685 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
1686 {
1687 // Remember the args in case a GC happens in FindMethodFromCode.
1688 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1689 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1690 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001691 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1692 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001693 visitor.FixupReferences();
1694 }
1695
1696 if (UNLIKELY(method == NULL)) {
1697 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001698 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001699 }
1700 }
1701 DCHECK(!self->IsExceptionPending());
1702 const void* code = method->GetEntryPointFromQuickCompiledCode();
1703
1704 // When we return, the caller will branch to this address, so it had better not be 0!
1705 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1706 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001707
1708 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001709}
1710
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001711// Explicit artInvokeCommon template function declarations to please analysis tool.
1712#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1713 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gampe51f76352014-05-21 08:28:48 -07001714 MethodAndCode artInvokeCommon<type, access_check>(uint32_t method_idx, \
1715 mirror::Object* this_object, \
1716 mirror::ArtMethod* caller_method, \
1717 Thread* self, mirror::ArtMethod** sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001718
1719EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1720EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1721EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1722EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1723EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1724EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1725EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1726EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1727EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1728EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1729#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1730
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001731
1732// See comments in runtime_support_asm.S
Andreas Gampe51f76352014-05-21 08:28:48 -07001733extern "C" MethodAndCode artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1734 mirror::Object* this_object,
1735 mirror::ArtMethod* caller_method,
1736 Thread* self,
1737 mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1739 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp);
1740}
1741
1742
Andreas Gampe51f76352014-05-21 08:28:48 -07001743extern "C" MethodAndCode artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1744 mirror::Object* this_object,
1745 mirror::ArtMethod* caller_method,
1746 Thread* self,
1747 mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001748 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1749 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp);
1750}
1751
Andreas Gampe51f76352014-05-21 08:28:48 -07001752extern "C" MethodAndCode artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1753 mirror::Object* this_object,
1754 mirror::ArtMethod* caller_method,
1755 Thread* self,
1756 mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001757 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1758 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp);
1759}
1760
Andreas Gampe51f76352014-05-21 08:28:48 -07001761extern "C" MethodAndCode artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1762 mirror::Object* this_object,
1763 mirror::ArtMethod* caller_method,
1764 Thread* self,
1765 mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001766 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1767 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp);
1768}
1769
Andreas Gampe51f76352014-05-21 08:28:48 -07001770extern "C" MethodAndCode artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1771 mirror::Object* this_object,
1772 mirror::ArtMethod* caller_method,
1773 Thread* self,
1774 mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001775 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1776 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp);
1777}
1778
1779// Determine target of interface dispatch. This object is known non-null.
Andreas Gampe51f76352014-05-21 08:28:48 -07001780extern "C" MethodAndCode artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
1781 mirror::Object* this_object,
1782 mirror::ArtMethod* caller_method,
1783 Thread* self, mirror::ArtMethod** sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1785 mirror::ArtMethod* method;
1786 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1787 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1788 if (UNLIKELY(method == NULL)) {
1789 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1790 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1791 caller_method);
Andreas Gampe51f76352014-05-21 08:28:48 -07001792 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001793 }
1794 } else {
1795 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1796 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
1797 // Determine method index from calling dex instruction.
1798#if defined(__arm__)
1799 // On entry the stack pointed by sp is:
1800 // | argN | |
1801 // | ... | |
1802 // | arg4 | |
1803 // | arg3 spill | | Caller's frame
1804 // | arg2 spill | |
1805 // | arg1 spill | |
1806 // | Method* | ---
1807 // | LR |
1808 // | ... | callee saves
1809 // | R3 | arg3
1810 // | R2 | arg2
1811 // | R1 | arg1
1812 // | R0 |
1813 // | Method* | <- sp
1814 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1815 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
1816 uintptr_t caller_pc = regs[10];
1817#elif defined(__i386__)
1818 // On entry the stack pointed by sp is:
1819 // | argN | |
1820 // | ... | |
1821 // | arg4 | |
1822 // | arg3 spill | | Caller's frame
1823 // | arg2 spill | |
1824 // | arg1 spill | |
1825 // | Method* | ---
1826 // | Return |
1827 // | EBP,ESI,EDI | callee saves
1828 // | EBX | arg3
1829 // | EDX | arg2
1830 // | ECX | arg1
1831 // | EAX/Method* | <- sp
1832 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1833 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1834 uintptr_t caller_pc = regs[7];
1835#elif defined(__mips__)
1836 // On entry the stack pointed by sp is:
1837 // | argN | |
1838 // | ... | |
1839 // | arg4 | |
1840 // | arg3 spill | | Caller's frame
1841 // | arg2 spill | |
1842 // | arg1 spill | |
1843 // | Method* | ---
1844 // | RA |
1845 // | ... | callee saves
1846 // | A3 | arg3
1847 // | A2 | arg2
1848 // | A1 | arg1
1849 // | A0/Method* | <- sp
1850 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1851 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1852 uintptr_t caller_pc = regs[15];
1853#else
1854 UNIMPLEMENTED(FATAL);
1855 uintptr_t caller_pc = 0;
1856#endif
1857 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
1858 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
1859 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1860 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1861 Instruction::Code instr_code = instr->Opcode();
1862 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1863 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1864 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1865 uint32_t dex_method_idx;
1866 if (instr_code == Instruction::INVOKE_INTERFACE) {
1867 dex_method_idx = instr->VRegB_35c();
1868 } else {
1869 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1870 dex_method_idx = instr->VRegB_3rc();
1871 }
1872
1873 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1874 uint32_t shorty_len;
1875 const char* shorty =
1876 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
1877 {
1878 // Remember the args in case a GC happens in FindMethodFromCode.
1879 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1880 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1881 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001882 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001883 self);
1884 visitor.FixupReferences();
1885 }
1886
1887 if (UNLIKELY(method == nullptr)) {
1888 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001889 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001890 }
1891 }
1892 const void* code = method->GetEntryPointFromQuickCompiledCode();
1893
1894 // When we return, the caller will branch to this address, so it had better not be 0!
1895 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1896 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001897
1898 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001899}
1900
Ian Rogers848871b2013-08-05 10:56:33 -07001901} // namespace art