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.h"
18
19 #include "berberis/base/checks.h"
20 #include "berberis/guest_state/guest_addr.h"
21
22 namespace berberis {
23
SetReturnValueRegister(CPUState & cpu,GuestAddr val)24 void SetReturnValueRegister(CPUState& cpu, GuestAddr val) {
25 cpu.x[0] = val;
26 }
27
GetReturnValueRegister(const CPUState & cpu)28 GuestAddr GetReturnValueRegister(const CPUState& cpu) {
29 return cpu.x[0];
30 }
31
SetStackRegister(CPUState & cpu,GuestAddr val)32 void SetStackRegister(CPUState& cpu, GuestAddr val) {
33 cpu.sp = val;
34 }
35
GetStackRegister(const CPUState & cpu)36 GuestAddr GetStackRegister(const CPUState& cpu) {
37 return cpu.sp;
38 }
39
SetLinkRegister(CPUState & cpu,GuestAddr val)40 void SetLinkRegister(CPUState& cpu, GuestAddr val) {
41 cpu.x[30] = val;
42 }
43
GetLinkRegister(const CPUState & cpu)44 GuestAddr GetLinkRegister(const CPUState& cpu) {
45 return cpu.x[30];
46 }
47
SetTlsAddr(ThreadState & state,GuestAddr addr)48 void SetTlsAddr(ThreadState& state, GuestAddr addr) {
49 state.tls = addr;
50 }
51
GetTlsAddr(const ThreadState & state)52 GuestAddr GetTlsAddr(const ThreadState& state) {
53 return state.tls;
54 }
55
SetShadowCallStackPointer(CPUState & cpu,GuestAddr scs_sp)56 void SetShadowCallStackPointer(CPUState& cpu, GuestAddr scs_sp) {
57 cpu.x[18] = scs_sp;
58 }
59
AdvanceInsnAddrBeyondSyscall(CPUState & cpu)60 void AdvanceInsnAddrBeyondSyscall(CPUState& cpu) {
61 cpu.insn_addr += 4;
62 }
63
GetThreadStateRegOffset(int reg)64 std::size_t GetThreadStateRegOffset(int reg) {
65 return offsetof(ThreadState, cpu.x[reg]);
66 }
GetThreadStateFRegOffset(int)67 std::size_t GetThreadStateFRegOffset(int /* reg */) {
68 CHECK(false);
69 }
GetThreadStateVRegOffset(int)70 std::size_t GetThreadStateVRegOffset(int /* reg */) {
71 CHECK(false);
72 }
GetThreadStateSimdRegOffset(int reg)73 std::size_t GetThreadStateSimdRegOffset(int reg) {
74 return offsetof(ThreadState, cpu.v[reg]);
75 }
76
IsSimdOffset(size_t offset)77 bool IsSimdOffset(size_t offset) {
78 size_t v0_offset = offsetof(ThreadState, cpu.v);
79 return (offset >= v0_offset) && ((offset - v0_offset) < sizeof(ThreadState::cpu.v));
80 }
81
DoesCpuStateHaveFlags()82 bool DoesCpuStateHaveFlags() {
83 return true;
84 }
85
DoesCpuStateHaveDedicatedFpRegs()86 bool DoesCpuStateHaveDedicatedFpRegs() {
87 return false;
88 }
89
DoesCpuStateHaveDedicatedVecRegs()90 bool DoesCpuStateHaveDedicatedVecRegs() {
91 return false;
92 }
93
DoesCpuStateHaveDedicatedSimdRegs()94 bool DoesCpuStateHaveDedicatedSimdRegs() {
95 return true;
96 }
97
GetThreadStateFlagOffset()98 std::size_t GetThreadStateFlagOffset() {
99 return offsetof(ThreadState, cpu.flags);
100 }
101
102 } // namespace berberis
103