thunk.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include "qemu.h"
  23. #include "thunk.h"
  24. //#define DEBUG
  25. #define MAX_STRUCTS 128
  26. /* XXX: make it dynamic */
  27. StructEntry struct_entries[MAX_STRUCTS];
  28. static const argtype *thunk_type_next_ptr(const argtype *type_ptr);
  29. static inline const argtype *thunk_type_next(const argtype *type_ptr)
  30. {
  31. int type;
  32. type = *type_ptr++;
  33. switch(type) {
  34. case TYPE_CHAR:
  35. case TYPE_SHORT:
  36. case TYPE_INT:
  37. case TYPE_LONGLONG:
  38. case TYPE_ULONGLONG:
  39. case TYPE_LONG:
  40. case TYPE_ULONG:
  41. case TYPE_PTRVOID:
  42. return type_ptr;
  43. case TYPE_PTR:
  44. return thunk_type_next_ptr(type_ptr);
  45. case TYPE_ARRAY:
  46. return thunk_type_next_ptr(type_ptr + 1);
  47. case TYPE_STRUCT:
  48. return type_ptr + 1;
  49. default:
  50. return NULL;
  51. }
  52. }
  53. static const argtype *thunk_type_next_ptr(const argtype *type_ptr)
  54. {
  55. return thunk_type_next(type_ptr);
  56. }
  57. void thunk_register_struct(int id, const char *name, const argtype *types)
  58. {
  59. const argtype *type_ptr;
  60. StructEntry *se;
  61. int nb_fields, offset, max_align, align, size, i, j;
  62. se = struct_entries + id;
  63. /* first we count the number of fields */
  64. type_ptr = types;
  65. nb_fields = 0;
  66. while (*type_ptr != TYPE_NULL) {
  67. type_ptr = thunk_type_next(type_ptr);
  68. nb_fields++;
  69. }
  70. se->field_types = types;
  71. se->nb_fields = nb_fields;
  72. se->name = name;
  73. #ifdef DEBUG
  74. printf("struct %s: id=%d nb_fields=%d\n",
  75. se->name, id, se->nb_fields);
  76. #endif
  77. /* now we can alloc the data */
  78. for(i = 0;i < 2; i++) {
  79. offset = 0;
  80. max_align = 1;
  81. se->field_offsets[i] = malloc(nb_fields * sizeof(int));
  82. type_ptr = se->field_types;
  83. for(j = 0;j < nb_fields; j++) {
  84. size = thunk_type_size(type_ptr, i);
  85. align = thunk_type_align(type_ptr, i);
  86. offset = (offset + align - 1) & ~(align - 1);
  87. se->field_offsets[i][j] = offset;
  88. offset += size;
  89. if (align > max_align)
  90. max_align = align;
  91. type_ptr = thunk_type_next(type_ptr);
  92. }
  93. offset = (offset + max_align - 1) & ~(max_align - 1);
  94. se->size[i] = offset;
  95. se->align[i] = max_align;
  96. #ifdef DEBUG
  97. printf("%s: size=%d align=%d\n",
  98. i == THUNK_HOST ? "host" : "target", offset, max_align);
  99. #endif
  100. }
  101. }
  102. void thunk_register_struct_direct(int id, const char *name,
  103. const StructEntry *se1)
  104. {
  105. StructEntry *se;
  106. se = struct_entries + id;
  107. *se = *se1;
  108. se->name = name;
  109. }
  110. /* now we can define the main conversion functions */
  111. const argtype *thunk_convert(void *dst, const void *src,
  112. const argtype *type_ptr, int to_host)
  113. {
  114. int type;
  115. type = *type_ptr++;
  116. switch(type) {
  117. case TYPE_CHAR:
  118. *(uint8_t *)dst = *(uint8_t *)src;
  119. break;
  120. case TYPE_SHORT:
  121. *(uint16_t *)dst = tswap16(*(uint16_t *)src);
  122. break;
  123. case TYPE_INT:
  124. *(uint32_t *)dst = tswap32(*(uint32_t *)src);
  125. break;
  126. case TYPE_LONGLONG:
  127. case TYPE_ULONGLONG:
  128. *(uint64_t *)dst = tswap64(*(uint64_t *)src);
  129. break;
  130. #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
  131. case TYPE_LONG:
  132. case TYPE_ULONG:
  133. case TYPE_PTRVOID:
  134. *(uint32_t *)dst = tswap32(*(uint32_t *)src);
  135. break;
  136. #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
  137. case TYPE_LONG:
  138. case TYPE_ULONG:
  139. case TYPE_PTRVOID:
  140. if (to_host) {
  141. if (type == TYPE_LONG) {
  142. /* sign extension */
  143. *(uint64_t *)dst = (int32_t)tswap32(*(uint32_t *)src);
  144. } else {
  145. *(uint64_t *)dst = tswap32(*(uint32_t *)src);
  146. }
  147. } else {
  148. *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff);
  149. }
  150. break;
  151. #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
  152. case TYPE_LONG:
  153. case TYPE_ULONG:
  154. case TYPE_PTRVOID:
  155. *(uint64_t *)dst = tswap64(*(uint64_t *)src);
  156. break;
  157. #elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
  158. case TYPE_LONG:
  159. case TYPE_ULONG:
  160. case TYPE_PTRVOID:
  161. if (to_host) {
  162. *(uint32_t *)dst = tswap64(*(uint64_t *)src);
  163. } else {
  164. if (type == TYPE_LONG) {
  165. /* sign extension */
  166. *(uint64_t *)dst = tswap64(*(int32_t *)src);
  167. } else {
  168. *(uint64_t *)dst = tswap64(*(uint32_t *)src);
  169. }
  170. }
  171. break;
  172. #else
  173. #warning unsupported conversion
  174. #endif
  175. case TYPE_ARRAY:
  176. {
  177. int array_length, i, dst_size, src_size;
  178. const uint8_t *s;
  179. uint8_t *d;
  180. array_length = *type_ptr++;
  181. dst_size = thunk_type_size(type_ptr, to_host);
  182. src_size = thunk_type_size(type_ptr, 1 - to_host);
  183. d = dst;
  184. s = src;
  185. for(i = 0;i < array_length; i++) {
  186. thunk_convert(d, s, type_ptr, to_host);
  187. d += dst_size;
  188. s += src_size;
  189. }
  190. type_ptr = thunk_type_next(type_ptr);
  191. }
  192. break;
  193. case TYPE_STRUCT:
  194. {
  195. int i;
  196. const StructEntry *se;
  197. const uint8_t *s;
  198. uint8_t *d;
  199. const argtype *field_types;
  200. const int *dst_offsets, *src_offsets;
  201. se = struct_entries + *type_ptr++;
  202. if (se->convert[0] != NULL) {
  203. /* specific conversion is needed */
  204. (*se->convert[to_host])(dst, src);
  205. } else {
  206. /* standard struct conversion */
  207. field_types = se->field_types;
  208. dst_offsets = se->field_offsets[to_host];
  209. src_offsets = se->field_offsets[1 - to_host];
  210. d = dst;
  211. s = src;
  212. for(i = 0;i < se->nb_fields; i++) {
  213. field_types = thunk_convert(d + dst_offsets[i],
  214. s + src_offsets[i],
  215. field_types, to_host);
  216. }
  217. }
  218. }
  219. break;
  220. default:
  221. fprintf(stderr, "Invalid type 0x%x\n", type);
  222. break;
  223. }
  224. return type_ptr;
  225. }
  226. /* from em86 */
  227. /* Utility function: Table-driven functions to translate bitmasks
  228. * between X86 and Alpha formats...
  229. */
  230. unsigned int target_to_host_bitmask(unsigned int x86_mask,
  231. const bitmask_transtbl * trans_tbl)
  232. {
  233. const bitmask_transtbl *btp;
  234. unsigned int alpha_mask = 0;
  235. for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
  236. if((x86_mask & btp->x86_mask) == btp->x86_bits) {
  237. alpha_mask |= btp->alpha_bits;
  238. }
  239. }
  240. return(alpha_mask);
  241. }
  242. unsigned int host_to_target_bitmask(unsigned int alpha_mask,
  243. const bitmask_transtbl * trans_tbl)
  244. {
  245. const bitmask_transtbl *btp;
  246. unsigned int x86_mask = 0;
  247. for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
  248. if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
  249. x86_mask |= btp->x86_bits;
  250. }
  251. }
  252. return(x86_mask);
  253. }
  254. #ifndef NO_THUNK_TYPE_SIZE
  255. int thunk_type_size_array(const argtype *type_ptr, int is_host)
  256. {
  257. return thunk_type_size(type_ptr, is_host);
  258. }
  259. int thunk_type_align_array(const argtype *type_ptr, int is_host)
  260. {
  261. return thunk_type_align(type_ptr, is_host);
  262. }
  263. #endif /* ndef NO_THUNK_TYPE_SIZE */