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