1*4e111653SPhilippe Mathieu-Daudé /*
2*4e111653SPhilippe Mathieu-Daudé * Generic thunking code to convert data between host and target CPU
3*4e111653SPhilippe Mathieu-Daudé *
4*4e111653SPhilippe Mathieu-Daudé * Copyright (c) 2003 Fabrice Bellard
5*4e111653SPhilippe Mathieu-Daudé *
6*4e111653SPhilippe Mathieu-Daudé * This library is free software; you can redistribute it and/or
7*4e111653SPhilippe Mathieu-Daudé * modify it under the terms of the GNU Lesser General Public
8*4e111653SPhilippe Mathieu-Daudé * License as published by the Free Software Foundation; either
9*4e111653SPhilippe Mathieu-Daudé * version 2.1 of the License, or (at your option) any later version.
10*4e111653SPhilippe Mathieu-Daudé *
11*4e111653SPhilippe Mathieu-Daudé * This library is distributed in the hope that it will be useful,
12*4e111653SPhilippe Mathieu-Daudé * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*4e111653SPhilippe Mathieu-Daudé * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*4e111653SPhilippe Mathieu-Daudé * Lesser General Public License for more details.
15*4e111653SPhilippe Mathieu-Daudé *
16*4e111653SPhilippe Mathieu-Daudé * You should have received a copy of the GNU Lesser General Public
17*4e111653SPhilippe Mathieu-Daudé * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18*4e111653SPhilippe Mathieu-Daudé */
19*4e111653SPhilippe Mathieu-Daudé
20*4e111653SPhilippe Mathieu-Daudé #ifndef USER_THUNK_H
21*4e111653SPhilippe Mathieu-Daudé #define USER_THUNK_H
22*4e111653SPhilippe Mathieu-Daudé
23*4e111653SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY
24*4e111653SPhilippe Mathieu-Daudé #error Cannot include this header from system emulation
25*4e111653SPhilippe Mathieu-Daudé #endif
26*4e111653SPhilippe Mathieu-Daudé
27*4e111653SPhilippe Mathieu-Daudé #include "cpu.h"
28*4e111653SPhilippe Mathieu-Daudé #include "user/abitypes.h"
29*4e111653SPhilippe Mathieu-Daudé
30*4e111653SPhilippe Mathieu-Daudé /* types enums definitions */
31*4e111653SPhilippe Mathieu-Daudé
32*4e111653SPhilippe Mathieu-Daudé typedef enum argtype {
33*4e111653SPhilippe Mathieu-Daudé TYPE_NULL,
34*4e111653SPhilippe Mathieu-Daudé TYPE_CHAR,
35*4e111653SPhilippe Mathieu-Daudé TYPE_SHORT,
36*4e111653SPhilippe Mathieu-Daudé TYPE_INT,
37*4e111653SPhilippe Mathieu-Daudé TYPE_LONG,
38*4e111653SPhilippe Mathieu-Daudé TYPE_ULONG,
39*4e111653SPhilippe Mathieu-Daudé TYPE_PTRVOID, /* pointer on unknown data */
40*4e111653SPhilippe Mathieu-Daudé TYPE_LONGLONG,
41*4e111653SPhilippe Mathieu-Daudé TYPE_ULONGLONG,
42*4e111653SPhilippe Mathieu-Daudé TYPE_PTR,
43*4e111653SPhilippe Mathieu-Daudé TYPE_ARRAY,
44*4e111653SPhilippe Mathieu-Daudé TYPE_STRUCT,
45*4e111653SPhilippe Mathieu-Daudé TYPE_OLDDEVT,
46*4e111653SPhilippe Mathieu-Daudé } argtype;
47*4e111653SPhilippe Mathieu-Daudé
48*4e111653SPhilippe Mathieu-Daudé #define MK_PTR(type) TYPE_PTR, type
49*4e111653SPhilippe Mathieu-Daudé #define MK_ARRAY(type, size) TYPE_ARRAY, (int)(size), type
50*4e111653SPhilippe Mathieu-Daudé #define MK_STRUCT(id) TYPE_STRUCT, id
51*4e111653SPhilippe Mathieu-Daudé
52*4e111653SPhilippe Mathieu-Daudé #define THUNK_TARGET 0
53*4e111653SPhilippe Mathieu-Daudé #define THUNK_HOST 1
54*4e111653SPhilippe Mathieu-Daudé
55*4e111653SPhilippe Mathieu-Daudé typedef struct {
56*4e111653SPhilippe Mathieu-Daudé /* standard struct handling */
57*4e111653SPhilippe Mathieu-Daudé const argtype *field_types;
58*4e111653SPhilippe Mathieu-Daudé int nb_fields;
59*4e111653SPhilippe Mathieu-Daudé int *field_offsets[2];
60*4e111653SPhilippe Mathieu-Daudé /* special handling */
61*4e111653SPhilippe Mathieu-Daudé void (*convert[2])(void *dst, const void *src);
62*4e111653SPhilippe Mathieu-Daudé void (*print)(void *arg);
63*4e111653SPhilippe Mathieu-Daudé int size[2];
64*4e111653SPhilippe Mathieu-Daudé int align[2];
65*4e111653SPhilippe Mathieu-Daudé const char *name;
66*4e111653SPhilippe Mathieu-Daudé } StructEntry;
67*4e111653SPhilippe Mathieu-Daudé
68*4e111653SPhilippe Mathieu-Daudé /* Translation table for bitmasks... */
69*4e111653SPhilippe Mathieu-Daudé typedef struct bitmask_transtbl {
70*4e111653SPhilippe Mathieu-Daudé unsigned int target_mask;
71*4e111653SPhilippe Mathieu-Daudé unsigned int target_bits;
72*4e111653SPhilippe Mathieu-Daudé unsigned int host_mask;
73*4e111653SPhilippe Mathieu-Daudé unsigned int host_bits;
74*4e111653SPhilippe Mathieu-Daudé } bitmask_transtbl;
75*4e111653SPhilippe Mathieu-Daudé
76*4e111653SPhilippe Mathieu-Daudé void thunk_register_struct(int id, const char *name, const argtype *types);
77*4e111653SPhilippe Mathieu-Daudé void thunk_register_struct_direct(int id, const char *name,
78*4e111653SPhilippe Mathieu-Daudé const StructEntry *se1);
79*4e111653SPhilippe Mathieu-Daudé const argtype *thunk_convert(void *dst, const void *src,
80*4e111653SPhilippe Mathieu-Daudé const argtype *type_ptr, int to_host);
81*4e111653SPhilippe Mathieu-Daudé const argtype *thunk_print(void *arg, const argtype *type_ptr);
82*4e111653SPhilippe Mathieu-Daudé
83*4e111653SPhilippe Mathieu-Daudé extern StructEntry *struct_entries;
84*4e111653SPhilippe Mathieu-Daudé
85*4e111653SPhilippe Mathieu-Daudé int thunk_type_size_array(const argtype *type_ptr, int is_host);
86*4e111653SPhilippe Mathieu-Daudé int thunk_type_align_array(const argtype *type_ptr, int is_host);
87*4e111653SPhilippe Mathieu-Daudé
thunk_type_size(const argtype * type_ptr,int is_host)88*4e111653SPhilippe Mathieu-Daudé static inline int thunk_type_size(const argtype *type_ptr, int is_host)
89*4e111653SPhilippe Mathieu-Daudé {
90*4e111653SPhilippe Mathieu-Daudé int type, size;
91*4e111653SPhilippe Mathieu-Daudé const StructEntry *se;
92*4e111653SPhilippe Mathieu-Daudé
93*4e111653SPhilippe Mathieu-Daudé type = *type_ptr;
94*4e111653SPhilippe Mathieu-Daudé switch(type) {
95*4e111653SPhilippe Mathieu-Daudé case TYPE_CHAR:
96*4e111653SPhilippe Mathieu-Daudé return 1;
97*4e111653SPhilippe Mathieu-Daudé case TYPE_SHORT:
98*4e111653SPhilippe Mathieu-Daudé return 2;
99*4e111653SPhilippe Mathieu-Daudé case TYPE_INT:
100*4e111653SPhilippe Mathieu-Daudé return 4;
101*4e111653SPhilippe Mathieu-Daudé case TYPE_LONGLONG:
102*4e111653SPhilippe Mathieu-Daudé case TYPE_ULONGLONG:
103*4e111653SPhilippe Mathieu-Daudé return 8;
104*4e111653SPhilippe Mathieu-Daudé case TYPE_LONG:
105*4e111653SPhilippe Mathieu-Daudé case TYPE_ULONG:
106*4e111653SPhilippe Mathieu-Daudé case TYPE_PTRVOID:
107*4e111653SPhilippe Mathieu-Daudé case TYPE_PTR:
108*4e111653SPhilippe Mathieu-Daudé if (is_host) {
109*4e111653SPhilippe Mathieu-Daudé return sizeof(void *);
110*4e111653SPhilippe Mathieu-Daudé } else {
111*4e111653SPhilippe Mathieu-Daudé return TARGET_ABI_BITS / 8;
112*4e111653SPhilippe Mathieu-Daudé }
113*4e111653SPhilippe Mathieu-Daudé break;
114*4e111653SPhilippe Mathieu-Daudé case TYPE_OLDDEVT:
115*4e111653SPhilippe Mathieu-Daudé if (is_host) {
116*4e111653SPhilippe Mathieu-Daudé #if defined(HOST_X86_64)
117*4e111653SPhilippe Mathieu-Daudé return 8;
118*4e111653SPhilippe Mathieu-Daudé #elif defined(HOST_MIPS) || defined(HOST_SPARC64)
119*4e111653SPhilippe Mathieu-Daudé return 4;
120*4e111653SPhilippe Mathieu-Daudé #elif defined(HOST_PPC)
121*4e111653SPhilippe Mathieu-Daudé return sizeof(void *);
122*4e111653SPhilippe Mathieu-Daudé #else
123*4e111653SPhilippe Mathieu-Daudé return 2;
124*4e111653SPhilippe Mathieu-Daudé #endif
125*4e111653SPhilippe Mathieu-Daudé } else {
126*4e111653SPhilippe Mathieu-Daudé #if defined(TARGET_X86_64)
127*4e111653SPhilippe Mathieu-Daudé return 8;
128*4e111653SPhilippe Mathieu-Daudé #elif defined(TARGET_ALPHA) || defined(TARGET_IA64) || defined(TARGET_MIPS) || \
129*4e111653SPhilippe Mathieu-Daudé defined(TARGET_PARISC) || defined(TARGET_SPARC64)
130*4e111653SPhilippe Mathieu-Daudé return 4;
131*4e111653SPhilippe Mathieu-Daudé #elif defined(TARGET_PPC)
132*4e111653SPhilippe Mathieu-Daudé return TARGET_ABI_BITS / 8;
133*4e111653SPhilippe Mathieu-Daudé #else
134*4e111653SPhilippe Mathieu-Daudé return 2;
135*4e111653SPhilippe Mathieu-Daudé #endif
136*4e111653SPhilippe Mathieu-Daudé }
137*4e111653SPhilippe Mathieu-Daudé break;
138*4e111653SPhilippe Mathieu-Daudé case TYPE_ARRAY:
139*4e111653SPhilippe Mathieu-Daudé size = type_ptr[1];
140*4e111653SPhilippe Mathieu-Daudé return size * thunk_type_size_array(type_ptr + 2, is_host);
141*4e111653SPhilippe Mathieu-Daudé case TYPE_STRUCT:
142*4e111653SPhilippe Mathieu-Daudé se = struct_entries + type_ptr[1];
143*4e111653SPhilippe Mathieu-Daudé return se->size[is_host];
144*4e111653SPhilippe Mathieu-Daudé default:
145*4e111653SPhilippe Mathieu-Daudé g_assert_not_reached();
146*4e111653SPhilippe Mathieu-Daudé }
147*4e111653SPhilippe Mathieu-Daudé }
148*4e111653SPhilippe Mathieu-Daudé
thunk_type_align(const argtype * type_ptr,int is_host)149*4e111653SPhilippe Mathieu-Daudé static inline int thunk_type_align(const argtype *type_ptr, int is_host)
150*4e111653SPhilippe Mathieu-Daudé {
151*4e111653SPhilippe Mathieu-Daudé int type;
152*4e111653SPhilippe Mathieu-Daudé const StructEntry *se;
153*4e111653SPhilippe Mathieu-Daudé
154*4e111653SPhilippe Mathieu-Daudé type = *type_ptr;
155*4e111653SPhilippe Mathieu-Daudé switch(type) {
156*4e111653SPhilippe Mathieu-Daudé case TYPE_CHAR:
157*4e111653SPhilippe Mathieu-Daudé return 1;
158*4e111653SPhilippe Mathieu-Daudé case TYPE_SHORT:
159*4e111653SPhilippe Mathieu-Daudé if (is_host) {
160*4e111653SPhilippe Mathieu-Daudé return __alignof__(short);
161*4e111653SPhilippe Mathieu-Daudé } else {
162*4e111653SPhilippe Mathieu-Daudé return ABI_SHORT_ALIGNMENT;
163*4e111653SPhilippe Mathieu-Daudé }
164*4e111653SPhilippe Mathieu-Daudé case TYPE_INT:
165*4e111653SPhilippe Mathieu-Daudé if (is_host) {
166*4e111653SPhilippe Mathieu-Daudé return __alignof__(int);
167*4e111653SPhilippe Mathieu-Daudé } else {
168*4e111653SPhilippe Mathieu-Daudé return ABI_INT_ALIGNMENT;
169*4e111653SPhilippe Mathieu-Daudé }
170*4e111653SPhilippe Mathieu-Daudé case TYPE_LONGLONG:
171*4e111653SPhilippe Mathieu-Daudé case TYPE_ULONGLONG:
172*4e111653SPhilippe Mathieu-Daudé if (is_host) {
173*4e111653SPhilippe Mathieu-Daudé return __alignof__(long long);
174*4e111653SPhilippe Mathieu-Daudé } else {
175*4e111653SPhilippe Mathieu-Daudé return ABI_LLONG_ALIGNMENT;
176*4e111653SPhilippe Mathieu-Daudé }
177*4e111653SPhilippe Mathieu-Daudé case TYPE_LONG:
178*4e111653SPhilippe Mathieu-Daudé case TYPE_ULONG:
179*4e111653SPhilippe Mathieu-Daudé case TYPE_PTRVOID:
180*4e111653SPhilippe Mathieu-Daudé case TYPE_PTR:
181*4e111653SPhilippe Mathieu-Daudé if (is_host) {
182*4e111653SPhilippe Mathieu-Daudé return __alignof__(long);
183*4e111653SPhilippe Mathieu-Daudé } else {
184*4e111653SPhilippe Mathieu-Daudé return ABI_LONG_ALIGNMENT;
185*4e111653SPhilippe Mathieu-Daudé }
186*4e111653SPhilippe Mathieu-Daudé break;
187*4e111653SPhilippe Mathieu-Daudé case TYPE_OLDDEVT:
188*4e111653SPhilippe Mathieu-Daudé return thunk_type_size(type_ptr, is_host);
189*4e111653SPhilippe Mathieu-Daudé case TYPE_ARRAY:
190*4e111653SPhilippe Mathieu-Daudé return thunk_type_align_array(type_ptr + 2, is_host);
191*4e111653SPhilippe Mathieu-Daudé case TYPE_STRUCT:
192*4e111653SPhilippe Mathieu-Daudé se = struct_entries + type_ptr[1];
193*4e111653SPhilippe Mathieu-Daudé return se->align[is_host];
194*4e111653SPhilippe Mathieu-Daudé default:
195*4e111653SPhilippe Mathieu-Daudé g_assert_not_reached();
196*4e111653SPhilippe Mathieu-Daudé }
197*4e111653SPhilippe Mathieu-Daudé }
198*4e111653SPhilippe Mathieu-Daudé
199*4e111653SPhilippe Mathieu-Daudé unsigned int target_to_host_bitmask_len(unsigned int target_mask,
200*4e111653SPhilippe Mathieu-Daudé const bitmask_transtbl *trans_tbl,
201*4e111653SPhilippe Mathieu-Daudé size_t trans_len);
202*4e111653SPhilippe Mathieu-Daudé unsigned int host_to_target_bitmask_len(unsigned int host_mask,
203*4e111653SPhilippe Mathieu-Daudé const bitmask_transtbl * trans_tbl,
204*4e111653SPhilippe Mathieu-Daudé size_t trans_len);
205*4e111653SPhilippe Mathieu-Daudé
206*4e111653SPhilippe Mathieu-Daudé #define target_to_host_bitmask(M, T) \
207*4e111653SPhilippe Mathieu-Daudé target_to_host_bitmask_len(M, T, ARRAY_SIZE(T))
208*4e111653SPhilippe Mathieu-Daudé #define host_to_target_bitmask(M, T) \
209*4e111653SPhilippe Mathieu-Daudé host_to_target_bitmask_len(M, T, ARRAY_SIZE(T))
210*4e111653SPhilippe Mathieu-Daudé
211*4e111653SPhilippe Mathieu-Daudé void thunk_init(unsigned int max_structs);
212*4e111653SPhilippe Mathieu-Daudé
213*4e111653SPhilippe Mathieu-Daudé #endif
214