thunk.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "qemu/osdep.h"
  20. #include "qemu.h"
  21. #include "exec/user/thunk.h"
  22. //#define DEBUG
  23. static unsigned int max_struct_entries;
  24. StructEntry *struct_entries;
  25. static const argtype *thunk_type_next_ptr(const argtype *type_ptr);
  26. static inline const argtype *thunk_type_next(const argtype *type_ptr)
  27. {
  28. int type;
  29. type = *type_ptr++;
  30. switch(type) {
  31. case TYPE_CHAR:
  32. case TYPE_SHORT:
  33. case TYPE_INT:
  34. case TYPE_LONGLONG:
  35. case TYPE_ULONGLONG:
  36. case TYPE_LONG:
  37. case TYPE_ULONG:
  38. case TYPE_PTRVOID:
  39. case TYPE_OLDDEVT:
  40. return type_ptr;
  41. case TYPE_PTR:
  42. return thunk_type_next_ptr(type_ptr);
  43. case TYPE_ARRAY:
  44. return thunk_type_next_ptr(type_ptr + 1);
  45. case TYPE_STRUCT:
  46. return type_ptr + 1;
  47. default:
  48. return NULL;
  49. }
  50. }
  51. static const argtype *thunk_type_next_ptr(const argtype *type_ptr)
  52. {
  53. return thunk_type_next(type_ptr);
  54. }
  55. void thunk_register_struct(int id, const char *name, const argtype *types)
  56. {
  57. const argtype *type_ptr;
  58. StructEntry *se;
  59. int nb_fields, offset, max_align, align, size, i, j;
  60. assert(id < max_struct_entries);
  61. se = struct_entries + id;
  62. /* first we count the number of fields */
  63. type_ptr = types;
  64. nb_fields = 0;
  65. while (*type_ptr != TYPE_NULL) {
  66. type_ptr = thunk_type_next(type_ptr);
  67. nb_fields++;
  68. }
  69. se->field_types = types;
  70. se->nb_fields = nb_fields;
  71. se->name = name;
  72. #ifdef DEBUG
  73. printf("struct %s: id=%d nb_fields=%d\n",
  74. se->name, id, se->nb_fields);
  75. #endif
  76. /* now we can alloc the data */
  77. for(i = 0;i < 2; i++) {
  78. offset = 0;
  79. max_align = 1;
  80. se->field_offsets[i] = malloc(nb_fields * sizeof(int));
  81. type_ptr = se->field_types;
  82. for(j = 0;j < nb_fields; j++) {
  83. size = thunk_type_size(type_ptr, i);
  84. align = thunk_type_align(type_ptr, i);
  85. offset = (offset + align - 1) & ~(align - 1);
  86. se->field_offsets[i][j] = offset;
  87. offset += size;
  88. if (align > max_align)
  89. max_align = align;
  90. type_ptr = thunk_type_next(type_ptr);
  91. }
  92. offset = (offset + max_align - 1) & ~(max_align - 1);
  93. se->size[i] = offset;
  94. se->align[i] = max_align;
  95. #ifdef DEBUG
  96. printf("%s: size=%d align=%d\n",
  97. i == THUNK_HOST ? "host" : "target", offset, max_align);
  98. #endif
  99. }
  100. }
  101. void thunk_register_struct_direct(int id, const char *name,
  102. const StructEntry *se1)
  103. {
  104. StructEntry *se;
  105. assert(id < max_struct_entries);
  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_OLDDEVT:
  176. {
  177. uint64_t val = 0;
  178. switch (thunk_type_size(type_ptr - 1, !to_host)) {
  179. case 2:
  180. val = *(uint16_t *)src;
  181. break;
  182. case 4:
  183. val = *(uint32_t *)src;
  184. break;
  185. case 8:
  186. val = *(uint64_t *)src;
  187. break;
  188. }
  189. switch (thunk_type_size(type_ptr - 1, to_host)) {
  190. case 2:
  191. *(uint16_t *)dst = tswap16(val);
  192. break;
  193. case 4:
  194. *(uint32_t *)dst = tswap32(val);
  195. break;
  196. case 8:
  197. *(uint64_t *)dst = tswap64(val);
  198. break;
  199. }
  200. break;
  201. }
  202. case TYPE_ARRAY:
  203. {
  204. int array_length, i, dst_size, src_size;
  205. const uint8_t *s;
  206. uint8_t *d;
  207. array_length = *type_ptr++;
  208. dst_size = thunk_type_size(type_ptr, to_host);
  209. src_size = thunk_type_size(type_ptr, 1 - to_host);
  210. d = dst;
  211. s = src;
  212. for(i = 0;i < array_length; i++) {
  213. thunk_convert(d, s, type_ptr, to_host);
  214. d += dst_size;
  215. s += src_size;
  216. }
  217. type_ptr = thunk_type_next(type_ptr);
  218. }
  219. break;
  220. case TYPE_STRUCT:
  221. {
  222. int i;
  223. const StructEntry *se;
  224. const uint8_t *s;
  225. uint8_t *d;
  226. const argtype *field_types;
  227. const int *dst_offsets, *src_offsets;
  228. assert(*type_ptr < max_struct_entries);
  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 host and target formats
  257. */
  258. unsigned int target_to_host_bitmask(unsigned int target_mask,
  259. const bitmask_transtbl * trans_tbl)
  260. {
  261. const bitmask_transtbl *btp;
  262. unsigned int host_mask = 0;
  263. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  264. if ((target_mask & btp->target_mask) == btp->target_bits) {
  265. host_mask |= btp->host_bits;
  266. }
  267. }
  268. return host_mask;
  269. }
  270. unsigned int host_to_target_bitmask(unsigned int host_mask,
  271. const bitmask_transtbl * trans_tbl)
  272. {
  273. const bitmask_transtbl *btp;
  274. unsigned int target_mask = 0;
  275. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  276. if ((host_mask & btp->host_mask) == btp->host_bits) {
  277. target_mask |= btp->target_bits;
  278. }
  279. }
  280. return target_mask;
  281. }
  282. int thunk_type_size_array(const argtype *type_ptr, int is_host)
  283. {
  284. return thunk_type_size(type_ptr, is_host);
  285. }
  286. int thunk_type_align_array(const argtype *type_ptr, int is_host)
  287. {
  288. return thunk_type_align(type_ptr, is_host);
  289. }
  290. void thunk_init(unsigned int max_structs)
  291. {
  292. max_struct_entries = max_structs;
  293. struct_entries = g_new0(StructEntry, max_structs);
  294. }