[PATCH v5 1/4] kvm: decode the argument for KVM_SET_USER_MEMORY_REGION ioctl command

Masatake YAMATO yamato at redhat.com
Thu Dec 7 14:03:32 UTC 2017


* configure.ac (AC_CHECK_TYPES): Add struct kvm_userspace_memory_region.
* xlat/kvm_mem_flags.in: New file.
* kvm.c: Include print_fields.h and xlat/kvm_mem_flags.h.
(kvm_ioctl_set_user_memory_region): New function.
(kvm_ioctl) <KVM_SET_USER_MEMORY_REGION>: Use it.

Change in v2:
* Use umove_or_printaddr instead of umove because umove_or_printaddr
  performs verbose(tcp) check and prints the zero address nicely.
  Suggested by ldv.

Change in v3:
* Verify the availability struct kvm_userspace_memory_region.
  Suggested by ldv.

Change in v4:
* Guard whole the case label with ifdef HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION.
  Suggested by ldv.

Change in v5:
* Added missing closing curly bracket.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 configure.ac          |  2 ++
 kvm.c                 | 31 +++++++++++++++++++++++++++++++
 xlat/kvm_mem_flags.in |  2 ++
 3 files changed, 35 insertions(+)
 create mode 100644 xlat/kvm_mem_flags.in

diff --git a/configure.ac b/configure.ac
index fa451d84..dad995c4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -541,6 +541,8 @@ AC_CHECK_TYPES([struct statfs64], [
 
 AC_CHECK_TYPES([struct blk_user_trace_setup],,, [#include <linux/blktrace_api.h>])
 
+AC_CHECK_TYPES([struct kvm_userspace_memory_region],,, [#include <linux/kvm.h>])
+
 AC_CHECK_HEADERS([linux/btrfs.h], [
 	AC_CHECK_MEMBERS(m4_normalize([
 		struct btrfs_ioctl_feature_flags.compat_flags,
diff --git a/kvm.c b/kvm.c
index 9e4f9978..05e572b9 100644
--- a/kvm.c
+++ b/kvm.c
@@ -32,6 +32,7 @@
 
 #ifdef HAVE_LINUX_KVM_H
 # include <linux/kvm.h>
+# include "print_fields.h"
 
 static int
 kvm_ioctl_create_vcpu(struct tcb *const tcp, const kernel_ulong_t arg)
@@ -42,12 +43,42 @@ kvm_ioctl_create_vcpu(struct tcb *const tcp, const kernel_ulong_t arg)
 	return RVAL_IOCTL_DECODED | RVAL_FD;
 }
 
+# ifdef HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION
+#  include "xlat/kvm_mem_flags.h"
+static int
+kvm_ioctl_set_user_memory_region(struct tcb *const tcp, const kernel_ulong_t arg)
+{
+	struct kvm_userspace_memory_region u_memory_region;
+
+	tprints(", ");
+	if (umove_or_printaddr(tcp, arg, &u_memory_region))
+		return RVAL_IOCTL_DECODED;
+
+	PRINT_FIELD_U("{", u_memory_region, slot);
+	PRINT_FIELD_FLAGS(", ", u_memory_region, flags, kvm_mem_flags,
+			  "KVM_MEM_???");
+	PRINT_FIELD_X(", ", u_memory_region, guest_phys_addr);
+	PRINT_FIELD_U(", ", u_memory_region, memory_size);
+	PRINT_FIELD_X(", ", u_memory_region, userspace_addr);
+	tprints("}");
+
+	return RVAL_IOCTL_DECODED;
+}
+# endif /* HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION */
+
+
 int
 kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
 {
 	switch (code) {
 	case KVM_CREATE_VCPU:
 		return kvm_ioctl_create_vcpu(tcp, arg);
+
+# ifdef HAVE_STRUCT_KVM_USERSPACE_MEMORY_REGION
+	case KVM_SET_USER_MEMORY_REGION:
+		return kvm_ioctl_set_user_memory_region(tcp, arg);
+# endif
+
 	case KVM_CREATE_VM:
 		return RVAL_DECODED | RVAL_FD;
 	case KVM_RUN:
diff --git a/xlat/kvm_mem_flags.in b/xlat/kvm_mem_flags.in
new file mode 100644
index 00000000..82b93ba7
--- /dev/null
+++ b/xlat/kvm_mem_flags.in
@@ -0,0 +1,2 @@
+KVM_MEM_LOG_DIRTY_PAGES	(1 << 0)
+KVM_MEM_READONLY	(1 << 1)
-- 
2.13.6





More information about the Strace-devel mailing list