VM introspection
Subsystems (17)
v6 cover VM introspection
The KVM introspection subsystem provides a facility for applications running on the host or in a separate VM, to control the execution of other VM-s (pause, resume, shutdown), query the state of the vCPUs (GPRs, MSRs etc.), alter the page access bits in the shadow page tables (only for the hardware backed ones, eg. Intel's EPT) and receive notifications when events of interest have taken place (shadow page table level faults, key MSR writes, hypercalls etc.). Some notifications can be responded to with an action (like preventing an MSR from being written), others are mere informative (like breakpoint events which can be used for execution tracing). With few exceptions, all events are optional. An application using this subsystem will explicitly register for them.
The use case that gave way for the creation of this subsystem is to monitor the guest OS and as such the ABI/API is highly influenced by how the guest software (kernel, applications) sees the world. For example, some events provide information specific for the host CPU architecture
(eg. MSR_IA32_SYSENTER_EIP) merely because its leveraged by guest software to implement a critical feature (fast system calls).
At the moment, the target audience for KVMI are security software authors that wish to perform forensics on newly discovered threats (exploits) or to implement another layer of security like preventing a large set of kernel rootkits simply by "locking" the kernel image in the shadow page tables (ie. enforce .text r-x, .rodata rw- etc.). It's the latter case that made KVMI a separate subsystem, even though many of these features are available in the device manager (eg. QEMU). The ability to build a security application that does not interfere (in terms of performance) with the guest software asks for a specialized interface that is designed for minimum overhead.
This patch series is based on 5.0-rc7,
commit de3ccd26fafc ("KVM: MMU: record maximum physical address width in kvm_mmu_extended_role").
The previous RFC (v5) can be read here:
https://www.spinics.net/lists/kvm/msg179441.html
Thanks to Samuel Laurén and Mathieu Tarral, the previous version has been integrated and tested with libVMI.
KVM-VMI: https://github.com/KVM-VMI/kvm-vmi
Kernel: https://github.com/KVM-VMI/kvm/tree/kvmi
QEMU: https://github.com/KVM-VMI/qemu/tree/kvmi
(not all patches, but enough to work)
libVMI: https://github.com/KVM-VMI/libvmi/tree/kvmi
Thanks to Weijiang Yang, the previous version has been integrated and tested with the SPP patch series.
https://github.com/adlazar/kvm/tree/kvmi-v5-spp
Quickstart:
https://github.com/adlazar/kvm/blob/kvmi-v5-spp/tools/kvm/kvmi/README
I hope this version will be merged into KVM-VMI project too.
Patches 1-20: unroll a big part of the KVM introspection subsystem, sent in one patch in the previous versions.
Patches 21-24: extend the current page tracking code.
Patches 25-33: make use of page tracking to support the KVMI_SET_PAGE_ACCESS introspection command and the KVMI_EVENT_PF event (on EPT violations caused by the tracking settings).
Patches 34-42: include the SPP feature (Enable Sub-page Write Protection Support), already sent to KVM list:
https://lore.kernel.org/lkml/[email protected]/
Patches 43-46: add the commands needed to use SPP.
Patches 47-63: unroll almost all the rest of the introspection code.
Patches 64-67: add single-stepping, mostly as a way to overcome the unimplemented instructions, but also as a feature for the introspection tool.
Patches 68-70: cover more cases related to EPT violations.
Patches 71-73: add the remote mapping feature, allowing the introspection tool to map into its address space a page from guest memory.
Patches 74: add a fix to hypercall emulation.
Patches 75-76: disable some features/optimizations when the introspection code is present.
Patches 77-78: add trace functions for the introspection code and change some related to interrupts/exceptions injection.
Patches 79-92: new instruction for the x86 emulator, including cmpxchg fixes.
To do:
- run stress tests with SPP enabled
- add introspection support for alternate EPT views (almost done)
- add introspection support for virtualization exceptions #VE (almost done)
- add KVM tests
Changes since v5:
- small changes to the protocol, but enough to make it backward incompatible with v5
- fix CR3 interception (thanks to Mathieu Tarral for reporting the issue)
- add SPP support (thanks to Weijiang Yang)
- add two more ioctls in order to let userspace/QEMU control the commands/events allowed for introspection
- extend the breakpoint event with the instruction length
- complete the descriptor table registers interception
- add new instructions to the x86 emulator
- move arch dependent code to arch/x86/kvm/
- lots of fixes, especially on page tracking, single-stepping, exception injection and remote memory mapping
- the guests are much more stable (on pair with our introspection products using Xen)
- speed improvements (the penalty on web browsing actions is 50% lower, at least)
Adalbert Lazăr (25):
kvm: introspection: add basic ioctls (hook/unhook)
kvm: introspection: add permission access ioctls
kvm: introspection: add the read/dispatch message function
kvm: introspection: add KVMI_GET_VERSION
kvm: introspection: add KVMI_CONTROL_CMD_RESPONSE
kvm: introspection: honor the reply option when handling the
KVMI_GET_VERSION command
kvm: introspection: add KVMI_CHECK_COMMAND and KVMI_CHECK_EVENT
kvm: introspection: add KVMI_CONTROL_VM_EVENTS
kvm: introspection: add a jobs list to every introspected vCPU
kvm: introspection: make the vCPU wait even when its jobs list is
empty
kvm: introspection: add KVMI_EVENT_UNHOOK
kvm: x86: intercept the write access on sidt and other emulated
instructions
kvm: introspection: add KVMI_CONTROL_SPP
kvm: introspection: extend the internal database of tracked pages with
write_bitmap info
kvm: introspection: add KVMI_GET_PAGE_WRITE_BITMAP
kvm: introspection: add KVMI_SET_PAGE_WRITE_BITMAP
kvm: add kvm_vcpu_kick_and_wait()
kvm: introspection: add KVMI_PAUSE_VCPU and KVMI_EVENT_PAUSE_VCPU
kvm: x86: add kvm_arch_vcpu_set_guest_debug()
kvm: introspection: add custom input when single-stepping a vCPU
kvm: x86: keep the page protected if tracked by the introspection tool
kvm: x86: filter out access rights only when tracked by the
introspection tool
kvm: x86: disable gpa_available optimization in
emulator_read_write_onepage()
kvm: x86: disable EPT A/D bits if introspection is present
kvm: introspection: add trace functions
Marian Rotariu (1):
kvm: introspection: add KVMI_GET_CPUID
Mihai Donțu (47):
kvm: introduce KVMI (VM introspection subsystem)
kvm: introspection: add KVMI_GET_GUEST_INFO
kvm: introspection: handle introspection commands before returning to
guest
kvm: introspection: handle vCPU related introspection commands
kvm: introspection: handle events and event replies
kvm: introspection: introduce event actions
kvm: introspection: add KVMI_GET_VCPU_INFO
kvm: page track: add track_create_slot() callback
kvm: x86: provide all page tracking hooks with the guest virtual
address
kvm: page track: add support for preread, prewrite and preexec
kvm: x86: wire in the preread/prewrite/preexec page trackers
kvm: x86: add kvm_mmu_nested_pagefault()
kvm: introspection: use page track
kvm: x86: consult the page tracking from kvm_mmu_get_page() and
__direct_map()
kvm: introspection: add KVMI_CONTROL_EVENTS
kvm: x86: add kvm_spt_fault()
kvm: introspection: add KVMI_EVENT_PF
kvm: introspection: add KVMI_GET_PAGE_ACCESS
kvm: introspection: add KVMI_SET_PAGE_ACCESS
kvm: introspection: add KVMI_READ_PHYSICAL and KVMI_WRITE_PHYSICAL
kvm: introspection: add KVMI_GET_REGISTERS
kvm: introspection: add KVMI_SET_REGISTERS
kvm: introspection: add KVMI_INJECT_EXCEPTION + KVMI_EVENT_TRAP
kvm: introspection: add KVMI_CONTROL_CR and KVMI_EVENT_CR
kvm: introspection: add KVMI_CONTROL_MSR and KVMI_EVENT_MSR
kvm: introspection: add KVMI_GET_XSAVE
kvm: introspection: add KVMI_GET_MTRR_TYPE
kvm: introspection: add KVMI_EVENT_XSETBV
kvm: introspection: add KVMI_EVENT_BREAKPOINT
kvm: introspection: add KVMI_EVENT_HYPERCALL
kvm: introspection: use single stepping on unimplemented instructions
kvm: x86: emulate a guest page table walk on SPT violations due to A/D
bit updates
kvm: x86: do not unconditionally patch the hypercall instruction
during emulation
kvm: x86: emulate movsd xmm, m64
kvm: x86: emulate movss xmm, m32
kvm: x86: emulate movq xmm, m64
kvm: x86: emulate movq r, xmm
kvm: x86: emulate movd xmm, m32
kvm: x86: enable the half part of movss, movsd, movups
kvm: x86: emulate lfence
kvm: x86: emulate xorpd xmm2/m128, xmm1
kvm: x86: emulate xorps xmm/m128, xmm
kvm: x86: emulate fst/fstp m64fp
kvm: x86: make lock cmpxchg r, r/m atomic
kvm: x86: emulate lock cmpxchg8b atomically
kvm: x86: emulate lock cmpxchg16b m128
kvm: x86: fallback to the single-step on multipage CMPXCHG emulation
Mircea Cîrjaliu (5):
kvm: introspection: add vCPU related data
kvm: introspection: add KVMI_EVENT_CREATE_VCPU
mm: add support for remote mapping
kvm: introspection: add memory map/unmap support on the guest side
kvm: introspection: use remote mapping
Nicușor Cîțu (5):
kvm: x86: block any attempt to disable MSR interception if tracked by
introspection
kvm: introspection: add KVMI_EVENT_DESCRIPTOR
kvm: introspection: add single-stepping
kvm: introspection: add KVMI_EVENT_SINGLESTEP
kvm: x86: add tracepoints for interrupt and exception injections
Yang Weijiang (9):
Documentation: Introduce EPT based Subpage Protection
KVM: VMX: Add control flags for SPP enabling
KVM: VMX: Implement functions for SPPT paging setup
KVM: VMX: Introduce SPP access bitmap and operation functions
KVM: VMX: Add init/set/get functions for SPP
KVM: VMX: Introduce SPP user-space IOCTLs
KVM: VMX: Handle SPP induced vmexit and page fault
KVM: MMU: Enable Lazy mode SPPT setup
KVM: MMU: Handle host memory remapping and reclaim
Documentation/virtual/kvm/api.txt | 104 ++ Documentation/virtual/kvm/hypercalls.txt | 68 +- Documentation/virtual/kvm/kvmi.rst | 1640 +++++++++++++++++ Documentation/virtual/kvm/spp_kvm.txt | 173 ++ arch/x86/Kconfig | 9 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/kvm_emulate.h | 3 +- arch/x86/include/asm/kvm_host.h | 52 +- arch/x86/include/asm/kvm_page_track.h | 33 +- arch/x86/include/asm/kvmi_guest.h | 10 + arch/x86/include/asm/kvmi_host.h | 51 + arch/x86/include/asm/vmx.h | 12 + arch/x86/include/uapi/asm/kvmi.h | 124 ++ arch/x86/include/uapi/asm/vmx.h | 2 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/cpu/intel.c | 4 + arch/x86/kernel/kvmi_mem_guest.c | 26 + arch/x86/kvm/Kconfig | 7 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/emulate.c | 315 +++- arch/x86/kvm/kvmi.c | 1141 ++++++++++++ arch/x86/kvm/mmu.c | 645 ++++++- arch/x86/kvm/mmu.h | 5 + arch/x86/kvm/page_track.c | 147 +- arch/x86/kvm/svm.c | 168 +- arch/x86/kvm/trace.h | 118 +- arch/x86/kvm/vmx/capabilities.h | 5 + arch/x86/kvm/vmx/vmx.c | 347 +++- arch/x86/kvm/vmx/vmx.h | 2 + arch/x86/kvm/x86.c | 601 ++++++- drivers/gpu/drm/i915/gvt/kvmgt.c | 2 +- include/linux/kvm_host.h | 26 + include/linux/kvmi.h | 68 + include/linux/page-flags.h | 9 +- include/linux/remote_mapping.h | 167 ++ include/linux/swait.h | 11 + include/trace/events/kvmi.h | 680 +++++++ include/uapi/linux/kvm.h | 34 + include/uapi/linux/kvm_para.h | 10 +- include/uapi/linux/kvmi.h | 286 +++ include/uapi/linux/remote_mapping.h | 18 + kernel/signal.c | 1 + mm/Kconfig | 8 + mm/Makefile | 1 + mm/memory-failure.c | 69 +- mm/migrate.c | 9 +- mm/remote_mapping.c | 1834 +++++++++++++++++++ mm/rmap.c | 13 +- mm/vmscan.c | 3 +- virt/kvm/kvm_main.c | 70 +- virt/kvm/kvmi.c | 2054 ++++++++++++++++++++++ virt/kvm/kvmi_int.h | 311 ++++ virt/kvm/kvmi_mem.c | 324 ++++ virt/kvm/kvmi_mem_guest.c | 651 +++++++ virt/kvm/kvmi_msg.c | 1236 +++++++++++++ 55 files changed, 13485 insertions(+), 225 deletions(-) create mode 100644 Documentation/virtual/kvm/kvmi.rst create mode 100644 Documentation/virtual/kvm/spp_kvm.txt create mode 100644 arch/x86/include/asm/kvmi_guest.h create mode 100644 arch/x86/include/asm/kvmi_host.h create mode 100644 arch/x86/include/uapi/asm/kvmi.h create mode 100644 arch/x86/kernel/kvmi_mem_guest.c create mode 100644 arch/x86/kvm/kvmi.c create mode 100644 include/linux/kvmi.h create mode 100644 include/linux/remote_mapping.h create mode 100644 include/trace/events/kvmi.h create mode 100644 include/uapi/linux/kvmi.h create mode 100644 include/uapi/linux/remote_mapping.h create mode 100644 mm/remote_mapping.c create mode 100644 virt/kvm/kvmi.c create mode 100644 virt/kvm/kvmi_int.h create mode 100644 virt/kvm/kvmi_mem.c create mode 100644 virt/kvm/kvmi_mem_guest.c create mode 100644 virt/kvm/kvmi_msg.c
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 1/92 kvm: introduce KVMI (VM introspection subsystem)
From: Mihai Donțu <[email protected]>
Besides the pointer to the new structure, the patch adds to the kvm structure a reference counter (the new object will be used by the thread receiving introspection commands/events) and a completion variable (to signal that the VM can be hooked by the introspection tool).
--- Documentation/virtual/kvm/kvmi.rst | 75 ++++++++++++++++++++++++++++++ arch/x86/kvm/Kconfig | 7 +++ arch/x86/kvm/Makefile | 1 + include/linux/kvm_host.h | 4 ++ include/linux/kvmi.h | 23 +++++++++ include/uapi/linux/kvmi.h | 68 +++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 10 +++- virt/kvm/kvmi.c | 64 +++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 12 +++++ 9 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 Documentation/virtual/kvm/kvmi.rst create mode 100644 include/linux/kvmi.h create mode 100644 include/uapi/linux/kvmi.h create mode 100644 virt/kvm/kvmi.c create mode 100644 virt/kvm/kvmi_int.h
Documentation/virtual/kvm/kvmi.rst new file +75 -0
@@ -0,0 +1,75 @@ +========================================================= +KVMI - The kernel virtual machine introspection subsystem +========================================================= + +The KVM introspection subsystem provides a facility for applications running +on the host or in a separate VM, to control the execution of other VM-s +(pause, resume, shutdown), query the state of the vCPUs (GPRs, MSRs etc.), +alter the page access bits in the shadow page tables (only for the hardware +backed ones, eg. Intel's EPT) and receive notifications when events of +interest have taken place (shadow page table level faults, key MSR writes, +hypercalls etc.). Some notifications can be responded to with an action +(like preventing an MSR from being written), others are mere informative +(like breakpoint events which can be used for execution tracing). +With few exceptions, all events are optional. An application using this +subsystem will explicitly register for them. + +The use case that gave way for the creation of this subsystem is to monitor +the guest OS and as such the ABI/API is highly influenced by how the guest +software (kernel, applications) sees the world. For example, some events +provide information specific for the host CPU architecture +(eg. MSR_IA32_SYSENTER_EIP) merely because its leveraged by guest software +to implement a critical feature (fast system calls). + +At the moment, the target audience for KVMI are security software authors +that wish to perform forensics on newly discovered threats (exploits) or +to implement another layer of security like preventing a large set of +kernel rootkits simply by "locking" the kernel image in the shadow page +tables (ie. enforce .text r-x, .rodata rw- etc.). It's the latter case that +made KVMI a separate subsystem, even though many of these features are +available in the device manager (eg. QEMU). The ability to build a security +application that does not interfere (in terms of performance) with the +guest software asks for a specialized interface that is designed for minimum +overhead. + +API/ABI +======= + +This chapter describes the VMI interface used to monitor and control local +guests from a user application. + +Overview +-------- + +The interface is socket based, one connection for every VM. One end is in the +host kernel while the other is held by the user application (introspection +tool). + +The initial connection is established by an application running on the host +(eg. QEMU) that connects to the introspection tool and after a handshake the +socket is passed to the host kernel making all further communication take +place between it and the introspection tool. The initiating party (QEMU) can +close its end so that any potential exploits cannot take a hold of it. + +The socket protocol allows for commands and events to be multiplexed over +the same connection. As such, it is possible for the introspection tool to +receive an event while waiting for the result of a command. Also, it can +send a command while the host kernel is waiting for a reply to an event. + +The kernel side of the socket communication is blocking and will wait for +an answer from its peer indefinitely or until the guest is powered off +(killed), restarted or the peer goes away, at which point it will wake +up and properly cleanup as if the introspection subsystem has never been +used on that guest. Obviously, whether the guest can really continue +normal execution depends on whether the introspection tool has made any +modifications that require an active KVMI channel. + +Memory access safety +-------------------- + +The KVMI API gives access to the entire guest physical address space but +provides no information on which parts of it are system RAM and which are +device-specific memory (DMA, emulated MMIO, reserved by a passthrough +device etc.). It is up to the user to determine, using the guest operating +system data structures, the areas that are safe to access (code, stack, heap +etc.).
arch/x86/kvm/Kconfig +7 -0
@@ -96,6 +96,13 @@ config KVM_MMU_AUDIT This option adds a R/W kVM module parameter 'mmu_audit', which allows auditing of KVM MMU events at runtime. +config KVM_INTROSPECTION + bool "VM Introspection" + depends on KVM && (KVM_INTEL || KVM_AMD) + help + This option enables functions to control the execution of VM-s, query + the state of the vCPU-s (GPR-s, MSR-s etc.). + # OK, it's a little counter-intuitive to do this, but it puts it neatly under # the virtualization menu. source "drivers/vhost/Kconfig"
arch/x86/kvm/Makefile +1 -0
@@ -7,6 +7,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
include/linux/kvm_host.h +4 -0
@@ -455,6 +455,10 @@ struct kvm {struct srcu_struct srcu;struct srcu_struct irq_srcu;pid_t userspace_pid; + +struct completion kvmi_completed; +refcount_t kvmi_ref; +void *kvmi; };#define kvm_err (fmt, ...) \
include/linux/kvmi.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_H__ +#define __KVMI_H__ + +#define kvmi_is_present () IS_ENABLED(CONFIG_KVM_INTROSPECTION) + +#ifdef CONFIG_KVM_INTROSPECTION + +int kvmi_init (void ); +void kvmi_uninit (void ); +void kvmi_create_vm (struct kvm *kvm); +void kvmi_destroy_vm (struct kvm *kvm); + +#else + +static inline int kvmi_init (void ) {return 0 ; } +static inline void kvmi_uninit (void ) { } +static inline void kvmi_create_vm (struct kvm *kvm) { } +static inline void kvmi_destroy_vm (struct kvm *kvm) { } + +#endif /* CONFIG_KVM_INTROSPECTION */ + +#endif
include/uapi/linux/kvmi.h new file +68 -0
68 lines not on this page · raw message
virt/kvm/kvm_main.c +9 -1
@@ -51,6 +51,7 @@#include <linux/slab.h> #include <linux/sort.h> #include <linux/bsearch.h> +#include <linux/kvmi.h> #include <asm/processor.h> #include <asm/io.h>
@@ -680,6 +681,8 @@ static struct kvm *kvm_create_vm(unsigned long type)if (r) goto out_err; +kvmi_create_vm (kvm); +spin_lock (&kvm_lock);list_add (&kvm->vm_list, &vm_list);spin_unlock (&kvm_lock);
@@ -725,6 +728,7 @@ static void kvm_destroy_vm(struct kvm *kvm)int i;struct mm_struct *mm = kvm->mm; +kvmi_destroy_vm (kvm);kvm_uevent_notify_change (KVM_EVENT_DESTROY_VM , kvm);kvm_destroy_vm_debugfs (kvm);kvm_arch_sync_events (kvm);
@@ -1556,7 +1560,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,* Whoever called remap_pfn_range is also going to call e.g. * unmap_mapping_range before the underlying pages are freed, * causing a call to our MMU notifier. -*/ +*/ kvm_get_pfn (pfn); *p_pfn = pfn;
@@ -4204,6 +4208,9 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, r =kvm_vfio_ops_init ();WARN_ON (r); + r =kvmi_init (); +WARN_ON (r); +return 0 ; out_unreg:
@@ -4229,6 +4236,7 @@ EXPORT_SYMBOL_GPL(kvm_init);void kvm_exit (void ) { +kvmi_uninit ();debugfs_remove_recursive (kvm_debugfs_dir);misc_deregister (&kvm_dev);kmem_cache_destroy (kvm_vcpu_cache);
virt/kvm/kvmi.c new file +64 -0
64 lines not on this page · raw message
virt/kvm/kvmi_int.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_INT_H__ +#define __KVMI_INT_H__ + +#include <linux/kvm_host.h> + +#define IKVM (kvm) ((struct kvmi *)((kvm)->kvmi)) + +struct kvmi { +}; + +#endif
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 2/92 kvm: introspection: add basic ioctls (hook/unhook)
In reply to Adalbert LazărThe connection of the introspection socket with the introspection tool is initialized by userspace/QEMU. Once the handshake is done, the file descriptor is passed to KVMi using the KVM_INTROSPECTION_HOOK ioctl. A new thread will be created to handle/dispatch all introspection commands or replies to introspection events. This thread will finish when the socket is closed by userspace (eg. when the guest is restarted) or by the introspection tool. The uuid member of struct kvm_introspection is used to show the guest id with the error messages.
On certain actions from userspace (pause, suspend, migrate, etc.) the KVM_INTROSPECTION_UNHOOK ioctl is used to notify the introspection tool to remove its hooks (eg. breakpoints).
--- Documentation/virtual/kvm/api.txt | 50 ++++++++++ Documentation/virtual/kvm/kvmi.rst | 65 +++++++++++++ arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/x86.c | 7 ++ include/linux/kvmi.h | 4 + include/uapi/linux/kvm.h | 11 +++ virt/kvm/kvm_main.c | 8 ++ virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 31 ++++++ virt/kvm/kvmi_msg.c | 42 +++++++++ 10 files changed, 364 insertions(+), 1 deletion(-) create mode 100644 virt/kvm/kvmi_msg.c
Documentation/virtual/kvm/api.txt +50 -0
@@ -3857,6 +3857,56 @@ number of valid entries in the 'entries' array, which is then filled. 'index' and 'flags' fields in 'struct kvm_cpuid_entry2' are currently reserved, userspace should not expect to get any particular value there. +4.996 KVM_INTROSPECTION_HOOK + +Capability: KVM_CAP_INTROSPECTION +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_introspection (in) +Returns: 0 on success, a negative value on error + +This ioctl is used to enable the introspection of the current VM. + +struct kvm_introspection { + __s32 fd; + __u32 padding; + __u8 uuid[16]; +}; + +fd is the file handle of a socket connected to the introspection tool, + +padding must be zero (it might be used in the future), + +uuid is used for debug and error messages. + +It can fail with -EFAULT if: + - memory allocation failed + - this VM is already introspected + - the file handle doesn't correspond to an active socket + +It will fail with -EINVAL if padding is not zero. + +The KVMI version can be retrieved using the KVM_CAP_INTROSPECTION of +the KVM_CHECK_EXTENSION ioctl() at run-time. + +4.997 KVM_INTROSPECTION_UNHOOK + +Capability: KVM_CAP_INTROSPECTION +Architectures: x86 +Type: vm ioctl +Parameters: none +Returns: 0 on success, a negative value on error + +This ioctl is used to disable the introspection of the current VM. +It is useful when the VM is paused/suspended/migrated. + +It can fail with -EFAULT if: + - the introspection is not enabled + - the socket (passed with KVM_INTROSPECTION_HOOK) had an error + +If the ioctl is successful, the userspace should give the introspection +tool a chance to unhook the VM. + 5. The kvm_run structure ------------------------
Documentation/virtual/kvm/kvmi.rst +65 -0
71 lines not on this page · raw message
arch/x86/kvm/Makefile +1 -1
@@ -7,7 +7,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o -kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o +kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
arch/x86/kvm/x86.c +7 -0
@@ -20,6 +20,8 @@*/ #include <linux/kvm_host.h> +#include <uapi/linux/kvmi.h> +#include <linux/kvmi.h> #include "irq.h" #include "mmu.h" #include "i8254.h"
@@ -3083,6 +3085,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r = kvm_x86_ops->get_nested_state ? kvm_x86_ops->get_nested_state (NULL ,0 ,0 ) :0 ;break ; +#ifdef CONFIG_KVM_INTROSPECTION +case KVM_CAP_INTROSPECTION : + r =KVMI_VERSION ; +break ; +#endif default :break ; }
include/linux/kvmi.h +4 -0
@@ -10,6 +10,10 @@ int kvmi_init(void);void kvmi_uninit (void );void kvmi_create_vm (struct kvm *kvm);void kvmi_destroy_vm (struct kvm *kvm); +int kvmi_ioctl_hook (struct kvm *kvm,void __user *argp); +int kvmi_ioctl_command (struct kvm *kvm,void __user *argp); +int kvmi_ioctl_event (struct kvm *kvm,void __user *argp); +int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset);#else
include/uapi/linux/kvm.h +11 -0
@@ -989,6 +989,8 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_MANUAL_DIRTY_LOG_PROTECT 166#define KVM_CAP_HYPERV_CPUID 167 +#define KVM_CAP_INTROSPECTION 999 +#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing_irqchip {
@@ -1520,6 +1522,15 @@ struct kvm_sev_dbg {__u32 len; }; +struct kvm_introspection { +__s32 fd; +__u32 padding; +__u8 uuid[16 ]; +}; +#define KVM_INTROSPECTION_HOOK _IOW(KVMIO, 0xff, struct kvm_introspection) +#define KVM_INTROSPECTION_UNHOOK _IO(KVMIO, 0xfe) +/* write true on force-reset, false otherwise */ +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
virt/kvm/kvm_main.c +8 -0
@@ -3266,6 +3266,14 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_CHECK_EXTENSION : r =kvm_vm_ioctl_check_extension_generic (kvm, arg);break ; +#ifdef CONFIG_KVM_INTROSPECTION +case KVM_INTROSPECTION_HOOK : + r =kvmi_ioctl_hook (kvm, argp); +break ; +case KVM_INTROSPECTION_UNHOOK : + r =kvmi_ioctl_unhook (kvm, arg); +break ; +#endif /* CONFIG_KVM_INTROSPECTION */ default : r =kvm_arch_vm_ioctl (filp, ioctl, arg); }
virt/kvm/kvmi.c +145 -0
181 lines not on this page · raw message
virt/kvm/kvmi_int.h +31 -0
@@ -2,11 +2,42 @@#ifndef __KVMI_INT_H__#define __KVMI_INT_H__ +#include <linux/types.h> #include <linux/kvm_host.h> +#include <uapi/linux/kvmi.h> + +#define kvmi_debug (ikvm, fmt, ...) \ + kvm_debug("%pU " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define kvmi_info (ikvm, fmt, ...) \ + kvm_info("%pU " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define kvmi_warn (ikvm, fmt, ...) \ + kvm_info("%pU WARNING: " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define kvmi_warn_once (ikvm, fmt, ...) ({ \ + static bool __section(.data.once) __warned; \ + if (!__warned) { \ + __warned = true; \ + kvmi_warn(ikvm, fmt, ## __VA_ARGS__); \ + } \ + }) +#define kvmi_err (ikvm, fmt, ...) \ + kvm_info("%pU ERROR: " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define IKVM (kvm) ((struct kvmi *)((kvm)->kvmi))struct kvmi { +struct kvm *kvm; + +struct socket *sock; +struct task_struct *recv; + +uuid_t uuid; }; +/* kvmi_msg.c */ +bool kvmi_sock_get (struct kvmi *ikvm,int fd); +void kvmi_sock_shutdown (struct kvmi *ikvm); +void kvmi_sock_put (struct kvmi *ikvm); +bool kvmi_msg_process (struct kvmi *ikvm); +#endif
virt/kvm/kvmi_msg.c new file +42 -0
42 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 3/92 kvm: introspection: add permission access ioctls
In reply to Adalbert LazărKVM_INTROSPECTION_COMMAND and KVM_INTROSPECTION_EVENTS should be used by userspace/QEMU to allow access to specific (or all) introspection commands and events.
By default, all introspection events and almost all introspection commands are disallowed. There are a couple of commands that are always allowed (those querying the introspection capabilities).
--- Documentation/virtual/kvm/api.txt | 56 +++++++++++++++++++- include/uapi/linux/kvm.h | 6 +++ virt/kvm/kvm_main.c | 6 +++ virt/kvm/kvmi.c | 85 +++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 51 +++++++++++++++++++ 5 files changed, 203 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/api.txt +55 -1
@@ -3889,7 +3889,61 @@ It will fail with -EINVAL if padding is not zero. The KVMI version can be retrieved using the KVM_CAP_INTROSPECTION of the KVM_CHECK_EXTENSION ioctl() at run-time. -4.997 KVM_INTROSPECTION_UNHOOK +4.997 KVM_INTROSPECTION_COMMAND + +Capability: KVM_CAP_INTROSPECTION +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_introspection_feature (in) +Returns: 0 on success, a negative value on error + +This ioctl is used to allow or disallow introspection commands +for the current VM. By default, almost all commands are disallowed +except for those used to query the API. + +struct kvm_introspection_feature { + __u32 allow; + __s32 id; +}; + +If allow is 1, the command specified by id is allowed. If allow is 0, +the command is disallowed. + +Unless set to -1 (meaning all commands), id must be a command ID +(e.g. KVMI_GET_VERSION, KVMI_GET_GUEST_INFO etc.) + +Errors: + + -EINVAL if the command is unknown + -EPERM if the command can't be disallowed (e.g. KVMI_GET_VERSION) + +4.998 KVM_INTROSPECTION_EVENT + +Capability: KVM_CAP_INTROSPECTION +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_introspection_feature (in) +Returns: 0 on success, a negative value on error + +This ioctl is used to allow or disallow introspection events +for the current VM. By default, all events are disallowed. + +struct kvm_introspection_feature { + __u32 allow; + __s32 id; +}; + +If allow is 1, the event specified by id is allowed. If allow is 0, +the event is disallowed. + +Unless set to -1 (meaning all event), id must be a event ID +(e.g. KVMI_EVENT_UNHOOK, KVMI_EVENT_CR, etc.) + +Errors: + + -EINVAL if the event is unknown + +4.999 KVM_INTROSPECTION_UNHOOK Capability: KVM_CAP_INTROSPECTION Architectures: x86
include/uapi/linux/kvm.h +6 -0
@@ -1527,9 +1527,15 @@ struct kvm_introspection {__u32 padding;__u8 uuid[16 ]; }; +struct kvm_introspection_feature { +__u32 allow; +__s32 id; +};#define KVM_INTROSPECTION_HOOK _IOW(KVMIO, 0xff, struct kvm_introspection)#define KVM_INTROSPECTION_UNHOOK _IO(KVMIO, 0xfe)/* write true on force-reset, false otherwise */ +#define KVM_INTROSPECTION_COMMAND _IOW(KVMIO, 0xfd, struct kvm_introspection_feature) +#define KVM_INTROSPECTION_EVENT _IOW(KVMIO, 0xfc, struct kvm_introspection_feature)#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
virt/kvm/kvm_main.c +6 -0
@@ -3270,6 +3270,12 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_INTROSPECTION_HOOK : r =kvmi_ioctl_hook (kvm, argp);break ; +case KVM_INTROSPECTION_COMMAND : + r =kvmi_ioctl_command (kvm, argp); +break ; +case KVM_INTROSPECTION_EVENT : + r =kvmi_ioctl_event (kvm, argp); +break ;case KVM_INTROSPECTION_UNHOOK : r =kvmi_ioctl_unhook (kvm, arg);break ;
virt/kvm/kvmi.c +85 -0
91 lines not on this page · raw message
virt/kvm/kvmi_int.h +51 -0
63 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 4/92 kvm: introspection: add the read/dispatch message function
In reply to Adalbert LazărBased on the common header used by all messages (struct kvmi_msg_hdr), the worker will read/validate all messages, execute the VM introspection commands (eg. KVMI_GET_GUEST_INFO) and dispatch to vCPUs the vCPU introspection commands (eg. KVMI_GET_REGISTERS) and the replies to vCPU events. The vCPU threads will reply to vCPU introspection commands without the help of the receiving worker.
Because of the command header (struct kvmi_error_code) used in any command reply, this worker could respond to any unsupported/disallowed command with an error code.
This thread will end when the socket is closed (signaled by userspace/QEMU or the introspection tool) or on the first API error (eg. wrong message size).
--- Documentation/virtual/kvm/kvmi.rst | 86 +++++++++++ include/uapi/linux/kvmi.h | 13 ++ virt/kvm/kvmi.c | 43 +++++- virt/kvm/kvmi_int.h | 7 + virt/kvm/kvmi_msg.c | 240 ++++++++++++++++++++++++++++- 5 files changed, 386 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/kvmi.rst +86 -0
@@ -64,6 +64,85 @@ used on that guest. Obviously, whether the guest can really continue normal execution depends on whether the introspection tool has made any modifications that require an active KVMI channel. +All messages (commands or events) have a common header:: + + struct kvmi_msg_hdr { + __u16 id; + __u16 size; + __u32 seq; + }; + +The replies have the same header, with the sequence number (``seq``) +and message id (``id``) matching the command/event. + +After ``kvmi_msg_hdr``, ``id`` specific data of ``size`` bytes will +follow. + +The message header and its data must be sent with one ``sendmsg()`` call +to the socket. This simplifies the receiver loop and avoids +the reconstruction of messages on the other side. + +The wire protocol uses the host native byte-order. The introspection tool +must check this during the handshake and do the necessary conversion. + +A command reply begins with:: + + struct kvmi_error_code { + __s32 err; + __u32 padding; + } + +followed by the command specific data if the error code ``err`` is zero. + +The error code -KVM_EOPNOTSUPP is returned for unsupported commands. + +The error code -KVM_EPERM is returned for disallowed commands (see **Hooking**). + +The error code is related to the message processing, including unsupported +commands. For all the other errors (incomplete messages, wrong sequence +numbers, socket errors etc.) the socket will be closed. The device +manager should reconnect. + +While all commands will have a reply as soon as possible, the replies +to events will probably be delayed until a set of (new) commands will +complete:: + + Host kernel Tool + ----------- ---- + event 1 -> + <- command 1 + command 1 reply -> + <- command 2 + command 2 reply -> + <- event 1 reply + +If both ends send a message at the same time:: + + Host kernel Tool + ----------- ---- + event X -> <- command X + +the host kernel will reply to 'command X', regardless of the receive time +(before or after the 'event X' was sent). + +As it can be seen below, the wire protocol specifies occasional padding. This +is to permit working with the data by directly using C structures or to round +the structure size to a multiple of 8 bytes (64bit) to improve the copy +operations that happen during ``recvmsg()`` or ``sendmsg()``. The members +should have the native alignment of the host (4 bytes on x86). All padding +must be initialized with zero otherwise the respective commands will fail +with -KVM_EINVAL. + +To describe the commands/events, we reuse some conventions from api.txt: + + - Architectures: which instruction set architectures provide this command/event + + - Versions: which versions provide this command/event + + - Parameters: incoming message data + + - Returns: outgoing/reply message data + Handshake ---------
@@ -99,6 +178,13 @@ commands/events) to KVM, and forget about it. It will be notified by KVM when the introspection tool closes the file handle (in case of errors), and should reinitiate the handshake. +Once the file handle reaches KVM, the introspection tool should use +the *KVMI_GET_VERSION* command to get the API version and/or +the *KVMI_CHECK_COMMAND* and *KVMI_CHECK_EVENTS* commands to see which +commands/events are allowed for this guest. The error code -KVM_EPERM +will be returned if the introspection tool uses a command or enables an +event which is disallowed. + Unhooking ---------
include/uapi/linux/kvmi.h +13 -0
@@ -65,4 +65,17 @@ enum {KVMI_NUM_EVENTS }; +#define KVMI_MSG_SIZE (4096 - sizeof(struct kvmi_msg_hdr)) + +struct kvmi_msg_hdr { +__u16 id; +__u16 size; +__u32 seq; +}; + +struct kvmi_error_code { +__s32 err; +__u32 padding; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +42 -1
55 lines not on this page · raw message
virt/kvm/kvmi_int.h +7 -0
@@ -23,6 +23,8 @@#define kvmi_err (ikvm, fmt, ...) \ kvm_info("%pU ERROR: " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define KVMI_MSG_SIZE_ALLOC (sizeof(struct kvmi_msg_hdr) + KVMI_MSG_SIZE) +#define KVMI_KNOWN_VCPU_EVENTS ( \ BIT(KVMI_EVENT_CR) | \ BIT(KVMI_EVENT_MSR) | \
@@ -91,4 +93,9 @@ void kvmi_sock_shutdown(struct kvmi *ikvm);void kvmi_sock_put (struct kvmi *ikvm);bool kvmi_msg_process (struct kvmi *ikvm); +/* kvmi.c */ +void *kvmi_msg_alloc (void ); +void *kvmi_msg_alloc_check (size_t size); +void kvmi_msg_free (void *addr); +#endif
virt/kvm/kvmi_msg.c +238 -2
252 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 5/92 kvm: introspection: add KVMI_GET_VERSION
In reply to Adalbert LazărThis command should be used by the introspection tool to identify the commands/events supported by the KVMi subsystem and, most important, what messages must be used for event replies. The kernel side will accept smaller or bigger command messages, but it can be more strict with bigger event reply messages.
The command is always allowed and any attempt from userspace to disallow it through KVM_INTROSPECTION_COMMAND will get -EPERM (unless userspace choose to disable all commands, using id=-1, in which case KVMI_GET_VERSION is quietly allowed, without an error).
--- Documentation/virtual/kvm/kvmi.rst | 28 ++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 5 +++++ virt/kvm/kvmi.c | 14 ++++++++++++++ virt/kvm/kvmi_msg.c | 13 +++++++++++++ 4 files changed, 60 insertions(+)
Documentation/virtual/kvm/kvmi.rst +28 -0
@@ -224,3 +224,31 @@ device-specific memory (DMA, emulated MMIO, reserved by a passthrough device etc.). It is up to the user to determine, using the guest operating system data structures, the areas that are safe to access (code, stack, heap etc.). + +Commands +-------- + +The following C structures are meant to be used directly when communicating +over the wire. The peer that detects any size mismatch should simply close +the connection and report the error. + +1. KVMI_GET_VERSION +------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: none +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_version_reply { + __u32 version; + __u32 padding; + }; + +Returns the introspection API version. + +This command is always allowed and successful (if the introspection is +built in kernel).
include/uapi/linux/kvmi.h +5 -0
@@ -78,4 +78,9 @@ struct kvmi_error_code { __u32 padding; }; +struct kvmi_get_version_reply { +__u32 version; +__u32 padding; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +14 -0
@@ -68,6 +68,8 @@ static bool alloc_kvmi(struct kvm *kvm, const struct kvm_introspection *qemu)if (!ikvm)return false; +set_bit (KVMI_GET_VERSION , ikvm->cmd_allow_mask); +memcpy (&ikvm->uuid, &qemu->uuid,sizeof (ikvm->uuid)); ikvm->kvm = kvm;
@@ -290,6 +292,18 @@ int kvmi_ioctl_command(struct kvm *kvm, void __user *argp)bitmap_from_u64 (known,KVMI_KNOWN_COMMANDS );bitmap_and (requested, requested, known,KVMI_NUM_COMMANDS ); +if (!allow) { +DECLARE_BITMAP (always_allowed,KVMI_NUM_COMMANDS ); + +if (id ==KVMI_GET_VERSION ) +return -EPERM ; + +set_bit (KVMI_GET_VERSION , always_allowed); + +bitmap_andnot (requested, requested, always_allowed, +KVMI_NUM_COMMANDS ); + } +return kvmi_ioctl_feature (kvm, allow, requested, offsetof(struct kvmi , cmd_allow_mask),KVMI_NUM_COMMANDS );
virt/kvm/kvmi_msg.c +13 -0
@@ -9,6 +9,7 @@#include "kvmi_int.h" static const char *const msg_IDs[] = { + [KVMI_GET_VERSION ] ="KVMI_GET_VERSION" , };static bool is_known_message (u16 id)
@@ -129,6 +130,17 @@ static int kvmi_msg_vm_reply(struct kvmi *ikvm,return kvmi_msg_reply (ikvm, msg, err, rpl, rpl_size); } +static int handle_get_version (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg,const void *req) +{ +struct kvmi_get_version_reply rpl; + +memset (&rpl,0 ,sizeof (rpl)); + rpl.version =KVMI_VERSION ; + +return kvmi_msg_vm_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); +} +static bool is_command_allowed (struct kvmi *ikvm,int id) {return test_bit (id, ikvm->cmd_allow_mask);
@@ -139,6 +151,7 @@ static bool is_command_allowed(struct kvmi *ikvm, int id)*/ static int (*const msg_vm[])(struct kvmi *,const struct kvmi_msg_hdr *,const void *) = { + [KVMI_GET_VERSION ] = handle_get_version, };static bool is_vm_message (u16 id)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 6/92 kvm: introspection: add KVMI_CONTROL_CMD_RESPONSE
In reply to Adalbert LazărThis command enables/disables the command replies. It is useful when the introspection tool send multiple messages with one write() call and doesn't have to wait for a reply.
IIRC, the speed improvment seen during UnixBench tests in a VM introspected through vsock (the introspection tool was running in a different VM) was around 5-10%.
--- Documentation/virtual/kvm/kvmi.rst | 50 ++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 7 ++++ virt/kvm/kvmi_int.h | 2 ++ virt/kvm/kvmi_msg.c | 57 ++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+)
Documentation/virtual/kvm/kvmi.rst +50 -0
@@ -252,3 +252,53 @@ Returns the introspection API version. This command is always allowed and successful (if the introspection is built in kernel). + +2. KVMI_CONTROL_CMD_RESPONSE +---------------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_control_cmd_response { + __u8 enable; + __u8 now; + __u16 padding1; + __u32 padding2; + }; + +:Returns: + +:: + struct kvmi_error_code + +Enables or disables the command replies. By default, all commands need +a reply. + +If `now` is 1, the command reply is enabled/disabled (according to +`enable`) starting with the current command. For example, `enable=0` +and `now=1` means that the reply is disabled for this command too, +while `enable=0` and `now=0` means that a reply will be send for this +command, but not for the next ones (until enabled back with another +*KVMI_CONTROL_CMD_RESPONSE*). + +This command is used by the introspection tool to disable the replies +for commands returning an error code only (eg. *KVMI_SET_REGISTERS*) +when an error is less likely to happen. For example, the following +commands can be used to reply to an event with a single `write()` call: + + KVMI_CONTROL_CMD_RESPONSE enable=0 now=1 + KVMI_SET_REGISTERS vcpu=N + KVMI_EVENT_REPLY vcpu=N + KVMI_CONTROL_CMD_RESPONSE enable=1 now=0 + +While the command reply is disabled: + +* the socket will be closed on any command for which the reply should + contain more than just an error code (eg. *KVMI_GET_REGISTERS*) + +* the reply status is ignored for any unsupported/unknown or disallowed + commands (and ``struct kvmi_error_code`` will be sent with -KVM_EOPNOTSUPP + or -KVM_PERM).
include/uapi/linux/kvmi.h +7 -0
@@ -83,4 +83,11 @@ struct kvmi_get_version_reply { __u32 padding; }; +struct kvmi_control_cmd_response { +__u8 enable; +__u8 now; +__u16 padding1; +__u32 padding2; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi_int.h +2 -0
@@ -85,6 +85,8 @@ struct kvmi {DECLARE_BITMAP (cmd_allow_mask,KVMI_NUM_COMMANDS );DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS ); + +bool cmd_reply_disabled; };/* kvmi_msg.c */
virt/kvm/kvmi_msg.c +57 -0
80 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 7/92 kvm: introspection: honor the reply option when handling the KVMI_GET_VERSION command
In reply to Adalbert LazărObviously, the KVMI_GET_VERSION command must not be used when the command reply is disabled by a previous KVMI_CONTROL_CMD_RESPONSE command.
This commit changes the code path in order to check the reply option (enabled/disabled) before trying to reply to this command. If the command reply is disabled it will return an error to the caller. In the end, the receiving worker will finish and the introspection socket will be closed.
--- virt/kvm/kvmi_msg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
virt/kvm/kvmi_msg.c +1 -1
@@ -169,7 +169,7 @@ static int handle_get_version(struct kvmi *ikvm,memset (&rpl,0 ,sizeof (rpl)); rpl.version =KVMI_VERSION ; -return kvmi_msg_vm_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); +return kvmi_msg_vm_maybe_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); }static bool is_command_allowed (struct kvmi *ikvm,int id)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 8/92 kvm: introspection: add KVMI_CHECK_COMMAND and KVMI_CHECK_EVENT
In reply to Adalbert LazărThese commands can be used by the introspection tool to check what introspection commands and events are supported (by KVMi) and allowed (by userspace/QEMU).
The introspection tool will get one of the following error codes:
* -KVM_EOPNOTSUPP (unsupported command/event)
* -KVM_PERM (disallowed command/event)
* -KVM_EINVAL (the padding space, used for future extensions, is not zero)
* 0 (the command/event is supported and allowed)
These commands can be seen as an alternative method to KVMI_GET_VERSION in checking if the introspection supports a specific command/event.
As with the KVMI_GET_VERSION command, these commands can never be disallowed by userspace/QEMU.
--- Documentation/virtual/kvm/kvmi.rst | 60 ++++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 12 ++++++ virt/kvm/kvmi.c | 8 +++- virt/kvm/kvmi_msg.c | 38 +++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/kvmi.rst +60 -0
@@ -302,3 +302,63 @@ While the command reply is disabled: * the reply status is ignored for any unsupported/unknown or disallowed commands (and ``struct kvmi_error_code`` will be sent with -KVM_EOPNOTSUPP or -KVM_PERM). + +3. KVMI_CHECK_COMMAND +--------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_check_command { + __u16 id; + __u16 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Checks if the command specified by ``id`` is allowed. + +This command is always allowed. + +:Errors: + +* -KVM_PERM - the command specified by ``id`` is disallowed +* -KVM_EINVAL - padding is not zero + +4. KVMI_CHECK_EVENT +------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_check_event { + __u16 id; + __u16 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Checks if the event specified by ``id`` is allowed. + +This command is always allowed. + +:Errors: + +* -KVM_PERM - the event specified by ``id`` is disallowed +* -KVM_EINVAL - padding is not zero
include/uapi/linux/kvmi.h +12 -0
@@ -90,4 +90,16 @@ struct kvmi_control_cmd_response { __u32 padding2; }; +struct kvmi_check_command { +__u16 id; +__u16 padding1; +__u32 padding2; +}; + +struct kvmi_check_event { +__u16 id; +__u16 padding1; +__u32 padding2; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +7 -1
@@ -69,6 +69,8 @@ static bool alloc_kvmi(struct kvm *kvm, const struct kvm_introspection *qemu)return false;set_bit (KVMI_GET_VERSION , ikvm->cmd_allow_mask); +set_bit (KVMI_CHECK_COMMAND , ikvm->cmd_allow_mask); +set_bit (KVMI_CHECK_EVENT , ikvm->cmd_allow_mask);memcpy (&ikvm->uuid, &qemu->uuid,sizeof (ikvm->uuid));
@@ -295,10 +297,14 @@ int kvmi_ioctl_command(struct kvm *kvm, void __user *argp)if (!allow) {DECLARE_BITMAP (always_allowed,KVMI_NUM_COMMANDS ); -if (id ==KVMI_GET_VERSION ) +if (id ==KVMI_GET_VERSION + || id ==KVMI_CHECK_COMMAND + || id ==KVMI_CHECK_EVENT )return -EPERM ;set_bit (KVMI_GET_VERSION , always_allowed); +set_bit (KVMI_CHECK_COMMAND , always_allowed); +set_bit (KVMI_CHECK_EVENT , always_allowed);bitmap_andnot (requested, requested, always_allowed,KVMI_NUM_COMMANDS );
virt/kvm/kvmi_msg.c +38 -0
@@ -9,6 +9,8 @@#include "kvmi_int.h" static const char *const msg_IDs[] = { + [KVMI_CHECK_COMMAND ] ="KVMI_CHECK_COMMAND" , + [KVMI_CHECK_EVENT ] ="KVMI_CHECK_EVENT" , [KVMI_CONTROL_CMD_RESPONSE ] ="KVMI_CONTROL_CMD_RESPONSE" , [KVMI_GET_VERSION ] ="KVMI_GET_VERSION" , };
@@ -177,6 +179,40 @@ static bool is_command_allowed(struct kvmi *ikvm, int id)return test_bit (id, ikvm->cmd_allow_mask); } +static int handle_check_command (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +const struct kvmi_check_command *req = _req; +int ec =0 ; + +if (req->padding1 || req->padding2) + ec = -KVM_EINVAL ; +else if (!is_command_allowed (ikvm, req->id)) + ec = -KVM_EPERM ; + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} + +static bool is_event_allowed (struct kvmi *ikvm,int id) +{ +return test_bit (id, ikvm->event_allow_mask); +} + +static int handle_check_event (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg,const void *_req) +{ +const struct kvmi_check_event *req = _req; +int ec =0 ; + +if (req->padding1 || req->padding2) + ec = -KVM_EINVAL ; +else if (!is_event_allowed (ikvm, req->id)) + ec = -KVM_EPERM ; + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} +static int handle_control_cmd_response (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg,const void *_req)
@@ -207,6 +243,8 @@ static int handle_control_cmd_response(struct kvmi *ikvm,*/ static int (*const msg_vm[])(struct kvmi *,const struct kvmi_msg_hdr *,const void *) = { + [KVMI_CHECK_COMMAND ] = handle_check_command, + [KVMI_CHECK_EVENT ] = handle_check_event, [KVMI_CONTROL_CMD_RESPONSE ] = handle_control_cmd_response, [KVMI_GET_VERSION ] = handle_get_version, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 9/92 kvm: introspection: add KVMI_GET_GUEST_INFO
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
For now, this command returns only the number of online vCPUs.
--- Documentation/virtual/kvm/kvmi.rst | 18 ++++++++++++++++++ include/uapi/linux/kvmi.h | 5 +++++ virt/kvm/kvmi_msg.c | 14 ++++++++++++++ 3 files changed, 37 insertions(+)
Documentation/virtual/kvm/kvmi.rst +18 -0
@@ -362,3 +362,21 @@ This command is always allowed. * -KVM_PERM - the event specified by ``id`` is disallowed * -KVM_EINVAL - padding is not zero + +5. KVMI_GET_GUEST_INFO +---------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters:: none +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_guest_info_reply { + __u32 vcpu_count; + __u32 padding[3]; + }; + +Returns the number of online vCPUs.
include/uapi/linux/kvmi.h +5 -0
@@ -102,4 +102,9 @@ struct kvmi_check_event { __u32 padding2; }; +struct kvmi_get_guest_info_reply { +__u32 vcpu_count; +__u32 padding[3 ]; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi_msg.c +14 -0
@@ -12,6 +12,7 @@ static const char *const msg_IDs[] = { [KVMI_CHECK_COMMAND] = "KVMI_CHECK_COMMAND", [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", + [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", };
@@ -213,6 +214,18 @@ static int handle_check_event(struct kvmi *ikvm,return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); } +static int handle_get_guest_info (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *req) +{ +struct kvmi_get_guest_info_reply rpl; + +memset (&rpl,0 ,sizeof (rpl)); + rpl.vcpu_count =atomic_read (&ikvm->kvm->online_vcpus); + +return kvmi_msg_vm_maybe_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); +} +static int handle_control_cmd_response (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg,const void *_req)
@@ -246,6 +259,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CHECK_COMMAND] = handle_check_command, [KVMI_CHECK_EVENT] = handle_check_event, [KVMI_CONTROL_CMD_RESPONSE] = handle_control_cmd_response, + [KVMI_GET_GUEST_INFO] = handle_get_guest_info, [KVMI_GET_VERSION] = handle_get_version, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 10/92 kvm: introspection: add KVMI_CONTROL_VM_EVENTS
In reply to Adalbert LazărNo introspection event (neither VM event, nor vCPU event) will be sent to the introspection tool unless enabled/requested.
This command enables/disables VM events. For now, these events are:
* KVMI_EVENT_UNHOOK
* KVMI_EVENT_CREATE_VCPU
The first event is initiated by userspace/QEMU in order to give the introspection tool a chance to remove its hooks in the event of pause/suspend/migrate.
The second event is actually a vCPU event, added to cover the case when the introspection tool has paused all vCPUs and userspace hotplugs (and starts) another one. The event is controlled by this command because its status (enabled/disabled) is kept in the VM related structures (as opposed to vCPU related structures). I didn't had a better idea. Not to mention that, the vCPU events are controlled with commands like "enable/disable event X for vCPU Y" and Y is _unknown_ for X=KVMI_EVENT_CREATE_VCPU.
--- Documentation/virtual/kvm/kvmi.rst | 39 ++++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 7 ++++++ virt/kvm/kvmi.c | 11 +++++++++ virt/kvm/kvmi_int.h | 3 +++ virt/kvm/kvmi_msg.c | 23 ++++++++++++++++++ 5 files changed, 83 insertions(+)
Documentation/virtual/kvm/kvmi.rst +39 -0
@@ -380,3 +380,42 @@ This command is always allowed. }; Returns the number of online vCPUs. + +6. KVMI_CONTROL_VM_EVENTS +------------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_control_vm_events { + __u16 event_id; + __u8 enable; + __u8 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Enables/disables VM introspection events. This command can be used with +the following events:: + + KVMI_EVENT_CREATE_VCPU + KVMI_EVENT_UNHOOK + +When an event is enabled, the introspection tool is notified and, +in almost all cases, it must reply with: continue, retry, crash, etc. +(see **Events** below). + +:Errors: + +* -KVM_EINVAL - the event ID is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EPERM - the access is restricted by the host +
include/uapi/linux/kvmi.h +7 -0
@@ -107,4 +107,11 @@ struct kvmi_get_guest_info_reply { __u32 padding[3]; }; +struct kvmi_control_vm_events { +__u16 event_id; +__u8 enable; +__u8 padding1; +__u32 padding2; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +11 -0
@@ -338,6 +338,17 @@ void kvmi_destroy_vm(struct kvm *kvm)wait_for_completion_killable (&kvm->kvmi_completed); } +int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id, +bool enable) +{ +if (enable) +set_bit (event_id, ikvm->vm_ev_mask); +else +clear_bit (event_id, ikvm->vm_ev_mask); + +return 0 ; +} +int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset) {struct kvmi *ikvm;
virt/kvm/kvmi_int.h +3 -0
@@ -85,6 +85,7 @@ struct kvmi {DECLARE_BITMAP (cmd_allow_mask,KVMI_NUM_COMMANDS );DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS ); +DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS );bool cmd_reply_disabled; };
@@ -99,5 +100,7 @@ bool kvmi_msg_process(struct kvmi *ikvm);void *kvmi_msg_alloc (void );void *kvmi_msg_alloc_check (size_t size);void kvmi_msg_free (void *addr); +int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id, +bool enable);#endif
virt/kvm/kvmi_msg.c +23 -0
@@ -12,6 +12,7 @@ static const char *const msg_IDs[] = { [KVMI_CHECK_COMMAND] = "KVMI_CHECK_COMMAND", [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", + [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", };
@@ -226,6 +227,27 @@ static int handle_get_guest_info(struct kvmi *ikvm,return kvmi_msg_vm_maybe_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); } +static int handle_control_vm_events (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +const unsigned long known_events =KVMI_KNOWN_VM_EVENTS ; +const struct kvmi_control_vm_events *req = _req; +int ec; + +if (req->padding1 || req->padding2) + ec = -KVM_EINVAL ; +else if (!test_bit (req->event_id, &known_events)) + ec = -KVM_EINVAL ; +else if (!is_event_allowed (ikvm, req->event_id)) + ec = -KVM_EPERM ; +else + ec =kvmi_cmd_control_vm_events (ikvm, req->event_id, + req->enable); + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} +static int handle_control_cmd_response (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg,const void *_req)
@@ -259,6 +281,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CHECK_COMMAND] = handle_check_command, [KVMI_CHECK_EVENT] = handle_check_event, [KVMI_CONTROL_CMD_RESPONSE] = handle_control_cmd_response, + [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, [KVMI_GET_VERSION] = handle_get_version, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 11/92 kvm: introspection: add vCPU related data
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
An opaque pointer is added to struct kvm_vcpu, pointing to its coresponding introspection structure, allocated (a) when the introspection socket is connected or (b) when the vCPU is hotpluged and deallocated when the introspection socket is disconnected.
--- include/linux/kvm_host.h | 1 + include/linux/kvmi.h | 4 +++ virt/kvm/kvm_main.c | 8 +++++ virt/kvm/kvmi.c | 73 +++++++++++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 5 +++ 5 files changed, 90 insertions(+), 1 deletion(-)
include/linux/kvm_host.h +1 -0
@@ -275,6 +275,7 @@ struct kvm_vcpu {bool preempted;struct kvm_vcpu_arch arch;struct dentry *debugfs_dentry; +void *kvmi; };static inline int kvm_vcpu_exiting_guest_mode (struct kvm_vcpu *vcpu)
include/linux/kvmi.h +4 -0
@@ -14,6 +14,8 @@ int kvmi_ioctl_hook(struct kvm *kvm, void __user *argp);int kvmi_ioctl_command (struct kvm *kvm,void __user *argp);int kvmi_ioctl_event (struct kvm *kvm,void __user *argp);int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset); +int kvmi_vcpu_init (struct kvm_vcpu *vcpu); +void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu);#else
@@ -21,6 +23,8 @@ static inline int kvmi_init(void) { return 0; }static inline void kvmi_uninit (void ) { }static inline void kvmi_create_vm (struct kvm *kvm) { }static inline void kvmi_destroy_vm (struct kvm *kvm) { } +static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; } +static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }#endif /* CONFIG_KVM_INTROSPECTION */
virt/kvm/kvm_main.c +8 -0
@@ -316,6 +316,13 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) r =kvm_arch_vcpu_init (vcpu);if (r <0 ) goto fail_free_run; + + r =kvmi_vcpu_init (vcpu); +if (r <0 ) { +kvm_arch_vcpu_uninit (vcpu); + goto fail_free_run; + } +return 0 ; fail_free_run:
@@ -333,6 +340,7 @@ void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)* descriptors are already gone. */ put_pid (rcu_dereference_protected (vcpu->pid,1 )); +kvmi_vcpu_uninit (vcpu);kvm_arch_vcpu_uninit (vcpu);free_page ((unsigned long )vcpu->run); }
virt/kvm/kvmi.c +72 -1
106 lines not on this page · raw message
virt/kvm/kvmi_int.h +5 -0
@@ -23,6 +23,8 @@#define kvmi_err (ikvm, fmt, ...) \ kvm_info("%pU ERROR: " fmt, &ikvm->uuid, ## __VA_ARGS__) +#define IVCPU (vcpu) ((struct kvmi_vcpu *)((vcpu)->kvmi)) +#define KVMI_MSG_SIZE_ALLOC (sizeof(struct kvmi_msg_hdr) + KVMI_MSG_SIZE)#define KVMI_KNOWN_VCPU_EVENTS ( \
@@ -73,6 +75,9 @@#define KVMI_NUM_COMMANDS KVMI_NEXT_AVAILABLE_COMMAND +struct kvmi_vcpu { +}; +#define IKVM (kvm) ((struct kvmi *)((kvm)->kvmi))struct kvmi {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 12/92 kvm: introspection: add a jobs list to every introspected vCPU
In reply to Adalbert LazărEvery vCPU has a lock-protected list in which (mostly) the receiving worker places the jobs to be done by the vCPU once it is kicked (KVM_REQ_INTROSPECTION) out of guest.
A job is defined by a "do" function, a pointer (context) and a "free" function.
--- arch/x86/include/asm/kvm_host.h | 1 + virt/kvm/kvmi.c | 102 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 9 +++ 3 files changed, 111 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -78,6 +78,7 @@#define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22)#define KVM_REQ_LOAD_EOI_EXITMAP KVM_ARCH_REQ(23)#define KVM_REQ_GET_VMCS12_PAGES KVM_ARCH_REQ(24) +#define KVM_REQ_INTROSPECTION KVM_ARCH_REQ(25)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
virt/kvm/kvmi.c +101 -1
157 lines not on this page · raw message
virt/kvm/kvmi_int.h +9 -0
@@ -75,7 +75,16 @@#define KVMI_NUM_COMMANDS KVMI_NEXT_AVAILABLE_COMMAND +struct kvmi_job { +struct list_head link; +void *ctx; +void (*fct)(struct kvm_vcpu *vcpu,void *ctx); +void (*free_fct)(void *ctx); +}; +struct kvmi_vcpu { +struct list_head job_list; +spinlock_t job_lock; };#define IKVM (kvm) ((struct kvmi *)((kvm)->kvmi))
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 13/92 kvm: introspection: make the vCPU wait even when its jobs list is empty
In reply to Adalbert LazărUsually, the vCPU thread will run the functions from its jobs list (unless the thread is SIGKILL-ed) and continue to guest when the list is empty. But, there are cases when it has to wait for something (e.g. another vCPU runs in single-step mode, or the current vCPU waits for an event reply from the introspection tool).
In these cases, it will append a "wait job" into its own list, which will do (a) nothing if the list is not empty or it doesn't have to wait any longer or (b) wait (in the same wake-queue used by KVM) until it is kicked. It should be OK if the receiving worker appends a new job in the same time.
--- include/linux/swait.h | 11 ++++++ virt/kvm/kvmi.c | 80 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 2 ++ 3 files changed, 93 insertions(+)
include/linux/swait.h +11 -0
@@ -297,4 +297,15 @@ do { \ __ret; \ }) +#define __swait_event_killable (wq, condition) \ + ___swait_event(wq, condition, TASK_KILLABLE, 0, schedule()) \ + +#define swait_event_killable (wq, condition) \ +({ \ + int __ret = 0; \ + if (!(condition)) \ + __ret = __swait_event_killable(wq, condition); \ + __ret; \ +}) +#endif /* _LINUX_SWAIT_H */
virt/kvm/kvmi.c +80 -0
92 lines not on this page · raw message
virt/kvm/kvmi_int.h +2 -0
@@ -85,6 +85,8 @@ struct kvmi_job {struct kvmi_vcpu {struct list_head job_list;spinlock_t job_lock; + +bool killed; };#define IKVM (kvm) ((struct kvmi *)((kvm)->kvmi))
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 14/92 kvm: introspection: handle introspection commands before returning to guest
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
The introspection requests (KVM_REQ_INTROSPECTION) are checked by any introspected vCPU in two places:
* on its way to guest - vcpu_enter_guest()
* when halted - kvm_vcpu_block()
In kvm_vcpu_block(), we check to see if there are any introspection requests during the swait loop, handle them outside of swait loop and start swait again.
--- arch/x86/kvm/x86.c | 3 +++ include/linux/kvmi.h | 2 ++ virt/kvm/kvm_main.c | 28 ++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-)
arch/x86/kvm/x86.c +3 -0
@@ -7742,6 +7742,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)*/ if (kvm_check_request (KVM_REQ_HV_STIMER , vcpu))kvm_hv_process_stimers (vcpu); + +if (kvm_check_request (KVM_REQ_INTROSPECTION , vcpu)) +kvmi_handle_requests (vcpu); }if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {
include/linux/kvmi.h +2 -0
@@ -16,6 +16,7 @@ int kvmi_ioctl_event(struct kvm *kvm, void __user *argp);int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset);int kvmi_vcpu_init (struct kvm_vcpu *vcpu);void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu); +void kvmi_handle_requests (struct kvm_vcpu *vcpu);#else
@@ -25,6 +26,7 @@ static inline void kvmi_create_vm(struct kvm *kvm) { }static inline void kvmi_destroy_vm (struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { } +static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }#endif /* CONFIG_KVM_INTROSPECTION */
virt/kvm/kvm_main.c +22 -6
@@ -2282,16 +2282,32 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)kvm_arch_vcpu_blocking (vcpu);for (;;) { -prepare_to_swait_exclusive (&vcpu->wq, &wait,TASK_INTERRUPTIBLE ); +bool do_kvmi_work = false; -if (kvm_vcpu_check_block (vcpu) <0 ) -break ; +for (;;) { +prepare_to_swait_exclusive (&vcpu->wq, &wait, +TASK_INTERRUPTIBLE ); + +if (kvm_vcpu_check_block (vcpu) <0 ) +break ; + + waited = true; +schedule (); + +if (kvm_check_request (KVM_REQ_INTROSPECTION , vcpu)) { + do_kvmi_work = true; +break ; + } + } - waited = true; -schedule (); +finish_swait (&vcpu->wq, &wait); + +if (do_kvmi_work) +kvmi_handle_requests (vcpu); +else +break ; } -finish_swait (&vcpu->wq, &wait); cur =ktime_get ();kvm_arch_vcpu_unblocking (vcpu);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 15/92 kvm: introspection: handle vCPU related introspection commands
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
Following the common structure used for all messages (kvmi_msg_hdr), all vCPU related commands have another common structure (kvmi_vcpu_hdr). This allows the receiving worker to validate and dispatch the message to the proper vCPU (adding the handling function to its jobs list).
--- Documentation/virtual/kvm/kvmi.rst | 8 ++ include/uapi/linux/kvm_para.h | 4 +- include/uapi/linux/kvmi.h | 6 ++ virt/kvm/kvmi_int.h | 3 + virt/kvm/kvmi_msg.c | 159 ++++++++++++++++++++++++++++- 5 files changed, 177 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/kvmi.rst +8 -0
@@ -232,6 +232,14 @@ The following C structures are meant to be used directly when communicating over the wire. The peer that detects any size mismatch should simply close the connection and report the error. +The commands related to vCPU-s start with:: + + struct kvmi_vcpu_hdr { + __u16 vcpu; + __u16 padding1; + __u32 padding2; + } + 1. KVMI_GET_VERSION -------------------
include/uapi/linux/kvm_para.h +3 -1
@@ -10,13 +10,15 @@* - kvm_para_available */ -/* Return values for hypercalls */ +/* Return values for hypercalls and VM introspection */ #define KVM_ENOSYS 1000#define KVM_EFAULT EFAULT#define KVM_EINVAL EINVAL#define KVM_E2BIG E2BIG#define KVM_EPERM EPERM#define KVM_EOPNOTSUPP 95 +#define KVM_EAGAIN 11 +#define KVM_ENOMEM ENOMEM#define KVM_HC_VAPIC_POLL_IRQ 1#define KVM_HC_MMU_OP 2
include/uapi/linux/kvmi.h +6 -0
@@ -114,4 +114,10 @@ struct kvmi_control_vm_events { __u32 padding2; }; +struct kvmi_vcpu_hdr { +__u16 vcpu; +__u16 padding1; +__u32 padding2; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi_int.h +3 -0
@@ -118,5 +118,8 @@ void *kvmi_msg_alloc_check(size_t size);void kvmi_msg_free (void *addr);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable); +int kvmi_add_job (struct kvm_vcpu *vcpu, +void (*fct)(struct kvm_vcpu *vcpu,void *ctx), +void *ctx,void (*free_fct)(void *ctx));#endif
virt/kvm/kvmi_msg.c +157 -2
209 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 16/92 kvm: introspection: handle events and event replies
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
All events are sent by the vCPU thread, which will handle any introspection command while waiting for the reply.
The event reply messages contain a common strucure (kvmi_vcpu_hdr), as any vCPU related command, which allows the receiving worker to dispatch the reply as it does with any other introspection command sent for a specific vCPU.
The kernel side will gracefully handle commands coming from an introspection tool compiled with older or newer versions of KVMI API. However, it will only accept smaller replies (coming from older versions), but not the bigger/newer ones (this should make the kernel code simpler).
TODO: Not quite true. An event reply has a common part (kvmi_event_reply) and an event specific part (eg. the new value for MSR x). If the common part is smaller, the event will be rejected.
The code from handle_event_reply():
common = sizeof(struct kvmi_vcpu_hdr) + sizeof(*reply); if (unlikely(msg->size < common)) goto out;
should be changed to
min_common = sizeof(struct kvmi_vcpu_hdr) + offsetof(reply...) if (unlikely(msg->size < min_common)) goto out;
--- Documentation/virtual/kvm/kvmi.rst | 56 +++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 29 +++++++ arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/kvmi.c | 92 ++++++++++++++++++++ arch/x86/kvm/x86.c | 10 +++ include/linux/kvm_host.h | 3 + include/uapi/linux/kvmi.h | 16 ++++ virt/kvm/kvmi.c | 15 ++++ virt/kvm/kvmi_int.h | 16 ++++ virt/kvm/kvmi_msg.c | 129 +++++++++++++++++++++++++++++ 10 files changed, 367 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/uapi/asm/kvmi.h create mode 100644 arch/x86/kvm/kvmi.c
Documentation/virtual/kvm/kvmi.rst +56 -0
@@ -427,3 +427,59 @@ in almost all cases, it must reply with: continue, retry, crash, etc. * -KVM_EINVAL - padding is not zero * -KVM_EPERM - the access is restricted by the host +Events +====== + +All vCPU events are sent using the *KVMI_EVENT* message id. No event +will be sent unless explicitly enabled with a *KVMI_CONTROL_EVENTS* +or a *KVMI_CONTROL_VM_EVENTS* command or requested, as it is the case +with the *KVMI_EVENT_PAUSE_VCPU* event (see **KVMI_PAUSE_VCPU**). + +There is one VM event, *KVMI_EVENT_UNHOOK*, which doesn't have a reply, +but shares the kvmi_event structure, for consistency with the vCPU events. + +The message data begins with a common structure, having the size of the +structure, the vCPU index and the event id:: + + struct kvmi_event { + __u16 size; + __u16 vcpu; + __u8 event; + __u8 padding[3]; + struct kvmi_event_arch arch; + } + +On x86 the structure looks like this:: + + struct kvmi_event_arch { + __u8 mode; + __u8 padding[7]; + struct kvm_regs regs; + struct kvm_sregs sregs; + struct { + __u64 sysenter_cs; + __u64 sysenter_esp; + __u64 sysenter_eip; + __u64 efer; + __u64 star; + __u64 lstar; + __u64 cstar; + __u64 pat; + __u64 shadow_gs; + } msrs; + }; + +It contains information about the vCPU state at the time of the event. + +The reply to events have the *KVMI_EVENT_REPLY* message id and begins +with two common structures:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply { + __u8 action; + __u8 event; + __u16 padding1; + __u32 padding2; + }; + +Specific data can follow these common structures.
arch/x86/include/uapi/asm/kvmi.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_ASM_X86_KVMI_H +#define _UAPI_ASM_X86_KVMI_H + +/* +* KVM introspection - x86 specific structures and definitions +*/ + +#include <asm/kvm.h> + +struct kvmi_event_arch { +__u8 mode;/* 2, 4 or 8 */ +__u8 padding[7 ]; +struct kvm_regs regs; +struct kvm_sregs sregs; +struct { +__u64 sysenter_cs; +__u64 sysenter_esp; +__u64 sysenter_eip; +__u64 efer; +__u64 star; +__u64 lstar; +__u64 cstar; +__u64 pat; +__u64 shadow_gs; + } msrs; +}; + +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/Makefile +1 -1
@@ -7,7 +7,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o -kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o +kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o kvmi.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
arch/x86/kvm/kvmi.c new file +92 -0
92 lines not on this page · raw message
arch/x86/kvm/x86.c +10 -0
@@ -8244,6 +8244,11 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return 0 ; } +void kvm_arch_vcpu_get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs) +{ +__get_regs (vcpu, regs); +} +static void __set_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs) { vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
@@ -8339,6 +8344,11 @@ int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,return 0 ; } +void kvm_arch_vcpu_get_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs) +{ +__get_sregs (vcpu, sregs); +} +int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) {
include/linux/kvm_host.h +3 -0
@@ -788,9 +788,12 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, struct kvm_translation *tr);int kvm_arch_vcpu_ioctl_get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs); +void kvm_arch_vcpu_get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs);int kvm_arch_vcpu_ioctl_set_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs);int kvm_arch_vcpu_ioctl_get_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs); +void kvm_arch_vcpu_get_sregs (struct kvm_vcpu *vcpu, +struct kvm_sregs *sregs);int kvm_arch_vcpu_ioctl_set_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs); int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
include/uapi/linux/kvmi.h +16 -0
@@ -8,6 +8,7 @@#include <linux/kernel.h> #include <linux/types.h> +#include <asm/kvmi.h> #define KVMI_VERSION 0x00000001
@@ -120,4 +121,19 @@ struct kvmi_vcpu_hdr { __u32 padding2; }; +struct kvmi_event { +__u16 size; +__u16 vcpu; +__u8 event; +__u8 padding[3 ]; +struct kvmi_event_arch arch; +}; + +struct kvmi_event_reply { +__u8 action; +__u8 event; +__u16 padding1; +__u32 padding2; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +15 -0
@@ -76,6 +76,8 @@ static bool alloc_kvmi(struct kvm *kvm, const struct kvm_introspection *qemu)if (!ikvm)return false; +atomic_set (&ikvm->ev_seq,0 ); +set_bit (KVMI_GET_VERSION , ikvm->cmd_allow_mask);set_bit (KVMI_CHECK_COMMAND , ikvm->cmd_allow_mask);set_bit (KVMI_CHECK_EVENT , ikvm->cmd_allow_mask);
@@ -520,10 +522,20 @@ void kvmi_run_jobs(struct kvm_vcpu *vcpu) } } +static bool need_to_wait (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +return ivcpu->reply_waiting; +} +static bool done_waiting (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +if (!need_to_wait (vcpu)) +return true; +return !list_empty (&ivcpu->job_list); }
@@ -552,6 +564,9 @@ int kvmi_run_jobs_and_wait(struct kvm_vcpu *vcpu)break ; } +if (!need_to_wait (vcpu)) +break ; +kvmi_add_job (vcpu, kvmi_job_wait,NULL ,NULL ); }
virt/kvm/kvmi_int.h +16 -0
@@ -82,7 +82,18 @@ struct kvmi_job {void (*free_fct)(void *ctx); }; +struct kvmi_vcpu_reply { +int error; +int action; +u32 seq; +void *data; +size_t size; +}; +struct kvmi_vcpu { +bool reply_waiting; +struct kvmi_vcpu_reply reply; +struct list_head job_list;spinlock_t job_lock;
@@ -96,6 +107,7 @@ struct kvmi {struct socket *sock;struct task_struct *recv; +atomic_t ev_seq;uuid_t uuid;
@@ -118,8 +130,12 @@ void *kvmi_msg_alloc_check(size_t size);void kvmi_msg_free (void *addr);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable); +int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu);int kvmi_add_job (struct kvm_vcpu *vcpu,void (*fct)(struct kvm_vcpu *vcpu,void *ctx),void *ctx,void (*free_fct)(void *ctx)); +/* arch */ +void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev); +#endif
virt/kvm/kvmi_msg.c +129 -0
150 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 17/92 kvm: introspection: introduce event actions
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
All vCPU event replies contains the action requested by the introspection tool, which can be one of the following:
* KVMI_EVENT_ACTION_CONTINUE
* KVMI_EVENT_ACTION_RETRY
* KVMI_EVENT_ACTION_CRASH
The CONTINUE action can be seen as "continue with the old KVM code path", while the RETRY action as "re-enter guest".
Note: KVMI_EVENT_UNHOOK, a VM event, doesn't have/need a reply.
--- Documentation/virtual/kvm/kvmi.rst | 10 ++++++++ include/uapi/linux/kvmi.h | 4 +++ kernel/signal.c | 1 + virt/kvm/kvmi.c | 40 ++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+)
Documentation/virtual/kvm/kvmi.rst +10 -0
@@ -482,4 +482,14 @@ with two common structures:: __u32 padding2; }; +All events accept the KVMI_EVENT_ACTION_CRASH action, which stops the +guest ungracefully but as soon as possible. + +Most of the events accept the KVMI_EVENT_ACTION_CONTINUE action, which +lets the instruction that caused the event to continue (unless specified +otherwise). + +Some of the events accept the KVMI_EVENT_ACTION_RETRY action, to continue +by re-entering the guest. + Specific data can follow these common structures.
include/uapi/linux/kvmi.h +4 -0
@@ -66,6 +66,10 @@ enum {KVMI_NUM_EVENTS }; +#define KVMI_EVENT_ACTION_CONTINUE 0 +#define KVMI_EVENT_ACTION_RETRY 1 +#define KVMI_EVENT_ACTION_CRASH 2 +#define KVMI_MSG_SIZE (4096 - sizeof(struct kvmi_msg_hdr))struct kvmi_msg_hdr {
kernel/signal.c +1 -0
@@ -1413,6 +1413,7 @@ int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)*/ } } +EXPORT_SYMBOL (kill_pid_info);static int kill_proc_info (int sig,struct kernel_siginfo *info,pid_t pid) {
virt/kvm/kvmi.c +40 -0
@@ -511,6 +511,46 @@ void kvmi_destroy_vm(struct kvm *kvm)wait_for_completion_killable (&kvm->kvmi_completed); } +static int kvmi_vcpu_kill (int sig,struct kvm_vcpu *vcpu) +{ +int err = -ESRCH ; +struct pid *pid; +struct kernel_siginfo siginfo[1 ] = {}; + +rcu_read_lock (); + pid =rcu_dereference (vcpu->pid); +if (pid) + err =kill_pid_info (sig, siginfo, pid); +rcu_read_unlock (); + +return err; +} + +static void kvmi_vm_shutdown (struct kvm *kvm) +{ +int i; +struct kvm_vcpu *vcpu; + +kvm_for_each_vcpu (i, vcpu, kvm) +kvmi_vcpu_kill (SIGTERM , vcpu); +} + +void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action, +const char *str) +{ +struct kvm *kvm = vcpu->kvm; + +switch (action) { +case KVMI_EVENT_ACTION_CRASH : +kvmi_vm_shutdown (kvm); +break ; + +default : +kvmi_err (IKVM (kvm),"Unsupported action %d for event %s\n" , + action, str); + } +} +void kvmi_run_jobs (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 18/92 kvm: introspection: add KVMI_EVENT_UNHOOK
In reply to Adalbert LazărIn certain situations (when the guest has to be paused, suspended, migrated, etc.), userspace/QEMU will use the KVM_INTROSPECTION_UNHOOK ioctl in order to trigger the KVMI_EVENT_UNHOOK. If the event is sent successfully (the VM has an active introspection channel), userspace should delay the action (pause/suspend/...) to give the introspection tool the chance to remove its hooks (eg. breakpoints). Once a timeout is reached or the introspection tool has closed the socket, QEMU should continue with the planned action.
--- Documentation/virtual/kvm/kvmi.rst | 20 ++++++++++++++++++ virt/kvm/kvmi.c | 34 +++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 1 + virt/kvm/kvmi_msg.c | 20 ++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/kvmi.rst +20 -0
@@ -493,3 +493,23 @@ Some of the events accept the KVMI_EVENT_ACTION_RETRY action, to continue by re-entering the guest. Specific data can follow these common structures. + +1. KVMI_EVENT_UNHOOK +-------------------- + +:Architecture: all +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: none + +This event is sent when the device manager (ie. QEMU) has to +pause/stop/migrate the guest (see **Unhooking**) and the introspection +has been enabled for this event (see **KVMI_CONTROL_VM_EVENTS**). +The introspection tool has a chance to unhook and close the KVMI channel +(signaling that the operation can proceed).
virt/kvm/kvmi.c +33 -1
@@ -644,6 +644,9 @@ int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,static void kvmi_job_abort (struct kvm_vcpu *vcpu,void *ctx) { +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + + ivcpu->reply_waiting = false; }static void kvmi_abort_events (struct kvm *kvm)
@@ -655,6 +658,34 @@ static void kvmi_abort_events(struct kvm *kvm)kvmi_add_job (vcpu, kvmi_job_abort,NULL ,NULL ); } +static bool __kvmi_unhook_event (struct kvmi *ikvm) +{ +int err; + +if (!test_bit (KVMI_EVENT_UNHOOK , ikvm->vm_ev_mask)) +return false; + + err =kvmi_msg_send_unhook (ikvm); + +return !err; +} + +static bool kvmi_unhook_event (struct kvm *kvm) +{ +struct kvmi *ikvm; +bool ret = true; + + ikvm =kvmi_get (kvm); +if (!ikvm) +return false; + + ret =__kvmi_unhook_event (ikvm); + +kvmi_put (kvm); + +return ret; +} +int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset) {struct kvmi *ikvm;
@@ -664,7 +695,8 @@ int kvmi_ioctl_unhook(struct kvm *kvm, bool force_reset)if (!ikvm)return -EFAULT ; -kvm_info ("TODO: %s force_reset %d" , __func__, force_reset); +if (!force_reset && !kvmi_unhook_event (kvm)) + err = -ENOENT ;kvmi_put (kvm);
virt/kvm/kvmi_int.h +1 -0
@@ -123,6 +123,7 @@ bool kvmi_sock_get(struct kvmi *ikvm, int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);void kvmi_sock_put (struct kvmi *ikvm);bool kvmi_msg_process (struct kvmi *ikvm); +int kvmi_msg_send_unhook (struct kvmi *ikvm);/* kvmi.c */ void *kvmi_msg_alloc (void );
virt/kvm/kvmi_msg.c +20 -0
@@ -705,3 +705,23 @@ int kvmi_send_event(struct kvm_vcpu *vcpu, u32 ev_id,return err; } +int kvmi_msg_send_unhook (struct kvmi *ikvm) +{ +struct kvmi_msg_hdr hdr; +struct kvmi_event common; +struct kvec vec[] = { + {.iov_base = &hdr, .iov_len =sizeof (hdr) }, + {.iov_base = &common, .iov_len =sizeof (common)}, + }; +size_t msg_size =sizeof (hdr) +sizeof (common); +size_t n =ARRAY_SIZE (vec); + +memset (&hdr,0 ,sizeof (hdr)); + hdr.id =KVMI_EVENT ; + hdr.seq =new_seq (ikvm); + hdr.size = msg_size -sizeof (hdr); + +kvmi_setup_event_common (&common,KVMI_EVENT_UNHOOK ,0 ); + +return kvmi_sock_write (ikvm, vec, n, msg_size); +}
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 19/92 kvm: introspection: add KVMI_EVENT_CREATE_VCPU
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
This event is sent when a vCPU is ready to be introspected.
--- Documentation/virtual/kvm/kvmi.rst | 23 +++++++++++++++ virt/kvm/kvmi.c | 47 ++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 1 + virt/kvm/kvmi_msg.c | 12 ++++++++ 4 files changed, 83 insertions(+)
Documentation/virtual/kvm/kvmi.rst +23 -0
@@ -513,3 +513,26 @@ pause/stop/migrate the guest (see **Unhooking**) and the introspection has been enabled for this event (see **KVMI_CONTROL_VM_EVENTS**). The introspection tool has a chance to unhook and close the KVMI channel (signaling that the operation can proceed). + +2. KVMI_EVENT_CREATE_VCPU +------------------------- + +:Architectures: all +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when a new vCPU is created and the introspection has +been enabled for this event (see *KVMI_CONTROL_VM_EVENTS*). +
virt/kvm/kvmi.c +47 -0
77 lines not on this page · raw message
virt/kvm/kvmi_int.h +1 -0
@@ -123,6 +123,7 @@ bool kvmi_sock_get(struct kvmi *ikvm, int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);void kvmi_sock_put (struct kvmi *ikvm);bool kvmi_msg_process (struct kvmi *ikvm); +u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu);int kvmi_msg_send_unhook (struct kvmi *ikvm);/* kvmi.c */
virt/kvm/kvmi_msg.c +12 -0
@@ -725,3 +725,15 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm)return kvmi_sock_write (ikvm, vec, n, msg_size); } + +u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu) +{ +int err, action; + + err =kvmi_send_event (vcpu,KVMI_EVENT_CREATE_VCPU ,NULL ,0 , +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +}
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 20/92 kvm: introspection: add KVMI_GET_VCPU_INFO
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
For now, this command returns the TSC frequency (in HZ) for the specified vCPU if available (otherwise it returns zero).
--- Documentation/virtual/kvm/kvmi.rst | 29 +++++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 12 ++++++++++++ include/uapi/linux/kvmi.h | 4 ++++ virt/kvm/kvmi_int.h | 2 ++ virt/kvm/kvmi_msg.c | 14 ++++++++++++++ 5 files changed, 61 insertions(+)
Documentation/virtual/kvm/kvmi.rst +29 -0
@@ -427,6 +427,35 @@ in almost all cases, it must reply with: continue, retry, crash, etc. * -KVM_EINVAL - padding is not zero * -KVM_EPERM - the access is restricted by the host +7. KVMI_GET_VCPU_INFO +--------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_vcpu_info_reply { + __u64 tsc_speed; + }; + +Returns the TSC frequency (in HZ) for the specified vCPU if available +(otherwise it returns zero). + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
arch/x86/kvm/kvmi.c +12 -0
@@ -90,3 +90,15 @@ void kvmi_arch_setup_event(struct kvm_vcpu *vcpu, struct kvmi_event *ev) ev->arch.mode =kvmi_vcpu_mode (vcpu, &event->sregs);kvmi_get_msrs (vcpu, event); } + +int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu, +struct kvmi_get_vcpu_info_reply *rpl) +{ +if (kvm_has_tsc_control) + rpl->tsc_speed =1000ul * vcpu->arch.virtual_tsc_khz; +else + rpl->tsc_speed =0 ; + +return 0 ; +} +
include/uapi/linux/kvmi.h +4 -0
@@ -112,6 +112,10 @@ struct kvmi_get_guest_info_reply {__u32 padding[3 ]; }; +struct kvmi_get_vcpu_info_reply { +__u64 tsc_speed; +}; +struct kvmi_control_vm_events {__u16 event_id;__u8 enable;
virt/kvm/kvmi_int.h +2 -0
@@ -139,5 +139,7 @@ int kvmi_add_job(struct kvm_vcpu *vcpu,/* arch */ void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev); +int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu, +struct kvmi_get_vcpu_info_reply *rpl);#endif
virt/kvm/kvmi_msg.c +14 -0
@@ -28,6 +28,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT] = "KVMI_EVENT", [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", + [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", };
@@ -390,6 +391,18 @@ static int handle_event_reply(struct kvm_vcpu *vcpu,return expected->error; } +static int handle_get_vcpu_info (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *req,vcpu_reply_fct reply_cb) +{ +struct kvmi_get_vcpu_info_reply rpl; + +memset (&rpl,0 ,sizeof (rpl)); +kvmi_arch_cmd_get_vcpu_info (vcpu, &rpl); + +return reply_cb (vcpu, msg,0 , &rpl,sizeof (rpl)); +} +/* * These commands are executed on the vCPU thread. The receiving thread * passes the messages using a newly allocated 'struct kvmi_vcpu_cmd'
@@ -400,6 +413,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, const struct kvmi_msg_hdr *, const void *, vcpu_reply_fct) = { [KVMI_EVENT_REPLY ] = handle_event_reply, + [KVMI_GET_VCPU_INFO ] = handle_get_vcpu_info, };static void kvmi_job_vcpu_cmd (struct kvm_vcpu *vcpu,void *_ctx)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 21/92 kvm: page track: add track_create_slot() callback
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is used to add page access notifications as soon as a slot appears.
--- arch/x86/include/asm/kvm_page_track.h | 5 ++++- arch/x86/kvm/page_track.c | 18 ++++++++++++++++-- arch/x86/kvm/x86.c | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_page_track.h +4 -1
@@ -34,6 +34,9 @@ struct kvm_page_track_notifier_node {*/ void (*track_write)(struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes,struct kvm_page_track_notifier_node *node); +void (*track_create_slot)(struct kvm *kvm,struct kvm_memory_slot *slot, +unsigned long npages, +struct kvm_page_track_notifier_node *node);/* * It is called when memory slot is being moved or removed * users can drop write-protection for the pages in that memory slot
@@ -51,7 +54,7 @@ void kvm_page_track_cleanup(struct kvm *kvm);void kvm_page_track_free_memslot (struct kvm_memory_slot *free,struct kvm_memory_slot *dont); -int kvm_page_track_create_memslot (struct kvm_memory_slot *slot, +int kvm_page_track_create_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,unsigned long npages); void kvm_slot_page_track_add_page(struct kvm *kvm,
arch/x86/kvm/page_track.c +16 -2
@@ -34,10 +34,13 @@ void kvm_page_track_free_memslot(struct kvm_memory_slot *free, } } -int kvm_page_track_create_memslot (struct kvm_memory_slot *slot, +int kvm_page_track_create_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,unsigned long npages) { -int i; +struct kvm_page_track_notifier_head *head; +struct kvm_page_track_notifier_node *n; +int idx; +int i;for (i =0 ; i <KVM_PAGE_TRACK_MAX ; i++) { slot->arch.gfn_track[i] =
@@ -47,6 +50,17 @@ int kvm_page_track_create_memslot(struct kvm_memory_slot *slot, goto track_free; } + head = &kvm->arch.track_notifier_head; + +if (hlist_empty (&head->track_notifier_list)) +return 0 ; + + idx =srcu_read_lock (&head->track_srcu); +hlist_for_each_entry_rcu (n, &head->track_notifier_list, node) +if (n->track_create_slot) + n->track_create_slot (kvm, slot, npages, n); +srcu_read_unlock (&head->track_srcu, idx); +return 0 ; track_free:
arch/x86/kvm/x86.c +1 -1
@@ -9350,7 +9350,7 @@ int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, } } -if (kvm_page_track_create_memslot (slot, npages)) +if (kvm_page_track_create_memslot (kvm, slot, npages)) goto out_free;return 0 ;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 22/92 kvm: x86: provide all page tracking hooks with the guest virtual address
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed because the emulator calls the page tracking code irrespective of the current VMEXIT reason or available information.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/kvm_page_track.h | 9 +++++---- arch/x86/kvm/mmu.c | 2 +- arch/x86/kvm/page_track.c | 6 +++--- arch/x86/kvm/x86.c | 16 ++++++++-------- drivers/gpu/drm/i915/gvt/kvmgt.c | 2 +- 6 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1263,7 +1263,7 @@ void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);int load_pdptrs (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,unsigned long cr3);bool pdptrs_changed (struct kvm_vcpu *vcpu); -int emulator_write_phys (struct kvm_vcpu *vcpu,gpa_t gpa, +int emulator_write_phys (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const void *val,int bytes);struct kvm_irq_mask_notifier {
arch/x86/include/asm/kvm_page_track.h +5 -4
@@ -32,8 +32,9 @@ struct kvm_page_track_notifier_node {* @bytes: the written length. * @node: this node */ -void (*track_write)(struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, -int bytes,struct kvm_page_track_notifier_node *node); +void (*track_write)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +const u8 *new,int bytes, +struct kvm_page_track_notifier_node *node);void (*track_create_slot)(struct kvm *kvm,struct kvm_memory_slot *slot,unsigned long npages,struct kvm_page_track_notifier_node *node);
@@ -72,7 +73,7 @@ kvm_page_track_register_notifier(struct kvm *kvm,void kvm_page_track_unregister_notifier (struct kvm *kvm,struct kvm_page_track_notifier_node *n); -void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, -int bytes); +void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +const u8 *new,int bytes);void kvm_page_track_flush_slot (struct kvm *kvm,struct kvm_memory_slot *slot);#endif
arch/x86/kvm/mmu.c +1 -1
@@ -5222,7 +5222,7 @@ static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte)return spte; } -static void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, +static void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const u8 *new,int bytes,struct kvm_page_track_notifier_node *node) {
arch/x86/kvm/page_track.c +3 -3
@@ -236,8 +236,8 @@ EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);* The node should figure out if the written page is the one that node is * interested in by itself. */ -void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, -int bytes) +void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +const u8 *new,int bytes) {struct kvm_page_track_notifier_head *head;struct kvm_page_track_notifier_node *n;
@@ -251,7 +251,7 @@ void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, idx =srcu_read_lock (&head->track_srcu);hlist_for_each_entry_rcu (n, &head->track_notifier_list, node)if (n->track_write) - n->track_write (vcpu, gpa, new, bytes, n); + n->track_write (vcpu, gpa, gva, new, bytes, n);srcu_read_unlock (&head->track_srcu, idx); }
arch/x86/kvm/x86.c +8 -8
@@ -5281,7 +5281,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,return vcpu_is_mmio_gpa (vcpu, gva, *gpa, write); } -int emulator_write_phys (struct kvm_vcpu *vcpu,gpa_t gpa, +int emulator_write_phys (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const void *val,int bytes) {int ret;
@@ -5289,14 +5289,14 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, ret =kvm_vcpu_write_guest (vcpu, gpa, val, bytes);if (ret <0 )return 0 ; -kvm_page_track_write (vcpu, gpa, val, bytes); +kvm_page_track_write (vcpu, gpa, gva, val, bytes);return 1 ; }struct read_write_emulator_ops {int (*read_write_prepare)(struct kvm_vcpu *vcpu,void *val,int bytes); -int (*read_write_emulate)(struct kvm_vcpu *vcpu,gpa_t gpa, +int (*read_write_emulate)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,void *val,int bytes);int (*read_write_mmio)(struct kvm_vcpu *vcpu,gpa_t gpa,int bytes,void *val);
@@ -5317,16 +5317,16 @@ static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)return 0 ; } -static int read_emulate (struct kvm_vcpu *vcpu,gpa_t gpa, +static int read_emulate (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,void *val,int bytes) {return !kvm_vcpu_read_guest (vcpu, gpa, val, bytes); } -static int write_emulate (struct kvm_vcpu *vcpu,gpa_t gpa, +static int write_emulate (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,void *val,int bytes) { -return emulator_write_phys (vcpu, gpa, val, bytes); +return emulator_write_phys (vcpu, gpa, gva, val, bytes); }static int write_mmio (struct kvm_vcpu *vcpu,gpa_t gpa,int bytes,void *val)
@@ -5395,7 +5395,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,return X86EMUL_PROPAGATE_FAULT ; } -if (!ret && ops->read_write_emulate (vcpu, gpa, val, bytes)) +if (!ret && ops->read_write_emulate (vcpu, gpa, addr, val, bytes))return X86EMUL_CONTINUE ;/*
@@ -5556,7 +5556,7 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,return X86EMUL_CMPXCHG_FAILED ;kvm_vcpu_mark_page_dirty (vcpu, gpa >>PAGE_SHIFT ); -kvm_page_track_write (vcpu, gpa, new, bytes); +kvm_page_track_write (vcpu, gpa, addr, new, bytes);return X86EMUL_CONTINUE ;
drivers/gpu/drm/i915/gvt/kvmgt.c +1 -1
@@ -1550,7 +1550,7 @@ static int kvmgt_page_track_remove(unsigned long handle, u64 gfn)return 0 ; } -static void kvmgt_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa, +static void kvmgt_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const u8 *val,int len,struct kvm_page_track_notifier_node *node) {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 23/92 kvm: page track: add support for preread, prewrite and preexec
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
These callbacks return a boolean value. If false, the emulation should stop and the instruction should be reexecuted in guest. The preread callback can return the bytes needed by the read operation.
--- arch/x86/include/asm/kvm_page_track.h | 19 +++- arch/x86/kvm/mmu.c | 81 +++++++++++++++++ arch/x86/kvm/mmu.h | 4 + arch/x86/kvm/page_track.c | 123 ++++++++++++++++++++++++-- 4 files changed, 217 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_page_track.h +18 -1
@@ -3,7 +3,10 @@#define _ASM_X86_KVM_PAGE_TRACK_Henum kvm_page_track_mode { +KVM_PAGE_TRACK_PREREAD , +KVM_PAGE_TRACK_PREWRITE ,KVM_PAGE_TRACK_WRITE , +KVM_PAGE_TRACK_PREEXEC ,KVM_PAGE_TRACK_MAX , };
@@ -22,6 +25,13 @@ struct kvm_page_track_notifier_head {struct kvm_page_track_notifier_node {struct hlist_node node; +bool (*track_preread)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +u8 *new,int bytes, +struct kvm_page_track_notifier_node *node, +bool *data_ready); +bool (*track_prewrite)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +const u8 *new,int bytes, +struct kvm_page_track_notifier_node *node);/* * It is called when guest is writing the write-tracked page * and write emulation is finished at that time.
@@ -35,12 +45,14 @@ struct kvm_page_track_notifier_node {void (*track_write)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const u8 *new,int bytes,struct kvm_page_track_notifier_node *node); +bool (*track_preexec)(struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +struct kvm_page_track_notifier_node *node);void (*track_create_slot)(struct kvm *kvm,struct kvm_memory_slot *slot,unsigned long npages,struct kvm_page_track_notifier_node *node);/* * It is called when memory slot is being moved or removed -* users can drop write-protection for the pages in that memory slot +* users can drop active protection for the pages in that memory slot * * @kvm: the kvm where memory slot being moved or removed * @slot: the memory slot being moved or removed
@@ -73,7 +85,12 @@ kvm_page_track_register_notifier(struct kvm *kvm,void kvm_page_track_unregister_notifier (struct kvm *kvm,struct kvm_page_track_notifier_node *n); +bool kvm_page_track_preread (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +u8 *new,int bytes,bool *data_ready); +bool kvm_page_track_prewrite (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +const u8 *new,int bytes);void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,const u8 *new,int bytes); +bool kvm_page_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva);void kvm_page_track_flush_slot (struct kvm *kvm,struct kvm_memory_slot *slot);#endif
arch/x86/kvm/mmu.c +81 -0
99 lines not on this page · raw message
arch/x86/kvm/mmu.h +4 -0
@@ -210,5 +210,9 @@ void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn);void kvm_mmu_gfn_allow_lpage (struct kvm_memory_slot *slot,gfn_t gfn);bool kvm_mmu_slot_gfn_write_protect (struct kvm *kvm,struct kvm_memory_slot *slot,u64 gfn); +bool kvm_mmu_slot_gfn_read_protect (struct kvm *kvm, +struct kvm_memory_slot *slot,u64 gfn); +bool kvm_mmu_slot_gfn_exec_protect (struct kvm *kvm, +struct kvm_memory_slot *slot,u64 gfn);int kvm_arch_write_log_dirty (struct kvm_vcpu *vcpu);#endif
arch/x86/kvm/page_track.c +114 -9
166 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 24/92 kvm: x86: wire in the preread/prewrite/preexec page trackers
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
These are needed by the introspection subsystem.
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 10 +++++- arch/x86/kvm/mmu.c | 37 ++++++++++++++------- arch/x86/kvm/x86.c | 52 ++++++++++++++++++++++++------ 4 files changed, 79 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -444,6 +444,7 @@ bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);#define EMULATION_OK 0#define EMULATION_RESTART 1#define EMULATION_INTERCEPTED 2 +#define EMULATION_RETRY_INSTR 3void init_decode_cache (struct x86_emulate_ctxt *ctxt);int x86_emulate_insn (struct x86_emulate_ctxt *ctxt); int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
arch/x86/kvm/emulate.c +9 -1
@@ -5366,7 +5366,12 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) ctxt->memopp->addr.mem.ea + ctxt->_eip); done: -return (rc !=X86EMUL_CONTINUE ) ?EMULATION_FAILED :EMULATION_OK ; +if (rc ==X86EMUL_RETRY_INSTR ) +return EMULATION_RETRY_INSTR ; +else if (rc ==X86EMUL_CONTINUE ) +return EMULATION_OK ; +else +return EMULATION_FAILED ; }bool x86_page_table_writing_insn (struct x86_emulate_ctxt *ctxt)
@@ -5736,6 +5741,9 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)if (rc ==X86EMUL_INTERCEPTED )return EMULATION_INTERCEPTED ; +if (rc ==X86EMUL_RETRY_INSTR ) +return EMULATION_RETRY_INSTR ; +if (rc ==X86EMUL_CONTINUE )writeback_registers (ctxt);
arch/x86/kvm/mmu.c +26 -11
@@ -1111,9 +1111,13 @@ static void account_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) slot =__gfn_to_memslot (slots, gfn);/* the non-leaf shadow pages are keeping readonly. */ -if (sp->role.level >PT_PAGE_TABLE_LEVEL ) -return kvm_slot_page_track_add_page (kvm, slot, gfn, -KVM_PAGE_TRACK_WRITE ); +if (sp->role.level >PT_PAGE_TABLE_LEVEL ) { +kvm_slot_page_track_add_page (kvm, slot, gfn, +KVM_PAGE_TRACK_PREWRITE ); +kvm_slot_page_track_add_page (kvm, slot, gfn, +KVM_PAGE_TRACK_WRITE ); +return ; + }kvm_mmu_gfn_disallow_lpage (slot, gfn); }
@@ -1128,9 +1132,13 @@ static void unaccount_shadowed(struct kvm *kvm, struct kvm_mmu_page *sp) gfn = sp->gfn; slots =kvm_memslots_for_spte_role (kvm, sp->role); slot =__gfn_to_memslot (slots, gfn); -if (sp->role.level >PT_PAGE_TABLE_LEVEL ) -return kvm_slot_page_track_remove_page (kvm, slot, gfn, -KVM_PAGE_TRACK_WRITE ); +if (sp->role.level >PT_PAGE_TABLE_LEVEL ) { +kvm_slot_page_track_remove_page (kvm, slot, gfn, +KVM_PAGE_TRACK_PREWRITE ); +kvm_slot_page_track_remove_page (kvm, slot, gfn, +KVM_PAGE_TRACK_WRITE ); +return ; + }kvm_mmu_gfn_allow_lpage (slot, gfn); }
@@ -2884,7 +2892,8 @@ static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn, {struct kvm_mmu_page *sp; -if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE )) +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREWRITE ) || +kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE ))return true;for_each_gfn_indirect_valid_sp (vcpu->kvm, sp, gfn) {
@@ -4006,15 +4015,21 @@ static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,if (unlikely (error_code &PFERR_RSVD_MASK ))return false; -if (!(error_code &PFERR_PRESENT_MASK ) || - !(error_code &PFERR_WRITE_MASK )) +if (!(error_code &PFERR_PRESENT_MASK ))return false;/* -* guest is writing the page which is write tracked which can +* guest is reading/writing/fetching the page which is +* read/write/execute tracked which can * not be fixed by page fault handler. */ -if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE )) +if (((error_code &PFERR_USER_MASK ) + &&kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) + || ((error_code &PFERR_WRITE_MASK ) + && (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREWRITE ) + ||kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE ))) + || ((error_code &PFERR_FETCH_MASK ) + &&kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREEXEC )))return true;return false;
arch/x86/kvm/x86.c +43 -9
107 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 25/92 kvm: x86: intercept the write access on sidt and other emulated instructions
In reply to Adalbert LazărThis is needed for the introspection subsystem to track the changes to descriptor table registers.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -5185,11 +5185,14 @@ static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytesif (gpa ==UNMAPPED_GVA )return X86EMUL_PROPAGATE_FAULT ; +if (!kvm_page_track_prewrite (vcpu, gpa, addr, data, towrite)) +return X86EMUL_RETRY_INSTR ; ret =kvm_vcpu_write_guest (vcpu, gpa, data, towrite);if (ret <0 ) { r =X86EMUL_IO_NEEDED ; goto out; } +kvm_page_track_write (vcpu, gpa, addr, data, towrite); bytes -= towrite; data += towrite;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 26/92 kvm: x86: add kvm_mmu_nested_pagefault()
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed to filter #PF introspection events.
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/mmu.c | 5 +++++ arch/x86/kvm/svm.c | 7 +++++++ arch/x86/kvm/vmx/vmx.c | 9 +++++++++ 4 files changed, 25 insertions(+)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1004,6 +1004,8 @@ struct kvm_x86_ops {bool (*has_emulated_msr)(int index);void (*cpuid_update)(struct kvm_vcpu *vcpu); +bool (*nested_pagefault)(struct kvm_vcpu *vcpu); +struct kvm *(*vm_alloc)(void );void (*vm_free)(struct kvm *);int (*vm_init)(struct kvm *kvm);
@@ -1593,4 +1595,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)#define put_smstate (type, buf, offset, val) \ *(type *)((buf) + (offset) - 0x7e00) = val +bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/mmu.c +5 -0
@@ -6169,3 +6169,8 @@ void kvm_mmu_module_exit(void)unregister_shrinker (&mmu_shrinker);mmu_audit_disable (); } + +bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->nested_pagefault (vcpu); +}
arch/x86/kvm/svm.c +7 -0
@@ -7098,6 +7098,11 @@ static int nested_enable_evmcs(struct kvm_vcpu *vcpu,return -ENODEV ; } +static bool svm_nested_pagefault (struct kvm_vcpu *vcpu) +{ +return false; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -7109,6 +7114,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .nested_pagefault = svm_nested_pagefault, + .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu, .vcpu_reset = svm_vcpu_reset,
arch/x86/kvm/vmx/vmx.c +9 -0
@@ -7682,6 +7682,13 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static bool vmx_nested_pagefault (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) +return false; +return true; +} +static struct kvm_x86_ops vmx_x86_ops__ro_after_init = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -7693,6 +7700,8 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .nested_pagefault = vmx_nested_pagefault, + .vm_init = vmx_vm_init, .vm_alloc = vmx_vm_alloc, .vm_free = vmx_vm_free,
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 27/92 kvm: introspection: use page track
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
From preread, prewrite and preexec callbacks we will send the KVMI_EVENT_PF events caused by access rights enforced by the introspection tool.
--- arch/x86/include/asm/kvmi_host.h | 12 ++ arch/x86/kvm/kvmi.c | 45 +++++ include/uapi/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 293 ++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 21 +++ 5 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/kvmi_host.h
arch/x86/include/asm/kvmi_host.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_KVMI_HOST_H +#define _ASM_X86_KVMI_HOST_H + +#include <asm/kvm_host.h> +#include <asm/kvm_page_track.h> + +struct kvmi_arch_mem_access { +unsigned long active[KVM_PAGE_TRACK_MAX ][BITS_TO_LONGS (KVM_MEM_SLOTS_NUM )]; +}; + +#endif /* _ASM_X86_KVMI_HOST_H */
arch/x86/kvm/kvmi.c +45 -0
54 lines not on this page · raw message
include/uapi/linux/kvmi.h +4 -0
@@ -70,6 +70,10 @@ enum {#define KVMI_EVENT_ACTION_RETRY 1#define KVMI_EVENT_ACTION_CRASH 2 +#define KVMI_PAGE_ACCESS_R (1 << 0) +#define KVMI_PAGE_ACCESS_W (1 << 1) +#define KVMI_PAGE_ACCESS_X (1 << 2) +#define KVMI_MSG_SIZE (4096 - sizeof(struct kvmi_msg_hdr))struct kvmi_msg_hdr {
virt/kvm/kvmi.c +292 -1
355 lines not on this page · raw message
virt/kvm/kvmi_int.h +21 -0
@@ -6,6 +6,7 @@#include <linux/kvm_host.h> #include <uapi/linux/kvmi.h> +#include <asm/kvmi_host.h> #define kvmi_debug (ikvm, fmt, ...) \ kvm_debug("%pU " fmt, &ikvm->uuid, ## __VA_ARGS__)
@@ -104,6 +105,10 @@ struct kvmi_vcpu {struct kvmi {struct kvm *kvm; +struct kvm_page_track_notifier_node kptn_node; + +struct radix_tree_root access_tree; +rwlock_t access_tree_lock;struct socket *sock;struct task_struct *recv;
@@ -118,6 +123,17 @@ struct kvmi {bool cmd_reply_disabled; }; +struct kvmi_mem_access { +gfn_t gfn; +u8 access; +struct kvmi_arch_mem_access arch; +}; + +static inline bool is_event_enabled (struct kvm_vcpu *vcpu,int event) +{ +return false;/* TODO */ +} +/* kvmi_msg.c */ bool kvmi_sock_get (struct kvmi *ikvm,int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);
@@ -138,7 +154,12 @@ int kvmi_add_job(struct kvm_vcpu *vcpu, void *ctx, void (*free_fct)(void *ctx));/* arch */ +void kvmi_arch_update_page_tracking (struct kvm *kvm, +struct kvm_memory_slot *slot, +struct kvmi_mem_access *m);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev); +bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva, +u8 access);int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu,struct kvmi_get_vcpu_info_reply *rpl);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 28/92 kvm: x86: consult the page tracking from kvm_mmu_get_page() and __direct_map()
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
KVM doesn't normally need to keep track that closely to page access bits, however for the introspection subsystem this is essential.
--- arch/x86/kvm/mmu.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +19 -2
@@ -2491,6 +2491,20 @@ static void clear_sp_write_flooding_count(u64 *spte)__clear_sp_write_flooding_count (sp); } +static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn, +unsigned int acc) +{ +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) + acc &= ~ACC_USER_MASK ; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREWRITE ) || +kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_WRITE )) + acc &= ~ACC_WRITE_MASK ; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREEXEC )) + acc &= ~ACC_EXEC_MASK ; + +return acc; +} + static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, gfn_t gfn, gva_t gaddr,
@@ -2511,7 +2525,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, role.direct = direct;if (role.direct) role.cr4_pae =0 ; - role.access = access; + role.access =kvm_mmu_page_track_acc (vcpu, gfn, access);if (!vcpu->arch.mmu->direct_map && vcpu->arch.mmu->root_level <=PT32_ROOT_LEVEL ) { quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level));
@@ -3234,7 +3248,10 @@ static int __direct_map(struct kvm_vcpu *vcpu, int write, int map_writable,for_each_shadow_entry (vcpu, (u64 )gfn <<PAGE_SHIFT , iterator) {if (iterator.level == level) { - emulate =mmu_set_spte (vcpu, iterator.sptep,ACC_ALL , +unsigned int acc =kvm_mmu_page_track_acc (vcpu, gfn, +ACC_ALL ); + + emulate =mmu_set_spte (vcpu, iterator.sptep, acc, write, level, gfn, pfn, prefault, map_writable);direct_pte_prefetch (vcpu, iterator.sptep);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 29/92 kvm: introspection: add KVMI_CONTROL_EVENTS
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This command enables/disables vCPU introspection events.
--- Documentation/virtual/kvm/kvmi.rst | 53 ++++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 7 ++++ virt/kvm/kvmi.c | 13 ++++++++ virt/kvm/kvmi_int.h | 6 +++- virt/kvm/kvmi_msg.c | 24 ++++++++++++++ 5 files changed, 102 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/kvmi.rst +53 -0
@@ -456,6 +456,59 @@ Returns the TSC frequency (in HZ) for the specified vCPU if available * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +8. KVMI_CONTROL_EVENTS +---------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_control_events { + __u16 event_id; + __u8 enable; + __u8 padding1; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Enables/disables vCPU introspection events. This command can be used with +the following events:: + + KVMI_EVENT_CR + KVMI_EVENT_MSR + KVMI_EVENT_XSETBV + KVMI_EVENT_BREAKPOINT + KVMI_EVENT_HYPERCALL + KVMI_EVENT_PF + KVMI_EVENT_TRAP + KVMI_EVENT_DESCRIPTOR + KVMI_EVENT_SINGLESTEP + +When an event is enabled, the introspection tool is notified and it +must reply with: continue, retry, crash, etc. (see **Events** below). + +The *KVMI_EVENT_PAUSE_VCPU* event is always allowed, +because it is triggered by the *KVMI_PAUSE_VCPU* command. +The *KVMI_EVENT_CREATE_VCPU* and *KVMI_EVENT_UNHOOK* events are controlled +by the *KVMI_CONTROL_VM_EVENTS* command. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - the event ID is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_EPERM - the access is restricted by the host +* -KVM_EOPNOTSUPP - one the events can't be intercepted in the current setup + Events ======
include/uapi/linux/kvmi.h +7 -0
@@ -120,6 +120,13 @@ struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; }; +struct kvmi_control_events { +__u16 event_id; +__u8 enable; +__u8 padding1; +__u32 padding2; +}; +struct kvmi_control_vm_events {__u16 event_id;__u8 enable;
virt/kvm/kvmi.c +13 -0
@@ -969,6 +969,19 @@ void kvmi_handle_requests(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id, +bool enable) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (enable) +set_bit (event_id, ivcpu->ev_mask); +else +clear_bit (event_id, ivcpu->ev_mask); + +return 0 ; +} +int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable) {
virt/kvm/kvmi_int.h +5 -1
@@ -95,6 +95,8 @@ struct kvmi_vcpu {bool reply_waiting;struct kvmi_vcpu_reply reply; +DECLARE_BITMAP (ev_mask,KVMI_NUM_EVENTS ); +struct list_head job_list;spinlock_t job_lock;
@@ -131,7 +133,7 @@ struct kvmi_mem_access {static inline bool is_event_enabled (struct kvm_vcpu *vcpu,int event) { -return false;/* TODO */ +return test_bit (event,IVCPU (vcpu)->ev_mask); }/* kvmi_msg.c */
@@ -146,6 +148,8 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm);void *kvmi_msg_alloc (void );void *kvmi_msg_alloc_check (size_t size);void kvmi_msg_free (void *addr); +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id, +bool enable);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu);
virt/kvm/kvmi_msg.c +24 -0
@@ -24,6 +24,7 @@ static const char *const msg_IDs[] = { [KVMI_CHECK_COMMAND] = "KVMI_CHECK_COMMAND", [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", + [KVMI_CONTROL_EVENTS] = "KVMI_CONTROL_EVENTS", [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS", [KVMI_EVENT] = "KVMI_EVENT", [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY",
@@ -403,6 +404,28 @@ static int handle_get_vcpu_info(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg,0 , &rpl,sizeof (rpl)); } +static int handle_control_events (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *_req, +vcpu_reply_fct reply_cb) +{ +unsigned long known_events =KVMI_KNOWN_VCPU_EVENTS ; +const struct kvmi_control_events *req = _req; +struct kvmi *ikvm =IKVM (vcpu->kvm); +int ec; + +if (req->padding1 || req->padding2) + ec = -KVM_EINVAL ; +else if (!test_bit (req->event_id, &known_events)) + ec = -KVM_EINVAL ; +else if (!is_event_allowed (ikvm, req->event_id)) + ec = -KVM_EPERM ; +else + ec =kvmi_cmd_control_events (vcpu, req->event_id, req->enable); + +return reply_cb (vcpu, msg, ec,NULL ,0 ); +} +/* * These commands are executed on the vCPU thread. The receiving thread * passes the messages using a newly allocated 'struct kvmi_vcpu_cmd'
@@ -412,6 +435,7 @@ static int handle_get_vcpu_info(struct kvm_vcpu *vcpu,static int (*const msg_vcpu[])(struct kvm_vcpu *,const struct kvmi_msg_hdr *,const void *,vcpu_reply_fct ) = { + [KVMI_CONTROL_EVENTS ] = handle_control_events, [KVMI_EVENT_REPLY ] = handle_event_reply, [KVMI_GET_VCPU_INFO ] = handle_get_vcpu_info, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 30/92 kvm: x86: add kvm_spt_fault()
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This is needed to filter #PF introspection events, when not caused by EPT/NPT fault. One such case is when handle_ud() calls the emulator which failes to fetch the opcodes from stack (which is hooked rw-) which leads to a page fault event.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm.c | 8 ++++++++ arch/x86/kvm/vmx/vmx.c | 8 ++++++++ arch/x86/kvm/x86.c | 6 ++++++ 4 files changed, 24 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1005,6 +1005,7 @@ struct kvm_x86_ops {void (*cpuid_update)(struct kvm_vcpu *vcpu);bool (*nested_pagefault)(struct kvm_vcpu *vcpu); +bool (*spt_fault)(struct kvm_vcpu *vcpu);struct kvm *(*vm_alloc)(void );void (*vm_free)(struct kvm *);
@@ -1596,5 +1597,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu) *(type *)((buf) + (offset) - 0x7e00) = valbool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu); +bool kvm_spt_fault (struct kvm_vcpu *vcpu);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/svm.c +8 -0
@@ -7103,6 +7103,13 @@ static bool svm_nested_pagefault(struct kvm_vcpu *vcpu)return false; } +static bool svm_spt_fault (struct kvm_vcpu *vcpu) +{ +const struct vcpu_svm *svm =to_svm (vcpu); + +return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -7115,6 +7122,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .has_emulated_msr = svm_has_emulated_msr, .nested_pagefault = svm_nested_pagefault, + .spt_fault = svm_spt_fault, .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu,
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -7689,6 +7689,13 @@ static bool vmx_nested_pagefault(struct kvm_vcpu *vcpu)return true; } +static bool vmx_spt_fault (struct kvm_vcpu *vcpu) +{ +const struct vcpu_vmx *vmx =to_vmx (vcpu); + +return (vmx->exit_reason ==EXIT_REASON_EPT_VIOLATION ); +} +static struct kvm_x86_ops vmx_x86_ops__ro_after_init = { .cpu_has_kvm_support = cpu_has_kvm_support, .disabled_by_bios = vmx_disabled_by_bios,
@@ -7701,6 +7708,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .has_emulated_msr = vmx_has_emulated_msr, .nested_pagefault = vmx_nested_pagefault, + .spt_fault = vmx_spt_fault, .vm_init = vmx_vm_init, .vm_alloc = vmx_vm_alloc,
arch/x86/kvm/x86.c +6 -0
@@ -9884,6 +9884,12 @@ bool kvm_vector_hashing_enabled(void) }EXPORT_SYMBOL_GPL (kvm_vector_hashing_enabled); +bool kvm_spt_fault (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->spt_fault (vcpu); +} +EXPORT_SYMBOL (kvm_spt_fault); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 31/92 kvm: introspection: add KVMI_EVENT_PF
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This event is sent when a #PF occurs due to a failed permission check in the shadow page tables, for a page in which the introspection tool has shown interest.
The introspection tool can respond to a KVMI_EVENT_PF event with custom input for the current instruction. This input is used to trick the guest software into believing it has read certain data, in order to hide the content of certain memory areas (eg. hide injected code from integrity checkers).
--- Documentation/virtual/kvm/kvmi.rst | 63 ++++++++++++++++++ arch/x86/kvm/kvmi.c | 38 ++++++++++- arch/x86/kvm/x86.c | 7 +- include/linux/kvmi.h | 4 ++ include/uapi/linux/kvmi.h | 18 +++++ virt/kvm/kvmi.c | 103 +++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 13 ++++ virt/kvm/kvmi_msg.c | 55 +++++++++++++++ 8 files changed, 298 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/kvmi.rst +63 -0
@@ -618,3 +618,66 @@ The introspection tool has a chance to unhook and close the KVMI channel This event is sent when a new vCPU is created and the introspection has been enabled for this event (see *KVMI_CONTROL_VM_EVENTS*). +3. KVMI_EVENT_PF +---------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH, RETRY +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_pf { + __u64 gva; + __u64 gpa; + __u8 access; + __u8 padding1; + __u16 view; + __u32 padding2; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + struct kvmi_event_pf_reply { + __u64 ctx_addr; + __u32 ctx_size; + __u8 singlestep; + __u8 rep_complete; + __u16 padding; + __u8 ctx_data[256]; + }; + +This event is sent when a hypervisor page fault occurs due to a failed +permission check in the shadow page tables, the introspection has +been enabled for this event (see *KVMI_CONTROL_EVENTS*) and the event was +generated for a page in which the introspector has shown interest +(ie. has previously touched it by adjusting the spte permissions). + +The shadow page tables can be used by the introspection tool to guarantee +the purpose of code areas inside the guest (code, rodata, stack, heap +etc.) Each attempt at an operation unfitting for a certain memory +range (eg. execute code in heap) triggers a page fault and gives the +introspection tool the chance to audit the code attempting the operation. + +``kvmi_event``, guest virtual address (or 0xffffffff/UNMAPPED_GVA), +guest physical address, access flags (eg. KVMI_PAGE_ACCESS_R) and the +EPT view are sent to the introspector. + +The *CONTINUE* action will continue the page fault handling via emulation +(with custom input if ``ctx_size`` > 0). The use of custom input is +to trick the guest software into believing it has read certain data, +in order to hide the content of certain memory areas (eg. hide injected +code from integrity checkers). If ``rep_complete`` is not zero, the REP +prefixed instruction should be emulated just once (or at least no other +*KVMI_EVENT_PF* event should be sent for the current instruction). + +The *RETRY* action is used by the introspector to retry the execution of +the current instruction. Either using single-step (if ``singlestep`` is +not zero) or return to guest (if the introspector changed the instruction +pointer or the page restrictions).
arch/x86/kvm/kvmi.c +37 -1
@@ -94,7 +94,43 @@ void kvmi_arch_setup_event(struct kvm_vcpu *vcpu, struct kvmi_event *ev)bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access) { -return KVMI_EVENT_ACTION_CONTINUE ;/* TODO */ +struct kvmi_vcpu *ivcpu; +u32 ctx_size; +u64 ctx_addr; +u32 action; +bool singlestep_ignored; +bool ret = false; + +if (!kvm_spt_fault (vcpu)) +/* We are only interested in EPT/NPT violations */ +return true; + + ivcpu =IVCPU (vcpu); + ctx_size =sizeof (ivcpu->ctx_data); + +if (ivcpu->effective_rep_complete) +return true; + + action =kvmi_msg_send_pf (vcpu, gpa, gva, access, &singlestep_ignored, + &ivcpu->rep_complete, &ctx_addr, + ivcpu->ctx_data, &ctx_size); + + ivcpu->ctx_size =0 ; + ivcpu->ctx_addr =0 ; + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : + ivcpu->ctx_size = ctx_size; + ivcpu->ctx_addr = ctx_addr; + ret = true; +break ; +case KVMI_EVENT_ACTION_RETRY : +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"PF" ); + } + +return ret; } int kvmi_arch_cmd_get_vcpu_info(struct kvm_vcpu *vcpu,
arch/x86/kvm/x86.c +5 -2
@@ -6418,6 +6418,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, vcpu->arch.l1tf_flush_l1d = true; +kvmi_init_emulate (vcpu); +/* * Clear write_fault_to_shadow_pgtable here to ensure it is * never reused.
@@ -6523,9 +6525,10 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, writeback = false; r =EMULATE_USER_EXIT ; vcpu->arch.complete_userspace_io = complete_emulated_mmio; - }else if (r ==EMULATION_RESTART ) + }else if (r ==EMULATION_RESTART ) { +kvmi_activate_rep_complete (vcpu); goto restart; -else + }else r =EMULATE_DONE ;if (writeback) {
include/linux/kvmi.h +4 -0
@@ -17,6 +17,8 @@ int kvmi_ioctl_unhook(struct kvm *kvm, bool force_reset);int kvmi_vcpu_init (struct kvm_vcpu *vcpu);void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +void kvmi_init_emulate (struct kvm_vcpu *vcpu); +void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);#else
@@ -27,6 +29,8 @@ static inline void kvmi_destroy_vm(struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { } +static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { } +static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { }#endif /* CONFIG_KVM_INTROSPECTION */
include/uapi/linux/kvmi.h +18 -0
@@ -155,4 +155,22 @@ struct kvmi_event_reply { __u32 padding2; }; +struct kvmi_event_pf { +__u64 gva; +__u64 gpa; +__u8 access; +__u8 padding1; +__u16 view; +__u32 padding2; +}; + +struct kvmi_event_pf_reply { +__u64 ctx_addr; +__u32 ctx_size; +__u8 singlestep; +__u8 rep_complete; +__u16 padding; +__u8 ctx_data[256 ]; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +103 -0
124 lines not on this page · raw message
virt/kvm/kvmi_int.h +13 -0
@@ -26,6 +26,8 @@#define IVCPU (vcpu) ((struct kvmi_vcpu *)((vcpu)->kvmi)) +#define KVMI_CTX_DATA_SIZE FIELD_SIZEOF(struct kvmi_event_pf_reply, ctx_data) +#define KVMI_MSG_SIZE_ALLOC (sizeof(struct kvmi_msg_hdr) + KVMI_MSG_SIZE)#define KVMI_KNOWN_VCPU_EVENTS ( \
@@ -92,6 +94,12 @@ struct kvmi_vcpu_reply { };struct kvmi_vcpu { +u8 ctx_data[KVMI_CTX_DATA_SIZE ]; +u32 ctx_size; +u64 ctx_addr; +bool rep_complete; +bool effective_rep_complete; +bool reply_waiting;struct kvmi_vcpu_reply reply;
@@ -141,6 +149,9 @@ bool kvmi_sock_get(struct kvmi *ikvm, int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);void kvmi_sock_put (struct kvmi *ikvm);bool kvmi_msg_process (struct kvmi *ikvm); +u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access, +bool *singlestep,bool *rep_complete, +u64 *ctx_addr,u8 *ctx,u32 *ctx_size);u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu);int kvmi_msg_send_unhook (struct kvmi *ikvm);
@@ -156,6 +167,8 @@ int kvmi_run_jobs_and_wait(struct kvm_vcpu *vcpu);int kvmi_add_job (struct kvm_vcpu *vcpu,void (*fct)(struct kvm_vcpu *vcpu,void *ctx),void *ctx,void (*free_fct)(void *ctx)); +void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action, +const char *str);/* arch */ void kvmi_arch_update_page_tracking(struct kvm *kvm,
virt/kvm/kvmi_msg.c +55 -0
61 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 32/92 kvm: introspection: add KVMI_GET_PAGE_ACCESS
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
Returns the spte access bits (rwx) for an array of guest physical addresses.
It does this by checking the radix tree in which only the spte bits "enforced" by the introspection tool are saved. This information should already be known by the tool. Not to mention that the KVMI_EVENT_PF events are sent only for EPT violation caused by these restrictions. So, we might drop this command.
--- Documentation/virtual/kvm/kvmi.rst | 54 ++++++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 41 +++++++++++++++++++++++ include/uapi/linux/kvmi.h | 11 ++++++ virt/kvm/kvmi.c | 9 +++++ virt/kvm/kvmi_int.h | 6 ++++ virt/kvm/kvmi_msg.c | 17 ++++++++++ 6 files changed, 138 insertions(+)
Documentation/virtual/kvm/kvmi.rst +54 -0
@@ -509,6 +509,60 @@ by the *KVMI_CONTROL_VM_EVENTS* command. * -KVM_EPERM - the access is restricted by the host * -KVM_EOPNOTSUPP - one the events can't be intercepted in the current setup +9. KVMI_GET_PAGE_ACCESS +----------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_get_page_access { + __u16 view; + __u16 count; + __u32 padding; + __u64 gpa[0]; + }; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_page_access_reply { + __u8 access[0]; + }; + +Returns the spte access bits (rwx) for an array of ``count`` guest +physical addresses. + +The valid access bits for *KVMI_GET_PAGE_ACCESS* and *KVMI_SET_PAGE_ACCESS* +are:: + + KVMI_PAGE_ACCESS_R + KVMI_PAGE_ACCESS_W + KVMI_PAGE_ACCESS_X + +By default, for any guest physical address, the returned access mode will +be 'rwx' (all the above bits). If the introspection tool must prevent +the code execution from a guest page, for example, it should use the +KVMI_SET_PAGE_ACCESS command to set the 'rw' bits for any guest physical +addresses contained in that page. Of course, in order to receive +page fault events when these violations take place, the KVMI_CONTROL_EVENTS +command must be used to enable this type of event (KVMI_EVENT_PF). + +On Intel hardware with multiple EPT views, the ``view`` argument selects the +EPT view (0 is primary). On all other hardware it must be zero. + +:Errors: + +* -KVM_EINVAL - the selected SPT view is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EOPNOTSUPP - a SPT view was selected but the hardware doesn't support it +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to allocate the reply + Events ======
arch/x86/kvm/kvmi.c +41 -0
44 lines not on this page · raw message
include/uapi/linux/kvmi.h +11 -0
@@ -116,6 +116,17 @@ struct kvmi_get_guest_info_reply {__u32 padding[3 ]; }; +struct kvmi_get_page_access { +__u16 view; +__u16 count; +__u32 padding; +__u64 gpa[0 ]; +}; + +struct kvmi_get_page_access_reply { +__u8 access[0 ]; +}; +struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; };
virt/kvm/kvmi.c +9 -0
@@ -1072,6 +1072,15 @@ void kvmi_handle_requests(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access) +{ +gfn_t gfn =gpa_to_gfn (gpa); + +kvmi_get_gfn_access (ikvm, gfn, access); + +return 0 ; +} +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable) {
virt/kvm/kvmi_int.h +6 -0
@@ -159,6 +159,7 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm);void *kvmi_msg_alloc (void );void *kvmi_msg_alloc_check (size_t size);void kvmi_msg_free (void *addr); +int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access);int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable); int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,
@@ -174,6 +175,11 @@ void kvmi_handle_common_event_actions(struct kvm_vcpu *vcpu, u32 action,void kvmi_arch_update_page_tracking (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvmi_mem_access *m); +int kvmi_arch_cmd_get_page_access (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_get_page_access *req, +struct kvmi_get_page_access_reply **dest, +size_t *dest_size);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access);
virt/kvm/kvmi_msg.c +17 -0
@@ -29,6 +29,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT] = "KVMI_EVENT", [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", + [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", };
@@ -323,6 +324,21 @@ static int handle_control_cmd_response(struct kvmi *ikvm,return err; } +static int handle_get_page_access (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *req) +{ +struct kvmi_get_page_access_reply *rpl =NULL ; +size_t rpl_size =0 ; +int err, ec; + + ec =kvmi_arch_cmd_get_page_access (ikvm, msg, req, &rpl, &rpl_size); + + err =kvmi_msg_vm_maybe_reply (ikvm, msg, ec, rpl, rpl_size); +kvmi_msg_free (rpl); +return err; +} +static bool invalid_vcpu_hdr (const struct kvmi_vcpu_hdr *hdr) {return hdr->padding1 || hdr->padding2;
@@ -338,6 +354,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CONTROL_CMD_RESPONSE] = handle_control_cmd_response, [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, + [KVMI_GET_PAGE_ACCESS] = handle_get_page_access, [KVMI_GET_VERSION] = handle_get_version, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 33/92 kvm: introspection: add KVMI_SET_PAGE_ACCESS
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This command sets the spte access bits (rwx) for an array of guest physical addresses (through the page track subsystem).
These pages, with the requested access bits, are also kept in a radix tree in order to filter out the #PF events which are of no interest to the introspection tool.
--- Documentation/virtual/kvm/kvmi.rst | 54 ++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 36 ++++++++++++++++++ include/uapi/linux/kvmi.h | 15 ++++++++ virt/kvm/kvmi.c | 61 ++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 4 ++ virt/kvm/kvmi_msg.c | 13 +++++++ 6 files changed, 183 insertions(+)
Documentation/virtual/kvm/kvmi.rst +54 -0
@@ -563,6 +563,60 @@ EPT view (0 is primary). On all other hardware it must be zero. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to allocate the reply +10. KVMI_SET_PAGE_ACCESS +------------------------ + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_set_page_access { + __u16 view; + __u16 count; + __u32 padding; + struct kvmi_page_access_entry entries[0]; + }; + +where:: + + struct kvmi_page_access_entry { + __u64 gpa; + __u8 access; + __u8 padding1; + __u16 padding2; + __u32 padding3; + }; + + +:Returns: + +:: + + struct kvmi_error_code + +Sets the spte access bits (rwx) for an array of ``count`` guest physical +addresses. + +The command will fail with -KVM_EINVAL if any of the specified combination +of access bits is not supported. + +The command will make the changes in order and it will stop on the first +error. The introspection tool should handle the rollback. + +In order to 'forget' an address, all the access bits ('rwx') must be set. + +:Errors: + +* -KVM_EINVAL - the specified access bits combination is invalid +* -KVM_EINVAL - the selected SPT view is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EINVAL - the message size is invalid +* -KVM_EOPNOTSUPP - a SPT view was selected but the hardware doesn't support it +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to add the page tracking structures + Events ======
arch/x86/kvm/kvmi.c +36 -0
@@ -224,3 +224,39 @@ int kvmi_arch_cmd_get_page_access(struct kvmi *ikvm,return 0 ; } +int kvmi_arch_cmd_set_page_access (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_set_page_access *req) +{ +const struct kvmi_page_access_entry *entry = req->entries; +const struct kvmi_page_access_entry *end = req->entries + req->count; +u8 unknown_bits = ~(KVMI_PAGE_ACCESS_R |KVMI_PAGE_ACCESS_W + |KVMI_PAGE_ACCESS_X ); +int ec =0 ; + +if (req->padding) +return -KVM_EINVAL ; + +if (msg->size <sizeof (*req) + (end - entry) *sizeof (*entry)) +return -KVM_EINVAL ; + +if (req->view !=0 )/* TODO */ +return -KVM_EOPNOTSUPP ; + +for (; entry < end; entry++) { +if ((entry->access & unknown_bits) || entry->padding1 + || entry->padding2 || entry->padding3) + ec = -KVM_EINVAL ; +else + ec =kvmi_cmd_set_page_access (ikvm, entry->gpa, + entry->access); +if (ec) +kvmi_warn (ikvm,"%s: %llx %x padding %x,%x,%x" , + __func__, entry->gpa, entry->access, + entry->padding1, entry->padding2, + entry->padding3); + } + +return ec; +} +
include/uapi/linux/kvmi.h +15 -0
@@ -127,6 +127,21 @@ struct kvmi_get_page_access_reply {__u8 access[0 ]; }; +struct kvmi_page_access_entry { +__u64 gpa; +__u8 access; +__u8 padding1; +__u16 padding2; +__u32 padding3; +}; + +struct kvmi_set_page_access { +__u16 view; +__u16 count; +__u32 padding; +struct kvmi_page_access_entry entries[0 ]; +}; +struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; };
virt/kvm/kvmi.c +61 -0
73 lines not on this page · raw message
virt/kvm/kvmi_int.h +4 -0
@@ -160,6 +160,7 @@ void *kvmi_msg_alloc(void);void *kvmi_msg_alloc_check (size_t size);void kvmi_msg_free (void *addr);int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access); +int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access);int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable); int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,
@@ -180,6 +181,9 @@ int kvmi_arch_cmd_get_page_access(struct kvmi *ikvm,const struct kvmi_get_page_access *req, struct kvmi_get_page_access_reply **dest, size_t *dest_size); +int kvmi_arch_cmd_set_page_access (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_set_page_access *req);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access);
virt/kvm/kvmi_msg.c +13 -0
@@ -32,6 +32,7 @@ static const char *const msg_IDs[] = { [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", + [KVMI_SET_PAGE_ACCESS] = "KVMI_SET_PAGE_ACCESS", };static bool is_known_message (u16 id)
@@ -339,6 +340,17 @@ static int handle_get_page_access(struct kvmi *ikvm,return err; } +static int handle_set_page_access (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *req) +{ +int ec; + + ec =kvmi_arch_cmd_set_page_access (ikvm, msg, req); + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} +static bool invalid_vcpu_hdr (const struct kvmi_vcpu_hdr *hdr) {return hdr->padding1 || hdr->padding2;
@@ -356,6 +368,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, [KVMI_GET_PAGE_ACCESS] = handle_get_page_access, [KVMI_GET_VERSION] = handle_get_version, + [KVMI_SET_PAGE_ACCESS] = handle_set_page_access, }; static int handle_event_reply(struct kvm_vcpu *vcpu,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 34/92 Documentation: Introduce EPT based Subpage Protection
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
Message-Id: <[email protected]>
--- Documentation/virtual/kvm/spp_kvm.txt | 173 ++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 Documentation/virtual/kvm/spp_kvm.txt
Documentation/virtual/kvm/spp_kvm.txt new file +173 -0
@@ -0,0 +1,173 @@ +EPT-Based Sub-Page Protection (SPP) for KVM +==================================================== + +1.Overview + EPT-based Sub-Page Protection(SPP) allows VMM to specify + fine-grained(128byte per sub-page) write-protection for guest physical + memory. When it's enabled, the CPU enforces write-access permission + for the sub-pages within a 4KB page, if corresponding bit is set in + permission vector, write to sub-page region is allowed, otherwise, + it's prevented with a EPT violation. + +2.SPP Operation + Sub-Page Protection Table (SPPT) is introduced to manage sub-page + write-access permission. + + It is active when: + a) large paging is disabled on host side. + b) "sub-page write protection" VM-execution control is 1. + c) SPP is initialized with KVM_INIT_SPP ioctl successfully. + d) Sub-page permissions are set with KVM_SUBPAGES_SET_ACCESS ioctl + successfully. see below sections for details. + + __________________________________________________________________________ + + How SPP hardware works: + __________________________________________________________________________ + + Guest write access --> GPA --> Walk EPT --> EPT leaf entry -----| + |---------------------------------------------------------------| + |-> if VMexec_control.spp && ept_leaf_entry.spp_bit (bit 61) + | + |-> <false> --> EPT legacy behavior + | + | + |-> <true> --> if ept_leaf_entry.writable + | + |-> <true> --> Ignore SPP + | + |-> <false> --> GPA --> Walk SPP 4-level table--| + | + |------------<----------get-the-SPPT-point-from-VMCS-filed-----<------| + | + Walk SPP L4E table + | + |---> if-entry-misconfiguration ------------>-------|-------<---------| + | | | + else | | + | | | + | |------------------SPP VMexit<-----------------| | + | | | + | |-> exit_qualification & sppt_misconfig --> sppt misconfig | + | | | + | |-> exit_qualification & sppt_miss --> sppt miss | + |---| | + | | + walk SPPT L3E--|--> if-entry-misconfiguration------------>------------| + | | + else | + | | + | | + walk SPPT L2E --|--> if-entry-misconfiguration-------->-------| + | | + else | + | | + | | + walk SPPT L1E --|-> if-entry-misconfiguration--->----| + | + else + | + |-> if sub-page writable + |-> <true> allow, write access + |-> <false> disallow, EPT violation + ______________________________________________________________________________ + +3.IOCTL Interfaces + + KVM_INIT_SPP: + Allocate storage for sub-page permission vectors and SPPT root page. + + KVM_SUBPAGES_GET_ACCESS: + Get sub-page write permission vectors for given continuous guest pages. + + KVM_SUBPAGES_SET_ACCESS + Set sub-pages write permission vectors for given continuous guest pages. + + /* for KVM_SUBPAGES_GET_ACCESS and KVM_SUBPAGES_SET_ACCESS */ + struct kvm_subpage_info { + __u64 gfn; /* the first page gfn of the continuous pages */ + __u64 npages; /* number of 4K pages */ + __u64 *access_map; /* sub-page write-access bitmap array */ + }; + + #define KVM_SUBPAGES_GET_ACCESS _IOR(KVMIO, 0x49, __u64) + #define KVM_SUBPAGES_SET_ACCESS _IOW(KVMIO, 0x4a, __u64) + #define KVM_INIT_SPP _IOW(KVMIO, 0x4b, __u64) + +4.Set Sub-Page Permission + + * To enable SPP protection, system admin sets sub-page permission via + KVM_SUBPAGES_SET_ACCESS ioctl: + + (1) If the target 4KB pages are there, it locates EPT leaf entries + via the guest physical addresses, sets the bit 61 of the corresponding + entries to enable sub-page protection, then set up SPPT paging structure. + (2) otherwise, stores the [gfn,permission] mappings in KVM data structure. When + EPT page-fault is generated due to access to target page, it settles + EPT entry configuration together with SPPT setup, this is called lazy mode + setup. + + The SPPT paging structure format is as below: + + Format of the SPPT L4E, L3E, L2E: + | Bit | Contents | + | :----- | :------------------------------------------------------------------------| + | 0 | Valid entry when set; indicates whether the entry is present | + | 11:1 | Reserved (0) | + | N-1:12 | Physical address of 4KB aligned SPPT LX-1 Table referenced by this entry | + | 51:N | Reserved (0) | + | 63:52 | Reserved (0) | + Note: N is the physical address width supported by the processor. X is the page level + + Format of the SPPT L1E: + | Bit | Contents | + | :---- | :---------------------------------------------------------------- | + | 0+2i | Write permission for i-th 128 byte sub-page region. | + | 1+2i | Reserved (0). | + Note: 0<=i<=31 + +5.SPPT-induced VM exit + + * SPPT miss and misconfiguration induced VM exit + + A SPPT missing VM exit occurs when walk the SPPT, there is no SPPT + misconfiguration but a paging-structure entry is not + present in any of L4E/L3E/L2E entries. + + A SPPT misconfiguration VM exit occurs when reserved bits or unsupported values + are set in SPPT entry. + + *NOTE* SPPT miss and SPPT misconfigurations can occur only due to an + attempt to write memory with a guest physical address. + + * SPP permission induced VM exit + SPP sub-page permission induced violation is reported as EPT violation + thesefore causes VM exit. + +6.SPPT-induced VM exit handling + + #define EXIT_REASON_SPP 66 + + static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { + ... + [EXIT_REASON_SPP] = handle_spp, + ... + }; + + New exit qualification for SPPT-induced vmexits. + + | Bit | Contents | + | :---- | :---------------------------------------------------------------- | + | 10:0 | Reserved (0). | + | 11 | SPPT VM exit type. Set for SPPT Miss, cleared for SPPT Misconfig. | + | 12 | NMI unblocking due to IRET | + | 63:13 | Reserved (0) | + + In addition to the exit qualification, guest linear address and guest + physical address fields will be reported. + + * SPPT miss and misconfiguration induced VM exit + Allocate a physical page for the SPPT and set the entry correctly. + + * SPP permission induced VM exit + This kind of VM exit is left to VMI tool to handle.
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 35/92 KVM: VMX: Add control flags for SPP enabling
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
Check SPP capability in MSR_IA32_VMX_PROCBASED_CTLS2, its 23-bit indicates SPP support. Mark SPP bit in CPU capabilities bitmap if it's supported.
Message-Id: <[email protected]>
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/vmx.h | 1 + arch/x86/kernel/cpu/intel.c | 4 ++++ arch/x86/kvm/vmx/capabilities.h | 5 +++++ arch/x86/kvm/vmx/vmx.c | 10 ++++++++++ 5 files changed, 21 insertions(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -228,6 +228,7 @@#define X86_FEATURE_FLEXPRIORITY ( 8*32+ 2)/* Intel FlexPriority */ #define X86_FEATURE_EPT ( 8*32+ 3)/* Intel Extended Page Table */ #define X86_FEATURE_VPID ( 8*32+ 4)/* Intel Virtual Processor ID */ +#define X86_FEATURE_SPP ( 8*32+ 5)/* Intel EPT-based Sub-Page Write Protection */ #define X86_FEATURE_VMMCALL ( 8*32+15)/* Prefer VMMCALL to VMCALL */ #define X86_FEATURE_XENPV ( 8*32+16)/* "" Xen paravirtual guest */
arch/x86/include/asm/vmx.h +1 -0
@@ -81,6 +81,7 @@#define SECONDARY_EXEC_XSAVES 0x00100000#define SECONDARY_EXEC_PT_USE_GPA 0x01000000#define SECONDARY_EXEC_MODE_BASED_EPT_EXEC 0x00400000 +#define SECONDARY_EXEC_ENABLE_SPP 0x00800000#define SECONDARY_EXEC_TSC_SCALING 0x02000000#define PIN_BASED_EXT_INTR_MASK 0x00000001
arch/x86/kernel/cpu/intel.c +4 -0
@@ -476,6 +476,7 @@ static void detect_vmx_virtcap(struct cpuinfo_x86 *c)#define X86_VMX_FEATURE_PROC_CTLS2_EPT 0x00000002#define X86_VMX_FEATURE_PROC_CTLS2_VPID 0x00000020#define x86_VMX_FEATURE_EPT_CAP_AD 0x00200000 +#define X86_VMX_FEATURE_PROC_CTLS2_SPP 0x00800000u32 vmx_msr_low, vmx_msr_high, msr_ctl, msr_ctl2;u32 msr_vpid_cap, msr_ept_cap;
@@ -486,6 +487,7 @@ static void detect_vmx_virtcap(struct cpuinfo_x86 *c)clear_cpu_cap (c,X86_FEATURE_EPT );clear_cpu_cap (c,X86_FEATURE_VPID );clear_cpu_cap (c,X86_FEATURE_EPT_AD ); +clear_cpu_cap (c,X86_FEATURE_SPP );rdmsr (MSR_IA32_VMX_PROCBASED_CTLS , vmx_msr_low, vmx_msr_high); msr_ctl = vmx_msr_high | vmx_msr_low;
@@ -509,6 +511,8 @@ static void detect_vmx_virtcap(struct cpuinfo_x86 *c) }if (msr_ctl2 &X86_VMX_FEATURE_PROC_CTLS2_VPID )set_cpu_cap (c,X86_FEATURE_VPID ); +if (msr_ctl2 &X86_VMX_FEATURE_PROC_CTLS2_SPP ) +set_cpu_cap (c,X86_FEATURE_SPP ); } }
arch/x86/kvm/vmx/capabilities.h +5 -0
@@ -239,6 +239,11 @@ static inline bool cpu_has_vmx_pml(void)return vmcs_config.cpu_based_2nd_exec_ctrl &SECONDARY_EXEC_ENABLE_PML ; } +static inline bool cpu_has_vmx_ept_spp (void ) +{ +return vmcs_config.cpu_based_2nd_exec_ctrl &SECONDARY_EXEC_ENABLE_SPP ; +} +static inline bool vmx_xsaves_supported (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/vmx.c +10 -0
@@ -114,6 +114,8 @@ static u64 __read_mostly host_xss;bool __read_mostly enable_pml =1 ;module_param_named (pml, enable_pml, bool,S_IRUGO ); +static bool __read_mostly spp_supported =0 ; +#define MSR_BITMAP_MODE_X2APIC 1#define MSR_BITMAP_MODE_X2APIC_APICV 2
@@ -2247,6 +2249,7 @@ static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,SECONDARY_EXEC_RDSEED_EXITING |SECONDARY_EXEC_RDRAND_EXITING |SECONDARY_EXEC_ENABLE_PML | +SECONDARY_EXEC_ENABLE_SPP |SECONDARY_EXEC_TSC_SCALING |SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX |
@@ -3901,6 +3904,9 @@ static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)if (!enable_pml) exec_control &= ~SECONDARY_EXEC_ENABLE_PML ; +if (!spp_supported) + exec_control &= ~SECONDARY_EXEC_ENABLE_SPP ; +if (vmx_xsaves_supported ()) {/* Exposing XSAVES only when XSAVE is exposed */ bool xsaves_enabled =
@@ -7570,6 +7576,10 @@ static __init int hardware_setup(void)if (!cpu_has_vmx_flexpriority ()) flexpriority_enabled =0 ; +if (cpu_has_vmx_ept_spp () && enable_ept && +boot_cpu_has (X86_FEATURE_SPP )) + spp_supported =1 ; +if (!cpu_has_virtual_nmis ()) enable_vnmi =0 ;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 36/92 KVM: VMX: Implement functions for SPPT paging setup
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
SPPT is a 4-level paging structure similar to EPT, when SPP is kicked for target physical page, bit 61 of the corresponding EPT enty will be flaged, then SPPT is traversed with the gfn to build up entries, the leaf entry of SPPT contains the access bitmap for subpages inside the target 4KB physical page, one bit per 128-byte subpage.
SPPT entries are set up in below cases:
1. the EPT faulted page is SPP protected.
2. SPP mis-config induced vmexit is handled.
3. User configures SPP protected pages via SPP IOCTLs.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_host.h | 7 +- arch/x86/kvm/mmu.c | 207 ++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu.h | 1 + include/linux/kvm_host.h | 3 + 4 files changed, 217 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +6 -1
@@ -271,7 +271,8 @@ union kvm_mmu_page_role { unsigned smap_andnot_wp:1; unsigned ad_disabled:1; unsigned guest_mode:1; - unsigned :6; + unsigned spp:1; + unsigned reserved:5;/* * This is left at the top of the word so that
@@ -1410,6 +1411,10 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);int kvm_mmu_page_fault (struct kvm_vcpu *vcpu,gva_t gva,u64 error_code,void *insn,int insn_len); + +int kvm_mmu_setup_spp_structure (struct kvm_vcpu *vcpu, +u32 access_map,gfn_t gfn); +void kvm_mmu_invlpg (struct kvm_vcpu *vcpu,gva_t gva);void kvm_mmu_invpcid_gva (struct kvm_vcpu *vcpu,gva_t gva,unsigned long pcid);void kvm_mmu_new_cr3 (struct kvm_vcpu *vcpu,gpa_t new_cr3,bool skip_tlb_flush);
arch/x86/kvm/mmu.c +207 -0
255 lines not on this page · raw message
arch/x86/kvm/mmu.h +1 -0
@@ -26,6 +26,7 @@#define PT_PAGE_SIZE_MASK (1ULL << PT_PAGE_SIZE_SHIFT)#define PT_PAT_MASK (1ULL << 7)#define PT_GLOBAL_MASK (1ULL << 8) +#define PT_SPP_MASK (1ULL << 61)#define PT64_NX_SHIFT 63#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
include/linux/kvm_host.h +3 -0
@@ -832,6 +832,9 @@ int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu);bool kvm_arch_vcpu_in_kernel (struct kvm_vcpu *vcpu);int kvm_arch_vcpu_should_kick (struct kvm_vcpu *vcpu); +struct kvm_mmu_page *kvm_mmu_get_spp_page (struct kvm_vcpu *vcpu, +gfn_t gfn,unsigned int level); +#ifndef __KVM_HAVE_ARCH_VM_ALLOC/* * All architectures that want to use vzalloc currently also
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 37/92 KVM: VMX: Introduce SPP access bitmap and operation functions
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
Create access bitmap for SPP subpages, 4KB/128B = 32bits, for each 4KB physical page, 32bits are required. The bitmap can be easily accessed with a gfn. The initial access bitmap for each physical page is 0xFFFFFFFF, meaning SPP is not enabled for the subpages.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu.c | 50 +++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 11 ++++++++ 3 files changed, 62 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -790,6 +790,7 @@ struct kvm_lpage_info {struct kvm_arch_memory_slot {struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES ]; +u32 *subpage_wp_info;struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES -1 ];unsigned short *gfn_track[KVM_PAGE_TRACK_MAX ]; };
arch/x86/kvm/mmu.c +50 -0
56 lines not on this page · raw message
arch/x86/kvm/x86.c +11 -0
@@ -9320,6 +9320,17 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_hv_destroy_vm (kvm); } +void kvm_subpage_free_memslot (struct kvm_memory_slot *free, +struct kvm_memory_slot *dont) +{ + +if (!dont || free->arch.subpage_wp_info != + dont->arch.subpage_wp_info) { +kvfree (free->arch.subpage_wp_info); + free->arch.subpage_wp_info =NULL ; + } +} +void kvm_arch_free_memslot (struct kvm *kvm,struct kvm_memory_slot *free,struct kvm_memory_slot *dont) {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 38/92 KVM: VMX: Add init/set/get functions for SPP
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
init_spp() must be called before {get, set}_subpage functions, it creates subpage access bitmaps for memory pages and issues a KVM request to setup SPPT root pages.
kvm_mmu_set_subpages() is to enable SPP bit in EPT leaf page and setup corresponding SPPT entries. The mmu_lock
is held before above operation. If it's called in EPT fault and SPPT mis-config induced handler, mmu_lock is acquired outside the function, otherwise, it's acquired inside it.
kvm_mmu_get_subpages() is used to query access bitmap for protected page, it's also used in EPT fault handler to check whether the fault EPT page is SPP protected as well.
Message-Id: <[email protected]>
--- arch/x86/include/asm/kvm_host.h | 18 ++++ arch/x86/include/asm/vmx.h | 2 + arch/x86/kvm/mmu.c | 160 ++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 48 ++++++++++ arch/x86/kvm/x86.c | 57 ++++++++++++ include/linux/kvm_host.h | 3 + include/uapi/linux/kvm.h | 9 ++ 7 files changed, 297 insertions(+)
arch/x86/include/asm/kvm_host.h +18 -0
@@ -399,8 +399,13 @@ struct kvm_mmu {void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);void (*update_pte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,u64 *spte,const void *pte); +int (*get_subpages)(struct kvm *kvm,struct kvm_subpage *spp_info); +int (*set_subpages)(struct kvm *kvm,struct kvm_subpage *spp_info); +int (*init_spp)(struct kvm *kvm); +hpa_t root_hpa;gpa_t root_cr3; +hpa_t sppt_root;union kvm_mmu_role mmu_role;u8 root_level;u8 shadow_root_level;
@@ -929,6 +934,8 @@ struct kvm_arch {bool guest_can_read_msr_platform_info;bool exception_payload_enabled; + +bool spp_active; };struct kvm_vm_stat {
@@ -1202,6 +1209,11 @@ struct kvm_x86_ops {int (*nested_enable_evmcs)(struct kvm_vcpu *vcpu,uint16_t *vmcs_version);uint16_t (*nested_get_evmcs_version)(struct kvm_vcpu *vcpu); + +bool (*get_spp_status)(void ); +int (*get_subpages)(struct kvm *kvm,struct kvm_subpage *spp_info); +int (*set_subpages)(struct kvm *kvm,struct kvm_subpage *spp_info); +int (*init_spp)(struct kvm *kvm); };struct kvm_arch_async_pf {
@@ -1420,6 +1432,12 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);void kvm_mmu_invpcid_gva (struct kvm_vcpu *vcpu,gva_t gva,unsigned long pcid);void kvm_mmu_new_cr3 (struct kvm_vcpu *vcpu,gpa_t new_cr3,bool skip_tlb_flush); +int kvm_mmu_get_subpages (struct kvm *kvm,struct kvm_subpage *spp_info, +bool mmu_locked); +int kvm_mmu_set_subpages (struct kvm *kvm,struct kvm_subpage *spp_info, +bool mmu_locked); +int kvm_mmu_init_spp (struct kvm *kvm); +void kvm_enable_tdp (void );void kvm_disable_tdp (void );
arch/x86/include/asm/vmx.h +2 -0
@@ -224,6 +224,8 @@ enum vmcs_field {XSS_EXIT_BITMAP_HIGH =0x0000202D ,ENCLS_EXITING_BITMAP =0x0000202E ,ENCLS_EXITING_BITMAP_HIGH =0x0000202F , +SPPT_POINTER =0x00002030 , +SPPT_POINTER_HIGH =0x00002031 ,TSC_MULTIPLIER =0x00002032 ,TSC_MULTIPLIER_HIGH =0x00002033 , GUEST_PHYSICAL_ADDRESS = 0x00002400,
arch/x86/kvm/mmu.c +160 -0
184 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +48 -0
89 lines not on this page · raw message
arch/x86/kvm/x86.c +57 -0
69 lines not on this page · raw message
include/linux/kvm_host.h +3 -0
@@ -834,6 +834,9 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);struct kvm_mmu_page *kvm_mmu_get_spp_page (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int level); +int kvm_get_subpages (struct kvm *kvm,struct kvm_subpage *spp_info); +int kvm_set_subpages (struct kvm *kvm,struct kvm_subpage *spp_info); +int kvm_init_spp (struct kvm *kvm);#ifndef __KVM_HAVE_ARCH_VM_ALLOC/*
include/uapi/linux/kvm.h +9 -0
@@ -102,6 +102,15 @@ struct kvm_userspace_memory_region {__u64 userspace_addr;/* start of the userspace allocated memory */ }; +/* for KVM_SUBPAGES_GET_ACCESS and KVM_SUBPAGES_SET_ACCESS */ +#define SUBPAGE_MAX_BITMAP 64 +struct kvm_subpage { +__u64 base_gfn; +__u64 npages; +/* sub-page write-access bitmap array */ +__u32 access_map[SUBPAGE_MAX_BITMAP ]; +}; +/* * The bit 0 ~ bit 15 of kvm_memory_region::flags are visible for userspace, * other bits are reserved for kvm internal use which are defined in
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 39/92 KVM: VMX: Introduce SPP user-space IOCTLs
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
User application, e.g., QEMU or VMI, must initialize SPP before gets/sets SPP subpages, the dynamic initialization is to reduce the extra storage cost if the SPP feature is not not used.
Message-Id: <[email protected]>
--- arch/x86/kvm/x86.c | 73 ++++++++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 3 ++ include/uapi/linux/kvm.h | 3 ++ 3 files changed, 79 insertions(+)
arch/x86/kvm/x86.c +73 -0
@@ -4926,6 +4926,53 @@ long kvm_arch_vm_ioctl(struct file *filp,if (copy_from_user (&hvevfd, argp,sizeof (hvevfd))) goto out; r =kvm_vm_ioctl_hv_eventfd (kvm, &hvevfd); + } +case KVM_SUBPAGES_GET_ACCESS : { +struct kvm_subpage spp_info; + +if (!kvm->arch.spp_active) { + r = -ENODEV ; + goto out; + } + + r = -EFAULT ; +if (copy_from_user (&spp_info, argp,sizeof (spp_info))) + goto out; + + r = -EINVAL ; +if (spp_info.npages ==0 || + spp_info.npages >SUBPAGE_MAX_BITMAP ) + goto out; + + r =kvm_vm_ioctl_get_subpages (kvm, &spp_info); +if (copy_to_user (argp, &spp_info,sizeof (spp_info))) { + r = -EFAULT ; + goto out; + } +break ; + } +case KVM_SUBPAGES_SET_ACCESS : { +struct kvm_subpage spp_info; + +if (!kvm->arch.spp_active) { + r = -ENODEV ; + goto out; + } + + r = -EFAULT ; +if (copy_from_user (&spp_info, argp,sizeof (spp_info))) + goto out; + + r = -EINVAL ; +if (spp_info.npages ==0 || + spp_info.npages >SUBPAGE_MAX_BITMAP ) + goto out; + + r =kvm_vm_ioctl_set_subpages (kvm, &spp_info); +break ; + } +case KVM_INIT_SPP : { + r =kvm_vm_ioctl_init_spp (kvm);break ; }default :
@@ -9906,6 +9953,32 @@ bool kvm_arch_has_irq_bypass(void)return kvm_x86_ops->update_pi_irte !=NULL ; } +int kvm_arch_get_subpages (struct kvm *kvm, +struct kvm_subpage *spp_info) +{ +if (!kvm_x86_ops->get_subpages) +return -EINVAL ; + +return kvm_x86_ops->get_subpages (kvm, spp_info); +} + +int kvm_arch_set_subpages (struct kvm *kvm, +struct kvm_subpage *spp_info) +{ +if (!kvm_x86_ops->set_subpages) +return -EINVAL ; + +return kvm_x86_ops->set_subpages (kvm, spp_info); +} + +int kvm_arch_init_spp (struct kvm *kvm) +{ +if (!kvm_x86_ops->init_spp) +return -EINVAL ; + +return kvm_x86_ops->init_spp (kvm); +} +int kvm_arch_irq_bypass_add_producer (struct irq_bypass_consumer *cons,struct irq_bypass_producer *prod) {
include/linux/kvm_host.h +3 -0
@@ -837,6 +837,9 @@ struct kvm_mmu_page *kvm_mmu_get_spp_page(struct kvm_vcpu *vcpu,int kvm_get_subpages (struct kvm *kvm,struct kvm_subpage *spp_info);int kvm_set_subpages (struct kvm *kvm,struct kvm_subpage *spp_info);int kvm_init_spp (struct kvm *kvm); +int kvm_arch_get_subpages (struct kvm *kvm,struct kvm_subpage *spp_info); +int kvm_arch_set_subpages (struct kvm *kvm,struct kvm_subpage *spp_info); +int kvm_arch_init_spp (struct kvm *kvm);#ifndef __KVM_HAVE_ARCH_VM_ALLOC/*
include/uapi/linux/kvm.h +3 -0
@@ -1248,6 +1248,9 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SUBPAGES_GET_ACCESS _IOR(KVMIO, 0x49, __u64) +#define KVM_SUBPAGES_SET_ACCESS _IOW(KVMIO, 0x4a, __u64) +#define KVM_INIT_SPP _IOW(KVMIO, 0x4b, __u64)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 40/92 KVM: VMX: Handle SPP induced vmexit and page fault
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
If write to subpage is not allowed, EPT violation is generated, it's propagated to QEMU or VMI to handle.
If the target page is SPP protected, however SPPT missing is encoutered while traversing with gfn, vmexit is generated so that KVM can handle the issue. Any SPPT misconfig will be propagated to QEMU or VMI.
A SPP specific bit(11) is added to exit_qualification and a new exit reason(66) is introduced for SPP.
Message-Id: <[email protected]>
--- arch/x86/include/asm/vmx.h | 7 ++++ arch/x86/include/uapi/asm/vmx.h | 2 + arch/x86/kvm/mmu.c | 17 ++++++++ arch/x86/kvm/vmx/vmx.c | 71 +++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 5 +++ 5 files changed, 102 insertions(+)
arch/x86/include/asm/vmx.h +7 -0
@@ -547,6 +547,13 @@ struct vmx_msr_entry {#define EPT_VIOLATION_EXECUTABLE (1 << EPT_VIOLATION_EXECUTABLE_BIT)#define EPT_VIOLATION_GVA_TRANSLATED (1 << EPT_VIOLATION_GVA_TRANSLATED_BIT) +/* +* Exit Qualifications for SPPT-Induced vmexits +*/ +#define SPPT_INDUCED_EXIT_TYPE_BIT 11 +#define SPPT_INDUCED_EXIT_TYPE (1 << SPPT_INDUCED_EXIT_TYPE_BIT) +#define SPPT_INTR_INFO_UNBLOCK_NMI INTR_INFO_UNBLOCK_NMI +/* * VM-instruction error numbers */
arch/x86/include/uapi/asm/vmx.h +2 -0
@@ -85,6 +85,7 @@#define EXIT_REASON_PML_FULL 62#define EXIT_REASON_XSAVES 63#define EXIT_REASON_XRSTORS 64 +#define EXIT_REASON_SPP 66#define VMX_EXIT_REASONS \ { EXIT_REASON_EXCEPTION_NMI, "EXCEPTION_NMI" }, \
@@ -141,6 +142,7 @@ { EXIT_REASON_ENCLS, "ENCLS" }, \ { EXIT_REASON_RDSEED, "RDSEED" }, \ { EXIT_REASON_PML_FULL, "PML_FULL" }, \ + { EXIT_REASON_SPP, "SPP" }, \ { EXIT_REASON_XSAVES, "XSAVES" }, \ { EXIT_REASON_XRSTORS, "XRSTORS" }
arch/x86/kvm/mmu.c +17 -0
@@ -3692,6 +3692,19 @@ static bool fast_page_fault(struct kvm_vcpu *vcpu, gva_t gva, int level,if ((error_code &PFERR_WRITE_MASK ) &&spte_can_locklessly_be_made_writable (spte)) { +/* +* Record write protect fault caused by +* Sub-page Protection, let VMI decide +* the next step. +*/ +if (spte &PT_SPP_MASK ) { + fault_handled = true; + vcpu->run->exit_reason =KVM_EXIT_SPP ; + vcpu->run->spp.addr = gva; +kvm_skip_emulated_instruction (vcpu); +break ; + } + new_spte |=PT_WRITABLE_MASK ;/*
@@ -5880,6 +5893,10 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code, r = vcpu->arch.mmu->page_fault (vcpu, cr2,lower_32_bits (error_code), false); + +if (vcpu->run->exit_reason ==KVM_EXIT_SPP ) +return 0 ; +WARN_ON (r ==RET_PF_INVALID ); }
arch/x86/kvm/vmx/vmx.c +71 -0
83 lines not on this page · raw message
include/uapi/linux/kvm.h +5 -0
@@ -244,6 +244,7 @@ struct kvm_hyperv_exit {#define KVM_EXIT_S390_STSI 25#define KVM_EXIT_IOAPIC_EOI 26#define KVM_EXIT_HYPERV 27 +#define KVM_EXIT_SPP 28/* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */
@@ -399,6 +400,10 @@ struct kvm_run {struct {__u8 vector; } eoi; +/* KVM_EXIT_SPP */ +struct { +__u64 addr; + } spp;/* KVM_EXIT_HYPERV */ struct kvm_hyperv_exit hyperv;/* Fix the size of the union. */
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 41/92 KVM: MMU: Enable Lazy mode SPPT setup
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
If SPP subpages are set while the physical page are not available in EPT leaf entry, the mapping is first stored in SPP access bitmap buffer. SPPT setup is deferred to access to the protected page, in EPT page fault handler, the SPPT enries are set up.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
arch/x86/kvm/mmu.c +24 -0
@@ -4400,6 +4400,26 @@ check_hugepage_cache_consistency(struct kvm_vcpu *vcpu, gfn_t gfn, int level)return kvm_mtrr_check_gfn_range_consistency (vcpu, gfn, page_num); } +static int kvm_enable_spp_protection (struct kvm *kvm,u64 gfn) +{ +struct kvm_subpage spp_info = {0 }; +struct kvm_memory_slot *slot; + + slot =gfn_to_memslot (kvm, gfn); +if (!slot) +return -EFAULT ; + + spp_info.base_gfn = gfn; + spp_info.npages =1 ; + +if (kvm_mmu_get_subpages (kvm, &spp_info, true) <0 ) +return -EFAULT ; + +if (spp_info.access_map[0 ] !=FULL_SPP_ACCESS ) +kvm_mmu_set_subpages (kvm, &spp_info, true); + +return 0 ; +}static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code,bool prefault) {
@@ -4451,6 +4471,10 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,if (likely (!force_pt_level))transparent_hugepage_adjust (vcpu, &gfn, &pfn, &level); r =__direct_map (vcpu, write, map_writable, level, gfn, pfn, prefault); + +if (vcpu->kvm->arch.spp_active && level ==PT_PAGE_TABLE_LEVEL ) +kvm_enable_spp_protection (vcpu->kvm, gfn); +spin_unlock (&vcpu->kvm->mmu_lock);return r;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 42/92 KVM: MMU: Handle host memory remapping and reclaim
In reply to Adalbert LazărFrom: Yang Weijiang <[email protected]>
Host page swapping/migration may change the translation in EPT leaf entry, if the target page is SPP protected, re-enable SPP protection in MMU notifier. If SPPT shadow page is reclaimed, the level1 pages don't have rmap to clear.
Message-Id: <[email protected]>
--- arch/x86/kvm/mmu.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
arch/x86/kvm/mmu.c +22 -0
@@ -2004,6 +2004,24 @@ static int kvm_set_pte_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head, new_spte &= ~PT_WRITABLE_MASK ; new_spte &= ~SPTE_HOST_WRITEABLE ; +/* +* if it's EPT leaf entry and the physical page is +* SPP protected, then re-enable SPP protection for +* the page. +*/ +if (kvm->arch.spp_active && + level ==PT_PAGE_TABLE_LEVEL ) { +struct kvm_subpage spp_info = {0 }; +int i; + + spp_info.base_gfn = gfn; + spp_info.npages =1 ; + i =kvm_mmu_get_subpages (kvm, &spp_info, true); +if (i ==1 && + spp_info.access_map[0 ] !=FULL_SPP_ACCESS ) + new_spte |=PT_SPP_MASK ; + } + new_spte =mark_spte_for_access_track (new_spte);mmu_spte_clear_track_bits (sptep);
@@ -2905,6 +2923,10 @@ static bool mmu_page_zap_pte(struct kvm *kvm, struct kvm_mmu_page *sp, pte = *spte;if (is_shadow_present_pte (pte)) {if (is_last_spte (pte, sp->role.level)) { +/* SPPT leaf entries don't have rmaps*/ +if (sp->role.level ==PT_PAGE_TABLE_LEVEL && +is_spp_spte (sp)) +return true;drop_spte (kvm, spte);if (is_large_pte (pte)) --kvm->stat.lpages;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 43/92 kvm: introspection: add KVMI_CONTROL_SPP
In reply to Adalbert LazărThis command enables/disables subpage protection (SPP) for the current VM.
--- Documentation/virtual/kvm/kvmi.rst | 33 ++++++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 4 ++++ include/uapi/linux/kvmi.h | 7 +++++++ virt/kvm/kvmi_int.h | 6 ++++++ virt/kvm/kvmi_msg.c | 33 ++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+)
Documentation/virtual/kvm/kvmi.rst +33 -0
@@ -617,6 +617,39 @@ In order to 'forget' an address, all the access bits ('rwx') must be set. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to add the page tracking structures +11. KVMI_CONTROL_SPP +-------------------- + +:Architectures: x86/intel +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_control_spp { + __u8 enable; + __u8 padding1; + __u16 padding2; + __u32 padding3; + } + +:Returns: + +:: + + struct kvmi_error_code; + +Enables/disables subpage protection (SPP) for the current VM. + +If SPP is not enabled, *KVMI_GET_PAGE_WRITE_BITMAP* and +*KVMI_SET_PAGE_WRITE_BITMAP* commands will fail. + +:Errors: + +* -KVM_EINVAL - padding is not zero +* -KVM_EOPNOTSUPP - the hardware doesn't support SPP +* -KVM_EOPNOTSUPP - the current implementation can't disable SPP + Events ======
arch/x86/kvm/kvmi.c +4 -0
@@ -260,3 +260,7 @@ int kvmi_arch_cmd_set_page_access(struct kvmi *ikvm,return ec; } +int kvmi_arch_cmd_control_spp (struct kvmi *ikvm) +{ +return kvm_arch_init_spp (ikvm->kvm); +}
include/uapi/linux/kvmi.h +7 -0
@@ -142,6 +142,13 @@ struct kvmi_set_page_access {struct kvmi_page_access_entry entries[0 ]; }; +struct kvmi_control_spp { +__u8 enable; +__u8 padding1; +__u16 padding2; +__u32 padding3; +}; +struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; };
virt/kvm/kvmi_int.h +6 -0
@@ -130,6 +130,11 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +struct { +bool initialized; +atomic_t enabled; + } spp; +bool cmd_reply_disabled; };
@@ -184,6 +189,7 @@ int kvmi_arch_cmd_get_page_access(struct kvmi *ikvm,int kvmi_arch_cmd_set_page_access (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg,const struct kvmi_set_page_access *req); +int kvmi_arch_cmd_control_spp (struct kvmi *ikvm);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access);
virt/kvm/kvmi_msg.c +33 -0
@@ -25,6 +25,7 @@ static const char *const msg_IDs[] = { [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", [KVMI_CONTROL_EVENTS] = "KVMI_CONTROL_EVENTS", + [KVMI_CONTROL_SPP] = "KVMI_CONTROL_SPP", [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS", [KVMI_EVENT] = "KVMI_EVENT", [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY",
@@ -300,6 +301,37 @@ static int kvmi_get_vcpu(struct kvmi *ikvm, unsigned int vcpu_idx,return 0 ; } +static bool enable_spp (struct kvmi *ikvm) +{ +if (!ikvm->spp.initialized) { +int err =kvmi_arch_cmd_control_spp (ikvm); + + ikvm->spp.initialized = true; + +if (!err) +atomic_set (&ikvm->spp.enabled, true); + } + +return atomic_read (&ikvm->spp.enabled); +} + +static int handle_control_spp (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +const struct kvmi_control_spp *req = _req; +int ec; + +if (req->padding1 || req->padding2 || req->padding3) + ec = -KVM_EINVAL ; +else if (req->enable &&enable_spp (ikvm)) + ec =0 ; +else + ec = -KVM_EOPNOTSUPP ; + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} +static int handle_control_cmd_response (struct kvmi *ikvm,const struct kvmi_msg_hdr *msg,const void *_req)
@@ -364,6 +396,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CHECK_COMMAND] = handle_check_command, [KVMI_CHECK_EVENT] = handle_check_event, [KVMI_CONTROL_CMD_RESPONSE] = handle_control_cmd_response, + [KVMI_CONTROL_SPP] = handle_control_spp, [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, [KVMI_GET_PAGE_ACCESS] = handle_get_page_access,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 44/92 kvm: introspection: extend the internal database of tracked pages with write_bitmap info
In reply to Adalbert LazărThis will allow us to use the subpage protection feature.
--- virt/kvm/kvmi.c | 46 +++++++++++++++++++++++++++++++++++++-------- virt/kvm/kvmi_int.h | 1 + 2 files changed, 39 insertions(+), 8 deletions(-)
virt/kvm/kvmi.c +38 -8
@@ -32,6 +32,7 @@ static void kvmi_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot,static const u8 full_access =KVMI_PAGE_ACCESS_R |KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X ; +static const u32 default_write_access_bitmap;void *kvmi_msg_alloc (void ) {
@@ -57,23 +58,32 @@ static struct kvmi_mem_access *__kvmi_get_gfn_access(struct kvmi *ikvm,return radix_tree_lookup (&ikvm->access_tree, gfn); } +/* +* TODO: intercept any SPP change made on pages present in our radix tree. +* +* bitmap must have the same value as the corresponding SPPT entry. +*/ static int kvmi_get_gfn_access (struct kvmi *ikvm,const gfn_t gfn, -u8 *access) +u8 *access,u32 *write_bitmap) {struct kvmi_mem_access *m; + *write_bitmap = default_write_access_bitmap; *access = full_access;read_lock (&ikvm->access_tree_lock); m =__kvmi_get_gfn_access (ikvm, gfn); -if (m) +if (m) { *access = m->access; + *write_bitmap = m->write_bitmap; + }read_unlock (&ikvm->access_tree_lock);return m ?0 :-1 ; } -static int kvmi_set_gfn_access (struct kvm *kvm,gfn_t gfn,u8 access) +static int kvmi_set_gfn_access (struct kvm *kvm,gfn_t gfn,u8 access, +u32 write_bitmap) {struct kvmi_mem_access *m;struct kvmi_mem_access *__m;
@@ -87,6 +97,7 @@ static int kvmi_set_gfn_access(struct kvm *kvm, gfn_t gfn, u8 access) m->gfn = gfn; m->access = access; + m->write_bitmap = write_bitmap;if (radix_tree_preload (GFP_KERNEL )) { err = -KVM_ENOMEM ;
@@ -100,6 +111,7 @@ static int kvmi_set_gfn_access(struct kvm *kvm, gfn_t gfn, u8 access) __m =__kvmi_get_gfn_access (ikvm, gfn);if (__m) { __m->access = access; + __m->write_bitmap = write_bitmap;kvmi_arch_update_page_tracking (kvm,NULL , __m);if (access == full_access) {radix_tree_delete (&ikvm->access_tree, gfn);
@@ -124,12 +136,22 @@ static int kvmi_set_gfn_access(struct kvm *kvm, gfn_t gfn, u8 access)return err; } +static bool spp_access_allowed (gpa_t gpa,unsigned long bitmap) +{ +u32 off = (gpa & ~PAGE_MASK ); +u32 spp = off /128 ; + +return test_bit (spp, &bitmap); +} +static bool kvmi_restricted_access (struct kvmi *ikvm,gpa_t gpa,u8 access) { +u32 allowed_bitmap;u8 allowed_access;int err; - err =kvmi_get_gfn_access (ikvm,gpa_to_gfn (gpa), &allowed_access); + err =kvmi_get_gfn_access (ikvm,gpa_to_gfn (gpa), &allowed_access, + &allowed_bitmap);if (err)return false;
@@ -138,8 +160,14 @@ static bool kvmi_restricted_access(struct kvmi *ikvm, gpa_t gpa, u8 access)* We want to be notified only for violations involving access * bits that we've specifically cleared */ -if ((~allowed_access) & access) +if ((~allowed_access) & access) { +bool write_access = (access &KVMI_PAGE_ACCESS_W ); + +if (write_access &&spp_access_allowed (gpa, allowed_bitmap)) +return false; +return true; + }return false; }
@@ -1126,8 +1154,9 @@ void kvmi_handle_requests(struct kvm_vcpu *vcpu)int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access) {gfn_t gfn =gpa_to_gfn (gpa); +u32 ignored_write_bitmap; -kvmi_get_gfn_access (ikvm, gfn, access); +kvmi_get_gfn_access (ikvm, gfn, access, &ignored_write_bitmap);return 0 ; }
@@ -1136,10 +1165,11 @@ int kvmi_cmd_set_page_access(struct kvmi *ikvm, u64 gpa, u8 access) {gfn_t gfn =gpa_to_gfn (gpa);u8 ignored_access; +u32 write_bitmap; -kvmi_get_gfn_access (ikvm, gfn, &ignored_access); +kvmi_get_gfn_access (ikvm, gfn, &ignored_access, &write_bitmap); -return kvmi_set_gfn_access (ikvm->kvm, gfn, access); +return kvmi_set_gfn_access (ikvm->kvm, gfn, access, write_bitmap); } int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id,
virt/kvm/kvmi_int.h +1 -0
@@ -141,6 +141,7 @@ struct kvmi {struct kvmi_mem_access {gfn_t gfn;u8 access; +u32 write_bitmap;struct kvmi_arch_mem_access arch; };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 45/92 kvm: introspection: add KVMI_GET_PAGE_WRITE_BITMAP
In reply to Adalbert LazărThis command returns subpage protection (SPP) write bitmaps for an array of guest physical addresses of 4KB size.
Like the KVMI_GET_PAGE_ACCESS command, it checks only the radix tree, not the SPP tables. So, either we change it to check the SPP tables or we drop it. Given the fact that the KVMI_EVENT_PF events are filter using the radix tree and that the introspection tool should know what it tracks, we should choose the later.
--- Documentation/virtual/kvm/kvmi.rst | 44 ++++++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 44 ++++++++++++++++++++++++++++++ include/uapi/linux/kvmi.h | 11 ++++++++ virt/kvm/kvmi.c | 11 ++++++++ virt/kvm/kvmi_int.h | 11 ++++++++ virt/kvm/kvmi_msg.c | 18 ++++++++++++ 6 files changed, 139 insertions(+)
Documentation/virtual/kvm/kvmi.rst +44 -0
@@ -650,6 +650,50 @@ If SPP is not enabled, *KVMI_GET_PAGE_WRITE_BITMAP* and * -KVM_EOPNOTSUPP - the hardware doesn't support SPP * -KVM_EOPNOTSUPP - the current implementation can't disable SPP +12. KVMI_GET_PAGE_WRITE_BITMAP +------------------------------ + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_get_page_write_bitmap { + __u16 view; + __u16 count; + __u32 padding; + __u64 gpa[0]; + }; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_page_write_bitmap_reply { + __u32 bitmap[0]; + }; + +Returns subpage protection (SPP) write bitmaps for an array of ``count`` +guest physical addresses of 4KB size. + +By default, for any guest physical address, the returned bits will be zero +(no write access for any subpage if the *KVMI_PAGE_ACCESS_W* flag has been +cleared for the whole 4KB page - see *KVMI_SET_PAGE_ACCESS*). + +On Intel hardware with multiple EPT views, the ``view`` argument selects the +EPT view (0 is primary). On all other hardware it must be zero. + +:Errors: + +* -KVM_EINVAL - the selected SPT view is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EOPNOTSUPP - a SPT view was selected but the hardware doesn't support it +* -KVM_EOPNOTSUPP - the hardware doesn't support SPP or hasn't been enabled +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to allocate the reply + Events ======
arch/x86/kvm/kvmi.c +44 -0
50 lines not on this page · raw message
include/uapi/linux/kvmi.h +11 -0
@@ -149,6 +149,17 @@ struct kvmi_control_spp {__u32 padding3; }; +struct kvmi_get_page_write_bitmap { +__u16 view; +__u16 count; +__u32 padding; +__u64 gpa[0 ]; +}; + +struct kvmi_get_page_write_bitmap_reply { +__u32 bitmap[0 ]; +}; +struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; };
virt/kvm/kvmi.c +11 -0
@@ -1161,6 +1161,17 @@ int kvmi_cmd_get_page_access(struct kvmi *ikvm, u64 gpa, u8 *access)return 0 ; } +int kvmi_cmd_get_page_write_bitmap (struct kvmi *ikvm,u64 gpa, +u32 *write_bitmap) +{ +gfn_t gfn =gpa_to_gfn (gpa); +u8 ignored_access; + +kvmi_get_gfn_access (ikvm, gfn, &ignored_access, write_bitmap); + +return 0 ; +} +int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access) {gfn_t gfn =gpa_to_gfn (gpa);
virt/kvm/kvmi_int.h +11 -0
@@ -150,6 +150,11 @@ static inline bool is_event_enabled(struct kvm_vcpu *vcpu, int event)return test_bit (event,IVCPU (vcpu)->ev_mask); } +static inline bool kvmi_spp_enabled (struct kvmi *ikvm) +{ +return atomic_read (&ikvm->spp.enabled); +} +/* kvmi_msg.c */ bool kvmi_sock_get (struct kvmi *ikvm,int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);
@@ -167,6 +172,7 @@ void *kvmi_msg_alloc_check(size_t size);void kvmi_msg_free (void *addr);int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access);int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access); +int kvmi_cmd_get_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 *bitmap);int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable); int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,
@@ -191,6 +197,11 @@ int kvmi_arch_cmd_set_page_access(struct kvmi *ikvm,const struct kvmi_msg_hdr *msg, const struct kvmi_set_page_access *req);int kvmi_arch_cmd_control_spp (struct kvmi *ikvm); +int kvmi_arch_cmd_get_page_write_bitmap (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_get_page_write_bitmap *req, +struct kvmi_get_page_write_bitmap_reply **dest, +size_t *dest_size);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access);
virt/kvm/kvmi_msg.c +18 -0
@@ -31,6 +31,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", + [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", [KVMI_SET_PAGE_ACCESS] = "KVMI_SET_PAGE_ACCESS",
@@ -383,6 +384,22 @@ static int handle_set_page_access(struct kvmi *ikvm,return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); } +static int handle_get_page_write_bitmap (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *req) +{ +struct kvmi_get_page_write_bitmap_reply *rpl =NULL ; +size_t rpl_size =0 ; +int err, ec; + + ec =kvmi_arch_cmd_get_page_write_bitmap (ikvm, msg, req, &rpl, + &rpl_size); + + err =kvmi_msg_vm_maybe_reply (ikvm, msg, ec, rpl, rpl_size); +kvmi_msg_free (rpl); +return err; +} +static bool invalid_vcpu_hdr (const struct kvmi_vcpu_hdr *hdr) {return hdr->padding1 || hdr->padding2;
@@ -400,6 +417,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, [KVMI_GET_PAGE_ACCESS] = handle_get_page_access, + [KVMI_GET_PAGE_WRITE_BITMAP] = handle_get_page_write_bitmap, [KVMI_GET_VERSION] = handle_get_version, [KVMI_SET_PAGE_ACCESS] = handle_set_page_access, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 46/92 kvm: introspection: add KVMI_SET_PAGE_WRITE_BITMAP
In reply to Adalbert LazărThis command sets the subpage protection (SPP) write bitmap for an array of guest physical addresses of 4KB bytes.
--- Documentation/virtual/kvm/kvmi.rst | 66 ++++++++++++++++++++++++++++++ arch/x86/kvm/kvmi.c | 30 ++++++++++++++ include/uapi/linux/kvmi.h | 13 ++++++ virt/kvm/kvmi.c | 37 +++++++++++++++++ virt/kvm/kvmi_int.h | 4 ++ virt/kvm/kvmi_msg.c | 13 ++++++ 6 files changed, 163 insertions(+)
Documentation/virtual/kvm/kvmi.rst +66 -0
@@ -694,6 +694,72 @@ EPT view (0 is primary). On all other hardware it must be zero. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to allocate the reply +13. KVMI_SET_PAGE_WRITE_BITMAP +------------------------------ + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_set_page_write_bitmap { + __u16 view; + __u16 count; + __u32 padding; + struct kvmi_page_write_bitmap_entry entries[0]; + }; + +where:: + + struct kvmi_page_write_bitmap_entry { + __u64 gpa; + __u32 bitmap; + __u32 padding; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Sets the subpage protection (SPP) write bitmap for an array of ``count`` +guest physical addresses of 4KB bytes. + +The command will make the changes starting with the first entry and +it will stop on the first error. The introspection tool should handle +the rollback. + +While the *KVMI_SET_PAGE_ACCESS* command can be used to write-protect a +4KB page, this command can write-protect 128-bytes subpages inside of a +4KB page by setting the corresponding bit to 1 (write allowed) or to 0 +(write disallowed). For example, to allow write access to the A and B +subpages only, the bitmap must be set to:: + + BIT(A) | BIT(B) + +A and B must be a number between 0 (first subpage) and 31 (last subpage). + +Using this command to set all bits to 1 (allow write access for +all subpages) will allow write access to the whole 4KB page (like a +*KVMI_SET_PAGE_ACCESS* command with the *KVMI_PAGE_ACCESS_W* flag set) +and vice versa. + +Using this command to set any bit to 0 will write-protect the whole 4KB +page (like a *KVMI_SET_PAGE_ACCESS* command with the *KVMI_PAGE_ACCESS_W* +flag cleared) and allow write access only for subpages with the +corresponding bit set to 1. + +:Errors: + +* -KVM_EINVAL - the selected SPT view is invalid +* -KVM_EOPNOTSUPP - a SPT view was selected but the hardware doesn't support it +* -KVM_EOPNOTSUPP - the hardware doesn't support SPP or hasn't been enabled +* -KVM_EINVAL - the write access is already allowed for the whole 4KB page +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to add the page tracking structures + Events ======
arch/x86/kvm/kvmi.c +30 -0
@@ -304,6 +304,36 @@ int kvmi_arch_cmd_set_page_access(struct kvmi *ikvm,return ec; } +int kvmi_arch_cmd_set_page_write_bitmap (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_set_page_write_bitmap + *req) +{ +u16 k, n = req->count; +int ec =0 ; + +if (req->padding) +return -KVM_EINVAL ; + +if (msg->size <sizeof (*req) + req->count *sizeof (req->entries[0 ])) +return -KVM_EINVAL ; + +if (!kvmi_spp_enabled (ikvm)) +return -KVM_EOPNOTSUPP ; + +if (req->view !=0 )/* TODO */ +return -KVM_EOPNOTSUPP ; + +for (k =0 ; k < n && ec ==0 ; k++) { +u64 gpa = req->entries[k].gpa; +u32 bitmap = req->entries[k].bitmap; + + ec =kvmi_cmd_set_page_write_bitmap (ikvm, gpa, bitmap); + } + +return ec; +} +int kvmi_arch_cmd_control_spp (struct kvmi *ikvm) {return kvm_arch_init_spp (ikvm->kvm);
include/uapi/linux/kvmi.h +13 -0
@@ -160,6 +160,19 @@ struct kvmi_get_page_write_bitmap_reply {__u32 bitmap[0 ]; }; +struct kvmi_page_write_bitmap_entry { +__u64 gpa; +__u32 bitmap; +__u32 padding; +}; + +struct kvmi_set_page_write_bitmap { +__u16 view; +__u16 count; +__u32 padding; +struct kvmi_page_write_bitmap_entry entries[0 ]; +}; +struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; };
virt/kvm/kvmi.c +37 -0
@@ -99,6 +99,24 @@ static int kvmi_set_gfn_access(struct kvm *kvm, gfn_t gfn, u8 access, m->access = access; m->write_bitmap = write_bitmap; +/* +* Only try to set SPP bitmap when the page is writable. +* Be careful, kvm_mmu_set_subpages() will enable page write-protection +* by default when set SPP bitmap. If bitmap contains all 1s, it'll +* make the page writable by default too. +*/ +if (!(access &KVMI_PAGE_ACCESS_W ) &&kvmi_spp_enabled (ikvm)) { +struct kvm_subpage spp_info; + + spp_info.base_gfn = gfn; + spp_info.npages =1 ; + spp_info.access_map[0 ] = write_bitmap; + + err =kvm_arch_set_subpages (kvm, &spp_info); +if (err) + goto exit; + } +if (radix_tree_preload (GFP_KERNEL )) { err = -KVM_ENOMEM ; goto exit;
@@ -1183,6 +1201,25 @@ int kvmi_cmd_set_page_access(struct kvmi *ikvm, u64 gpa, u8 access)return kvmi_set_gfn_access (ikvm->kvm, gfn, access, write_bitmap); } +int kvmi_cmd_set_page_write_bitmap (struct kvmi *ikvm,u64 gpa, +u32 write_bitmap) +{ +bool write_allowed_for_all; +gfn_t gfn =gpa_to_gfn (gpa); +u32 ignored_write_bitmap; +u8 access; + +kvmi_get_gfn_access (ikvm, gfn, &access, &ignored_write_bitmap); + + write_allowed_for_all = (write_bitmap == (u32)((1ULL <<32 ) -1 )); +if (write_allowed_for_all) + access |=KVMI_PAGE_ACCESS_W ; +else + access &= ~KVMI_PAGE_ACCESS_W ; + +return kvmi_set_gfn_access (ikvm->kvm, gfn, access, write_bitmap); +} +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable) {
virt/kvm/kvmi_int.h +4 -0
@@ -173,6 +173,7 @@ void kvmi_msg_free(void *addr);int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access);int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access);int kvmi_cmd_get_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 *bitmap); +int kvmi_cmd_set_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 bitmap);int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable); int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,
@@ -202,6 +203,9 @@ int kvmi_arch_cmd_get_page_write_bitmap(struct kvmi *ikvm,const struct kvmi_get_page_write_bitmap *req, struct kvmi_get_page_write_bitmap_reply **dest, size_t *dest_size); +int kvmi_arch_cmd_set_page_write_bitmap (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const struct kvmi_set_page_write_bitmap *req);void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access);
virt/kvm/kvmi_msg.c +13 -0
@@ -35,6 +35,7 @@ static const char *const msg_IDs[] = { [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", [KVMI_SET_PAGE_ACCESS] = "KVMI_SET_PAGE_ACCESS", + [KVMI_SET_PAGE_WRITE_BITMAP] = "KVMI_SET_PAGE_WRITE_BITMAP", };static bool is_known_message (u16 id)
@@ -400,6 +401,17 @@ static int handle_get_page_write_bitmap(struct kvmi *ikvm,return err; } +static int handle_set_page_write_bitmap (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *req) +{ +int ec; + + ec =kvmi_arch_cmd_set_page_write_bitmap (ikvm, msg, req); + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); +} +static bool invalid_vcpu_hdr (const struct kvmi_vcpu_hdr *hdr) {return hdr->padding1 || hdr->padding2;
@@ -420,6 +432,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_GET_PAGE_WRITE_BITMAP] = handle_get_page_write_bitmap, [KVMI_GET_VERSION] = handle_get_version, [KVMI_SET_PAGE_ACCESS] = handle_set_page_access, + [KVMI_SET_PAGE_WRITE_BITMAP] = handle_set_page_write_bitmap, }; static int handle_event_reply(struct kvm_vcpu *vcpu,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 47/92 kvm: introspection: add KVMI_READ_PHYSICAL and KVMI_WRITE_PHYSICAL
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
These commands allows the introspection tool to read/write from/to the guest memory.
--- Documentation/virtual/kvm/kvmi.rst | 60 ++++++++++++++++ include/uapi/linux/kvmi.h | 11 +++ virt/kvm/kvmi.c | 107 +++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 7 ++ virt/kvm/kvmi_msg.c | 42 +++++++++++ 5 files changed, 227 insertions(+)
Documentation/virtual/kvm/kvmi.rst +60 -0
@@ -760,6 +760,66 @@ corresponding bit set to 1. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to add the page tracking structures +14. KVMI_READ_PHYSICAL +---------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_read_physical { + __u64 gpa; + __u64 size; + }; + +:Returns: + +:: + + struct kvmi_error_code; + __u8 data[0]; + +Reads from the guest memory. + +Currently, the size must be non-zero and the read must be restricted to +one page (offset + size <= PAGE_SIZE). + +:Errors: + +* -KVM_EINVAL - the specified gpa is invalid + +15. KVMI_WRITE_PHYSICAL +----------------------- + +:Architectures: all +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_write_physical { + __u64 gpa; + __u64 size; + __u8 data[0]; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Writes into the guest memory. + +Currently, the size must be non-zero and the write must be restricted to +one page (offset + size <= PAGE_SIZE). + +:Errors: + +* -KVM_EINVAL - the specified gpa is invalid + Events ======
include/uapi/linux/kvmi.h +11 -0
@@ -191,6 +191,17 @@ struct kvmi_control_vm_events {__u32 padding2; }; +struct kvmi_read_physical { +__u64 gpa; +__u64 size; +}; + +struct kvmi_write_physical { +__u64 gpa; +__u64 size; +__u8 data[0 ]; +}; +struct kvmi_vcpu_hdr {__u16 vcpu;__u16 padding1;
virt/kvm/kvmi.c +107 -0
119 lines not on this page · raw message
virt/kvm/kvmi_int.h +7 -0
@@ -174,6 +174,13 @@ int kvmi_cmd_get_page_access(struct kvmi *ikvm, u64 gpa, u8 *access);int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access);int kvmi_cmd_get_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 *bitmap);int kvmi_cmd_set_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 bitmap); +int kvmi_cmd_read_physical (struct kvm *kvm,u64 gpa,u64 size, +int (*send)(struct kvmi *, +const struct kvmi_msg_hdr *, +int err,const void *buf,size_t ), +const struct kvmi_msg_hdr *ctx); +int kvmi_cmd_write_physical (struct kvm *kvm,u64 gpa,u64 size, +const void *buf);int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable); int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id,
virt/kvm/kvmi_msg.c +42 -0
64 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 48/92 kvm: add kvm_vcpu_kick_and_wait()
In reply to Adalbert LazărThis function is needed for the KVMI_PAUSE_VCPU command. There are cases when it is easier for the introspection tool if it knows that the vCPU doesn't run guest code when the command is completed, without waiting for the KVMI_EVENT_PAUSE_VCPU event.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
include/linux/kvm_host.h +1 -0
@@ -738,6 +738,7 @@ void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu);void kvm_arch_vcpu_unblocking (struct kvm_vcpu *vcpu);bool kvm_vcpu_wake_up (struct kvm_vcpu *vcpu);void kvm_vcpu_kick (struct kvm_vcpu *vcpu); +void kvm_vcpu_kick_and_wait (struct kvm_vcpu *vcpu);int kvm_vcpu_yield_to (struct kvm_vcpu *target);void kvm_vcpu_on_spin (struct kvm_vcpu *vcpu,bool usermode_vcpu_not_eligible);
virt/kvm/kvm_main.c +10 -0
@@ -2370,6 +2370,16 @@ void kvm_vcpu_kick(struct kvm_vcpu *vcpu)EXPORT_SYMBOL_GPL (kvm_vcpu_kick);#endif /* !CONFIG_S390 */ +void kvm_vcpu_kick_and_wait (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_wake_up (vcpu)) +return ; + +if (kvm_request_needs_ipi (vcpu,KVM_REQUEST_WAIT )) +smp_call_function_single (vcpu->cpu, ack_flush,NULL ,1 ); +} +EXPORT_SYMBOL_GPL (kvm_vcpu_kick_and_wait); +int kvm_vcpu_yield_to (struct kvm_vcpu *target) {struct pid *pid;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 49/92 kvm: introspection: add KVMI_PAUSE_VCPU and KVMI_EVENT_PAUSE_VCPU
In reply to Adalbert LazărThis is the only vCPU command handled by the receiving worker. It increments a pause request counter and kicks the vCPU.
This event is send by the vCPU thread, but has a low priority. It will be sent after any other vCPU introspection event and when no vCPU introspection command is queued.
--- Documentation/virtual/kvm/kvmi.rst | 68 ++++++++++++++++++++++++++++++ include/uapi/linux/kvm_para.h | 1 + include/uapi/linux/kvmi.h | 7 +++ virt/kvm/kvmi.c | 65 ++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 4 ++ virt/kvm/kvmi_msg.c | 61 +++++++++++++++++++++++++++ 6 files changed, 206 insertions(+)
Documentation/virtual/kvm/kvmi.rst +68 -0
@@ -820,6 +820,48 @@ one page (offset + size <= PAGE_SIZE). * -KVM_EINVAL - the specified gpa is invalid +16. KVMI_PAUSE_VCPU +------------------- + +:Architecture: all +:Versions: >= 1 +:Parameters: + + struct kvmi_vcpu_hdr; + struct kvmi_pause_vcpu { + __u8 wait; + __u8 padding1; + __u16 padding2; + __u32 padding3; + }; + +:Returns: + +:: + + struct kvmi_error_code; + +Kicks the vCPU from guest. + +If `wait` is 1, the command will wait for vCPU to acknowledge the IPI. + +The vCPU will handle the pending commands/events and send the +*KVMI_EVENT_PAUSE_VCPU* event (one for every successful *KVMI_PAUSE_VCPU* +command) before returning to guest. + +Please note that new vCPUs might by created at any time. +The introspection tool should use *KVMI_CONTROL_VM_EVENTS* to enable the +*KVMI_EVENT_CREATE_VCPU* event in order to stop these new vCPUs as well +(by delaying the event reply). + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_EBUSY - the selected vCPU has too many queued *KVMI_EVENT_PAUSE_VCPU* events +* -KVM_EPERM - the *KVMI_EVENT_PAUSE_VCPU* event is disallowed (see *KVMI_CONTROL_EVENTS*) + and the introspection tool expects a reply. Events ======
@@ -992,3 +1034,29 @@ The *RETRY* action is used by the introspector to retry the execution of the current instruction. Either using single-step (if ``singlestep`` is not zero) or return to guest (if the introspector changed the instruction pointer or the page restrictions). + +4. KVMI_EVENT_PAUSE_VCPU +------------------------ + +:Architectures: all +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent in response to a *KVMI_PAUSE_VCPU* command and +cannot be disabled via *KVMI_CONTROL_EVENTS*. + +This event has a low priority. It will be sent after any other vCPU +introspection event and when no vCPU introspection command is queued. +
include/uapi/linux/kvm_para.h +1 -0
@@ -18,6 +18,7 @@#define KVM_EPERM EPERM#define KVM_EOPNOTSUPP 95#define KVM_EAGAIN 11 +#define KVM_EBUSY EBUSY#define KVM_ENOMEM ENOMEM#define KVM_HC_VAPIC_POLL_IRQ 1
include/uapi/linux/kvmi.h +7 -0
@@ -177,6 +177,13 @@ struct kvmi_get_vcpu_info_reply {__u64 tsc_speed; }; +struct kvmi_pause_vcpu { +__u8 wait; +__u8 padding1; +__u16 padding2; +__u32 padding3; +}; +struct kvmi_control_events {__u16 event_id;__u8 enable;
virt/kvm/kvmi.c +65 -0
99 lines not on this page · raw message
virt/kvm/kvmi_int.h +4 -0
@@ -100,6 +100,8 @@ struct kvmi_vcpu {bool rep_complete;bool effective_rep_complete; +atomic_t pause_requests; +bool reply_waiting;struct kvmi_vcpu_reply reply;
@@ -164,6 +166,7 @@ u32 kvmi_msg_send_pf(struct kvm_vcpu *vcpu, u64 gpa, u64 gva, u8 access, bool *singlestep, bool *rep_complete, u64 *ctx_addr, u8 *ctx, u32 *ctx_size);u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu); +u32 kvmi_msg_send_pause_vcpu (struct kvm_vcpu *vcpu);int kvmi_msg_send_unhook (struct kvmi *ikvm);/* kvmi.c */
@@ -185,6 +188,7 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id, bool enable);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable); +int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu,bool wait);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu); int kvmi_add_job(struct kvm_vcpu *vcpu, void (*fct)(struct kvm_vcpu *vcpu, void *ctx),
virt/kvm/kvmi_msg.c +61 -0
82 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 50/92 kvm: introspection: add KVMI_GET_REGISTERS
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This command is used to get kvm_regs and kvm_sregs structures, plus the list of struct kvm_msrs.
--- Documentation/virtual/kvm/kvmi.rst | 43 ++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 15 ++++++ arch/x86/kvm/kvmi.c | 78 ++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 5 ++ virt/kvm/kvmi_msg.c | 17 +++++++ 5 files changed, 158 insertions(+)
Documentation/virtual/kvm/kvmi.rst +43 -0
@@ -862,6 +862,49 @@ The introspection tool should use *KVMI_CONTROL_VM_EVENTS* to enable the * -KVM_EBUSY - the selected vCPU has too many queued *KVMI_EVENT_PAUSE_VCPU* events * -KVM_EPERM - the *KVMI_EVENT_PAUSE_VCPU* event is disallowed (see *KVMI_CONTROL_EVENTS*) and the introspection tool expects a reply. + +17. KVMI_GET_REGISTERS +---------------------- + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_get_registers { + __u16 nmsrs; + __u16 padding1; + __u32 padding2; + __u32 msrs_idx[0]; + }; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_registers_reply { + __u32 mode; + __u32 padding; + struct kvm_regs regs; + struct kvm_sregs sregs; + struct kvm_msrs msrs; + }; + +For the given vCPU and the ``nmsrs`` sized array of MSRs registers, +returns the current vCPU mode (in bytes: 2, 4 or 8), the general purpose +registers, the special registers and the requested set of MSRs. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - one of the indicated MSR-s is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to allocate the reply + Events ======
arch/x86/include/uapi/asm/kvmi.h +15 -0
@@ -26,4 +26,19 @@ struct kvmi_event_arch { } msrs; }; +struct kvmi_get_registers { +__u16 nmsrs; +__u16 padding1; +__u32 padding2; +__u32 msrs_idx[0 ]; +}; + +struct kvmi_get_registers_reply { +__u32 mode; +__u32 padding; +struct kvm_regs regs; +struct kvm_sregs sregs; +struct kvm_msrs msrs; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +78 -0
90 lines not on this page · raw message
virt/kvm/kvmi_int.h +5 -0
@@ -200,6 +200,11 @@ void kvmi_handle_common_event_actions(struct kvm_vcpu *vcpu, u32 action,void kvmi_arch_update_page_tracking (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvmi_mem_access *m); +int kvmi_arch_cmd_get_registers (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const struct kvmi_get_registers *req, +struct kvmi_get_registers_reply **dest, +size_t *dest_size); int kvmi_arch_cmd_get_page_access(struct kvmi *ikvm, const struct kvmi_msg_hdr *msg, const struct kvmi_get_page_access *req,
virt/kvm/kvmi_msg.c +17 -0
@@ -32,6 +32,7 @@ static const char *const msg_IDs[] = { [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP", + [KVMI_GET_REGISTERS] = "KVMI_GET_REGISTERS", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", [KVMI_PAUSE_VCPU] = "KVMI_PAUSE_VCPU",
@@ -589,6 +590,21 @@ static int handle_get_vcpu_info(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg,0 , &rpl,sizeof (rpl)); } +static int handle_get_registers (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *req,vcpu_reply_fct reply_cb) +{ +struct kvmi_get_registers_reply *rpl =NULL ; +size_t rpl_size =0 ; +int err, ec; + + ec =kvmi_arch_cmd_get_registers (vcpu, msg, req, &rpl, &rpl_size); + + err =reply_cb (vcpu, msg, ec, rpl, rpl_size); +kvmi_msg_free (rpl); +return err; +} + static int handle_control_events(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, const void *_req,
@@ -622,6 +638,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, vcpu_reply_fct) = { [KVMI_CONTROL_EVENTS] = handle_control_events, [KVMI_EVENT_REPLY] = handle_event_reply, + [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 51/92 kvm: introspection: add KVMI_SET_REGISTERS
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This command is allowed only during a vCPU event (an event has been sent and the vCPU is waiting for the reply). The registers will be set only when the reply has been received.
--- Documentation/virtual/kvm/kvmi.rst | 28 +++++++++++++++++++++++++ arch/x86/kvm/x86.c | 33 ++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 1 + virt/kvm/kvmi.c | 25 ++++++++++++++++++++++ virt/kvm/kvmi_int.h | 5 +++++ virt/kvm/kvmi_msg.c | 16 +++++++++++++++ 6 files changed, 108 insertions(+)
Documentation/virtual/kvm/kvmi.rst +28 -0
@@ -905,6 +905,34 @@ registers, the special registers and the requested set of MSRs. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to allocate the reply +18. KVMI_SET_REGISTERS +---------------------- + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvm_regs; + +:Returns: + +:: + + struct kvmi_error_code + +Sets the general purpose registers for the given vCPU. The changes become +visible to other threads accessing the KVM vCPU structure after the event +currently being handled is replied to. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
arch/x86/kvm/x86.c +33 -0
@@ -8431,6 +8431,39 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return 0 ; } +/* +* Similar to __set_regs() but it does not reset the exceptions +*/ +void kvm_arch_vcpu_set_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs) +{ + vcpu->arch.emulate_regs_need_sync_from_vcpu = true; + vcpu->arch.emulate_regs_need_sync_to_vcpu = false; + +kvm_register_write (vcpu,VCPU_REGS_RAX , regs->rax); +kvm_register_write (vcpu,VCPU_REGS_RBX , regs->rbx); +kvm_register_write (vcpu,VCPU_REGS_RCX , regs->rcx); +kvm_register_write (vcpu,VCPU_REGS_RDX , regs->rdx); +kvm_register_write (vcpu,VCPU_REGS_RSI , regs->rsi); +kvm_register_write (vcpu,VCPU_REGS_RDI , regs->rdi); +kvm_register_write (vcpu,VCPU_REGS_RSP , regs->rsp); +kvm_register_write (vcpu,VCPU_REGS_RBP , regs->rbp); +#ifdef CONFIG_X86_64 +kvm_register_write (vcpu,VCPU_REGS_R8 , regs->r8); +kvm_register_write (vcpu,VCPU_REGS_R9 , regs->r9); +kvm_register_write (vcpu,VCPU_REGS_R10 , regs->r10); +kvm_register_write (vcpu,VCPU_REGS_R11 , regs->r11); +kvm_register_write (vcpu,VCPU_REGS_R12 , regs->r12); +kvm_register_write (vcpu,VCPU_REGS_R13 , regs->r13); +kvm_register_write (vcpu,VCPU_REGS_R14 , regs->r14); +kvm_register_write (vcpu,VCPU_REGS_R15 , regs->r15); +#endif + +kvm_rip_write (vcpu, regs->rip); +kvm_set_rflags (vcpu, regs->rflags |X86_EFLAGS_FIXED ); + +kvm_make_request (KVM_REQ_EVENT , vcpu); +} +void kvm_get_cs_db_l_bits (struct kvm_vcpu *vcpu,int *db,int *l) {struct kvm_segment cs;
include/linux/kvm_host.h +1 -0
@@ -791,6 +791,7 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs);void kvm_arch_vcpu_get_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs);int kvm_arch_vcpu_ioctl_set_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs); +void kvm_arch_vcpu_set_regs (struct kvm_vcpu *vcpu,struct kvm_regs *regs);int kvm_arch_vcpu_ioctl_get_sregs (struct kvm_vcpu *vcpu,struct kvm_sregs *sregs); void kvm_arch_vcpu_get_sregs(struct kvm_vcpu *vcpu,
virt/kvm/kvmi.c +25 -0
@@ -1212,6 +1212,31 @@ void kvmi_handle_requests(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +void kvmi_post_reply (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (ivcpu->have_delayed_regs) { +kvm_arch_vcpu_set_regs (vcpu, &ivcpu->delayed_regs); + ivcpu->have_delayed_regs = false; + } +} + +int kvmi_cmd_set_registers (struct kvm_vcpu *vcpu,const struct kvm_regs *regs) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (ivcpu->reply_waiting) { +/* defer set registers until we get the reply */ +memcpy (&ivcpu->delayed_regs, regs,sizeof (ivcpu->delayed_regs)); + ivcpu->have_delayed_regs = true; + }else { +kvmi_err (IKVM (vcpu->kvm),"Dropped KVMI_SET_REGISTERS\n" ); + } + +return 0 ; +} +int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access) {gfn_t gfn =gpa_to_gfn (gpa);
virt/kvm/kvmi_int.h +5 -0
@@ -105,6 +105,9 @@ struct kvmi_vcpu {bool reply_waiting;struct kvmi_vcpu_reply reply; +bool have_delayed_regs; +struct kvm_regs delayed_regs; +DECLARE_BITMAP (ev_mask,KVMI_NUM_EVENTS );struct list_head job_list;
@@ -173,6 +176,7 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm);void *kvmi_msg_alloc (void );void *kvmi_msg_alloc_check (size_t size);void kvmi_msg_free (void *addr); +int kvmi_cmd_set_registers (struct kvm_vcpu *vcpu,const struct kvm_regs *regs);int kvmi_cmd_get_page_access (struct kvmi *ikvm,u64 gpa,u8 *access);int kvmi_cmd_set_page_access (struct kvmi *ikvm,u64 gpa,u8 access);int kvmi_cmd_get_page_write_bitmap (struct kvmi *ikvm,u64 gpa,u32 *bitmap);
@@ -190,6 +194,7 @@ int kvmi_cmd_control_vm_events(struct kvmi *ikvm, unsigned int event_id, bool enable);int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu,bool wait);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu); +void kvmi_post_reply (struct kvm_vcpu *vcpu);int kvmi_add_job (struct kvm_vcpu *vcpu,void (*fct)(struct kvm_vcpu *vcpu,void *ctx),void *ctx,void (*free_fct)(void *ctx));
virt/kvm/kvmi_msg.c +16 -0
@@ -39,6 +39,7 @@ static const char *const msg_IDs[] = { [KVMI_READ_PHYSICAL] = "KVMI_READ_PHYSICAL", [KVMI_SET_PAGE_ACCESS] = "KVMI_SET_PAGE_ACCESS", [KVMI_SET_PAGE_WRITE_BITMAP] = "KVMI_SET_PAGE_WRITE_BITMAP", + [KVMI_SET_REGISTERS] = "KVMI_SET_REGISTERS", [KVMI_WRITE_PHYSICAL] = "KVMI_WRITE_PHYSICAL", };
@@ -605,6 +606,19 @@ static int handle_get_registers(struct kvm_vcpu *vcpu,return err; } +static int handle_set_registers (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *_req, +vcpu_reply_fct reply_cb) +{ +const struct kvm_regs *regs = _req; +int err; + + err =kvmi_cmd_set_registers (vcpu, regs); + +return reply_cb (vcpu, msg, err,NULL ,0 ); +} + static int handle_control_events(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, const void *_req,
@@ -640,6 +654,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, [KVMI_EVENT_REPLY] = handle_event_reply, [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, + [KVMI_SET_REGISTERS] = handle_set_registers, };static void kvmi_job_vcpu_cmd (struct kvm_vcpu *vcpu,void *_ctx)
@@ -937,6 +952,7 @@ int kvmi_send_event(struct kvm_vcpu *vcpu, u32 ev_id,if (err) goto out; +kvmi_post_reply (vcpu); *action = ivcpu->reply.action; out:
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 52/92 kvm: introspection: add KVMI_GET_CPUID
In reply to Adalbert LazărFrom: Marian Rotariu <[email protected]>
This command returns a CPUID leaf (as seen by the guest OS).
--- Documentation/virtual/kvm/kvmi.rst | 36 ++++++++++++++++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 12 ++++++++++ arch/x86/kvm/kvmi.c | 19 ++++++++++++++++ include/uapi/linux/kvm_para.h | 1 + virt/kvm/kvmi_int.h | 3 +++ virt/kvm/kvmi_msg.c | 16 +++++++++++++ 6 files changed, 87 insertions(+)
Documentation/virtual/kvm/kvmi.rst +36 -0
@@ -933,6 +933,42 @@ currently being handled is replied to. * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +19. KVMI_GET_CPUID +------------------ + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_get_cpuid { + __u32 function; + __u32 index; + }; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_cpuid_reply { + __u32 eax; + __u32 ebx; + __u32 ecx; + __u32 edx; + }; + +Returns a CPUID leaf (as seen by the guest OS). + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOENT - the selected leaf is not present or is invalid + Events ======
arch/x86/include/uapi/asm/kvmi.h +12 -0
@@ -41,4 +41,16 @@ struct kvmi_get_registers_reply { struct kvm_msrs msrs; }; +struct kvmi_get_cpuid { +__u32 function; +__u32 index; +}; + +struct kvmi_get_cpuid_reply { +__u32 eax; +__u32 ebx; +__u32 ecx; +__u32 edx; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +19 -0
@@ -5,6 +5,7 @@* Copyright (C) 2019 Bitdefender S.R.L. */ #include "x86.h" +#include "cpuid.h" #include "../../../virt/kvm/kvmi_int.h" static void *alloc_get_registers_reply(const struct kvmi_msg_hdr *msg,
@@ -211,6 +212,24 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,return ret; } +int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu, +const struct kvmi_get_cpuid *req, +struct kvmi_get_cpuid_reply *rpl) +{ +struct kvm_cpuid_entry2 *e; + + e =kvm_find_cpuid_entry (vcpu, req->function, req->index); +if (!e) +return -KVM_ENOENT ; + + rpl->eax = e->eax; + rpl->ebx = e->ebx; + rpl->ecx = e->ecx; + rpl->edx = e->edx; + +return 0 ; +} +int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu,struct kvmi_get_vcpu_info_reply *rpl) {
include/uapi/linux/kvm_para.h +1 -0
@@ -19,6 +19,7 @@#define KVM_EOPNOTSUPP 95#define KVM_EAGAIN 11#define KVM_EBUSY EBUSY +#define KVM_ENOENT ENOENT#define KVM_ENOMEM ENOMEM#define KVM_HC_VAPIC_POLL_IRQ 1
virt/kvm/kvmi_int.h +3 -0
@@ -230,6 +230,9 @@ int kvmi_arch_cmd_set_page_write_bitmap(struct kvmi *ikvm,void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access); +int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu, +const struct kvmi_get_cpuid *req, +struct kvmi_get_cpuid_reply *rpl);int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu,struct kvmi_get_vcpu_info_reply *rpl);
virt/kvm/kvmi_msg.c +16 -0
@@ -29,6 +29,7 @@ static const char *const msg_IDs[] = { [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS", [KVMI_EVENT] = "KVMI_EVENT", [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", + [KVMI_GET_CPUID] = "KVMI_GET_CPUID", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP",
@@ -641,6 +642,20 @@ static int handle_control_events(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, ec,NULL ,0 ); } +static int handle_get_cpuid (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *req,vcpu_reply_fct reply_cb) +{ +struct kvmi_get_cpuid_reply rpl; +int ec; + +memset (&rpl,0 ,sizeof (rpl)); + + ec =kvmi_arch_cmd_get_cpuid (vcpu, req, &rpl); + +return reply_cb (vcpu, msg, ec, &rpl,sizeof (rpl)); +} +/* * These commands are executed on the vCPU thread. The receiving thread * passes the messages using a newly allocated 'struct kvmi_vcpu_cmd'
@@ -652,6 +667,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, vcpu_reply_fct) = { [KVMI_CONTROL_EVENTS] = handle_control_events, [KVMI_EVENT_REPLY] = handle_event_reply, + [KVMI_GET_CPUID] = handle_get_cpuid, [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, [KVMI_SET_REGISTERS] = handle_set_registers,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 53/92 kvm: introspection: add KVMI_INJECT_EXCEPTION + KVMI_EVENT_TRAP
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
The KVMI_INJECT_EXCEPTION command is used by the introspection tool to inject exceptions (eg. get a page from swap). The exception is queued right before entering the guest. If there is already an event pending (exception, interrupt or NMI) we notify the introspection tool with the KVMI_EVENT_TRAP event and abort the injection. The introspecion tool is expected to try again at a later time.
--- Documentation/virtual/kvm/kvmi.rst | 71 +++++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 8 +++ arch/x86/kvm/kvmi.c | 108 +++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 11 +++ include/linux/kvmi.h | 4 ++ include/uapi/linux/kvmi.h | 8 +++ virt/kvm/kvmi.c | 40 +++++++++++ virt/kvm/kvmi_int.h | 16 +++++ virt/kvm/kvmi_msg.c | 21 ++++++ 9 files changed, 287 insertions(+)
Documentation/virtual/kvm/kvmi.rst +71 -0
@@ -969,6 +969,44 @@ Returns a CPUID leaf (as seen by the guest OS). * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOENT - the selected leaf is not present or is invalid +20. KVMI_INJECT_EXCEPTION +------------------------- + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_inject_exception { + __u8 nr; + __u8 has_error; + __u16 padding; + __u32 error_code; + __u64 address; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Injects a vCPU exception with or without an error code. In case of page fault +exception, the guest virtual address has to be specified. + +The introspection tool should enable the *KVMI_EVENT_TRAP* event in +order to be notified if the expection was not delivered. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - the specified exception number is invalid +* -KVM_EINVAL - the specified address is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
@@ -1167,3 +1205,36 @@ cannot be disabled via *KVMI_CONTROL_EVENTS*. This event has a low priority. It will be sent after any other vCPU introspection event and when no vCPU introspection command is queued. +5. KVMI_EVENT_TRAP +------------------ + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_trap { + __u32 vector; + __u32 type; + __u32 error_code; + __u32 padding; + __u64 cr2; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent if a previous *KVMI_INJECT_EXCEPTION* command has +been overwritten by an interrupt picked up during guest reentry and the +introspection has been enabled for this event (see *KVMI_CONTROL_EVENTS*). + +``kvmi_event``, exception/interrupt number (vector), exception/interrupt +type, exception code (``error_code``) and CR2 are sent to the introspector. +
arch/x86/include/uapi/asm/kvmi.h +8 -0
@@ -26,6 +26,14 @@ struct kvmi_event_arch { } msrs; }; +struct kvmi_event_trap { +__u32 vector; +__u32 type; +__u32 error_code; +__u32 padding; +__u64 cr2; +}; +struct kvmi_get_registers {__u16 nmsrs;__u16 padding1;
arch/x86/kvm/kvmi.c +108 -0
126 lines not on this page · raw message
arch/x86/kvm/x86.c +11 -0
@@ -7930,6 +7930,17 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) } } +if (!kvmi_queue_exception (vcpu)) +kvmi_trap_event (vcpu); +else if (vcpu->arch.exception.pending) { + kvm_x86_ops->queue_exception (vcpu); +if (exception_type (vcpu->arch.exception.nr) ==EXCPT_FAULT ) +__kvm_set_rflags (vcpu,kvm_get_rflags (vcpu) | +X86_EFLAGS_RF ); + vcpu->arch.exception.pending = false; + vcpu->arch.exception.injected = true; + } + r =kvm_mmu_reload (vcpu);if (unlikely (r)) { goto cancel_injection;
include/linux/kvmi.h +4 -0
@@ -16,6 +16,8 @@ int kvmi_ioctl_event(struct kvm *kvm, void __user *argp);int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset);int kvmi_vcpu_init (struct kvm_vcpu *vcpu);void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu); +bool kvmi_queue_exception (struct kvm_vcpu *vcpu); +void kvmi_trap_event (struct kvm_vcpu *vcpu);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);
@@ -29,6 +31,8 @@ static inline void kvmi_destroy_vm(struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; } +static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { }
include/uapi/linux/kvmi.h +8 -0
@@ -215,6 +215,14 @@ struct kvmi_vcpu_hdr {__u32 padding2; }; +struct kvmi_inject_exception { +__u8 nr; +__u8 has_error; +__u16 padding; +__u32 error_code; +__u64 address; +}; +struct kvmi_event {__u16 size;__u16 vcpu;
virt/kvm/kvmi.c +40 -0
@@ -1058,6 +1058,46 @@ void kvmi_activate_rep_complete(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL (kvmi_activate_rep_complete); +/* +* This function returns false if there is an exception or interrupt pending. +* It returns true in all other cases including KVMI not being initialized. +*/ +bool kvmi_queue_exception (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm; +bool ret = true; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return true; + +if (!IVCPU (vcpu)->exception.pending) + goto out; + + ret =kvmi_arch_queue_exception (vcpu); + +memset (&IVCPU (vcpu)->exception,0 ,sizeof (IVCPU (vcpu)->exception)); + +out: +kvmi_put (vcpu->kvm); + +return ret; +} + +void kvmi_trap_event (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return ; + +if (is_event_enabled (vcpu,KVMI_EVENT_TRAP )) +kvmi_arch_trap_event (vcpu); + +kvmi_put (vcpu->kvm); +} +static bool __kvmi_create_vcpu_event (struct kvm_vcpu *vcpu) {u32 action;
virt/kvm/kvmi_int.h +16 -0
@@ -105,6 +105,14 @@ struct kvmi_vcpu {bool reply_waiting;struct kvmi_vcpu_reply reply; +struct { +u8 nr; +u32 error_code; +bool error_code_valid; +u64 address; +bool pending; + } exception; +bool have_delayed_regs;struct kvm_regs delayed_regs;
@@ -165,6 +173,9 @@ bool kvmi_sock_get(struct kvmi *ikvm, int fd);void kvmi_sock_shutdown (struct kvmi *ikvm);void kvmi_sock_put (struct kvmi *ikvm);bool kvmi_msg_process (struct kvmi *ikvm); +int kvmi_send_event (struct kvm_vcpu *vcpu,u32 ev_id, +void *ev,size_t ev_size, +void *rpl,size_t rpl_size,int *action);u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx,u32 *ctx_size);
@@ -230,10 +241,15 @@ int kvmi_arch_cmd_set_page_write_bitmap(struct kvmi *ikvm,void kvmi_arch_setup_event (struct kvm_vcpu *vcpu,struct kvmi_event *ev);bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access); +bool kvmi_arch_queue_exception (struct kvm_vcpu *vcpu); +void kvmi_arch_trap_event (struct kvm_vcpu *vcpu);int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_get_cpuid *req,struct kvmi_get_cpuid_reply *rpl);int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu,struct kvmi_get_vcpu_info_reply *rpl); +int kvmi_arch_cmd_inject_exception (struct kvm_vcpu *vcpu,u8 vector, +bool error_code_valid,u32 error_code, +u64 address);#endif
virt/kvm/kvmi_msg.c +21 -0
@@ -36,6 +36,7 @@ static const char *const msg_IDs[] = { [KVMI_GET_REGISTERS] = "KVMI_GET_REGISTERS", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", + [KVMI_INJECT_EXCEPTION] = "KVMI_INJECT_EXCEPTION", [KVMI_PAUSE_VCPU] = "KVMI_PAUSE_VCPU", [KVMI_READ_PHYSICAL] = "KVMI_READ_PHYSICAL", [KVMI_SET_PAGE_ACCESS] = "KVMI_SET_PAGE_ACCESS",
@@ -620,6 +621,25 @@ static int handle_set_registers(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, err,NULL ,0 ); } +static int handle_inject_exception (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *_req, +vcpu_reply_fct reply_cb) +{ +const struct kvmi_inject_exception *req = _req; +int ec; + +if (req->padding) + ec = -KVM_EINVAL ; +else + ec =kvmi_arch_cmd_inject_exception (vcpu, req->nr, + req->has_error, + req->error_code, + req->address); + +return reply_cb (vcpu, msg, ec,NULL ,0 ); +} + static int handle_control_events(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, const void *_req,
@@ -670,6 +690,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, [KVMI_GET_CPUID] = handle_get_cpuid, [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, + [KVMI_INJECT_EXCEPTION] = handle_inject_exception, [KVMI_SET_REGISTERS] = handle_set_registers, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 54/92 kvm: introspection: add KVMI_CONTROL_CR and KVMI_EVENT_CR
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
Using the KVMI_CONTROL_CR command, the introspection tool subscribes to KVMI_EVENT_CR events that will be sent when CR{0,3,4} is going to be changed.
--- Documentation/virtual/kvm/kvmi.rst | 70 ++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 + arch/x86/include/asm/kvmi_host.h | 16 +++++ arch/x86/include/uapi/asm/kvmi.h | 18 ++++++ arch/x86/kvm/kvmi.c | 95 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 14 +++++ arch/x86/kvm/x86.c | 19 +++++- virt/kvm/kvmi_int.h | 7 +++ virt/kvm/kvmi_msg.c | 13 ++++ 10 files changed, 258 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/kvmi.rst +70 -0
@@ -1007,6 +1007,41 @@ order to be notified if the expection was not delivered. * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +21. KVMI_CONTROL_CR +------------------- + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_control_cr { + __u8 enable; + __u8 padding1; + __u16 padding2; + __u32 cr; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Enables/disables introspection for a specific control register and must +be used in addition to *KVMI_CONTROL_EVENTS* with the *KVMI_EVENT_CR* +ID set. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - the specified control register is not part of the CR0, CR3 + or CR4 set +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
@@ -1238,3 +1273,38 @@ introspection has been enabled for this event (see *KVMI_CONTROL_EVENTS*). ``kvmi_event``, exception/interrupt number (vector), exception/interrupt type, exception code (``error_code``) and CR2 are sent to the introspector. +6. KVMI_EVENT_CR +---------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_cr { + __u16 cr; + __u16 padding[3]; + __u64 old_value; + __u64 new_value; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + struct kvmi_event_cr_reply { + __u64 new_val; + }; + +This event is sent when a control register is going to be changed and the +introspection has been enabled for this event and for this specific +register (see **KVMI_CONTROL_EVENTS**). + +``kvmi_event``, the control register number, the old value and the new value +are sent to the introspector. The *CONTINUE* action will set the ``new_val``. +
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1013,6 +1013,7 @@ struct kvm_x86_ops {bool (*has_emulated_msr)(int index);void (*cpuid_update)(struct kvm_vcpu *vcpu); +void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
@@ -1622,5 +1623,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu); +void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/include/asm/kvmi_host.h +16 -0
@@ -9,4 +9,20 @@ struct kvmi_arch_mem_access { unsigned long active[KVM_PAGE_TRACK_MAX][BITS_TO_LONGS(KVM_MEM_SLOTS_NUM)]; }; +#ifdef CONFIG_KVM_INTROSPECTION + +bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr, +unsigned long old_value,unsigned long *new_value); + +#else /* CONFIG_KVM_INTROSPECTION */ + +static inline bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr, +unsigned long old_value, +unsigned long *new_value) +{ +return true; +} + +#endif /* CONFIG_KVM_INTROSPECTION */ +#endif /* _ASM_X86_KVMI_HOST_H */
arch/x86/include/uapi/asm/kvmi.h +18 -0
@@ -61,4 +61,22 @@ struct kvmi_get_cpuid_reply { __u32 edx; }; +struct kvmi_control_cr { +__u8 enable; +__u8 padding1; +__u16 padding2; +__u32 cr; +}; + +struct kvmi_event_cr { +__u16 cr; +__u16 padding[3 ]; +__u64 old_value; +__u64 new_value; +}; + +struct kvmi_event_cr_reply { +__u64 new_val; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +95 -0
107 lines not on this page · raw message
arch/x86/kvm/svm.c +5 -0
@@ -7110,6 +7110,10 @@ static bool svm_spt_fault(struct kvm_vcpu *vcpu)return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); } +static void svm_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) +{ +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -7121,6 +7125,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .cr3_write_exiting = svm_cr3_write_exiting, .nested_pagefault = svm_nested_pagefault, .spt_fault = svm_spt_fault,
arch/x86/kvm/vmx/vmx.c +14 -0
@@ -7784,6 +7784,19 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_cr3_write_exiting (struct kvm_vcpu *vcpu, +bool enable) +{ +if (enable) +vmcs_set_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_CR3_LOAD_EXITING ); +else +vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_CR3_LOAD_EXITING ); + +/* TODO: nested ? vmcs12->cpu_based_vm_exec_control */ +} +static bool vmx_nested_pagefault (struct kvm_vcpu *vcpu) {if (vcpu->arch.exit_qualification &EPT_VIOLATION_GVA_TRANSLATED )
@@ -7831,6 +7844,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .cr3_write_exiting = vmx_cr3_write_exiting, .nested_pagefault = vmx_nested_pagefault, .spt_fault = vmx_spt_fault,
arch/x86/kvm/x86.c +18 -1
@@ -22,6 +22,7 @@#include <linux/kvm_host.h> #include <uapi/linux/kvmi.h> #include <linux/kvmi.h> +#include <asm/kvmi_host.h> #include "irq.h" #include "mmu.h" #include "i8254.h"
@@ -777,6 +778,9 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE ))return 1 ; +if (!kvmi_cr_event (vcpu,0 , old_cr0, &cr0)) +return 1 ; + kvm_x86_ops->set_cr0 (vcpu, cr0);if ((cr0 ^ old_cr0) &X86_CR0_PG ) {
@@ -921,6 +925,9 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } +if (!kvmi_cr_event (vcpu,4 , old_cr4, &cr4)) +return 1 ; +if (kvm_x86_ops->set_cr4 (vcpu, cr4))return 1 ;
@@ -937,6 +944,7 @@ EXPORT_SYMBOL_GPL(kvm_set_cr4);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3) { +unsigned long old_cr3 =kvm_read_cr3 (vcpu);bool skip_tlb_flush = false;#ifdef CONFIG_X86_64 bool pcid_enabled =kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE );
@@ -947,7 +955,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) }#endif -if (cr3 ==kvm_read_cr3 (vcpu) && !pdptrs_changed (vcpu)) { +if (cr3 == old_cr3 && !pdptrs_changed (vcpu)) {if (!skip_tlb_flush) {kvm_mmu_sync_roots (vcpu);kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu);
@@ -962,6 +970,9 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))return 1 ; +if (!kvmi_cr_event (vcpu,3 , old_cr3, &cr3)) +return 1 ; +kvm_mmu_new_cr3 (vcpu, cr3, skip_tlb_flush); vcpu->arch.cr3 = cr3;__set_bit (VCPU_EXREG_CR3 , (ulong *)&vcpu->arch.regs_avail);
@@ -10072,6 +10083,12 @@ bool kvm_vector_hashing_enabled(void) }EXPORT_SYMBOL_GPL (kvm_vector_hashing_enabled); +void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) +{ + kvm_x86_ops->cr3_write_exiting (vcpu, enable); +} +EXPORT_SYMBOL (kvm_control_cr3_write_exiting); +bool kvm_spt_fault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->spt_fault (vcpu);
virt/kvm/kvmi_int.h +7 -0
@@ -26,6 +26,8 @@#define IVCPU (vcpu) ((struct kvmi_vcpu *)((vcpu)->kvmi)) +#define KVMI_NUM_CR 9 +#define KVMI_CTX_DATA_SIZE FIELD_SIZEOF(struct kvmi_event_pf_reply, ctx_data)#define KVMI_MSG_SIZE_ALLOC (sizeof(struct kvmi_msg_hdr) + KVMI_MSG_SIZE)
@@ -117,6 +119,7 @@ struct kvmi_vcpu {struct kvm_regs delayed_regs;DECLARE_BITMAP (ev_mask,KVMI_NUM_EVENTS ); +DECLARE_BITMAP (cr_mask,KVMI_NUM_CR );struct list_head job_list;spinlock_t job_lock;
@@ -204,6 +207,8 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id,int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable);int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu,bool wait); +struct kvmi *__must_check kvmi_get (struct kvm *kvm); +void kvmi_put (struct kvm *kvm);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu);void kvmi_post_reply (struct kvm_vcpu *vcpu); int kvmi_add_job(struct kvm_vcpu *vcpu,
@@ -251,5 +256,7 @@ int kvmi_arch_cmd_get_vcpu_info(struct kvm_vcpu *vcpu,int kvmi_arch_cmd_inject_exception (struct kvm_vcpu *vcpu,u8 vector,bool error_code_valid,u32 error_code,u64 address); +int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu, +const struct kvmi_control_cr *req);#endif
virt/kvm/kvmi_msg.c +13 -0
@@ -24,6 +24,7 @@ static const char *const msg_IDs[] = { [KVMI_CHECK_COMMAND] = "KVMI_CHECK_COMMAND", [KVMI_CHECK_EVENT] = "KVMI_CHECK_EVENT", [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", + [KVMI_CONTROL_CR] = "KVMI_CONTROL_CR", [KVMI_CONTROL_EVENTS] = "KVMI_CONTROL_EVENTS", [KVMI_CONTROL_SPP] = "KVMI_CONTROL_SPP", [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS",
@@ -662,6 +663,17 @@ static int handle_control_events(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, ec,NULL ,0 ); } +static int handle_control_cr (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg,const void *req, +vcpu_reply_fct reply_cb) +{ +int ec; + + ec =kvmi_arch_cmd_control_cr (vcpu, req); + +return reply_cb (vcpu, msg, ec,NULL ,0 ); +} +static int handle_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_msg_hdr *msg,const void *req,vcpu_reply_fct reply_cb)
@@ -685,6 +697,7 @@ static int handle_get_cpuid(struct kvm_vcpu *vcpu,static int (*const msg_vcpu[])(struct kvm_vcpu *,const struct kvmi_msg_hdr *,const void *,vcpu_reply_fct ) = { + [KVMI_CONTROL_CR ] = handle_control_cr, [KVMI_CONTROL_EVENTS ] = handle_control_events, [KVMI_EVENT_REPLY ] = handle_event_reply, [KVMI_GET_CPUID ] = handle_get_cpuid,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 55/92 kvm: introspection: add KVMI_CONTROL_MSR and KVMI_EVENT_MSR
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
The KVMI_CONTROL_MSR is used to enable/disable introspection for a specific MSR. The KVMI_EVENT_MSR is send when the tracked MSR is going to be changed. The introspection tool can respond by allowing the guest to continue with normal execution or by discarding the change.
This is meant to prevent malicious changes to MSR-s such as MSR_IA32_SYSENTER_EIP.
--- Documentation/virtual/kvm/kvmi.rst | 73 +++++++++++++++++ arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/asm/kvmi_host.h | 6 ++ arch/x86/include/uapi/asm/kvmi.h | 18 ++++ arch/x86/kvm/kvmi.c | 127 +++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 15 ++++ arch/x86/kvm/vmx/vmx.c | 10 +++ arch/x86/kvm/x86.c | 10 +++ virt/kvm/kvmi_int.h | 8 +- virt/kvm/kvmi_msg.c | 13 +++ 10 files changed, 283 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/kvmi.rst +73 -0
@@ -1042,6 +1042,45 @@ ID set. * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +22. KVMI_CONTROL_MSR +-------------------- + +:Architectures: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_control_msr { + __u8 enable; + __u8 padding1; + __u16 padding2; + __u32 msr; + }; + +:Returns: + +:: + + struct kvmi_error_code + +Enables/disables introspection for a specific MSR and must be used +in addition to *KVMI_CONTROL_EVENTS* with the *KVMI_EVENT_MSR* ID set. + +Currently, only MSRs within the following two ranges are supported. Trying +to control events for any other register will fail with -KVM_EINVAL:: + + 0 ... 0x00001fff + 0xc0000000 ... 0xc0001fff + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - the specified MSR is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
@@ -1308,3 +1347,37 @@ register (see **KVMI_CONTROL_EVENTS**). ``kvmi_event``, the control register number, the old value and the new value are sent to the introspector. The *CONTINUE* action will set the ``new_val``. +7. KVMI_EVENT_MSR +----------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_msr { + __u32 msr; + __u32 padding; + __u64 old_value; + __u64 new_value; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + struct kvmi_event_msr_reply { + __u64 new_val; + }; + +This event is sent when a model specific register is going to be changed +and the introspection has been enabled for this event and for this specific +register (see **KVMI_CONTROL_EVENTS**). + +``kvmi_event``, the MSR number, the old value and the new value are +sent to the introspector. The *CONTINUE* action will set the ``new_val``.
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1013,6 +1013,8 @@ struct kvm_x86_ops {bool (*has_emulated_msr)(int index);void (*cpuid_update)(struct kvm_vcpu *vcpu); +void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr, +bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
@@ -1621,6 +1623,8 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)#define put_smstate (type, buf, offset, val) \ *(type *)((buf) + (offset) - 0x7e00) = val +void kvm_arch_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr, +bool enable);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu);void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);
arch/x86/include/asm/kvmi_host.h +6 -0
@@ -11,11 +11,17 @@ struct kvmi_arch_mem_access {#ifdef CONFIG_KVM_INTROSPECTION +bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr);bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value);#else /* CONFIG_KVM_INTROSPECTION */ +static inline bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +return true; +} +static inline bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value)
arch/x86/include/uapi/asm/kvmi.h +18 -0
@@ -79,4 +79,22 @@ struct kvmi_event_cr_reply { __u64 new_val; }; +struct kvmi_control_msr { +__u8 enable; +__u8 padding1; +__u16 padding2; +__u32 msr; +}; + +struct kvmi_event_msr { +__u32 msr; +__u32 padding; +__u64 old_value; +__u64 new_value; +}; + +struct kvmi_event_msr_reply { +__u64 new_val; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +127 -0
133 lines not on this page · raw message
arch/x86/kvm/svm.c +15 -0
@@ -7098,6 +7098,20 @@ static int nested_enable_evmcs(struct kvm_vcpu *vcpu,return -ENODEV ; } +static void svm_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr, +bool enable) +{ +struct vcpu_svm *svm =to_svm (vcpu); +u32 *msrpm = svm->msrpm; + +/* +* The below code enable or disable the msr interception for both +* read and write. The best way will be to get here the current +* bit status for read and send that value as argument. +*/ +set_msr_interception (msrpm, msr, enable, enable); +} +static bool svm_nested_pagefault (struct kvm_vcpu *vcpu) {return false;
@@ -7126,6 +7140,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .has_emulated_msr = svm_has_emulated_msr, .cr3_write_exiting = svm_cr3_write_exiting, + .msr_intercept = svm_msr_intercept, .nested_pagefault = svm_nested_pagefault, .spt_fault = svm_spt_fault,
arch/x86/kvm/vmx/vmx.c +10 -0
@@ -7784,6 +7784,15 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr, +bool enable) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); +unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; + +vmx_set_intercept_for_msr (msr_bitmap, msr,MSR_TYPE_W , enable); +} +static void vmx_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) {
@@ -7844,6 +7853,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .nested_pagefault = vmx_nested_pagefault, .spt_fault = vmx_spt_fault,
arch/x86/kvm/x86.c +10 -0
@@ -1319,6 +1319,9 @@ EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);*/ int kvm_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr) { +if (!kvmi_msr_event (vcpu, msr)) +return 1 ; +switch (msr->index) {case MSR_FS_BASE :case MSR_GS_BASE :
@@ -10083,6 +10086,13 @@ bool kvm_vector_hashing_enabled(void) }EXPORT_SYMBOL_GPL (kvm_vector_hashing_enabled); +void kvm_arch_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr, +bool enable) +{ + kvm_x86_ops->msr_intercept (vcpu, msr, enable); +} +EXPORT_SYMBOL_GPL (kvm_arch_msr_intercept); +void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) { kvm_x86_ops->cr3_write_exiting (vcpu, enable);
virt/kvm/kvmi_int.h +7 -1
@@ -27,7 +27,7 @@#define IVCPU (vcpu) ((struct kvmi_vcpu *)((vcpu)->kvmi))#define KVMI_NUM_CR 9 - +#define KVMI_NUM_MSR 0x2000#define KVMI_CTX_DATA_SIZE FIELD_SIZEOF(struct kvmi_event_pf_reply, ctx_data)#define KVMI_MSG_SIZE_ALLOC (sizeof(struct kvmi_msg_hdr) + KVMI_MSG_SIZE)
@@ -120,6 +120,10 @@ struct kvmi_vcpu {DECLARE_BITMAP (ev_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (cr_mask,KVMI_NUM_CR ); +struct { + DECLARE_BITMAP(low, KVMI_NUM_MSR); + DECLARE_BITMAP(high, KVMI_NUM_MSR); + } msr_mask;struct list_head job_list;spinlock_t job_lock;
@@ -258,5 +262,7 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu, +const struct kvmi_control_msr *req);#endif
virt/kvm/kvmi_msg.c +13 -0
@@ -26,6 +26,7 @@ static const char *const msg_IDs[] = { [KVMI_CONTROL_CMD_RESPONSE] = "KVMI_CONTROL_CMD_RESPONSE", [KVMI_CONTROL_CR] = "KVMI_CONTROL_CR", [KVMI_CONTROL_EVENTS] = "KVMI_CONTROL_EVENTS", + [KVMI_CONTROL_MSR] = "KVMI_CONTROL_MSR", [KVMI_CONTROL_SPP] = "KVMI_CONTROL_SPP", [KVMI_CONTROL_VM_EVENTS] = "KVMI_CONTROL_VM_EVENTS", [KVMI_EVENT] = "KVMI_EVENT",
@@ -674,6 +675,17 @@ static int handle_control_cr(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, ec,NULL ,0 ); } +static int handle_control_msr (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg,const void *req, +vcpu_reply_fct reply_cb) +{ +int ec; + + ec =kvmi_arch_cmd_control_msr (vcpu, req); + +return reply_cb (vcpu, msg, ec,NULL ,0 ); +} +static int handle_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_msg_hdr *msg,const void *req,vcpu_reply_fct reply_cb)
@@ -699,6 +711,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, vcpu_reply_fct) = { [KVMI_CONTROL_CR] = handle_control_cr, [KVMI_CONTROL_EVENTS] = handle_control_events, + [KVMI_CONTROL_MSR] = handle_control_msr, [KVMI_EVENT_REPLY] = handle_event_reply, [KVMI_GET_CPUID] = handle_get_cpuid, [KVMI_GET_REGISTERS] = handle_get_registers,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 56/92 kvm: x86: block any attempt to disable MSR interception if tracked by introspection
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
Intercept all calls that might disable the MSR interception (writes) and do nothing if that specific MSR is currently tracked by the introspection tool.
--- arch/x86/include/asm/kvmi_host.h | 6 +++ arch/x86/kvm/kvmi.c | 25 +++++++++++++ arch/x86/kvm/svm.c | 33 ++++++++++------- arch/x86/kvm/vmx/vmx.c | 63 +++++++++++++++++++------------- 4 files changed, 88 insertions(+), 39 deletions(-)
arch/x86/include/asm/kvmi_host.h +6 -0
@@ -12,6 +12,7 @@ struct kvmi_arch_mem_access {#ifdef CONFIG_KVM_INTROSPECTION bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr); +bool kvmi_monitored_msr (struct kvm_vcpu *vcpu,u32 msr);bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value);
@@ -22,6 +23,11 @@ static inline bool kvmi_msr_event(struct kvm_vcpu *vcpu, struct msr_data *msr)return true; } +static inline bool kvmi_monitored_msr (struct kvm_vcpu *vcpu,u32 msr) +{ +return false; +} +static inline bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value)
arch/x86/kvm/kvmi.c +25 -0
@@ -136,6 +136,31 @@ bool kvmi_msr_event(struct kvm_vcpu *vcpu, struct msr_data *msr)return ret; } +bool kvmi_monitored_msr (struct kvm_vcpu *vcpu,u32 msr) +{ +struct kvmi *ikvm; +bool ret = false; + +if (!vcpu) +return false; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + +if (test_msr_mask (vcpu, msr)) { +kvmi_warn_once (ikvm, +"Trying to disable write interception for MSR %x\n" , + msr); + ret = true; + } + +kvmi_put (vcpu->kvm); + +return ret; +} +EXPORT_SYMBOL (kvmi_monitored_msr); +static void *alloc_get_registers_reply (const struct kvmi_msg_hdr *msg,const struct kvmi_get_registers *req,size_t *rpl_size)
arch/x86/kvm/svm.c +20 -13
@@ -18,6 +18,7 @@#define pr_fmt (fmt) "SVM: " fmt#include <linux/kvm_host.h> +#include <asm/kvmi_host.h> #include "irq.h" #include "mmu.h"
@@ -1049,13 +1050,19 @@ static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)return !!test_bit (bit_write, &tmp); } -static void set_msr_interception (u32 *msrpm,unsigned msr, +static void set_msr_interception (struct vcpu_svm *svm, +u32 *msrpm,unsigned int msr,int read,int write) {u8 bit_read, bit_write;unsigned long tmp;u32 offset; +#ifdef CONFIG_KVM_INTROSPECTION +if (!write &&kvmi_monitored_msr (&svm->vcpu, msr)) +return ; +#endif /* CONFIG_KVM_INTROSPECTION */ +/* * If this warning triggers extend the direct_access_msrs list at the * beginning of the file
@@ -1085,7 +1092,7 @@ static void svm_vcpu_init_msrpm(u32 *msrpm)if (!direct_access_msrs[i].always)continue ; -set_msr_interception (msrpm, direct_access_msrs[i].index,1 ,1 ); +set_msr_interception (NULL , msrpm, direct_access_msrs[i].index,1 ,1 ); } }
@@ -1137,10 +1144,10 @@ static void svm_enable_lbrv(struct vcpu_svm *svm)u32 *msrpm = svm->msrpm; svm->vmcb->control.virt_ext |=LBR_CTL_ENABLE_MASK ; -set_msr_interception (msrpm,MSR_IA32_LASTBRANCHFROMIP ,1 ,1 ); -set_msr_interception (msrpm,MSR_IA32_LASTBRANCHTOIP ,1 ,1 ); -set_msr_interception (msrpm,MSR_IA32_LASTINTFROMIP ,1 ,1 ); -set_msr_interception (msrpm,MSR_IA32_LASTINTTOIP ,1 ,1 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTBRANCHFROMIP ,1 ,1 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTBRANCHTOIP ,1 ,1 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTINTFROMIP ,1 ,1 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTINTTOIP ,1 ,1 ); }static void svm_disable_lbrv (struct vcpu_svm *svm)
@@ -1148,10 +1155,10 @@ static void svm_disable_lbrv(struct vcpu_svm *svm)u32 *msrpm = svm->msrpm; svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK ; -set_msr_interception (msrpm,MSR_IA32_LASTBRANCHFROMIP ,0 ,0 ); -set_msr_interception (msrpm,MSR_IA32_LASTBRANCHTOIP ,0 ,0 ); -set_msr_interception (msrpm,MSR_IA32_LASTINTFROMIP ,0 ,0 ); -set_msr_interception (msrpm,MSR_IA32_LASTINTTOIP ,0 ,0 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTBRANCHFROMIP ,0 ,0 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTBRANCHTOIP ,0 ,0 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTINTFROMIP ,0 ,0 ); +set_msr_interception (svm, msrpm,MSR_IA32_LASTINTTOIP ,0 ,0 ); }static void disable_nmi_singlestep (struct vcpu_svm *svm)
@@ -4290,7 +4297,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)* We update the L1 MSR bit as well since it will end up * touching the MSR anyway now. */ -set_msr_interception (svm->msrpm,MSR_IA32_SPEC_CTRL ,1 ,1 ); +set_msr_interception (svm, svm->msrpm,MSR_IA32_SPEC_CTRL ,1 ,1 );break ;case MSR_IA32_PRED_CMD : if (!msr->host_initiated &&
@@ -4306,7 +4313,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)wrmsrl (MSR_IA32_PRED_CMD ,PRED_CMD_IBPB );if (is_guest_mode (vcpu))break ; -set_msr_interception (svm->msrpm,MSR_IA32_PRED_CMD ,0 ,1 ); +set_msr_interception (svm, svm->msrpm,MSR_IA32_PRED_CMD ,0 ,1 );break ;case MSR_AMD64_VIRT_SPEC_CTRL : if (!msr->host_initiated &&
@@ -7109,7 +7116,7 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,* read and write. The best way will be to get here the current * bit status for read and send that value as argument. */ -set_msr_interception (msrpm, msr, enable, enable); +set_msr_interception (svm, msrpm, msr, enable, enable); }static bool svm_nested_pagefault (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +37 -26
157 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 57/92 kvm: introspection: add KVMI_GET_XSAVE
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This vCPU command is used to get the XSAVE area.
--- Documentation/virtual/kvm/kvmi.rst | 31 ++++++++++++++++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 4 ++++ arch/x86/kvm/kvmi.c | 21 ++++++++++++++++++++ arch/x86/kvm/x86.c | 4 ++-- include/linux/kvm_host.h | 2 ++ virt/kvm/kvmi_int.h | 3 +++ virt/kvm/kvmi_msg.c | 17 ++++++++++++++++ 7 files changed, 80 insertions(+), 2 deletions(-)
Documentation/virtual/kvm/kvmi.rst +31 -0
@@ -1081,6 +1081,37 @@ to control events for any other register will fail with -KVM_EINVAL:: * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +23. KVMI_GET_XSAVE +------------------ + +:Architecture: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_xsave_reply { + __u32 region[0]; + }; + +Returns a buffer containing the XSAVE area. Currently, the size of +``kvm_xsave`` is used, but it could change. The userspace should get +the buffer size from the message size. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet +* -KVM_ENOMEM - not enough memory to allocate the reply + Events ======
arch/x86/include/uapi/asm/kvmi.h +4 -0
@@ -97,4 +97,8 @@ struct kvmi_event_msr_reply { __u64 new_val; }; +struct kvmi_get_xsave_reply { +__u32 region[0 ]; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +21 -0
@@ -790,3 +790,24 @@ int kvmi_arch_cmd_control_spp(struct kvmi *ikvm) {return kvm_arch_init_spp (ikvm->kvm); } + +int kvmi_arch_cmd_get_xsave (struct kvm_vcpu *vcpu, +struct kvmi_get_xsave_reply **dest, +size_t *dest_size) +{ +struct kvmi_get_xsave_reply *rpl =NULL ; +size_t rpl_size =sizeof (*rpl) +sizeof (struct kvm_xsave ); +struct kvm_xsave *area; + + rpl =kvmi_msg_alloc_check (rpl_size); +if (!rpl) +return -KVM_ENOMEM ; + + area = (struct kvm_xsave *) &rpl->region[0 ]; +kvm_vcpu_ioctl_x86_get_xsave (vcpu, area); + + *dest = rpl; + *dest_size = rpl_size; + +return 0 ; +}
arch/x86/kvm/x86.c +2 -2
@@ -3745,8 +3745,8 @@ static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) } } -static void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu, -struct kvm_xsave *guest_xsave) +void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu, +struct kvm_xsave *guest_xsave) {if (boot_cpu_has (X86_FEATURE_XSAVE )) {memset (guest_xsave,0 ,sizeof (struct kvm_xsave ));
include/linux/kvm_host.h +2 -0
@@ -805,6 +805,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg);int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run); +void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu, +struct kvm_xsave *guest_xsave);int kvm_arch_init (void *opaque);void kvm_arch_exit (void );
virt/kvm/kvmi_int.h +3 -0
@@ -255,6 +255,9 @@ void kvmi_arch_trap_event(struct kvm_vcpu *vcpu);int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_get_cpuid *req,struct kvmi_get_cpuid_reply *rpl); +int kvmi_arch_cmd_get_xsave (struct kvm_vcpu *vcpu, +struct kvmi_get_xsave_reply **dest, +size_t *dest_size);int kvmi_arch_cmd_get_vcpu_info (struct kvm_vcpu *vcpu,struct kvmi_get_vcpu_info_reply *rpl); int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector,
virt/kvm/kvmi_msg.c +17 -0
@@ -38,6 +38,7 @@ static const char *const msg_IDs[] = { [KVMI_GET_REGISTERS] = "KVMI_GET_REGISTERS", [KVMI_GET_VCPU_INFO] = "KVMI_GET_VCPU_INFO", [KVMI_GET_VERSION] = "KVMI_GET_VERSION", + [KVMI_GET_XSAVE] = "KVMI_GET_XSAVE", [KVMI_INJECT_EXCEPTION] = "KVMI_INJECT_EXCEPTION", [KVMI_PAUSE_VCPU] = "KVMI_PAUSE_VCPU", [KVMI_READ_PHYSICAL] = "KVMI_READ_PHYSICAL",
@@ -700,6 +701,21 @@ static int handle_get_cpuid(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, ec, &rpl,sizeof (rpl)); } +static int handle_get_xsave (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg,const void *req, +vcpu_reply_fct reply_cb) +{ +struct kvmi_get_xsave_reply *rpl =NULL ; +size_t rpl_size =0 ; +int err, ec; + + ec =kvmi_arch_cmd_get_xsave (vcpu, &rpl, &rpl_size); + + err =reply_cb (vcpu, msg, ec, rpl, rpl_size); +kvmi_msg_free (rpl); +return err; +} +/* * These commands are executed on the vCPU thread. The receiving thread * passes the messages using a newly allocated 'struct kvmi_vcpu_cmd'
@@ -716,6 +732,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, [KVMI_GET_CPUID] = handle_get_cpuid, [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, + [KVMI_GET_XSAVE] = handle_get_xsave, [KVMI_INJECT_EXCEPTION] = handle_inject_exception, [KVMI_SET_REGISTERS] = handle_set_registers, };
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 58/92 kvm: introspection: add KVMI_GET_MTRR_TYPE
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This command returns the memory type for a guest physical address.
--- Documentation/virtual/kvm/kvmi.rst | 32 ++++++++++++++++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 9 +++++++++ arch/x86/kvm/kvmi.c | 7 +++++++ virt/kvm/kvmi_int.h | 1 + virt/kvm/kvmi_msg.c | 17 ++++++++++++++++ 5 files changed, 66 insertions(+)
Documentation/virtual/kvm/kvmi.rst +32 -0
@@ -1112,6 +1112,38 @@ the buffer size from the message size. * -KVM_EAGAIN - the selected vCPU can't be introspected yet * -KVM_ENOMEM - not enough memory to allocate the reply +24. KVMI_GET_MTRR_TYPE +---------------------- + +:Architecture: x86 +:Versions: >= 1 +:Parameters: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_get_mtrr_type { + __u64 gpa; + }; + +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_mtrr_type_reply { + __u8 type; + __u8 padding[7]; + }; + +Returns the guest memory type for a specific physical address. + +:Errors: + +* -KVM_EINVAL - the selected vCPU is invalid +* -KVM_EINVAL - padding is not zero +* -KVM_EAGAIN - the selected vCPU can't be introspected yet + Events ======
arch/x86/include/uapi/asm/kvmi.h +9 -0
@@ -101,4 +101,13 @@ struct kvmi_get_xsave_reply { __u32 region[0]; }; +struct kvmi_get_mtrr_type { +__u64 gpa; +}; + +struct kvmi_get_mtrr_type_reply { +__u8 type; +__u8 padding[7 ]; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +7 -0
@@ -811,3 +811,10 @@ int kvmi_arch_cmd_get_xsave(struct kvm_vcpu *vcpu,return 0 ; } + +int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type) +{ + *type =kvm_mtrr_get_guest_memory_type (vcpu,gpa_to_gfn (gpa)); + +return 0 ; +}
virt/kvm/kvmi_int.h +1 -0
@@ -267,5 +267,6 @@ int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu, const struct kvmi_control_cr *req);int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu,const struct kvmi_control_msr *req); +int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type);#endif
virt/kvm/kvmi_msg.c +17 -0
@@ -33,6 +33,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_CPUID] = "KVMI_GET_CPUID", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", + [KVMI_GET_MTRR_TYPE] = "KVMI_GET_MTRR_TYPE", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP", [KVMI_GET_REGISTERS] = "KVMI_GET_REGISTERS",
@@ -701,6 +702,21 @@ static int handle_get_cpuid(struct kvm_vcpu *vcpu,return reply_cb (vcpu, msg, ec, &rpl,sizeof (rpl)); } +static int handle_get_mtrr_type (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +const void *_req,vcpu_reply_fct reply_cb) +{ +const struct kvmi_get_mtrr_type *req = _req; +struct kvmi_get_mtrr_type_reply rpl; +int ec; + +memset (&rpl,0 ,sizeof (rpl)); + + ec =kvmi_arch_cmd_get_mtrr_type (vcpu, req->gpa, &rpl.type); + +return reply_cb (vcpu, msg, ec, &rpl,sizeof (rpl)); +} +static int handle_get_xsave (struct kvm_vcpu *vcpu,const struct kvmi_msg_hdr *msg,const void *req,vcpu_reply_fct reply_cb)
@@ -730,6 +746,7 @@ static int(*const msg_vcpu[])(struct kvm_vcpu *, [KVMI_CONTROL_MSR] = handle_control_msr, [KVMI_EVENT_REPLY] = handle_event_reply, [KVMI_GET_CPUID] = handle_get_cpuid, + [KVMI_GET_MTRR_TYPE] = handle_get_mtrr_type, [KVMI_GET_REGISTERS] = handle_get_registers, [KVMI_GET_VCPU_INFO] = handle_get_vcpu_info, [KVMI_GET_XSAVE] = handle_get_xsave,
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 59/92 kvm: introspection: add KVMI_EVENT_XSETBV
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This event is sent when the extended control register XCR0 is going to be changed.
--- Documentation/virtual/kvm/kvmi.rst | 25 +++++++++++++++++++ arch/x86/include/asm/kvmi_host.h | 5 ++++ arch/x86/kvm/kvmi.c | 39 ++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 5 ++++ 4 files changed, 74 insertions(+)
Documentation/virtual/kvm/kvmi.rst +25 -0
@@ -1444,3 +1444,28 @@ register (see **KVMI_CONTROL_EVENTS**). ``kvmi_event``, the MSR number, the old value and the new value are sent to the introspector. The *CONTINUE* action will set the ``new_val``. + +8. KVMI_EVENT_XSETBV +-------------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when the extended control register XCR0 is going +to be changed and the introspection has been enabled for this event +(see *KVMI_CONTROL_EVENTS*). + +``kvmi_event`` is sent to the introspector.
arch/x86/include/asm/kvmi_host.h +5 -0
@@ -15,6 +15,7 @@ bool kvmi_msr_event(struct kvm_vcpu *vcpu, struct msr_data *msr);bool kvmi_monitored_msr (struct kvm_vcpu *vcpu,u32 msr);bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value); +void kvmi_xsetbv_event (struct kvm_vcpu *vcpu);#else /* CONFIG_KVM_INTROSPECTION */
@@ -35,6 +36,10 @@ static inline bool kvmi_cr_event(struct kvm_vcpu *vcpu, unsigned int cr,return true; } +static inline void kvmi_xsetbv_event (struct kvm_vcpu *vcpu) +{ +} +#endif /* CONFIG_KVM_INTROSPECTION */ #endif /* _ASM_X86_KVMI_HOST_H */
arch/x86/kvm/kvmi.c +39 -0
@@ -389,6 +389,45 @@ bool kvmi_cr_event(struct kvm_vcpu *vcpu, unsigned int cr,return ret; } +static u32 kvmi_send_xsetbv (struct kvm_vcpu *vcpu) +{ +int err, action; + + err =kvmi_send_event (vcpu,KVMI_EVENT_XSETBV ,NULL ,0 , +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} + +static void __kvmi_xsetbv_event (struct kvm_vcpu *vcpu) +{ +u32 action; + + action =kvmi_send_xsetbv (vcpu); +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"XSETBV" ); + } +} + +void kvmi_xsetbv_event (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return ; + +if (is_event_enabled (vcpu,KVMI_EVENT_XSETBV )) +__kvmi_xsetbv_event (vcpu); + +kvmi_put (vcpu->kvm); +} +bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access) {
arch/x86/kvm/x86.c +5 -0
@@ -868,6 +868,11 @@ static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)int kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr) { +#ifdef CONFIG_KVM_INTROSPECTION +if (xcr != vcpu->arch.xcr0) +kvmi_xsetbv_event (vcpu); +#endif /* CONFIG_KVM_INTROSPECTION */ +if (kvm_x86_ops->get_cpl (vcpu) !=0 ||__kvm_set_xcr (vcpu, index, xcr)) {kvm_inject_gp (vcpu,0 );
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 60/92 kvm: x86: add kvm_arch_vcpu_set_guest_debug()
In reply to Adalbert LazărThis function is need in order to intercept breakpoints and send KVMI_EVENT_BREAKPOINT events to the introspection tool.
--- arch/x86/kvm/x86.c | 18 +++++++++++++----- include/linux/kvm_host.h | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-)
arch/x86/kvm/x86.c +13 -5
@@ -8747,14 +8747,12 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,return ret; } -int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu, -struct kvm_guest_debug *dbg) +int kvm_arch_vcpu_set_guest_debug (struct kvm_vcpu *vcpu, +struct kvm_guest_debug *dbg) {unsigned long rflags;int i, r; -vcpu_load (vcpu); -if (dbg->control & (KVM_GUESTDBG_INJECT_DB |KVM_GUESTDBG_INJECT_BP )) { r = -EBUSY ;if (vcpu->arch.exception.pending)
@@ -8800,10 +8798,20 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, r =0 ; out: -vcpu_put (vcpu);return r; } +int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu, +struct kvm_guest_debug *dbg) +{ +int ret; + +vcpu_load (vcpu); + ret =kvm_arch_vcpu_set_guest_debug (vcpu, dbg); +vcpu_put (vcpu); +return ret; +} +/* * Translate a guest virtual address to a guest physical address. */
include/linux/kvm_host.h +2 -0
@@ -804,6 +804,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state);int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg); +int kvm_arch_vcpu_set_guest_debug (struct kvm_vcpu *vcpu, +struct kvm_guest_debug *dbg);int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run);void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu,struct kvm_xsave *guest_xsave);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 61/92 kvm: introspection: add KVMI_EVENT_BREAKPOINT
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This event is sent when a breakpoint was reached. It has to be enabled with the KVMI_CONTROL_EVENTS command first.
The introspection tool can place breakpoints and use them as notification for when the OS or an application has reached a certain state or is trying to perform a certain operation (like creating a process).
--- Documentation/virtual/kvm/kvmi.rst | 36 +++++++++++++ arch/x86/kvm/kvmi.c | 20 +++++++ arch/x86/kvm/svm.c | 6 +++ arch/x86/kvm/vmx/vmx.c | 17 ++++-- arch/x86/kvm/x86.c | 12 +++++ include/linux/kvm_host.h | 2 + include/linux/kvmi.h | 7 +++ include/uapi/linux/kvmi.h | 6 +++ virt/kvm/kvmi.c | 84 ++++++++++++++++++++++++++++-- virt/kvm/kvmi_int.h | 3 ++ virt/kvm/kvmi_msg.c | 17 ++++++ 11 files changed, 201 insertions(+), 9 deletions(-)
Documentation/virtual/kvm/kvmi.rst +36 -0
@@ -1469,3 +1469,39 @@ to be changed and the introspection has been enabled for this event (see *KVMI_CONTROL_EVENTS*). ``kvmi_event`` is sent to the introspector. + +9. KVMI_EVENT_BREAKPOINT +------------------------ + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH, RETRY +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_breakpoint { + __u64 gpa; + __u8 insn_len; + __u8 padding[7]; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when a breakpoint was reached and the introspection has +been enabled for this event (see *KVMI_CONTROL_EVENTS*). + +Some of these breakpoints could have been injected by the introspector, +placed in the slack space of various functions and used as notification +for when the OS or an application has reached a certain state or is +trying to perform a certain operation (like creating a process). + +``kvmi_event`` and the guest physical address are sent to the introspector. + +The *RETRY* action is used by the introspector for its own breakpoints.
arch/x86/kvm/kvmi.c +20 -0
@@ -428,6 +428,26 @@ void kvmi_xsetbv_event(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +void kvmi_arch_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) +{ +u32 action; +u64 gpa; + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + + action =kvmi_msg_send_bp (vcpu, gpa, insn_len); +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +kvm_arch_queue_bp (vcpu); +break ; +case KVMI_EVENT_ACTION_RETRY : +/* rip was most likely adjusted past the INT 3 instruction */ +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"BP" ); + } +} +bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access) {
arch/x86/kvm/svm.c +6 -0
@@ -18,6 +18,7 @@#define pr_fmt (fmt) "SVM: " fmt#include <linux/kvm_host.h> +#include <linux/kvmi.h> #include <asm/kvmi_host.h> #include "irq.h"
@@ -2722,6 +2723,11 @@ static int bp_interception(struct vcpu_svm *svm) {struct kvm_run *kvm_run = svm->vcpu.run; +if (!kvmi_breakpoint_event (&svm->vcpu, + svm->vmcb->save.cs.base + svm->vmcb->save.rip, + svm->vmcb->control.insn_len)) +return 1 ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; kvm_run->debug.arch.exception =BP_VECTOR ;
arch/x86/kvm/vmx/vmx.c +13 -4
@@ -22,6 +22,7 @@#include <linux/kernel.h> #include <linux/kvm_host.h> #include <asm/kvmi_host.h> +#include <linux/kvmi.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/mod_devicetable.h>
@@ -4484,7 +4485,7 @@ static int handle_exception(struct kvm_vcpu *vcpu)struct vcpu_vmx *vmx =to_vmx (vcpu);struct kvm_run *kvm_run = vcpu->run;u32 intr_info, ex_no, error_code; -unsigned long cr2, rip, dr6; +unsigned long cr2, dr6;u32 vect_info;enum emulation_result er;
@@ -4562,7 +4563,10 @@ static int handle_exception(struct kvm_vcpu *vcpu) kvm_run->debug.arch.dr6 = dr6 |DR6_FIXED_1 ; kvm_run->debug.arch.dr7 =vmcs_readl (GUEST_DR7 );/* fall through */ -case BP_VECTOR : +case BP_VECTOR : { +unsigned long gva =vmcs_readl (GUEST_CS_BASE ) + +kvm_rip_read (vcpu); +/* * Update instruction length as we may reinject #BP from * user space while in guest debugging mode. Reading it for
@@ -4570,11 +4574,16 @@ static int handle_exception(struct kvm_vcpu *vcpu)*/ vmx->vcpu.arch.event_exit_inst_len =vmcs_read32 (VM_EXIT_INSTRUCTION_LEN ); + +if (!kvmi_breakpoint_event (vcpu, gva, + vmx->vcpu.arch.event_exit_inst_len)) +return 1 ; + kvm_run->exit_reason =KVM_EXIT_DEBUG ; - rip =kvm_rip_read (vcpu); - kvm_run->debug.arch.pc =vmcs_readl (GUEST_CS_BASE ) + rip; + kvm_run->debug.arch.pc = gva; kvm_run->debug.arch.exception = ex_no;break ; + }default : kvm_run->exit_reason =KVM_EXIT_EXCEPTION ; kvm_run->ex.exception = ex_no;
arch/x86/kvm/x86.c +12 -0
@@ -8763,6 +8763,13 @@ int kvm_arch_vcpu_set_guest_debug(struct kvm_vcpu *vcpu,kvm_queue_exception (vcpu,BP_VECTOR ); } +#ifdef CONFIG_KVM_INTROSPECTION +if (kvmi_bp_intercepted (vcpu, dbg->control)) { + r = -EBUSY ; + goto out; + } +#endif +/* * Read rflags as long as potentially injected trace flags are still * filtered out.
@@ -10106,6 +10113,11 @@ void kvm_arch_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, }EXPORT_SYMBOL_GPL (kvm_arch_msr_intercept); +void kvm_arch_queue_bp (struct kvm_vcpu *vcpu) +{ +kvm_queue_exception (vcpu,BP_VECTOR ); +} +void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) { kvm_x86_ops->cr3_write_exiting (vcpu, enable);
include/linux/kvm_host.h +2 -0
@@ -1330,4 +1330,6 @@ static inline int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu) }#endif /* CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE */ +void kvm_arch_queue_bp (struct kvm_vcpu *vcpu); +#endif
include/linux/kvmi.h +7 -0
@@ -16,11 +16,13 @@ int kvmi_ioctl_event(struct kvm *kvm, void __user *argp);int kvmi_ioctl_unhook (struct kvm *kvm,bool force_reset);int kvmi_vcpu_init (struct kvm_vcpu *vcpu);void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu); +bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu); +bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg);#else
@@ -29,12 +31,17 @@ static inline void kvmi_uninit(void) { }static inline void kvmi_create_vm (struct kvm *kvm) { }static inline void kvmi_destroy_vm (struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; } +static inline bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva, +u8 insn_len) + {return true; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg) + {return false; }#endif /* CONFIG_KVM_INTROSPECTION */
include/uapi/linux/kvmi.h +6 -0
@@ -256,4 +256,10 @@ struct kvmi_event_pf_reply { __u8 ctx_data[256]; }; +struct kvmi_event_breakpoint { +__u64 gpa; +__u8 insn_len; +__u8 padding[7 ]; +}; +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi.c +79 -5
104 lines not on this page · raw message
virt/kvm/kvmi_int.h +3 -0
@@ -118,6 +118,7 @@ struct kvmi_vcpu {bool have_delayed_regs;struct kvm_regs delayed_regs; +bool bp_intercepted;DECLARE_BITMAP (ev_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (cr_mask,KVMI_NUM_CR );struct {
@@ -183,6 +184,7 @@ bool kvmi_msg_process(struct kvmi *ikvm);int kvmi_send_event (struct kvm_vcpu *vcpu,u32 ev_id,void *ev,size_t ev_size,void *rpl,size_t rpl_size,int *action); +u32 kvmi_msg_send_bp (struct kvm_vcpu *vcpu,u64 gpa,u8 insn_len);u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx,u32 *ctx_size);
@@ -252,6 +254,7 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva, u8 access);bool kvmi_arch_queue_exception (struct kvm_vcpu *vcpu);void kvmi_arch_trap_event (struct kvm_vcpu *vcpu); +void kvmi_arch_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len);int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_get_cpuid *req,struct kvmi_get_cpuid_reply *rpl);
virt/kvm/kvmi_msg.c +17 -0
@@ -1079,6 +1079,23 @@ int kvmi_msg_send_unhook(struct kvmi *ikvm)return kvmi_sock_write (ikvm, vec, n, msg_size); } +u32 kvmi_msg_send_bp (struct kvm_vcpu *vcpu,u64 gpa,u8 insn_len) +{ +struct kvmi_event_breakpoint e; +int err, action; + +memset (&e,0 ,sizeof (e)); + e.gpa = gpa; + e.insn_len = insn_len; + + err =kvmi_send_event (vcpu,KVMI_EVENT_BREAKPOINT , &e,sizeof (e), +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} +u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx_data,u32 *ctx_size)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 62/92 kvm: introspection: add KVMI_EVENT_HYPERCALL
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
This event is sent on a specific user hypercall.
It is used by the code residing inside the introspected guest to call the introspection tool and to report certain details about its operation. For example, a classic antimalware remediation tool can report what it has found during a scan.
--- Documentation/virtual/kvm/hypercalls.txt | 34 +++++++++++++++++++++++- Documentation/virtual/kvm/kvmi.rst | 31 +++++++++++++++++++++ arch/x86/kvm/kvmi.c | 33 +++++++++++++++++++++++ arch/x86/kvm/x86.c | 16 ++++++++--- include/linux/kvmi.h | 2 ++ include/uapi/linux/kvm_para.h | 2 ++ virt/kvm/kvmi.c | 22 +++++++++++++++ virt/kvm/kvmi_int.h | 3 +++ virt/kvm/kvmi_msg.c | 12 +++++++++ 9 files changed, 151 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/hypercalls.txt +33 -1
@@ -122,7 +122,7 @@ compute the CLOCK_REALTIME for its clock, at the same instant. Returns KVM_EOPNOTSUPP if the host does not use TSC clocksource, or if clock type is different than KVM_CLOCK_PAIRING_WALLCLOCK. -6. KVM_HC_SEND_IPI +7. KVM_HC_SEND_IPI ------------------------ Architecture: x86 Status: active
@@ -141,3 +141,35 @@ a0 corresponds to the APIC ID in the third argument (a2), bit 1 corresponds to the APIC ID a2+1, and so on. Returns the number of CPUs to which the IPIs were delivered successfully. + +8. KVM_HC_XEN_HVM_OP +-------------------- + +Architecture: x86 +Status: active +Purpose: To enable communication between a guest agent and a VMI application +Usage: + +An event will be sent to the VMI application (see kvmi.rst) if the following +registers, which differ between 32bit and 64bit, have the following values: + + 32bit 64bit value + --------------------------- + ebx (a0) rdi KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT + ecx (a1) rsi 0 + +This specification copies Xen's { __HYPERVISOR_hvm_op, +HVMOP_guest_request_vm_event } hypercall and can originate from kernel or +userspace. + +It returns 0 if successful, or a negative POSIX.1 error code if it fails. The +absence of an active VMI application is not signaled in any way. + +The following registers are clobbered: + + * 32bit: edx, esi, edi, ebp + * 64bit: rdx, r10, r8, r9 + +In particular, for KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT, the last two +registers can be poisoned deliberately and cannot be used for passing +information.
Documentation/virtual/kvm/kvmi.rst +31 -0
@@ -1505,3 +1505,34 @@ trying to perform a certain operation (like creating a process). ``kvmi_event`` and the guest physical address are sent to the introspector. The *RETRY* action is used by the introspector for its own breakpoints. + +10. KVMI_EVENT_HYPERCALL +------------------------ + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent on a specific user hypercall when the introspection has +been enabled for this event (see *KVMI_CONTROL_EVENTS*). + +The hypercall number must be ``KVM_HC_XEN_HVM_OP`` with the +``KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT`` sub-function +(see hypercalls.txt). + +It is used by the code residing inside the introspected guest to call the +introspection tool and to report certain details about its operation. For +example, a classic antimalware remediation tool can report what it has +found during a scan.
arch/x86/kvm/kvmi.c +33 -0
@@ -448,6 +448,39 @@ void kvmi_arch_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len) } } +#define KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT 24 +bool kvmi_arch_is_agent_hypercall (struct kvm_vcpu *vcpu) +{ +unsigned long subfunc1, subfunc2; +bool longmode =is_64_bit_mode (vcpu); + +if (longmode) { + subfunc1 =kvm_register_read (vcpu,VCPU_REGS_RDI ); + subfunc2 =kvm_register_read (vcpu,VCPU_REGS_RSI ); + }else { + subfunc1 =kvm_register_read (vcpu,VCPU_REGS_RBX ); + subfunc1 &=0xFFFFFFFF ; + subfunc2 =kvm_register_read (vcpu,VCPU_REGS_RCX ); + subfunc2 &=0xFFFFFFFF ; + } + +return (subfunc1 ==KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT + && subfunc2 ==0 ); +} + +void kvmi_arch_hypercall_event (struct kvm_vcpu *vcpu) +{ +u32 action; + + action =kvmi_msg_send_hypercall (vcpu); +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"HYPERCALL" ); + } +} +bool kvmi_arch_pf_event (struct kvm_vcpu *vcpu,gpa_t gpa,gva_t gva,u8 access) {
arch/x86/kvm/x86.c +13 -3
@@ -7231,11 +7231,14 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) {unsigned long nr, a0, a1, a2, a3, ret;int op_64_bit; +bool kvmi_hc; -if (kvm_hv_hypercall_enabled (vcpu->kvm)) + nr =kvm_register_read (vcpu,VCPU_REGS_RAX ); + kvmi_hc = (u32 )nr ==KVM_HC_XEN_HVM_OP ; + +if (kvm_hv_hypercall_enabled (vcpu->kvm) && !kvmi_hc)return kvm_hv_hypercall (vcpu); - nr =kvm_register_read (vcpu,VCPU_REGS_RAX ); a0 =kvm_register_read (vcpu,VCPU_REGS_RBX ); a1 =kvm_register_read (vcpu,VCPU_REGS_RCX ); a2 =kvm_register_read (vcpu,VCPU_REGS_RDX );
@@ -7252,7 +7255,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) a3 &=0xFFFFFFFF ; } -if (kvm_x86_ops->get_cpl (vcpu) !=0 ) { +if (kvm_x86_ops->get_cpl (vcpu) !=0 && !kvmi_hc) { ret = -KVM_EPERM ; goto out; }
@@ -7273,6 +7276,13 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)case KVM_HC_SEND_IPI : ret =kvm_pv_send_ipi (vcpu->kvm, a0, a1, a2, a3, op_64_bit);break ; +#ifdef CONFIG_KVM_INTROSPECTION +case KVM_HC_XEN_HVM_OP : + ret =0 ; +if (!kvmi_hypercall_event (vcpu)) + ret = -KVM_ENOSYS ; +break ; +#endif /* CONFIG_KVM_INTROSPECTION */ default : ret = -KVM_ENOSYS ;break ;
include/linux/kvmi.h +2 -0
@@ -17,6 +17,7 @@ int kvmi_ioctl_unhook(struct kvm *kvm, bool force_reset);int kvmi_vcpu_init (struct kvm_vcpu *vcpu);void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu);bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len); +bool kvmi_hypercall_event (struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);void kvmi_handle_requests (struct kvm_vcpu *vcpu);
@@ -36,6 +37,7 @@ static inline bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, {return true; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }
include/uapi/linux/kvm_para.h +2 -0
@@ -33,6 +33,8 @@#define KVM_HC_CLOCK_PAIRING 9#define KVM_HC_SEND_IPI 10 +#define KVM_HC_XEN_HVM_OP 34/* Xen's __HYPERVISOR_hvm_op */ +/* * hypercalls use architecture specific */
virt/kvm/kvmi.c +22 -0
@@ -1120,6 +1120,28 @@ bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len) }EXPORT_SYMBOL (kvmi_breakpoint_event); +bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm; +bool ret = false; + +if (!kvmi_arch_is_agent_hypercall (vcpu)) +return ret; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return ret; + +if (is_event_enabled (vcpu,KVMI_EVENT_HYPERCALL )) { +kvmi_arch_hypercall_event (vcpu); + ret = true; + } + +kvmi_put (vcpu->kvm); + +return ret; +} +/* * This function returns false if there is an exception or interrupt pending. * It returns true in all other cases including KVMI not being initialized.
virt/kvm/kvmi_int.h +3 -0
@@ -185,6 +185,7 @@ int kvmi_send_event(struct kvm_vcpu *vcpu, u32 ev_id, void *ev, size_t ev_size, void *rpl, size_t rpl_size, int *action);u32 kvmi_msg_send_bp (struct kvm_vcpu *vcpu,u64 gpa,u8 insn_len); +u32 kvmi_msg_send_hypercall (struct kvm_vcpu *vcpu);u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx,u32 *ctx_size);
@@ -255,6 +256,8 @@ bool kvmi_arch_pf_event(struct kvm_vcpu *vcpu, gpa_t gpa, gva_t gva,bool kvmi_arch_queue_exception (struct kvm_vcpu *vcpu);void kvmi_arch_trap_event (struct kvm_vcpu *vcpu);void kvmi_arch_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len); +bool kvmi_arch_is_agent_hypercall (struct kvm_vcpu *vcpu); +void kvmi_arch_hypercall_event (struct kvm_vcpu *vcpu);int kvmi_arch_cmd_get_cpuid (struct kvm_vcpu *vcpu,const struct kvmi_get_cpuid *req,struct kvmi_get_cpuid_reply *rpl);
virt/kvm/kvmi_msg.c +12 -0
@@ -1096,6 +1096,18 @@ u32 kvmi_msg_send_bp(struct kvm_vcpu *vcpu, u64 gpa, u8 insn_len)return action; } +u32 kvmi_msg_send_hypercall (struct kvm_vcpu *vcpu) +{ +int err, action; + + err =kvmi_send_event (vcpu,KVMI_EVENT_HYPERCALL ,NULL ,0 , +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} +u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx_data,u32 *ctx_size)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 63/92 kvm: introspection: add KVMI_EVENT_DESCRIPTOR
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This event is sent when IDTR, GDTR, LDTR or TR are accessed.
These could be used to implement a tiny agent which runs in the context of an introspected guest and uses virtualized exceptions (#VE) and alternate EPT views (VMFUNC #0) to filter converted VMEXITS. The events of interested will be suppressed (after some appropriate guest-side handling) while the rest will be sent to the introspector via a VMCALL.
--- Documentation/virtual/kvm/kvmi.rst | 38 +++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvmi.h | 11 +++++ arch/x86/kvm/kvmi.c | 70 ++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 74 ++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 59 +++++++++++++++++++++++- arch/x86/kvm/vmx/vmx.h | 2 + arch/x86/kvm/x86.c | 6 +++ include/linux/kvm_host.h | 1 + include/linux/kvmi.h | 4 ++ virt/kvm/kvmi.c | 2 +- virt/kvm/kvmi_int.h | 3 ++ virt/kvm/kvmi_msg.c | 17 +++++++ 13 files changed, 285 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/kvmi.rst +38 -0
@@ -1536,3 +1536,41 @@ It is used by the code residing inside the introspected guest to call the introspection tool and to report certain details about its operation. For example, a classic antimalware remediation tool can report what it has found during a scan. + +11. KVMI_EVENT_DESCRIPTOR +------------------------- + +:Architecture: x86 +:Versions: >= 1 +:Actions: CONTINUE, RETRY, CRASH +:Parameters: + +:: + + struct kvmi_event; + struct kvmi_event_descriptor { + __u8 descriptor; + __u8 write; + __u8 padding[6]; + }; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when a descriptor table register is accessed and the +introspection has been enabled for this event (see **KVMI_CONTROL_EVENTS**). + +``kvmi_event`` and ``kvmi_event_descriptor`` are sent to the introspector. + +``descriptor`` can be one of:: + + KVMI_DESC_IDTR + KVMI_DESC_GDTR + KVMI_DESC_LDTR + KVMI_DESC_TR + +``write`` is 1 if the descriptor was written, 0 otherwise.
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1015,6 +1015,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
arch/x86/include/uapi/asm/kvmi.h +11 -0
@@ -110,4 +110,15 @@ struct kvmi_get_mtrr_type_reply { __u8 padding[7]; }; +#define KVMI_DESC_IDTR 1 +#define KVMI_DESC_GDTR 2 +#define KVMI_DESC_LDTR 3 +#define KVMI_DESC_TR 4 + +struct kvmi_event_descriptor { +__u8 descriptor; +__u8 write; +__u8 padding[6 ]; +}; +#endif /* _UAPI_ASM_X86_KVMI_H */
arch/x86/kvm/kvmi.c +70 -0
82 lines not on this page · raw message
arch/x86/kvm/svm.c +74 -0
104 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +57 -2
90 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +2 -0
@@ -268,6 +268,8 @@ struct vcpu_vmx {u64 msr_ia32_feature_control_valid_bits;u64 ept_pointer; +bool tracking_desc; +struct pt_desc pt_desc; };
arch/x86/kvm/x86.c +6 -0
@@ -10140,6 +10140,12 @@ bool kvm_spt_fault(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL (kvm_spt_fault); +bool kvm_arch_vcpu_intercept_desc (struct kvm_vcpu *vcpu,bool enable) +{ +return kvm_x86_ops->desc_intercept (vcpu, enable); +} +EXPORT_SYMBOL (kvm_arch_vcpu_intercept_desc); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);
include/linux/kvm_host.h +1 -0
@@ -806,6 +806,7 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg);int kvm_arch_vcpu_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg); +bool kvm_arch_vcpu_intercept_desc (struct kvm_vcpu *vcpu,bool enable);int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run);void kvm_vcpu_ioctl_x86_get_xsave (struct kvm_vcpu *vcpu,struct kvm_xsave *guest_xsave);
include/linux/kvmi.h +4 -0
@@ -20,6 +20,7 @@ bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len);bool kvmi_hypercall_event (struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu); +bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);
@@ -35,6 +36,9 @@ static inline int kvmi_vcpu_init(struct kvm_vcpu *vcpu) { return 0; }static inline bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) {return true; } +static inline bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor, +u8 write) + {return true; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }
virt/kvm/kvmi.c +1 -1
@@ -1529,7 +1529,7 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id, err =kvmi_control_event_breakpoint (vcpu, enable);break ;default : - err =0 ; + err =kvmi_arch_cmd_control_event (vcpu, event_id, enable);break ; }
virt/kvm/kvmi_int.h +3 -0
@@ -189,6 +189,7 @@ u32 kvmi_msg_send_hypercall(struct kvm_vcpu *vcpu);u32 kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u8 access,bool *singlestep,bool *rep_complete,u64 *ctx_addr,u8 *ctx,u32 *ctx_size); +u32 kvmi_msg_send_descriptor (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu);u32 kvmi_msg_send_pause_vcpu (struct kvm_vcpu *vcpu);int kvmi_msg_send_unhook (struct kvmi *ikvm);
@@ -228,6 +229,8 @@ void kvmi_handle_common_event_actions(struct kvm_vcpu *vcpu, u32 action,void kvmi_arch_update_page_tracking (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvmi_mem_access *m); +int kvmi_arch_cmd_control_event (struct kvm_vcpu *vcpu,unsigned int event_id, +bool enable); int kvmi_arch_cmd_get_registers(struct kvm_vcpu *vcpu, const struct kvmi_msg_hdr *msg, const struct kvmi_get_registers *req,
virt/kvm/kvmi_msg.c +17 -0
@@ -1163,6 +1163,23 @@ u32 kvmi_msg_send_pf(struct kvm_vcpu *vcpu, u64 gpa, u64 gva, u8 access,return action; } +u32 kvmi_msg_send_descriptor (struct kvm_vcpu *vcpu,u8 descriptor,u8 write) +{ +struct kvmi_event_descriptor e; +int err, action; + +memset (&e,0 ,sizeof (e)); + e.descriptor = descriptor; + e.write = write; + + err =kvmi_send_event (vcpu,KVMI_EVENT_DESCRIPTOR , &e,sizeof (e), +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} +u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu) {int err, action;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 64/92 kvm: introspection: add single-stepping
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This would be used either if the introspection tool request it as a reply to a KVMI_EVENT_PF event or to cope with instructions that cannot be handled by the x86 emulator during the handling of a VMEXIT. In these situations, all other vCPU-s are kicked and held, the EPT-based protection is removed and the guest is single stepped by the vCPU that triggered the initial VMEXIT. Upon completion the EPT-base protection is reinstalled and all vCPU-s all allowed to return to the guest.
This is a rather slow workaround that kicks in occasionally. In the future, the most frequently single-stepped instructions should be added to the emulator (usually, stores to and from memory - SSE/AVX).
For the moment it works only on Intel.
--- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/kvmi.c | 47 ++++++++++- arch/x86/kvm/svm.c | 5 ++ arch/x86/kvm/vmx/vmx.c | 17 ++++ arch/x86/kvm/x86.c | 19 +++++ include/linux/kvmi.h | 4 + virt/kvm/kvmi.c | 145 +++++++++++++++++++++++++++++++- virt/kvm/kvmi_int.h | 16 ++++ 8 files changed, 253 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1016,6 +1016,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);bool (*spt_fault)(struct kvm_vcpu *vcpu);
@@ -1628,6 +1629,8 @@ void kvm_arch_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, bool enable);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable); +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask);void kvm_control_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/kvmi.c +45 -2
71 lines not on this page · raw message
arch/x86/kvm/svm.c +5 -0
@@ -7183,6 +7183,10 @@ static bool svm_spt_fault(struct kvm_vcpu *vcpu)return (svm->vmcb->control.exit_code ==SVM_EXIT_NPF ); } +static void svm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +} +static void svm_cr3_write_exiting (struct kvm_vcpu *vcpu,bool enable) { }
@@ -7225,6 +7229,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, .has_emulated_msr = svm_has_emulated_msr, + .set_mtf = svm_set_mtf, .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept, .desc_intercept = svm_desc_intercept,
arch/x86/kvm/vmx/vmx.c +17 -0
@@ -5384,6 +5384,7 @@ static int handle_invalid_op(struct kvm_vcpu *vcpu)static int handle_monitor_trap (struct kvm_vcpu *vcpu) { +kvmi_stop_ss (vcpu);return 1 ; }
@@ -5992,6 +5993,11 @@ static int vmx_handle_exit(struct kvm_vcpu *vcpu) } } +if (kvmi_vcpu_enabled_ss (vcpu) + && exit_reason !=EXIT_REASON_EPT_VIOLATION + && exit_reason !=EXIT_REASON_MONITOR_TRAP_FLAG ) +kvmi_stop_ss (vcpu); +if (exit_reason < kvm_vmx_max_exit_handlers && kvm_vmx_exit_handlers[exit_reason])return kvm_vmx_exit_handlers[exit_reason](vcpu);
@@ -7842,6 +7848,16 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } +static void vmx_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ +if (enable) +vmcs_set_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +else +vmcs_clear_bits (CPU_BASED_VM_EXEC_CONTROL , +CPU_BASED_MONITOR_TRAP_FLAG ); +} +static void vmx_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable) {
@@ -7927,6 +7943,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .cpu_has_accelerated_tpr = report_flexpriority, .has_emulated_msr = vmx_has_emulated_msr, + .set_mtf = vmx_set_mtf, .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .desc_intercept = vmx_desc_intercept,
arch/x86/kvm/x86.c +19 -0
@@ -7358,6 +7358,13 @@ static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win) {int r; +if (kvmi_vcpu_enabled_ss (vcpu)) +/* +* We cannot inject events during single-stepping. +* Try again later. +*/ +return -1 ; +/* try to reinject previous events if any */ if (vcpu->arch.exception.injected)
@@ -10134,6 +10141,18 @@ void kvm_control_cr3_write_exiting(struct kvm_vcpu *vcpu, bool enable) }EXPORT_SYMBOL (kvm_control_cr3_write_exiting); +void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable) +{ + kvm_x86_ops->set_mtf (vcpu, enable); +} +EXPORT_SYMBOL (kvm_set_mtf); + +void kvm_set_interrupt_shadow (struct kvm_vcpu *vcpu,int mask) +{ + kvm_x86_ops->set_interrupt_shadow (vcpu, mask); +} +EXPORT_SYMBOL (kvm_set_interrupt_shadow); +bool kvm_spt_fault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->spt_fault (vcpu);
include/linux/kvmi.h +4 -0
@@ -22,6 +22,8 @@ bool kvmi_queue_exception(struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +void kvmi_stop_ss (struct kvm_vcpu *vcpu); +bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu);bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg);
@@ -44,6 +46,8 @@ static inline void kvmi_handle_requests(struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }static inline bool kvmi_queue_exception (struct kvm_vcpu *vcpu) {return true; }static inline void kvmi_trap_event (struct kvm_vcpu *vcpu) { } +static inline void kvmi_stop_ss (struct kvm_vcpu *vcpu) { } +static inline bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu) {return false; }static inline void kvmi_init_emulate (struct kvm_vcpu *vcpu) { }static inline void kvmi_activate_rep_complete (struct kvm_vcpu *vcpu) { }static inline bool kvmi_bp_intercepted (struct kvm_vcpu *vcpu,u32 dbg)
virt/kvm/kvmi.c +144 -1
161 lines not on this page · raw message
virt/kvm/kvmi_int.h +16 -0
@@ -126,6 +126,9 @@ struct kvmi_vcpu {DECLARE_BITMAP (high,KVMI_NUM_MSR ); } msr_mask; +bool ss_owner; +bool ss_requested; +struct list_head job_list;spinlock_t job_lock;
@@ -151,6 +154,15 @@ struct kvmi {DECLARE_BITMAP (event_allow_mask,KVMI_NUM_EVENTS );DECLARE_BITMAP (vm_ev_mask,KVMI_NUM_EVENTS ); +#define SINGLE_STEP_MAX_DEPTH 8 +struct { +gfn_t gfn; +u8 old_access; +u32 old_write_bitmap; + } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_level; +atomic_t ss_active; +struct {bool initialized;atomic_t enabled;
@@ -224,6 +236,7 @@ int kvmi_add_job(struct kvm_vcpu *vcpu, void *ctx, void (*free_fct)(void *ctx));void kvmi_handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action,const char *str); +bool kvmi_start_ss (struct kvm_vcpu *vcpu,gpa_t gpa,u8 access);/* arch */ void kvmi_arch_update_page_tracking(struct kvm *kvm,
@@ -274,6 +287,9 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu); +void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu); +u8 kvmi_arch_relax_page_access (u8 old,u8 new);int kvmi_arch_cmd_control_msr (struct kvm_vcpu *vcpu,const struct kvmi_control_msr *req);int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 65/92 kvm: introspection: add KVMI_EVENT_SINGLESTEP
In reply to Adalbert LazărFrom: Nicușor Cîțu <[email protected]>
This event is sent when the current instruction has been single stepped as a result of a KVMI_EVENT_PF event to which the introspection tool set the singlestep field and responded with CONTINUE.
--- Documentation/virtual/kvm/kvmi.rst | 25 +++++++++++++++++++ virt/kvm/kvmi.c | 40 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
Documentation/virtual/kvm/kvmi.rst +25 -0
@@ -1574,3 +1574,28 @@ introspection has been enabled for this event (see **KVMI_CONTROL_EVENTS**). KVMI_DESC_TR ``write`` is 1 if the descriptor was written, 0 otherwise. + +12. KVMI_EVENT_SINGLESTEP +------------------------- + +:Architectures: x86 +:Versions: >= 1 +:Actions: CONTINUE, CRASH +:Parameters: + +:: + + struct kvmi_event; + +:Returns: + +:: + + struct kvmi_vcpu_hdr; + struct kvmi_event_reply; + +This event is sent when the current instruction has been executed +(as a result of a *KVMI_EVENT_PF* event to which the introspection +tool set the ``singlestep`` field and responded with *CONTINUE*) +and the introspection has been enabled for this event +(see **KVMI_CONTROL_EVENTS**).
virt/kvm/kvmi.c +40 -0
@@ -1182,6 +1182,44 @@ void kvmi_trap_event(struct kvm_vcpu *vcpu)kvmi_put (vcpu->kvm); } +static u32 kvmi_send_singlestep (struct kvm_vcpu *vcpu) +{ +int err, action; + + err =kvmi_send_event (vcpu,KVMI_EVENT_SINGLESTEP ,NULL ,0 , +NULL ,0 , &action); +if (err) +return KVMI_EVENT_ACTION_CONTINUE ; + +return action; +} + +static void __kvmi_singlestep_event (struct kvm_vcpu *vcpu) +{ +u32 action; + + action =kvmi_send_singlestep (vcpu); +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +kvmi_handle_common_event_actions (vcpu, action,"SINGLESTEP" ); + } +} + +static void kvmi_singlestep_event (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (!ivcpu->ss_requested) +return ; + +if (is_event_enabled (vcpu,KVMI_EVENT_SINGLESTEP )) +__kvmi_singlestep_event (vcpu); + + ivcpu->ss_requested = false; +} +static bool __kvmi_create_vcpu_event (struct kvm_vcpu *vcpu) {u32 action;
@@ -1616,6 +1654,8 @@ void kvmi_stop_ss(struct kvm_vcpu *vcpu) ivcpu->ss_owner = false; +kvmi_singlestep_event (vcpu); + out:kvmi_put (kvm); }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 66/92 kvm: introspection: add custom input when single-stepping a vCPU
In reply to Adalbert LazărThe introspection tool can respond to a KVMI_EVENT_PF event with custom input for the current instruction. This input is used to trick the guest software into believing it has read certain data, in order to hide the content of certain memory areas (eg. hide injected code from integrity checkers). There are cases when this can happen while the vCPU has to be single stepped, Either the current instruction is not supported by the KVM emulator or the introspection tool requested single-stepping.
This patch saves the old data, write the custom input, start the single stepping and restore the old data.
--- virt/kvm/kvmi.c | 119 ++++++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 3 ++ 2 files changed, 122 insertions(+)
virt/kvm/kvmi.c +119 -0
@@ -1618,6 +1618,116 @@ int kvmi_cmd_pause_vcpu(struct kvm_vcpu *vcpu, bool wait)return 0 ; } +static int write_custom_data_to_page (struct kvm_vcpu *vcpu,gva_t gva, +u8 *backup,size_t bytes) +{ +u8 *ptr_page, *ptr; +struct page *page; +gpa_t gpa; + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); +if (gpa ==UNMAPPED_GVA ) +return -KVM_EINVAL ; + + ptr_page =get_page_ptr (vcpu->kvm, gpa, &page, true); +if (!ptr_page) +return -KVM_EINVAL ; + + ptr = ptr_page + (gpa & ~PAGE_MASK ); + +memcpy (backup, ptr, bytes); +use_custom_input (vcpu, gva, ptr, bytes); + +put_page_ptr (ptr_page, page); + +return 0 ; +} + +static int write_custom_data (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +size_t bytes = ivcpu->ctx_size; +gva_t gva = ivcpu->ctx_addr; +u8 *backup; + +if (ikvm->ss_custom_size) +return 0 ; + +if (!bytes) +return 0 ; + + backup = ikvm->ss_custom_data; + +while (bytes) { +size_t offset = gva & ~PAGE_MASK ; +size_t chunk =min (bytes,PAGE_SIZE - offset); + +if (write_custom_data_to_page (vcpu, gva, backup, chunk)) +return -KVM_EINVAL ; + + bytes -= chunk; + backup += chunk; + gva += chunk; + ikvm->ss_custom_size += chunk; + } + +return 0 ; +} + +static int restore_backup_data_to_page (struct kvm_vcpu *vcpu,gva_t gva, +u8 *src,size_t bytes) +{ +u8 *ptr_page, *ptr; +struct page *page; +gpa_t gpa; + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); +if (gpa ==UNMAPPED_GVA ) +return -KVM_EINVAL ; + + ptr_page =get_page_ptr (vcpu->kvm, gpa, &page, true); +if (!ptr_page) +return -KVM_EINVAL ; + + ptr = ptr_page + (gpa & ~PAGE_MASK ); + +memcpy (ptr, src, bytes); + +put_page_ptr (ptr_page, page); + +return 0 ; +} + +static void restore_backup_data (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +size_t bytes = ikvm->ss_custom_size; +gva_t gva = ivcpu->ctx_addr; +u8 *backup; + +if (!bytes) +return ; + + backup = ikvm->ss_custom_data; + +while (bytes) { +size_t offset = gva & ~PAGE_MASK ; +size_t chunk =min (bytes,PAGE_SIZE - offset); + +if (restore_backup_data_to_page (vcpu, gva, backup, chunk)) + goto out; + + bytes -= chunk; + backup += chunk; + gva += chunk; + } + +out: + ikvm->ss_custom_size =0 ; +} +void kvmi_stop_ss (struct kvm_vcpu *vcpu) {struct kvmi_vcpu *ivcpu =IVCPU (vcpu);
@@ -1642,6 +1752,8 @@ void kvmi_stop_ss(struct kvm_vcpu *vcpu) ikvm->ss_level =0 ; +restore_backup_data (vcpu); +kvmi_arch_stop_single_step (vcpu);atomic_set (&ikvm->ss_active, false);
@@ -1676,6 +1788,7 @@ static bool kvmi_acquire_ss(struct kvm_vcpu *vcpu) KVM_REQUEST_WAIT); ivcpu->ss_owner = true; + ikvm->ss_custom_size =0 ;return true; }
@@ -1690,6 +1803,12 @@ static bool kvmi_run_ss(struct kvm_vcpu *vcpu, gpa_t gpa, u8 access)kvmi_arch_start_single_step (vcpu); + err =write_custom_data (vcpu); +if (err) { +kvmi_err (ikvm,"writing custom data failed, err %d\n" , err); +return false; + } + err =kvmi_get_gfn_access (ikvm, gfn, &old_access, &old_write_bitmap);/* likely was removed from radix tree due to rwx */ if (err) {
virt/kvm/kvmi_int.h +3 -0
@@ -160,6 +160,9 @@ struct kvmi {u8 old_access;u32 old_write_bitmap; } ss_context[SINGLE_STEP_MAX_DEPTH ]; +u8 ss_custom_data[KVMI_CTX_DATA_SIZE ]; +size_t ss_custom_size; +gpa_t ss_custom_addr;u8 ss_level;atomic_t ss_active;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 67/92 kvm: introspection: use single stepping on unimplemented instructions
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
On emulation failures, we notify the introspection tool for read/write operations if needed. Unless it responds with RETRY (to re-enter guest), we continue single stepping the vCPU.
--- arch/x86/include/asm/kvm_host.h | 5 +++ arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/kvmi.c | 21 ++++++++++++ arch/x86/kvm/mmu.c | 5 +++ arch/x86/kvm/svm.c | 8 +++++ arch/x86/kvm/vmx/vmx.c | 13 ++++++-- arch/x86/kvm/x86.c | 57 ++++++++++++++++++++++++++++++++- include/linux/kvmi.h | 4 +++ virt/kvm/kvmi.c | 56 ++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 1 + 10 files changed, 169 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +5 -0
@@ -770,6 +770,9 @@ struct kvm_vcpu_arch {/* set at EPT violation at this point */ unsigned long exit_qualification; +/* #PF translated error code from EPT/NPT exit reason */ +u64 error_code; +/* pv related host specific info */ struct {bool pv_unhalted;
@@ -1016,6 +1019,7 @@ struct kvm_x86_ops {void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable);bool (*desc_intercept)(struct kvm_vcpu *vcpu,bool enable); +u64 (*fault_gla)(struct kvm_vcpu *vcpu);void (*set_mtf)(struct kvm_vcpu *vcpu,bool enable);void (*cr3_write_exiting)(struct kvm_vcpu *vcpu,bool enable);bool (*nested_pagefault)(struct kvm_vcpu *vcpu);
@@ -1627,6 +1631,7 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)void kvm_arch_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu);bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu);bool kvm_spt_fault (struct kvm_vcpu *vcpu);void kvm_set_mtf (struct kvm_vcpu *vcpu,bool enable);
arch/x86/include/asm/vmx.h +2 -0
@@ -538,6 +538,7 @@ struct vmx_msr_entry {#define EPT_VIOLATION_READABLE_BIT 3#define EPT_VIOLATION_WRITABLE_BIT 4#define EPT_VIOLATION_EXECUTABLE_BIT 5 +#define EPT_VIOLATION_GLA_VALID_BIT 7#define EPT_VIOLATION_GVA_TRANSLATED_BIT 8#define EPT_VIOLATION_ACC_READ (1 << EPT_VIOLATION_ACC_READ_BIT)#define EPT_VIOLATION_ACC_WRITE (1 << EPT_VIOLATION_ACC_WRITE_BIT)
@@ -545,6 +546,7 @@ struct vmx_msr_entry {#define EPT_VIOLATION_READABLE (1 << EPT_VIOLATION_READABLE_BIT)#define EPT_VIOLATION_WRITABLE (1 << EPT_VIOLATION_WRITABLE_BIT)#define EPT_VIOLATION_EXECUTABLE (1 << EPT_VIOLATION_EXECUTABLE_BIT) +#define EPT_VIOLATION_GLA_VALID (1 << EPT_VIOLATION_GLA_VALID_BIT)#define EPT_VIOLATION_GVA_TRANSLATED (1 << EPT_VIOLATION_GVA_TRANSLATED_BIT)/*
arch/x86/kvm/kvmi.c +21 -0
@@ -759,6 +759,27 @@ int kvmi_arch_cmd_control_cr(struct kvm_vcpu *vcpu,return 0 ; } +bool is_ud2_instruction (struct kvm_vcpu *vcpu,int *emulation_type) +{ +u8 ud2[] = {0x0F ,0x0B }; +u8 insn_len = vcpu->arch.emulate_ctxt.fetch.ptr - + vcpu->arch.emulate_ctxt.fetch.data; + +if (insn_len !=sizeof (ud2)) +return false; + +if (memcmp (vcpu->arch.emulate_ctxt.fetch.data, ud2, insn_len)) +return false; + +/* Do not reexecute the UD2 instruction, else we might enter to an +* endless emulation loop. Let the emulator fall down through the +* handle_emulation_failure() which shall inject the #UD exception. +*/ + *emulation_type &= ~EMULTYPE_ALLOW_RETRY ; + +return true; +} +void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu) {kvm_set_mtf (vcpu, true);
arch/x86/kvm/mmu.c +5 -0
@@ -6667,6 +6667,11 @@ void kvm_mmu_module_exit(void)mmu_audit_disable (); } +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->fault_gla (vcpu); +} +bool kvm_mmu_nested_pagefault (struct kvm_vcpu *vcpu) {return kvm_x86_ops->nested_pagefault (vcpu);
arch/x86/kvm/svm.c +8 -0
@@ -2675,6 +2675,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 fault_address =__sme_clr (svm->vmcb->control.exit_info_2);u64 error_code = svm->vmcb->control.exit_info_1; + svm->vcpu.arch.error_code = error_code; + return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address, static_cpu_has(X86_FEATURE_DECODEASSISTS) ? svm->vmcb->control.insn_bytes : NULL,
@@ -7171,6 +7173,11 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,set_msr_interception (svm, msrpm, msr, enable, enable); } +static u64 svm_fault_gla (struct kvm_vcpu *vcpu) +{ +return ~0ull ; +} +static bool svm_nested_pagefault (struct kvm_vcpu *vcpu) {return false;
@@ -7233,6 +7240,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .cr3_write_exiting = svm_cr3_write_exiting, .msr_intercept = svm_msr_intercept, .desc_intercept = svm_desc_intercept, + .fault_gla = svm_fault_gla, .nested_pagefault = svm_nested_pagefault, .spt_fault = svm_spt_fault,
arch/x86/kvm/vmx/vmx.c +11 -2
@@ -5171,10 +5171,11 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) EPT_VIOLATION_EXECUTABLE)) ?PFERR_PRESENT_MASK :0 ; - error_code |= (exit_qualification &0x100 ) !=0 ? -PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; + error_code |= (exit_qualification &EPT_VIOLATION_GVA_TRANSLATED ) + ?PFERR_GUEST_FINAL_MASK :PFERR_GUEST_PAGE_MASK ; vcpu->arch.exit_qualification = exit_qualification; + vcpu->arch.error_code = error_code;return kvm_mmu_page_fault (vcpu, gpa, error_code,NULL ,0 ); }
@@ -7880,6 +7881,13 @@ static void vmx_cr3_write_exiting(struct kvm_vcpu *vcpu,/* TODO: nested ? vmcs12->cpu_based_vm_exec_control */ } +static u64 vmx_fault_gla (struct kvm_vcpu *vcpu) +{ +if (vcpu->arch.exit_qualification &EPT_VIOLATION_GLA_VALID ) +return vmcs_readl (GUEST_LINEAR_ADDRESS ); +return ~0ull ; +} +static bool vmx_nested_pagefault (struct kvm_vcpu *vcpu) {if (vcpu->arch.exit_qualification &EPT_VIOLATION_GVA_TRANSLATED )
@@ -7947,6 +7955,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .msr_intercept = vmx_msr_intercept, .cr3_write_exiting = vmx_cr3_write_exiting, .desc_intercept = vmx_desc_intercept, + .fault_gla = vmx_fault_gla, .nested_pagefault = vmx_nested_pagefault, .spt_fault = vmx_spt_fault,
arch/x86/kvm/x86.c +56 -1
77 lines not on this page · raw message
include/linux/kvmi.h +4 -0
@@ -21,6 +21,7 @@ bool kvmi_hypercall_event(struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write); +bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_stop_ss (struct kvm_vcpu *vcpu);bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);
@@ -41,6 +42,9 @@ static inline bool kvmi_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva,static inline bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write) {return true; } +static inline bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa, +int *emulation_type) + {return false; }static inline void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) { }static inline void kvmi_handle_requests (struct kvm_vcpu *vcpu) { }static inline bool kvmi_hypercall_event (struct kvm_vcpu *vcpu) {return false; }
virt/kvm/kvmi.c +56 -0
62 lines not on this page · raw message
virt/kvm/kvmi_int.h +1 -0
@@ -290,6 +290,7 @@ int kvmi_arch_cmd_inject_exception(struct kvm_vcpu *vcpu, u8 vector, u64 address);int kvmi_arch_cmd_control_cr (struct kvm_vcpu *vcpu,const struct kvmi_control_cr *req); +bool is_ud2_instruction (struct kvm_vcpu *vcpu,int *emulation_type);void kvmi_arch_start_single_step (struct kvm_vcpu *vcpu);void kvmi_arch_stop_single_step (struct kvm_vcpu *vcpu);u8 kvmi_arch_relax_page_access (u8 old,u8 new);
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 68/92 kvm: x86: emulate a guest page table walk on SPT violations due to A/D bit updates
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
On SPT page faults caused by guest page table walks, use the existing guest page table walk code to make the necessary adjustments to the A/D bits and return to guest. This effectively bypasses the x86 emulator who was making the wrong modifications leading one OS (Windows 8.1 x64) to triple-fault very early in the boot process with the introspection enabled.
With introspection disabled, these faults are handled by simply removing the protection from the affected guest page and returning to guest.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/include/asm/kvmi_host.h | 6 ++++++ arch/x86/kvm/kvmi.c | 34 +++++++++++++++++++++++++++++++- arch/x86/kvm/mmu.c | 11 +++++++++-- arch/x86/kvm/x86.c | 6 +++--- include/linux/kvmi.h | 3 +++ virt/kvm/kvmi.c | 31 +++++++++++++++++++++++++++-- 7 files changed, 84 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1425,7 +1425,7 @@ gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception);gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception); +u32 access,struct x86_exception *exception);void kvm_vcpu_deactivate_apicv (struct kvm_vcpu *vcpu);
arch/x86/include/asm/kvmi_host.h +6 -0
@@ -16,6 +16,7 @@ bool kvmi_monitored_msr(struct kvm_vcpu *vcpu, u32 msr);bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr,unsigned long old_value,unsigned long *new_value);void kvmi_xsetbv_event (struct kvm_vcpu *vcpu); +bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu);#else /* CONFIG_KVM_INTROSPECTION */
@@ -40,6 +41,11 @@ static inline void kvmi_xsetbv_event(struct kvm_vcpu *vcpu) { } +static inline bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif /* CONFIG_KVM_INTROSPECTION */ #endif /* _ASM_X86_KVMI_HOST_H */
arch/x86/kvm/kvmi.c +33 -1
@@ -465,7 +465,7 @@ void kvmi_arch_breakpoint_event(struct kvm_vcpu *vcpu, u64 gva, u8 insn_len)u32 action;u64 gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL ); action =kvmi_msg_send_bp (vcpu, gpa, insn_len);switch (action) {
@@ -822,6 +822,38 @@ u8 kvmi_arch_relax_page_access(u8 old, u8 new)return ret; } +bool kvmi_update_ad_flags (struct kvm_vcpu *vcpu) +{ +struct x86_exception exception = { }; +struct kvmi *ikvm; +bool ret = false; +gva_t gva; +gpa_t gpa; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + gva =kvm_mmu_fault_gla (vcpu); + +if (gva == ~0ull ) { +kvmi_warn_once (ikvm,"%s: cannot perform translation\n" , + __func__); + goto out; + } + + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,PFERR_WRITE_MASK ,NULL ); +if (gpa ==UNMAPPED_GVA ) + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 , &exception); + + ret = (gpa !=UNMAPPED_GVA ); + +out: +kvmi_put (vcpu->kvm); + +return ret; +} + static const struct { unsigned int allow_bit; enum kvm_page_track_mode track_mode;
arch/x86/kvm/mmu.c +9 -2
@@ -40,7 +40,9 @@#include <linux/uaccess.h> #include <linux/hash.h> #include <linux/kern_levels.h> +#include <linux/kvmi.h> +#include <asm/kvmi_host.h> #include <asm/page.h> #include <asm/pat.h> #include <asm/cmpxchg.h>
@@ -5960,8 +5962,13 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,*/ if (vcpu->arch.mmu->direct_map && (error_code &PFERR_NESTED_GUEST_PAGE ) ==PFERR_NESTED_GUEST_PAGE ) { -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (cr2)); -return 1 ; +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (cr2))) { +if (kvmi_update_ad_flags (vcpu)) +return 1 ; + }else { +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (cr2)); +return 1 ; + } }/*
arch/x86/kvm/x86.c +3 -3
@@ -5175,9 +5175,9 @@ gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,/* uses this to access any guest's mapped memory without checking CPL */ gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva, -struct x86_exception *exception) +u32 access,struct x86_exception *exception) { -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva,0 , exception); +return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); } static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
@@ -8904,7 +8904,7 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,vcpu_load (vcpu); idx =srcu_read_lock (&vcpu->kvm->srcu); - gpa =kvm_mmu_gva_to_gpa_system (vcpu, vaddr,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, vaddr,0 ,NULL );srcu_read_unlock (&vcpu->kvm->srcu, idx); tr->physical_address = gpa; tr->valid = gpa !=UNMAPPED_GVA ;
include/linux/kvmi.h +3 -0
@@ -21,6 +21,7 @@ bool kvmi_hypercall_event(struct kvm_vcpu *vcpu);bool kvmi_queue_exception (struct kvm_vcpu *vcpu);void kvmi_trap_event (struct kvm_vcpu *vcpu);bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u8 descriptor,u8 write); +bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn);bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu);void kvmi_stop_ss (struct kvm_vcpu *vcpu);
@@ -36,6 +37,8 @@ static inline void kvmi_uninit(void) { }static inline void kvmi_create_vm (struct kvm *kvm) { }static inline void kvmi_destroy_vm (struct kvm *kvm) { }static inline int kvmi_vcpu_init (struct kvm_vcpu *vcpu) {return 0 ; } +static inline bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn) + {return false; }static inline bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva,u8 insn_len) {return true; }
virt/kvm/kvmi.c +29 -2
@@ -193,6 +193,33 @@ static bool kvmi_restricted_access(struct kvmi *ikvm, gpa_t gpa, u8 access)return false; } +bool is_tracked_gfn (struct kvmi *ikvm,gfn_t gfn) +{ +struct kvmi_mem_access *m; + +read_lock (&ikvm->access_tree_lock); + m =__kvmi_get_gfn_access (ikvm, gfn); +read_unlock (&ikvm->access_tree_lock); + +return !!m; +} + +bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +struct kvmi *ikvm; +bool ret; + + ikvm =kvmi_get (vcpu->kvm); +if (!ikvm) +return false; + + ret =is_tracked_gfn (ikvm, gfn); + +kvmi_put (vcpu->kvm); + +return ret; +} +static void kvmi_clear_mem_access (struct kvm *kvm) {void **slot;
@@ -1681,7 +1708,7 @@ static int write_custom_data_to_page(struct kvm_vcpu *vcpu, gva_t gva,struct page *page;gpa_t gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL );if (gpa ==UNMAPPED_GVA )return -KVM_EINVAL ;
@@ -1738,7 +1765,7 @@ static int restore_backup_data_to_page(struct kvm_vcpu *vcpu, gva_t gva,struct page *page;gpa_t gpa; - gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,NULL ); + gpa =kvm_mmu_gva_to_gpa_system (vcpu, gva,0 ,NULL );if (gpa ==UNMAPPED_GVA )return -KVM_EINVAL ;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 69/92 kvm: x86: keep the page protected if tracked by the introspection tool
In reply to Adalbert LazărThis patch might be obsolete thanks to single-stepping.
--- arch/x86/kvm/x86.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +7 -2
@@ -6311,7 +6311,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2, indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;spin_unlock (&vcpu->kvm->mmu_lock); -if (indirect_shadow_pages) +if (indirect_shadow_pages + && !kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa)))kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
@@ -6322,7 +6323,8 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,* and it failed try to unshadow page and re-enter the * guest to let CPU execute the instruction. */ -kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa)); +if (!kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));/* * If the access faults on its page table, it can not
@@ -6374,6 +6376,9 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt,if (!vcpu->arch.mmu->direct_map) gpa =kvm_mmu_gva_to_gpa_write (vcpu, cr2,NULL ); +if (kvmi_tracked_gfn (vcpu,gpa_to_gfn (gpa))) +return false; +kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));return true;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 70/92 kvm: x86: filter out access rights only when tracked by the introspection tool
In reply to Adalbert LazărIt should complete the commit fd34a9518173 ("kvm: x86: consult the page tracking from kvm_mmu_get_page() and __direct_map()")
--- arch/x86/kvm/mmu.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/mmu.c +3 -0
@@ -2660,6 +2660,9 @@ static void clear_sp_write_flooding_count(u64 *spte)static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn,unsigned int acc) { +if (!kvmi_tracked_gfn (vcpu, gfn)) +return acc; +if (kvm_page_track_is_active (vcpu, gfn,KVM_PAGE_TRACK_PREREAD )) acc &= ~ACC_USER_MASK ; if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_PREWRITE) ||
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 71/92 mm: add support for remote mapping
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
The following two new mm exports are introduced:
* mm_remote_map(struct mm_struct *req_mm, unsigned long req_hva, unsigned long map_hva)
* mm_remote_unmap(unsigned long map_hva)
* mm_remote_reset(void)
* rmap_walk_remote(struct page *page, struct rmap_walk_control *rwc)
This patch allows one process to map into its address space a page from another process. The previous page (if it exists) is dropped. There is no corresponding pair of system calls as this API is meant to be used by the kernel itself only.
The targeted user is the upcoming KVM VM introspection subsystem (KVMI), where an introspector running in its own VM will map pages from the introspected guest in order to eliminate round trips to the host kernel (read/write guest pages).
The flow is as follows: the introspector identifies a guest physical address where some information of interest is located. It creates a one page anonymous mapping with MAP_LOCKED | MAP_POPULATE and calls the kernel via an IOCTL on /dev/kvmmem giving the map virtual address and the guest physical address as arguments. The kernel converts the map va into a physical page (gpa in KVM-speak) and passes it to the host kernel via a hypercall, along with the introspected guest gpa. The host kernel converts the two gpa-s into their appropriate hva-s (host virtual addresses) and makes sure the vma backing up the page belonging to the VM in which the introspector runs, points to the indicated page into the introspected guest. I have not included here the use of the mapping token described in the KVMI documentation.
--- include/linux/page-flags.h | 9 +- include/linux/remote_mapping.h | 167 +++ include/uapi/linux/remote_mapping.h | 18 + mm/Kconfig | 8 + mm/Makefile | 1 + mm/memory-failure.c | 69 +- mm/migrate.c | 9 +- mm/remote_mapping.c | 1834 +++++++++++++++++++++++++++ mm/rmap.c | 13 +- mm/vmscan.c | 3 +- 10 files changed, 2108 insertions(+), 23 deletions(-) create mode 100644 include/linux/remote_mapping.h create mode 100644 include/uapi/linux/remote_mapping.h create mode 100644 mm/remote_mapping.c
include/linux/page-flags.h +8 -1
@@ -417,8 +417,10 @@ PAGEFLAG(Idle, idle, PF_ANY)*/ #define PAGE_MAPPING_ANON 0x1#define PAGE_MAPPING_MOVABLE 0x2 +#define PAGE_MAPPING_REMOTE 0x4#define PAGE_MAPPING_KSM (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE) -#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE) +#define PAGE_MAPPING_FLAGS (PAGE_MAPPING_ANON | PAGE_MAPPING_MOVABLE | \ + PAGE_MAPPING_REMOTE) static __always_inline int PageMappingFlags(struct page *page) {
@@ -431,6 +433,11 @@ static __always_inline int PageAnon(struct page *page)return ((unsigned long )page->mapping &PAGE_MAPPING_ANON ) !=0 ; } +static __always_inline int PageRemote(struct page *page) +{ +return ((unsigned long )page->mapping &PAGE_MAPPING_REMOTE ) !=0 ; +} + static __always_inline int __PageMovable(struct page *page) { return ((unsigned long)page->mapping & PAGE_MAPPING_FLAGS) ==
include/linux/remote_mapping.h new file +167 -0
167 lines not on this page · raw message
include/uapi/linux/remote_mapping.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ + +#ifndef __UAPI_REMOTE_MAPPING_H__ +#define __UAPI_REMOTE_MAPPING_H__ + +#include <linux/types.h> +#include <linux/ioctl.h> + +struct remote_map_request { +__u32 req_pid; +__u64 req_hva; +__u64 map_hva; +}; + +#define REMOTE_MAP _IOW('r', 0x01, struct remote_map_request) +#define REMOTE_UNMAP _IOW('r', 0x02, unsigned long) + +#endif /* __UAPI_REMOTE_MAPPING_H__ */
mm/Kconfig +8 -0
@@ -758,4 +758,12 @@ config GUP_BENCHMARK config ARCH_HAS_PTE_SPECIAL bool +config REMOTE_MAPPING + bool "Remote memory mapping" + depends on MMU && !KSM + default n + help + Allows a given application to map pages of another application in its own + address space. + endmenu
mm/Makefile +1 -0
@@ -99,3 +99,4 @@ obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_HMM) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_REMOTE_MAPPING) += remote_mapping.o
mm/memory-failure.c +56 -13
105 lines not on this page · raw message
mm/migrate.c +5 -4
@@ -47,6 +47,7 @@#include <linux/page_owner.h> #include <linux/sched/mm.h> #include <linux/ptrace.h> +#include <linux/remote_mapping.h> #include <asm/tlbflush.h>
@@ -215,7 +216,7 @@ static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,VM_BUG_ON_PAGE (PageTail (page), page);while (page_vma_mapped_walk (&pvmw)) { -if (PageKsm (page)) +if (PageKsm (page) ||PageRemote (page)) new = page;else new = page - pvmw.page->index +
@@ -1065,7 +1066,7 @@ static int __unmap_and_move(struct page *page, struct page *newpage,* because that implies that the anon page is no longer mapped * (and cannot be remapped so long as we hold the page lock). */ -if (PageAnon (page) && !PageKsm (page)) +if (PageAnon (page) && !PageKsm (page) && !PageRemote (page)) anon_vma =page_get_anon_vma (page);/*
@@ -1104,8 +1105,8 @@ static int __unmap_and_move(struct page *page, struct page *newpage, } }else if (page_mapped (page)) {/* Establish migration ptes */ -VM_BUG_ON_PAGE (PageAnon (page) && !PageKsm (page) && !anon_vma, - page); +VM_BUG_ON_PAGE (PageAnon (page) && !PageKsm (page) && + !PageRemote (page) && !anon_vma, page);try_to_unmap (page,TTU_MIGRATION |TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS ); page_was_mapped =1 ;
mm/remote_mapping.c new file +1834 -0
1,834 lines not on this page · raw message
mm/rmap.c +9 -4
@@ -65,6 +65,7 @@#include <linux/page_idle.h> #include <linux/memremap.h> #include <linux/userfaultfd_k.h> +#include <linux/remote_mapping.h> #include <asm/tlbflush.h>
@@ -856,7 +857,7 @@ int page_referenced(struct page *page,if (!page_rmapping (page))return 0 ; -if (!is_locked && (!PageAnon (page) ||PageKsm (page))) { +if (!is_locked && (!PageAnon (page) ||PageKsm (page) ||PageRemote (page))) { we_locked =trylock_page (page);if (!we_locked)return 1 ;
@@ -1021,7 +1022,7 @@ void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)* __page_set_anon_rmap - set up new anonymous rmap * @page: Page or Hugepage to add to rmap * @vma: VM area to add page to. -* @address: User virtual address of the mapping +* @address: User virtual address of the mapping * @exclusive: the page is exclusively owned by the current process */ static void __page_set_anon_rmap(struct page *page,
@@ -1125,7 +1126,8 @@ void do_page_add_anon_rmap(struct page *page,__inc_node_page_state (page,NR_ANON_THPS );__mod_node_page_state (page_pgdat (page),NR_ANON_MAPPED , nr); } -if (unlikely (PageKsm (page))) + +if (unlikely (PageKsm (page) ||PageRemote (page)))return ;VM_BUG_ON_PAGE (!PageLocked (page), page);
@@ -1897,6 +1899,8 @@ void rmap_walk(struct page *page, struct rmap_walk_control *rwc) {if (unlikely (PageKsm (page)))rmap_walk_ksm (page, rwc); +else if (unlikely (PageRemote (page))) +rmap_walk_remote (page, rwc);else if (PageAnon (page))rmap_walk_anon (page, rwc, false);else
@@ -1906,8 +1910,9 @@ void rmap_walk(struct page *page, struct rmap_walk_control *rwc)/* Like rmap_walk, but caller holds relevant rmap lock */ void rmap_walk_locked (struct page *page,struct rmap_walk_control *rwc) { -/* no ksm support for now */ +/* no ksm/remote support for now */ VM_BUG_ON_PAGE (PageKsm (page), page); +VM_BUG_ON_PAGE (PageRemote (page), page);if (PageAnon (page))rmap_walk_anon (page, rwc, true);else
mm/vmscan.c +2 -1
@@ -4277,7 +4277,8 @@ int page_evictable(struct page *page)/* Prevent address_space of inode and swap cache from being freed */ rcu_read_lock (); - ret = !mapping_unevictable (page_mapping (page)) && !PageMlocked (page); + ret = !mapping_unevictable (page_mapping (page)) && + !PageMlocked (page) && !PageRemote (page);rcu_read_unlock ();return ret; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 72/92 kvm: introspection: add memory map/unmap support on the guest side
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
An introspection tool running in a dedicated VM can use the new device (/dev/kvmmem) to map memory from other introspected VM-s.
Two ioctl operations are supported:
- KVM_HC_MEM_MAP/struct kvmi_mem_map
- KVM_HC_MEM_UNMAP/unsigned long
In order to map an introspected gpa to the local gva, the process using this device needs to obtain a token from the host KVMI subsystem (see Documentation/virtual/kvm/kvmi.rst - KVMI_GET_MAP_TOKEN).
Both operations use hypercalls (KVM_HC_MEM_MAP, KVM_HC_MEM_UNMAP) to pass the requests to the host kernel/KVMi (see hypercalls.txt).
--- Documentation/virtual/kvm/hypercalls.txt | 34 ++ arch/x86/Kconfig | 9 + arch/x86/include/asm/kvmi_guest.h | 10 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/kvmi_mem_guest.c | 26 + include/uapi/linux/kvm_para.h | 2 + include/uapi/linux/kvmi.h | 21 + virt/kvm/kvmi_mem_guest.c | 651 +++++++++++++++++++++++ 8 files changed, 754 insertions(+) create mode 100644 arch/x86/include/asm/kvmi_guest.h create mode 100644 arch/x86/kernel/kvmi_mem_guest.c create mode 100644 virt/kvm/kvmi_mem_guest.c
Documentation/virtual/kvm/hypercalls.txt +34 -0
@@ -173,3 +173,37 @@ The following registers are clobbered: In particular, for KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT, the last two registers can be poisoned deliberately and cannot be used for passing information. + +9. KVM_HC_MEM_MAP +----------------- + +Architecture: x86 +Status: active +Purpose: Map a guest physical page to another VM (the introspector). +Usage: + +a0: pointer to a token obtained with a KVMI_GET_MAP_TOKEN command (see kvmi.rst) + struct kvmi_map_mem_token { + __u64 token[4]; + }; + +a1: guest physical address to be mapped + +a2: guest physical address from introspector that will be replaced + +Both guest physical addresses will end up poiting to the same physical page. + +Returns any error that the memory manager can return. + +10. KVM_HC_MEM_UNMAP +------------------- + +Architecture: x86 +Status: active +Purpose: Unmap a previously mapped page. +Usage: + +a0: guest physical address from introspector + +The address will stop pointing to the introspected page and a new physical +page is allocated for this gpa.
arch/x86/Kconfig +9 -0
@@ -820,6 +820,15 @@ config KVM_DEBUG_FS Statistics are displayed in debugfs filesystem. Enabling this option may incur significant overhead. +config KVM_INTROSPECTION_GUEST + bool "KVM Memory Introspection support on Guest" + depends on KVM_GUEST + default n + help + This option enables functions and hypercalls for security applications + running in a separate VM to control the execution of other VM-s, query + the state of the vCPU-s (GPR-s, MSR-s etc.). + config PARAVIRT_TIME_ACCOUNTING bool "Paravirtual steal time accounting" depends on PARAVIRT
arch/x86/include/asm/kvmi_guest.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_GUEST_H__ +#define __KVMI_GUEST_H__ + +long kvmi_arch_map_hc (struct kvmi_map_mem_token *tknp, +gpa_t req_gpa,gpa_t map_gpa); +long kvmi_arch_unmap_hc (gpa_t map_gpa); + + +#endif
arch/x86/kernel/Makefile +1 -0
@@ -116,6 +116,7 @@ obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_X86_PMEM_LEGACY_DEVICE) += pmem.o +obj-$(CONFIG_KVM_INTROSPECTION_GUEST) += kvmi_mem_guest.o ../../../virt/kvm/kvmi_mem_guest.o obj-$(CONFIG_JAILHOUSE_GUEST) += jailhouse.o
arch/x86/kernel/kvmi_mem_guest.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection guest implementation +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +* Author: +* Mircea Cirjaliu <[email protected]> +*/ + +#include <uapi/linux/kvmi.h> +#include <uapi/linux/kvm_para.h> +#include <linux/kvm_types.h> +#include <asm/kvm_para.h> + +long kvmi_arch_map_hc (struct kvmi_map_mem_token *tknp, +gpa_t req_gpa,gpa_t map_gpa) +{ +return kvm_hypercall3 (KVM_HC_MEM_MAP , (unsigned long )tknp, + req_gpa, map_gpa); +} + +long kvmi_arch_unmap_hc (gpa_t map_gpa) +{ +return kvm_hypercall1 (KVM_HC_MEM_UNMAP , map_gpa); +}
include/uapi/linux/kvm_para.h +2 -0
@@ -33,6 +33,8 @@#define KVM_HC_CLOCK_PAIRING 9#define KVM_HC_SEND_IPI 10 +#define KVM_HC_MEM_MAP 32 +#define KVM_HC_MEM_UNMAP 33#define KVM_HC_XEN_HVM_OP 34/* Xen's __HYPERVISOR_hvm_op */ /*
include/uapi/linux/kvmi.h +21 -0
@@ -262,4 +262,25 @@ struct kvmi_event_breakpoint { __u8 padding[7]; }; +struct kvmi_map_mem_token { +__u64 token[4 ]; +}; + +struct kvmi_get_map_token_reply { +struct kvmi_map_mem_token token; +}; + +/* Map other guest's gpa to local gva */ +struct kvmi_mem_map { +struct kvmi_map_mem_token token; +__u64 gpa; +__u64 gva; +}; + +/* +* ioctls for /dev/kvmmem +*/ +#define KVM_INTRO_MEM_MAP _IOW('i', 0x01, struct kvmi_mem_map) +#define KVM_INTRO_MEM_UNMAP _IOW('i', 0x02, unsigned long) +#endif /* _UAPI__LINUX_KVMI_H */
virt/kvm/kvmi_mem_guest.c new file +651 -0
651 lines not on this page · raw message
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 73/92 kvm: introspection: use remote mapping
In reply to Adalbert LazărFrom: Mircea Cîrjaliu <[email protected]>
This commit adds the missing KVMI_GET_MAP_TOKEN command and handle the hypercalls used to map/unmap guest pages.
--- Documentation/virtual/kvm/kvmi.rst | 39 ++++ arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/x86.c | 6 + include/linux/kvmi.h | 3 + virt/kvm/kvmi.c | 12 +- virt/kvm/kvmi_int.h | 10 + virt/kvm/kvmi_mem.c | 319 +++++++++++++++++++++++++++++ virt/kvm/kvmi_msg.c | 15 ++ 8 files changed, 404 insertions(+), 2 deletions(-) create mode 100644 virt/kvm/kvmi_mem.c
Documentation/virtual/kvm/kvmi.rst +39 -0
@@ -1144,6 +1144,45 @@ Returns the guest memory type for a specific physical address. * -KVM_EINVAL - padding is not zero * -KVM_EAGAIN - the selected vCPU can't be introspected yet +25. KVMI_GET_MAP_TOKEN +---------------------- + +:Architecture: all +:Versions: >= 1 +:Parameters: none +:Returns: + +:: + + struct kvmi_error_code; + struct kvmi_get_map_token_reply { + struct kvmi_map_mem_token token; + }; + +Where:: + + struct kvmi_map_mem_token { + __u64 token[4]; + }; + +Requests a token for a memory map operation. + +On this command, the host generates a random token to be used (once) +to map a physical page from the introspected guest. The introspector +could use the token with the KVM_INTRO_MEM_MAP ioctl (on /dev/kvmmem) +to map a guest physical page to one of its memory pages. The ioctl, +in turn, will use the KVM_HC_MEM_MAP hypercall (see hypercalls.txt). + +The guest kernel exposing /dev/kvmmem keeps a list with all the mappings +(to all the guests introspected by the tool) in order to unmap them +(using the KVM_HC_MEM_UNMAP hypercall) when /dev/kvmmem is closed or on +demand (using the KVM_INTRO_MEM_UNMAP ioctl). + +:Errors: + +* -KVM_EAGAIN - too many tokens have accumulated +* -KVM_ENOMEM - not enough memory to allocate a new token + Events ======
arch/x86/kvm/Makefile +1 -1
@@ -7,7 +7,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o -kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o kvmi.o +kvm-$(CONFIG_KVM_INTROSPECTION) += $(KVM)/kvmi.o $(KVM)/kvmi_msg.o $(KVM)/kvmi_mem.o kvmi.o kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
arch/x86/kvm/x86.c +6 -0
@@ -7337,6 +7337,12 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret =kvm_pv_send_ipi (vcpu->kvm, a0, a1, a2, a3, op_64_bit);break ;#ifdef CONFIG_KVM_INTROSPECTION +case KVM_HC_MEM_MAP : + ret =kvmi_host_mem_map (vcpu, (gva_t )a0, (gpa_t )a1, (gpa_t )a2); +break ; +case KVM_HC_MEM_UNMAP : + ret =kvmi_host_mem_unmap (vcpu, (gpa_t )a0); +break ;case KVM_HC_XEN_HVM_OP : ret =0 ;if (!kvmi_hypercall_event (vcpu))
include/linux/kvmi.h +3 -0
@@ -24,6 +24,9 @@ bool kvmi_descriptor_event(struct kvm_vcpu *vcpu, u8 descriptor, u8 write);bool kvmi_tracked_gfn (struct kvm_vcpu *vcpu,gfn_t gfn);bool kvmi_single_step (struct kvm_vcpu *vcpu,gpa_t gpa,int *emulation_type);void kvmi_handle_requests (struct kvm_vcpu *vcpu); +int kvmi_host_mem_map (struct kvm_vcpu *vcpu,gva_t tkn_gva, +gpa_t req_gpa,gpa_t map_gpa); +int kvmi_host_mem_unmap (struct kvm_vcpu *vcpu,gpa_t map_gpa);void kvmi_stop_ss (struct kvm_vcpu *vcpu);bool kvmi_vcpu_enabled_ss (struct kvm_vcpu *vcpu);void kvmi_init_emulate (struct kvm_vcpu *vcpu);
virt/kvm/kvmi.c +11 -1
@@ -10,6 +10,7 @@#include "kvmi_int.h" #include <linux/kthread.h> #include <linux/bitmap.h> +#include <linux/remote_mapping.h> #define MAX_PAUSE_REQUESTS 1001
@@ -320,11 +321,13 @@ static int kvmi_cache_create(void)int kvmi_init (void ) { +kvmi_mem_init ();return kvmi_cache_create (); }void kvmi_uninit (void ) { +kvmi_mem_exit ();kvmi_cache_destroy (); }
@@ -1647,6 +1650,11 @@ int kvmi_cmd_write_physical(struct kvm *kvm, u64 gpa, u64 size, const void *buf)return 0 ; } +int kvmi_cmd_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token) +{ +return kvmi_mem_generate_token (kvm, token); +} +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,unsigned int event_id,bool enable) {
@@ -2015,7 +2023,9 @@ int kvmi_ioctl_unhook(struct kvm *kvm, bool force_reset)if (!ikvm)return -EFAULT ; -if (!force_reset && !kvmi_unhook_event (kvm)) +if (force_reset) +mm_remote_reset (); +else if (!kvmi_unhook_event (kvm)) err = -ENOENT ;kvmi_put (kvm);
virt/kvm/kvmi_int.h +10 -0
@@ -148,6 +148,8 @@ struct kvmi {struct task_struct *recv;atomic_t ev_seq; +atomic_t num_tokens; +uuid_t uuid;DECLARE_BITMAP (cmd_allow_mask,KVMI_NUM_COMMANDS );
@@ -229,7 +231,9 @@ int kvmi_cmd_control_events(struct kvm_vcpu *vcpu, unsigned int event_id, bool enable);int kvmi_cmd_control_vm_events (struct kvmi *ikvm,unsigned int event_id,bool enable); +int kvmi_cmd_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token);int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu,bool wait); +unsigned long gfn_to_hva_safe (struct kvm *kvm,gfn_t gfn);struct kvmi *__must_check kvmi_get (struct kvm *kvm);void kvmi_put (struct kvm *kvm);int kvmi_run_jobs_and_wait (struct kvm_vcpu *vcpu);
@@ -298,4 +302,10 @@ int kvmi_arch_cmd_control_msr(struct kvm_vcpu *vcpu, const struct kvmi_control_msr *req);int kvmi_arch_cmd_get_mtrr_type (struct kvm_vcpu *vcpu,u64 gpa,u8 *type); +/* kvmi_mem.c */ +void kvmi_mem_init (void ); +void kvmi_mem_exit (void ); +int kvmi_mem_generate_token (struct kvm *kvm,struct kvmi_map_mem_token *token); +void kvmi_clear_vm_tokens (struct kvm *kvm); +#endif
virt/kvm/kvmi_mem.c new file +319 -0
319 lines not on this page · raw message
virt/kvm/kvmi_msg.c +15 -0
@@ -33,6 +33,7 @@ static const char *const msg_IDs[] = { [KVMI_EVENT_REPLY] = "KVMI_EVENT_REPLY", [KVMI_GET_CPUID] = "KVMI_GET_CPUID", [KVMI_GET_GUEST_INFO] = "KVMI_GET_GUEST_INFO", + [KVMI_GET_MAP_TOKEN] = "KVMI_GET_MAP_TOKEN", [KVMI_GET_MTRR_TYPE] = "KVMI_GET_MTRR_TYPE", [KVMI_GET_PAGE_ACCESS] = "KVMI_GET_PAGE_ACCESS", [KVMI_GET_PAGE_WRITE_BITMAP] = "KVMI_GET_PAGE_WRITE_BITMAP",
@@ -352,6 +353,19 @@ static int handle_write_physical(struct kvmi *ikvm,return kvmi_msg_vm_maybe_reply (ikvm, msg, ec,NULL ,0 ); } +static int handle_get_map_token (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +struct kvmi_get_map_token_reply rpl; +int ec; + +memset (&rpl,0 ,sizeof (rpl)); + ec =kvmi_cmd_alloc_token (ikvm->kvm, &rpl.token); + +return kvmi_msg_vm_maybe_reply (ikvm, msg, ec, &rpl,sizeof (rpl)); +} +static bool enable_spp (struct kvmi *ikvm) {if (!ikvm->spp.initialized) {
@@ -524,6 +538,7 @@ static int(*const msg_vm[])(struct kvmi *, const struct kvmi_msg_hdr *, [KVMI_CONTROL_SPP] = handle_control_spp, [KVMI_CONTROL_VM_EVENTS] = handle_control_vm_events, [KVMI_GET_GUEST_INFO] = handle_get_guest_info, + [KVMI_GET_MAP_TOKEN] = handle_get_map_token, [KVMI_GET_PAGE_ACCESS] = handle_get_page_access, [KVMI_GET_PAGE_WRITE_BITMAP] = handle_get_page_write_bitmap, [KVMI_GET_VERSION] = handle_get_version,
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 74/92 kvm: x86: do not unconditionally patch the hypercall instruction during emulation
In reply to Adalbert LazărFrom: Mihai Donțu <[email protected]>
It can happened for us to end up emulating the VMCALL instruction as a result of the handling of an EPT write fault. In this situation, the emulator will try to unconditionally patch the correct hypercall opcode bytes using emulator_write_emulated(). However, this last call uses the fault GPA (if available) or walks the guest page tables at RIP, otherwise. The trouble begins when using KVMI, when we forbid the use of the fault GPA and fallback to the guest pt walk: in Windows (8.1 and newer) the page that we try to write into is marked read-execute and as such emulator_write_emulated() fails and we inject a write #PF, leading to a guest crash.
The fix is rather simple: check the existing instruction bytes before doing the patching. This does not change the normal KVM behaviour, but does help when using KVMI as we no longer inject a write #PF.
--- arch/x86/kvm/x86.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +20 -3
@@ -7363,16 +7363,33 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_hypercall); +#define KVM_HYPERCALL_INSN_LEN 3 +static int emulator_fix_hypercall (struct x86_emulate_ctxt *ctxt) { +int err;struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -char instruction[3 ]; +char buf[KVM_HYPERCALL_INSN_LEN ]; +char instruction[KVM_HYPERCALL_INSN_LEN ];unsigned long rip =kvm_rip_read (vcpu); + err =emulator_read_emulated (ctxt, rip, buf,sizeof (buf), + &ctxt->exception); +if (err !=X86EMUL_CONTINUE ) +return err; + kvm_x86_ops->patch_hypercall (vcpu, instruction); +if (!memcmp (instruction, buf,sizeof (instruction))) +/* +* The hypercall instruction is the correct one. Retry +* its execution maybe we got here as a result of an +* event other than #UD which has been resolved in the +* mean time. +*/ +return X86EMUL_CONTINUE ; -return emulator_write_emulated (ctxt, rip, instruction,3 , - &ctxt->exception); +return emulator_write_emulated (ctxt, rip, instruction, +sizeof (instruction), &ctxt->exception); }static int dm_request_for_irq_injection (struct kvm_vcpu *vcpu)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 75/92 kvm: x86: disable gpa_available optimization in emulator_read_write_onepage()
In reply to Adalbert LazărIf the EPT violation was caused by an execute restriction imposed by the introspection tool, gpa_available will point to the instruction pointer, not the to the read/write location that has to be used to emulate the current instruction.
This optimization should be disabled only when the VM is introspected, not just because the introspection subsystem is present.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -5532,7 +5532,7 @@ static int emulator_read_write_onepage(unsigned long addr, void *val,* operation using rep will only have the initial GPA from the NPF * occurred. */ -if (vcpu->arch.gpa_available && +if (vcpu->arch.gpa_available && !kvmi_is_present () &&emulator_can_use_gpa (ctxt) && (addr & ~PAGE_MASK ) == (vcpu->arch.gpa_val & ~PAGE_MASK )) { gpa = vcpu->arch.gpa_val;
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
RFC PATCH v6 76/92 kvm: x86: disable EPT A/D bits if introspection is present
In reply to Adalbert Lazăr--- arch/x86/kvm/vmx/vmx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -7718,7 +7718,7 @@ static __init int hardware_setup(void) !cpu_has_vmx_invept_global()) enable_ept =0 ; -if (!cpu_has_vmx_ept_ad_bits () || !enable_ept) +if (!cpu_has_vmx_ept_ad_bits () || !enable_ept ||kvmi_is_present ()) enable_ept_ad_bits =0 ;if (!cpu_has_vmx_unrestricted_guest () || !enable_ept)
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization