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.1 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 20 #ifndef THUNK_H 21 #define THUNK_H 22 23 #include "cpu.h" 24 #include "exec/user/abitypes.h" 25 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, 41 TYPE_OLDDEVT, 42 } argtype; 43 44 #define MK_PTR(type) TYPE_PTR, type 45 #define MK_ARRAY(type, size) TYPE_ARRAY, (int)(size), type 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); 58 void (*print)(void *arg); 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 { 66 unsigned int target_mask; 67 unsigned int target_bits; 68 unsigned int host_mask; 69 unsigned int host_bits; 70 } bitmask_transtbl; 71 72 void thunk_register_struct(int id, const char *name, const argtype *types); 73 void thunk_register_struct_direct(int id, const char *name, 74 const StructEntry *se1); 75 const argtype *thunk_convert(void *dst, const void *src, 76 const argtype *type_ptr, int to_host); 77 const argtype *thunk_print(void *arg, const argtype *type_ptr); 78 79 extern StructEntry *struct_entries; 80 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 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) { 105 return sizeof(void *); 106 } else { 107 return TARGET_ABI_BITS / 8; 108 } 109 break; 110 case TYPE_OLDDEVT: 111 if (is_host) { 112 #if defined(HOST_X86_64) 113 return 8; 114 #elif defined(HOST_MIPS) || defined(HOST_SPARC64) 115 return 4; 116 #elif defined(HOST_PPC) 117 return sizeof(void *); 118 #else 119 return 2; 120 #endif 121 } else { 122 #if defined(TARGET_X86_64) 123 return 8; 124 #elif defined(TARGET_ALPHA) || defined(TARGET_IA64) || defined(TARGET_MIPS) || \ 125 defined(TARGET_PARISC) || defined(TARGET_SPARC64) 126 return 4; 127 #elif defined(TARGET_PPC) 128 return TARGET_ABI_BITS / 8; 129 #else 130 return 2; 131 #endif 132 } 133 break; 134 case TYPE_ARRAY: 135 size = type_ptr[1]; 136 return size * thunk_type_size_array(type_ptr + 2, is_host); 137 case TYPE_STRUCT: 138 se = struct_entries + type_ptr[1]; 139 return se->size[is_host]; 140 default: 141 g_assert_not_reached(); 142 } 143 } 144 145 static inline int thunk_type_align(const argtype *type_ptr, int is_host) 146 { 147 int type; 148 const StructEntry *se; 149 150 type = *type_ptr; 151 switch(type) { 152 case TYPE_CHAR: 153 return 1; 154 case TYPE_SHORT: 155 if (is_host) { 156 return __alignof__(short); 157 } else { 158 return ABI_SHORT_ALIGNMENT; 159 } 160 case TYPE_INT: 161 if (is_host) { 162 return __alignof__(int); 163 } else { 164 return ABI_INT_ALIGNMENT; 165 } 166 case TYPE_LONGLONG: 167 case TYPE_ULONGLONG: 168 if (is_host) { 169 return __alignof__(long long); 170 } else { 171 return ABI_LLONG_ALIGNMENT; 172 } 173 case TYPE_LONG: 174 case TYPE_ULONG: 175 case TYPE_PTRVOID: 176 case TYPE_PTR: 177 if (is_host) { 178 return __alignof__(long); 179 } else { 180 return ABI_LONG_ALIGNMENT; 181 } 182 break; 183 case TYPE_OLDDEVT: 184 return thunk_type_size(type_ptr, is_host); 185 case TYPE_ARRAY: 186 return thunk_type_align_array(type_ptr + 2, is_host); 187 case TYPE_STRUCT: 188 se = struct_entries + type_ptr[1]; 189 return se->align[is_host]; 190 default: 191 g_assert_not_reached(); 192 } 193 } 194 195 unsigned int target_to_host_bitmask_len(unsigned int target_mask, 196 const bitmask_transtbl *trans_tbl, 197 size_t trans_len); 198 unsigned int host_to_target_bitmask_len(unsigned int host_mask, 199 const bitmask_transtbl * trans_tbl, 200 size_t trans_len); 201 202 #define target_to_host_bitmask(M, T) \ 203 target_to_host_bitmask_len(M, T, ARRAY_SIZE(T)) 204 #define host_to_target_bitmask(M, T) \ 205 host_to_target_bitmask_len(M, T, ARRAY_SIZE(T)) 206 207 void thunk_init(unsigned int max_structs); 208 209 #endif 210