From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: "Thomas Huth" <thuth@redhat.com>,
"Laurent Vivier" <laurent@vivier.eu>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
"Beraldo Leal" <bleal@redhat.com>, "Warner Losh" <imp@bsdimp.com>,
"Kyle Evans" <kevans@freebsd.org>,
"Stacey Son" <sson@FreeBSD.org>,
"Karim Taha" <kariem.taha2.7@gmail.com>
Subject: [PULL 21/51] bsd-user: Implement procctl(2) along with necessary conversion functions.
Date: Tue, 3 Oct 2023 17:31:45 -0600 [thread overview]
Message-ID: <20231003233215.95557-22-imp@bsdimp.com> (raw)
In-Reply-To: <20231003233215.95557-1-imp@bsdimp.com>
From: Stacey Son <sson@FreeBSD.org>
Implement t2h_procctl_cmd, h2t_reaper_status, h2t_reaper_pidinfo and h2t/t2h reaper_kill conversion functions.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
Reviewed-by: Warner Losh <imp@bsdimp.com>
Message-Id: <20230925182425.3163-22-kariem.taha2.7@gmail.com>
---
bsd-user/freebsd/os-proc.c | 223 ++++++++++++++++++++++++++++++++++
bsd-user/freebsd/os-syscall.c | 3 +
2 files changed, 226 insertions(+)
diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 12d78b7fc98..4e67ae4d56c 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -255,3 +255,226 @@ execve_end:
return ret;
}
+#include <sys/procctl.h>
+
+static abi_long
+t2h_procctl_cmd(int target_cmd, int *host_cmd)
+{
+ switch (target_cmd) {
+ case TARGET_PROC_SPROTECT:
+ *host_cmd = PROC_SPROTECT;
+ break;
+
+ case TARGET_PROC_REAP_ACQUIRE:
+ *host_cmd = PROC_REAP_ACQUIRE;
+ break;
+
+ case TARGET_PROC_REAP_RELEASE:
+ *host_cmd = PROC_REAP_RELEASE;
+ break;
+
+ case TARGET_PROC_REAP_STATUS:
+ *host_cmd = PROC_REAP_STATUS;
+ break;
+
+ case TARGET_PROC_REAP_KILL:
+ *host_cmd = PROC_REAP_KILL;
+ break;
+
+ default:
+ return -TARGET_EINVAL;
+ }
+
+ return 0;
+}
+
+static abi_long
+h2t_reaper_status(struct procctl_reaper_status *host_rs,
+ abi_ulong target_rs_addr)
+{
+ struct target_procctl_reaper_status *target_rs;
+
+ if (!lock_user_struct(VERIFY_WRITE, target_rs, target_rs_addr, 0)) {
+ return -TARGET_EFAULT;
+ }
+ __put_user(host_rs->rs_flags, &target_rs->rs_flags);
+ __put_user(host_rs->rs_children, &target_rs->rs_children);
+ __put_user(host_rs->rs_descendants, &target_rs->rs_descendants);
+ __put_user(host_rs->rs_reaper, &target_rs->rs_reaper);
+ __put_user(host_rs->rs_pid, &target_rs->rs_pid);
+ unlock_user_struct(target_rs, target_rs_addr, 1);
+
+ return 0;
+}
+
+static abi_long
+t2h_reaper_kill(abi_ulong target_rk_addr, struct procctl_reaper_kill *host_rk)
+{
+ struct target_procctl_reaper_kill *target_rk;
+
+ if (!lock_user_struct(VERIFY_READ, target_rk, target_rk_addr, 1)) {
+ return -TARGET_EFAULT;
+ }
+ __get_user(host_rk->rk_sig, &target_rk->rk_sig);
+ __get_user(host_rk->rk_flags, &target_rk->rk_flags);
+ __get_user(host_rk->rk_subtree, &target_rk->rk_subtree);
+ __get_user(host_rk->rk_killed, &target_rk->rk_killed);
+ __get_user(host_rk->rk_fpid, &target_rk->rk_fpid);
+ unlock_user_struct(target_rk, target_rk_addr, 0);
+
+ return 0;
+}
+
+static abi_long
+h2t_reaper_kill(struct procctl_reaper_kill *host_rk, abi_ulong target_rk_addr)
+{
+ struct target_procctl_reaper_kill *target_rk;
+
+ if (!lock_user_struct(VERIFY_WRITE, target_rk, target_rk_addr, 0)) {
+ return -TARGET_EFAULT;
+ }
+ __put_user(host_rk->rk_sig, &target_rk->rk_sig);
+ __put_user(host_rk->rk_flags, &target_rk->rk_flags);
+ __put_user(host_rk->rk_subtree, &target_rk->rk_subtree);
+ __put_user(host_rk->rk_killed, &target_rk->rk_killed);
+ __put_user(host_rk->rk_fpid, &target_rk->rk_fpid);
+ unlock_user_struct(target_rk, target_rk_addr, 1);
+
+ return 0;
+}
+
+static abi_long
+h2t_procctl_reaper_pidinfo(struct procctl_reaper_pidinfo *host_pi,
+ abi_ulong target_pi_addr)
+{
+ struct target_procctl_reaper_pidinfo *target_pi;
+
+ if (!lock_user_struct(VERIFY_WRITE, target_pi, target_pi_addr, 0)) {
+ return -TARGET_EFAULT;
+ }
+ __put_user(host_pi->pi_pid, &target_pi->pi_pid);
+ __put_user(host_pi->pi_subtree, &target_pi->pi_subtree);
+ __put_user(host_pi->pi_flags, &target_pi->pi_flags);
+ unlock_user_struct(target_pi, target_pi_addr, 1);
+
+ return 0;
+}
+
+abi_long
+do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2, abi_ulong arg3,
+ abi_ulong arg4, abi_ulong arg5, abi_ulong arg6)
+{
+ abi_long error = 0, target_rp_pids;
+ void *data;
+ int host_cmd, flags;
+ uint32_t u, target_rp_count;
+ g_autofree union {
+ struct procctl_reaper_status rs;
+ struct procctl_reaper_pids rp;
+ struct procctl_reaper_kill rk;
+ } host;
+ struct target_procctl_reaper_pids *target_rp;
+ id_t id; /* 64-bit */
+ int target_cmd;
+ abi_ulong target_arg;
+
+#if TARGET_ABI_BITS == 32
+ /* See if we need to align the register pairs. */
+ if (regpairs_aligned(cpu_env)) {
+ id = (id_t)target_arg64(arg3, arg4);
+ target_cmd = (int)arg5;
+ target_arg = arg6;
+ } else {
+ id = (id_t)target_arg64(arg2, arg3);
+ target_cmd = (int)arg4;
+ target_arg = arg5;
+ }
+#else
+ id = (id_t)arg2;
+ target_cmd = (int)arg3;
+ target_arg = arg4;
+#endif
+
+ error = t2h_procctl_cmd(target_cmd, &host_cmd);
+ if (error) {
+ return error;
+ }
+ switch (host_cmd) {
+ case PROC_SPROTECT:
+ data = &flags;
+ break;
+
+ case PROC_REAP_ACQUIRE:
+ case PROC_REAP_RELEASE:
+ if (target_arg == 0) {
+ data = NULL;
+ } else {
+ error = -TARGET_EINVAL;
+ }
+ break;
+
+ case PROC_REAP_STATUS:
+ data = &host.rs;
+ break;
+
+ case PROC_REAP_GETPIDS:
+ if (!lock_user_struct(VERIFY_READ, target_rp, target_arg, 1)) {
+ return -TARGET_EFAULT;
+ }
+ __get_user(target_rp_count, &target_rp->rp_count);
+ __get_user(target_rp_pids, &target_rp->rp_pids);
+ unlock_user_struct(target_rp, target_arg, 0);
+ host.rp.rp_count = target_rp_count;
+ host.rp.rp_pids = g_try_new(struct procctl_reaper_pidinfo,
+ target_rp_count);
+
+ if (host.rp.rp_pids == NULL) {
+ error = -TARGET_ENOMEM;
+ } else {
+ data = &host.rp;
+ }
+ break;
+
+ case PROC_REAP_KILL:
+ error = t2h_reaper_kill(target_arg, &host.rk);
+ break;
+ }
+
+ if (error) {
+ return error;
+ }
+ error = get_errno(procctl(idtype, id, host_cmd, data));
+
+ if (error) {
+ return error;
+ }
+ switch (host_cmd) {
+ case PROC_SPROTECT:
+ if (put_user_s32(flags, target_arg)) {
+ return -TARGET_EFAULT;
+ }
+ break;
+
+ case PROC_REAP_STATUS:
+ error = h2t_reaper_status(&host.rs, target_arg);
+ break;
+
+ case PROC_REAP_GETPIDS:
+ /* copyout reaper pidinfo */
+ for (u = 0; u < target_rp_count; u++) {
+ error = h2t_procctl_reaper_pidinfo(&host.rp.rp_pids[u],
+ target_rp_pids +
+ (u * sizeof(struct target_procctl_reaper_pidinfo)));
+ if (error) {
+ break;
+ }
+ }
+ break;
+
+ case PROC_REAP_KILL:
+ error = h2t_reaper_kill(&host.rk, target_arg);
+ break;
+ }
+
+ return error;
+}
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 71a2657dd0f..b7bd0b92a65 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -367,6 +367,9 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
ret = do_bsd_setpriority(arg1, arg2, arg3);
break;
+ case TARGET_FREEBSD_NR_procctl: /* procctl(2) */
+ ret = do_freebsd_procctl(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
+ break;
/*
* File system calls.
--
2.41.0
next prev parent reply other threads:[~2023-10-03 23:37 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-03 23:31 [PULL 00/51] Bsd user mmap patches Warner Losh
2023-10-03 23:31 ` [PULL 01/51] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics, and fix RLIM_INFINITY Warner Losh
2023-10-03 23:31 ` [PULL 02/51] bsd-user: Define procctl(2) related structs Warner Losh
2023-10-03 23:31 ` [PULL 03/51] bsd-user: Implement host_to_target_siginfo Warner Losh
2023-10-03 23:31 ` [PULL 04/51] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h Warner Losh
2023-10-03 23:31 ` [PULL 05/51] bsd-user: add extern declarations for bsd-proc.c conversion functions Warner Losh
2023-10-03 23:31 ` [PULL 06/51] bsd-user: Implement target_to_host_resource conversion function Warner Losh
2023-10-03 23:31 ` [PULL 07/51] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion Warner Losh
2023-10-03 23:31 ` [PULL 08/51] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage Warner Losh
2023-10-03 23:31 ` [PULL 09/51] bsd-user: Implement host_to_target_waitstatus conversion Warner Losh
2023-10-03 23:31 ` [PULL 10/51] bsd-user: Get number of cpus Warner Losh
2023-10-03 23:31 ` [PULL 11/51] bsd-user: Implement getgroups(2) and setgroups(2) system calls Warner Losh
2023-10-03 23:31 ` [PULL 12/51] bsd-user: Implement umask(2), setlogin(2) and getlogin(2) Warner Losh
2023-10-03 23:31 ` [PULL 13/51] bsd-user: Implement getrusage(2) Warner Losh
2023-10-03 23:31 ` [PULL 14/51] bsd-user: Implement getrlimit(2) and setrlimit(2) Warner Losh
2023-10-03 23:31 ` [PULL 15/51] bsd-user: Implement several get/set system calls: Warner Losh
2023-10-03 23:31 ` [PULL 16/51] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid Warner Losh
2023-10-03 23:31 ` [PULL 17/51] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2) Warner Losh
2023-10-03 23:31 ` [PULL 18/51] bsd-user: Implement getpriority(2) and setpriority(2) Warner Losh
2023-10-03 23:31 ` [PULL 19/51] bsd-user: Implement get_filename_from_fd Warner Losh
2023-10-03 23:31 ` [PULL 20/51] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve Warner Losh
2023-10-03 23:31 ` Warner Losh [this message]
2023-10-03 23:31 ` [PULL 22/51] bsd-user: Implement execve(2) and fexecve(2) system calls Warner Losh
2023-10-03 23:31 ` [PULL 23/51] bsd-user: Implement wait4(2) and wait6(2) " Warner Losh
2023-10-03 23:31 ` [PULL 24/51] bsd-user: Implement setloginclass(2) and getloginclass(2) " Warner Losh
2023-10-03 23:31 ` [PULL 25/51] bsd-user: Implement pdgetpid(2) and the undocumented setugid Warner Losh
2023-10-03 23:31 ` [PULL 26/51] bsd-user: Implement fork(2) and vfork(2) system calls Warner Losh
2023-10-03 23:31 ` [PULL 27/51] bsd-user: Implement rfork(2) system call Warner Losh
2023-10-03 23:31 ` [PULL 28/51] bsd-user: Implement pdfork(2) " Warner Losh
2023-10-03 23:31 ` [PULL 29/51] bsd-user: Implement struct target_ipc_perm Warner Losh
2023-10-03 23:31 ` [PULL 30/51] bsd-user: Implement struct target_shmid_ds Warner Losh
2023-10-03 23:31 ` [PULL 31/51] bsd-user: Declarations for ipc_perm and shmid_ds conversion functions Warner Losh
2023-10-03 23:31 ` [PULL 32/51] bsd-user: Introduce freebsd/os-misc.h to the source tree Warner Losh
2023-10-03 23:31 ` [PULL 33/51] bsd-user: Implement shm_open2(2) system call Warner Losh
2023-10-03 23:31 ` [PULL 34/51] bsd-user: Implement shm_rename(2) " Warner Losh
2023-10-03 23:31 ` [PULL 35/51] bsd-user: Add bsd-mem.c to meson.build Warner Losh
2023-10-03 23:32 ` [PULL 36/51] bsd-user: Implement target_set_brk function in bsd-mem.c instead of os-syscall.c Warner Losh
2023-10-03 23:32 ` [PULL 37/51] bsd-user: Implement ipc_perm conversion between host and target Warner Losh
2023-10-03 23:32 ` [PULL 38/51] bsd-user: Implement shmid_ds " Warner Losh
2023-10-03 23:32 ` [PULL 39/51] bsd-user: Introduce bsd-mem.h to the source tree Warner Losh
2023-10-03 23:32 ` [PULL 40/51] bsd-user: Implement mmap(2) and munmap(2) Warner Losh
2023-10-03 23:32 ` [PULL 41/51] bsd-user: Implement mprotect(2) Warner Losh
2023-10-03 23:32 ` [PULL 42/51] bsd-user: Implement msync(2) Warner Losh
2023-10-03 23:32 ` [PULL 43/51] bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2), minherit(2) Warner Losh
2023-10-03 23:32 ` [PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user implementation Warner Losh
2023-10-03 23:32 ` [PULL 45/51] bsd-user: Implement mincore(2) Warner Losh
2023-10-03 23:32 ` [PULL 46/51] bsd-user: Implement do_obreak function Warner Losh
2023-10-03 23:32 ` [PULL 47/51] bsd-user: Implement shm_open(2) Warner Losh
2023-10-03 23:32 ` [PULL 48/51] bsd-user: Implement shm_unlink(2) and shmget(2) Warner Losh
2023-10-03 23:32 ` [PULL 49/51] bsd-user: Implement shmctl(2) Warner Losh
2023-10-03 23:32 ` [PULL 50/51] bsd-user: Implement shmat(2) and shmdt(2) Warner Losh
2023-10-03 23:32 ` [PULL 51/51] bsd-user: Add stubs for vadvise(), sbrk() and sstk() Warner Losh
2023-10-04 18:33 ` [PULL 00/51] Bsd user mmap patches Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231003233215.95557-22-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=alex.bennee@linaro.org \
--cc=bleal@redhat.com \
--cc=kariem.taha2.7@gmail.com \
--cc=kevans@freebsd.org \
--cc=laurent@vivier.eu \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=sson@FreeBSD.org \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).