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 const argtype *thunk_print(void *arg, const argtype *type_ptr); 77 78 extern StructEntry *struct_entries; 79 80 int thunk_type_size_array(const argtype *type_ptr, int is_host); 81 int thunk_type_align_array(const argtype *type_ptr, int is_host); 82 83 static inline int thunk_type_size(const argtype *type_ptr, int is_host) 84 { 85 int type, size; 86 const StructEntry *se; 87 88 type = *type_ptr; 89 switch(type) { 90 case TYPE_CHAR: 91 return 1; 92 case TYPE_SHORT: 93 return 2; 94 case TYPE_INT: 95 return 4; 96 case TYPE_LONGLONG: 97 case TYPE_ULONGLONG: 98 return 8; 99 case TYPE_LONG: 100 case TYPE_ULONG: 101 case TYPE_PTRVOID: 102 case TYPE_PTR: 103 if (is_host) { 104 return sizeof(void *); 105 } else { 106 return TARGET_ABI_BITS / 8; 107 } 108 break; 109 case TYPE_OLDDEVT: 110 if (is_host) { 111 #if defined(HOST_X86_64) 112 return 8; 113 #elif defined(HOST_ALPHA) || defined(HOST_IA64) || defined(HOST_MIPS) || \ 114 defined(HOST_PARISC) || 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(unsigned int target_mask, 196 const bitmask_transtbl * trans_tbl); 197 unsigned int host_to_target_bitmask(unsigned int host_mask, 198 const bitmask_transtbl * trans_tbl); 199 200 void thunk_init(unsigned int max_structs); 201 202 #endif 203