• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "translator.h"
18 
19 #include "berberis/base/checks.h"
20 #include "berberis/guest_os_primitives/guest_map_shadow.h"
21 #include "berberis/guest_state/guest_addr.h"
22 #include "berberis/guest_state/guest_state_opaque.h"
23 #include "berberis/interpreter/riscv64/interpreter.h"
24 #include "berberis/runtime_primitives/host_code.h"
25 #include "berberis/runtime_primitives/runtime_library.h"
26 #include "berberis/runtime_primitives/translation_cache.h"
27 
28 namespace berberis {
29 
InitTranslatorArch()30 void InitTranslatorArch() {}
31 
TranslateRegion(GuestAddr pc)32 void TranslateRegion(GuestAddr pc) {
33   using Kind = GuestCodeEntry::Kind;
34 
35   TranslationCache* cache = TranslationCache::GetInstance();
36 
37   GuestCodeEntry* entry = cache->AddAndLockForTranslation(pc, 0);
38   if (!entry) {
39     return;
40   }
41 
42   GuestMapShadow* guest_map_shadow = GuestMapShadow::GetInstance();
43   auto [is_executable, insn_size] = IsPcExecutable(pc, guest_map_shadow);
44   if (!is_executable) {
45     cache->SetTranslatedAndUnlock(pc, entry, insn_size, Kind::kSpecialHandler, {kEntryNoExec, 0});
46     return;
47   }
48 
49   cache->SetTranslatedAndUnlock(pc, entry, insn_size, Kind::kInterpreted, {kEntryInterpret, 0});
50 }
51 
52 // ATTENTION: This symbol gets called directly, without PLT. To keep text
53 // sharable we should prevent preemption of this symbol, so do not export it!
54 // TODO(b/232598137): may be set default visibility to protected instead?
berberis_HandleNotTranslated(ThreadState * state)55 extern "C" __attribute__((used, __visibility__("hidden"))) void berberis_HandleNotTranslated(
56     ThreadState* state) {
57   TranslateRegion(state->cpu.insn_addr);
58 }
59 
berberis_HandleInterpret(ThreadState * state)60 extern "C" __attribute__((used, __visibility__("hidden"))) void berberis_HandleInterpret(
61     ThreadState* state) {
62   InterpretInsn(state);
63 }
64 
berberis_GetDispatchAddress(ThreadState * state)65 extern "C" __attribute__((used, __visibility__("hidden"))) const void* berberis_GetDispatchAddress(
66     ThreadState* state) {
67   CHECK(state);
68   if (ArePendingSignalsPresent(*state)) {
69     return AsHostCode(kEntryExitGeneratedCode);
70   }
71   return AsHostCode(TranslationCache::GetInstance()->GetHostCodePtr(state->cpu.insn_addr)->load());
72 }
73 
74 }  // namespace berberis
75