[PATCH 7/8] kvm: decode the argument for KVM_{SET, GET}_SREGS ioctl command

Masatake YAMATO yamato at redhat.com
Fri Dec 1 19:05:31 UTC 2017


* configure.ac (AC_CHECK_TYPES): Add struct kvm_sregs.
* kvm.c (kvm_ioctl_decode_regs_segment): New function.
(kvm_ioctl) <KVM_SET_SREGS, KVM_GET_SREGS>: Use it.
* linux/arch_kvm.c (arch_print_kvm_sregs): New function.
* linux/x86_64/arch_kvm.c (arch_print_kvm_sregs): New function.
(kvm_ioctl_decode_regs_dtable): Ditto.
(kvm_ioctl_decode_regs_segment): Ditto.

Changes in v2:
* Decode only if struct kvm_sregs is available.
* Put arch-specific and generic stub decoders to arch_kvm.c.
* Use umove_or_printaddr instead of umove.

    Above 3 items are suggested by ldv.

* Use more const modifiers.

Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
 configure.ac            |  5 +++-
 kvm.c                   | 23 +++++++++++++++++
 linux/arch_kvm.c        |  8 ++++++
 linux/x86_64/arch_kvm.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index 93d4bd73..b0ddfd17 100644
--- a/configure.ac
+++ b/configure.ac
@@ -541,7 +541,10 @@ AC_CHECK_TYPES([struct statfs64], [
 
 AC_CHECK_TYPES([struct blk_user_trace_setup],,, [#include <linux/blktrace_api.h>])
 
-AC_CHECK_TYPES([struct kvm_regs],,, [#include <linux/kvm.h>])
+AC_CHECK_TYPES(m4_normalize([
+		struct kvm_regs,
+		struct kvm_sregs
+		]),,, [#include <linux/kvm.h>])
 
 AC_CHECK_HEADERS([linux/btrfs.h], [
 	AC_CHECK_MEMBERS(m4_normalize([
diff --git a/kvm.c b/kvm.c
index a4bab9d4..32c278ac 100644
--- a/kvm.c
+++ b/kvm.c
@@ -84,6 +84,26 @@ kvm_ioctl_decode_regs(struct tcb *const tcp, const unsigned int code, const kern
 #endif
 }
 
+static int
+kvm_ioctl_decode_sregs(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
+{
+#ifdef HAVE_STRUCT_KVM_REGS
+	struct kvm_sregs sregs;
+
+	if (code == KVM_GET_SREGS && entering(tcp))
+		return 0;
+
+	tprints(", ");
+	if (umove_or_printaddr(tcp, arg, &sregs) < 0)
+		return RVAL_DECODED;
+
+	arch_print_kvm_sregs(tcp, arg, &sregs);
+	return RVAL_IOCTL_DECODED;
+#else
+	return RVAL_DECODED;
+#endif
+}
+
 int
 kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
 {
@@ -95,6 +115,9 @@ kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t a
 	case KVM_SET_REGS:
 	case KVM_GET_REGS:
 		return kvm_ioctl_decode_regs(tcp, code, arg);
+	case KVM_SET_SREGS:
+	case KVM_GET_SREGS:
+		return kvm_ioctl_decode_sregs(tcp, code, arg);
 	case KVM_CREATE_VM:
 		return RVAL_DECODED | RVAL_FD;
 	case KVM_RUN:
diff --git a/linux/arch_kvm.c b/linux/arch_kvm.c
index 041f6715..19a068a4 100644
--- a/linux/arch_kvm.c
+++ b/linux/arch_kvm.c
@@ -5,3 +5,11 @@ arch_print_kvm_regs(struct tcb *const tcp,
 {
 	printaddr(addr);
 }
+
+static void
+arch_print_kvm_sregs(struct tcb *const tcp,
+		    const kernel_ulong_t addr,
+		    const struct kvm_regs *const sregs)
+{
+	printaddr(addr);
+}
diff --git a/linux/x86_64/arch_kvm.c b/linux/x86_64/arch_kvm.c
index 8bd98acc..c3284e9c 100644
--- a/linux/x86_64/arch_kvm.c
+++ b/linux/x86_64/arch_kvm.c
@@ -34,3 +34,71 @@ arch_print_kvm_regs(struct tcb *const tcp,
 
 	tprints("}");
 }
+
+
+static void
+kvm_ioctl_decode_regs_segment(const char *prefix,
+			      const struct kvm_segment *const segment)
+{
+	tprints(prefix);
+	PRINT_FIELD_X("={", *segment, base);
+	PRINT_FIELD_U(", ", *segment, limit);
+	PRINT_FIELD_U(", ", *segment, selector);
+	PRINT_FIELD_U(", ", *segment, type);
+	PRINT_FIELD_U(", ", *segment, present);
+	PRINT_FIELD_U(", ", *segment, dpl);
+	PRINT_FIELD_U(", ", *segment, db);
+	PRINT_FIELD_U(", ", *segment, s);
+	PRINT_FIELD_U(", ", *segment, l);
+	PRINT_FIELD_U(", ", *segment, g);
+	PRINT_FIELD_U(", ", *segment, avl);
+	tprints("}");
+}
+
+static void
+kvm_ioctl_decode_regs_dtable(const char *prefix,
+			     const struct kvm_dtable *const dtable)
+{
+	tprints(prefix);
+	PRINT_FIELD_X("={", *dtable, base);
+	PRINT_FIELD_U(", ", *dtable, limit);
+	tprints("}");
+}
+
+static void
+arch_print_kvm_sregs(struct tcb *const tcp,
+		     const kernel_ulong_t addr,
+		     const struct kvm_sregs *const sregs)
+{
+	tprints("{");
+	kvm_ioctl_decode_regs_segment("cs", &sregs->cs);
+	if (abbrev(tcp)) {
+		tprints(", ...}");
+		return;
+	}
+
+	kvm_ioctl_decode_regs_segment(", ds", &sregs->ds);
+	kvm_ioctl_decode_regs_segment(", es", &sregs->es);
+	kvm_ioctl_decode_regs_segment(", fs", &sregs->fs);
+	kvm_ioctl_decode_regs_segment(", gs", &sregs->gs);
+	kvm_ioctl_decode_regs_segment(", ss", &sregs->ss);
+	kvm_ioctl_decode_regs_segment(", tr", &sregs->tr);
+	kvm_ioctl_decode_regs_segment(", ldt", &sregs->ldt);
+	kvm_ioctl_decode_regs_dtable(", gdt", &sregs->gdt);
+	kvm_ioctl_decode_regs_dtable(", idt", &sregs->idt);
+	PRINT_FIELD_U(", ", *sregs, cr0);
+	PRINT_FIELD_U(", ", *sregs, cr2);
+	PRINT_FIELD_U(", ", *sregs, cr3);
+	PRINT_FIELD_U(", ", *sregs, cr4);
+	PRINT_FIELD_U(", ", *sregs, cr8);
+	PRINT_FIELD_U(", ", *sregs, efer);
+	PRINT_FIELD_X(", ", *sregs, apic_base);
+	tprints(", interrupt_bitmap=[");
+	for (int i = 0; i < (KVM_NR_INTERRUPTS + 63) / 64; i++) {
+		if (i != 0)
+			tprints(", ");
+		tprintf("%" PRI__u64, sregs->interrupt_bitmap[i]);
+	}
+	tprints("]");
+	tprints("}");
+}
-- 
2.13.6





More information about the Strace-devel mailing list