thunk.c 8.8 KB

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