bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Generic thunking code to convert data between host and target CPU |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
| 5 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 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. |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 10 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 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. |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 15 | * |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 18 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 19 | #include "qemu/osdep.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 20 | |
bellard | 3ef693a | 2003-03-23 20:17:16 +0000 | [diff] [blame] | 21 | #include "qemu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 22 | #include "exec/user/thunk.h" |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 23 | |
| 24 | //#define DEBUG |
| 25 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 26 | static unsigned int max_struct_entries; |
| 27 | StructEntry *struct_entries; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 28 | |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 29 | static const argtype *thunk_type_next_ptr(const argtype *type_ptr); |
| 30 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 31 | static inline const argtype *thunk_type_next(const argtype *type_ptr) |
| 32 | { |
| 33 | int type; |
| 34 | |
| 35 | type = *type_ptr++; |
| 36 | switch(type) { |
| 37 | case TYPE_CHAR: |
| 38 | case TYPE_SHORT: |
| 39 | case TYPE_INT: |
| 40 | case TYPE_LONGLONG: |
| 41 | case TYPE_ULONGLONG: |
| 42 | case TYPE_LONG: |
| 43 | case TYPE_ULONG: |
| 44 | case TYPE_PTRVOID: |
Alexander Graf | 6083abd | 2012-01-31 19:44:41 +0100 | [diff] [blame] | 45 | case TYPE_OLDDEVT: |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 46 | return type_ptr; |
| 47 | case TYPE_PTR: |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 48 | return thunk_type_next_ptr(type_ptr); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 49 | case TYPE_ARRAY: |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 50 | return thunk_type_next_ptr(type_ptr + 1); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 51 | case TYPE_STRUCT: |
| 52 | return type_ptr + 1; |
| 53 | default: |
| 54 | return NULL; |
| 55 | } |
| 56 | } |
| 57 | |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 58 | static const argtype *thunk_type_next_ptr(const argtype *type_ptr) |
| 59 | { |
| 60 | return thunk_type_next(type_ptr); |
| 61 | } |
| 62 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 63 | void thunk_register_struct(int id, const char *name, const argtype *types) |
| 64 | { |
| 65 | const argtype *type_ptr; |
| 66 | StructEntry *se; |
| 67 | int nb_fields, offset, max_align, align, size, i, j; |
| 68 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 69 | assert(id < max_struct_entries); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 70 | se = struct_entries + id; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 71 | |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 72 | /* first we count the number of fields */ |
| 73 | type_ptr = types; |
| 74 | nb_fields = 0; |
| 75 | while (*type_ptr != TYPE_NULL) { |
| 76 | type_ptr = thunk_type_next(type_ptr); |
| 77 | nb_fields++; |
| 78 | } |
| 79 | se->field_types = types; |
| 80 | se->nb_fields = nb_fields; |
| 81 | se->name = name; |
| 82 | #ifdef DEBUG |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 83 | printf("struct %s: id=%d nb_fields=%d\n", |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 84 | se->name, id, se->nb_fields); |
| 85 | #endif |
| 86 | /* now we can alloc the data */ |
| 87 | |
| 88 | for(i = 0;i < 2; i++) { |
| 89 | offset = 0; |
| 90 | max_align = 1; |
| 91 | se->field_offsets[i] = malloc(nb_fields * sizeof(int)); |
| 92 | type_ptr = se->field_types; |
| 93 | for(j = 0;j < nb_fields; j++) { |
| 94 | size = thunk_type_size(type_ptr, i); |
| 95 | align = thunk_type_align(type_ptr, i); |
| 96 | offset = (offset + align - 1) & ~(align - 1); |
| 97 | se->field_offsets[i][j] = offset; |
| 98 | offset += size; |
| 99 | if (align > max_align) |
| 100 | max_align = align; |
bellard | 2437490 | 2003-06-15 19:52:54 +0000 | [diff] [blame] | 101 | type_ptr = thunk_type_next(type_ptr); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 102 | } |
| 103 | offset = (offset + max_align - 1) & ~(max_align - 1); |
| 104 | se->size[i] = offset; |
| 105 | se->align[i] = max_align; |
| 106 | #ifdef DEBUG |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 107 | printf("%s: size=%d align=%d\n", |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 108 | i == THUNK_HOST ? "host" : "target", offset, max_align); |
| 109 | #endif |
| 110 | } |
| 111 | } |
| 112 | |
blueswir1 | 8e853dc | 2008-10-05 10:49:32 +0000 | [diff] [blame] | 113 | void thunk_register_struct_direct(int id, const char *name, |
| 114 | const StructEntry *se1) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 115 | { |
| 116 | StructEntry *se; |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 117 | |
| 118 | assert(id < max_struct_entries); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 119 | se = struct_entries + id; |
| 120 | *se = *se1; |
| 121 | se->name = name; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* now we can define the main conversion functions */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 126 | const argtype *thunk_convert(void *dst, const void *src, |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 127 | const argtype *type_ptr, int to_host) |
| 128 | { |
| 129 | int type; |
| 130 | |
| 131 | type = *type_ptr++; |
| 132 | switch(type) { |
| 133 | case TYPE_CHAR: |
| 134 | *(uint8_t *)dst = *(uint8_t *)src; |
| 135 | break; |
| 136 | case TYPE_SHORT: |
| 137 | *(uint16_t *)dst = tswap16(*(uint16_t *)src); |
| 138 | break; |
| 139 | case TYPE_INT: |
| 140 | *(uint32_t *)dst = tswap32(*(uint32_t *)src); |
| 141 | break; |
| 142 | case TYPE_LONGLONG: |
| 143 | case TYPE_ULONGLONG: |
| 144 | *(uint64_t *)dst = tswap64(*(uint64_t *)src); |
| 145 | break; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 146 | #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 147 | case TYPE_LONG: |
| 148 | case TYPE_ULONG: |
| 149 | case TYPE_PTRVOID: |
| 150 | *(uint32_t *)dst = tswap32(*(uint32_t *)src); |
| 151 | break; |
blueswir1 | 992f48a | 2007-10-14 16:27:31 +0000 | [diff] [blame] | 152 | #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 153 | case TYPE_LONG: |
| 154 | case TYPE_ULONG: |
| 155 | case TYPE_PTRVOID: |
bellard | d0cd3b8 | 2003-04-07 21:35:13 +0000 | [diff] [blame] | 156 | if (to_host) { |
bellard | 70499c9 | 2007-11-11 19:31:34 +0000 | [diff] [blame] | 157 | if (type == TYPE_LONG) { |
| 158 | /* sign extension */ |
| 159 | *(uint64_t *)dst = (int32_t)tswap32(*(uint32_t *)src); |
| 160 | } else { |
| 161 | *(uint64_t *)dst = tswap32(*(uint32_t *)src); |
| 162 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 163 | } else { |
| 164 | *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff); |
| 165 | } |
| 166 | break; |
bellard | 70499c9 | 2007-11-11 19:31:34 +0000 | [diff] [blame] | 167 | #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 |
| 168 | case TYPE_LONG: |
| 169 | case TYPE_ULONG: |
| 170 | case TYPE_PTRVOID: |
| 171 | *(uint64_t *)dst = tswap64(*(uint64_t *)src); |
| 172 | break; |
| 173 | #elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64 |
| 174 | case TYPE_LONG: |
| 175 | case TYPE_ULONG: |
| 176 | case TYPE_PTRVOID: |
| 177 | if (to_host) { |
| 178 | *(uint32_t *)dst = tswap64(*(uint64_t *)src); |
| 179 | } else { |
| 180 | if (type == TYPE_LONG) { |
| 181 | /* sign extension */ |
| 182 | *(uint64_t *)dst = tswap64(*(int32_t *)src); |
| 183 | } else { |
| 184 | *(uint64_t *)dst = tswap64(*(uint32_t *)src); |
| 185 | } |
| 186 | } |
| 187 | break; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 188 | #else |
bellard | 64b3ab2 | 2005-01-30 22:43:42 +0000 | [diff] [blame] | 189 | #warning unsupported conversion |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 190 | #endif |
Alexander Graf | 6083abd | 2012-01-31 19:44:41 +0100 | [diff] [blame] | 191 | case TYPE_OLDDEVT: |
| 192 | { |
| 193 | uint64_t val = 0; |
| 194 | switch (thunk_type_size(type_ptr - 1, !to_host)) { |
| 195 | case 2: |
| 196 | val = *(uint16_t *)src; |
| 197 | break; |
| 198 | case 4: |
| 199 | val = *(uint32_t *)src; |
| 200 | break; |
| 201 | case 8: |
| 202 | val = *(uint64_t *)src; |
| 203 | break; |
| 204 | } |
| 205 | switch (thunk_type_size(type_ptr - 1, to_host)) { |
| 206 | case 2: |
| 207 | *(uint16_t *)dst = tswap16(val); |
| 208 | break; |
| 209 | case 4: |
| 210 | *(uint32_t *)dst = tswap32(val); |
| 211 | break; |
| 212 | case 8: |
| 213 | *(uint64_t *)dst = tswap64(val); |
| 214 | break; |
| 215 | } |
| 216 | break; |
| 217 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 218 | case TYPE_ARRAY: |
| 219 | { |
| 220 | int array_length, i, dst_size, src_size; |
| 221 | const uint8_t *s; |
| 222 | uint8_t *d; |
| 223 | |
| 224 | array_length = *type_ptr++; |
| 225 | dst_size = thunk_type_size(type_ptr, to_host); |
| 226 | src_size = thunk_type_size(type_ptr, 1 - to_host); |
| 227 | d = dst; |
| 228 | s = src; |
| 229 | for(i = 0;i < array_length; i++) { |
| 230 | thunk_convert(d, s, type_ptr, to_host); |
| 231 | d += dst_size; |
| 232 | s += src_size; |
| 233 | } |
| 234 | type_ptr = thunk_type_next(type_ptr); |
| 235 | } |
| 236 | break; |
| 237 | case TYPE_STRUCT: |
| 238 | { |
| 239 | int i; |
| 240 | const StructEntry *se; |
| 241 | const uint8_t *s; |
| 242 | uint8_t *d; |
| 243 | const argtype *field_types; |
| 244 | const int *dst_offsets, *src_offsets; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 245 | |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 246 | assert(*type_ptr < max_struct_entries); |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 247 | se = struct_entries + *type_ptr++; |
| 248 | if (se->convert[0] != NULL) { |
| 249 | /* specific conversion is needed */ |
| 250 | (*se->convert[to_host])(dst, src); |
| 251 | } else { |
| 252 | /* standard struct conversion */ |
| 253 | field_types = se->field_types; |
| 254 | dst_offsets = se->field_offsets[to_host]; |
| 255 | src_offsets = se->field_offsets[1 - to_host]; |
| 256 | d = dst; |
| 257 | s = src; |
| 258 | for(i = 0;i < se->nb_fields; i++) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 259 | field_types = thunk_convert(d + dst_offsets[i], |
| 260 | s + src_offsets[i], |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 261 | field_types, to_host); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | break; |
| 266 | default: |
| 267 | fprintf(stderr, "Invalid type 0x%x\n", type); |
| 268 | break; |
| 269 | } |
| 270 | return type_ptr; |
| 271 | } |
| 272 | |
| 273 | /* from em86 */ |
| 274 | |
| 275 | /* Utility function: Table-driven functions to translate bitmasks |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 276 | * between host and target formats |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 277 | */ |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 278 | unsigned int target_to_host_bitmask(unsigned int target_mask, |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 279 | const bitmask_transtbl * trans_tbl) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 280 | { |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 281 | const bitmask_transtbl *btp; |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 282 | unsigned int host_mask = 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 283 | |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 284 | for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) { |
| 285 | if ((target_mask & btp->target_mask) == btp->target_bits) { |
| 286 | host_mask |= btp->host_bits; |
| 287 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 288 | } |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 289 | return host_mask; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 292 | unsigned int host_to_target_bitmask(unsigned int host_mask, |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 293 | const bitmask_transtbl * trans_tbl) |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 294 | { |
blueswir1 | b39bc50 | 2008-10-05 10:51:10 +0000 | [diff] [blame] | 295 | const bitmask_transtbl *btp; |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 296 | unsigned int target_mask = 0; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 297 | |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 298 | for (btp = trans_tbl; btp->target_mask && btp->host_mask; btp++) { |
| 299 | if ((host_mask & btp->host_mask) == btp->host_bits) { |
| 300 | target_mask |= btp->target_bits; |
| 301 | } |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 302 | } |
Peter Maydell | e0ca2ed | 2016-06-02 17:10:20 +0100 | [diff] [blame] | 303 | return target_mask; |
bellard | 31e31b8 | 2003-02-18 22:55:36 +0000 | [diff] [blame] | 304 | } |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 305 | |
j_mayer | 2655311 | 2007-11-19 01:06:24 +0000 | [diff] [blame] | 306 | int thunk_type_size_array(const argtype *type_ptr, int is_host) |
| 307 | { |
| 308 | return thunk_type_size(type_ptr, is_host); |
| 309 | } |
| 310 | |
| 311 | int thunk_type_align_array(const argtype *type_ptr, int is_host) |
| 312 | { |
| 313 | return thunk_type_align(type_ptr, is_host); |
| 314 | } |
Alexander Graf | 8be656b | 2015-05-06 23:47:32 +0200 | [diff] [blame] | 315 | |
| 316 | void thunk_init(unsigned int max_structs) |
| 317 | { |
| 318 | max_struct_entries = max_structs; |
| 319 | struct_entries = g_new0(StructEntry, max_structs); |
| 320 | } |