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 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, 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 int size[2]; 59 int align[2]; 60 const char *name; 61 } StructEntry; 62 63 /* Translation table for bitmasks... */ 64 typedef struct bitmask_transtbl { 65 unsigned int target_mask; 66 unsigned int target_bits; 67 unsigned int host_mask; 68 unsigned int host_bits; 69 } bitmask_transtbl; 70 71 void thunk_register_struct(int id, const char *name, const argtype *types); 72 void thunk_register_struct_direct(int id, const char *name, 73 const StructEntry *se1); 74 const argtype *thunk_convert(void *dst, const void *src, 75 const argtype *type_ptr, int to_host); 76 77 extern StructEntry *struct_entries; 78 79 int thunk_type_size_array(const argtype *type_ptr, int is_host); 80 int thunk_type_align_array(const argtype *type_ptr, int is_host); 81 82 static inline int thunk_type_size(const argtype *type_ptr, int is_host) 83 { 84 int type, size; 85 const StructEntry *se; 86 87 type = *type_ptr; 88 switch(type) { 89 case TYPE_CHAR: 90 return 1; 91 case TYPE_SHORT: 92 return 2; 93 case TYPE_INT: 94 return 4; 95 case TYPE_LONGLONG: 96 case TYPE_ULONGLONG: 97 return 8; 98 case TYPE_LONG: 99 case TYPE_ULONG: 100 case TYPE_PTRVOID: 101 case TYPE_PTR: 102 if (is_host) { 103 return sizeof(void *); 104 } else { 105 return TARGET_ABI_BITS / 8; 106 } 107 break; 108 case TYPE_OLDDEVT: 109 if (is_host) { 110 #if defined(HOST_X86_64) 111 return 8; 112 #elif defined(HOST_ALPHA) || defined(HOST_IA64) || defined(HOST_MIPS) || \ 113 defined(HOST_PARISC) || defined(HOST_SPARC64) 114 return 4; 115 #elif defined(HOST_PPC) 116 return sizeof(void *); 117 #else 118 return 2; 119 #endif 120 } else { 121 #if defined(TARGET_X86_64) 122 return 8; 123 #elif defined(TARGET_ALPHA) || defined(TARGET_IA64) || defined(TARGET_MIPS) || \ 124 defined(TARGET_PARISC) || defined(TARGET_SPARC64) 125 return 4; 126 #elif defined(TARGET_PPC) 127 return TARGET_ABI_BITS / 8; 128 #else 129 return 2; 130 #endif 131 } 132 break; 133 case TYPE_ARRAY: 134 size = type_ptr[1]; 135 return size * thunk_type_size_array(type_ptr + 2, is_host); 136 case TYPE_STRUCT: 137 se = struct_entries + type_ptr[1]; 138 return se->size[is_host]; 139 default: 140 g_assert_not_reached(); 141 } 142 } 143 144 static inline int thunk_type_align(const argtype *type_ptr, int is_host) 145 { 146 int type; 147 const StructEntry *se; 148 149 type = *type_ptr; 150 switch(type) { 151 case TYPE_CHAR: 152 return 1; 153 case TYPE_SHORT: 154 if (is_host) { 155 return __alignof__(short); 156 } else { 157 return ABI_SHORT_ALIGNMENT; 158 } 159 case TYPE_INT: 160 if (is_host) { 161 return __alignof__(int); 162 } else { 163 return ABI_INT_ALIGNMENT; 164 } 165 case TYPE_LONGLONG: 166 case TYPE_ULONGLONG: 167 if (is_host) { 168 return __alignof__(long long); 169 } else { 170 return ABI_LLONG_ALIGNMENT; 171 } 172 case TYPE_LONG: 173 case TYPE_ULONG: 174 case TYPE_PTRVOID: 175 case TYPE_PTR: 176 if (is_host) { 177 return __alignof__(long); 178 } else { 179 return ABI_LONG_ALIGNMENT; 180 } 181 break; 182 case TYPE_OLDDEVT: 183 return thunk_type_size(type_ptr, is_host); 184 case TYPE_ARRAY: 185 return thunk_type_align_array(type_ptr + 2, is_host); 186 case TYPE_STRUCT: 187 se = struct_entries + type_ptr[1]; 188 return se->align[is_host]; 189 default: 190 g_assert_not_reached(); 191 } 192 } 193 194 unsigned int target_to_host_bitmask(unsigned int target_mask, 195 const bitmask_transtbl * trans_tbl); 196 unsigned int host_to_target_bitmask(unsigned int host_mask, 197 const bitmask_transtbl * trans_tbl); 198 199 void thunk_init(unsigned int max_structs); 200 201 #endif 202