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 "lock_word.h"
23 #include "mirror/class.h"
24 #include "mirror/string.h"
25 #include "runtime.h"
26 #include "thread.h"
27 #endif
28
29 #include "read_barrier_c.h"
30
31 #if defined(__arm__) || defined(__mips__)
32 // In quick code for ARM and MIPS we make poor use of registers and perform frequent suspend
33 // checks in the event of loop back edges. The SUSPEND_CHECK_INTERVAL constant is loaded into a
34 // register at the point of an up-call or after handling a suspend check. It reduces the number of
35 // loads of the TLS suspend check value by the given amount (turning it into a decrement and compare
36 // of a register). This increases the time for a thread to respond to requests from GC and the
37 // debugger, damaging GC performance and creating other unwanted artifacts. For example, this count
38 // has the effect of making loops and Java code look cold in profilers, where the count is reset
39 // impacts where samples will occur. Reducing the count as much as possible improves profiler
40 // accuracy in tools like traceview.
41 // TODO: get a compiler that can do a proper job of loop optimization and remove this.
42 #define SUSPEND_CHECK_INTERVAL 96
43 #endif
44
45 #if defined(__cplusplus)
46
47 #ifndef ADD_TEST_EQ // Allow #include-r to replace with their own.
48 #define ADD_TEST_EQ(x, y) CHECK_EQ(x, y);
49 #endif
50
CheckAsmSupportOffsetsAndSizes()51 static inline void CheckAsmSupportOffsetsAndSizes() {
52 #else
53 #define ADD_TEST_EQ(x, y)
54 #endif
55
56 // Size of references to the heap on the stack.
57 #define STACK_REFERENCE_SIZE 4
58 ADD_TEST_EQ(static_cast<size_t>(STACK_REFERENCE_SIZE), sizeof(art::StackReference<art::mirror::Object>))
59
60 // Size of heap references
61 #define COMPRESSED_REFERENCE_SIZE 4
62 ADD_TEST_EQ(static_cast<size_t>(COMPRESSED_REFERENCE_SIZE),
63 sizeof(art::mirror::CompressedReference<art::mirror::Object>))
64
65 // Note: these callee save methods loads require read barriers.
66 // Offset of field Runtime::callee_save_methods_[kSaveAll]
67 #define RUNTIME_SAVE_ALL_CALLEE_SAVE_FRAME_OFFSET 0
68 ADD_TEST_EQ(static_cast<size_t>(RUNTIME_SAVE_ALL_CALLEE_SAVE_FRAME_OFFSET),
69 art::Runtime::GetCalleeSaveMethodOffset(art::Runtime::kSaveAll))
70
71 // Offset of field Runtime::callee_save_methods_[kRefsOnly]
72 #define RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET 8
73 ADD_TEST_EQ(static_cast<size_t>(RUNTIME_REFS_ONLY_CALLEE_SAVE_FRAME_OFFSET),
74 art::Runtime::GetCalleeSaveMethodOffset(art::Runtime::kRefsOnly))
75
76 // Offset of field Runtime::callee_save_methods_[kRefsAndArgs]
77 #define RUNTIME_REFS_AND_ARGS_CALLEE_SAVE_FRAME_OFFSET (2 * 8)
78 ADD_TEST_EQ(static_cast<size_t>(RUNTIME_REFS_AND_ARGS_CALLEE_SAVE_FRAME_OFFSET),
79 art::Runtime::GetCalleeSaveMethodOffset(art::Runtime::kRefsAndArgs))
80
81 // Offset of field Thread::tls32_.state_and_flags.
82 #define THREAD_FLAGS_OFFSET 0
83 ADD_TEST_EQ(THREAD_FLAGS_OFFSET,
84 art::Thread::ThreadFlagsOffset<__SIZEOF_POINTER__>().Int32Value())
85
86 // Offset of field Thread::tls32_.thin_lock_thread_id.
87 #define THREAD_ID_OFFSET 12
88 ADD_TEST_EQ(THREAD_ID_OFFSET,
89 art::Thread::ThinLockIdOffset<__SIZEOF_POINTER__>().Int32Value())
90
91 // Offset of field Thread::tlsPtr_.card_table.
92 #define THREAD_CARD_TABLE_OFFSET 128
93 ADD_TEST_EQ(THREAD_CARD_TABLE_OFFSET,
94 art::Thread::CardTableOffset<__SIZEOF_POINTER__>().Int32Value())
95
96 // Offset of field Thread::tlsPtr_.exception.
97 #define THREAD_EXCEPTION_OFFSET (THREAD_CARD_TABLE_OFFSET + __SIZEOF_POINTER__)
98 ADD_TEST_EQ(THREAD_EXCEPTION_OFFSET,
99 art::Thread::ExceptionOffset<__SIZEOF_POINTER__>().Int32Value())
100
101 // Offset of field Thread::tlsPtr_.managed_stack.top_quick_frame_.
102 #define THREAD_TOP_QUICK_FRAME_OFFSET (THREAD_CARD_TABLE_OFFSET + (3 * __SIZEOF_POINTER__))
103 ADD_TEST_EQ(THREAD_TOP_QUICK_FRAME_OFFSET,
104 art::Thread::TopOfManagedStackOffset<__SIZEOF_POINTER__>().Int32Value())
105
106 // Offset of field Thread::tlsPtr_.managed_stack.top_quick_frame_.
107 #define THREAD_SELF_OFFSET (THREAD_CARD_TABLE_OFFSET + (9 * __SIZEOF_POINTER__))
108 ADD_TEST_EQ(THREAD_SELF_OFFSET,
109 art::Thread::SelfOffset<__SIZEOF_POINTER__>().Int32Value())
110
111 #define THREAD_LOCAL_POS_OFFSET (THREAD_CARD_TABLE_OFFSET + 147 * __SIZEOF_POINTER__)
112 ADD_TEST_EQ(THREAD_LOCAL_POS_OFFSET,
113 art::Thread::ThreadLocalPosOffset<__SIZEOF_POINTER__>().Int32Value())
114 #define THREAD_LOCAL_END_OFFSET (THREAD_LOCAL_POS_OFFSET + __SIZEOF_POINTER__)
115 ADD_TEST_EQ(THREAD_LOCAL_END_OFFSET,
116 art::Thread::ThreadLocalEndOffset<__SIZEOF_POINTER__>().Int32Value())
117 #define THREAD_LOCAL_OBJECTS_OFFSET (THREAD_LOCAL_POS_OFFSET + 2 * __SIZEOF_POINTER__)
118 ADD_TEST_EQ(THREAD_LOCAL_OBJECTS_OFFSET,
119 art::Thread::ThreadLocalObjectsOffset<__SIZEOF_POINTER__>().Int32Value())
120
121 // Offsets within java.lang.Object.
122 #define MIRROR_OBJECT_CLASS_OFFSET 0
123 ADD_TEST_EQ(MIRROR_OBJECT_CLASS_OFFSET, art::mirror::Object::ClassOffset().Int32Value())
124 #define MIRROR_OBJECT_LOCK_WORD_OFFSET 4
125 ADD_TEST_EQ(MIRROR_OBJECT_LOCK_WORD_OFFSET, art::mirror::Object::MonitorOffset().Int32Value())
126
127 #if defined(USE_BROOKS_READ_BARRIER)
128 #define MIRROR_OBJECT_HEADER_SIZE 16
129 #else
130 #define MIRROR_OBJECT_HEADER_SIZE 8
131 #endif
132 ADD_TEST_EQ(size_t(MIRROR_OBJECT_HEADER_SIZE), sizeof(art::mirror::Object))
133
134 // Offsets within java.lang.Class.
135 #define MIRROR_CLASS_COMPONENT_TYPE_OFFSET (4 + MIRROR_OBJECT_HEADER_SIZE)
136 ADD_TEST_EQ(MIRROR_CLASS_COMPONENT_TYPE_OFFSET,
137 art::mirror::Class::ComponentTypeOffset().Int32Value())
138 #define MIRROR_CLASS_ACCESS_FLAGS_OFFSET (36 + MIRROR_OBJECT_HEADER_SIZE)
139 ADD_TEST_EQ(MIRROR_CLASS_ACCESS_FLAGS_OFFSET,
140 art::mirror::Class::AccessFlagsOffset().Int32Value())
141 #define MIRROR_CLASS_OBJECT_SIZE_OFFSET (112 + MIRROR_OBJECT_HEADER_SIZE)
142 ADD_TEST_EQ(MIRROR_CLASS_OBJECT_SIZE_OFFSET,
143 art::mirror::Class::ObjectSizeOffset().Int32Value())
144 #define MIRROR_CLASS_STATUS_OFFSET (124 + MIRROR_OBJECT_HEADER_SIZE)
145 ADD_TEST_EQ(MIRROR_CLASS_STATUS_OFFSET,
146 art::mirror::Class::StatusOffset().Int32Value())
147
148 #define MIRROR_CLASS_STATUS_INITIALIZED 10
149 ADD_TEST_EQ(static_cast<uint32_t>(MIRROR_CLASS_STATUS_INITIALIZED),
150 static_cast<uint32_t>(art::mirror::Class::kStatusInitialized))
151 #define ACCESS_FLAGS_CLASS_IS_FINALIZABLE 0x80000000
152 ADD_TEST_EQ(static_cast<uint32_t>(ACCESS_FLAGS_CLASS_IS_FINALIZABLE),
153 static_cast<uint32_t>(kAccClassIsFinalizable))
154
155 // Array offsets.
156 #define MIRROR_ARRAY_LENGTH_OFFSET MIRROR_OBJECT_HEADER_SIZE
157 ADD_TEST_EQ(MIRROR_ARRAY_LENGTH_OFFSET, art::mirror::Array::LengthOffset().Int32Value())
158
159 #define MIRROR_CHAR_ARRAY_DATA_OFFSET (4 + MIRROR_OBJECT_HEADER_SIZE)
160 ADD_TEST_EQ(MIRROR_CHAR_ARRAY_DATA_OFFSET,
161 art::mirror::Array::DataOffset(sizeof(uint16_t)).Int32Value())
162
163 #define MIRROR_OBJECT_ARRAY_DATA_OFFSET (4 + MIRROR_OBJECT_HEADER_SIZE)
164 ADD_TEST_EQ(MIRROR_OBJECT_ARRAY_DATA_OFFSET,
165 art::mirror::Array::DataOffset(
166 sizeof(art::mirror::HeapReference<art::mirror::Object>)).Int32Value())
167
168 #define MIRROR_OBJECT_ARRAY_COMPONENT_SIZE 4
169 ADD_TEST_EQ(static_cast<size_t>(MIRROR_OBJECT_ARRAY_COMPONENT_SIZE),
170 sizeof(art::mirror::HeapReference<art::mirror::Object>))
171
172 #define MIRROR_LONG_ARRAY_DATA_OFFSET (8 + MIRROR_OBJECT_HEADER_SIZE)
173 ADD_TEST_EQ(MIRROR_LONG_ARRAY_DATA_OFFSET,
174 art::mirror::Array::DataOffset(sizeof(uint64_t)).Int32Value())
175
176 // Offsets within java.lang.String.
177 #define MIRROR_STRING_COUNT_OFFSET MIRROR_OBJECT_HEADER_SIZE
178 ADD_TEST_EQ(MIRROR_STRING_COUNT_OFFSET, art::mirror::String::CountOffset().Int32Value())
179
180 #define MIRROR_STRING_VALUE_OFFSET (8 + MIRROR_OBJECT_HEADER_SIZE)
181 ADD_TEST_EQ(MIRROR_STRING_VALUE_OFFSET, art::mirror::String::ValueOffset().Int32Value())
182
183 // Offsets within java.lang.reflect.ArtMethod.
184 #define ART_METHOD_DEX_CACHE_METHODS_OFFSET 4
185 ADD_TEST_EQ(ART_METHOD_DEX_CACHE_METHODS_OFFSET,
186 art::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value())
187
188 #define ART_METHOD_DEX_CACHE_TYPES_OFFSET 8
189 ADD_TEST_EQ(ART_METHOD_DEX_CACHE_TYPES_OFFSET,
190 art::ArtMethod::DexCacheResolvedTypesOffset().Int32Value())
191
192 #define ART_METHOD_QUICK_CODE_OFFSET_32 36
193 ADD_TEST_EQ(ART_METHOD_QUICK_CODE_OFFSET_32,
194 art::ArtMethod::EntryPointFromQuickCompiledCodeOffset(4).Int32Value())
195
196 #define ART_METHOD_QUICK_CODE_OFFSET_64 48
197 ADD_TEST_EQ(ART_METHOD_QUICK_CODE_OFFSET_64,
198 art::ArtMethod::EntryPointFromQuickCompiledCodeOffset(8).Int32Value())
199
200 #define LOCK_WORD_STATE_SHIFT 30
201 ADD_TEST_EQ(LOCK_WORD_STATE_SHIFT, static_cast<int32_t>(art::LockWord::kStateShift))
202
203 #define LOCK_WORD_STATE_MASK 0xC0000000
204 ADD_TEST_EQ(LOCK_WORD_STATE_MASK, static_cast<uint32_t>(art::LockWord::kStateMaskShifted))
205
206 #define LOCK_WORD_READ_BARRIER_STATE_SHIFT 28
207 ADD_TEST_EQ(LOCK_WORD_READ_BARRIER_STATE_SHIFT,
208 static_cast<int32_t>(art::LockWord::kReadBarrierStateShift))
209
210 #define LOCK_WORD_READ_BARRIER_STATE_MASK 0x30000000
211 ADD_TEST_EQ(LOCK_WORD_READ_BARRIER_STATE_MASK,
212 static_cast<int32_t>(art::LockWord::kReadBarrierStateMaskShifted))
213
214 #define LOCK_WORD_READ_BARRIER_STATE_MASK_TOGGLED 0xCFFFFFFF
215 ADD_TEST_EQ(LOCK_WORD_READ_BARRIER_STATE_MASK_TOGGLED,
216 static_cast<uint32_t>(art::LockWord::kReadBarrierStateMaskShiftedToggled))
217
218 #define LOCK_WORD_THIN_LOCK_COUNT_ONE 65536
219 ADD_TEST_EQ(LOCK_WORD_THIN_LOCK_COUNT_ONE, static_cast<int32_t>(art::LockWord::kThinLockCountOne))
220
221 #define OBJECT_ALIGNMENT_MASK 7
222 ADD_TEST_EQ(static_cast<size_t>(OBJECT_ALIGNMENT_MASK), art::kObjectAlignment - 1)
223
224 #define OBJECT_ALIGNMENT_MASK_TOGGLED 0xFFFFFFF8
225 ADD_TEST_EQ(static_cast<uint32_t>(OBJECT_ALIGNMENT_MASK_TOGGLED),
226 ~static_cast<uint32_t>(art::kObjectAlignment - 1))
227
228 #if defined(__cplusplus)
229 } // End of CheckAsmSupportOffsets.
230 #endif
231
232 #endif // ART_RUNTIME_ASM_SUPPORT_H_
233