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 "berberis/guest_state/guest_state_opaque.h"
18 
19 #include "berberis/base/checks.h"
20 #include "berberis/base/mmap.h"
21 #include "berberis/guest_state/guest_addr.h"
22 #include "berberis/guest_state/guest_state.h"
23 #include "native_bridge_support/guest_state_accessor/accessor.h"
24 
25 #include <atomic>   // std::memory_order_relaxed
26 #include <cstddef>  // size_t
27 #include <cstdint>  // uint_least8_t
28 #include <cstring>  // memset
29 #include <new>
30 
31 namespace berberis {
32 
33 namespace {
34 
35 #if defined(NATIVE_BRIDGE_GUEST_ARCH_ARM) && defined(__i386__)
36 
37 const uint32_t kGuestArch = NATIVE_BRIDGE_ARCH_ARM;
38 const uint32_t kHostArch = NATIVE_BRIDGE_ARCH_X86;
39 
40 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_ARM64) && defined(__x86_64__)
41 
42 const uint32_t kGuestArch = NATIVE_BRIDGE_ARCH_ARM64;
43 const uint32_t kHostArch = NATIVE_BRIDGE_ARCH_X86_64;
44 
45 #elif defined(NATIVE_BRIDGE_GUEST_ARCH_RISCV64) && defined(__x86_64__)
46 
47 const uint32_t kGuestArch = NATIVE_BRIDGE_ARCH_RISCV64;
48 const uint32_t kHostArch = NATIVE_BRIDGE_ARCH_X86_64;
49 
50 #else
51 
52 #error "Unknown guest/host arch combination"
53 
54 #endif
55 
56 const size_t kThreadStatePageAlignedSize = AlignUpPageSize(sizeof(ThreadState));
57 const size_t kThreadStateStoragePageAlignedSize = AlignUpPageSize(
58     AlignUp(sizeof(NativeBridgeGuestStateHeader), alignof(ThreadState)) + sizeof(ThreadState));
59 
InitThreadState(ThreadState * state)60 void InitThreadState(ThreadState* state) {
61   // This is needed to set all flag values to 0.
62   memset(&(state->cpu), 0, sizeof(CPUState));
63 
64   // ATTENTION: Set fields specific for current thread when actually attaching to host thread!
65   state->thread = nullptr;
66   SetTlsAddr(*state, 0);
67 
68   state->pending_signals_status.store(kPendingSignalsDisabled, std::memory_order_relaxed);
69   state->residence = kOutsideGeneratedCode;
70   state->instrument_data = nullptr;
71 }
72 
InitializeNativeBridgeGuestStateHeader(NativeBridgeGuestStateHeader * guest_state_header,const ThreadState * state)73 void InitializeNativeBridgeGuestStateHeader(NativeBridgeGuestStateHeader* guest_state_header,
74                                             const ThreadState* state) {
75   guest_state_header->signature = NATIVE_BRIDGE_GUEST_STATE_SIGNATURE;
76   guest_state_header->native_bridge_host_arch = kHostArch;
77   guest_state_header->native_bridge_guest_arch = kGuestArch;
78   guest_state_header->guest_state_data = state;
79   guest_state_header->guest_state_data_size = kThreadStatePageAlignedSize;
80 }
81 
82 }  // namespace
83 
CreateThreadState()84 ThreadState* CreateThreadState() {
85   void* storage = Mmap(kThreadStateStoragePageAlignedSize);
86   if (storage == MAP_FAILED) {
87     return nullptr;
88   }
89   ThreadState* state =
90       new (AlignUp(reinterpret_cast<uint8_t*>(storage) + sizeof(NativeBridgeGuestStateHeader),
91                    alignof(ThreadState))) ThreadState;
92   CHECK(state);
93   state->thread_state_storage = storage;
94 
95   InitThreadState(state);
96 
97   InitializeNativeBridgeGuestStateHeader(static_cast<NativeBridgeGuestStateHeader*>(storage),
98                                          state);
99 
100   return state;
101 };
102 
DestroyThreadState(ThreadState * state)103 void DestroyThreadState(ThreadState* state) {
104   CHECK(state);
105   MunmapOrDie(state->thread_state_storage, kThreadStateStoragePageAlignedSize);
106 }
107 
108 class GuestThread;
GetThreadStateStorage(ThreadState & state)109 void* GetThreadStateStorage(ThreadState& state) {
110   return state.thread_state_storage;
111 }
112 
SetGuestThread(ThreadState & state,GuestThread * thread)113 void SetGuestThread(ThreadState& state, GuestThread* thread) {
114   state.thread = thread;
115 }
116 
GetGuestThread(const ThreadState & state)117 GuestThread* GetGuestThread(const ThreadState& state) {
118   return state.thread;
119 }
120 
GetResidence(const ThreadState & state)121 GuestThreadResidence GetResidence(const ThreadState& state) {
122   return state.residence;
123 }
124 
SetResidence(ThreadState & state,GuestThreadResidence residence)125 void SetResidence(ThreadState& state, GuestThreadResidence residence) {
126   state.residence = residence;
127 }
128 
GetPendingSignalsStatusAtomic(ThreadState & state)129 std::atomic<uint_least8_t>& GetPendingSignalsStatusAtomic(ThreadState& state) {
130   return state.pending_signals_status;
131 }
132 
SetPendingSignalsStatusAtomic(ThreadState & state,PendingSignalsStatus status)133 void SetPendingSignalsStatusAtomic(ThreadState& state, PendingSignalsStatus status) {
134   state.pending_signals_status = status;
135 }
136 
ArePendingSignalsPresent(const ThreadState & state)137 bool ArePendingSignalsPresent(const ThreadState& state) {
138   return state.pending_signals_status.load(std::memory_order_relaxed) == kPendingSignalsPresent;
139 }
140 
GetCPUState(const ThreadState & state)141 const CPUState& GetCPUState(const ThreadState& state) {
142   return state.cpu;
143 }
144 
GetCPUState(ThreadState & state)145 CPUState& GetCPUState(ThreadState& state) {
146   return state.cpu;
147 }
148 
SetCPUState(ThreadState & state,const CPUState & cpu)149 void SetCPUState(ThreadState& state, const CPUState& cpu) {
150   state.cpu = cpu;
151 }
152 
SetInsnAddr(CPUState & cpu,GuestAddr addr)153 void SetInsnAddr(CPUState& cpu, GuestAddr addr) {
154   cpu.insn_addr = addr;
155 }
156 
GetInsnAddr(const CPUState & cpu)157 GuestAddr GetInsnAddr(const CPUState& cpu) {
158   return cpu.insn_addr;
159 }
160 
161 }  // namespace berberis
162