1 /* 2 * gdbstub helpers 3 * 4 * These are all used by the various frontends and have to be host 5 * aware to ensure things are store in target order. 6 * 7 * Copyright (c) 2022 Linaro Ltd 8 * 9 * SPDX-License-Identifier: GPL-2.0-or-later 10 */ 11 12 #ifndef _GDBSTUB_HELPERS_H_ 13 #define _GDBSTUB_HELPERS_H_ 14 15 #ifndef COMPILING_PER_TARGET 16 #error "gdbstub helpers should only be included by target specific code" 17 #endif 18 19 #include "qemu/bswap.h" 20 #include "qemu/target-info.h" 21 #include "cpu-param.h" 22 23 /* 24 * The GDB remote protocol transfers values in target byte order. As 25 * the gdbstub may be batching up several register values we always 26 * append to the array. 27 */ 28 29 static inline int gdb_get_reg8(GByteArray *buf, uint8_t val) 30 { 31 g_byte_array_append(buf, &val, 1); 32 return 1; 33 } 34 35 static inline int gdb_get_reg16(GByteArray *buf, uint16_t val) 36 { 37 if (target_big_endian()) { 38 cpu_to_be16s(&val); 39 } else { 40 cpu_to_le16s(&val); 41 } 42 g_byte_array_append(buf, (uint8_t *) &val, 2); 43 return 2; 44 } 45 46 static inline int gdb_get_reg32(GByteArray *buf, uint32_t val) 47 { 48 if (target_big_endian()) { 49 cpu_to_be32s(&val); 50 } else { 51 cpu_to_le32s(&val); 52 } 53 g_byte_array_append(buf, (uint8_t *) &val, 4); 54 return 4; 55 } 56 57 static inline int gdb_get_reg64(GByteArray *buf, uint64_t val) 58 { 59 if (target_big_endian()) { 60 cpu_to_be64s(&val); 61 } else { 62 cpu_to_le64s(&val); 63 } 64 g_byte_array_append(buf, (uint8_t *) &val, 8); 65 return 8; 66 } 67 68 static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi, 69 uint64_t val_lo) 70 { 71 uint64_t tmp[2]; 72 if (target_big_endian()) { 73 tmp[0] = cpu_to_be64(val_hi); 74 tmp[1] = cpu_to_be64(val_lo); 75 } else { 76 tmp[0] = cpu_to_le64(val_lo); 77 tmp[1] = cpu_to_le64(val_hi); 78 } 79 g_byte_array_append(buf, (uint8_t *)&tmp, 16); 80 return 16; 81 } 82 83 static inline int gdb_get_zeroes(GByteArray *array, size_t len) 84 { 85 guint oldlen = array->len; 86 g_byte_array_set_size(array, oldlen + len); 87 memset(array->data + oldlen, 0, len); 88 89 return len; 90 } 91 92 /** 93 * gdb_get_reg_ptr: get pointer to start of last element 94 * @len: length of element 95 * 96 * This is a helper function to extract the pointer to the last 97 * element for additional processing. Some front-ends do additional 98 * dynamic swapping of the elements based on CPU state. 99 */ 100 static inline uint8_t *gdb_get_reg_ptr(GByteArray *buf, int len) 101 { 102 return buf->data + buf->len - len; 103 } 104 105 #if TARGET_LONG_BITS == 64 106 #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val) 107 #define ldtul_p(addr) ldq_p(addr) 108 #define ldtul_le_p(addr) ldq_le_p(addr) 109 #define ldtul_be_p(addr) ldq_be_p(addr) 110 #else 111 #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val) 112 #define ldtul_p(addr) ldl_p(addr) 113 #define ldtul_le_p(addr) ldl_le_p(addr) 114 #define ldtul_be_p(addr) ldl_be_p(addr) 115 #endif 116 117 #endif /* _GDBSTUB_HELPERS_H_ */ 118