1 /* 2 * Copyright (C) 2024 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 "berberis/runtime_primitives/runtime_library.h" 18 19 #if defined(__x86_64__) 20 #include "berberis/assembler/machine_code.h" 21 #include "berberis/assembler/x86_64.h" 22 #include "berberis/runtime_primitives/code_pool.h" 23 #endif 24 25 namespace berberis { 26 27 HostCodeAddr kEntryInterpret; 28 HostCodeAddr kEntryExitGeneratedCode; 29 HostCodeAddr kEntryStop; 30 HostCodeAddr kEntryNoExec; 31 HostCodeAddr kEntryNotTranslated; 32 HostCodeAddr kEntryTranslating; 33 HostCodeAddr kEntryInvalidating; 34 HostCodeAddr kEntryWrapping; 35 36 namespace { 37 // This function installs a trampoline in the CodePool address space. 38 // This needed to ensure that all entries in the translation cache 39 // are always pointing to the memory allocated via CodePool. InstallEntryTrampoline(HostCode target_function_ptr)40HostCodeAddr InstallEntryTrampoline(HostCode target_function_ptr) { 41 #if defined(__x86_64__) 42 MachineCode mc; 43 x86_64::Assembler as(&mc); 44 as.Jmp(target_function_ptr); 45 as.Finalize(); 46 // TODO(b/232598137): maybe use ColdPool? 47 return GetDefaultCodePoolInstance()->Add(&mc); 48 #else 49 return AsHostCodeAddr(target_function_ptr); 50 #endif 51 } 52 } // namespace 53 InitHostEntries()54void InitHostEntries() { 55 kEntryInterpret = InstallEntryTrampoline(AsHostCode(berberis_entry_Interpret)); 56 kEntryExitGeneratedCode = InstallEntryTrampoline(AsHostCode(berberis_entry_ExitGeneratedCode)); 57 kEntryStop = InstallEntryTrampoline(AsHostCode(berberis_entry_Stop)); 58 kEntryNoExec = InstallEntryTrampoline(AsHostCode(berberis_entry_NoExec)); 59 kEntryNotTranslated = InstallEntryTrampoline(AsHostCode(berberis_entry_NotTranslated)); 60 kEntryTranslating = InstallEntryTrampoline(AsHostCode(berberis_entry_Translating)); 61 kEntryInvalidating = InstallEntryTrampoline(AsHostCode(berberis_entry_Invalidating)); 62 kEntryWrapping = InstallEntryTrampoline(AsHostCode(berberis_entry_Wrapping)); 63 } 64 65 } // namespace berberis 66