root / thunk.c @ d3c61721
History | View | Annotate | Download (6.9 kB)
1 | 31e31b8a | bellard | /*
|
---|---|---|---|
2 | 31e31b8a | bellard | * Generic thunking code to convert data between host and target CPU
|
3 | 31e31b8a | bellard | *
|
4 | 31e31b8a | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 31e31b8a | bellard | *
|
6 | 3ef693a0 | bellard | * This library is free software; you can redistribute it and/or
|
7 | 3ef693a0 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 3ef693a0 | bellard | * License as published by the Free Software Foundation; either
|
9 | 3ef693a0 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 31e31b8a | bellard | *
|
11 | 3ef693a0 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 3ef693a0 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3ef693a0 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 3ef693a0 | bellard | * Lesser General Public License for more details.
|
15 | 31e31b8a | bellard | *
|
16 | 3ef693a0 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 3ef693a0 | bellard | * License along with this library; if not, write to the Free Software
|
18 | 3ef693a0 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 31e31b8a | bellard | */
|
20 | 31e31b8a | bellard | #include <stdlib.h> |
21 | 31e31b8a | bellard | #include <stdio.h> |
22 | 31e31b8a | bellard | #include <stdarg.h> |
23 | 31e31b8a | bellard | |
24 | 3ef693a0 | bellard | #include "qemu.h" |
25 | 31e31b8a | bellard | #include "thunk.h" |
26 | 31e31b8a | bellard | |
27 | 31e31b8a | bellard | //#define DEBUG
|
28 | 31e31b8a | bellard | |
29 | 31e31b8a | bellard | #define MAX_STRUCTS 128 |
30 | 31e31b8a | bellard | |
31 | 31e31b8a | bellard | /* XXX: make it dynamic */
|
32 | 24374901 | bellard | StructEntry struct_entries[MAX_STRUCTS]; |
33 | 31e31b8a | bellard | |
34 | 31e31b8a | bellard | static inline const argtype *thunk_type_next(const argtype *type_ptr) |
35 | 31e31b8a | bellard | { |
36 | 31e31b8a | bellard | int type;
|
37 | 31e31b8a | bellard | |
38 | 31e31b8a | bellard | type = *type_ptr++; |
39 | 31e31b8a | bellard | switch(type) {
|
40 | 31e31b8a | bellard | case TYPE_CHAR:
|
41 | 31e31b8a | bellard | case TYPE_SHORT:
|
42 | 31e31b8a | bellard | case TYPE_INT:
|
43 | 31e31b8a | bellard | case TYPE_LONGLONG:
|
44 | 31e31b8a | bellard | case TYPE_ULONGLONG:
|
45 | 31e31b8a | bellard | case TYPE_LONG:
|
46 | 31e31b8a | bellard | case TYPE_ULONG:
|
47 | 31e31b8a | bellard | case TYPE_PTRVOID:
|
48 | 31e31b8a | bellard | return type_ptr;
|
49 | 31e31b8a | bellard | case TYPE_PTR:
|
50 | 31e31b8a | bellard | return thunk_type_next(type_ptr);
|
51 | 31e31b8a | bellard | case TYPE_ARRAY:
|
52 | 31e31b8a | bellard | return thunk_type_next(type_ptr + 1); |
53 | 31e31b8a | bellard | case TYPE_STRUCT:
|
54 | 31e31b8a | bellard | return type_ptr + 1; |
55 | 31e31b8a | bellard | default:
|
56 | 31e31b8a | bellard | return NULL; |
57 | 31e31b8a | bellard | } |
58 | 31e31b8a | bellard | } |
59 | 31e31b8a | bellard | |
60 | 31e31b8a | bellard | void thunk_register_struct(int id, const char *name, const argtype *types) |
61 | 31e31b8a | bellard | { |
62 | 31e31b8a | bellard | const argtype *type_ptr;
|
63 | 31e31b8a | bellard | StructEntry *se; |
64 | 31e31b8a | bellard | int nb_fields, offset, max_align, align, size, i, j;
|
65 | 31e31b8a | bellard | |
66 | 31e31b8a | bellard | se = struct_entries + id; |
67 | 31e31b8a | bellard | |
68 | 31e31b8a | bellard | /* first we count the number of fields */
|
69 | 31e31b8a | bellard | type_ptr = types; |
70 | 31e31b8a | bellard | nb_fields = 0;
|
71 | 31e31b8a | bellard | while (*type_ptr != TYPE_NULL) {
|
72 | 31e31b8a | bellard | type_ptr = thunk_type_next(type_ptr); |
73 | 31e31b8a | bellard | nb_fields++; |
74 | 31e31b8a | bellard | } |
75 | 31e31b8a | bellard | se->field_types = types; |
76 | 31e31b8a | bellard | se->nb_fields = nb_fields; |
77 | 31e31b8a | bellard | se->name = name; |
78 | 31e31b8a | bellard | #ifdef DEBUG
|
79 | 31e31b8a | bellard | printf("struct %s: id=%d nb_fields=%d\n",
|
80 | 31e31b8a | bellard | se->name, id, se->nb_fields); |
81 | 31e31b8a | bellard | #endif
|
82 | 31e31b8a | bellard | /* now we can alloc the data */
|
83 | 31e31b8a | bellard | |
84 | 31e31b8a | bellard | for(i = 0;i < 2; i++) { |
85 | 31e31b8a | bellard | offset = 0;
|
86 | 31e31b8a | bellard | max_align = 1;
|
87 | 31e31b8a | bellard | se->field_offsets[i] = malloc(nb_fields * sizeof(int)); |
88 | 31e31b8a | bellard | type_ptr = se->field_types; |
89 | 31e31b8a | bellard | for(j = 0;j < nb_fields; j++) { |
90 | 31e31b8a | bellard | size = thunk_type_size(type_ptr, i); |
91 | 31e31b8a | bellard | align = thunk_type_align(type_ptr, i); |
92 | 31e31b8a | bellard | offset = (offset + align - 1) & ~(align - 1); |
93 | 31e31b8a | bellard | se->field_offsets[i][j] = offset; |
94 | 31e31b8a | bellard | offset += size; |
95 | 31e31b8a | bellard | if (align > max_align)
|
96 | 31e31b8a | bellard | max_align = align; |
97 | 24374901 | bellard | type_ptr = thunk_type_next(type_ptr); |
98 | 31e31b8a | bellard | } |
99 | 31e31b8a | bellard | offset = (offset + max_align - 1) & ~(max_align - 1); |
100 | 31e31b8a | bellard | se->size[i] = offset; |
101 | 31e31b8a | bellard | se->align[i] = max_align; |
102 | 31e31b8a | bellard | #ifdef DEBUG
|
103 | 31e31b8a | bellard | printf("%s: size=%d align=%d\n",
|
104 | 31e31b8a | bellard | i == THUNK_HOST ? "host" : "target", offset, max_align); |
105 | 31e31b8a | bellard | #endif
|
106 | 31e31b8a | bellard | } |
107 | 31e31b8a | bellard | } |
108 | 31e31b8a | bellard | |
109 | 31e31b8a | bellard | void thunk_register_struct_direct(int id, const char *name, StructEntry *se1) |
110 | 31e31b8a | bellard | { |
111 | 31e31b8a | bellard | StructEntry *se; |
112 | 31e31b8a | bellard | se = struct_entries + id; |
113 | 31e31b8a | bellard | *se = *se1; |
114 | 31e31b8a | bellard | se->name = name; |
115 | 31e31b8a | bellard | } |
116 | 31e31b8a | bellard | |
117 | 31e31b8a | bellard | |
118 | 31e31b8a | bellard | /* now we can define the main conversion functions */
|
119 | 31e31b8a | bellard | const argtype *thunk_convert(void *dst, const void *src, |
120 | 31e31b8a | bellard | const argtype *type_ptr, int to_host) |
121 | 31e31b8a | bellard | { |
122 | 31e31b8a | bellard | int type;
|
123 | 31e31b8a | bellard | |
124 | 31e31b8a | bellard | type = *type_ptr++; |
125 | 31e31b8a | bellard | switch(type) {
|
126 | 31e31b8a | bellard | case TYPE_CHAR:
|
127 | 31e31b8a | bellard | *(uint8_t *)dst = *(uint8_t *)src; |
128 | 31e31b8a | bellard | break;
|
129 | 31e31b8a | bellard | case TYPE_SHORT:
|
130 | 31e31b8a | bellard | *(uint16_t *)dst = tswap16(*(uint16_t *)src); |
131 | 31e31b8a | bellard | break;
|
132 | 31e31b8a | bellard | case TYPE_INT:
|
133 | 31e31b8a | bellard | *(uint32_t *)dst = tswap32(*(uint32_t *)src); |
134 | 31e31b8a | bellard | break;
|
135 | 31e31b8a | bellard | case TYPE_LONGLONG:
|
136 | 31e31b8a | bellard | case TYPE_ULONGLONG:
|
137 | 31e31b8a | bellard | *(uint64_t *)dst = tswap64(*(uint64_t *)src); |
138 | 31e31b8a | bellard | break;
|
139 | 31e31b8a | bellard | #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32 |
140 | 31e31b8a | bellard | case TYPE_LONG:
|
141 | 31e31b8a | bellard | case TYPE_ULONG:
|
142 | 31e31b8a | bellard | case TYPE_PTRVOID:
|
143 | 31e31b8a | bellard | *(uint32_t *)dst = tswap32(*(uint32_t *)src); |
144 | 31e31b8a | bellard | break;
|
145 | 31e31b8a | bellard | #elif HOST_LONG_BITS == 64 && TARGET_LONG_BITS == 32 |
146 | 31e31b8a | bellard | case TYPE_LONG:
|
147 | 31e31b8a | bellard | case TYPE_ULONG:
|
148 | 31e31b8a | bellard | case TYPE_PTRVOID:
|
149 | d0cd3b8d | bellard | if (to_host) {
|
150 | 31e31b8a | bellard | *(uint64_t *)dst = tswap32(*(uint32_t *)src); |
151 | 31e31b8a | bellard | } else {
|
152 | 31e31b8a | bellard | *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff);
|
153 | 31e31b8a | bellard | } |
154 | 31e31b8a | bellard | break;
|
155 | 31e31b8a | bellard | #else
|
156 | 31e31b8a | bellard | #error unsupported conversion
|
157 | 31e31b8a | bellard | #endif
|
158 | 31e31b8a | bellard | case TYPE_ARRAY:
|
159 | 31e31b8a | bellard | { |
160 | 31e31b8a | bellard | int array_length, i, dst_size, src_size;
|
161 | 31e31b8a | bellard | const uint8_t *s;
|
162 | 31e31b8a | bellard | uint8_t *d; |
163 | 31e31b8a | bellard | |
164 | 31e31b8a | bellard | array_length = *type_ptr++; |
165 | 31e31b8a | bellard | dst_size = thunk_type_size(type_ptr, to_host); |
166 | 31e31b8a | bellard | src_size = thunk_type_size(type_ptr, 1 - to_host);
|
167 | 31e31b8a | bellard | d = dst; |
168 | 31e31b8a | bellard | s = src; |
169 | 31e31b8a | bellard | for(i = 0;i < array_length; i++) { |
170 | 31e31b8a | bellard | thunk_convert(d, s, type_ptr, to_host); |
171 | 31e31b8a | bellard | d += dst_size; |
172 | 31e31b8a | bellard | s += src_size; |
173 | 31e31b8a | bellard | } |
174 | 31e31b8a | bellard | type_ptr = thunk_type_next(type_ptr); |
175 | 31e31b8a | bellard | } |
176 | 31e31b8a | bellard | break;
|
177 | 31e31b8a | bellard | case TYPE_STRUCT:
|
178 | 31e31b8a | bellard | { |
179 | 31e31b8a | bellard | int i;
|
180 | 31e31b8a | bellard | const StructEntry *se;
|
181 | 31e31b8a | bellard | const uint8_t *s;
|
182 | 31e31b8a | bellard | uint8_t *d; |
183 | 31e31b8a | bellard | const argtype *field_types;
|
184 | 31e31b8a | bellard | const int *dst_offsets, *src_offsets; |
185 | 31e31b8a | bellard | |
186 | 31e31b8a | bellard | se = struct_entries + *type_ptr++; |
187 | 31e31b8a | bellard | if (se->convert[0] != NULL) { |
188 | 31e31b8a | bellard | /* specific conversion is needed */
|
189 | 31e31b8a | bellard | (*se->convert[to_host])(dst, src); |
190 | 31e31b8a | bellard | } else {
|
191 | 31e31b8a | bellard | /* standard struct conversion */
|
192 | 31e31b8a | bellard | field_types = se->field_types; |
193 | 31e31b8a | bellard | dst_offsets = se->field_offsets[to_host]; |
194 | 31e31b8a | bellard | src_offsets = se->field_offsets[1 - to_host];
|
195 | 31e31b8a | bellard | d = dst; |
196 | 31e31b8a | bellard | s = src; |
197 | 31e31b8a | bellard | for(i = 0;i < se->nb_fields; i++) { |
198 | 31e31b8a | bellard | field_types = thunk_convert(d + dst_offsets[i], |
199 | 31e31b8a | bellard | s + src_offsets[i], |
200 | 31e31b8a | bellard | field_types, to_host); |
201 | 31e31b8a | bellard | } |
202 | 31e31b8a | bellard | } |
203 | 31e31b8a | bellard | } |
204 | 31e31b8a | bellard | break;
|
205 | 31e31b8a | bellard | default:
|
206 | 31e31b8a | bellard | fprintf(stderr, "Invalid type 0x%x\n", type);
|
207 | 31e31b8a | bellard | break;
|
208 | 31e31b8a | bellard | } |
209 | 31e31b8a | bellard | return type_ptr;
|
210 | 31e31b8a | bellard | } |
211 | 31e31b8a | bellard | |
212 | 31e31b8a | bellard | /* from em86 */
|
213 | 31e31b8a | bellard | |
214 | 31e31b8a | bellard | /* Utility function: Table-driven functions to translate bitmasks
|
215 | 31e31b8a | bellard | * between X86 and Alpha formats...
|
216 | 31e31b8a | bellard | */
|
217 | 31e31b8a | bellard | unsigned int target_to_host_bitmask(unsigned int x86_mask, |
218 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl) |
219 | 31e31b8a | bellard | { |
220 | 31e31b8a | bellard | bitmask_transtbl * btp; |
221 | 31e31b8a | bellard | unsigned int alpha_mask = 0; |
222 | 31e31b8a | bellard | |
223 | 31e31b8a | bellard | for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
|
224 | 31e31b8a | bellard | if((x86_mask & btp->x86_mask) == btp->x86_bits) {
|
225 | 31e31b8a | bellard | alpha_mask |= btp->alpha_bits; |
226 | 31e31b8a | bellard | } |
227 | 31e31b8a | bellard | } |
228 | 31e31b8a | bellard | return(alpha_mask);
|
229 | 31e31b8a | bellard | } |
230 | 31e31b8a | bellard | |
231 | 31e31b8a | bellard | unsigned int host_to_target_bitmask(unsigned int alpha_mask, |
232 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl) |
233 | 31e31b8a | bellard | { |
234 | 31e31b8a | bellard | bitmask_transtbl * btp; |
235 | 31e31b8a | bellard | unsigned int x86_mask = 0; |
236 | 31e31b8a | bellard | |
237 | 31e31b8a | bellard | for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
|
238 | 31e31b8a | bellard | if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
|
239 | 7a987127 | bellard | x86_mask |= btp->x86_bits; |
240 | 31e31b8a | bellard | } |
241 | 31e31b8a | bellard | } |
242 | 31e31b8a | bellard | return(x86_mask);
|
243 | 31e31b8a | bellard | } |