thunk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. const argtype *thunk_print(void *arg, const argtype *type_ptr)
  256. {
  257. int type;
  258. type = *type_ptr++;
  259. switch (type) {
  260. case TYPE_CHAR:
  261. qemu_log("%c", *(uint8_t *)arg);
  262. break;
  263. case TYPE_SHORT:
  264. qemu_log("%" PRId16, tswap16(*(uint16_t *)arg));
  265. break;
  266. case TYPE_INT:
  267. qemu_log("%" PRId32, tswap32(*(uint32_t *)arg));
  268. break;
  269. case TYPE_LONGLONG:
  270. qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
  271. break;
  272. case TYPE_ULONGLONG:
  273. qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
  274. break;
  275. #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
  276. case TYPE_PTRVOID:
  277. qemu_log("0x%" PRIx32, tswap32(*(uint32_t *)arg));
  278. break;
  279. case TYPE_LONG:
  280. qemu_log("%" PRId32, tswap32(*(uint32_t *)arg));
  281. break;
  282. case TYPE_ULONG:
  283. qemu_log("%" PRIu32, tswap32(*(uint32_t *)arg));
  284. break;
  285. #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
  286. case TYPE_PTRVOID:
  287. qemu_log("0x%" PRIx32, tswap32(*(uint64_t *)arg & 0xffffffff));
  288. break;
  289. case TYPE_LONG:
  290. qemu_log("%" PRId32, tswap32(*(uint64_t *)arg & 0xffffffff));
  291. break;
  292. case TYPE_ULONG:
  293. qemu_log("%" PRIu32, tswap32(*(uint64_t *)arg & 0xffffffff));
  294. break;
  295. #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
  296. case TYPE_PTRVOID:
  297. qemu_log("0x%" PRIx64, tswap64(*(uint64_t *)arg));
  298. break;
  299. case TYPE_LONG:
  300. qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
  301. break;
  302. case TYPE_ULONG:
  303. qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
  304. break;
  305. #else
  306. case TYPE_PTRVOID:
  307. qemu_log("0x%" PRIx64, tswap64(*(uint64_t *)arg));
  308. break;
  309. case TYPE_LONG:
  310. qemu_log("%" PRId64, tswap64(*(uint64_t *)arg));
  311. break;
  312. case TYPE_ULONG:
  313. qemu_log("%" PRIu64, tswap64(*(uint64_t *)arg));
  314. break;
  315. #endif
  316. case TYPE_OLDDEVT:
  317. {
  318. uint64_t val = 0;
  319. switch (thunk_type_size(type_ptr - 1, 1)) {
  320. case 2:
  321. val = *(uint16_t *)arg;
  322. break;
  323. case 4:
  324. val = *(uint32_t *)arg;
  325. break;
  326. case 8:
  327. val = *(uint64_t *)arg;
  328. break;
  329. }
  330. switch (thunk_type_size(type_ptr - 1, 0)) {
  331. case 2:
  332. qemu_log("%" PRIu16, tswap16(val));
  333. break;
  334. case 4:
  335. qemu_log("%" PRIu32, tswap32(val));
  336. break;
  337. case 8:
  338. qemu_log("%" PRIu64, tswap64(val));
  339. break;
  340. }
  341. }
  342. break;
  343. case TYPE_ARRAY:
  344. {
  345. int i, array_length, arg_size;
  346. uint8_t *a;
  347. int is_string = 0;
  348. array_length = *type_ptr++;
  349. arg_size = thunk_type_size(type_ptr, 0);
  350. a = arg;
  351. if (*type_ptr == TYPE_CHAR) {
  352. qemu_log("\"");
  353. is_string = 1;
  354. } else {
  355. qemu_log("[");
  356. }
  357. for (i = 0; i < array_length; i++) {
  358. if (i > 0 && !is_string) {
  359. qemu_log(",");
  360. }
  361. thunk_print(a, type_ptr);
  362. a += arg_size;
  363. }
  364. if (is_string) {
  365. qemu_log("\"");
  366. } else {
  367. qemu_log("]");
  368. }
  369. type_ptr = thunk_type_next(type_ptr);
  370. }
  371. break;
  372. case TYPE_STRUCT:
  373. {
  374. int i;
  375. const StructEntry *se;
  376. uint8_t *a;
  377. const argtype *field_types;
  378. const int *arg_offsets;
  379. se = struct_entries + *type_ptr++;
  380. if (se->print != NULL) {
  381. se->print(arg);
  382. } else {
  383. a = arg;
  384. field_types = se->field_types;
  385. arg_offsets = se->field_offsets[0];
  386. qemu_log("{");
  387. for (i = 0; i < se->nb_fields; i++) {
  388. if (i > 0) {
  389. qemu_log(",");
  390. }
  391. field_types = thunk_print(a + arg_offsets[i], field_types);
  392. }
  393. qemu_log("}");
  394. }
  395. }
  396. break;
  397. default:
  398. g_assert_not_reached();
  399. }
  400. return type_ptr;
  401. }
  402. /* from em86 */
  403. /* Utility function: Table-driven functions to translate bitmasks
  404. * between host and target formats
  405. */
  406. unsigned int target_to_host_bitmask(unsigned int target_mask,
  407. const bitmask_transtbl * trans_tbl)
  408. {
  409. const bitmask_transtbl *btp;
  410. unsigned int host_mask = 0;
  411. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  412. if ((target_mask & btp->target_mask) == btp->target_bits) {
  413. host_mask |= btp->host_bits;
  414. }
  415. }
  416. return host_mask;
  417. }
  418. unsigned int host_to_target_bitmask(unsigned int host_mask,
  419. const bitmask_transtbl * trans_tbl)
  420. {
  421. const bitmask_transtbl *btp;
  422. unsigned int target_mask = 0;
  423. for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) {
  424. if ((host_mask & btp->host_mask) == btp->host_bits) {
  425. target_mask |= btp->target_bits;
  426. }
  427. }
  428. return target_mask;
  429. }
  430. int thunk_type_size_array(const argtype *type_ptr, int is_host)
  431. {
  432. return thunk_type_size(type_ptr, is_host);
  433. }
  434. int thunk_type_align_array(const argtype *type_ptr, int is_host)
  435. {
  436. return thunk_type_align(type_ptr, is_host);
  437. }
  438. void thunk_init(unsigned int max_structs)
  439. {
  440. max_struct_entries = max_structs;
  441. struct_entries = g_new0(StructEntry, max_structs);
  442. }