Statistics
| Branch: | Revision:

root / thunk.c @ 8c5e95d8

History | View | Annotate | Download (8.2 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 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 26553115 j_mayer
static const argtype *thunk_type_next_ptr(const argtype *type_ptr);
35 26553115 j_mayer
36 31e31b8a bellard
static inline const argtype *thunk_type_next(const argtype *type_ptr)
37 31e31b8a bellard
{
38 31e31b8a bellard
    int type;
39 31e31b8a bellard
40 31e31b8a bellard
    type = *type_ptr++;
41 31e31b8a bellard
    switch(type) {
42 31e31b8a bellard
    case TYPE_CHAR:
43 31e31b8a bellard
    case TYPE_SHORT:
44 31e31b8a bellard
    case TYPE_INT:
45 31e31b8a bellard
    case TYPE_LONGLONG:
46 31e31b8a bellard
    case TYPE_ULONGLONG:
47 31e31b8a bellard
    case TYPE_LONG:
48 31e31b8a bellard
    case TYPE_ULONG:
49 31e31b8a bellard
    case TYPE_PTRVOID:
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 31e31b8a bellard
void thunk_register_struct_direct(int id, const char *name, StructEntry *se1)
117 31e31b8a bellard
{
118 31e31b8a bellard
    StructEntry *se;
119 31e31b8a bellard
    se = struct_entries + id;
120 31e31b8a bellard
    *se = *se1;
121 31e31b8a bellard
    se->name = name;
122 31e31b8a bellard
}
123 31e31b8a bellard
124 31e31b8a bellard
125 31e31b8a bellard
/* now we can define the main conversion functions */
126 5fafdf24 ths
const argtype *thunk_convert(void *dst, const void *src,
127 31e31b8a bellard
                             const argtype *type_ptr, int to_host)
128 31e31b8a bellard
{
129 31e31b8a bellard
    int type;
130 31e31b8a bellard
131 31e31b8a bellard
    type = *type_ptr++;
132 31e31b8a bellard
    switch(type) {
133 31e31b8a bellard
    case TYPE_CHAR:
134 31e31b8a bellard
        *(uint8_t *)dst = *(uint8_t *)src;
135 31e31b8a bellard
        break;
136 31e31b8a bellard
    case TYPE_SHORT:
137 31e31b8a bellard
        *(uint16_t *)dst = tswap16(*(uint16_t *)src);
138 31e31b8a bellard
        break;
139 31e31b8a bellard
    case TYPE_INT:
140 31e31b8a bellard
        *(uint32_t *)dst = tswap32(*(uint32_t *)src);
141 31e31b8a bellard
        break;
142 31e31b8a bellard
    case TYPE_LONGLONG:
143 31e31b8a bellard
    case TYPE_ULONGLONG:
144 31e31b8a bellard
        *(uint64_t *)dst = tswap64(*(uint64_t *)src);
145 31e31b8a bellard
        break;
146 992f48a0 blueswir1
#if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
147 31e31b8a bellard
    case TYPE_LONG:
148 31e31b8a bellard
    case TYPE_ULONG:
149 31e31b8a bellard
    case TYPE_PTRVOID:
150 31e31b8a bellard
        *(uint32_t *)dst = tswap32(*(uint32_t *)src);
151 31e31b8a bellard
        break;
152 992f48a0 blueswir1
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
153 31e31b8a bellard
    case TYPE_LONG:
154 31e31b8a bellard
    case TYPE_ULONG:
155 31e31b8a bellard
    case TYPE_PTRVOID:
156 d0cd3b8d bellard
        if (to_host) {
157 70499c98 bellard
            if (type == TYPE_LONG) {
158 70499c98 bellard
                /* sign extension */
159 70499c98 bellard
                *(uint64_t *)dst = (int32_t)tswap32(*(uint32_t *)src);
160 70499c98 bellard
            } else {
161 70499c98 bellard
                *(uint64_t *)dst = tswap32(*(uint32_t *)src);
162 70499c98 bellard
            }
163 31e31b8a bellard
        } else {
164 31e31b8a bellard
            *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff);
165 31e31b8a bellard
        }
166 31e31b8a bellard
        break;
167 70499c98 bellard
#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
168 70499c98 bellard
    case TYPE_LONG:
169 70499c98 bellard
    case TYPE_ULONG:
170 70499c98 bellard
    case TYPE_PTRVOID:
171 70499c98 bellard
        *(uint64_t *)dst = tswap64(*(uint64_t *)src);
172 70499c98 bellard
        break;
173 70499c98 bellard
#elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
174 70499c98 bellard
    case TYPE_LONG:
175 70499c98 bellard
    case TYPE_ULONG:
176 70499c98 bellard
    case TYPE_PTRVOID:
177 70499c98 bellard
        if (to_host) {
178 70499c98 bellard
            *(uint32_t *)dst = tswap64(*(uint64_t *)src);
179 70499c98 bellard
        } else {
180 70499c98 bellard
            if (type == TYPE_LONG) {
181 70499c98 bellard
                /* sign extension */
182 70499c98 bellard
                *(uint64_t *)dst = tswap64(*(int32_t *)src);
183 70499c98 bellard
            } else {
184 70499c98 bellard
                *(uint64_t *)dst = tswap64(*(uint32_t *)src);
185 70499c98 bellard
            }
186 70499c98 bellard
        }
187 70499c98 bellard
        break;
188 31e31b8a bellard
#else
189 64b3ab24 bellard
#warning unsupported conversion
190 31e31b8a bellard
#endif
191 31e31b8a bellard
    case TYPE_ARRAY:
192 31e31b8a bellard
        {
193 31e31b8a bellard
            int array_length, i, dst_size, src_size;
194 31e31b8a bellard
            const uint8_t *s;
195 31e31b8a bellard
            uint8_t  *d;
196 31e31b8a bellard
197 31e31b8a bellard
            array_length = *type_ptr++;
198 31e31b8a bellard
            dst_size = thunk_type_size(type_ptr, to_host);
199 31e31b8a bellard
            src_size = thunk_type_size(type_ptr, 1 - to_host);
200 31e31b8a bellard
            d = dst;
201 31e31b8a bellard
            s = src;
202 31e31b8a bellard
            for(i = 0;i < array_length; i++) {
203 31e31b8a bellard
                thunk_convert(d, s, type_ptr, to_host);
204 31e31b8a bellard
                d += dst_size;
205 31e31b8a bellard
                s += src_size;
206 31e31b8a bellard
            }
207 31e31b8a bellard
            type_ptr = thunk_type_next(type_ptr);
208 31e31b8a bellard
        }
209 31e31b8a bellard
        break;
210 31e31b8a bellard
    case TYPE_STRUCT:
211 31e31b8a bellard
        {
212 31e31b8a bellard
            int i;
213 31e31b8a bellard
            const StructEntry *se;
214 31e31b8a bellard
            const uint8_t *s;
215 31e31b8a bellard
            uint8_t  *d;
216 31e31b8a bellard
            const argtype *field_types;
217 31e31b8a bellard
            const int *dst_offsets, *src_offsets;
218 3b46e624 ths
219 31e31b8a bellard
            se = struct_entries + *type_ptr++;
220 31e31b8a bellard
            if (se->convert[0] != NULL) {
221 31e31b8a bellard
                /* specific conversion is needed */
222 31e31b8a bellard
                (*se->convert[to_host])(dst, src);
223 31e31b8a bellard
            } else {
224 31e31b8a bellard
                /* standard struct conversion */
225 31e31b8a bellard
                field_types = se->field_types;
226 31e31b8a bellard
                dst_offsets = se->field_offsets[to_host];
227 31e31b8a bellard
                src_offsets = se->field_offsets[1 - to_host];
228 31e31b8a bellard
                d = dst;
229 31e31b8a bellard
                s = src;
230 31e31b8a bellard
                for(i = 0;i < se->nb_fields; i++) {
231 5fafdf24 ths
                    field_types = thunk_convert(d + dst_offsets[i],
232 5fafdf24 ths
                                                s + src_offsets[i],
233 31e31b8a bellard
                                                field_types, to_host);
234 31e31b8a bellard
                }
235 31e31b8a bellard
            }
236 31e31b8a bellard
        }
237 31e31b8a bellard
        break;
238 31e31b8a bellard
    default:
239 31e31b8a bellard
        fprintf(stderr, "Invalid type 0x%x\n", type);
240 31e31b8a bellard
        break;
241 31e31b8a bellard
    }
242 31e31b8a bellard
    return type_ptr;
243 31e31b8a bellard
}
244 31e31b8a bellard
245 31e31b8a bellard
/* from em86 */
246 31e31b8a bellard
247 31e31b8a bellard
/* Utility function: Table-driven functions to translate bitmasks
248 31e31b8a bellard
 * between X86 and Alpha formats...
249 31e31b8a bellard
 */
250 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
251 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl)
252 31e31b8a bellard
{
253 31e31b8a bellard
    bitmask_transtbl *        btp;
254 31e31b8a bellard
    unsigned int        alpha_mask = 0;
255 31e31b8a bellard
256 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
257 31e31b8a bellard
        if((x86_mask & btp->x86_mask) == btp->x86_bits) {
258 31e31b8a bellard
            alpha_mask |= btp->alpha_bits;
259 31e31b8a bellard
        }
260 31e31b8a bellard
    }
261 31e31b8a bellard
    return(alpha_mask);
262 31e31b8a bellard
}
263 31e31b8a bellard
264 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
265 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl)
266 31e31b8a bellard
{
267 31e31b8a bellard
    bitmask_transtbl *        btp;
268 31e31b8a bellard
    unsigned int        x86_mask = 0;
269 31e31b8a bellard
270 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
271 31e31b8a bellard
        if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
272 7a987127 bellard
            x86_mask |= btp->x86_bits;
273 31e31b8a bellard
        }
274 31e31b8a bellard
    }
275 31e31b8a bellard
    return(x86_mask);
276 31e31b8a bellard
}
277 26553115 j_mayer
278 26553115 j_mayer
#ifndef NO_THUNK_TYPE_SIZE
279 26553115 j_mayer
int thunk_type_size_array(const argtype *type_ptr, int is_host)
280 26553115 j_mayer
{
281 26553115 j_mayer
    return thunk_type_size(type_ptr, is_host);
282 26553115 j_mayer
}
283 26553115 j_mayer
284 26553115 j_mayer
int thunk_type_align_array(const argtype *type_ptr, int is_host)
285 26553115 j_mayer
{
286 26553115 j_mayer
    return thunk_type_align(type_ptr, is_host);
287 26553115 j_mayer
}
288 26553115 j_mayer
#endif /* ndef NO_THUNK_TYPE_SIZE */