]>
Commit | Line | Data |
---|---|---|
3ef693a0 FB |
1 | /* |
2 | * Generic thunking code to convert data between host and target CPU | |
5fafdf24 | 3 | * |
3ef693a0 FB |
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 | |
61f3c91a | 9 | * version 2.1 of the License, or (at your option) any later version. |
3ef693a0 FB |
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
3ef693a0 | 18 | */ |
ec150c7e | 19 | |
31e31b8a FB |
20 | #ifndef THUNK_H |
21 | #define THUNK_H | |
22 | ||
35b66fc4 | 23 | #include "cpu.h" |
ec150c7e | 24 | #include "exec/user/abitypes.h" |
34313956 | 25 | |
31e31b8a FB |
26 | /* types enums definitions */ |
27 | ||
28 | typedef enum argtype { | |
29 | TYPE_NULL, | |
30 | TYPE_CHAR, | |
31 | TYPE_SHORT, | |
32 | TYPE_INT, | |
33 | TYPE_LONG, | |
34 | TYPE_ULONG, | |
35 | TYPE_PTRVOID, /* pointer on unknown data */ | |
36 | TYPE_LONGLONG, | |
37 | TYPE_ULONGLONG, | |
38 | TYPE_PTR, | |
39 | TYPE_ARRAY, | |
40 | TYPE_STRUCT, | |
6083abd9 | 41 | TYPE_OLDDEVT, |
31e31b8a FB |
42 | } argtype; |
43 | ||
44 | #define MK_PTR(type) TYPE_PTR, type | |
b1193396 | 45 | #define MK_ARRAY(type, size) TYPE_ARRAY, (int)(size), type |
31e31b8a FB |
46 | #define MK_STRUCT(id) TYPE_STRUCT, id |
47 | ||
48 | #define THUNK_TARGET 0 | |
49 | #define THUNK_HOST 1 | |
50 | ||
51 | typedef struct { | |
52 | /* standard struct handling */ | |
53 | const argtype *field_types; | |
54 | int nb_fields; | |
55 | int *field_offsets[2]; | |
56 | /* special handling */ | |
57 | void (*convert[2])(void *dst, const void *src); | |
888468db | 58 | void (*print)(void *arg); |
31e31b8a FB |
59 | int size[2]; |
60 | int align[2]; | |
61 | const char *name; | |
62 | } StructEntry; | |
63 | ||
64 | /* Translation table for bitmasks... */ | |
65 | typedef struct bitmask_transtbl { | |
e0ca2ed5 PM |
66 | unsigned int target_mask; |
67 | unsigned int target_bits; | |
68 | unsigned int host_mask; | |
69 | unsigned int host_bits; | |
31e31b8a FB |
70 | } bitmask_transtbl; |
71 | ||
72 | void thunk_register_struct(int id, const char *name, const argtype *types); | |
8e853dc7 BS |
73 | void thunk_register_struct_direct(int id, const char *name, |
74 | const StructEntry *se1); | |
5fafdf24 | 75 | const argtype *thunk_convert(void *dst, const void *src, |
31e31b8a | 76 | const argtype *type_ptr, int to_host); |
79482e59 | 77 | const argtype *thunk_print(void *arg, const argtype *type_ptr); |
31e31b8a | 78 | |
8be656b8 | 79 | extern StructEntry *struct_entries; |
24374901 | 80 | |
26553115 JM |
81 | int thunk_type_size_array(const argtype *type_ptr, int is_host); |
82 | int thunk_type_align_array(const argtype *type_ptr, int is_host); | |
83 | ||
24374901 FB |
84 | static inline int thunk_type_size(const argtype *type_ptr, int is_host) |
85 | { | |
86 | int type, size; | |
87 | const StructEntry *se; | |
88 | ||
89 | type = *type_ptr; | |
90 | switch(type) { | |
91 | case TYPE_CHAR: | |
92 | return 1; | |
93 | case TYPE_SHORT: | |
94 | return 2; | |
95 | case TYPE_INT: | |
96 | return 4; | |
97 | case TYPE_LONGLONG: | |
98 | case TYPE_ULONGLONG: | |
99 | return 8; | |
100 | case TYPE_LONG: | |
101 | case TYPE_ULONG: | |
102 | case TYPE_PTRVOID: | |
103 | case TYPE_PTR: | |
104 | if (is_host) { | |
9c6ecf3e | 105 | return sizeof(void *); |
24374901 | 106 | } else { |
992f48a0 | 107 | return TARGET_ABI_BITS / 8; |
24374901 FB |
108 | } |
109 | break; | |
6083abd9 AG |
110 | case TYPE_OLDDEVT: |
111 | if (is_host) { | |
112 | #if defined(HOST_X86_64) | |
113 | return 8; | |
114 | #elif defined(HOST_ALPHA) || defined(HOST_IA64) || defined(HOST_MIPS) || \ | |
115 | defined(HOST_PARISC) || defined(HOST_SPARC64) | |
116 | return 4; | |
117 | #elif defined(HOST_PPC) | |
65074706 | 118 | return sizeof(void *); |
6083abd9 AG |
119 | #else |
120 | return 2; | |
121 | #endif | |
122 | } else { | |
123 | #if defined(TARGET_X86_64) | |
124 | return 8; | |
125 | #elif defined(TARGET_ALPHA) || defined(TARGET_IA64) || defined(TARGET_MIPS) || \ | |
126 | defined(TARGET_PARISC) || defined(TARGET_SPARC64) | |
127 | return 4; | |
128 | #elif defined(TARGET_PPC) | |
129 | return TARGET_ABI_BITS / 8; | |
130 | #else | |
131 | return 2; | |
132 | #endif | |
133 | } | |
134 | break; | |
24374901 FB |
135 | case TYPE_ARRAY: |
136 | size = type_ptr[1]; | |
26553115 | 137 | return size * thunk_type_size_array(type_ptr + 2, is_host); |
24374901 FB |
138 | case TYPE_STRUCT: |
139 | se = struct_entries + type_ptr[1]; | |
140 | return se->size[is_host]; | |
141 | default: | |
75578d6f | 142 | g_assert_not_reached(); |
24374901 FB |
143 | } |
144 | } | |
145 | ||
146 | static inline int thunk_type_align(const argtype *type_ptr, int is_host) | |
147 | { | |
148 | int type; | |
149 | const StructEntry *se; | |
150 | ||
151 | type = *type_ptr; | |
152 | switch(type) { | |
153 | case TYPE_CHAR: | |
154 | return 1; | |
155 | case TYPE_SHORT: | |
f606e4d6 LV |
156 | if (is_host) { |
157 | return __alignof__(short); | |
158 | } else { | |
159 | return ABI_SHORT_ALIGNMENT; | |
160 | } | |
24374901 | 161 | case TYPE_INT: |
f606e4d6 LV |
162 | if (is_host) { |
163 | return __alignof__(int); | |
164 | } else { | |
165 | return ABI_INT_ALIGNMENT; | |
166 | } | |
24374901 FB |
167 | case TYPE_LONGLONG: |
168 | case TYPE_ULONGLONG: | |
f606e4d6 LV |
169 | if (is_host) { |
170 | return __alignof__(long long); | |
171 | } else { | |
172 | return ABI_LLONG_ALIGNMENT; | |
173 | } | |
24374901 FB |
174 | case TYPE_LONG: |
175 | case TYPE_ULONG: | |
176 | case TYPE_PTRVOID: | |
177 | case TYPE_PTR: | |
178 | if (is_host) { | |
f606e4d6 | 179 | return __alignof__(long); |
24374901 | 180 | } else { |
f606e4d6 | 181 | return ABI_LONG_ALIGNMENT; |
24374901 FB |
182 | } |
183 | break; | |
6083abd9 AG |
184 | case TYPE_OLDDEVT: |
185 | return thunk_type_size(type_ptr, is_host); | |
24374901 | 186 | case TYPE_ARRAY: |
26553115 | 187 | return thunk_type_align_array(type_ptr + 2, is_host); |
24374901 FB |
188 | case TYPE_STRUCT: |
189 | se = struct_entries + type_ptr[1]; | |
190 | return se->align[is_host]; | |
191 | default: | |
75578d6f | 192 | g_assert_not_reached(); |
24374901 FB |
193 | } |
194 | } | |
195 | ||
e0ca2ed5 | 196 | unsigned int target_to_host_bitmask(unsigned int target_mask, |
b39bc503 | 197 | const bitmask_transtbl * trans_tbl); |
e0ca2ed5 | 198 | unsigned int host_to_target_bitmask(unsigned int host_mask, |
b39bc503 | 199 | const bitmask_transtbl * trans_tbl); |
31e31b8a | 200 | |
8be656b8 AG |
201 | void thunk_init(unsigned int max_structs); |
202 | ||
31e31b8a | 203 | #endif |