@samitouri / QOSamiQemu / commits / 6ed4495110

bsd-user: Add init_bsd_ioctl function

Add initialization function that registers thunk structures and patches ioctl table entries with correct size parameters for target architecture. Signed-off-by: Stacey Son <sson@FreeBSD.org> Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com> Signed-off-by: Warner Losh <imp@bsdimp.com>

Stacey Son committed Mar 14, 2026 at 11:51 UTC 6ed4495110af9c52a5746722e537b4618880b7e0
1 file changed +42
bsd-user/bsd-ioctl.c
+42
@@ -392,3 +392,45 @@ abi_long do_bsd_ioctl(int fd, abi_long cmd, abi_long arg)
392 }
393 return ret;
394 }
395 +
396 +void init_bsd_ioctl(void)
397 +{
398 + IOCTLEntry *ie;
399 + const argtype *arg_type;
400 + int size;
401 +
402 + thunk_init(STRUCT_MAX);
403 +
404 +#define STRUCT(name, ...) \
405 + thunk_register_struct(STRUCT_ ## name, #name, struct_ ## name ## _def);
406 +#define STRUCT_SPECIAL(name) \
407 + thunk_register_struct_direct(STRUCT_ ## name, #name, &struct_ ## name ## _def);
408 +#include "os-ioctl-types.h"
409 +#undef STRUCT
410 +#undef STRUCT_SPECIAL
411 +
412 + /*
413 + * Patch the ioctl size if necessary using the fact that no
414 + * ioctl has all the bits at '1' in the size field
415 + * (IOCPARM_MAX - 1).
416 + */
417 + ie = ioctl_entries;
418 + while (ie->target_cmd != 0) {
419 + if (((ie->target_cmd >> TARGET_IOCPARM_SHIFT) &
420 + TARGET_IOCPARM_MASK) == TARGET_IOCPARM_MASK) {
421 + arg_type = ie->arg_type;
422 + if (arg_type[0] != TYPE_PTR) {
423 + fprintf(stderr, "cannot patch size for ioctl 0x%x\n",
424 + ie->target_cmd);
425 + exit(1);
426 + }
427 + arg_type++;
428 + size = thunk_type_size(arg_type, 0);
429 + ie->target_cmd = (ie->target_cmd &
430 + ~(TARGET_IOCPARM_MASK << TARGET_IOCPARM_SHIFT)) |
431 + (size << TARGET_IOCPARM_SHIFT);
432 + }
433 + ie++;
434 + }
435 +
436 +}