You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thunk.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /* first we count the number of fields */
  62. type_ptr = types;
  63. nb_fields = 0;
  64. while (*type_ptr != TYPE_NULL) {
  65. type_ptr = thunk_type_next(type_ptr);
  66. nb_fields++;
  67. }
  68. assert(nb_fields > 0);
  69. se = struct_entries + id;
  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 < ARRAY_SIZE(se->field_offsets); i++) {
  79. offset = 0;
  80. max_align = 1;
  81. se->field_offsets[i] = g_new(int, nb_fields);
  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. assert(id < max_struct_entries);
  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. assert(*type_ptr < max_struct_entries);
  230. se = struct_entries + *type_ptr++;
  231. if (se->convert[0] != NULL) {
  232. /* specific conversion is needed */
  233. (*se->convert[to_host])(dst, src);
  234. } else {
  235. /* standard struct conversion */
  236. field_types = se->field_types;
  237. dst_offsets = se->field_offsets[to_host];
  238. src_offsets = se->field_offsets[1 - to_host];
  239. d = dst;
  240. s = src;
  241. for(i = 0;i < se->nb_fields; i++) {
  242. field_types = thunk_convert(d + dst_offsets[i],
  243. s + src_offsets[i],
  244. field_types, to_host);
  245. }
  246. }
  247. }
  248. break;
  249. default:
  250. fprintf(stderr, "Invalid type 0x%x\n", type);
  251. break;
  252. }
  253. return type_ptr;
  254. }
  255. /* from em86 */
  256. /* Utility function: Table-driven functions to translate bitmasks
  257. * between host and target formats
  258. */
  259. unsigned int target_to_host_bitmask(unsigned int target_mask,
  260. const bitmask_transtbl * trans_tbl)
  261. {
  262. const bitmask_transtbl *btp;
  263. unsigned int host_mask = 0;
  264. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  265. if ((target_mask & btp->target_mask) == btp->target_bits) {
  266. host_mask |= btp->host_bits;
  267. }
  268. }
  269. return host_mask;
  270. }
  271. unsigned int host_to_target_bitmask(unsigned int host_mask,
  272. const bitmask_transtbl * trans_tbl)
  273. {
  274. const bitmask_transtbl *btp;
  275. unsigned int target_mask = 0;
  276. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  277. if ((host_mask & btp->host_mask) == btp->host_bits) {
  278. target_mask |= btp->target_bits;
  279. }
  280. }
  281. return target_mask;
  282. }
  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. void thunk_init(unsigned int max_structs)
  292. {
  293. max_struct_entries = max_structs;
  294. struct_entries = g_new0(StructEntry, max_structs);
  295. }