blob: 9670e6ed282cd9678a03712ef6f02c1ba8ee0f73 [file] [log] [blame]
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +00001/*
2 * Copyright (C) 2019 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 "art_method-inl.h"
18#include "dex/code_item_accessors.h"
19#include "entrypoints/quick/callee_save_frame.h"
Nicolas Geoffray0315efa2020-06-26 11:42:39 +010020#include "interpreter/mterp/nterp.h"
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000021#include "nterp_helpers.h"
22#include "oat_quick_method_header.h"
23#include "quick/quick_method_frame_info.h"
24
25namespace art {
26
27/**
28 * An nterp frame follows the optimizing compiler's ABI conventions, with
29 * int/long/reference parameters being passed in core registers / stack and
30 * float/double parameters being passed in floating point registers / stack.
31 *
32 * There are no ManagedStack transitions between compiler and nterp frames.
33 *
34 * On entry, nterp will copy its parameters to a dex register array allocated on
35 * the stack. There is a fast path when calling from nterp to nterp to not
36 * follow the ABI but just copy the parameters from the caller's dex registers
37 * to the callee's dex registers.
38 *
39 * The stack layout of an nterp frame is:
40 * ----------------
41 * | | All callee save registers of the platform
42 * | callee-save | (core and floating point).
43 * | registers | On x86 and x64 this includes the return address,
44 * | | already spilled on entry.
45 * ----------------
Nicolas Geoffray63a57932020-01-10 14:12:26 +000046 * | alignment | Stack aligment of kStackAlignment.
47 * ----------------
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000048 * | | Contains `registers_size` entries (of size 4) from
49 * | dex | the code item information of the method.
50 * | registers |
51 * | |
52 * ----------------
53 * | | A copy of the dex registers above, but only
54 * | reference | containing references, used for GC.
55 * | registers |
56 * | |
57 * ----------------
58 * | caller fp | Frame pointer of caller. Stored below the reference
59 * ---------------- registers array for easy access from nterp when returning.
60 * | dex_pc_ptr | Pointer to the dex instruction being executed.
61 * ---------------- Stored whenever nterp goes into the runtime.
Vladimir Marko3f04ae52020-04-24 17:19:51 +010062 * | alignment | Pointer aligment for dex_pc_ptr and caller_fp.
63 * ----------------
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000064 * | | In case nterp calls compiled code, we reserve space
65 * | out | for out registers. This space will be used for
66 * | registers | arguments passed on stack.
67 * | |
68 * ----------------
69 * | ArtMethod* | The method being currently executed.
70 * ----------------
71 *
72 * Exception handling:
73 * Nterp follows the same convention than the compiler,
74 * with the addition of:
75 * - All catch handlers have the same landing pad.
76 * - Before doing the longjmp for exception delivery, the register containing the
77 * dex PC pointer must be updated.
78 *
79 * Stack walking:
80 * An nterp frame is walked like a compiled code frame. We add an
81 * OatQuickMethodHeader prefix to the nterp entry point, which contains:
82 * - vmap_table_offset=0 (nterp doesn't need one).
83 * - code_size=NterpEnd-NterpStart
84 */
85
86static constexpr size_t kPointerSize = static_cast<size_t>(kRuntimePointerSize);
87
Nicolas Geoffrayc39af942021-01-25 08:43:57 +000088static constexpr size_t NterpGetFrameEntrySize(InstructionSet isa) {
89 uint32_t core_spills = 0;
90 uint32_t fp_spills = 0;
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +000091 // Note: the return address is considered part of the callee saves.
Nicolas Geoffrayc39af942021-01-25 08:43:57 +000092 switch (isa) {
93 case InstructionSet::kX86:
94 core_spills = x86::X86CalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
95 fp_spills = x86::X86CalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
96 break;
97 case InstructionSet::kX86_64:
98 core_spills =
99 x86_64::X86_64CalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
100 fp_spills = x86_64::X86_64CalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
101 break;
102 case InstructionSet::kArm:
103 case InstructionSet::kThumb2:
104 core_spills = arm::ArmCalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
105 fp_spills = arm::ArmCalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
106 break;
107 case InstructionSet::kArm64:
108 core_spills = arm64::Arm64CalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
109 fp_spills = arm64::Arm64CalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
110 break;
111 default:
112 InstructionSetAbort(isa);
113 }
114 // Note: the return address is considered part of the callee saves.
115 return (POPCOUNT(core_spills) + POPCOUNT(fp_spills)) *
116 static_cast<size_t>(InstructionSetPointerSize(isa));
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000117}
118
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000119size_t NterpGetFrameSize(ArtMethod* method, InstructionSet isa) {
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000120 CodeItemDataAccessor accessor(method->DexInstructionData());
121 const uint16_t num_regs = accessor.RegistersSize();
122 const uint16_t out_regs = accessor.OutsSize();
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000123 size_t pointer_size = static_cast<size_t>(InstructionSetPointerSize(isa));
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000124
Vladimir Marko3f04ae52020-04-24 17:19:51 +0100125 // Note: There may be two pieces of alignment but there is no need to align
126 // out args to `kPointerSize` separately before aligning to kStackAlignment.
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000127 DCHECK(IsAlignedParam(kStackAlignment, pointer_size));
128 DCHECK(IsAlignedParam(NterpGetFrameEntrySize(isa), pointer_size));
129 DCHECK(IsAlignedParam(kVRegSize * 2, pointer_size));
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000130 size_t frame_size =
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000131 NterpGetFrameEntrySize(isa) +
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000132 (num_regs * kVRegSize) * 2 + // dex registers and reference registers
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000133 pointer_size + // previous frame
134 pointer_size + // saved dex pc
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000135 (out_regs * kVRegSize) + // out arguments
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000136 pointer_size; // method
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000137 return RoundUp(frame_size, kStackAlignment);
138}
139
140QuickMethodFrameInfo NterpFrameInfo(ArtMethod** frame) {
141 uint32_t core_spills =
142 RuntimeCalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
143 uint32_t fp_spills =
144 RuntimeCalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
145 return QuickMethodFrameInfo(NterpGetFrameSize(*frame), core_spills, fp_spills);
146}
147
148uintptr_t NterpGetRegistersArray(ArtMethod** frame) {
149 CodeItemDataAccessor accessor((*frame)->DexInstructionData());
150 const uint16_t num_regs = accessor.RegistersSize();
Nicolas Geoffray63a57932020-01-10 14:12:26 +0000151 // The registers array is just above the reference array.
152 return NterpGetReferenceArray(frame) + (num_regs * kVRegSize);
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000153}
154
155uintptr_t NterpGetReferenceArray(ArtMethod** frame) {
156 CodeItemDataAccessor accessor((*frame)->DexInstructionData());
Nicolas Geoffray63a57932020-01-10 14:12:26 +0000157 const uint16_t out_regs = accessor.OutsSize();
158 // The references array is just above the saved frame pointer.
159 return reinterpret_cast<uintptr_t>(frame) +
160 kPointerSize + // method
Vladimir Marko3f04ae52020-04-24 17:19:51 +0100161 RoundUp(out_regs * kVRegSize, kPointerSize) + // out arguments and pointer alignment
Nicolas Geoffray63a57932020-01-10 14:12:26 +0000162 kPointerSize + // saved dex pc
163 kPointerSize; // previous frame.
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000164}
165
166uint32_t NterpGetDexPC(ArtMethod** frame) {
Nicolas Geoffray63a57932020-01-10 14:12:26 +0000167 CodeItemDataAccessor accessor((*frame)->DexInstructionData());
168 const uint16_t out_regs = accessor.OutsSize();
169 uintptr_t dex_pc_ptr = reinterpret_cast<uintptr_t>(frame) +
170 kPointerSize + // method
Vladimir Marko3f04ae52020-04-24 17:19:51 +0100171 RoundUp(out_regs * kVRegSize, kPointerSize); // out arguments and pointer alignment
Nicolas Geoffray63a57932020-01-10 14:12:26 +0000172 CodeItemInstructionAccessor instructions((*frame)->DexInstructions());
173 return *reinterpret_cast<const uint16_t**>(dex_pc_ptr) - instructions.Insns();
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000174}
175
176uint32_t NterpGetVReg(ArtMethod** frame, uint16_t vreg) {
177 return reinterpret_cast<uint32_t*>(NterpGetRegistersArray(frame))[vreg];
178}
179
Nicolas Geoffrayd7651b12019-12-18 14:57:30 +0000180uint32_t NterpGetVRegReference(ArtMethod** frame, uint16_t vreg) {
181 return reinterpret_cast<uint32_t*>(NterpGetReferenceArray(frame))[vreg];
182}
183
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000184uintptr_t NterpGetCatchHandler() {
185 // Nterp uses the same landing pad for all exceptions. The dex_pc_ptr set before
186 // longjmp will actually be used to jmp to the catch handler.
187 return reinterpret_cast<uintptr_t>(artNterpAsmInstructionEnd);
188}
189
Nicolas Geoffrayc39af942021-01-25 08:43:57 +0000190bool CanMethodUseNterp(ArtMethod* method, InstructionSet isa) {
191 return !method->IsNative() &&
192 method->IsInvokable() &&
193 // Nterp supports the same methods the compiler supports.
194 method->IsCompilable() &&
195 !method->MustCountLocks() &&
196 // Proxy methods do not go through the JIT like other methods, so we don't
197 // run them with nterp.
198 !method->IsProxyMethod() &&
199 NterpGetFrameSize(method, isa) <= interpreter::kNterpMaxFrame;
200}
201
Nicolas Geoffray013d1ee2019-12-04 16:18:15 +0000202} // namespace art