Statistics
| Branch: | Revision:

root / thunk.c @ 57f45b62

History | View | Annotate | Download (8.8 kB)

1 31e31b8a bellard
/*
2 31e31b8a bellard
 *  Generic thunking code to convert data between host and target CPU
3 5fafdf24 ths
 *
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 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 31e31b8a bellard
 */
19 31e31b8a bellard
#include <stdlib.h>
20 31e31b8a bellard
#include <stdio.h>
21 31e31b8a bellard
#include <stdarg.h>
22 31e31b8a bellard
23 3ef693a0 bellard
#include "qemu.h"
24 022c62cb Paolo Bonzini
#include "exec/user/thunk.h"
25 31e31b8a bellard
26 31e31b8a bellard
//#define DEBUG
27 31e31b8a bellard
28 31e31b8a bellard
#define MAX_STRUCTS 128
29 31e31b8a bellard
30 31e31b8a bellard
/* XXX: make it dynamic */
31 24374901 bellard
StructEntry struct_entries[MAX_STRUCTS];
32 31e31b8a bellard
33 26553115 j_mayer
static const argtype *thunk_type_next_ptr(const argtype *type_ptr);
34 26553115 j_mayer
35 31e31b8a bellard
static inline const argtype *thunk_type_next(const argtype *type_ptr)
36 31e31b8a bellard
{
37 31e31b8a bellard
    int type;
38 31e31b8a bellard
39 31e31b8a bellard
    type = *type_ptr++;
40 31e31b8a bellard
    switch(type) {
41 31e31b8a bellard
    case TYPE_CHAR:
42 31e31b8a bellard
    case TYPE_SHORT:
43 31e31b8a bellard
    case TYPE_INT:
44 31e31b8a bellard
    case TYPE_LONGLONG:
45 31e31b8a bellard
    case TYPE_ULONGLONG:
46 31e31b8a bellard
    case TYPE_LONG:
47 31e31b8a bellard
    case TYPE_ULONG:
48 31e31b8a bellard
    case TYPE_PTRVOID:
49 6083abd9 Alexander Graf
    case TYPE_OLDDEVT:
50 31e31b8a bellard
        return type_ptr;
51 31e31b8a bellard
    case TYPE_PTR:
52 26553115 j_mayer
        return thunk_type_next_ptr(type_ptr);
53 31e31b8a bellard
    case TYPE_ARRAY:
54 26553115 j_mayer
        return thunk_type_next_ptr(type_ptr + 1);
55 31e31b8a bellard
    case TYPE_STRUCT:
56 31e31b8a bellard
        return type_ptr + 1;
57 31e31b8a bellard
    default:
58 31e31b8a bellard
        return NULL;
59 31e31b8a bellard
    }
60 31e31b8a bellard
}
61 31e31b8a bellard
62 26553115 j_mayer
static const argtype *thunk_type_next_ptr(const argtype *type_ptr)
63 26553115 j_mayer
{
64 26553115 j_mayer
    return thunk_type_next(type_ptr);
65 26553115 j_mayer
}
66 26553115 j_mayer
67 31e31b8a bellard
void thunk_register_struct(int id, const char *name, const argtype *types)
68 31e31b8a bellard
{
69 31e31b8a bellard
    const argtype *type_ptr;
70 31e31b8a bellard
    StructEntry *se;
71 31e31b8a bellard
    int nb_fields, offset, max_align, align, size, i, j;
72 31e31b8a bellard
73 31e31b8a bellard
    se = struct_entries + id;
74 3b46e624 ths
75 31e31b8a bellard
    /* first we count the number of fields */
76 31e31b8a bellard
    type_ptr = types;
77 31e31b8a bellard
    nb_fields = 0;
78 31e31b8a bellard
    while (*type_ptr != TYPE_NULL) {
79 31e31b8a bellard
        type_ptr = thunk_type_next(type_ptr);
80 31e31b8a bellard
        nb_fields++;
81 31e31b8a bellard
    }
82 31e31b8a bellard
    se->field_types = types;
83 31e31b8a bellard
    se->nb_fields = nb_fields;
84 31e31b8a bellard
    se->name = name;
85 31e31b8a bellard
#ifdef DEBUG
86 5fafdf24 ths
    printf("struct %s: id=%d nb_fields=%d\n",
87 31e31b8a bellard
           se->name, id, se->nb_fields);
88 31e31b8a bellard
#endif
89 31e31b8a bellard
    /* now we can alloc the data */
90 31e31b8a bellard
91 31e31b8a bellard
    for(i = 0;i < 2; i++) {
92 31e31b8a bellard
        offset = 0;
93 31e31b8a bellard
        max_align = 1;
94 31e31b8a bellard
        se->field_offsets[i] = malloc(nb_fields * sizeof(int));
95 31e31b8a bellard
        type_ptr = se->field_types;
96 31e31b8a bellard
        for(j = 0;j < nb_fields; j++) {
97 31e31b8a bellard
            size = thunk_type_size(type_ptr, i);
98 31e31b8a bellard
            align = thunk_type_align(type_ptr, i);
99 31e31b8a bellard
            offset = (offset + align - 1) & ~(align - 1);
100 31e31b8a bellard
            se->field_offsets[i][j] = offset;
101 31e31b8a bellard
            offset += size;
102 31e31b8a bellard
            if (align > max_align)
103 31e31b8a bellard
                max_align = align;
104 24374901 bellard
            type_ptr = thunk_type_next(type_ptr);
105 31e31b8a bellard
        }
106 31e31b8a bellard
        offset = (offset + max_align - 1) & ~(max_align - 1);
107 31e31b8a bellard
        se->size[i] = offset;
108 31e31b8a bellard
        se->align[i] = max_align;
109 31e31b8a bellard
#ifdef DEBUG
110 5fafdf24 ths
        printf("%s: size=%d align=%d\n",
111 31e31b8a bellard
               i == THUNK_HOST ? "host" : "target", offset, max_align);
112 31e31b8a bellard
#endif
113 31e31b8a bellard
    }
114 31e31b8a bellard
}
115 31e31b8a bellard
116 8e853dc7 blueswir1
void thunk_register_struct_direct(int id, const char *name,
117 8e853dc7 blueswir1
                                  const StructEntry *se1)
118 31e31b8a bellard
{
119 31e31b8a bellard
    StructEntry *se;
120 31e31b8a bellard
    se = struct_entries + id;
121 31e31b8a bellard
    *se = *se1;
122 31e31b8a bellard
    se->name = name;
123 31e31b8a bellard
}
124 31e31b8a bellard
125 31e31b8a bellard
126 31e31b8a bellard
/* now we can define the main conversion functions */
127 5fafdf24 ths
const argtype *thunk_convert(void *dst, const void *src,
128 31e31b8a bellard
                             const argtype *type_ptr, int to_host)
129 31e31b8a bellard
{
130 31e31b8a bellard
    int type;
131 31e31b8a bellard
132 31e31b8a bellard
    type = *type_ptr++;
133 31e31b8a bellard
    switch(type) {
134 31e31b8a bellard
    case TYPE_CHAR:
135 31e31b8a bellard
        *(uint8_t *)dst = *(uint8_t *)src;
136 31e31b8a bellard
        break;
137 31e31b8a bellard
    case TYPE_SHORT:
138 31e31b8a bellard
        *(uint16_t *)dst = tswap16(*(uint16_t *)src);
139 31e31b8a bellard
        break;
140 31e31b8a bellard
    case TYPE_INT:
141 31e31b8a bellard
        *(uint32_t *)dst = tswap32(*(uint32_t *)src);
142 31e31b8a bellard
        break;
143 31e31b8a bellard
    case TYPE_LONGLONG:
144 31e31b8a bellard
    case TYPE_ULONGLONG:
145 31e31b8a bellard
        *(uint64_t *)dst = tswap64(*(uint64_t *)src);
146 31e31b8a bellard
        break;
147 992f48a0 blueswir1
#if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
148 31e31b8a bellard
    case TYPE_LONG:
149 31e31b8a bellard
    case TYPE_ULONG:
150 31e31b8a bellard
    case TYPE_PTRVOID:
151 31e31b8a bellard
        *(uint32_t *)dst = tswap32(*(uint32_t *)src);
152 31e31b8a bellard
        break;
153 992f48a0 blueswir1
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
154 31e31b8a bellard
    case TYPE_LONG:
155 31e31b8a bellard
    case TYPE_ULONG:
156 31e31b8a bellard
    case TYPE_PTRVOID:
157 d0cd3b8d bellard
        if (to_host) {
158 70499c98 bellard
            if (type == TYPE_LONG) {
159 70499c98 bellard
                /* sign extension */
160 70499c98 bellard
                *(uint64_t *)dst = (int32_t)tswap32(*(uint32_t *)src);
161 70499c98 bellard
            } else {
162 70499c98 bellard
                *(uint64_t *)dst = tswap32(*(uint32_t *)src);
163 70499c98 bellard
            }
164 31e31b8a bellard
        } else {
165 31e31b8a bellard
            *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff);
166 31e31b8a bellard
        }
167 31e31b8a bellard
        break;
168 70499c98 bellard
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
169 70499c98 bellard
    case TYPE_LONG:
170 70499c98 bellard
    case TYPE_ULONG:
171 70499c98 bellard
    case TYPE_PTRVOID:
172 70499c98 bellard
        *(uint64_t *)dst = tswap64(*(uint64_t *)src);
173 70499c98 bellard
        break;
174 70499c98 bellard
#elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
175 70499c98 bellard
    case TYPE_LONG:
176 70499c98 bellard
    case TYPE_ULONG:
177 70499c98 bellard
    case TYPE_PTRVOID:
178 70499c98 bellard
        if (to_host) {
179 70499c98 bellard
            *(uint32_t *)dst = tswap64(*(uint64_t *)src);
180 70499c98 bellard
        } else {
181 70499c98 bellard
            if (type == TYPE_LONG) {
182 70499c98 bellard
                /* sign extension */
183 70499c98 bellard
                *(uint64_t *)dst = tswap64(*(int32_t *)src);
184 70499c98 bellard
            } else {
185 70499c98 bellard
                *(uint64_t *)dst = tswap64(*(uint32_t *)src);
186 70499c98 bellard
            }
187 70499c98 bellard
        }
188 70499c98 bellard
        break;
189 31e31b8a bellard
#else
190 64b3ab24 bellard
#warning unsupported conversion
191 31e31b8a bellard
#endif
192 6083abd9 Alexander Graf
    case TYPE_OLDDEVT:
193 6083abd9 Alexander Graf
    {
194 6083abd9 Alexander Graf
        uint64_t val = 0;
195 6083abd9 Alexander Graf
        switch (thunk_type_size(type_ptr - 1, !to_host)) {
196 6083abd9 Alexander Graf
        case 2:
197 6083abd9 Alexander Graf
            val = *(uint16_t *)src;
198 6083abd9 Alexander Graf
            break;
199 6083abd9 Alexander Graf
        case 4:
200 6083abd9 Alexander Graf
            val = *(uint32_t *)src;
201 6083abd9 Alexander Graf
            break;
202 6083abd9 Alexander Graf
        case 8:
203 6083abd9 Alexander Graf
            val = *(uint64_t *)src;
204 6083abd9 Alexander Graf
            break;
205 6083abd9 Alexander Graf
        }
206 6083abd9 Alexander Graf
        switch (thunk_type_size(type_ptr - 1, to_host)) {
207 6083abd9 Alexander Graf
        case 2:
208 6083abd9 Alexander Graf
            *(uint16_t *)dst = tswap16(val);
209 6083abd9 Alexander Graf
            break;
210 6083abd9 Alexander Graf
        case 4:
211 6083abd9 Alexander Graf
            *(uint32_t *)dst = tswap32(val);
212 6083abd9 Alexander Graf
            break;
213 6083abd9 Alexander Graf
        case 8:
214 6083abd9 Alexander Graf
            *(uint64_t *)dst = tswap64(val);
215 6083abd9 Alexander Graf
            break;
216 6083abd9 Alexander Graf
        }
217 6083abd9 Alexander Graf
        break;
218 6083abd9 Alexander Graf
    }
219 31e31b8a bellard
    case TYPE_ARRAY:
220 31e31b8a bellard
        {
221 31e31b8a bellard
            int array_length, i, dst_size, src_size;
222 31e31b8a bellard
            const uint8_t *s;
223 31e31b8a bellard
            uint8_t  *d;
224 31e31b8a bellard
225 31e31b8a bellard
            array_length = *type_ptr++;
226 31e31b8a bellard
            dst_size = thunk_type_size(type_ptr, to_host);
227 31e31b8a bellard
            src_size = thunk_type_size(type_ptr, 1 - to_host);
228 31e31b8a bellard
            d = dst;
229 31e31b8a bellard
            s = src;
230 31e31b8a bellard
            for(i = 0;i < array_length; i++) {
231 31e31b8a bellard
                thunk_convert(d, s, type_ptr, to_host);
232 31e31b8a bellard
                d += dst_size;
233 31e31b8a bellard
                s += src_size;
234 31e31b8a bellard
            }
235 31e31b8a bellard
            type_ptr = thunk_type_next(type_ptr);
236 31e31b8a bellard
        }
237 31e31b8a bellard
        break;
238 31e31b8a bellard
    case TYPE_STRUCT:
239 31e31b8a bellard
        {
240 31e31b8a bellard
            int i;
241 31e31b8a bellard
            const StructEntry *se;
242 31e31b8a bellard
            const uint8_t *s;
243 31e31b8a bellard
            uint8_t  *d;
244 31e31b8a bellard
            const argtype *field_types;
245 31e31b8a bellard
            const int *dst_offsets, *src_offsets;
246 3b46e624 ths
247 31e31b8a bellard
            se = struct_entries + *type_ptr++;
248 31e31b8a bellard
            if (se->convert[0] != NULL) {
249 31e31b8a bellard
                /* specific conversion is needed */
250 31e31b8a bellard
                (*se->convert[to_host])(dst, src);
251 31e31b8a bellard
            } else {
252 31e31b8a bellard
                /* standard struct conversion */
253 31e31b8a bellard
                field_types = se->field_types;
254 31e31b8a bellard
                dst_offsets = se->field_offsets[to_host];
255 31e31b8a bellard
                src_offsets = se->field_offsets[1 - to_host];
256 31e31b8a bellard
                d = dst;
257 31e31b8a bellard
                s = src;
258 31e31b8a bellard
                for(i = 0;i < se->nb_fields; i++) {
259 5fafdf24 ths
                    field_types = thunk_convert(d + dst_offsets[i],
260 5fafdf24 ths
                                                s + src_offsets[i],
261 31e31b8a bellard
                                                field_types, to_host);
262 31e31b8a bellard
                }
263 31e31b8a bellard
            }
264 31e31b8a bellard
        }
265 31e31b8a bellard
        break;
266 31e31b8a bellard
    default:
267 31e31b8a bellard
        fprintf(stderr, "Invalid type 0x%x\n", type);
268 31e31b8a bellard
        break;
269 31e31b8a bellard
    }
270 31e31b8a bellard
    return type_ptr;
271 31e31b8a bellard
}
272 31e31b8a bellard
273 31e31b8a bellard
/* from em86 */
274 31e31b8a bellard
275 31e31b8a bellard
/* Utility function: Table-driven functions to translate bitmasks
276 31e31b8a bellard
 * between X86 and Alpha formats...
277 31e31b8a bellard
 */
278 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
279 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl)
280 31e31b8a bellard
{
281 b39bc503 blueswir1
    const bitmask_transtbl *btp;
282 31e31b8a bellard
    unsigned int        alpha_mask = 0;
283 31e31b8a bellard
284 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
285 31e31b8a bellard
        if((x86_mask & btp->x86_mask) == btp->x86_bits) {
286 31e31b8a bellard
            alpha_mask |= btp->alpha_bits;
287 31e31b8a bellard
        }
288 31e31b8a bellard
    }
289 31e31b8a bellard
    return(alpha_mask);
290 31e31b8a bellard
}
291 31e31b8a bellard
292 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
293 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl)
294 31e31b8a bellard
{
295 b39bc503 blueswir1
    const bitmask_transtbl *btp;
296 31e31b8a bellard
    unsigned int        x86_mask = 0;
297 31e31b8a bellard
298 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
299 31e31b8a bellard
        if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
300 7a987127 bellard
            x86_mask |= btp->x86_bits;
301 31e31b8a bellard
        }
302 31e31b8a bellard
    }
303 31e31b8a bellard
    return(x86_mask);
304 31e31b8a bellard
}
305 26553115 j_mayer
306 26553115 j_mayer
#ifndef NO_THUNK_TYPE_SIZE
307 26553115 j_mayer
int thunk_type_size_array(const argtype *type_ptr, int is_host)
308 26553115 j_mayer
{
309 26553115 j_mayer
    return thunk_type_size(type_ptr, is_host);
310 26553115 j_mayer
}
311 26553115 j_mayer
312 26553115 j_mayer
int thunk_type_align_array(const argtype *type_ptr, int is_host)
313 26553115 j_mayer
{
314 26553115 j_mayer
    return thunk_type_align(type_ptr, is_host);
315 26553115 j_mayer
}
316 26553115 j_mayer
#endif /* ndef NO_THUNK_TYPE_SIZE */