1 /*
2  * Copyright (C) 2018 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 BERBERIS_GUEST_STATE_GUEST_STATE_ARCH_H_
18 #define BERBERIS_GUEST_STATE_GUEST_STATE_ARCH_H_
19 
20 #include <array>
21 #include <atomic>
22 
23 #include "berberis/guest_state/guest_addr.h"
24 #include "berberis/guest_state/guest_state_opaque.h"
25 #include "native_bridge_support/arm64/guest_state/guest_state_cpu_state.h"
26 
27 namespace berberis {
28 
29 // Guest CPU state + interface to access guest memory.
30 struct ThreadState {
31   CPUState cpu;
32 
33   // Guest thread pointer.
34   GuestThread* thread;
35 
36   // Guest TLS pointer.
37   // It can be read using MRC instruction.
38   // Statically linked ARM executable initializes it by set_tls syscall.
39   // For PIC objects, InitThreadState sets it either to host TLS or to (stub) thread-id.
40   // TODO(b/36890513): guest should have its own TLS area for PIC objects too.
41   GuestAddr tls;
42 
43   // Keep pending signals status here for fast checking in generated code.
44   // TODO(b/28058920): move to GuestThread!
45   std::atomic_uint_least8_t pending_signals_status;
46 
47   GuestThreadResidence residence;
48 
49   // Arbitrary per-thread data added by instrumentation.
50   void* instrument_data;
51 
52   // Point to the guest thread memory start position.
53   void* thread_state_storage;
54 };
55 
56 inline constexpr unsigned kNumGuestRegs = std::size(CPUState{}.x);
57 inline constexpr unsigned kNumGuestSimdRegs = std::size(CPUState{}.v);
58 
59 inline constexpr unsigned kGuestCacheLineSize = 64;
60 }  // namespace berberis
61 
62 #endif  // BERBERIS_GUEST_STATE_GUEST_STATE_ARCH_H_
63