1 /*
2 * Copyright (C) 2023 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 "code_generation_data.h"
18 #include "gc_root.h"
19 #include "jit_patches_arm64.h"
20
21 namespace art HIDDEN {
22
23 namespace arm64 {
24
DeduplicateUint32Literal(uint32_t value)25 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateUint32Literal(
26 uint32_t value) {
27 return uint32_literals_.GetOrCreate(
28 value,
29 [this, value]() {
30 return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(value);
31 });
32 }
33
DeduplicateUint64Literal(uint64_t value)34 vixl::aarch64::Literal<uint64_t>* JitPatchesARM64::DeduplicateUint64Literal(
35 uint64_t value) {
36 return uint64_literals_.GetOrCreate(
37 value,
38 [this, value]() {
39 return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint64_t>(value);
40 });
41 }
42
PatchJitRootUse(uint8_t * code,const uint8_t * roots_data,vixl::aarch64::Literal<uint32_t> * literal,uint64_t index_in_table)43 static void PatchJitRootUse(uint8_t* code,
44 const uint8_t* roots_data,
45 vixl::aarch64::Literal<uint32_t>* literal,
46 uint64_t index_in_table) {
47 uint32_t literal_offset = literal->GetOffset();
48 uintptr_t address =
49 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
50 uint8_t* data = code + literal_offset;
51 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
52 }
53
EmitJitRootPatches(uint8_t * code,const uint8_t * roots_data,const CodeGenerationData & code_generation_data) const54 void JitPatchesARM64::EmitJitRootPatches(
55 uint8_t* code,
56 const uint8_t* roots_data,
57 const CodeGenerationData& code_generation_data) const {
58 for (const auto& entry : jit_string_patches_) {
59 const StringReference& string_reference = entry.first;
60 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
61 uint64_t index_in_table = code_generation_data.GetJitStringRootIndex(string_reference);
62 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
63 }
64 for (const auto& entry : jit_class_patches_) {
65 const TypeReference& type_reference = entry.first;
66 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
67 uint64_t index_in_table = code_generation_data.GetJitClassRootIndex(type_reference);
68 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
69 }
70 }
71
DeduplicateBootImageAddressLiteral(uint64_t address)72 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateBootImageAddressLiteral(
73 uint64_t address) {
74 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address));
75 }
76
DeduplicateJitStringLiteral(const DexFile & dex_file,dex::StringIndex string_index,Handle<mirror::String> handle,CodeGenerationData * code_generation_data)77 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitStringLiteral(
78 const DexFile& dex_file,
79 dex::StringIndex string_index,
80 Handle<mirror::String> handle,
81 CodeGenerationData* code_generation_data) {
82 code_generation_data->ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
83 return jit_string_patches_.GetOrCreate(
84 StringReference(&dex_file, string_index),
85 [this]() {
86 return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
87 });
88 }
89
DeduplicateJitClassLiteral(const DexFile & dex_file,dex::TypeIndex type_index,Handle<mirror::Class> handle,CodeGenerationData * code_generation_data)90 vixl::aarch64::Literal<uint32_t>* JitPatchesARM64::DeduplicateJitClassLiteral(
91 const DexFile& dex_file,
92 dex::TypeIndex type_index,
93 Handle<mirror::Class> handle,
94 CodeGenerationData* code_generation_data) {
95 code_generation_data->ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
96 return jit_class_patches_.GetOrCreate(
97 TypeReference(&dex_file, type_index),
98 [this]() {
99 return GetVIXLAssembler()->CreateLiteralDestroyedWithPool<uint32_t>(/* value= */ 0u);
100 });
101 }
102
103 } // namespace arm64
104 } // namespace art
105