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 #ifndef NATIVE_BRIDGE_SUPPORT_ARM64_GUEST_STATE_GUEST_STATE_CPU_STATE_H_
18 #define NATIVE_BRIDGE_SUPPORT_ARM64_GUEST_STATE_GUEST_STATE_CPU_STATE_H_
19 
20 #include <cstdint>
21 
22 namespace berberis {
23 
24 using GuestAddr = uintptr_t;
25 using Reservation = __uint128_t;
26 
27 struct CPUState {
28   // General registers.
29   uint64_t x[31];
30 
31   // Flags
32   // clang-format off
33 #if defined(__x86_64__)
34   enum FlagMask {
35     kFlagNegative = 1 << 15,
36     kFlagZero     = 1 << 14,
37     kFlagCarry    = 1 << 8,
38     kFlagOverflow = 1,
39   };
40 #else
41   enum FlagMask {
42     kFlagNegative = 1 << 3,
43     kFlagZero     = 1 << 2,
44     kFlagCarry    = 1 << 1,
45     kFlagOverflow = 1,
46   };
47 #endif
48 
49   static constexpr uint32_t kFpsrQcBit = 1U << 27;
50 
51   // clang-format on
52   uint16_t flags;
53 
54   // Caches last-written FPCR, to minimize reads of host register
55   uint32_t cached_fpcr;
56 
57   // Stores the FPSR flags whose functionality we emulate: currently only IDC. (later IXC)
58   uint32_t emulated_fpsr;
59 
60   // Stack pointer.
61   uint64_t sp;
62 
63   // SIMD & FP registers.
64   alignas(16) __uint128_t v[32];
65 
66   // Current insn address.
67   uint64_t insn_addr;
68 
69   GuestAddr reservation_address;
70   Reservation reservation_value;
71 };
72 
73 }
74 #endif  // NATIVE_BRIDGE_SUPPORT_BRIDGE_SUPPORT_ARM64_GUEST_STATE_GUEST_STATE_CPU_STATE_H_
75