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 "berberis/base/logging.h"
18 #include "berberis/guest_state/get_cpu_state_opaque.h"
19 #include "berberis/guest_state/guest_state_arch.h"
20 #include "berberis/guest_state/guest_state_opaque.h"
21 #include "native_bridge_support/guest_state_accessor/accessor.h"
22
23 namespace berberis {
24
LoadGuestStateRegisters(const void * guest_state_data,size_t guest_state_data_size,NativeBridgeGuestRegs * guest_regs)25 extern "C" __attribute__((visibility("default"))) int LoadGuestStateRegisters(
26 const void* guest_state_data,
27 size_t guest_state_data_size,
28 NativeBridgeGuestRegs* guest_regs) {
29 if (guest_state_data_size < sizeof(ThreadState)) {
30 ALOGE("The guest state data size is invalid: %zu", guest_state_data_size);
31 return NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_INVALID_STATE;
32 }
33 guest_regs->guest_arch = NATIVE_BRIDGE_ARCH_RISCV64;
34 return GetCpuState(guest_regs, &(static_cast<const ThreadState*>(guest_state_data))->cpu);
35 }
36
GetCpuState(NativeBridgeGuestRegs * guest_regs,const CPUState * state)37 int GetCpuState(NativeBridgeGuestRegs* guest_regs, const CPUState* state) {
38 if (guest_regs->guest_arch != NATIVE_BRIDGE_ARCH_RISCV64) {
39 ALOGE("The guest architecture is unmatched: %lu", guest_regs->guest_arch);
40 return NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_UNSUPPORTED_ARCH;
41 }
42 memcpy(&guest_regs->regs_riscv64.x, &state->x, sizeof(guest_regs->regs_riscv64.x));
43 memcpy(&guest_regs->regs_riscv64.f, &state->f, sizeof(guest_regs->regs_riscv64.f));
44 memcpy(&guest_regs->regs_riscv64.v, &state->v, sizeof(guest_regs->regs_riscv64.v));
45 memcpy(&guest_regs->regs_riscv64.ip, &state->insn_addr, sizeof(guest_regs->regs_riscv64.ip));
46 return 0;
47 }
48 } // namespace berberis
49