1 /*
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"
20 #include "interpreter/mterp/nterp.h"
21 #include "nterp_helpers.h"
22 #include "oat_quick_method_header.h"
23 #include "quick/quick_method_frame_info.h"
24 
25 namespace 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  *    ----------------
46  *    |  alignment   |      Stack aligment of kStackAlignment.
47  *    ----------------
48  *    |              |      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.
62  *    |  alignment   |      Pointer aligment for dex_pc_ptr and caller_fp.
63  *    ----------------
64  *    |              |      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 
86 static constexpr size_t kPointerSize = static_cast<size_t>(kRuntimePointerSize);
87 
NterpGetFrameEntrySize()88 static constexpr size_t NterpGetFrameEntrySize() {
89   uint32_t core_spills =
90       RuntimeCalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
91   uint32_t fp_spills =
92       RuntimeCalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
93   // Note: the return address is considered part of the callee saves.
94   return (POPCOUNT(core_spills) + POPCOUNT(fp_spills)) * kPointerSize;
95 }
96 
NterpGetFrameSize(ArtMethod * method)97 size_t NterpGetFrameSize(ArtMethod* method) {
98   CodeItemDataAccessor accessor(method->DexInstructionData());
99   const uint16_t num_regs = accessor.RegistersSize();
100   const uint16_t out_regs = accessor.OutsSize();
101 
102   // Note: There may be two pieces of alignment but there is no need to align
103   // out args to `kPointerSize` separately before aligning to kStackAlignment.
104   static_assert(IsAligned<kPointerSize>(kStackAlignment));
105   static_assert(IsAligned<kPointerSize>(NterpGetFrameEntrySize()));
106   static_assert(IsAligned<kPointerSize>(kVRegSize * 2));
107   size_t frame_size =
108       NterpGetFrameEntrySize() +
109       (num_regs * kVRegSize) * 2 +  // dex registers and reference registers
110       kPointerSize +  // previous frame
111       kPointerSize +  // saved dex pc
112       (out_regs * kVRegSize) +  // out arguments
113       kPointerSize;  // method
114   return RoundUp(frame_size, kStackAlignment);
115 }
116 
NterpFrameInfo(ArtMethod ** frame)117 QuickMethodFrameInfo NterpFrameInfo(ArtMethod** frame) {
118   uint32_t core_spills =
119       RuntimeCalleeSaveFrame::GetCoreSpills(CalleeSaveType::kSaveAllCalleeSaves);
120   uint32_t fp_spills =
121       RuntimeCalleeSaveFrame::GetFpSpills(CalleeSaveType::kSaveAllCalleeSaves);
122   return QuickMethodFrameInfo(NterpGetFrameSize(*frame), core_spills, fp_spills);
123 }
124 
NterpGetRegistersArray(ArtMethod ** frame)125 uintptr_t NterpGetRegistersArray(ArtMethod** frame) {
126   CodeItemDataAccessor accessor((*frame)->DexInstructionData());
127   const uint16_t num_regs = accessor.RegistersSize();
128   // The registers array is just above the reference array.
129   return NterpGetReferenceArray(frame) + (num_regs * kVRegSize);
130 }
131 
NterpGetReferenceArray(ArtMethod ** frame)132 uintptr_t NterpGetReferenceArray(ArtMethod** frame) {
133   CodeItemDataAccessor accessor((*frame)->DexInstructionData());
134   const uint16_t out_regs = accessor.OutsSize();
135   // The references array is just above the saved frame pointer.
136   return reinterpret_cast<uintptr_t>(frame) +
137       kPointerSize +  // method
138       RoundUp(out_regs * kVRegSize, kPointerSize) +  // out arguments and pointer alignment
139       kPointerSize +  // saved dex pc
140       kPointerSize;  // previous frame.
141 }
142 
NterpGetDexPC(ArtMethod ** frame)143 uint32_t NterpGetDexPC(ArtMethod** frame) {
144   CodeItemDataAccessor accessor((*frame)->DexInstructionData());
145   const uint16_t out_regs = accessor.OutsSize();
146   uintptr_t dex_pc_ptr = reinterpret_cast<uintptr_t>(frame) +
147       kPointerSize +  // method
148       RoundUp(out_regs * kVRegSize, kPointerSize);  // out arguments and pointer alignment
149   CodeItemInstructionAccessor instructions((*frame)->DexInstructions());
150   return *reinterpret_cast<const uint16_t**>(dex_pc_ptr) - instructions.Insns();
151 }
152 
NterpGetVReg(ArtMethod ** frame,uint16_t vreg)153 uint32_t NterpGetVReg(ArtMethod** frame, uint16_t vreg) {
154   return reinterpret_cast<uint32_t*>(NterpGetRegistersArray(frame))[vreg];
155 }
156 
NterpGetVRegReference(ArtMethod ** frame,uint16_t vreg)157 uint32_t NterpGetVRegReference(ArtMethod** frame, uint16_t vreg) {
158   return reinterpret_cast<uint32_t*>(NterpGetReferenceArray(frame))[vreg];
159 }
160 
NterpGetCatchHandler()161 uintptr_t NterpGetCatchHandler() {
162   // Nterp uses the same landing pad for all exceptions. The dex_pc_ptr set before
163   // longjmp will actually be used to jmp to the catch handler.
164   return reinterpret_cast<uintptr_t>(artNterpAsmInstructionEnd);
165 }
166 
167 }  // namespace art
168