bsd-user: Add target_to_host_semarray for semaphore operations
Add target_to_host_semarray() to convert target semaphore array to host format for semctl(2) SETALL 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:48 UTC
ff1189f659d62792a2aa362264c1ba6fff11a6db
1 file changed
+35
bsd-user/bsd-misc.c
+35
@@ -7,6 +7,11 @@
7
*/
8
#include "qemu/osdep.h"
9
10
+#define _WANT_SEMUN
11
+#include <sys/types.h>
12
+#include <sys/ipc.h>
13
+#include <sys/msg.h>
14
+#include <sys/sem.h>
15
#include <sys/uuid.h>
16
17
#include "qemu.h"
@@ -33,3 +38,33 @@ abi_long host_to_target_uuid(abi_ulong target_addr, struct uuid *host_uuid)
38
unlock_user_struct(target_uuid, target_addr, 1);
39
return 0;
40
}
41
+
42
+abi_long target_to_host_semarray(int semid, unsigned short **host_array,
43
+ abi_ulong target_addr)
44
+{
45
+ abi_long ret;
46
+ int nsems, i;
47
+ unsigned short *array;
48
+ union semun semun;
49
+ struct semid_ds semid_ds;
50
+
51
+ semun.buf = &semid_ds;
52
+ ret = semctl(semid, 0, IPC_STAT, semun);
53
+ if (ret == -1) {
54
+ return get_errno(ret);
55
+ }
56
+ nsems = semid_ds.sem_nsems;
57
+ *host_array = g_new(unsigned short, nsems);
58
+ array = lock_user(VERIFY_READ, target_addr,
59
+ nsems * sizeof(unsigned short), 1);
60
+ if (array == NULL) {
61
+ free(*host_array);
62
+ return -TARGET_EFAULT;
63
+ }
64
+ for (i = 0; i < nsems; i++) {
65
+ __get_user((*host_array)[i], array + i);
66
+ }
67
+ unlock_user(array, target_addr, 0);
68
+
69
+ return 0;
70
+}