thunk.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Generic thunking code to convert data between host and target CPU
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef THUNK_H
  20. #define THUNK_H
  21. #include <inttypes.h>
  22. #include "cpu.h"
  23. /* types enums definitions */
  24. typedef enum argtype {
  25. TYPE_NULL,
  26. TYPE_CHAR,
  27. TYPE_SHORT,
  28. TYPE_INT,
  29. TYPE_LONG,
  30. TYPE_ULONG,
  31. TYPE_PTRVOID, /* pointer on unknown data */
  32. TYPE_LONGLONG,
  33. TYPE_ULONGLONG,
  34. TYPE_PTR,
  35. TYPE_ARRAY,
  36. TYPE_STRUCT,
  37. } argtype;
  38. #define MK_PTR(type) TYPE_PTR, type
  39. #define MK_ARRAY(type, size) TYPE_ARRAY, size, type
  40. #define MK_STRUCT(id) TYPE_STRUCT, id
  41. #define THUNK_TARGET 0
  42. #define THUNK_HOST 1
  43. typedef struct {
  44. /* standard struct handling */
  45. const argtype *field_types;
  46. int nb_fields;
  47. int *field_offsets[2];
  48. /* special handling */
  49. void (*convert[2])(void *dst, const void *src);
  50. int size[2];
  51. int align[2];
  52. const char *name;
  53. } StructEntry;
  54. /* Translation table for bitmasks... */
  55. typedef struct bitmask_transtbl {
  56. unsigned int x86_mask;
  57. unsigned int x86_bits;
  58. unsigned int alpha_mask;
  59. unsigned int alpha_bits;
  60. } bitmask_transtbl;
  61. void thunk_register_struct(int id, const char *name, const argtype *types);
  62. void thunk_register_struct_direct(int id, const char *name,
  63. const StructEntry *se1);
  64. const argtype *thunk_convert(void *dst, const void *src,
  65. const argtype *type_ptr, int to_host);
  66. #ifndef NO_THUNK_TYPE_SIZE
  67. extern StructEntry struct_entries[];
  68. int thunk_type_size_array(const argtype *type_ptr, int is_host);
  69. int thunk_type_align_array(const argtype *type_ptr, int is_host);
  70. static inline int thunk_type_size(const argtype *type_ptr, int is_host)
  71. {
  72. int type, size;
  73. const StructEntry *se;
  74. type = *type_ptr;
  75. switch(type) {
  76. case TYPE_CHAR:
  77. return 1;
  78. case TYPE_SHORT:
  79. return 2;
  80. case TYPE_INT:
  81. return 4;
  82. case TYPE_LONGLONG:
  83. case TYPE_ULONGLONG:
  84. return 8;
  85. case TYPE_LONG:
  86. case TYPE_ULONG:
  87. case TYPE_PTRVOID:
  88. case TYPE_PTR:
  89. if (is_host) {
  90. return HOST_LONG_SIZE;
  91. } else {
  92. return TARGET_ABI_BITS / 8;
  93. }
  94. break;
  95. case TYPE_ARRAY:
  96. size = type_ptr[1];
  97. return size * thunk_type_size_array(type_ptr + 2, is_host);
  98. case TYPE_STRUCT:
  99. se = struct_entries + type_ptr[1];
  100. return se->size[is_host];
  101. default:
  102. return -1;
  103. }
  104. }
  105. static inline int thunk_type_align(const argtype *type_ptr, int is_host)
  106. {
  107. int type;
  108. const StructEntry *se;
  109. type = *type_ptr;
  110. switch(type) {
  111. case TYPE_CHAR:
  112. return 1;
  113. case TYPE_SHORT:
  114. return 2;
  115. case TYPE_INT:
  116. return 4;
  117. case TYPE_LONGLONG:
  118. case TYPE_ULONGLONG:
  119. return 8;
  120. case TYPE_LONG:
  121. case TYPE_ULONG:
  122. case TYPE_PTRVOID:
  123. case TYPE_PTR:
  124. if (is_host) {
  125. return HOST_LONG_SIZE;
  126. } else {
  127. return TARGET_ABI_BITS / 8;
  128. }
  129. break;
  130. case TYPE_ARRAY:
  131. return thunk_type_align_array(type_ptr + 2, is_host);
  132. case TYPE_STRUCT:
  133. se = struct_entries + type_ptr[1];
  134. return se->align[is_host];
  135. default:
  136. return -1;
  137. }
  138. }
  139. #endif /* NO_THUNK_TYPE_SIZE */
  140. unsigned int target_to_host_bitmask(unsigned int x86_mask,
  141. const bitmask_transtbl * trans_tbl);
  142. unsigned int host_to_target_bitmask(unsigned int alpha_mask,
  143. const bitmask_transtbl * trans_tbl);
  144. #endif