helpers.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef _GDBSTUB_HELPERS_H_
  12. #define _GDBSTUB_HELPERS_H_
  13. #ifndef COMPILING_PER_TARGET
  14. #error "gdbstub helpers should only be included by target specific code"
  15. #endif
  16. #include "exec/tswap.h"
  17. #include "cpu-param.h"
  18. /*
  19. * The GDB remote protocol transfers values in target byte order. As
  20. * the gdbstub may be batching up several register values we always
  21. * append to the array.
  22. */
  23. static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
  24. {
  25. g_byte_array_append(buf, &val, 1);
  26. return 1;
  27. }
  28. static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
  29. {
  30. uint16_t to_word = tswap16(val);
  31. g_byte_array_append(buf, (uint8_t *) &to_word, 2);
  32. return 2;
  33. }
  34. static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
  35. {
  36. uint32_t to_long = tswap32(val);
  37. g_byte_array_append(buf, (uint8_t *) &to_long, 4);
  38. return 4;
  39. }
  40. static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
  41. {
  42. uint64_t to_quad = tswap64(val);
  43. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  44. return 8;
  45. }
  46. static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
  47. uint64_t val_lo)
  48. {
  49. uint64_t to_quad;
  50. #if TARGET_BIG_ENDIAN
  51. to_quad = tswap64(val_hi);
  52. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  53. to_quad = tswap64(val_lo);
  54. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  55. #else
  56. to_quad = tswap64(val_lo);
  57. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  58. to_quad = tswap64(val_hi);
  59. g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
  60. #endif
  61. return 16;
  62. }
  63. static inline int gdb_get_zeroes(GByteArray *array, size_t len)
  64. {
  65. guint oldlen = array->len;
  66. g_byte_array_set_size(array, oldlen + len);
  67. memset(array->data + oldlen, 0, len);
  68. return len;
  69. }
  70. /**
  71. * gdb_get_reg_ptr: get pointer to start of last element
  72. * @len: length of element
  73. *
  74. * This is a helper function to extract the pointer to the last
  75. * element for additional processing. Some front-ends do additional
  76. * dynamic swapping of the elements based on CPU state.
  77. */
  78. static inline uint8_t *gdb_get_reg_ptr(GByteArray *buf, int len)
  79. {
  80. return buf->data + buf->len - len;
  81. }
  82. #if TARGET_LONG_BITS == 64
  83. #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
  84. #define ldtul_p(addr) ldq_p(addr)
  85. #define ldtul_le_p(addr) ldq_le_p(addr)
  86. #define ldtul_be_p(addr) ldq_be_p(addr)
  87. #else
  88. #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
  89. #define ldtul_p(addr) ldl_p(addr)
  90. #define ldtul_le_p(addr) ldl_le_p(addr)
  91. #define ldtul_be_p(addr) ldl_be_p(addr)
  92. #endif
  93. #endif /* _GDBSTUB_HELPERS_H_ */