2 * Generic thunking code to convert data between host and target CPU
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu-common.h"
22 #include "exec/user/thunk.h"
26 #define MAX_STRUCTS 128
28 /* XXX: make it dynamic */
29 StructEntry struct_entries
[MAX_STRUCTS
];
31 static const argtype
*thunk_type_next_ptr(const argtype
*type_ptr
);
33 static inline const argtype
*thunk_type_next(const argtype
*type_ptr
)
50 return thunk_type_next_ptr(type_ptr
);
52 return thunk_type_next_ptr(type_ptr
+ 1);
60 static const argtype
*thunk_type_next_ptr(const argtype
*type_ptr
)
62 return thunk_type_next(type_ptr
);
65 void thunk_register_struct(int id
, const char *name
, const argtype
*types
)
67 const argtype
*type_ptr
;
69 int nb_fields
, offset
, max_align
, align
, size
, i
, j
;
71 se
= struct_entries
+ id
;
73 /* first we count the number of fields */
76 while (*type_ptr
!= TYPE_NULL
) {
77 type_ptr
= thunk_type_next(type_ptr
);
80 se
->field_types
= types
;
81 se
->nb_fields
= nb_fields
;
84 printf("struct %s: id=%d nb_fields=%d\n",
85 se
->name
, id
, se
->nb_fields
);
87 /* now we can alloc the data */
89 for(i
= 0;i
< 2; i
++) {
92 se
->field_offsets
[i
] = malloc(nb_fields
* sizeof(int));
93 type_ptr
= se
->field_types
;
94 for(j
= 0;j
< nb_fields
; j
++) {
95 size
= thunk_type_size(type_ptr
, i
);
96 align
= thunk_type_align(type_ptr
, i
);
97 offset
= (offset
+ align
- 1) & ~(align
- 1);
98 se
->field_offsets
[i
][j
] = offset
;
100 if (align
> max_align
)
102 type_ptr
= thunk_type_next(type_ptr
);
104 offset
= (offset
+ max_align
- 1) & ~(max_align
- 1);
105 se
->size
[i
] = offset
;
106 se
->align
[i
] = max_align
;
108 printf("%s: size=%d align=%d\n",
109 i
== THUNK_HOST
? "host" : "target", offset
, max_align
);
114 void thunk_register_struct_direct(int id
, const char *name
,
115 const StructEntry
*se1
)
118 se
= struct_entries
+ id
;
124 /* now we can define the main conversion functions */
125 const argtype
*thunk_convert(void *dst
, const void *src
,
126 const argtype
*type_ptr
, int to_host
)
133 *(uint8_t *)dst
= *(uint8_t *)src
;
136 *(uint16_t *)dst
= tswap16(*(uint16_t *)src
);
139 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
143 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
145 #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
149 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
151 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
156 if (type
== TYPE_LONG
) {
158 *(uint64_t *)dst
= (int32_t)tswap32(*(uint32_t *)src
);
160 *(uint64_t *)dst
= tswap32(*(uint32_t *)src
);
163 *(uint32_t *)dst
= tswap32(*(uint64_t *)src
& 0xffffffff);
166 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
170 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
172 #elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
177 *(uint32_t *)dst
= tswap64(*(uint64_t *)src
);
179 if (type
== TYPE_LONG
) {
181 *(uint64_t *)dst
= tswap64(*(int32_t *)src
);
183 *(uint64_t *)dst
= tswap64(*(uint32_t *)src
);
188 #warning unsupported conversion
193 switch (thunk_type_size(type_ptr
- 1, !to_host
)) {
195 val
= *(uint16_t *)src
;
198 val
= *(uint32_t *)src
;
201 val
= *(uint64_t *)src
;
204 switch (thunk_type_size(type_ptr
- 1, to_host
)) {
206 *(uint16_t *)dst
= tswap16(val
);
209 *(uint32_t *)dst
= tswap32(val
);
212 *(uint64_t *)dst
= tswap64(val
);
219 int array_length
, i
, dst_size
, src_size
;
223 array_length
= *type_ptr
++;
224 dst_size
= thunk_type_size(type_ptr
, to_host
);
225 src_size
= thunk_type_size(type_ptr
, 1 - to_host
);
228 for(i
= 0;i
< array_length
; i
++) {
229 thunk_convert(d
, s
, type_ptr
, to_host
);
233 type_ptr
= thunk_type_next(type_ptr
);
239 const StructEntry
*se
;
242 const argtype
*field_types
;
243 const int *dst_offsets
, *src_offsets
;
245 se
= struct_entries
+ *type_ptr
++;
246 if (se
->convert
[0] != NULL
) {
247 /* specific conversion is needed */
248 (*se
->convert
[to_host
])(dst
, src
);
250 /* standard struct conversion */
251 field_types
= se
->field_types
;
252 dst_offsets
= se
->field_offsets
[to_host
];
253 src_offsets
= se
->field_offsets
[1 - to_host
];
256 for(i
= 0;i
< se
->nb_fields
; i
++) {
257 field_types
= thunk_convert(d
+ dst_offsets
[i
],
259 field_types
, to_host
);
265 fprintf(stderr
, "Invalid type 0x%x\n", type
);
273 /* Utility function: Table-driven functions to translate bitmasks
274 * between X86 and Alpha formats...
276 unsigned int target_to_host_bitmask(unsigned int x86_mask
,
277 const bitmask_transtbl
* trans_tbl
)
279 const bitmask_transtbl
*btp
;
280 unsigned int alpha_mask
= 0;
282 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
283 if((x86_mask
& btp
->x86_mask
) == btp
->x86_bits
) {
284 alpha_mask
|= btp
->alpha_bits
;
290 unsigned int host_to_target_bitmask(unsigned int alpha_mask
,
291 const bitmask_transtbl
* trans_tbl
)
293 const bitmask_transtbl
*btp
;
294 unsigned int x86_mask
= 0;
296 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
297 if((alpha_mask
& btp
->alpha_mask
) == btp
->alpha_bits
) {
298 x86_mask
|= btp
->x86_bits
;
304 #ifndef NO_THUNK_TYPE_SIZE
305 int thunk_type_size_array(const argtype
*type_ptr
, int is_host
)
307 return thunk_type_size(type_ptr
, is_host
);
310 int thunk_type_align_array(const argtype
*type_ptr
, int is_host
)
312 return thunk_type_align(type_ptr
, is_host
);
314 #endif /* ndef NO_THUNK_TYPE_SIZE */