Page MenuHomeFreeBSD

D59108.id184734.diff
No OneTemporary

D59108.id184734.diff

diff --git a/lib/libvmmapi/internal.h b/lib/libvmmapi/internal.h
--- a/lib/libvmmapi/internal.h
+++ b/lib/libvmmapi/internal.h
@@ -58,7 +58,8 @@
VM_SUSPEND_CPU, \
VM_RESUME_CPU, \
VM_SET_TOPOLOGY, \
- VM_GET_TOPOLOGY
+ VM_GET_TOPOLOGY, \
+ VM_GET_PID
#define VM_PPT_IOCTLS \
VM_BIND_PPTDEV, \
diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h
--- a/lib/libvmmapi/vmmapi.h
+++ b/lib/libvmmapi/vmmapi.h
@@ -40,7 +40,7 @@
* API version for out-of-tree consumers like grub-bhyve for making compile
* time decisions.
*/
-#define VMMAPI_VERSION 0300 /* 2 digit major followed by 2 digit minor */
+#define VMMAPI_VERSION 0301 /* 2 digit major followed by 2 digit minor */
struct iovec;
struct vcpu;
@@ -291,6 +291,11 @@
int vm_snapshot_req(struct vmctx *ctx, struct vm_snapshot_meta *meta);
int vm_restore_time(struct vmctx *ctx);
+/*
+ * VM pid
+ */
+int vm_get_pid(struct vmctx *ctx, pid_t *pid);
+
/*
* Deprecated interfaces, do not use them in new code.
*/
diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c
--- a/lib/libvmmapi/vmmapi.c
+++ b/lib/libvmmapi/vmmapi.c
@@ -1293,6 +1293,19 @@
return (0);
}
+int
+vm_get_pid(struct vmctx *ctx, pid_t *pid)
+{
+ struct vm_pid vmpid;
+ int error;
+
+ error = ioctl(ctx->fd, VM_GET_PID, &vmpid);
+ if (error == 0)
+ *pid = vmpid.pid;
+
+ return (error);
+}
+
/*
* Avoid using in new code. Operations on the fd should be wrapped here so that
* capability rights can be kept in sync.
diff --git a/sys/amd64/include/vmm_dev.h b/sys/amd64/include/vmm_dev.h
--- a/sys/amd64/include/vmm_dev.h
+++ b/sys/amd64/include/vmm_dev.h
@@ -253,6 +253,10 @@
};
_Static_assert(sizeof(struct vm_readwrite_kernemu_device) == 24, "ABI");
+struct vm_pid {
+ pid_t pid;
+};
+
enum {
/* general routines */
IOCNUM_ABIVERS = 0,
@@ -341,7 +345,10 @@
/* checkpoint */
IOCNUM_SNAPSHOT_REQ = 113,
- IOCNUM_RESTORE_TIME = 115
+ IOCNUM_RESTORE_TIME = 115,
+
+ /* VM pid */
+ IOCNUM_GET_PID = 120
};
#define VM_RUN \
@@ -468,4 +475,6 @@
_IOWR('v', IOCNUM_SNAPSHOT_REQ, struct vm_snapshot_meta)
#define VM_RESTORE_TIME \
_IOWR('v', IOCNUM_RESTORE_TIME, int)
+#define VM_GET_PID \
+ _IOR('v', IOCNUM_GET_PID, struct vm_pid)
#endif
diff --git a/sys/arm64/include/vmm_dev.h b/sys/arm64/include/vmm_dev.h
--- a/sys/arm64/include/vmm_dev.h
+++ b/sys/arm64/include/vmm_dev.h
@@ -166,6 +166,10 @@
uint16_t maxcpus;
};
+struct vm_pid {
+ pid_t pid;
+};
+
enum {
/* general routines */
IOCNUM_ABIVERS = 0,
@@ -213,6 +217,9 @@
/* vm_attach_vgic */
IOCNUM_GET_VGIC_VERSION = 110,
IOCNUM_ATTACH_VGIC = 111,
+
+ /* VM pid */
+ IOCNUM_GET_PID = 120
};
#define VM_RUN \
@@ -273,4 +280,6 @@
_IOR('v', IOCNUM_GET_VGIC_VERSION, struct vm_vgic_version)
#define VM_ATTACH_VGIC \
_IOW('v', IOCNUM_ATTACH_VGIC, struct vm_vgic_descr)
+#define VM_GET_PID \
+ _IOR('v', IOCNUM_GET_PID, struct vm_pid)
#endif
diff --git a/sys/dev/vmm/vmm_dev.c b/sys/dev/vmm/vmm_dev.c
--- a/sys/dev/vmm/vmm_dev.c
+++ b/sys/dev/vmm/vmm_dev.c
@@ -83,6 +83,7 @@
LIST_ENTRY(vmmdev_softc) priv_link;
SLIST_HEAD(, devmem_softc) devmem;
int flags;
+ u_int vm_pid;
};
struct vmmctl_priv {
@@ -378,6 +379,17 @@
return (0);
}
+static int
+vmmdev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
+{
+ struct vmmdev_softc *sc;
+
+ sc = dev->si_drv1;
+ (void)atomic_cmpset_int(&sc->vm_pid, td->td_proc->p_pid, 0);
+
+ return (0);
+}
+
static const struct vmmdev_ioctl vmmdev_ioctls[] = {
VMMDEV_IOCTL(VM_GET_REGISTER, VMMDEV_IOCTL_LOCK_ONE_VCPU),
VMMDEV_IOCTL(VM_SET_REGISTER, VMMDEV_IOCTL_LOCK_ONE_VCPU),
@@ -427,6 +439,7 @@
VMMDEV_IOCTL(VM_GET_CPUS, 0),
VMMDEV_IOCTL(VM_GET_TOPOLOGY, 0),
VMMDEV_IOCTL(VM_SET_TOPOLOGY, 0),
+ VMMDEV_IOCTL(VM_GET_PID, 0),
};
static int
@@ -504,6 +517,12 @@
if (error)
goto lockfail;
}
+ if (cmd == VM_RUN) {
+ u_int pid = td->td_proc->p_pid;
+
+ if (__predict_false(atomic_load_int(&sc->vm_pid) != pid))
+ atomic_store_int(&sc->vm_pid, pid);
+ }
switch (cmd) {
case VM_SUSPEND: {
@@ -514,6 +533,7 @@
break;
}
case VM_REINIT:
+ atomic_store_int(&sc->vm_pid, 0);
error = vm_reinit(sc->vm);
break;
case VM_STAT_DESC: {
@@ -763,6 +783,20 @@
error = 0;
break;
}
+ case VM_GET_PID: {
+ struct vm_pid *vmpid;
+ pid_t pid;
+
+ pid = atomic_load_int(&sc->vm_pid);
+ if (pid == 0) {
+ error = ESRCH;
+ break;
+ }
+ vmpid = (struct vm_pid *)data;
+ vmpid->pid = pid;
+ error = 0;
+ break;
+ }
default:
error = vmmdev_machdep_ioctl(sc->vm, vcpu, cmd, data, fflag,
td);
@@ -960,7 +994,9 @@
static struct cdevsw vmmdevsw = {
.d_name = "vmmdev",
.d_version = D_VERSION,
+ .d_flags = D_TRACKCLOSE,
.d_open = vmmdev_open,
+ .d_close = vmmdev_close,
.d_ioctl = vmmdev_ioctl,
.d_mmap_single = vmmdev_mmap_single,
.d_read = vmmdev_rw,
diff --git a/sys/riscv/include/vmm_dev.h b/sys/riscv/include/vmm_dev.h
--- a/sys/riscv/include/vmm_dev.h
+++ b/sys/riscv/include/vmm_dev.h
@@ -160,6 +160,10 @@
uint16_t maxcpus;
};
+struct vm_pid {
+ pid_t pid;
+};
+
enum {
/* general routines */
IOCNUM_ABIVERS = 0,
@@ -206,6 +210,9 @@
/* vm_attach_aplic */
IOCNUM_ATTACH_APLIC = 110,
+
+ /* VM pid */
+ IOCNUM_GET_PID = 120
};
#define VM_RUN \
@@ -264,4 +271,6 @@
_IOW('v', IOCNUM_RESUME_CPU, struct vm_activate_cpu)
#define VM_ATTACH_APLIC \
_IOW('v', IOCNUM_ATTACH_APLIC, struct vm_aplic_descr)
+#define VM_GET_PID \
+ _IOR('v', IOCNUM_GET_PID, struct vm_pid)
#endif
diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -71,6 +71,7 @@
static uint64_t memsize;
static int run;
static int get_cpu_topology;
+static int get_vm_pid;
#ifdef BHYVE_SNAPSHOT
static int vm_suspend_opt;
#endif
@@ -136,6 +137,7 @@
{ "get-debug-cpus", NO_ARG, &get_debug_cpus, 1 },
{ "get-suspended-cpus", NO_ARG, &get_suspended_cpus, 1 },
{ "get-cpu-topology", NO_ARG, &get_cpu_topology, 1 },
+ { "get-vm-pid", NO_ARG, &get_vm_pid, 1 },
#ifdef BHYVE_SNAPSHOT
{ "checkpoint", REQ_ARG, 0, SET_CHECKPOINT_FILE},
{ "suspend", REQ_ARG, 0, SET_SUSPEND_FILE},
@@ -522,6 +524,16 @@
"maxcpus=%hu\n", sockets, cores, threads, maxcpus);
}
+ if (!error && (get_vm_pid || get_all)) {
+ pid_t pid;
+
+ error = vm_get_pid(ctx, &pid);
+ if (!error)
+ printf("vm pid:\t%d\n", pid);
+ else if (get_all && !get_vm_pid && errno == ESRCH)
+ error = 0;
+ }
+
if (!error && run) {
struct vm_exit vmexit;
cpuset_t cpuset;

File Metadata

Mime Type
text/plain
Expires
Mon, Aug 24, 5:37 PM (20 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37134766
Default Alt Text
D59108.id184734.diff (6 KB)

Event Timeline