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_GUEST_STATE_ACCESSOR_H_ 18 #define NATIVE_BRIDGE_SUPPORT_GUEST_STATE_ACCESSOR_H_ 19 20 #include <stdalign.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 #include <sys/cdefs.h> 24 25 __BEGIN_DECLS 26 27 // List of supported guest and host architecures 28 #define NATIVE_BRIDGE_ARCH_ARM 1 29 #define NATIVE_BRIDGE_ARCH_ARM64 2 30 #define NATIVE_BRIDGE_ARCH_RISCV64 4 31 #define NATIVE_BRIDGE_ARCH_X86 5 32 #define NATIVE_BRIDGE_ARCH_X86_64 6 33 34 #if defined(__LP64__) 35 struct NativeBridgeGuestRegsArm64 { 36 uint64_t x[31]; 37 uint64_t sp; 38 uint64_t ip; 39 alignas(16) __uint128_t v[32]; 40 }; 41 42 struct NativeBridgeGuestRegsRiscv64 { 43 uint64_t x[32]; 44 uint64_t f[32]; 45 alignas(16) __uint128_t v[32]; 46 uint64_t ip; 47 }; 48 #endif 49 50 struct NativeBridgeGuestRegsArm { 51 uint32_t r[16]; 52 alignas(16) uint64_t q[32]; 53 }; 54 55 // This structure represents guest registers for all supported architectures 56 // Use following fields depending on `arch` field value 57 // * NATIVE_BRIDGE_ARCH_ARM -> .regs_arm 58 // * NATIVE_BRIDGE_ARCH_ARM64 -> .regs_arm64 59 // * NATIVE_BRIDGE_ARCH_RISCV64 -> .regs_riscv64 60 // 61 // Note that 64bit architectures are only supported for 64bit host platform. 62 struct NativeBridgeGuestRegs { 63 uint64_t guest_arch; 64 union { 65 #if defined(__LP64__) 66 NativeBridgeGuestRegsArm64 regs_arm64; 67 NativeBridgeGuestRegsRiscv64 regs_riscv64; 68 #endif 69 NativeBridgeGuestRegsArm regs_arm; 70 }; 71 }; 72 73 // Signature value for NativeBridgeGuestStateHeader::signature 74 #define NATIVE_BRIDGE_GUEST_STATE_SIGNATURE 0x5349'5245'4252'4542 75 76 // This is the header of guest_state, pointer to which is stored in 77 // TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE and accessed by android debuggerd 78 // It can also be used by external debugging tools. 79 struct alignas(16) NativeBridgeGuestStateHeader { 80 // Guest state signature for initial check must always be 81 // equal to NATIVE_BRIDGE_GUEST_STATE_SIGNATURE 82 uint64_t signature; 83 // Guest and host architectures: defined as NATIVE_BRIDGE_ARCH_* 84 uint32_t native_bridge_host_arch; 85 uint32_t native_bridge_guest_arch; 86 // The pointer and size are used by debugging/crash reporting tools to copy 87 // the state from a (probably crashed) process. 88 // The pointer to the implementation specific guest state. 89 const void* guest_state_data; 90 // Size of implementation specific representation of the guest state. 91 size_t guest_state_data_size; 92 }; 93 94 // Unsupported combination of guest and host architectures 95 #define NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_UNSUPPORTED_ARCH -1 96 // Unsupported provider 97 #define NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_UNSUPPORTED_PROVIDER -2 98 // Unsupported guest state version 99 #define NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_UNSUPPORTED_VERSION -3 100 // Invalid guest state 101 #define NATIVE_BRIDGE_GUEST_STATE_ACCESSOR_ERROR_INVALID_STATE -11 102 103 // Returns non-zero error code in case of error, 0 on success. Updates 104 // `guest_regs` structure with values from internal representation of 105 // the guest state. 106 // 107 // `guest_state_data` points to the implementation specific guest_state 108 int LoadGuestStateRegisters(const void* guest_state_data, 109 size_t guest_state_data_size, 110 NativeBridgeGuestRegs* guest_regs); 111 112 __END_DECLS 113 114 #endif // NATIVE_BRIDGE_SUPPORT_GUEST_STATE_ACCESSOR_H_ 115