1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_ASM_SUPPORT_H_
18 #define ART_RUNTIME_ASM_SUPPORT_H_
19 
20 #if defined(__cplusplus)
21 #include "art_method.h"
22 #include "base/bit_utils.h"
23 #include "gc/allocator/rosalloc.h"
24 #include "gc/heap.h"
25 #include "jit/jit.h"
26 #include "lock_word.h"
27 #include "mirror/class.h"
28 #include "mirror/dex_cache.h"
29 #include "mirror/string.h"
30 #include "utils/dex_cache_arrays_layout.h"
31 #include "runtime.h"
32 #include "thread.h"
33 #endif
34 
35 #include "read_barrier_c.h"
36 
37 #if defined(__arm__) || defined(__mips__)
38 // In quick code for ARM and MIPS we make poor use of registers and perform frequent suspend
39 // checks in the event of loop back edges. The SUSPEND_CHECK_INTERVAL constant is loaded into a
40 // register at the point of an up-call or after handling a suspend check. It reduces the number of
41 // loads of the TLS suspend check value by the given amount (turning it into a decrement and compare
42 // of a register). This increases the time for a thread to respond to requests from GC and the
43 // debugger, damaging GC performance and creating other unwanted artifacts. For example, this count
44 // has the effect of making loops and Java code look cold in profilers, where the count is reset
45 // impacts where samples will occur. Reducing the count as much as possible improves profiler
46 // accuracy in tools like traceview.
47 // TODO: get a compiler that can do a proper job of loop optimization and remove this.
48 #define SUSPEND_CHECK_INTERVAL 96
49 #endif
50 
51 #if defined(__cplusplus)
52 
53 #ifndef ADD_TEST_EQ  // Allow #include-r to replace with their own.
54 #define ADD_TEST_EQ(x, y) CHECK_EQ(x, y);
55 #endif
56 
CheckAsmSupportOffsetsAndSizes()57 static inline void CheckAsmSupportOffsetsAndSizes() {
58 #else
59 #define ADD_TEST_EQ(x, y)
60 #endif
61 
62 #if defined(__LP64__)
63 #define POINTER_SIZE_SHIFT 3
64 #define POINTER_SIZE art::PointerSize::k64
65 #else
66 #define POINTER_SIZE_SHIFT 2
67 #define POINTER_SIZE art::PointerSize::k32
68 #endif
69 ADD_TEST_EQ(static_cast<size_t>(1U << POINTER_SIZE_SHIFT),
70             static_cast<size_t>(__SIZEOF_POINTER__))
71 
72 // Import platform-independent constant defines from our autogenerated list.
73 // Export new defines (for assembly use) by editing cpp-define-generator def files.
74 #define DEFINE_CHECK_EQ ADD_TEST_EQ
75 #include "asm_support_gen.h"
76 
77 // Offset of field Thread::tlsPtr_.exception.
78 #define THREAD_EXCEPTION_OFFSET (THREAD_CARD_TABLE_OFFSET + __SIZEOF_POINTER__)
79 ADD_TEST_EQ(THREAD_EXCEPTION_OFFSET,
80             art::Thread::ExceptionOffset<POINTER_SIZE>().Int32Value())
81 
82 // Offset of field Thread::tlsPtr_.managed_stack.top_quick_frame_.
83 #define THREAD_TOP_QUICK_FRAME_OFFSET (THREAD_CARD_TABLE_OFFSET + (3 * __SIZEOF_POINTER__))
84 ADD_TEST_EQ(THREAD_TOP_QUICK_FRAME_OFFSET,
85             art::Thread::TopOfManagedStackOffset<POINTER_SIZE>().Int32Value())
86 
87 // Offset of field Thread::tlsPtr_.self.
88 #define THREAD_SELF_OFFSET (THREAD_CARD_TABLE_OFFSET + (9 * __SIZEOF_POINTER__))
89 ADD_TEST_EQ(THREAD_SELF_OFFSET,
90             art::Thread::SelfOffset<POINTER_SIZE>().Int32Value())
91 
92 // Offset of field Thread::tlsPtr_.thread_local_pos.
93 #define THREAD_LOCAL_POS_OFFSET (THREAD_CARD_TABLE_OFFSET + 34 * __SIZEOF_POINTER__)
94 ADD_TEST_EQ(THREAD_LOCAL_POS_OFFSET,
95             art::Thread::ThreadLocalPosOffset<POINTER_SIZE>().Int32Value())
96 // Offset of field Thread::tlsPtr_.thread_local_end.
97 #define THREAD_LOCAL_END_OFFSET (THREAD_LOCAL_POS_OFFSET + __SIZEOF_POINTER__)
98 ADD_TEST_EQ(THREAD_LOCAL_END_OFFSET,
99             art::Thread::ThreadLocalEndOffset<POINTER_SIZE>().Int32Value())
100 // Offset of field Thread::tlsPtr_.thread_local_objects.
101 #define THREAD_LOCAL_OBJECTS_OFFSET (THREAD_LOCAL_END_OFFSET + 2 * __SIZEOF_POINTER__)
102 ADD_TEST_EQ(THREAD_LOCAL_OBJECTS_OFFSET,
103             art::Thread::ThreadLocalObjectsOffset<POINTER_SIZE>().Int32Value())
104 
105 // Offset of field Thread::tlsPtr_.mterp_current_ibase.
106 #define THREAD_CURRENT_IBASE_OFFSET \
107     (THREAD_LOCAL_OBJECTS_OFFSET + __SIZEOF_SIZE_T__ + (1 + 161) * __SIZEOF_POINTER__)
108 ADD_TEST_EQ(THREAD_CURRENT_IBASE_OFFSET,
109             art::Thread::MterpCurrentIBaseOffset<POINTER_SIZE>().Int32Value())
110 // Offset of field Thread::tlsPtr_.mterp_default_ibase.
111 #define THREAD_DEFAULT_IBASE_OFFSET (THREAD_CURRENT_IBASE_OFFSET + __SIZEOF_POINTER__)
112 ADD_TEST_EQ(THREAD_DEFAULT_IBASE_OFFSET,
113             art::Thread::MterpDefaultIBaseOffset<POINTER_SIZE>().Int32Value())
114 // Offset of field Thread::tlsPtr_.mterp_alt_ibase.
115 #define THREAD_ALT_IBASE_OFFSET (THREAD_DEFAULT_IBASE_OFFSET + __SIZEOF_POINTER__)
116 ADD_TEST_EQ(THREAD_ALT_IBASE_OFFSET,
117             art::Thread::MterpAltIBaseOffset<POINTER_SIZE>().Int32Value())
118 // Offset of field Thread::tlsPtr_.rosalloc_runs.
119 #define THREAD_ROSALLOC_RUNS_OFFSET (THREAD_ALT_IBASE_OFFSET + __SIZEOF_POINTER__)
120 ADD_TEST_EQ(THREAD_ROSALLOC_RUNS_OFFSET,
121             art::Thread::RosAllocRunsOffset<POINTER_SIZE>().Int32Value())
122 // Offset of field Thread::tlsPtr_.thread_local_alloc_stack_top.
123 #define THREAD_LOCAL_ALLOC_STACK_TOP_OFFSET (THREAD_ROSALLOC_RUNS_OFFSET + 16 * __SIZEOF_POINTER__)
124 ADD_TEST_EQ(THREAD_LOCAL_ALLOC_STACK_TOP_OFFSET,
125             art::Thread::ThreadLocalAllocStackTopOffset<POINTER_SIZE>().Int32Value())
126 // Offset of field Thread::tlsPtr_.thread_local_alloc_stack_end.
127 #define THREAD_LOCAL_ALLOC_STACK_END_OFFSET (THREAD_ROSALLOC_RUNS_OFFSET + 17 * __SIZEOF_POINTER__)
128 ADD_TEST_EQ(THREAD_LOCAL_ALLOC_STACK_END_OFFSET,
129             art::Thread::ThreadLocalAllocStackEndOffset<POINTER_SIZE>().Int32Value())
130 
131 // Offsets within ShadowFrame.
132 #define SHADOWFRAME_LINK_OFFSET 0
133 ADD_TEST_EQ(SHADOWFRAME_LINK_OFFSET,
134             static_cast<int32_t>(art::ShadowFrame::LinkOffset()))
135 #define SHADOWFRAME_METHOD_OFFSET (SHADOWFRAME_LINK_OFFSET + 1 * __SIZEOF_POINTER__)
136 ADD_TEST_EQ(SHADOWFRAME_METHOD_OFFSET,
137             static_cast<int32_t>(art::ShadowFrame::MethodOffset()))
138 #define SHADOWFRAME_RESULT_REGISTER_OFFSET (SHADOWFRAME_LINK_OFFSET + 2 * __SIZEOF_POINTER__)
139 ADD_TEST_EQ(SHADOWFRAME_RESULT_REGISTER_OFFSET,
140             static_cast<int32_t>(art::ShadowFrame::ResultRegisterOffset()))
141 #define SHADOWFRAME_DEX_PC_PTR_OFFSET (SHADOWFRAME_LINK_OFFSET + 3 * __SIZEOF_POINTER__)
142 ADD_TEST_EQ(SHADOWFRAME_DEX_PC_PTR_OFFSET,
143             static_cast<int32_t>(art::ShadowFrame::DexPCPtrOffset()))
144 #define SHADOWFRAME_CODE_ITEM_OFFSET (SHADOWFRAME_LINK_OFFSET + 4 * __SIZEOF_POINTER__)
145 ADD_TEST_EQ(SHADOWFRAME_CODE_ITEM_OFFSET,
146             static_cast<int32_t>(art::ShadowFrame::CodeItemOffset()))
147 #define SHADOWFRAME_LOCK_COUNT_DATA_OFFSET (SHADOWFRAME_LINK_OFFSET + 5 * __SIZEOF_POINTER__)
148 ADD_TEST_EQ(SHADOWFRAME_LOCK_COUNT_DATA_OFFSET,
149             static_cast<int32_t>(art::ShadowFrame::LockCountDataOffset()))
150 #define SHADOWFRAME_NUMBER_OF_VREGS_OFFSET (SHADOWFRAME_LINK_OFFSET + 6 * __SIZEOF_POINTER__)
151 ADD_TEST_EQ(SHADOWFRAME_NUMBER_OF_VREGS_OFFSET,
152             static_cast<int32_t>(art::ShadowFrame::NumberOfVRegsOffset()))
153 #define SHADOWFRAME_DEX_PC_OFFSET (SHADOWFRAME_NUMBER_OF_VREGS_OFFSET + 4)
154 ADD_TEST_EQ(SHADOWFRAME_DEX_PC_OFFSET,
155             static_cast<int32_t>(art::ShadowFrame::DexPCOffset()))
156 #define SHADOWFRAME_CACHED_HOTNESS_COUNTDOWN_OFFSET (SHADOWFRAME_NUMBER_OF_VREGS_OFFSET + 8)
157 ADD_TEST_EQ(SHADOWFRAME_CACHED_HOTNESS_COUNTDOWN_OFFSET,
158             static_cast<int32_t>(art::ShadowFrame::CachedHotnessCountdownOffset()))
159 #define SHADOWFRAME_HOTNESS_COUNTDOWN_OFFSET (SHADOWFRAME_NUMBER_OF_VREGS_OFFSET + 10)
160 ADD_TEST_EQ(SHADOWFRAME_HOTNESS_COUNTDOWN_OFFSET,
161             static_cast<int32_t>(art::ShadowFrame::HotnessCountdownOffset()))
162 #define SHADOWFRAME_VREGS_OFFSET (SHADOWFRAME_NUMBER_OF_VREGS_OFFSET + 12)
163 ADD_TEST_EQ(SHADOWFRAME_VREGS_OFFSET,
164             static_cast<int32_t>(art::ShadowFrame::VRegsOffset()))
165 
166 #if defined(USE_BROOKS_READ_BARRIER)
167 #define MIRROR_OBJECT_HEADER_SIZE 16
168 #else
169 #define MIRROR_OBJECT_HEADER_SIZE 8
170 #endif
171 ADD_TEST_EQ(size_t(MIRROR_OBJECT_HEADER_SIZE), sizeof(art::mirror::Object))
172 
173 // Offsets within java.lang.Class.
174 #define MIRROR_CLASS_COMPONENT_TYPE_OFFSET (4 + MIRROR_OBJECT_HEADER_SIZE)
175 ADD_TEST_EQ(MIRROR_CLASS_COMPONENT_TYPE_OFFSET,
176             art::mirror::Class::ComponentTypeOffset().Int32Value())
177 #define MIRROR_CLASS_IF_TABLE_OFFSET (16 + MIRROR_OBJECT_HEADER_SIZE)
178 ADD_TEST_EQ(MIRROR_CLASS_IF_TABLE_OFFSET,
179             art::mirror::Class::IfTableOffset().Int32Value())
180 #define MIRROR_CLASS_ACCESS_FLAGS_OFFSET (56 + MIRROR_OBJECT_HEADER_SIZE)
181 ADD_TEST_EQ(MIRROR_CLASS_ACCESS_FLAGS_OFFSET,
182             art::mirror::Class::AccessFlagsOffset().Int32Value())
183 #define MIRROR_CLASS_OBJECT_SIZE_OFFSET (88 + MIRROR_OBJECT_HEADER_SIZE)
184 ADD_TEST_EQ(MIRROR_CLASS_OBJECT_SIZE_OFFSET,
185             art::mirror::Class::ObjectSizeOffset().Int32Value())
186 #define MIRROR_CLASS_OBJECT_SIZE_ALLOC_FAST_PATH_OFFSET (92 + MIRROR_OBJECT_HEADER_SIZE)
187 ADD_TEST_EQ(MIRROR_CLASS_OBJECT_SIZE_ALLOC_FAST_PATH_OFFSET,
188             art::mirror::Class::ObjectSizeAllocFastPathOffset().Int32Value())
189 #define MIRROR_CLASS_OBJECT_PRIMITIVE_TYPE_OFFSET (96 + MIRROR_OBJECT_HEADER_SIZE)
190 ADD_TEST_EQ(MIRROR_CLASS_OBJECT_PRIMITIVE_TYPE_OFFSET,
191             art::mirror::Class::PrimitiveTypeOffset().Int32Value())
192 #define MIRROR_CLASS_STATUS_OFFSET (104 + MIRROR_OBJECT_HEADER_SIZE)
193 ADD_TEST_EQ(MIRROR_CLASS_STATUS_OFFSET,
194             art::mirror::Class::StatusOffset().Int32Value())
195 
196 #define PRIMITIVE_TYPE_SIZE_SHIFT_SHIFT 16
197 ADD_TEST_EQ(PRIMITIVE_TYPE_SIZE_SHIFT_SHIFT,
198             static_cast<int>(art::mirror::Class::kPrimitiveTypeSizeShiftShift))
199 
200 // Array offsets.
201 #define MIRROR_ARRAY_LENGTH_OFFSET      MIRROR_OBJECT_HEADER_SIZE
202 ADD_TEST_EQ(MIRROR_ARRAY_LENGTH_OFFSET, art::mirror::Array::LengthOffset().Int32Value())
203 
204 #define MIRROR_CHAR_ARRAY_DATA_OFFSET   (4 + MIRROR_OBJECT_HEADER_SIZE)
205 ADD_TEST_EQ(MIRROR_CHAR_ARRAY_DATA_OFFSET,
206             art::mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value())
207 
208 #define MIRROR_BOOLEAN_ARRAY_DATA_OFFSET MIRROR_CHAR_ARRAY_DATA_OFFSET
209 ADD_TEST_EQ(MIRROR_BOOLEAN_ARRAY_DATA_OFFSET,
210             art::mirror::Array::DataOffset(sizeof(uint8_t)).Int32Value())
211 
212 #define MIRROR_BYTE_ARRAY_DATA_OFFSET MIRROR_CHAR_ARRAY_DATA_OFFSET
213 ADD_TEST_EQ(MIRROR_BYTE_ARRAY_DATA_OFFSET,
214             art::mirror::Array::DataOffset(sizeof(int8_t)).Int32Value())
215 
216 #define MIRROR_SHORT_ARRAY_DATA_OFFSET MIRROR_CHAR_ARRAY_DATA_OFFSET
217 ADD_TEST_EQ(MIRROR_SHORT_ARRAY_DATA_OFFSET,
218             art::mirror::Array::DataOffset(sizeof(int16_t)).Int32Value())
219 
220 #define MIRROR_INT_ARRAY_DATA_OFFSET MIRROR_CHAR_ARRAY_DATA_OFFSET
221 ADD_TEST_EQ(MIRROR_INT_ARRAY_DATA_OFFSET,
222             art::mirror::Array::DataOffset(sizeof(int32_t)).Int32Value())
223 
224 #define MIRROR_WIDE_ARRAY_DATA_OFFSET (8 + MIRROR_OBJECT_HEADER_SIZE)
225 ADD_TEST_EQ(MIRROR_WIDE_ARRAY_DATA_OFFSET,
226             art::mirror::Array::DataOffset(sizeof(uint64_t)).Int32Value())
227 
228 #define MIRROR_OBJECT_ARRAY_DATA_OFFSET (4 + MIRROR_OBJECT_HEADER_SIZE)
229 ADD_TEST_EQ(MIRROR_OBJECT_ARRAY_DATA_OFFSET,
230     art::mirror::Array::DataOffset(
231         sizeof(art::mirror::HeapReference<art::mirror::Object>)).Int32Value())
232 
233 #define MIRROR_OBJECT_ARRAY_COMPONENT_SIZE 4
234 ADD_TEST_EQ(static_cast<size_t>(MIRROR_OBJECT_ARRAY_COMPONENT_SIZE),
235             sizeof(art::mirror::HeapReference<art::mirror::Object>))
236 
237 #define MIRROR_LONG_ARRAY_DATA_OFFSET (8 + MIRROR_OBJECT_HEADER_SIZE)
238 ADD_TEST_EQ(MIRROR_LONG_ARRAY_DATA_OFFSET,
239             art::mirror::Array::DataOffset(sizeof(uint64_t)).Int32Value())
240 
241 // Offsets within java.lang.String.
242 #define MIRROR_STRING_COUNT_OFFSET  MIRROR_OBJECT_HEADER_SIZE
243 ADD_TEST_EQ(MIRROR_STRING_COUNT_OFFSET, art::mirror::String::CountOffset().Int32Value())
244 
245 #define MIRROR_STRING_VALUE_OFFSET (8 + MIRROR_OBJECT_HEADER_SIZE)
246 ADD_TEST_EQ(MIRROR_STRING_VALUE_OFFSET, art::mirror::String::ValueOffset().Int32Value())
247 
248 // String compression feature.
249 #define STRING_COMPRESSION_FEATURE 1
250 ADD_TEST_EQ(STRING_COMPRESSION_FEATURE, art::mirror::kUseStringCompression);
251 
252 #if defined(__cplusplus)
253 }  // End of CheckAsmSupportOffsets.
254 #endif
255 
256 #endif  // ART_RUNTIME_ASM_SUPPORT_H_
257