Statistics
| Branch: | Revision:

root / thunk.c @ 55308450

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 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 31e31b8a bellard
    case TYPE_ARRAY:
193 31e31b8a bellard
        {
194 31e31b8a bellard
            int array_length, i, dst_size, src_size;
195 31e31b8a bellard
            const uint8_t *s;
196 31e31b8a bellard
            uint8_t  *d;
197 31e31b8a bellard
198 31e31b8a bellard
            array_length = *type_ptr++;
199 31e31b8a bellard
            dst_size = thunk_type_size(type_ptr, to_host);
200 31e31b8a bellard
            src_size = thunk_type_size(type_ptr, 1 - to_host);
201 31e31b8a bellard
            d = dst;
202 31e31b8a bellard
            s = src;
203 31e31b8a bellard
            for(i = 0;i < array_length; i++) {
204 31e31b8a bellard
                thunk_convert(d, s, type_ptr, to_host);
205 31e31b8a bellard
                d += dst_size;
206 31e31b8a bellard
                s += src_size;
207 31e31b8a bellard
            }
208 31e31b8a bellard
            type_ptr = thunk_type_next(type_ptr);
209 31e31b8a bellard
        }
210 31e31b8a bellard
        break;
211 31e31b8a bellard
    case TYPE_STRUCT:
212 31e31b8a bellard
        {
213 31e31b8a bellard
            int i;
214 31e31b8a bellard
            const StructEntry *se;
215 31e31b8a bellard
            const uint8_t *s;
216 31e31b8a bellard
            uint8_t  *d;
217 31e31b8a bellard
            const argtype *field_types;
218 31e31b8a bellard
            const int *dst_offsets, *src_offsets;
219 3b46e624 ths
220 31e31b8a bellard
            se = struct_entries + *type_ptr++;
221 31e31b8a bellard
            if (se->convert[0] != NULL) {
222 31e31b8a bellard
                /* specific conversion is needed */
223 31e31b8a bellard
                (*se->convert[to_host])(dst, src);
224 31e31b8a bellard
            } else {
225 31e31b8a bellard
                /* standard struct conversion */
226 31e31b8a bellard
                field_types = se->field_types;
227 31e31b8a bellard
                dst_offsets = se->field_offsets[to_host];
228 31e31b8a bellard
                src_offsets = se->field_offsets[1 - to_host];
229 31e31b8a bellard
                d = dst;
230 31e31b8a bellard
                s = src;
231 31e31b8a bellard
                for(i = 0;i < se->nb_fields; i++) {
232 5fafdf24 ths
                    field_types = thunk_convert(d + dst_offsets[i],
233 5fafdf24 ths
                                                s + src_offsets[i],
234 31e31b8a bellard
                                                field_types, to_host);
235 31e31b8a bellard
                }
236 31e31b8a bellard
            }
237 31e31b8a bellard
        }
238 31e31b8a bellard
        break;
239 31e31b8a bellard
    default:
240 31e31b8a bellard
        fprintf(stderr, "Invalid type 0x%x\n", type);
241 31e31b8a bellard
        break;
242 31e31b8a bellard
    }
243 31e31b8a bellard
    return type_ptr;
244 31e31b8a bellard
}
245 31e31b8a bellard
246 31e31b8a bellard
/* from em86 */
247 31e31b8a bellard
248 31e31b8a bellard
/* Utility function: Table-driven functions to translate bitmasks
249 31e31b8a bellard
 * between X86 and Alpha formats...
250 31e31b8a bellard
 */
251 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
252 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl)
253 31e31b8a bellard
{
254 b39bc503 blueswir1
    const bitmask_transtbl *btp;
255 31e31b8a bellard
    unsigned int        alpha_mask = 0;
256 31e31b8a bellard
257 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
258 31e31b8a bellard
        if((x86_mask & btp->x86_mask) == btp->x86_bits) {
259 31e31b8a bellard
            alpha_mask |= btp->alpha_bits;
260 31e31b8a bellard
        }
261 31e31b8a bellard
    }
262 31e31b8a bellard
    return(alpha_mask);
263 31e31b8a bellard
}
264 31e31b8a bellard
265 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
266 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl)
267 31e31b8a bellard
{
268 b39bc503 blueswir1
    const bitmask_transtbl *btp;
269 31e31b8a bellard
    unsigned int        x86_mask = 0;
270 31e31b8a bellard
271 31e31b8a bellard
    for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
272 31e31b8a bellard
        if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
273 7a987127 bellard
            x86_mask |= btp->x86_bits;
274 31e31b8a bellard
        }
275 31e31b8a bellard
    }
276 31e31b8a bellard
    return(x86_mask);
277 31e31b8a bellard
}
278 26553115 j_mayer
279 26553115 j_mayer
#ifndef NO_THUNK_TYPE_SIZE
280 26553115 j_mayer
int thunk_type_size_array(const argtype *type_ptr, int is_host)
281 26553115 j_mayer
{
282 26553115 j_mayer
    return thunk_type_size(type_ptr, is_host);
283 26553115 j_mayer
}
284 26553115 j_mayer
285 26553115 j_mayer
int thunk_type_align_array(const argtype *type_ptr, int is_host)
286 26553115 j_mayer
{
287 26553115 j_mayer
    return thunk_type_align(type_ptr, is_host);
288 26553115 j_mayer
}
289 26553115 j_mayer
#endif /* ndef NO_THUNK_TYPE_SIZE */