thunk.h (5493B)
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 28typedef 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 51typedef 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... */ 65typedef 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 72void thunk_register_struct(int id, const char *name, const argtype *types); 73void thunk_register_struct_direct(int id, const char *name, 74 const StructEntry *se1); 75const argtype *thunk_convert(void *dst, const void *src, 76 const argtype *type_ptr, int to_host); 77const argtype *thunk_print(void *arg, const argtype *type_ptr); 78 79extern StructEntry *struct_entries; 80 81int thunk_type_size_array(const argtype *type_ptr, int is_host); 82int thunk_type_align_array(const argtype *type_ptr, int is_host); 83 84static 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_ALPHA) || defined(HOST_IA64) || defined(HOST_MIPS) || \ 115 defined(HOST_PARISC) || defined(HOST_SPARC64) 116 return 4; 117#elif defined(HOST_PPC) 118 return sizeof(void *); 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; 135 case TYPE_ARRAY: 136 size = type_ptr[1]; 137 return size * thunk_type_size_array(type_ptr + 2, is_host); 138 case TYPE_STRUCT: 139 se = struct_entries + type_ptr[1]; 140 return se->size[is_host]; 141 default: 142 g_assert_not_reached(); 143 } 144} 145 146static 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: 156 if (is_host) { 157 return __alignof__(short); 158 } else { 159 return ABI_SHORT_ALIGNMENT; 160 } 161 case TYPE_INT: 162 if (is_host) { 163 return __alignof__(int); 164 } else { 165 return ABI_INT_ALIGNMENT; 166 } 167 case TYPE_LONGLONG: 168 case TYPE_ULONGLONG: 169 if (is_host) { 170 return __alignof__(long long); 171 } else { 172 return ABI_LLONG_ALIGNMENT; 173 } 174 case TYPE_LONG: 175 case TYPE_ULONG: 176 case TYPE_PTRVOID: 177 case TYPE_PTR: 178 if (is_host) { 179 return __alignof__(long); 180 } else { 181 return ABI_LONG_ALIGNMENT; 182 } 183 break; 184 case TYPE_OLDDEVT: 185 return thunk_type_size(type_ptr, is_host); 186 case TYPE_ARRAY: 187 return thunk_type_align_array(type_ptr + 2, is_host); 188 case TYPE_STRUCT: 189 se = struct_entries + type_ptr[1]; 190 return se->align[is_host]; 191 default: 192 g_assert_not_reached(); 193 } 194} 195 196unsigned int target_to_host_bitmask(unsigned int target_mask, 197 const bitmask_transtbl * trans_tbl); 198unsigned int host_to_target_bitmask(unsigned int host_mask, 199 const bitmask_transtbl * trans_tbl); 200 201void thunk_init(unsigned int max_structs); 202 203#endif