@samitouri / QOSamiQemu / commits / 685c763348

bsd-user: Add host_to_target_semarray for semaphore operations

Add host_to_target_semarray() to convert host semaphore array to target format for semctl(2) GETALL operations. Signed-off-by: Stacey Son <sson@FreeBSD.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Warner Losh <imp@bsdimp.com>

Stacey Son committed Feb 2, 2026 at 14:50 UTC 685c7633489eb43773096a149041fe4b6e6596be
1 file changed +30
bsd-user/bsd-misc.c
+30
@@ -68,3 +68,33 @@ abi_long target_to_host_semarray(int semid, unsigned short **host_array,
68
69 return 0;
70 }
71 +
72 +abi_long host_to_target_semarray(int semid, abi_ulong target_addr,
73 + unsigned short **host_arrayp)
74 +{
75 + g_autofree unsigned short *host_array = *host_arrayp;
76 + abi_long ret;
77 + int nsems, i;
78 + unsigned short *array;
79 + union semun semun;
80 + struct semid_ds semid_ds;
81 +
82 + semun.buf = &semid_ds;
83 +
84 + ret = semctl(semid, 0, IPC_STAT, semun);
85 + if (ret == -1) {
86 + return get_errno(ret);
87 + }
88 +
89 + nsems = semid_ds.sem_nsems;
90 + array = (unsigned short *)lock_user(VERIFY_WRITE, target_addr,
91 + nsems * sizeof(unsigned short), 0);
92 + if (array == NULL) {
93 + return -TARGET_EFAULT;
94 + }
95 + for (i = 0; i < nsems; i++) {
96 + __put_user(array[i], host_array + i);
97 + }
98 + unlock_user(array, target_addr, 1);
99 + return 0;
100 +}