VM introspection
Subsystems (8)
v4 cover VM introspection
From: Adalbert Lazar <[email protected]>
This patch series proposes a VM introspection subsystem for KVM (KVMI).
The previous RFC can be read here: https://marc.info/?l=kvm&m=150514457912721
These patches were tested on kvm/master,
commit 43aabca38aa9668eee3c3c1206207034614c0901 (Merge tag 'kvm-arm-fixes-for-v4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD).
In this iteration we refactored the code based on the feedback received from Paolo and others.
The handshake
-------------
We no longer listen on a vsock in kernel, accepting introspectors to control all the other VM-s. Instead, QEMU (ie. every introspected guest) initiates the connection with an introspection tool (running on the same host, in another VM, etc.) and passes the control to KVM where the in-kernel mechanism will take over.
The administrator has to choose which guests should be introspected, by which introspectors, what commands and events are allowed and for which guests. Currently, there is a bitmask for allowed commands/events, but it seems to be too complicated. For example, being allowed to set page accesses (eg. r--) and not being allowed to receive page fault events (eg. -wx) doesn't make sense.
The memory maping
-----------------
Besides the read/write commands to access guest memory, for performance reasons, we've implemented memory mapping for introspection tools running in another guest (on the same host, like page sharing between guests, but without copy-on-write): the KVMI_GET_TOKEN command is used to obtain a token, which is passed with a hypercall from the introspecting guest to the KVMI.
While this didn't had a high priority, somehow the stars aligned and we have it.
Page tracking
-------------
The current page tracking mechanism from KVM has support to track write accesses (after the write operation took place). We've extended it with preread, prewrite and preexec tracking.
We also added a notification for when a new memory slot is being created (see track_create_slot()).
Pause VM
--------
We've removed the commands to pause/resume VM. Having a "pause vCPU" command and a "paused vCPU" event seems to be enough for now.
Not implemented yet
-------------------
There are a few things documented, but not implemented yet: virtualized exceptions, single-stepping and EPT views.
We are also working on accomodating SPP (Sub Page Protection).
We hope to make public our repositories (kernel, QEMU, userland/simple-introspector) in a couple of days and we're looking forward to add unit tests.
Changes since v3:
- move the accept/handshake worker to QEMU
- extend and use the 'page_track' infrastructure to intercept page accesses during emulation
- remove the 0x40000000-0x40001fff range from monitored MSR-s
- make small changes to the wire protocol (error codes, padding, names)
- simplify KVMI_PAUSE_VCPU
- add new commands: KVMI_GET_MAP_TOKEN, KVMI_GET_XSAVE
- add pat to KVMI_EVENT
- document KVM_HC_MEM_MAP and KVM_HC_MEM_UNMAP hypercalls
Changes since v2:
- make small changes to the wire protocol (eg. use kvmi_error_code with every command reply, a few renames, etc.)
- removed '_x86' from x86 specific structure names. Architecture specific structures will have the same name.
- drop KVMI_GET_MTRR_TYPE and KVMI_GET_MTRRS (use KVMI_SET_REGISTERS)
- drop KVMI_EVENT_ACTION_SET_REGS (use KVMI_SET_REGISTERS)
- remove KVMI_MAP_PHYSICAL_PAGE_TO_GUEST and KVMI_UNMAP_PHYSICAL_PAGE_FROM_GUEST (to be replaced by a token+hypercall pair)
- extend KVMI_GET_VERSION with allowed commnd/event masks
- replace KVMI_PAUSE_GUEST/KVMI_UNPAUSE_GUEST with KVMI_PAUSE_VCPU
- replace KVMI_SHUTDOWN_GUEST with KVMI_EVENT_ACTION_CRASH
- replace KVMI_GET_XSAVE_INFO with KVMI_GET_CPUID
- merge KVMI_INJECT_PAGE_FAULT and KVMI_INJECT_BREAKPOINT
in KVMI_INJECT_EXCEPTION
- replace event reply flags with ALLOW/SKIP/RETRY/CRASH actions
- make KVMI_SET_REGISTERS work with vCPU events only
- add EPT view support in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add support for multiple pages in KVMI_GET_PAGE_ACCESS/KVMI_SET_PAGE_ACCESS
- add (back) KVMI_READ_PHYSICAL/KVMI_WRITE_PHYSICAL
- add KVMI_CONTROL_VE
- add cstar to KVMI_EVENT
- add new events: KVMI_EVENT_VCPU_PAUSED, KVMI_EVENT_CREATE_VCPU, KVMI_EVENT_DESCRIPTOR_ACCESS, KVMI_EVENT_SINGLESTEP
- add new sections: "Introspection capabilities", "Live migrations", "Guest snapshots with memory", "Memory access safety"
- document the hypercall used by the KVMI_EVENT_HYPERCALL command (was KVMI_EVENT_USER_CALL)
Changes since v1:
- add documentation and ABI [Paolo, Jan]
- drop all the other patches for now [Paolo]
- remove KVMI_GET_GUESTS, KVMI_EVENT_GUEST_ON, KVMI_EVENT_GUEST_OFF, and let libvirt/qemu handle this [Stefan, Paolo]
- change the license from LGPL to GPL [Jan]
- remove KVMI_READ_PHYSICAL and KVMI_WRITE_PHYSICAL (not used anymore)
- make the interface a little more consistent
Adalbert Lazar (18):
kvm: add documentation and ABI/API headers for the VM introspection
subsystem
add memory map/unmap support for VM introspection on the guest side
kvm: x86: add kvm_arch_msr_intercept()
kvm: x86: add kvm_mmu_nested_guest_page_fault() and
kvmi_mmu_fault_gla()
kvm: x86: add kvm_arch_vcpu_set_regs()
kvm: vmx: export the availability of EPT views
kvm: page track: add support for preread, prewrite and preexec
kvm: add the VM introspection subsystem
kvm: hook in the VM introspection subsystem
kvm: x86: handle the new vCPU request (KVM_REQ_INTROSPECTION)
kvm: x86: hook in the page tracking
kvm: x86: hook in kvmi_breakpoint_event()
kvm: x86: hook in kvmi_descriptor_event()
kvm: x86: hook in kvmi_cr_event()
kvm: x86: hook in kvmi_xsetbv_event()
kvm: x86: hook in kvmi_msr_event()
kvm: x86: handle the introspection hypercalls
kvm: x86: hook in kvmi_trap_event()
Documentation/virtual/kvm/00-INDEX | 2 + Documentation/virtual/kvm/hypercalls.txt | 66 ++ Documentation/virtual/kvm/kvmi.rst | 1323 ++++++++++++++++++++++++++++ arch/x86/Kconfig | 9 + arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/include/asm/kvm_host.h | 13 + arch/x86/include/asm/kvm_page_track.h | 24 +- arch/x86/include/asm/kvmi_guest.h | 10 + arch/x86/include/asm/vmx.h | 2 + arch/x86/include/uapi/asm/kvmi.h | 213 +++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/kvmi_mem_guest.c | 26 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/emulate.c | 9 +- arch/x86/kvm/mmu.c | 156 +++- arch/x86/kvm/mmu.h | 4 + arch/x86/kvm/page_track.c | 129 ++- arch/x86/kvm/svm.c | 66 ++ arch/x86/kvm/vmx.c | 109 ++- arch/x86/kvm/x86.c | 141 ++- include/linux/kvm_host.h | 5 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 ++++ include/uapi/linux/kvm.h | 8 + include/uapi/linux/kvm_para.h | 10 +- include/uapi/linux/kvmi.h | 150 ++++ mm/internal.h | 5 - virt/kvm/kvm_main.c | 19 + virt/kvm/kvmi.c | 1410 ++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 +++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++ virt/kvm/kvmi_mem_guest.c | 379 ++++++++ virt/kvm/kvmi_msg.c | 1134 ++++++++++++++++++++++++ 34 files changed, 6438 insertions(+), 47 deletions(-) create mode 100644 Documentation/virtual/kvm/kvmi.rst create mode 100644 arch/x86/include/asm/kvmi_guest.h create mode 100644 arch/x86/include/uapi/asm/kvmi.h create mode 100644 arch/x86/kernel/kvmi_mem_guest.c create mode 100644 include/linux/kvmi.h create mode 100644 include/trace/events/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 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
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 1/18 kvm: add documentation and ABI/API headers for the VM introspection subsystem
From: Adalbert Lazar <[email protected]>
This includes the new hypercalls and KVM_xxx return values.
--- Documentation/virtual/kvm/00-INDEX | 2 + Documentation/virtual/kvm/hypercalls.txt | 66 ++ Documentation/virtual/kvm/kvmi.rst | 1323 ++++++++++++++++++++++++++++++ arch/x86/include/uapi/asm/kvmi.h | 213 +++++ include/uapi/linux/kvm_para.h | 10 +- include/uapi/linux/kvmi.h | 150 ++++ 6 files changed, 1763 insertions(+), 1 deletion(-) create mode 100644 Documentation/virtual/kvm/kvmi.rst create mode 100644 arch/x86/include/uapi/asm/kvmi.h create mode 100644 include/uapi/linux/kvmi.h
Documentation/virtual/kvm/00-INDEX +2 -0
@@ -10,6 +10,8 @@ halt-polling.txt - notes on halt-polling hypercalls.txt - KVM hypercalls. +kvmi.rst + - VM introspection. locking.txt - notes on KVM locks. mmu.txt
Documentation/virtual/kvm/hypercalls.txt +66 -0
69 lines not on this page · raw message
Documentation/virtual/kvm/kvmi.rst new file +1323 -0
1,323 lines not on this page · raw message
arch/x86/include/uapi/asm/kvmi.h new file +213 -0
213 lines not on this page · raw message
include/uapi/linux/kvm_para.h +9 -1
@@ -10,12 +10,17 @@* - kvm_para_available */ -/* Return values for hypercalls */ +/* Return values for hypercalls and VM introspection */ #define KVM_ENOSYS 1000#define KVM_EFAULT EFAULT#define KVM_E2BIG E2BIG#define KVM_EPERM EPERM#define KVM_EOPNOTSUPP 95 +#define KVM_EAGAIN 11 +#define KVM_EBUSY EBUSY +#define KVM_EINVAL EINVAL +#define KVM_ENOENT ENOENT +#define KVM_ENOMEM ENOMEM#define KVM_HC_VAPIC_POLL_IRQ 1#define KVM_HC_MMU_OP 2
@@ -26,6 +31,9 @@#define KVM_HC_MIPS_EXIT_VM 7#define KVM_HC_MIPS_CONSOLE_OUTPUT 8#define KVM_HC_CLOCK_PAIRING 9 +#define KVM_HC_MEM_MAP 32 +#define KVM_HC_MEM_UNMAP 33 +#define KVM_HC_XEN_HVM_OP 34/* Xen's __HYPERVISOR_hvm_op */ /* * hypercalls use architecture specific
include/uapi/linux/kvmi.h new file +150 -0
150 lines not on this page · raw message
RFC PATCH v4 2/18 add memory map/unmap support for VM introspection on the guest side
In reply to Adalber LazărFrom: Adalbert Lazar <[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_INTRO_MEM_MAP/struct kvmi_mem_map
- KVM_INTRO_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_INTRO_MEM_UNMAP) to pass the requests to the host kernel/KVMi (see hypercalls.txt).
--- 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 +++ virt/kvm/kvmi_mem_guest.c | 379 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 425 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
arch/x86/Kconfig +9 -0
@@ -782,6 +782,15 @@ config KVM_DEBUG_FS Statistics are displayed in debugfs filesystem. Enabling this option may incur significant overhead. +config KVMI_MEM_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
@@ -111,6 +111,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_KVMI_MEM_GUEST) += kvmi_mem_guest.o ../../../virt/kvm/kvmi_mem_guest.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.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); +}
virt/kvm/kvmi_mem_guest.c new file +379 -0
379 lines not on this page · raw message
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 02/18] add memory map/unmap support for VM introspection on the guest side
Quoted 165 lines · Adalber Lazăr
From: Adalbert Lazar <[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_INTRO_MEM_MAP/structkvmi_mem_map
- KVM_INTRO_MEM_UNMAP/unsigned longIn 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_INTRO_MEM_UNMAP) to pass the requests to the host kernel/KVMi (see hypercalls.txt).Signed-off-by Mircea Cîrjaliu <[email protected]>--- 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 +++ virt/kvm/kvmi_mem_guest.c | 379 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 425 insertions(+)create mode 100644
arch/x86/include/asm/kvmi_guest.hcreate mode 100644arch/x86/kernel/kvmi_mem_guest.ccreate mode 100644virt/kvm/kvmi_mem_guest.carch/x86/Kconfig +9
-0@@ -782,6 +782,15 @@ config KVM_DEBUG_FS Statistics are displayed in debugfs filesystem. Enabling this option may incur significant overhead. +config KVMI_MEM_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 PARAVIRTarch/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@@ -111,6 +111,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_KVMI_MEM_GUEST) += kvmi_mem_guest.o ../../../virt/kvm/kvmi_mem_guest.o obj-$(CONFIG_EISA) += eisa.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.oarch/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); +}virt/kvm/kvmi_mem_guest.c new file +51
-0@@ -0,0 +1,379 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection guest implementation +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +* Author: +* Mircea Cirjaliu <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/miscdevice.h> +#include <linux/fs.h> +#include <linux/mm.h> +#include <linux/mman.h> +#include <linux/types.h> +#include <linux/kvm_host.h> +#include <linux/kvm_para.h> +#include <linux/uaccess.h> +#include <linux/slab.h> +#include <linux/rmap.h> +#include <linux/sched.h> +#include <linux/list.h> +#include <linux/spinlock.h> +#include <linux/mutex.h> +#include <uapi/linux/kvmi.h> +#include <asm/kvmi_guest.h> + +#define ASSERT (exp) BUG_ON(!(exp)) + + +static struct list_head file_list; +static spinlock_t file_lock; + +struct file_map { +struct list_head file_list; +struct file *file; +struct list_head map_list; +struct mutex lock; +int active;/* for tearing down */ +}; + +struct page_map { +struct list_head map_list; +__u64 gpa; +unsigned long vaddr; +unsigned long paddr; +}; +
virt/kvm/kvmi_mem_guest.c line 61
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+ +static int kvm_dev_open (struct inode *inodep,struct file *filp) +{ +struct file_map *fmp; + +pr_debug ("kvmi: file %016lx opened by process %s\n" , + (unsigned long ) filp, current->comm); + +/* link the file 1:1 with such a structure */ + fmp =kmalloc (sizeof (struct file_map ),GFP_KERNEL );
I think this is supposed to be "kmalloc(sizeof(*fmp), GFP_KERNEL)".
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+if (fmp ==NULL ) +return -ENOMEM ; + +INIT_LIST_HEAD (&fmp->file_list); + fmp->file = filp; + filp->private_data = fmp; +INIT_LIST_HEAD (&fmp->map_list); +mutex_init (&fmp->lock); + fmp->active =1 ; +
virt/kvm/kvmi_mem_guest.c line 81
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+/* add the entry to the global list */ +spin_lock (&file_lock); +list_add_tail (&fmp->file_list, &file_list); +spin_unlock (&file_lock); + +return 0 ; +} + +/* actually does the mapping of a page */ +static long _do_mapping (struct kvmi_mem_map *map_req,struct page_map *pmap)
Here you have a "struct page_map" and call it pmap. However, in the rest of the code, whenever there's a "struct page_map" it's called pmp. It seems that it would be good to stay consistent with the naming, so perhaps rename it here to pmp as well?
virt/kvm/kvmi_mem_guest.c
Quoted 3 lines
virt/kvm/kvmi_mem_guest.c new file +3
-0+{ +unsigned long paddr; + struct vm_area_struct *vma = NULL; > + struct page *page;
Out of curiosity, why do you set "*vma = NULL" but not "*page = NULL"?
Quoted 60 lines
virt/kvm/kvmi_mem_guest.c new file +60
-0+long result; + +pr_debug ("kvmi: mapping remote GPA %016llx into %016llx\n" , + map_req->gpa, map_req->gva); + +/* check access to memory location */ +if (!access_ok (VERIFY_READ , map_req->gva,PAGE_SIZE )) { +pr_err ("kvmi: invalid virtual address for mapping\n" ); +return -EINVAL ; + } + +down_read (¤t->mm->mmap_sem); + +/* find the page to be replaced */ + vma =find_vma (current->mm, map_req->gva); +if (IS_ERR_OR_NULL (vma)) { + result =PTR_ERR (vma); +pr_err ("kvmi: find_vma() failed with result %ld\n" , result); + goto out; + } + + page =follow_page (vma, map_req->gva,0 ); +if (IS_ERR_OR_NULL (page)) { + result =PTR_ERR (page); +pr_err ("kvmi: follow_page() failed with result %ld\n" , result); + goto out; + } + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (page,"page to map_req into" ); + +WARN (is_zero_pfn (page_to_pfn (page)),"zero-page still mapped" ); + +/* get the physical address and store it in page_map */ + paddr =page_to_phys (page); +pr_debug ("kvmi: page phys addr %016lx\n" , paddr); + pmap->paddr = paddr; + +/* last thing to do is host mapping */ + result =kvmi_arch_map_hc (&map_req->token, map_req->gpa, paddr); +if (IS_ERR_VALUE (result)) { +pr_err ("kvmi: HC failed with result %ld\n" , result); + goto out; + } + +out: +up_read (¤t->mm->mmap_sem); + +return result; +} + +/* actually does the unmapping of a page */ +static long _do_unmapping (unsigned long paddr) +{ +long result; + +pr_debug ("kvmi: unmapping request for phys addr %016lx\n" , paddr); + +/* local GPA uniquely identifies the mapping on the host */ + result =kvmi_arch_unmap_hc (paddr);
virt/kvm/kvmi_mem_guest.c line 155
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+if (IS_ERR_VALUE (result)) +pr_warn ("kvmi: HC failed with result %ld\n" , result); + +return result; +} + +static long kvm_dev_ioctl_map (struct file_map *fmp,struct kvmi_mem_map *map) +{ +struct page_map *pmp; +long result =0 ;
Out of curiosity again, why do you set "result = 0" here when it's always set before used (and, for e.g., _do_unmapping() doesn't do "result = 0")?
virt/kvm/kvmi_mem_guest.c line 163
Quoted 8 lines
virt/kvm/kvmi_mem_guest.c new file +8
-0+ +if (!access_ok (VERIFY_READ , map->gva,PAGE_SIZE )) +return -EINVAL ; +if (!access_ok (VERIFY_WRITE , map->gva,PAGE_SIZE )) +return -EINVAL ; + +/* prepare list entry */ + pmp =kmalloc (sizeof (struct page_map ),GFP_KERNEL );
This should also probably be "kmalloc(sizeof(*pmp), GFP_KERNEL)".
Quoted 40 lines
virt/kvm/kvmi_mem_guest.c new file +40
-0+if (pmp ==NULL ) +return -ENOMEM ; + +INIT_LIST_HEAD (&pmp->map_list); + pmp->gpa = map->gpa; + pmp->vaddr = map->gva; + +/* acquire the file mapping */ +mutex_lock (&fmp->lock); + +/* check if other thread is closing the file */ +if (!fmp->active) { + result = -ENODEV ; +pr_warn ("kvmi: unable to map, file is being closed\n" ); + goto out_err; + } + +/* do the actual mapping */ + result =_do_mapping (map, pmp); +if (IS_ERR_VALUE (result)) + goto out_err; + +/* link to list */ +list_add_tail (&pmp->map_list, &fmp->map_list); + +/* all fine */ + result =0 ; + goto out_finalize; + +out_err: +kfree (pmp); + +out_finalize: +mutex_unlock (&fmp->lock); + +return result; +} + +static long kvm_dev_ioctl_unmap (struct file_map *fmp,unsigned long vaddr) +{
virt/kvm/kvmi_mem_guest.c line 213
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+struct list_head *cur; +struct page_map *pmp; +bool found = false; + +/* acquire the file */ +mutex_lock (&fmp->lock); + +/* check if other thread is closing the file */ +if (!fmp->active) { +mutex_unlock (&fmp->lock);
Wouldn't this be better replaced with a "goto out_err" like in kvm_dev_ioctl_map()?
Quoted 8 lines
virt/kvm/kvmi_mem_guest.c new file +8
-0+pr_warn ("kvmi: unable to unmap, file is being closed\n" ); +return -ENODEV ; + } + +/* check that this address belongs to us */ +list_for_each (cur, &fmp->map_list) { + pmp = list_entry(cur, struct page_map, map_list); +
virt/kvm/kvmi_mem_guest.c line 231
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+/* found */ +if (pmp->vaddr == vaddr) { + found = true; +break ; + } + } + +/* not found ? */ +if (!found) { +mutex_unlock (&fmp->lock);
Here too: "goto out_err".
virt/kvm/kvmi_mem_guest.c line 238
Quoted 7 lines
virt/kvm/kvmi_mem_guest.c new file +7
-0+pr_err ("kvmi: address %016lx not mapped\n" , vaddr); +return -ENOENT ; + } + +/* decouple guest mapping */ +list_del (&pmp->map_list); +mutex_unlock (&fmp->lock);
In kvm_dev_ioctl_map(), the fmp mutex is held across the _do_mapping() call. Is there any particular reason why here the mutex doesn't need to be held across the _do_unmapping() call? Or was that more an artifact of having a common "out_err" exit in kvm_dev_ioctl_map()?
Quoted 26 lines
virt/kvm/kvmi_mem_guest.c new file +26
-0+ +/* unmap & ignore result */ +_do_unmapping (pmp->paddr); + +/* free guest mapping */ +kfree (pmp); + +return 0 ; +} + +static long kvm_dev_ioctl (struct file *filp, +unsigned int ioctl,unsigned long arg) +{ +void __user *argp = (void __user *) arg; +struct file_map *fmp; +long result; + +/* minor check */ + fmp = filp->private_data; +ASSERT (fmp->file == filp); + +switch (ioctl) { +case KVM_INTRO_MEM_MAP : { +struct kvmi_mem_map map; + + result = -EFAULT ;
virt/kvm/kvmi_mem_guest.c line 274
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+if (copy_from_user (&map, argp,sizeof (map))) +break ; + + result =kvm_dev_ioctl_map (fmp, &map); +if (IS_ERR_VALUE (result)) +break ; + + result =0 ; +break ; + }
Since kvm_dev_ioctl_map() either returns an error or 0, couldn't this just be reduced to:
result = kvm_dev_ioctl_map(fmap, &map); break; }
virt/kvm/kvmi_mem_guest.c line 284
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+case KVM_INTRO_MEM_UNMAP : { +unsigned long vaddr = (unsigned long ) arg; + + result =kvm_dev_ioctl_unmap (fmp, vaddr); +if (IS_ERR_VALUE (result)) +break ; + + result =0 ; +break ; + }
Ditto here.
virt/kvm/kvmi_mem_guest.c line 295
Quoted 11 lines
virt/kvm/kvmi_mem_guest.c new file +11
-0+default : +pr_err ("kvmi: ioctl %d not implemented\n" , ioctl); + result = -ENOTTY ; + } + +return result; +} + +static int kvm_dev_release (struct inode *inodep,struct file *filp) +{ +int result =0 ;
You set "result = 0" here, but result isn't used until the end, and just to return it.
Quoted 28 lines
virt/kvm/kvmi_mem_guest.c new file +28
-0+struct file_map *fmp; +struct list_head *cur, *next; +struct page_map *pmp; + +pr_debug ("kvmi: file %016lx closed by process %s\n" , + (unsigned long ) filp, current->comm); + +/* acquire the file */ + fmp = filp->private_data; +mutex_lock (&fmp->lock); + +/* mark for teardown */ + fmp->active =0 ; + +/* release mappings taken on this instance of the file */ +list_for_each_safe (cur, next, &fmp->map_list) { + pmp = list_entry(cur, struct page_map, map_list); + +/* unmap address */ +_do_unmapping (pmp->paddr); + +/* decouple & free guest mapping */ +list_del (&pmp->map_list); +kfree (pmp); + } + +/* done processing this file mapping */ +mutex_unlock (&fmp->lock);
virt/kvm/kvmi_mem_guest.c line 333
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+ +/* decouple file mapping */ +spin_lock (&file_lock); +list_del (&fmp->file_list); +spin_unlock (&file_lock); + +/* free it */ +kfree (fmp); + +return result;
This is the first time result is used. Couldn't this just be replaced with "return 0"?
virt/kvm/kvmi_mem_guest.c line 342
Quoted 9 lines
virt/kvm/kvmi_mem_guest.c new file +9
-0+} + + +static const struct file_operations kvmmem_ops = { + .open = kvm_dev_open, + .unlocked_ioctl = kvm_dev_ioctl, + .compat_ioctl = kvm_dev_ioctl, + .release = kvm_dev_release, +};
Here you have all the rvals aligned...
virt/kvm/kvmi_mem_guest.c line 348
Quoted 6 lines
virt/kvm/kvmi_mem_guest.c new file +6
-0+ +static struct miscdevice kvm_mem_dev = { + .minor =MISC_DYNAMIC_MINOR , + .name ="kvmmem" , + .fops = &kvmmem_ops, +};
...but here you don't. I'm not sure what the "proper" style is, but I think it should at least just be consistent.
Quoted 22 lines
virt/kvm/kvmi_mem_guest.c new file +21
-0+ +int __init kvm_intro_guest_init (void ) +{ +int result; + +if (!kvm_para_available ()) { +pr_err ("kvmi: paravirt not available\n" ); +return -EPERM ; + } + + result =misc_register (&kvm_mem_dev); +if (result) { +pr_err ("kvmi: misc device register failed: %d\n" , result); +return result; + } + +INIT_LIST_HEAD (&file_list); +spin_lock_init (&file_lock); + +pr_info ("kvmi: guest introspection device created\n" ); +
virt/kvm/kvmi_mem_guest.c line 379
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c new file +10
-0+return 0 ; +} + +void kvm_intro_guest_exit (void ) +{ +misc_deregister (&kvm_mem_dev); +} + +module_init (kvm_intro_guest_init) +module_exit (kvm_intro_guest_exit)
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RE: [RFC PATCH v4 02/18] add memory map/unmap support for VM introspection on the guest side
Rml4ZWQgYWxsIHRoZSBpc3N1ZXMgYWRkcmVzc2VkIGluIHRoZSBvcmlnaW5hbCBjb21tZW50cyAr IG1vcmUuDQpQbGVhc2Ugc2VlIGlubGluZSBhbnN3ZXJzLg0KDQotLS0tLU9yaWdpbmFsIE1lc3Nh Z2UtLS0tLQ0KRnJvbTogUGF0cmljayBDb2xwIFttYWlsdG86cGF0cmljay5jb2xwQG9yYWNsZS5j b21dIA0KU2VudDogVGh1cnNkYXksIDIxIERlY2VtYmVyIDIwMTcgMjM6MTcNClRvOiBBZGFsYmVy IExhesSDciA8YWxhemFyQGJpdGRlZmVuZGVyLmNvbT47IGt2bUB2Z2VyLmtlcm5lbC5vcmcNCkNj OiBsaW51eC1tbUBrdmFjay5vcmc7IFBhb2xvIEJvbnppbmkgPHBib256aW5pQHJlZGhhdC5jb20+ OyBSYWRpbSBLcsSNbcOhxZkgPHJrcmNtYXJAcmVkaGF0LmNvbT47IFhpYW8gR3Vhbmdyb25nIDxn dWFuZ3JvbmcueGlhb0BsaW51eC5pbnRlbC5jb20+OyBNaWhhaSBEb27Im3UgPG1kb250dUBiaXRk ZWZlbmRlci5jb20+OyBNaXJjZWEgQ0lSSkFMSVUtTUVMSVUgPG1jaXJqYWxpdUBiaXRkZWZlbmRl ci5jb20+DQpTdWJqZWN0OiBSZTogW1JGQyBQQVRDSCB2NCAwMi8xOF0gYWRkIG1lbW9yeSBtYXAv dW5tYXAgc3VwcG9ydCBmb3IgVk0gaW50cm9zcGVjdGlvbiBvbiB0aGUgZ3V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5IEJpdGRlZmVuZGVyDQo=
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 02/18] add memory map/unmap support for VM introspection on the guest side
*snip*
Quoted 10 lines
virt/kvm/kvmi_mem_guest.c line 238
Quoted 7 lines
virt/kvm/kvmi_mem_guest.c new file +7
-0+pr_err ("kvmi: address %016lx not mapped\n" , vaddr); +return -ENOENT ; + } + +/* decouple guest mapping */ +list_del (&pmp->map_list); +mutex_unlock (&fmp->lock);In
kvm_dev_ioctl_map(), the fmp mutex is held across the_do_mapping()call. Is there any particular reason why here the mutex doesn't need to be held across the_do_unmapping()call? Or was that more an artifact of having a common "out_err" exit inkvm_dev_ioctl_map()?
Quoted 7 lines
The fmp mutex:
1. protects the fmp list against concurrent access.
2. protects against teardown (one thread tries to do a mapping while another closes the file).
The call to_do_mapping()- which can fail, must be done inside the critical section before we add a valid pmp entry to the list. On the other hand, insidekvm_dev_ioctl_unmap()we must extract a valid pmp entry from the list before calling_do_unmapping(). There is no real reason for protecting the_do_mapping()call, but I chose not to revert the mapping in case I hit the teardown case.
Gotcha. That makes sense.
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 3/18 kvm: x86: add kvm_arch_msr_intercept()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
This function is used by the introspection subsytem to enable/disable MSR register interception.
The patch adds back the __vmx_enable_intercept_for_msr() function removed with the commit 40d8338d095e
("KVM: VMX: remove functions that enable msr intercepts").
--- arch/x86/include/asm/kvm_host.h | 4 ++++ arch/x86/kvm/svm.c | 11 +++++++++ arch/x86/kvm/vmx.c | 53 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 7 ++++++ 4 files changed, 75 insertions(+)
arch/x86/include/asm/kvm_host.h +4 -0
@@ -1079,6 +1079,8 @@ struct kvm_x86_ops {int (*pre_enter_smm)(struct kvm_vcpu *vcpu,char *smstate);int (*pre_leave_smm)(struct kvm_vcpu *vcpu,u64 smbase);int (*enable_smi_window)(struct kvm_vcpu *vcpu); + +void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); };struct kvm_arch_async_pf {
@@ -1451,4 +1453,6 @@ static inline int kvm_cpu_get_apicid(int mps_cpu)void kvm_arch_mmu_notifier_invalidate_range (struct kvm *kvm,unsigned long start,unsigned long end); +void kvm_arch_msr_intercept (struct kvm_vcpu *vcpu,unsigned int msr, +bool enable);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/svm.c +11 -0
@@ -5505,6 +5505,15 @@ static int enable_smi_window(struct kvm_vcpu *vcpu)return 0 ; } +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; + +set_msr_interception (msrpm, msr, enable, enable); +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -5620,6 +5629,8 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .pre_enter_smm = svm_pre_enter_smm, .pre_leave_smm = svm_pre_leave_smm, .enable_smi_window = enable_smi_window, + + .msr_intercept = svm_msr_intercept, };static int __init svm_init (void )
arch/x86/kvm/vmx.c +53 -0
65 lines not on this page · raw message
arch/x86/kvm/x86.c +7 -0
@@ -8871,6 +8871,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); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_exit);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_fast_mmio);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_inj_virq);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 4/18 kvm: x86: add kvm_mmu_nested_guest_page_fault() and kvmi_mmu_fault_gla()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
These are helper functions used by the VM introspection subsytem on the PF call path.
--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 8 ++++++++ arch/x86/kvm/vmx.c | 9 +++++++++ 5 files changed, 36 insertions(+)
arch/x86/include/asm/kvm_host.h +7 -0
@@ -692,6 +692,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;
@@ -1081,6 +1084,7 @@ struct kvm_x86_ops {int (*enable_smi_window)(struct kvm_vcpu *vcpu);void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +u64 (*fault_gla)(struct kvm_vcpu *vcpu); };struct kvm_arch_async_pf {
@@ -1455,4 +1459,7 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,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_guest_page_fault (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/include/asm/vmx.h +2 -0
@@ -530,6 +530,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)
@@ -537,6 +538,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/mmu.c +10 -0
@@ -5530,3 +5530,13 @@ void kvm_mmu_module_exit(void)unregister_shrinker (&mmu_shrinker);mmu_audit_disable (); } + +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->fault_gla (vcpu); +} + +bool kvm_mmu_nested_guest_page_fault (struct kvm_vcpu *vcpu) +{ +return !!(vcpu->arch.error_code &PFERR_GUEST_PAGE_MASK ); +}
arch/x86/kvm/svm.c +8 -0
@@ -2145,6 +2145,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 fault_address = 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, svm->vmcb->control.insn_bytes, svm->vmcb->control.insn_len);
@@ -5514,6 +5516,11 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,set_msr_interception (msrpm, msr, enable, enable); } +static u64 svm_fault_gla (struct kvm_vcpu *vcpu) +{ +return ~0ull ; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -5631,6 +5638,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window, .msr_intercept = svm_msr_intercept, + .fault_gla = svm_fault_gla };static int __init svm_init (void )
arch/x86/kvm/vmx.c +9 -0
@@ -6541,6 +6541,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) 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 ); }
@@ -12120,6 +12121,13 @@ static void vmx_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, } } +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 ~0ul ; +} +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,
@@ -12252,6 +12260,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window, .msr_intercept = vmx_msr_intercept, + .fault_gla = vmx_fault_gla };static int __init vmx_init (void )
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 04/18] kvm: x86: add kvm_mmu_nested_guest_page_fault() and kvmi_mmu_fault_gla()
Quoted 108 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
These are helper functions used by the VM introspection subsytem on the PF call path.
Signed-off-by Mihai Donțu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 8 ++++++++ arch/x86/kvm/vmx.c | 9 +++++++++ 5 files changed, 36 insertions(+)arch/x86/include/asm/kvm_host.h +7
-0@@ -692,6 +692,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;@@ -1081,6 +1084,7 @@ struct kvm_x86_ops {int (*enable_smi_window)(struct kvm_vcpu *vcpu);void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +u64 (*fault_gla)(struct kvm_vcpu *vcpu); };struct kvm_arch_async_pf {@@ -1455,4 +1459,7 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,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_guest_page_fault (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */ arch/x86/include/asm/vmx.h +2
-0@@ -530,6 +530,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)@@ -537,6 +538,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/mmu.c +10
-0@@ -5530,3 +5530,13 @@ void kvm_mmu_module_exit(void)unregister_shrinker (&mmu_shrinker);mmu_audit_disable (); } + +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->fault_gla (vcpu); +} + +bool kvm_mmu_nested_guest_page_fault (struct kvm_vcpu *vcpu) +{ +return !!(vcpu->arch.error_code &PFERR_GUEST_PAGE_MASK ); +}arch/x86/kvm/svm.c +7
-0@@ -2145,6 +2145,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 fault_address = 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, svm->vmcb->control.insn_bytes, svm->vmcb->control.insn_len);@@ -5514,6 +5516,11 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,set_msr_interception (msrpm, msr, enable, enable); } +static u64 svm_fault_gla (struct kvm_vcpu *vcpu) +{ +return ~0ull ; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
arch/x86/kvm/svm.c static struct kvm_x86_ops svm_x86_ops __ro_after_init = { · line 5641
Quoted 4 lines
arch/x86/kvm/svm.c +1
-0@@ -5631,6 +5638,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window, .msr_intercept = svm_msr_intercept, + .fault_gla = svm_fault_gla
Minor nit, it seems like this line should probably end with a "," so that future additions don't need to modify this line.
Quoted 16 lines
arch/x86/kvm/svm.c +0
-0};static int __init svm_init (void )arch/x86/kvm/vmx.c +1
-0@@ -6541,6 +6541,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) 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 ); }
arch/x86/kvm/vmx.c static void vmx_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, · line 12128
Quoted 8 lines
arch/x86/kvm/vmx.c +5
-0@@ -12120,6 +12121,13 @@ static void vmx_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, } } +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 ~0ul ;
Should this not be "return ~0ull" (like in svm_fault_gla())?
arch/x86/kvm/vmx.c static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · line 12263
Quoted 10 lines
arch/x86/kvm/vmx.c +3
-0+} +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,@@ -12252,6 +12260,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window, .msr_intercept = vmx_msr_intercept, + .fault_gla = vmx_fault_gla
Same deal here with the trailing ","
arch/x86/kvm/vmx.c static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · line 12266
Quoted 4 lines
arch/x86/kvm/vmx.c +0
-0};static int __init vmx_init (void )
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 04/18] kvm: x86: add kvm_mmu_nested_guest_page_fault() and kvmi_mmu_fault_gla()
Hi Patrick,
Quoted 111 lines · Patrick Colp
Quoted 110 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
These are helper functions used by the VM introspection subsytem on the PF call path.
Signed-off-by Mihai Donțu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 8 ++++++++ arch/x86/kvm/vmx.c | 9 +++++++++ 5 files changed, 36 insertions(+)arch/x86/include/asm/kvm_host.h +7
-0@@ -692,6 +692,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;@@ -1081,6 +1084,7 @@ struct kvm_x86_ops {int (*enable_smi_window)(struct kvm_vcpu *vcpu);void (*msr_intercept)(struct kvm_vcpu *vcpu,unsigned int msr,bool enable); +u64 (*fault_gla)(struct kvm_vcpu *vcpu); };struct kvm_arch_async_pf {@@ -1455,4 +1459,7 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,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_guest_page_fault (struct kvm_vcpu *vcpu); +#endif /* _ASM_X86_KVM_HOST_H */ arch/x86/include/asm/vmx.h +2
-0@@ -530,6 +530,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)@@ -537,6 +538,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/mmu.c +10
-0@@ -5530,3 +5530,13 @@ void kvm_mmu_module_exit(void)unregister_shrinker (&mmu_shrinker);mmu_audit_disable (); } + +u64 kvm_mmu_fault_gla (struct kvm_vcpu *vcpu) +{ +return kvm_x86_ops->fault_gla (vcpu); +} + +bool kvm_mmu_nested_guest_page_fault (struct kvm_vcpu *vcpu) +{ +return !!(vcpu->arch.error_code &PFERR_GUEST_PAGE_MASK ); +}arch/x86/kvm/svm.c +7
-0@@ -2145,6 +2145,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 fault_address = 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, svm->vmcb->control.insn_bytes, svm->vmcb->control.insn_len);@@ -5514,6 +5516,11 @@ static void svm_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr,set_msr_interception (msrpm, msr, enable, enable); } +static u64 svm_fault_gla (struct kvm_vcpu *vcpu) +{ +return ~0ull ; +} +static struct kvm_x86_ops svm_x86_ops__ro_after_init = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,@@ -5631,6 +5638,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window,
Quoted 5 lines
arch/x86/kvm/svm.c static struct kvm_x86_ops svm_x86_ops __ro_after_init = { · line 5641
Quoted 2 lines
arch/x86/kvm/svm.c +1
-0.msr_intercept = svm_msr_intercept, + .fault_gla = svm_fault_glaMinor nit, it seems like this line should probably end with a "," so that future additions don't need to modify this line.
Will do.
Quoted 20 lines
Quoted 20 lines
arch/x86/kvm/svm.c +0
-0};static int __init svm_init (void )arch/x86/kvm/vmx.c +1
-0@@ -6541,6 +6541,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu) 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 ); }@@ -12120,6 +12121,13 @@ static void vmx_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, } }
Quoted 7 lines
arch/x86/kvm/vmx.c static void vmx_msr_intercept(struct kvm_vcpu *vcpu, unsigned int msr, · line 12128
Quoted 5 lines
arch/x86/kvm/vmx.c +5
-0+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 ~0ul ;Should this not be "return ~0ull" (like in
svm_fault_gla())?
Yes, it should.
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx.c +2
-0+} +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,@@ -12252,6 +12260,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .enable_smi_window = enable_smi_window,
Quoted 4 lines
arch/x86/kvm/vmx.c static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · line 12263
Quoted 2 lines
arch/x86/kvm/vmx.c +1
-0.msr_intercept = vmx_msr_intercept, + .fault_gla = vmx_fault_glaSame deal here with the trailing ","
Will do.
Quoted 5 lines
arch/x86/kvm/vmx.c static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { · line 12266
Quoted 5 lines
arch/x86/kvm/vmx.c +0
-0};static int __init vmx_init (void )
Thank you for the review!
Signature · 7 lines
Mihai Donțu -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 5/18 kvm: x86: add kvm_arch_vcpu_set_regs()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
This is a version of kvm_arch_vcpu_ioctl_set_regs() which does not touch the exceptions vector.
--- arch/x86/kvm/x86.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 1 + 2 files changed, 35 insertions(+)
arch/x86/kvm/x86.c +34 -0
@@ -7389,6 +7389,40 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return 0 ; } +/* +* Similar to kvm_arch_vcpu_ioctl_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); + +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
@@ -767,6 +767,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);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); int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 05/18] kvm: x86: add kvm_arch_vcpu_set_regs()
Quoted 43 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This is a version of
kvm_arch_vcpu_ioctl_set_regs()which does not touch the exceptions vector.Signed-off-by Mihai Donțu <[email protected]>--- arch/x86/kvm/x86.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 1 + 2 files changed, 35 insertions(+)arch/x86/kvm/x86.c +24
-0@@ -7389,6 +7389,40 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return 0 ; } +/* +* Similar to kvm_arch_vcpu_ioctl_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);
arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) · line 7425
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+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); + +kvm_make_request (KVM_REQ_EVENT , vcpu); +} +
kvm_arch_vcpu_ioctl_set_regs() returns an int (so that, for e.g., in ARM it can return an error to indicate that the function is not supported/implemented). Is there a reason this function shouldn't do the same (is it only ever going to be implemented for x86)?
Quoted 9 lines
arch/x86/kvm/x86.c +0
-0void kvm_get_cs_db_l_bits (struct kvm_vcpu *vcpu,int *db,int *l) {struct kvm_segment cs;include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, · line 773
Quoted 7 lines
include/linux/kvm_host.h +1
-0@@ -767,6 +767,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);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); int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 05/18] kvm: x86: add kvm_arch_vcpu_set_regs()
Quoted 66 lines · Patrick Colp
Quoted 43 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This is a version of
kvm_arch_vcpu_ioctl_set_regs()which does not touch the exceptions vector.Signed-off-by Mihai Donțu <[email protected]>--- arch/x86/kvm/x86.c | 34 ++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 1 + 2 files changed, 35 insertions(+)arch/x86/kvm/x86.c +24
-0@@ -7389,6 +7389,40 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)return 0 ; } +/* +* Similar to kvm_arch_vcpu_ioctl_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);arch/x86/kvm/x86.c int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) · line 7425
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+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); + +kvm_make_request (KVM_REQ_EVENT , vcpu); +} +
kvm_arch_vcpu_ioctl_set_regs()returns an int (so that, for e.g., in ARM it can return an error to indicate that the function is not supported/implemented). Is there a reason this function shouldn't do the same (is it only ever going to be implemented for x86)?Quoted 6 lines
include/linux/kvm_host.h +0
-0@@ -767,6 +767,7 @@ int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
Quoted 8 lines
include/linux/kvm_host.h int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, · line 772
Quoted 5 lines
include/linux/kvm_host.h +1
-0int kvm_arch_vcpu_ioctl_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);Patrick
Hi Patrick,
Thank you for taking the time to review these patches.
You're right. This function should return an error code, regardless on the time when ARM will be supported.
Adalbert
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 6/18 kvm: vmx: export the availability of EPT views
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
This is used to validate the KVMI_GET_PAGE_ACCESS and KVMI_SET_PAGE_ACCESS commands when the guest introspection tool selects a different EPT view.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx.c | 2 ++ arch/x86/kvm/x86.c | 3 +++ 3 files changed, 6 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1162,6 +1162,7 @@ extern u64 kvm_max_tsc_scaling_ratio;extern u64 kvm_default_tsc_scaling_ratio;extern u64 kvm_mce_cap_supported; +extern bool kvm_eptp_switching_supported;enum emulation_result {EMULATE_DONE ,/* no further processing */
arch/x86/kvm/vmx.c +2 -0
@@ -6898,6 +6898,8 @@ static __init int hardware_setup(void) kvm_x86_ops->cancel_hv_timer =NULL ; } + kvm_eptp_switching_supported =cpu_has_vmx_vmfunc (); +kvm_set_posted_intr_wakeup_handler (wakeup_handler); kvm_mce_cap_supported |=MCG_LMCE_P ;
arch/x86/kvm/x86.c +3 -0
@@ -138,6 +138,9 @@ module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR);static bool __read_mostly vector_hashing = true;module_param (vector_hashing, bool,S_IRUGO ); +bool __read_mostly kvm_eptp_switching_supported; +EXPORT_SYMBOL_GPL (kvm_eptp_switching_supported); +#define KVM_NR_SHARED_MSRS 16struct kvm_shared_msrs_global {
RFC PATCH v4 7/18 kvm: page track: add support for preread, prewrite and preexec
In reply to Adalber LazărFrom: Adalbert Lazar <[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.
The kvm_page_track_create_memslot() was extended in order to track gfn-s as soon as the memory slots are created.
--- arch/x86/include/asm/kvm_page_track.h | 24 +++++- arch/x86/kvm/mmu.c | 143 ++++++++++++++++++++++++++++++---- arch/x86/kvm/mmu.h | 4 + arch/x86/kvm/page_track.c | 129 ++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 2 +- 5 files changed, 281 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_page_track.h +22 -2
@@ -3,8 +3,11 @@#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_MAX , +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,u8 *new, +int bytes, +struct kvm_page_track_notifier_node *node, +bool *data_ready); +bool (*track_prewrite)(struct kvm_vcpu *vcpu,gpa_t gpa,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.
@@ -34,6 +44,11 @@ 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); +bool (*track_preexec)(struct kvm_vcpu *vcpu,gpa_t gpa, +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 +66,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,
@@ -69,7 +84,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,u8 *new, +int bytes,bool *data_ready); +bool kvm_page_track_prewrite (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, +int bytes);void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes); +bool kvm_page_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa);void kvm_page_track_flush_slot (struct kvm *kvm,struct kvm_memory_slot *slot);#endif
arch/x86/kvm/mmu.c +130 -13
208 lines not on this page · raw message
arch/x86/kvm/mmu.h +4 -0
@@ -193,5 +193,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 +124 -5
175 lines not on this page · raw message
arch/x86/kvm/x86.c +1 -1
@@ -8421,7 +8421,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 ;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 07/18] kvm: page track: add support for preread, prewrite and preexec
Quoted 23 lines · Adalber Lazăr
From: Adalbert Lazar <[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.
The
kvm_page_track_create_memslot()was extended in order to track gfn-s as soon as the memory slots are created.Signed-off-by Mihai Donțu <[email protected]>--- arch/x86/include/asm/kvm_page_track.h | 24 +++++- arch/x86/kvm/mmu.c | 143 ++++++++++++++++++++++++++++++---- arch/x86/kvm/mmu.h | 4 + arch/x86/kvm/page_track.c | 129 ++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 2 +- 5 files changed, 281 insertions(+), 21 deletions(-)arch/x86/include/asm/kvm_page_track.h +0
-0
arch/x86/include/asm/kvm_page_track.h line 11
Quoted 10 lines
arch/x86/include/asm/kvm_page_track.h +4
-1@@ -3,8 +3,11 @@#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_MAX , +KVM_PAGE_TRACK_PREEXEC , +KVM_PAGE_TRACK_MAX };
The comma at the end of KVM_PAGE_TRACK_MAX should probably just be left in. This will tighten up this diff and prevent another commit potentially needing to add it back in later.
Quoted 361 lines
arch/x86/include/asm/kvm_page_track.h +18
-1/* @@ -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,u8 *new, +int bytes, +struct kvm_page_track_notifier_node *node, +bool *data_ready); +bool (*track_prewrite)(struct kvm_vcpu *vcpu,gpa_t gpa,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. @@ -34,6 +44,11 @@ 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); +bool (*track_preexec)(struct kvm_vcpu *vcpu,gpa_t gpa, +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 +66,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,@@ -69,7 +84,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,u8 *new, +int bytes,bool *data_ready); +bool kvm_page_track_prewrite (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, +int bytes);void kvm_page_track_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new,int bytes); +bool kvm_page_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa);void kvm_page_track_flush_slot (struct kvm *kvm,struct kvm_memory_slot *slot);#endif arch/x86/kvm/mmu.c +130
-13@@ -1014,9 +1014,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); }@@ -1031,9 +1035,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); }@@ -1416,6 +1424,29 @@ static bool spte_write_protect(u64 *sptep, bool pt_protect)return mmu_spte_update (sptep, spte); } +static bool spte_read_protect (u64 *sptep) +{ +u64 spte = *sptep; + +rmap_printk ("rmap_read_protect: spte %p %llx\n" , sptep, *sptep); + +/* TODO: verify if the CPU supports EPT-execute-only */ + spte = spte & ~(PT_WRITABLE_MASK |PT_PRESENT_MASK ); + +return mmu_spte_update (sptep, spte); +} + +static bool spte_exec_protect (u64 *sptep,bool pt_protect) +{ +u64 spte = *sptep; + +rmap_printk ("rmap_exec_protect: spte %p %llx\n" , sptep, *sptep); + + spte = spte & ~PT_USER_MASK ; + +return mmu_spte_update (sptep, spte); +} +static bool __rmap_write_protect (struct kvm *kvm,struct kvm_rmap_head *rmap_head,bool pt_protect)@@ -1430,6 +1461,34 @@ static bool __rmap_write_protect(struct kvm *kvm,return flush; } +static bool __rmap_read_protect (struct kvm *kvm, +struct kvm_rmap_head *rmap_head, +bool pt_protect) +{ +u64 *sptep; +struct rmap_iterator iter; +bool flush = false; + +for_each_rmap_spte (rmap_head, &iter, sptep) + flush |=spte_read_protect (sptep); + +return flush; +} + +static bool __rmap_exec_protect (struct kvm *kvm, +struct kvm_rmap_head *rmap_head, +bool pt_protect) +{ +u64 *sptep; +struct rmap_iterator iter; +bool flush = false; + +for_each_rmap_spte (rmap_head, &iter, sptep) + flush |=spte_exec_protect (sptep, pt_protect); + +return flush; +} +static bool spte_clear_dirty (u64 *sptep) {u64 spte = *sptep;@@ -1600,6 +1659,36 @@ bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,return write_protected; } +bool kvm_mmu_slot_gfn_read_protect (struct kvm *kvm, +struct kvm_memory_slot *slot,u64 gfn) +{ +struct kvm_rmap_head *rmap_head; +int i; +bool read_protected = false; + +for (i =PT_PAGE_TABLE_LEVEL ; i <=PT_MAX_HUGEPAGE_LEVEL ; ++i) { + rmap_head =__gfn_to_rmap (gfn, i, slot); + read_protected |=__rmap_read_protect (kvm, rmap_head, true); + } + +return read_protected; +} + +bool kvm_mmu_slot_gfn_exec_protect (struct kvm *kvm, +struct kvm_memory_slot *slot,u64 gfn) +{ +struct kvm_rmap_head *rmap_head; +int i; +bool exec_protected = false; + +for (i =PT_PAGE_TABLE_LEVEL ; i <=PT_MAX_HUGEPAGE_LEVEL ; ++i) { + rmap_head =__gfn_to_rmap (gfn, i, slot); + exec_protected |=__rmap_exec_protect (kvm, rmap_head, true); + } + +return exec_protected; +} +static bool rmap_write_protect (struct kvm_vcpu *vcpu,u64 gfn) {struct kvm_memory_slot *slot;@@ -2688,7 +2777,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) {@@ -2953,6 +3043,21 @@ static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep)__direct_pte_prefetch (vcpu, sp, sptep); } +static unsigned int kvm_mmu_page_track_acc (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +unsigned int acc =ACC_ALL ; + +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 int __direct_map (struct kvm_vcpu *vcpu,int write,int map_writable,int level,gfn_t gfn,kvm_pfn_t pfn,bool prefault) {@@ -2966,7 +3071,9 @@ 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); + + emulate =mmu_set_spte (vcpu, iterator.sptep, acc, write, level, gfn, pfn, prefault, map_writable);direct_pte_prefetch (vcpu, iterator.sptep);@@ -3713,15 +3820,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;@@ -4942,7 +5055,11 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,* and resume the guest. */ if (vcpu->arch.mmu.direct_map && - (error_code &PFERR_NESTED_GUEST_PAGE ) ==PFERR_NESTED_GUEST_PAGE ) { + (error_code &PFERR_NESTED_GUEST_PAGE ) ==PFERR_NESTED_GUEST_PAGE && + !kvm_page_track_is_active (vcpu,gpa_to_gfn (cr2),KVM_PAGE_TRACK_PREREAD ) && + !kvm_page_track_is_active (vcpu,gpa_to_gfn (cr2),KVM_PAGE_TRACK_PREWRITE ) && + !kvm_page_track_is_active (vcpu,gpa_to_gfn (cr2),KVM_PAGE_TRACK_WRITE ) && + !kvm_page_track_is_active (vcpu,gpa_to_gfn (cr2),KVM_PAGE_TRACK_PREEXEC )) {kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (cr2));return 1 ; }arch/x86/kvm/mmu.h +4
-0@@ -193,5 +193,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 +26
-5@@ -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] = kvzalloc(npages *@@ -46,6 +49,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:@@ -86,7 +100,7 @@ static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,* @kvm: the guest instance we are interested in. * @slot: the @gfn belongs to. * @gfn: the guest page. -* @mode: tracking mode, currently only write track is supported. +* @mode: tracking mode. */ void kvm_slot_page_track_add_page(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,@@ -104,9 +118,16 @@ void kvm_slot_page_track_add_page(struct kvm *kvm,*/ kvm_mmu_gfn_disallow_lpage (slot, gfn); -if (mode ==KVM_PAGE_TRACK_WRITE ) +if (mode ==KVM_PAGE_TRACK_PREWRITE || mode ==KVM_PAGE_TRACK_WRITE ) {if (kvm_mmu_slot_gfn_write_protect (kvm, slot, gfn))kvm_flush_remote_tlbs (kvm); + }else if (mode ==KVM_PAGE_TRACK_PREREAD ) { +if (kvm_mmu_slot_gfn_read_protect (kvm, slot, gfn)) +kvm_flush_remote_tlbs (kvm); + }else if (mode ==KVM_PAGE_TRACK_PREEXEC ) { +if (kvm_mmu_slot_gfn_exec_protect (kvm, slot, gfn)) +kvm_flush_remote_tlbs (kvm); + } }EXPORT_SYMBOL_GPL (kvm_slot_page_track_add_page);@@ -121,7 +142,7 @@ EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);* @kvm: the guest instance we are interested in. * @slot: the @gfn belongs to. * @gfn: the guest page. -* @mode: tracking mode, currently only write track is supported. +* @mode: tracking mode. */ void kvm_slot_page_track_remove_page(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn,
arch/x86/kvm/page_track.c kvm_page_track_unregister_notifier(struct kvm *kvm, · line 243
Quoted 9 lines
arch/x86/kvm/page_track.c +6
-0@@ -214,6 +235,75 @@ kvm_page_track_unregister_notifier(struct kvm *kvm, }EXPORT_SYMBOL_GPL (kvm_page_track_unregister_notifier); +/* +* Notify the node that a read access is about to happen. Returning false +* doesn't stop the other nodes from being called, but it will stop +* the emulation. +* +* The node should figure out if the written page is the one that node is
s/that node/that the node/
Quoted 25 lines
arch/x86/kvm/page_track.c +25
-0+* interested in by itself. +* +* The nodes will always be in conflict if they track the same page: +* - accepting a read won't guarantee that the next node will not override +* the data (filling new/bytes and setting data_ready) +* - filling new/bytes with custom data won't guarantee that the next node +* will not override that +*/ +bool kvm_page_track_preread (struct kvm_vcpu *vcpu,gpa_t gpa,u8 *new, +int bytes,bool *data_ready) +{ +struct kvm_page_track_notifier_head *head; +struct kvm_page_track_notifier_node *n; +int idx; +bool ret = true; + + *data_ready = false; + + head = &vcpu->kvm->arch.track_notifier_head; + +if (hlist_empty (&head->track_notifier_list)) +return ret; + + idx =srcu_read_lock (&head->track_srcu); +hlist_for_each_entry_rcu (n, &head->track_notifier_list, node)
arch/x86/kvm/page_track.c kvm_page_track_unregister_notifier(struct kvm *kvm, · line 278
Quoted 10 lines
arch/x86/kvm/page_track.c +10
-0+if (n->track_preread) +if (!n->track_preread (vcpu, gpa, new, bytes, n, + data_ready)) + ret = false; +srcu_read_unlock (&head->track_srcu, idx); +return ret; +} + +/* +* Notify the node that an write access is about to happen. Returning false
s/an write/a write/
arch/x86/kvm/page_track.c kvm_page_track_unregister_notifier(struct kvm *kvm, · line 243
Quoted 4 lines
arch/x86/kvm/page_track.c +4
-0+* doesn't stop the other nodes from being called, but it will stop +* the emulation. +* +* The node should figure out if the written page is the one that node is
s/that node/that the node/
Quoted 28 lines
arch/x86/kvm/page_track.c +24
-0+* interested in by itself. +*/ +bool kvm_page_track_prewrite (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *new, +int bytes) +{ +struct kvm_page_track_notifier_head *head; +struct kvm_page_track_notifier_node *n; +int idx; +bool ret = true; + + head = &vcpu->kvm->arch.track_notifier_head; + +if (hlist_empty (&head->track_notifier_list)) +return ret; + + idx =srcu_read_lock (&head->track_srcu); +hlist_for_each_entry_rcu (n, &head->track_notifier_list, node) +if (n->track_prewrite) +if (!n->track_prewrite (vcpu, gpa, new, bytes, n)) + ret = false; +srcu_read_unlock (&head->track_srcu, idx); +return ret; +} +/* * Notify the node that write access is intercepted and write emulation is * finished at this time.
arch/x86/kvm/page_track.c void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, · line 336
Quoted 7 lines
arch/x86/kvm/page_track.c +4
-0@@ -240,6 +330,35 @@ void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,srcu_read_unlock (&head->track_srcu, idx); } +/* +* Notify the node that an instruction is about to be executed. +* Returning false doesn't stop the other nodes from being called, +* but it will stop the emulation with ?!.
With what?
arch/x86/kvm/page_track.c void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, · line 338
Quoted 2 lines
arch/x86/kvm/page_track.c +2
-0+* +* The node should figure out if the written page is the one that node is
s/that node/that the node/
Quoted 32 lines
arch/x86/kvm/page_track.c +23
-0+* interested in by itself. +*/ +bool kvm_page_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa) +{ +struct kvm_page_track_notifier_head *head; +struct kvm_page_track_notifier_node *n; +int idx; +bool ret = true; + + head = &vcpu->kvm->arch.track_notifier_head; + +if (hlist_empty (&head->track_notifier_list)) +return ret; + + idx =srcu_read_lock (&head->track_srcu); +hlist_for_each_entry_rcu (n, &head->track_notifier_list, node) +if (n->track_preexec) +if (!n->track_preexec (vcpu, gpa, n)) + ret = false; +srcu_read_unlock (&head->track_srcu, idx); +return ret; +} +/* * Notify the node that memory slot is being removed or moved so that it can * drop write-protection for the pages in the memory slot. arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, · line 8427
Quoted 8 lines
arch/x86/kvm/x86.c +1
-1@@ -8421,7 +8421,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 ;
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 07/18] kvm: page track: add support for preread, prewrite and preexec
Quoted 15 lines · Patrick Colp
Quoted 6 lines · Adalber Lazăr
From: Adalbert Lazar <[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.
Quoted 7 lines
The
kvm_page_track_create_memslot()was extended in order to track gfn-s as soon as the memory slots are created.+/*
+ * Notify the node that an instruction is about to be executed.
+ * Returning false doesn't stop the other nodes from being called,
+ * but it will stop the emulation with ?!.
Quoted 6 lines
With what?
arch/x86/kvm/page_track.c void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, · line 342
Quoted 2 lines
arch/x86/kvm/page_track.c +2
-0+bool kvm_page_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa) +{Patrick
With X86EMUL_RETRY_INSTR, or some return value, depending on the context.
Currently, we call this function when the instruction is fetched, to give the introspection tool more options. Depending on its policies, the introspector could:
- skip the instruction (and retry to guest)
- remove the tracking for the "current" page (and retry to guest)
- change the instruction (and continue the emulation)
- do nothing but log (and continue the emulation)
Thanks for spotting this,
Adalbert
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 8/18 kvm: add the VM introspection subsystem
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
- kvmi_msg.c - ABI and socket related functions
- kvmi_mem.c - handle map/unmap requests from the introspector
- kvmi.c - all the other
The new data used by this subsystem is attached to the 'kvm' and 'kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).
Besides the KVMI system, this patch exports the kvm_vcpu_ioctl_x86_get_xsave() and the mm_find_pmd() functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-) create mode 100644 include/linux/kvmi.h create mode 100644 include/trace/events/kvmi.h 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_msg.c
arch/x86/include/asm/kvm_host.h +1 -0
@@ -73,6 +73,7 @@#define KVM_REQ_HV_RESET KVM_ARCH_REQ(20)#define KVM_REQ_HV_EXIT KVM_ARCH_REQ(21)#define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22) +#define KVM_REQ_INTROSPECTION KVM_ARCH_REQ(23)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
arch/x86/kvm/Makefile +1 -0
@@ -9,6 +9,7 @@ CFLAGS_vmx.o := -I. KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ + $(KVM)/kvmi.o $(KVM)/kvmi_msg.o $(KVM)/kvmi_mem.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
arch/x86/kvm/x86.c +2 -2
@@ -3346,8 +3346,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 +4 -0
@@ -274,6 +274,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)
@@ -446,6 +447,7 @@ struct kvm {struct srcu_struct srcu;struct srcu_struct irq_srcu;pid_t userspace_pid; +void *kvmi; };#define kvm_err (fmt, ...) \
@@ -779,6 +781,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 );
include/linux/kvmi.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_H__ +#define __KVMI_H__ + +#define kvmi_is_present () 1 + +int kvmi_init (void ); +void kvmi_uninit (void ); +void kvmi_destroy_vm (struct kvm *kvm); +bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu); +void kvmi_vcpu_init (struct kvm_vcpu *vcpu); +void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu); +bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr, +unsigned long old_value,unsigned long *new_value); +bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr); +void kvmi_xsetbv_event (struct kvm_vcpu *vcpu); +bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva); +bool kvmi_is_agent_hypercall (struct kvm_vcpu *vcpu); +void kvmi_hypercall_event (struct kvm_vcpu *vcpu); +bool kvmi_lost_exception (struct kvm_vcpu *vcpu); +void kvmi_trap_event (struct kvm_vcpu *vcpu); +bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u32 info, +unsigned long exit_qualification, +unsigned char descriptor,unsigned char write); +void kvmi_flush_mem_access (struct kvm *kvm); +void kvmi_handle_request (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); + + +#endif
include/linux/mm.h +3 -0
@@ -1115,6 +1115,9 @@ void page_address_init(void);#define page_address_init () do { } while(0)#endif +/* rmap.c */ +extern pmd_t *mm_find_pmd (struct mm_struct *mm,unsigned long address); +extern void *page_rmapping (struct page *page);extern struct anon_vma *page_anon_vma (struct page *page);extern struct address_space *page_mapping (struct page *page);
include/trace/events/kvmi.h new file +174 -0
174 lines not on this page · raw message
include/uapi/linux/kvm.h +8 -0
@@ -1359,6 +1359,14 @@ struct kvm_s390_ucas_mapping {#define KVM_S390_GET_CMMA_BITS _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)#define KVM_S390_SET_CMMA_BITS _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log) +struct kvm_introspection { +int fd; +__u32 padding; +__u32 commands; +__u32 events; +}; +#define KVM_INTROSPECTION _IOW(KVMIO, 0xff, struct kvm_introspection) +#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)
mm/internal.h +0 -5
@@ -92,11 +92,6 @@ extern unsigned long highest_memmap_pfn;extern int isolate_lru_page (struct page *page);extern void putback_lru_page (struct page *page); -/* -* in mm/rmap.c: -*/ -extern pmd_t *mm_find_pmd (struct mm_struct *mm,unsigned long address); -/* * in mm/page_alloc.c */
virt/kvm/kvmi.c new file +1410 -0
1,410 lines not on this page · raw message
virt/kvm/kvmi_int.h new file +121 -0
121 lines not on this page · raw message
virt/kvm/kvmi_mem.c new file +730 -0
730 lines not on this page · raw message
virt/kvm/kvmi_msg.c new file +1134 -0
453 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
Quoted 456 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
-kvmi_msg.c- ABI and socket related functions
-kvmi_mem.c- handle map/unmap requests from the introspector
-kvmi.c- all the otherThe new data used by this subsystem is attached to the 'kvm' and '
kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).Besides the KVMI system, this patch exports the
kvm_vcpu_ioctl_x86_get_xsave()and themm_find_pmd()functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.Signed-off-by Mihai Donțu <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Signed-off-by Mircea Cîrjaliu <[email protected]>Signed-off-by Marian Rotariu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-)create mode 100644
include/linux/kvmi.h
create mode 100644include/trace/events/kvmi.h
create mode 100644virt/kvm/kvmi.c
create mode 100644virt/kvm/kvmi_int.h
create mode 100644virt/kvm/kvmi_mem.c
create mode 100644virt/kvm/kvmi_msg.carch/x86/include/asm/kvm_host.h +1
-0@@ -73,6 +73,7 @@#define KVM_REQ_HV_RESET KVM_ARCH_REQ(20)#define KVM_REQ_HV_EXIT KVM_ARCH_REQ(21)#define KVM_REQ_HV_STIMER KVM_ARCH_REQ(22) +#define KVM_REQ_INTROSPECTION KVM_ARCH_REQ(23)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \arch/x86/kvm/Makefile +1
-0@@ -9,6 +9,7 @@ CFLAGS_vmx.o := -I. KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ + $(KVM)/kvmi.o $(KVM)/kvmi_msg.o $(KVM)/kvmi_mem.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.oarch/x86/kvm/x86.c +2
-2@@ -3346,8 +3346,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 +4
-0@@ -274,6 +274,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)@@ -446,6 +447,7 @@ struct kvm {struct srcu_struct srcu;struct srcu_struct irq_srcu;pid_t userspace_pid; +void *kvmi; };#define kvm_err (fmt, ...) \@@ -779,6 +781,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 );include/linux/kvmi.h new file +32
-0@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_H__ +#define __KVMI_H__ + +#define kvmi_is_present () 1 + +int kvmi_init (void ); +void kvmi_uninit (void ); +void kvmi_destroy_vm (struct kvm *kvm); +bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu); +void kvmi_vcpu_init (struct kvm_vcpu *vcpu); +void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu); +bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr, +unsigned long old_value,unsigned long *new_value); +bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr); +void kvmi_xsetbv_event (struct kvm_vcpu *vcpu); +bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva); +bool kvmi_is_agent_hypercall (struct kvm_vcpu *vcpu); +void kvmi_hypercall_event (struct kvm_vcpu *vcpu); +bool kvmi_lost_exception (struct kvm_vcpu *vcpu); +void kvmi_trap_event (struct kvm_vcpu *vcpu); +bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u32 info, +unsigned long exit_qualification, +unsigned char descriptor,unsigned char write); +void kvmi_flush_mem_access (struct kvm *kvm); +void kvmi_handle_request (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); + + +#endif include/linux/mm.h +3
-0@@ -1115,6 +1115,9 @@ void page_address_init(void);#define page_address_init () do { } while(0)#endif +/* rmap.c */ +extern pmd_t *mm_find_pmd (struct mm_struct *mm,unsigned long address); +extern void *page_rmapping (struct page *page);extern struct anon_vma *page_anon_vma (struct page *page);extern struct address_space *page_mapping (struct page *page);include/trace/events/kvmi.h new file +174
-0@@ -0,0 +1,174 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM kvmi + +#if !defined(_TRACE_KVMI_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_KVMI_H + +#include <linux/tracepoint.h> + +#ifndef __TRACE_KVMI_STRUCTURES +#define __TRACE_KVMI_STRUCTURES + +#undef EN +#define EN (x) { x, #x } + +static const struct trace_print_flags kvmi_msg_id_symbol[] = { +EN (KVMI_GET_VERSION ), +EN (KVMI_PAUSE_VCPU ), +EN (KVMI_GET_GUEST_INFO ), +EN (KVMI_GET_REGISTERS ), +EN (KVMI_SET_REGISTERS ), +EN (KVMI_GET_PAGE_ACCESS ), +EN (KVMI_SET_PAGE_ACCESS ), +EN (KVMI_INJECT_EXCEPTION ), +EN (KVMI_READ_PHYSICAL ), +EN (KVMI_WRITE_PHYSICAL ), +EN (KVMI_GET_MAP_TOKEN ), +EN (KVMI_CONTROL_EVENTS ), +EN (KVMI_CONTROL_CR ), +EN (KVMI_CONTROL_MSR ), +EN (KVMI_EVENT ), +EN (KVMI_EVENT_REPLY ), +EN (KVMI_GET_CPUID ), +EN (KVMI_GET_XSAVE ), + {-1 ,NULL } +}; + +static const struct trace_print_flags kvmi_event_id_symbol[] = { +EN (KVMI_EVENT_CR ), +EN (KVMI_EVENT_MSR ), +EN (KVMI_EVENT_XSETBV ), +EN (KVMI_EVENT_BREAKPOINT ), +EN (KVMI_EVENT_HYPERCALL ), +EN (KVMI_EVENT_PAGE_FAULT ), +EN (KVMI_EVENT_TRAP ), +EN (KVMI_EVENT_DESCRIPTOR ), +EN (KVMI_EVENT_CREATE_VCPU ), +EN (KVMI_EVENT_PAUSE_VCPU ), + {-1 ,NULL } +}; + +static const struct trace_print_flags kvmi_action_symbol[] = { + {KVMI_EVENT_ACTION_CONTINUE ,"continue" }, + {KVMI_EVENT_ACTION_RETRY ,"retry" }, + {KVMI_EVENT_ACTION_CRASH ,"crash" }, + {-1 ,NULL } +}; + +#endif /* __TRACE_KVMI_STRUCTURES */ + +TRACE_EVENT ( + kvmi_msg_dispatch, + TP_PROTO(__u16 id, __u16 size), +TP_ARGS (id, size), +TP_STRUCT__entry ( + __field(__u16, id) +__field (__u16, size) + ), +TP_fast_assign ( + __entry->id = id; + __entry->size = size; + ), +TP_printk ("%s size %u" , +trace_print_symbols_seq (p, __entry->id, kvmi_msg_id_symbol), + __entry->size) +); + +TRACE_EVENT ( + kvmi_send_event, + TP_PROTO(__u32 id), +TP_ARGS (id), +TP_STRUCT__entry ( +__field (__u32, id) + ), +TP_fast_assign ( + __entry->id = id; + ), +TP_printk ("%s" , +trace_print_symbols_seq (p, __entry->id, kvmi_event_id_symbol)) +); + +#define KVMI_ACCESS_PRINTK () ({ \ + const char *saved_ptr = trace_seq_buffer_ptr(p); \ + static const char * const access_str[] = { \ + "---", "r--", "-w-", "rw-", "--x", "r-x", "-wx", "rwx" \ + }; \ + trace_seq_printf(p, "%s", access_str[__entry->access & 7]); \ + saved_ptr; \ +}) + +TRACE_EVENT ( + kvmi_set_mem_access, + TP_PROTO(__u64 gfn, __u8 access, int err), +TP_ARGS (gfn, access, err), +TP_STRUCT__entry ( + __field(__u64, gfn) + __field(__u8, access) +__field (int, err) + ), +TP_fast_assign ( + __entry->gfn = gfn; + __entry->access = access; + __entry->err = err; + ), +TP_printk ("gfn %llx %s %s %d" , + __entry->gfn,KVMI_ACCESS_PRINTK (), + __entry->err ?"failed" :"succeeded" , __entry->err) +); + +TRACE_EVENT ( + kvmi_apply_mem_access, + TP_PROTO(__u64 gfn, __u8 access, int err), +TP_ARGS (gfn, access, err), +TP_STRUCT__entry ( + __field(__u64, gfn) + __field(__u8, access) +__field (int, err) + ), +TP_fast_assign ( + __entry->gfn = gfn; + __entry->access = access; + __entry->err = err; + ), +TP_printk ("gfn %llx %s flush %s %d" , + __entry->gfn,KVMI_ACCESS_PRINTK (), + __entry->err ?"failed" :"succeeded" , __entry->err) +); + +TRACE_EVENT ( + kvmi_event_page_fault, + TP_PROTO(__u64 gpa, __u64 gva, __u8 access, __u64 old_rip, + __u32 action, __u64 new_rip, __u32 ctx_size), +TP_ARGS (gpa, gva, access, old_rip, action, new_rip, ctx_size), +TP_STRUCT__entry ( + __field(__u64, gpa) + __field(__u64, gva) + __field(__u8, access) + __field(__u64, old_rip) + __field(__u32, action) + __field(__u64, new_rip) +__field (__u32, ctx_size) + ), +TP_fast_assign ( + __entry->gpa = gpa; + __entry->gva = gva; + __entry->access = access; + __entry->old_rip = old_rip; + __entry->action = action; + __entry->new_rip = new_rip; + __entry->ctx_size = ctx_size; + ), +TP_printk ("gpa %llx %s gva %llx rip %llx -> %s rip %llx ctx %u" , + __entry->gpa, +KVMI_ACCESS_PRINTK (), + __entry->gva, + __entry->old_rip, +trace_print_symbols_seq (p, __entry->action, + kvmi_action_symbol), + __entry->new_rip, __entry->ctx_size) +); + +#endif /* _TRACE_KVMI_H */ + +#include <trace/define_trace.h> include/uapi/linux/kvm.h +8
-0@@ -1359,6 +1359,14 @@ struct kvm_s390_ucas_mapping {#define KVM_S390_GET_CMMA_BITS _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)#define KVM_S390_SET_CMMA_BITS _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log) +struct kvm_introspection { +int fd; +__u32 padding; +__u32 commands; +__u32 events; +}; +#define KVM_INTROSPECTION _IOW(KVMIO, 0xff, struct kvm_introspection) +#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)mm/internal.h +0
-5@@ -92,11 +92,6 @@ extern unsigned long highest_memmap_pfn;extern int isolate_lru_page (struct page *page);extern void putback_lru_page (struct page *page); -/* -* in mm/rmap.c: -*/ -extern pmd_t *mm_find_pmd (struct mm_struct *mm,unsigned long address); -/* * in mm/page_alloc.c */ virt/kvm/kvmi.c new file +72
-0@@ -0,0 +1,1410 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +*/ +#include <linux/mmu_context.h> +#include <linux/random.h> +#include <uapi/linux/kvmi.h> +#include <uapi/asm/kvmi.h> +#include "../../arch/x86/kvm/x86.h" +#include "../../arch/x86/kvm/mmu.h" +#include <asm/vmx.h> +#include "cpuid.h" +#include "kvmi_int.h" +#include <asm/kvm_page_track.h> + +#define CREATE_TRACE_POINTS +#include <trace/events/kvmi.h> + +struct kvmi_mem_access { +struct list_head link; +gfn_t gfn; +u8 access; +bool active[KVM_PAGE_TRACK_MAX ]; +struct kvm_memory_slot *slot; +}; + +static void wakeup_events (struct kvm *kvm); +static bool kvmi_page_fault_event (struct kvm_vcpu *vcpu,unsigned long gpa, +unsigned long gva,u8 access); + +static struct workqueue_struct *wq; + +static const u8 full_access =KVMI_PAGE_ACCESS_R | +KVMI_PAGE_ACCESS_W |KVMI_PAGE_ACCESS_X ; + +static const struct { +unsigned int allow_bit; +enum kvm_page_track_mode track_mode; +} track_modes[] = { + {KVMI_PAGE_ACCESS_R ,KVM_PAGE_TRACK_PREREAD }, + {KVMI_PAGE_ACCESS_W ,KVM_PAGE_TRACK_PREWRITE }, + {KVMI_PAGE_ACCESS_X ,KVM_PAGE_TRACK_PREEXEC }, +}; + +void kvmi_make_request (struct kvmi_vcpu *ivcpu,int req) +{ +set_bit (req, &ivcpu->requests); +/* Make sure the bit is set when the worker wakes up */ +smp_wmb (); +up (&ivcpu->sem_requests); +} + +void kvmi_clear_request (struct kvmi_vcpu *ivcpu,int req) +{ +clear_bit (req, &ivcpu->requests); +} + +int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +/* +* This vcpu is already stopped, executing this command +* as a result of the REQ_CMD bit being set +* (see kvmi_handle_request). +*/ +if (ivcpu->pause) +return -KVM_EBUSY ; +
virt/kvm/kvmi.c line 82
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ ivcpu->pause = true; + +return 0 ; +} + +static void kvmi_apply_mem_access (struct kvm *kvm, +struct kvm_memory_slot *slot, +struct kvmi_mem_access *m) +{ +int idx, k;
This should probably be i instead of k. I'm guessing you chose k to avoid confusion of i with idx. However, there's precedent already set for using i as a loop counter even in this case (e.g., look at kvm_scan_ioapic_routes() in arch/x86/kvm/irq_comm.c and init_rmode_identity_map() in arch/x86/kvm/vmx.c)
Quoted 33 lines
virt/kvm/kvmi.c new file +33
-0+ +if (!slot) { + slot =gfn_to_memslot (kvm, m->gfn); +if (!slot) +return ; + } + + idx =srcu_read_lock (&kvm->srcu); + +spin_lock (&kvm->mmu_lock); + +for (k =0 ; k <ARRAY_SIZE (track_modes); k++) { +unsigned int allow_bit = track_modes[k].allow_bit; +enum kvm_page_track_mode mode = track_modes[k].track_mode; + +if (m->access & allow_bit) { +if (m->active[mode] && m->slot == slot) { +kvm_slot_page_track_remove_page (kvm, slot, + m->gfn, mode); + m->active[mode] = false; + m->slot =NULL ; + } + }else if (!m->active[mode] || m->slot != slot) { +kvm_slot_page_track_add_page (kvm, slot, m->gfn, mode); + m->active[mode] = true; + m->slot = slot; + } + } + +spin_unlock (&kvm->mmu_lock); + +srcu_read_unlock (&kvm->srcu, idx); +}
virt/kvm/kvmi.c line 125
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ +int kvmi_set_mem_access (struct kvm *kvm,u64 gpa,u8 access) +{ +struct kvmi_mem_access *m; +struct kvmi_mem_access *__m; +struct kvmi *ikvm =IKVM (kvm); +gfn_t gfn =gpa_to_gfn (gpa); + +if (kvm_is_error_hva (gfn_to_hva_safe (kvm, gfn))) +kvm_err ("Invalid gpa %llx (or memslot not available yet)" , gpa);
If there's an error, should this not return or something instead of continuing as if nothing is wrong?
virt/kvm/kvmi.c line 127
Quoted 2 lines
virt/kvm/kvmi.c new file +2
-0+ + m =kzalloc (sizeof (struct kvmi_mem_access ),GFP_KERNEL );
This should be "m = kzalloc(sizeof(*m), GFP_KERNEL);".
Quoted 151 lines
virt/kvm/kvmi.c new file +151
-0+if (!m) +return -KVM_ENOMEM ; + +INIT_LIST_HEAD (&m->link); + m->gfn = gfn; + m->access = access; + +mutex_lock (&ikvm->access_tree_lock); + __m =radix_tree_lookup (&ikvm->access_tree, m->gfn); +if (__m) { + __m->access = m->access; +if (list_empty (&__m->link)) +list_add_tail (&__m->link, &ikvm->access_list); + }else { +radix_tree_insert (&ikvm->access_tree, m->gfn, m); +list_add_tail (&m->link, &ikvm->access_list); + m =NULL ; + } +mutex_unlock (&ikvm->access_tree_lock); + +kfree (m); + +return 0 ; +} + +static bool kvmi_test_mem_access (struct kvm *kvm,unsigned long gpa, +u8 access) +{ +struct kvmi_mem_access *m; +struct kvmi *ikvm =IKVM (kvm); + +if (!ikvm) +return false; + +mutex_lock (&ikvm->access_tree_lock); + m =radix_tree_lookup (&ikvm->access_tree,gpa_to_gfn (gpa)); +mutex_unlock (&ikvm->access_tree_lock); + +/* +* We want to be notified only for violations involving access +* bits that we've specifically cleared +*/ +if (m && ((~m->access) & access)) +return true; + +return false; +} + +static struct kvmi_mem_access * +kvmi_get_mem_access_unlocked (struct kvm *kvm,const gfn_t gfn) +{ +return radix_tree_lookup (&IKVM (kvm)->access_tree, gfn); +} + +static bool is_introspected (struct kvmi *ikvm) +{ +return (ikvm && ikvm->sock); +} + +void kvmi_flush_mem_access (struct kvm *kvm) +{ +struct kvmi *ikvm =IKVM (kvm); + +if (!ikvm) +return ; + +mutex_lock (&ikvm->access_tree_lock); +while (!list_empty (&ikvm->access_list)) { +struct kvmi_mem_access *m = +list_first_entry (&ikvm->access_list, + struct kvmi_mem_access, link); + +list_del_init (&m->link); + +kvmi_apply_mem_access (kvm,NULL , m); + +if (m->access == full_access) { +radix_tree_delete (&ikvm->access_tree, m->gfn); +kfree (m); + } + } +mutex_unlock (&ikvm->access_tree_lock); +} + +static void kvmi_free_mem_access (struct kvm *kvm) +{ +void **slot; +struct radix_tree_iter iter; +struct kvmi *ikvm =IKVM (kvm); + +mutex_lock (&ikvm->access_tree_lock); +radix_tree_for_each_slot (slot, &ikvm->access_tree, &iter,0 ) { +struct kvmi_mem_access *m = *slot; + + m->access = full_access; +kvmi_apply_mem_access (kvm,NULL , m); + +radix_tree_delete (&ikvm->access_tree, m->gfn); +kfree (*slot); + } +mutex_unlock (&ikvm->access_tree_lock); +} + +static unsigned long *msr_mask (struct kvmi *ikvm,unsigned int *msr) +{ +switch (*msr) { + case 0 ... 0x1fff: +return ikvm->msr_mask.low; + case 0xc0000000 ... 0xc0001fff: + *msr &=0x1fff ; +return ikvm->msr_mask.high; + } +return NULL ; +} + +static bool test_msr_mask (struct kvmi *ikvm,unsigned int msr) +{ +unsigned long *mask =msr_mask (ikvm, &msr); + +if (!mask) +return false; +if (!test_bit (msr, mask)) +return false; + +return true; +} + +static int msr_control (struct kvmi *ikvm,unsigned int msr,bool enable) +{ +unsigned long *mask =msr_mask (ikvm, &msr); + +if (!mask) +return -KVM_EINVAL ; +if (enable) +set_bit (msr, mask); +else +clear_bit (msr, mask); +return 0 ; +} + +unsigned int kvmi_vcpu_mode (const struct kvm_vcpu *vcpu, +const struct kvm_sregs *sregs) +{ +unsigned int mode =0 ; + +if (is_long_mode ((struct kvm_vcpu *) vcpu)) { +if (sregs->cs.l) + mode =8 ; +else if (!sregs->cs.db) + mode =2 ; +else
virt/kvm/kvmi.c line 288
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ mode =4 ; + }else if (sregs->cr0 &X86_CR0_PE ) { +if (!sregs->cs.db) + mode =2 ; +else + mode =4 ; + }else if (!sregs->cs.db) + mode =2 ; +else + mode =4 ;
If one branch of a conditional uses braces, then all branches should (regardless of if they are only a single statements). The final "else if" and "else" blocks here should both be wrapped in braces.
Quoted 15 lines
virt/kvm/kvmi.c new file +15
-0+ +return mode; +} + +static int maybe_delayed_init (void ) +{ +if (wq) +return 0 ; + + wq =alloc_workqueue ("kvmi" ,WQ_CPU_INTENSIVE ,0 ); +if (!wq) +return -ENOMEM ; + +return 0 ; +}
virt/kvm/kvmi.c line 313
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ +int kvmi_init (void ) +{ +return 0 ; +} + +static void work_cb (struct work_struct *work) +{ + struct kvmi *ikvm = container_of(work, struct kvmi, work); +struct kvm *kvm = ikvm->kvm;
None of your other initial variable assignments are aligned like this. Any particular reason why this one is?
virt/kvm/kvmi.c line 316
Quoted 3 lines
virt/kvm/kvmi.c new file +3
-0+ +while (kvmi_msg_process (ikvm)) + ;
Typically if you're going to have an empty while block, you stick the semi-colon at the end of the while line. So this would be:
while (kvmi_msg_process(ikvm));
Quoted 61 lines
virt/kvm/kvmi.c new file +61
-0+ +/* We are no longer interested in any kind of events */ +atomic_set (&ikvm->event_mask,0 ); + +/* Clean-up for the next kvmi_hook() call */ + ikvm->cr_mask =0 ; +memset (&ikvm->msr_mask,0 ,sizeof (ikvm->msr_mask)); + +wakeup_events (kvm); + +/* Restore the spte access rights */ +/* Shouldn't wait for reconnection? */ +kvmi_free_mem_access (kvm); + +complete_all (&ikvm->finished); +} + +static void __alloc_vcpu_kvmi (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =kzalloc (sizeof (struct kvmi_vcpu ),GFP_KERNEL ); + +if (ivcpu) { +sema_init (&ivcpu->sem_requests,0 ); + +/* +* Make sure the ivcpu is initialized +* before making it visible. +*/ +smp_wmb (); + + vcpu->kvmi = ivcpu; + +kvmi_make_request (ivcpu,REQ_INIT ); +kvm_make_request (KVM_REQ_INTROSPECTION , vcpu); + } +} + +void kvmi_vcpu_init (struct kvm_vcpu *vcpu) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); + +if (is_introspected (ikvm)) { +mutex_lock (&vcpu->kvm->lock); +__alloc_vcpu_kvmi (vcpu); +mutex_unlock (&vcpu->kvm->lock); + } +} + +void kvmi_vcpu_uninit (struct kvm_vcpu *vcpu) +{ +kfree (IVCPU (vcpu)); +} + +static bool __alloc_kvmi (struct kvm *kvm) +{ +struct kvmi *ikvm =kzalloc (sizeof (struct kvmi ),GFP_KERNEL ); + +if (ikvm) { +INIT_LIST_HEAD (&ikvm->access_list); +mutex_init (&ikvm->access_tree_lock); +INIT_RADIX_TREE (&ikvm->access_tree,GFP_KERNEL );
virt/kvm/kvmi.c line 387
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+rwlock_init (&ikvm->sock_lock); +init_completion (&ikvm->finished); +INIT_WORK (&ikvm->work, work_cb); + + kvm->kvmi = ikvm; + ikvm->kvm = kvm;/* work_cb */ + } + +return (ikvm !=NULL ); +}
Would it maybe be better to just put a check for ikvm at the top and return false, otherwise do all the in the if body then return true?
Like this:
static bool __alloc_kvmi(struct kvm *kvm)
{
struct kvmi *ikvm = kzalloc(sizeof(struct kvmi), GFP_KERNEL);
if (!ikvm)
return false;
INIT_LIST_HEAD(&ikvm->access_list);
mutex_init(&ikvm->access_tree_lock);
INIT_RADIX_TREE(&ikvm->access_tree, GFP_KERNEL);
rwlock_init(&ikvm->sock_lock);
init_completion(&ikvm->finished);
INIT_WORK(&ikvm->work, work_cb);
kvm->kvmi = ikvm;
ikvm->kvm = kvm; /* work_cb */
return true;
}Quoted 29 lines
virt/kvm/kvmi.c new file +29
-0+ +static bool alloc_kvmi (struct kvm *kvm) +{ +bool done; + +mutex_lock (&kvm->lock); + done = ( +maybe_delayed_init () ==0 && +IKVM (kvm) ==NULL && +__alloc_kvmi (kvm) == true + ); +mutex_unlock (&kvm->lock); + +return done; +} + +static void alloc_all_kvmi_vcpu (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu; +int i; + +mutex_lock (&kvm->lock); +kvm_for_each_vcpu (i, vcpu, kvm) +if (!IKVM (vcpu)) +__alloc_vcpu_kvmi (vcpu); +mutex_unlock (&kvm->lock); +} + +static bool setup_socket (struct kvm *kvm,struct kvm_introspection *qemu)
virt/kvm/kvmi.c line 426
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+{ +struct kvmi *ikvm =IKVM (kvm); + +if (is_introspected (ikvm)) { +kvm_err ("Guest already introspected\n" ); +return false; + } + +if (!kvmi_msg_init (ikvm, qemu->fd)) +return false;
kvmi_msg_init assumes that ikvm is not NULL -- it makes no check and then does "WRITE_ONCE(ikvm->sock, sock)". is_introspected() does check if ikvm is NULL, but if it is, it returns false, which would still end up here. There should be a check that ikvm is not NULL before this if statement.
Quoted 50 lines
virt/kvm/kvmi.c new file +50
-0+ + ikvm->cmd_allow_mask =-1 ;/* TODO: qemu->commands; */ + ikvm->event_allow_mask =-1 ;/* TODO: qemu->events; */ + +alloc_all_kvmi_vcpu (kvm); +queue_work (wq, &ikvm->work); + +return true; +} + +/* +* When called from outside a page fault handler, this call should +* return ~0ull +*/ +static u64 kvmi_mmu_fault_gla (struct kvm_vcpu *vcpu,gpa_t gpa) +{ +u64 gla; +u64 gla_val; +u64 v; + +if (!vcpu->arch.gpa_available) +return ~0ull ; + + gla =kvm_mmu_fault_gla (vcpu); +if (gla == ~0ull ) +return gla; + gla_val = gla; + +/* Handle the potential overflow by returning ~0ull */ +if (vcpu->arch.gpa_val > gpa) { + v = vcpu->arch.gpa_val - gpa; +if (v > gla) + gla = ~0ull ; +else + gla -= v; + }else { + v = gpa - vcpu->arch.gpa_val; +if (v > (U64_MAX - gla)) + gla = ~0ull ; +else + gla += v; + } + +return gla; +} + +static bool kvmi_track_preread(struct kvm_vcpu *vcpu, gpa_t gpa, + u8 *new, + int bytes, + struct kvm_page_track_notifier_node *node,
virt/kvm/kvmi.c line 486
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ bool *data_ready) +{ +u64 gla; +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +bool ret = true; + +if (kvm_mmu_nested_guest_page_fault (vcpu)) +return ret; + gla =kvmi_mmu_fault_gla (vcpu, gpa); + ret =kvmi_page_fault_event (vcpu, gpa, gla,KVMI_PAGE_ACCESS_R );
Should you not check the value of ret here before proceeding?
Quoted 100 lines
virt/kvm/kvmi.c new file +100
-0+if (ivcpu && ivcpu->ctx_size >0 ) { +int s =min_t (int, bytes, ivcpu->ctx_size); + +memcpy (new, ivcpu->ctx_data, s); + ivcpu->ctx_size =0 ; + +if (*data_ready) +kvm_err ("Override custom data" ); + + *data_ready = true; + } + +return ret; +} + +static bool kvmi_track_prewrite (struct kvm_vcpu *vcpu,gpa_t gpa, +const u8 *new, +int bytes, +struct kvm_page_track_notifier_node *node) +{ +u64 gla; + +if (kvm_mmu_nested_guest_page_fault (vcpu)) +return true; + gla =kvmi_mmu_fault_gla (vcpu, gpa); +return kvmi_page_fault_event (vcpu, gpa, gla,KVMI_PAGE_ACCESS_W ); +} + +static bool kvmi_track_preexec (struct kvm_vcpu *vcpu,gpa_t gpa, +struct kvm_page_track_notifier_node *node) +{ +u64 gla; + +if (kvm_mmu_nested_guest_page_fault (vcpu)) +return true; + gla =kvmi_mmu_fault_gla (vcpu, gpa); + +return kvmi_page_fault_event (vcpu, gpa, gla,KVMI_PAGE_ACCESS_X ); +} + +static void kvmi_track_create_slot (struct kvm *kvm, +struct kvm_memory_slot *slot, +unsigned long npages, +struct kvm_page_track_notifier_node *node) +{ +struct kvmi *ikvm =IKVM (kvm); +gfn_t start = slot->base_gfn; +const gfn_t end = start + npages; + +if (!ikvm) +return ; + +mutex_lock (&ikvm->access_tree_lock); + +while (start < end) { +struct kvmi_mem_access *m; + + m =kvmi_get_mem_access_unlocked (kvm, start); +if (m) +kvmi_apply_mem_access (kvm, slot, m); + start++; + } + +mutex_unlock (&ikvm->access_tree_lock); +} + +static void kvmi_track_flush_slot (struct kvm *kvm,struct kvm_memory_slot *slot, +struct kvm_page_track_notifier_node *node) +{ +struct kvmi *ikvm =IKVM (kvm); +gfn_t start = slot->base_gfn; +const gfn_t end = start + slot->npages; + +if (!ikvm) +return ; + +mutex_lock (&ikvm->access_tree_lock); + +while (start < end) { +struct kvmi_mem_access *m; + + m =kvmi_get_mem_access_unlocked (kvm, start); +if (m) { +u8 prev_access = m->access; + + m->access = full_access; +kvmi_apply_mem_access (kvm, slot, m); + m->access = prev_access; + } + start++; + } + +mutex_unlock (&ikvm->access_tree_lock); +} + +static struct kvm_page_track_notifier_node kptn_node = { + .track_preread = kvmi_track_preread, + .track_prewrite = kvmi_track_prewrite, + .track_preexec = kvmi_track_preexec, + .track_create_slot = kvmi_track_create_slot,
virt/kvm/kvmi.c line 596
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ .track_flush_slot = kvmi_track_flush_slot +}; + +bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu) +{ +kvm_info ("Hooking vm with fd: %d\n" , qemu->fd); + +kvm_page_track_register_notifier (kvm, &kptn_node); + +return (alloc_kvmi (kvm) &&setup_socket (kvm, qemu));
Is this safe? It could return false if the alloc fails (in which case the caller has to do nothing) or if setting up the socket fails (in which case the caller needs to free the allocated kvmi).
Quoted 190 lines
virt/kvm/kvmi.c new file +190
-0+} + +void kvmi_destroy_vm (struct kvm *kvm) +{ +struct kvmi *ikvm =IKVM (kvm); + +if (ikvm) { +kvmi_msg_uninit (ikvm); + +mutex_destroy (&ikvm->access_tree_lock); +kfree (ikvm); + } + +kvmi_mem_destroy_vm (kvm); +} + +void kvmi_uninit (void ) +{ +if (wq) { +destroy_workqueue (wq); + wq =NULL ; + } +} + +void kvmi_get_msrs (struct kvm_vcpu *vcpu,struct kvmi_event *event) +{ +struct msr_data msr; + + msr.host_initiated = true; + + msr.index =MSR_IA32_SYSENTER_CS ; +kvm_get_msr (vcpu, &msr); + event->msrs.sysenter_cs = msr.data; + + msr.index =MSR_IA32_SYSENTER_ESP ; +kvm_get_msr (vcpu, &msr); + event->msrs.sysenter_esp = msr.data; + + msr.index =MSR_IA32_SYSENTER_EIP ; +kvm_get_msr (vcpu, &msr); + event->msrs.sysenter_eip = msr.data; + + msr.index =MSR_EFER ; +kvm_get_msr (vcpu, &msr); + event->msrs.efer = msr.data; + + msr.index =MSR_STAR ; +kvm_get_msr (vcpu, &msr); + event->msrs.star = msr.data; + + msr.index =MSR_LSTAR ; +kvm_get_msr (vcpu, &msr); + event->msrs.lstar = msr.data; + + msr.index =MSR_CSTAR ; +kvm_get_msr (vcpu, &msr); + event->msrs.cstar = msr.data; + + msr.index =MSR_IA32_CR_PAT ; +kvm_get_msr (vcpu, &msr); + event->msrs.pat = msr.data; +} + +static bool is_event_enabled (struct kvm *kvm,int event_bit) +{ +struct kvmi *ikvm =IKVM (kvm); + +return (ikvm && (atomic_read (&ikvm->event_mask) & event_bit)); +} + +static int kvmi_vcpu_kill (int sig,struct kvm_vcpu *vcpu) +{ +int err = -ESRCH ; +struct pid *pid; +struct 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; + +mutex_lock (&kvm->lock); +kvm_for_each_vcpu (i, vcpu, kvm) { +kvmi_vcpu_kill (SIGTERM , vcpu); + } +mutex_unlock (&kvm->lock); +} + +/* TODO: Do we need a return code ? */ +static void handle_common_event_actions (struct kvm_vcpu *vcpu,u32 action) +{ +switch (action) { +case KVMI_EVENT_ACTION_CRASH : +kvmi_vm_shutdown (vcpu->kvm); +break ; + +default : +kvm_err ("Unsupported event action: %d\n" , action); + } +} + +bool kvmi_cr_event (struct kvm_vcpu *vcpu,unsigned int cr, +unsigned long old_value,unsigned long *new_value) +{ +struct kvm *kvm = vcpu->kvm; +u64 ret_value; +u32 action; + +if (!is_event_enabled (kvm,KVMI_EVENT_CR )) +return true; +if (!test_bit (cr, &IKVM (kvm)->cr_mask)) +return true; +if (old_value == *new_value) +return true; + + action =kvmi_msg_send_cr (vcpu, cr, old_value, *new_value, &ret_value); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : + *new_value = ret_value; +return true; +default : +handle_common_event_actions (vcpu, action); + } + +return false; +} + +bool kvmi_msr_event (struct kvm_vcpu *vcpu,struct msr_data *msr) +{ +struct kvm *kvm = vcpu->kvm; +u64 ret_value; +u32 action; +struct msr_data old_msr = { .host_initiated = true, + .index = msr->index }; + +if (msr->host_initiated) +return true; +if (!is_event_enabled (kvm,KVMI_EVENT_MSR )) +return true; +if (!test_msr_mask (IKVM (kvm), msr->index)) +return true; +if (kvm_get_msr (vcpu, &old_msr)) +return true; +if (old_msr.data == msr->data) +return true; + + action =kvmi_msg_send_msr (vcpu, msr->index, old_msr.data, msr->data, + &ret_value); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : + msr->data = ret_value; +return true; +default : +handle_common_event_actions (vcpu, action); + } + +return false; +} + +void kvmi_xsetbv_event (struct kvm_vcpu *vcpu) +{ +u32 action; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_XSETBV )) +return ; + + action =kvmi_msg_send_xsetbv (vcpu); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +handle_common_event_actions (vcpu, action); + } +} + +bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva) +{ +u32 action;
virt/kvm/kvmi.c line 796
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+u64 gpa; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_BREAKPOINT )) +/* qemu will automatically reinject the breakpoint */ +return false; + + gpa =kvm_mmu_gva_to_gpa_read (vcpu, gva,NULL ); + +if (gpa ==UNMAPPED_GVA ) +kvm_err ("%s: invalid gva: %llx" , __func__, gva);
If the gpa is unmapped, shouldn't it return false rather than proceeding?
Quoted 69 lines
virt/kvm/kvmi.c new file +69
-0+ + action =kvmi_msg_send_bp (vcpu, gpa); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +case KVMI_EVENT_ACTION_RETRY : +/* rip was most likely adjusted past the INT 3 instruction */ +return true; +default : +handle_common_event_actions (vcpu, action); + } + +/* qemu will automatically reinject the breakpoint */ +return false; +} +EXPORT_SYMBOL (kvmi_breakpoint_event); + +#define KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT 24 +bool kvmi_is_agent_hypercall (struct kvm_vcpu *vcpu) +{ +unsigned long subfunc1, subfunc2; +bool longmode =is_64_bit_mode (vcpu); +unsigned long nr =kvm_register_read (vcpu,VCPU_REGS_RAX ); + +if (longmode) { + subfunc1 =kvm_register_read (vcpu,VCPU_REGS_RDI ); + subfunc2 =kvm_register_read (vcpu,VCPU_REGS_RSI ); + }else { + nr &=0xFFFFFFFF ; + subfunc1 =kvm_register_read (vcpu,VCPU_REGS_RBX ); + subfunc1 &=0xFFFFFFFF ; + subfunc2 =kvm_register_read (vcpu,VCPU_REGS_RCX ); + subfunc2 &=0xFFFFFFFF ; + } + +return (nr ==KVM_HC_XEN_HVM_OP + && subfunc1 ==KVM_HC_XEN_HVM_OP_GUEST_REQUEST_VM_EVENT + && subfunc2 ==0 ); +} + +void kvmi_hypercall_event (struct kvm_vcpu *vcpu) +{ +u32 action; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_HYPERCALL ) + || !kvmi_is_agent_hypercall (vcpu)) +return ; + + action =kvmi_msg_send_hypercall (vcpu); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +handle_common_event_actions (vcpu, action); + } +} + +bool kvmi_page_fault_event (struct kvm_vcpu *vcpu,unsigned long gpa, +unsigned long gva,u8 access) +{ +struct kvm *kvm = vcpu->kvm; +struct kvmi_vcpu *ivcpu; +bool trap_access, ret = true; +u32 ctx_size; +u64 old_rip; +u32 action; +
virt/kvm/kvmi.c line 875
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+if (!is_event_enabled (kvm,KVMI_EVENT_PAGE_FAULT )) +return true; + +/* Have we shown interest in this page? */ +if (!kvmi_test_mem_access (kvm, gpa, access)) +return true; + + ivcpu =IVCPU (vcpu); + ctx_size =sizeof (ivcpu->ctx_data); + old_rip =kvm_rip_read (vcpu);
Why are these assignments aligned liket this?
Quoted 88 lines
virt/kvm/kvmi.c new file +88
-0+ +if (!kvmi_msg_send_pf (vcpu, gpa, gva, access, &action, + &trap_access, + ivcpu->ctx_data, &ctx_size)) + goto out; + + ivcpu->ctx_size =0 ; + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : + ivcpu->ctx_size = ctx_size; +break ; +case KVMI_EVENT_ACTION_RETRY : + ret = false; +break ; +default : +handle_common_event_actions (vcpu, action); + } + +/* TODO: trap_access -> don't REPeat the instruction */ +out: +trace_kvmi_event_page_fault (gpa, gva, access, old_rip, action, +kvm_rip_read (vcpu), ctx_size); +return ret; +} + +bool kvmi_lost_exception (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (!ivcpu || !ivcpu->exception.injected) +return false; + + ivcpu->exception.injected =0 ; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_TRAP )) +return false; + +if ((vcpu->arch.exception.injected || vcpu->arch.exception.pending) + && vcpu->arch.exception.nr == ivcpu->exception.nr + && vcpu->arch.exception.error_code + == ivcpu->exception.error_code) +return false; + +return true; +} + +void kvmi_trap_event (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); +u32 vector, type, err; +u32 action; + +if (vcpu->arch.exception.pending) { + vector = vcpu->arch.exception.nr; + err = vcpu->arch.exception.error_code; + +if (kvm_exception_is_soft (vector)) + type =INTR_TYPE_SOFT_EXCEPTION ; +else + type =INTR_TYPE_HARD_EXCEPTION ; + }else if (vcpu->arch.interrupt.pending) { + vector = vcpu->arch.interrupt.nr; + err =0 ; + +if (vcpu->arch.interrupt.soft) + type =INTR_TYPE_SOFT_INTR ; +else + type =INTR_TYPE_EXT_INTR ; + }else { + vector =0 ; + type =0 ; + err =0 ; + } + +kvm_err ("New exception nr %d/%d err %x/%x addr %lx" , + vector, ivcpu->exception.nr, + err, ivcpu->exception.error_code, + vcpu->arch.cr2); + + action =kvmi_msg_send_trap (vcpu, vector, type, err, vcpu->arch.cr2); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +handle_common_event_actions (vcpu, action); + }
virt/kvm/kvmi.c line 973
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+} + +bool kvmi_descriptor_event (struct kvm_vcpu *vcpu,u32 info, +unsigned long exit_qualification, +unsigned char descriptor,unsigned char write) +{ +u32 action; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_DESCRIPTOR )) +return true;
How come it returns true here? The events below all return false from a similar condition check.
Quoted 154 lines
virt/kvm/kvmi.c new file +154
-0+ + action =kvmi_msg_send_descriptor (vcpu, info, exit_qualification, + descriptor, write); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +return true; +default : +handle_common_event_actions (vcpu, action); + } + +return false;/* TODO: double check this */ +} +EXPORT_SYMBOL (kvmi_descriptor_event); + +static bool kvmi_create_vcpu_event (struct kvm_vcpu *vcpu) +{ +u32 action; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_CREATE_VCPU )) +return false; + + action =kvmi_msg_send_create_vcpu (vcpu); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +handle_common_event_actions (vcpu, action); + } + +return true; +} + +static bool kvmi_pause_vcpu_event (struct kvm_vcpu *vcpu) +{ +u32 action; + +IVCPU (vcpu)->pause = false; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_PAUSE_VCPU )) +return false; + + action =kvmi_msg_send_pause_vcpu (vcpu); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +default : +handle_common_event_actions (vcpu, action); + } + +return true; +} + +/* TODO: refactor this function uto avoid recursive calls and the semaphore. */ +void kvmi_handle_request (struct kvm_vcpu *vcpu) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +while (ivcpu->ev_rpl_waiting + ||READ_ONCE (ivcpu->requests)) { + +down (&ivcpu->sem_requests); + +if (test_bit (REQ_INIT , &ivcpu->requests)) { +/* +* kvmi_create_vcpu_event() may call this function +* again and won't return unless there is no more work +* to be done. The while condition will be evaluated +* to false, but we explicitly exit the loop to avoid +* surprizing the reader more than we already did. +*/ +kvmi_clear_request (ivcpu,REQ_INIT ); +if (kvmi_create_vcpu_event (vcpu)) +break ; + }else if (test_bit (REQ_CMD , &ivcpu->requests)) { +kvmi_msg_handle_vcpu_cmd (vcpu); +/* it will clear the REQ_CMD bit */ +if (ivcpu->pause && !ivcpu->ev_rpl_waiting) { +/* Same warnings as with REQ_INIT. */ +if (kvmi_pause_vcpu_event (vcpu)) +break ; + } + }else if (test_bit (REQ_REPLY , &ivcpu->requests)) { +kvmi_clear_request (ivcpu,REQ_REPLY ); + ivcpu->ev_rpl_waiting = false; +if (ivcpu->have_delayed_regs) { +kvm_arch_vcpu_set_regs (vcpu, + &ivcpu->delayed_regs); + ivcpu->have_delayed_regs = false; + } +if (ivcpu->pause) { +/* Same warnings as with REQ_INIT. */ +if (kvmi_pause_vcpu_event (vcpu)) +break ; + } + }else if (test_bit (REQ_CLOSE , &ivcpu->requests)) { +kvmi_clear_request (ivcpu,REQ_CLOSE ); +break ; + }else { +kvm_err ("Unexpected request" ); + } + } + +kvmi_flush_mem_access (vcpu->kvm); +/* TODO: merge with kvmi_set_mem_access() */ +} + +int kvmi_cmd_get_cpuid (struct kvm_vcpu *vcpu,u32 function,u32 index, +u32 *eax,u32 *ebx,u32 *ecx,u32 *edx) +{ +struct kvm_cpuid_entry2 *e; + + e =kvm_find_cpuid_entry (vcpu, function, index); +if (!e) +return -KVM_ENOENT ; + + *eax = e->eax; + *ebx = e->ebx; + *ecx = e->ecx; + *edx = e->edx; + +return 0 ; +} + +int kvmi_cmd_get_guest_info (struct kvm_vcpu *vcpu,u16 *vcpu_cnt,u64 *tsc) +{ +/* +* Should we switch vcpu_cnt to unsigned int? +* If not, we should limit this to max u16 - 1 +*/ + *vcpu_cnt =atomic_read (&vcpu->kvm->online_vcpus); +if (kvm_has_tsc_control) + *tsc =1000ul * vcpu->arch.virtual_tsc_khz; +else + *tsc =0 ; + +return 0 ; +} + +static int get_first_vcpu (struct kvm *kvm,struct kvm_vcpu **vcpu) +{ +struct kvm_vcpu *v; + +if (!atomic_read (&kvm->online_vcpus)) +return -KVM_EINVAL ; + + v =kvm_get_vcpu (kvm,0 ); + +if (!v) +return -KVM_EINVAL ; + + *vcpu = v;
virt/kvm/kvmi.c line 1137
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ +return 0 ; +} + +int kvmi_cmd_get_registers (struct kvm_vcpu *vcpu,u32 *mode, +struct kvm_regs *regs, +struct kvm_sregs *sregs,struct kvm_msrs *msrs) +{ +struct kvm_msr_entry *msr = msrs->entries; +unsigned int n = msrs->nmsrs;
Again with randomly aligning variables...
virt/kvm/kvmi.c line 1143
Quoted 6 lines
virt/kvm/kvmi.c new file +6
-0+ +kvm_arch_vcpu_ioctl_get_regs (vcpu, regs); +kvm_arch_vcpu_ioctl_get_sregs (vcpu, sregs); + *mode =kvmi_vcpu_mode (vcpu, sregs); + +for (; n--; msr++) {
The conditional portion of this for loop appears to not be a conditional? Either way, this is a pretty ugly way to write this.
virt/kvm/kvmi.c line 1145
Quoted 2 lines
virt/kvm/kvmi.c new file +2
-0+struct msr_data m = { .index = msr->index }; +int err =kvm_get_msr (vcpu, &m);
And again with the alignment...
Quoted 9 lines
virt/kvm/kvmi.c new file +9
-0+ +if (err) +return -KVM_EINVAL ; + + msr->data = m.data; + } + +return 0 ; +}
virt/kvm/kvmi.c line 1164
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ +int kvmi_cmd_set_registers (struct kvm_vcpu *vcpu,const struct kvm_regs *regs) +{ +struct kvmi_vcpu *ivcpu =IVCPU (vcpu); + +if (ivcpu->ev_rpl_waiting) { +memcpy (&ivcpu->delayed_regs, regs,sizeof (ivcpu->delayed_regs)); + ivcpu->have_delayed_regs = true; + }else +kvm_err ("Drop KVMI_SET_REGISTERS" );
Since the if has braces, the else should too.
Quoted 73 lines
virt/kvm/kvmi.c new file +73
-0+return 0 ; +} + +int kvmi_cmd_get_page_access (struct kvm_vcpu *vcpu,u64 gpa,u8 *access) +{ +struct kvmi *ikvm =IKVM (vcpu->kvm); +struct kvmi_mem_access *m; + +mutex_lock (&ikvm->access_tree_lock); + m =kvmi_get_mem_access_unlocked (vcpu->kvm,gpa_to_gfn (gpa)); + *access = m ? m->access : full_access; +mutex_unlock (&ikvm->access_tree_lock); + +return 0 ; +} + +static bool is_vector_valid (u8 vector) +{ +return true; +} + +static bool is_gva_valid (struct kvm_vcpu *vcpu,u64 gva) +{ +return true; +} + +int kvmi_cmd_inject_exception (struct kvm_vcpu *vcpu,u8 vector, +bool error_code_valid,u16 error_code, +u64 address) +{ +struct x86_exception e = { + .vector = vector, + .error_code_valid = error_code_valid, + .error_code = error_code, + .address = address, + }; + +if (!(is_vector_valid (vector) &&is_gva_valid (vcpu, address))) +return -KVM_EINVAL ; + +if (e.vector ==PF_VECTOR ) +kvm_inject_page_fault (vcpu, &e); +else if (e.error_code_valid) +kvm_queue_exception_e (vcpu, e.vector, e.error_code); +else +kvm_queue_exception (vcpu, e.vector); + +if (IVCPU (vcpu)->exception.injected) +kvm_err ("Override exception" ); + +IVCPU (vcpu)->exception.injected =1 ; +IVCPU (vcpu)->exception.nr = e.vector; +IVCPU (vcpu)->exception.error_code = error_code_valid ? error_code :0 ; + +return 0 ; +} + +unsigned long gfn_to_hva_safe (struct kvm *kvm,gfn_t gfn) +{ +unsigned long hva; + +mutex_lock (&kvm->slots_lock); + hva =gfn_to_hva (kvm, gfn); +mutex_unlock (&kvm->slots_lock); + +return hva; +} + +static long get_user_pages_remote_unlocked (struct mm_struct *mm, +unsigned long start, +unsigned long nr_pages, +unsigned int gup_flags, +struct page **pages)
virt/kvm/kvmi.c line 1247
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+{ +long ret; +struct task_struct *tsk =NULL ; +struct vm_area_struct **vmas =NULL ; +int locked =1 ; + +down_read (&mm->mmap_sem); + ret = +get_user_pages_remote (tsk, mm, start, nr_pages, gup_flags, pages, + vmas, &locked);
Couldn't this line be "ret = get_user_pages_remote(..." and just break it on a different variable?
Quoted 84 lines
virt/kvm/kvmi.c new file +84
-0+if (locked) +up_read (&mm->mmap_sem); +return ret; +} + +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 err, ec; +unsigned long hva; +struct page *page =NULL ; +void *ptr_page =NULL , *ptr =NULL ; +size_t ptr_size =0 ; +struct kvm_vcpu *vcpu; + + ec =get_first_vcpu (kvm, &vcpu); + +if (ec) + goto out; + + hva =gfn_to_hva_safe (kvm,gpa_to_gfn (gpa)); + +if (kvm_is_error_hva (hva)) { + ec = -KVM_EINVAL ; + goto out; + } + +if (get_user_pages_remote_unlocked (kvm->mm, hva,1 ,0 , &page) !=1 ) { + ec = -KVM_EINVAL ; + goto out; + } + + ptr_page =kmap_atomic (page); + + ptr = ptr_page + (gpa & ~PAGE_MASK ); + ptr_size = size; + +out: + err =send (IKVM (kvm), ctx, ec, ptr, ptr_size); + +if (ptr_page) +kunmap_atomic (ptr_page); +if (page) +put_page (page); +return err; +} + +int kvmi_cmd_write_physical (struct kvm *kvm,u64 gpa,u64 size,const void *buf) +{ +int err; +unsigned long hva; +struct page *page; +void *ptr; +struct kvm_vcpu *vcpu; + + err =get_first_vcpu (kvm, &vcpu); + +if (err) +return err; + + hva =gfn_to_hva_safe (kvm,gpa_to_gfn (gpa)); + +if (kvm_is_error_hva (hva)) +return -KVM_EINVAL ; + +if (get_user_pages_remote_unlocked (kvm->mm, hva,1 ,FOLL_WRITE , + &page) !=1 ) +return -KVM_EINVAL ; + + ptr =kmap_atomic (page); + +memcpy (ptr + (gpa & ~PAGE_MASK ), buf, size); + +kunmap_atomic (ptr); +put_page (page); + +return 0 ; +} + +int kvmi_cmd_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token) +{ +int err =0 ;
virt/kvm/kvmi.c line 1341
Quoted 10 lines
virt/kvm/kvmi.c new file +10
-0+ +/* create random token */ +get_random_bytes (token,sizeof (struct kvmi_map_mem_token )); + +/* store token in HOST database */ +if (kvmi_store_token (kvm, token)) + err = -KVM_ENOMEM ; + +return err; +}
It seems like you could get rid of err altogether and just return -KVM_ENOMEM directly from the if body and 0 at the end.
Quoted 126 lines
virt/kvm/kvmi.c new file +69
-0+ +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,u32 events) +{ +int err =0 ; + +if (events & ~KVMI_KNOWN_EVENTS ) +return -KVM_EINVAL ; + +if (events &KVMI_EVENT_BREAKPOINT ) { +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_BREAKPOINT )) { +struct kvm_guest_debug dbg = { }; + + dbg.control = +KVM_GUESTDBG_ENABLE |KVM_GUESTDBG_USE_SW_BP ; + + err =kvm_arch_vcpu_ioctl_set_guest_debug (vcpu, &dbg); + } + } + +if (!err) +atomic_set (&IKVM (vcpu->kvm)->event_mask, events); + +return err; +} + +int kvmi_cmd_control_cr (struct kvmi *ikvm,bool enable,u32 cr) +{ +switch (cr) { +case 0 : +case 3 : +case 4 : +if (enable) +set_bit (cr, &ikvm->cr_mask); +else +clear_bit (cr, &ikvm->cr_mask); +return 0 ; + +default : +return -KVM_EINVAL ; + } +} + +int kvmi_cmd_control_msr (struct kvm *kvm,bool enable,u32 msr) +{ +struct kvm_vcpu *vcpu; +int err; + + err =get_first_vcpu (kvm, &vcpu); +if (err) +return err; + + err =msr_control (IKVM (kvm), msr, enable); + +if (!err) +kvm_arch_msr_intercept (vcpu, msr, enable); + +return err; +} + +void wakeup_events (struct kvm *kvm) +{ +int i; +struct kvm_vcpu *vcpu; + +mutex_lock (&kvm->lock); +kvm_for_each_vcpu (i, vcpu, kvm) +kvmi_make_request (IVCPU (vcpu),REQ_CLOSE ); +mutex_unlock (&kvm->lock); +}virt/kvm/kvmi_int.h new file +51
-0@@ -0,0 +1,121 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __KVMI_INT_H__ +#define __KVMI_INT_H__ + +#include <linux/types.h> +#include <linux/kvm_host.h> + +#include <uapi/linux/kvmi.h> + +#define IVCPU(vcpu) ((struct kvmi_vcpu *)((vcpu)->kvmi)) + +struct kvmi_vcpu { + u8 ctx_data[256]; + u32 ctx_size; + struct semaphore sem_requests; + unsigned long requests; + /* TODO: get this ~64KB buffer from a cache */ + u8 msg_buf[KVMI_MAX_MSG_SIZE]; + struct kvmi_event_reply ev_rpl; + void *ev_rpl_ptr; + size_t ev_rpl_size; + size_t ev_rpl_received; + u32 ev_seq; + bool ev_rpl_waiting; + struct { + u16 error_code; + u8 nr; + bool injected; + } exception; + struct kvm_regs delayed_regs; + bool have_delayed_regs; + bool pause; +}; + +#define IKVM(kvm) ((struct kvmi *)((kvm)->kvmi)) + +struct kvmi { + atomic_t event_mask; + unsigned long cr_mask; + struct { + unsigned long low[BITS_TO_LONGS(8192)]; + unsigned long high[BITS_TO_LONGS(8192)]; + } msr_mask; + struct radix_tree_root access_tree; + struct mutex access_tree_lock; + struct list_head access_list; + struct work_struct work; + struct socket *sock; + rwlock_t sock_lock; + struct completion finished; + struct kvm *kvm;
virt/kvm/kvmi_int.h line 61
Quoted 10 lines
virt/kvm/kvmi_int.h new file +10
-0+/* TODO: get this ~64KB buffer from a cache */ +u8 msg_buf[KVMI_MAX_MSG_SIZE ]; +u32 cmd_allow_mask; +u32 event_allow_mask; +}; + +#define REQ_INIT 0 +#define REQ_CMD 1 +#define REQ_REPLY 2 +#define REQ_CLOSE 3
Would these be better off being an enum?
Quoted 115 lines
virt/kvm/kvmi_int.h new file +60
-0+ +/* kvmi_msg.c */ +bool kvmi_msg_init (struct kvmi *ikvm,int fd); +bool kvmi_msg_process (struct kvmi *ikvm); +void kvmi_msg_uninit (struct kvmi *ikvm); +void kvmi_msg_handle_vcpu_cmd (struct kvm_vcpu *vcpu); +u32 kvmi_msg_send_cr (struct kvm_vcpu *vcpu,u32 cr,u64 old_value, +u64 new_value,u64 *ret_value); +u32 kvmi_msg_send_msr (struct kvm_vcpu *vcpu,u32 msr,u64 old_value, +u64 new_value,u64 *ret_value); +u32 kvmi_msg_send_xsetbv (struct kvm_vcpu *vcpu); +u32 kvmi_msg_send_bp (struct kvm_vcpu *vcpu,u64 gpa); +u32 kvmi_msg_send_hypercall (struct kvm_vcpu *vcpu); +bool kvmi_msg_send_pf (struct kvm_vcpu *vcpu,u64 gpa,u64 gva,u32 mode, +u32 *action,bool *trap_access,u8 *ctx, +u32 *ctx_size); +u32 kvmi_msg_send_trap (struct kvm_vcpu *vcpu,u32 vector,u32 type, +u32 error_code,u64 cr2); +u32 kvmi_msg_send_descriptor (struct kvm_vcpu *vcpu,u32 info, +u64 exit_qualification,u8 descriptor,u8 write); +u32 kvmi_msg_send_create_vcpu (struct kvm_vcpu *vcpu); +u32 kvmi_msg_send_pause_vcpu (struct kvm_vcpu *vcpu); + +/* kvmi.c */ +int kvmi_cmd_get_guest_info (struct kvm_vcpu *vcpu,u16 *vcpu_cnt,u64 *tsc); +int kvmi_cmd_pause_vcpu (struct kvm_vcpu *vcpu); +int kvmi_cmd_get_registers (struct kvm_vcpu *vcpu,u32 *mode, +struct kvm_regs *regs,struct kvm_sregs *sregs, +struct kvm_msrs *msrs); +int kvmi_cmd_set_registers (struct kvm_vcpu *vcpu,const struct kvm_regs *regs); +int kvmi_cmd_get_page_access (struct kvm_vcpu *vcpu,u64 gpa,u8 *access); +int kvmi_cmd_inject_exception (struct kvm_vcpu *vcpu,u8 vector, +bool error_code_valid,u16 error_code, +u64 address); +int kvmi_cmd_control_events (struct kvm_vcpu *vcpu,u32 events); +int kvmi_cmd_get_cpuid (struct kvm_vcpu *vcpu,u32 function,u32 index, +u32 *eax,u32 *ebx,u32 *rcx,u32 *edx); +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_alloc_token (struct kvm *kvm,struct kvmi_map_mem_token *token); +int kvmi_cmd_control_cr (struct kvmi *ikvm,bool enable,u32 cr); +int kvmi_cmd_control_msr (struct kvm *kvm,bool enable,u32 msr); +int kvmi_set_mem_access (struct kvm *kvm,u64 gpa,u8 access); +void kvmi_make_request (struct kvmi_vcpu *ivcpu,int req); +void kvmi_clear_request (struct kvmi_vcpu *ivcpu,int req); +unsigned int kvmi_vcpu_mode (const struct kvm_vcpu *vcpu, +const struct kvm_sregs *sregs); +void kvmi_get_msrs (struct kvm_vcpu *vcpu,struct kvmi_event *event); +unsigned long gfn_to_hva_safe (struct kvm *kvm,gfn_t gfn); +void kvmi_mem_destroy_vm (struct kvm *kvm); + +/* kvmi_mem.c */ +int kvmi_store_token (struct kvm *kvm,struct kvmi_map_mem_token *token); + +#endif virt/kvm/kvmi_mem.c new file +49
-0@@ -0,0 +1,730 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection memory mapping implementation +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +* Author: +* Mircea Cirjaliu <[email protected]> +*/ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/kvm_host.h> +#include <linux/rmap.h> +#include <linux/list.h> +#include <linux/slab.h> +#include <linux/pagemap.h> +#include <linux/swap.h> +#include <linux/spinlock.h> +#include <linux/printk.h> +#include <linux/kvmi.h> +#include <linux/huge_mm.h> + +#include <uapi/linux/kvmi.h> + +#include "kvmi_int.h" + + +static struct list_head mapping_list; +static spinlock_t mapping_lock; + +struct host_map { +struct list_head mapping_list; +gpa_t map_gpa; +struct kvm *machine; +gpa_t req_gpa; +}; + + +static struct list_head token_list; +static spinlock_t token_lock; + +struct token_entry { +struct list_head token_list; +struct kvmi_map_mem_token token; +struct kvm *kvm; +}; +
virt/kvm/kvmi_mem.c line 59
Quoted 10 lines
virt/kvm/kvmi_mem.c new file +10
-0+ +int kvmi_store_token (struct kvm *kvm,struct kvmi_map_mem_token *token) +{ +struct token_entry *tep; + +print_hex_dump_debug ("kvmi: new token " ,DUMP_PREFIX_NONE , +32 ,1 , token,sizeof (struct kvmi_map_mem_token ), + false); + + tep =kmalloc (sizeof (struct token_entry ),GFP_KERNEL );
tep = kmalloc(sizeof(*tep), GFP_KERNEL)
virt/kvm/kvmi_mem.c line 64
Quoted 5 lines
virt/kvm/kvmi_mem.c new file +5
-0+if (tep ==NULL ) +return -ENOMEM ; + +INIT_LIST_HEAD (&tep->token_list); +memcpy (&tep->token, token,sizeof (struct kvmi_map_mem_token ));
Here too it might be better to do "sizeof(*token)"
Quoted 58 lines
virt/kvm/kvmi_mem.c new file +58
-0+ tep->kvm = kvm; + +spin_lock (&token_lock); +list_add_tail (&tep->token_list, &token_list); +spin_unlock (&token_lock); + +return 0 ; +} + +static struct kvm *find_machine_at (struct kvm_vcpu *vcpu,gva_t tkn_gva) +{ +long result; +gpa_t tkn_gpa; +struct kvmi_map_mem_token token; +struct list_head *cur; +struct token_entry *tep, *found =NULL ; +struct kvm *target_kvm =NULL ; + +/* machine token is passed as pointer */ + tkn_gpa =kvm_mmu_gva_to_gpa_system (vcpu, tkn_gva,NULL ); +if (tkn_gpa ==UNMAPPED_GVA ) +return NULL ; + +/* copy token to local address space */ + result =kvm_read_guest (vcpu->kvm, tkn_gpa, &token,sizeof (token)); +if (IS_ERR_VALUE (result)) { +kvm_err ("kvmi: failed copying token from user\n" ); +return ERR_PTR (result); + } + +/* consume token & find the VM */ +spin_lock (&token_lock); +list_for_each (cur, &token_list) { + tep = list_entry(cur, struct token_entry, token_list); + +if (!memcmp (&token, &tep->token,sizeof (token))) { +list_del (&tep->token_list); + found = tep; +break ; + } + } +spin_unlock (&token_lock); + +if (found !=NULL ) { + target_kvm = found->kvm; +kfree (found); + } + +return target_kvm; +} + +static void remove_vm_token (struct kvm *kvm) +{ +struct list_head *cur, *next; +struct token_entry *tep; + +spin_lock (&token_lock); +list_for_each_safe (cur, next, &token_list) {
virt/kvm/kvmi_mem.c line 132
Quoted 10 lines
virt/kvm/kvmi_mem.c new file +10
-0+ tep = list_entry(cur, struct token_entry, token_list); + +if (tep->kvm == kvm) { +list_del (&tep->token_list); +kfree (tep); + } + } +spin_unlock (&token_lock); + +}
There's an extra blank line at the end of this function (before the brace).
virt/kvm/kvmi_mem.c line 139
Quoted 7 lines
virt/kvm/kvmi_mem.c new file +7
-0+ + +static int add_to_list (gpa_t map_gpa,struct kvm *machine,gpa_t req_gpa) +{ +struct host_map *map; + + map =kmalloc (sizeof (struct host_map ),GFP_KERNEL );
map = kmalloc(sizeof(*map), GFP_KERNEL);
Quoted 99 lines
virt/kvm/kvmi_mem.c new file +99
-0+if (map ==NULL ) +return -ENOMEM ; + +INIT_LIST_HEAD (&map->mapping_list); + map->map_gpa = map_gpa; + map->machine = machine; + map->req_gpa = req_gpa; + +spin_lock (&mapping_lock); +list_add_tail (&map->mapping_list, &mapping_list); +spin_unlock (&mapping_lock); + +return 0 ; +} + +static struct host_map *extract_from_list (gpa_t map_gpa) +{ +struct list_head *cur; +struct host_map *map; + +spin_lock (&mapping_lock); +list_for_each (cur, &mapping_list) { + map = list_entry(cur, struct host_map, mapping_list); + +/* found - extract and return */ +if (map->map_gpa == map_gpa) { +list_del (&map->mapping_list); +spin_unlock (&mapping_lock); + +return map; + } + } +spin_unlock (&mapping_lock); + +return NULL ; +} + +static void remove_vm_from_list (struct kvm *kvm) +{ +struct list_head *cur, *next; +struct host_map *map; + +spin_lock (&mapping_lock); + +list_for_each_safe (cur, next, &mapping_list) { + map = list_entry(cur, struct host_map, mapping_list); + +if (map->machine == kvm) { +list_del (&map->mapping_list); +kfree (map); + } + } + +spin_unlock (&mapping_lock); +} + +static void remove_entry (struct host_map *map) +{ +kfree (map); +} + + +static struct vm_area_struct *isolate_page_vma (struct vm_area_struct *vma, +unsigned long addr) +{ +int result; + +/* corner case */ +if (vma_pages (vma) ==1 ) +return vma; + +if (addr != vma->vm_start) { +/* first split only if address in the middle */ + result =split_vma (vma->vm_mm, vma, addr, false); +if (IS_ERR_VALUE ((long )result)) +return ERR_PTR ((long )result); + + vma =find_vma (vma->vm_mm, addr); +if (vma ==NULL ) +return ERR_PTR (-ENOENT ); + +/* corner case (again) */ +if (vma_pages (vma) ==1 ) +return vma; + } + + result =split_vma (vma->vm_mm, vma, addr +PAGE_SIZE , true); +if (IS_ERR_VALUE ((long )result)) +return ERR_PTR ((long )result); + + vma =find_vma (vma->vm_mm, addr); +if (vma ==NULL ) +return ERR_PTR (-ENOENT ); + +BUG_ON (vma_pages (vma) !=1 ); + +return vma; +} +
virt/kvm/kvmi_mem.c line 248
Quoted 10 lines
virt/kvm/kvmi_mem.c new file +10
-0+static int redirect_rmap (struct vm_area_struct *req_vma,struct page *req_page, +struct vm_area_struct *map_vma) +{ +int result; + +unlink_anon_vmas (map_vma); + + result =anon_vma_fork (map_vma, req_vma); +if (IS_ERR_VALUE ((long )result)) + goto out;
Why not just return result here?
Quoted 187 lines
virt/kvm/kvmi_mem.c new file +187
-0+ +page_dup_rmap (req_page, false); + +out: +return result; +} + +static int host_map_fix_ptes (struct vm_area_struct *map_vma,hva_t map_hva, +struct page *req_page,struct page *map_page) +{ +struct mm_struct *map_mm = map_vma->vm_mm; + +pmd_t *pmd; +pte_t *ptep; +spinlock_t *ptl; +pte_t newpte; + +unsigned long mmun_start; +unsigned long mmun_end; + +/* classic replace_page() code */ + pmd =mm_find_pmd (map_mm, map_hva); +if (!pmd) +return -EFAULT ; + + mmun_start = map_hva; + mmun_end = map_hva +PAGE_SIZE ; +mmu_notifier_invalidate_range_start (map_mm, mmun_start, mmun_end); + + ptep =pte_offset_map_lock (map_mm, pmd, map_hva, &ptl); + +/* create new PTE based on requested page */ + newpte =mk_pte (req_page, map_vma->vm_page_prot); + newpte =pte_set_flags (newpte,pte_flags (*ptep)); + +flush_cache_page (map_vma, map_hva,pte_pfn (*ptep)); +ptep_clear_flush_notify (map_vma, map_hva, ptep); +set_pte_at_notify (map_mm, map_hva, ptep, newpte); + +pte_unmap_unlock (ptep, ptl); + +mmu_notifier_invalidate_range_end (map_mm, mmun_start, mmun_end); + +return 0 ; +} + +static void discard_page (struct page *map_page) +{ +lock_page (map_page); +// TODO: put_anon_vma() ???? - should be here +page_remove_rmap (map_page, false); +if (!page_mapped (map_page)) +try_to_free_swap (map_page); +unlock_page (map_page); +put_page (map_page); +} + +static void kvmi_split_huge_pmd (struct vm_area_struct *req_vma, +hva_t req_hva,struct page *req_page) +{ +bool tail = false; + +/* move reference count from compound head... */ +if (PageTail (req_page)) { + tail = true; +put_page (req_page); + } + +if (PageCompound (req_page)) +split_huge_pmd_address (req_vma, req_hva, false,NULL ); + +/* ... to the actual page, after splitting */ +if (tail) +get_page (req_page); +} + +static int kvmi_map_action (struct mm_struct *req_mm,hva_t req_hva, +struct mm_struct *map_mm,hva_t map_hva) +{ +struct vm_area_struct *req_vma; +struct page *req_page =NULL ; + +struct vm_area_struct *map_vma; +struct page *map_page; + +long nrpages; +int result =0 ; + +/* VMAs will be modified */ +down_write (&req_mm->mmap_sem); +down_write (&map_mm->mmap_sem); + +/* get host page corresponding to requested address */ + nrpages =get_user_pages_remote (NULL , req_mm, + req_hva,1 ,0 , + &req_page, &req_vma,NULL ); +if (nrpages ==0 ) { +kvm_err ("kvmi: no page for req_hva %016lx\n" , req_hva); + result = -ENOENT ; + goto out_err; + }else if (IS_ERR_VALUE (nrpages)) { + result = nrpages; +kvm_err ("kvmi: get_user_pages_remote() failed with result %d\n" , + result); + goto out_err; + } + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (req_page,"req_page before remap" ); + +/* find (not get) local page corresponding to target address */ + map_vma =find_vma (map_mm, map_hva); +if (map_vma ==NULL ) { +kvm_err ("kvmi: no local VMA found for remapping\n" ); + result = -ENOENT ; + goto out_err; + } + + map_page =follow_page (map_vma, map_hva,0 ); +if (IS_ERR_VALUE (map_page)) { + result =PTR_ERR (map_page); +kvm_debug ("kvmi: follow_page() failed with result %d\n" , + result); + goto out_err; + }else if (map_page ==NULL ) { + result = -ENOENT ; +kvm_debug ("kvmi: follow_page() returned no page\n" ); + goto out_err; + } + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (map_page,"map_page before remap" ); + +/* split local VMA for rmap redirecting */ + map_vma =isolate_page_vma (map_vma, map_hva); +if (IS_ERR_VALUE (map_vma)) { + result =PTR_ERR (map_vma); +kvm_debug ("kvmi: isolate_page_vma() failed with result %d\n" , + result); + goto out_err; + } + +/* split remote huge page */ +kvmi_split_huge_pmd (req_vma, req_hva, req_page); + +/* re-link VMAs */ + result =redirect_rmap (req_vma, req_page, map_vma); +if (IS_ERR_VALUE ((long )result)) + goto out_err; + +/* also redirect page tables */ + result =host_map_fix_ptes (map_vma, map_hva, req_page, map_page); +if (IS_ERR_VALUE ((long )result)) + goto out_err; + +/* the old page will be discarded */ +discard_page (map_page); +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (map_page,"map_page after being discarded" ); + +/* done */ + goto out_finalize; + +out_err: +/* get_user_pages_remote() incremented page reference count */ +if (req_page !=NULL ) +put_page (req_page); + +out_finalize: +/* release semaphores in reverse order */ +up_write (&map_mm->mmap_sem); +up_write (&req_mm->mmap_sem); + +return result; +} + +int kvmi_host_mem_map (struct kvm_vcpu *vcpu,gva_t tkn_gva, +gpa_t req_gpa,gpa_t map_gpa) +{ +int result =0 ; +struct kvm *target_kvm; + +gfn_t req_gfn; +hva_t req_hva; +struct mm_struct *req_mm; + +gfn_t map_gfn;
virt/kvm/kvmi_mem.c line 445
Quoted 10 lines
virt/kvm/kvmi_mem.c new file +10
-0+hva_t map_hva; +struct mm_struct *map_mm = vcpu->kvm->mm; + +kvm_debug ("kvmi: mapping request req_gpa %016llx, map_gpa %016llx\n" , + req_gpa, map_gpa); + +/* get the struct kvm * corresponding to the token */ + target_kvm =find_machine_at (vcpu, tkn_gva); +if (IS_ERR_VALUE (target_kvm)) +return PTR_ERR (target_kvm);
Since the else if block below has braces, this if block should have braces too.
Quoted 947 lines
virt/kvm/kvmi_mem.c new file +285
-0+else if (target_kvm ==NULL ) { +kvm_err ("kvmi: unable to find target machine\n" ); +return -ENOENT ; + } +kvm_get_kvm (target_kvm); + req_mm = target_kvm->mm; + +/* translate source addresses */ + req_gfn =gpa_to_gfn (req_gpa); + req_hva =gfn_to_hva_safe (target_kvm, req_gfn); +if (kvm_is_error_hva (req_hva)) { +kvm_err ("kvmi: invalid req HVA %016lx\n" , req_hva); + result = -EFAULT ; + goto out; + } + +kvm_debug ("kvmi: req_gpa %016llx, req_gfn %016llx, req_hva %016lx\n" , + req_gpa, req_gfn, req_hva); + +/* translate destination addresses */ + map_gfn =gpa_to_gfn (map_gpa); + map_hva =gfn_to_hva_safe (vcpu->kvm, map_gfn); +if (kvm_is_error_hva (map_hva)) { +kvm_err ("kvmi: invalid map HVA %016lx\n" , map_hva); + result = -EFAULT ; + goto out; + } + +kvm_debug ("kvmi: map_gpa %016llx, map_gfn %016llx, map_hva %016lx\n" , + map_gpa, map_gfn, map_hva); + +/* go to step 2 */ + result =kvmi_map_action (req_mm, req_hva, map_mm, map_hva); +if (IS_ERR_VALUE ((long )result)) + goto out; + +/* add mapping to list */ + result =add_to_list (map_gpa, target_kvm, req_gpa); +if (IS_ERR_VALUE ((long )result)) + goto out; + +/* all fine */ +kvm_debug ("kvmi: mapping of req_gpa %016llx successful\n" , req_gpa); + +out: +/* mandatory dec refernce count */ +kvm_put_kvm (target_kvm); + +return result; +} + + +static int restore_rmap (struct vm_area_struct *map_vma,hva_t map_hva, +struct page *req_page,struct page *new_page) +{ +int result; + +/* decouple links to anon_vmas */ +unlink_anon_vmas (map_vma); + map_vma->anon_vma =NULL ; + +/* allocate new anon_vma */ + result =anon_vma_prepare (map_vma); +if (IS_ERR_VALUE ((long )result)) +return result; + +lock_page (new_page); +page_add_new_anon_rmap (new_page, map_vma, map_hva, false); +unlock_page (new_page); + +/* decrease req_page mapcount */ +atomic_dec (&req_page->_mapcount); + +return 0 ; +} + +static int host_unmap_fix_ptes (struct vm_area_struct *map_vma,hva_t map_hva, +struct page *new_page) +{ +struct mm_struct *map_mm = map_vma->vm_mm; +pmd_t *pmd; +pte_t *ptep; +spinlock_t *ptl; +pte_t newpte; + +unsigned long mmun_start; +unsigned long mmun_end; + +/* page replacing code */ + pmd =mm_find_pmd (map_mm, map_hva); +if (!pmd) +return -EFAULT ; + + mmun_start = map_hva; + mmun_end = map_hva +PAGE_SIZE ; +mmu_notifier_invalidate_range_start (map_mm, mmun_start, mmun_end); + + ptep =pte_offset_map_lock (map_mm, pmd, map_hva, &ptl); + + newpte =mk_pte (new_page, map_vma->vm_page_prot); + newpte =pte_set_flags (newpte,pte_flags (*ptep)); + +/* clear cache & MMU notifier entries */ +flush_cache_page (map_vma, map_hva,pte_pfn (*ptep)); +ptep_clear_flush_notify (map_vma, map_hva, ptep); +set_pte_at_notify (map_mm, map_hva, ptep, newpte); + +pte_unmap_unlock (ptep, ptl); + +mmu_notifier_invalidate_range_end (map_mm, mmun_start, mmun_end); + +return 0 ; +} + +static int kvmi_unmap_action (struct mm_struct *req_mm, +struct mm_struct *map_mm,hva_t map_hva) +{ +struct vm_area_struct *map_vma; +struct page *req_page =NULL ; +struct page *new_page =NULL ; + +int result; + +/* VMAs will be modified */ +down_write (&req_mm->mmap_sem); +down_write (&map_mm->mmap_sem); + +/* find destination VMA for mapping */ + map_vma =find_vma (map_mm, map_hva); +if (map_vma ==NULL ) { + result = -ENOENT ; +kvm_err ("kvmi: no local VMA found for unmapping\n" ); + goto out_err; + } + +/* find (not get) page mapped to destination address */ + req_page =follow_page (map_vma, map_hva,0 ); +if (IS_ERR_VALUE (req_page)) { + result =PTR_ERR (req_page); +kvm_err ("kvmi: follow_page() failed with result %d\n" , result); + goto out_err; + }else if (req_page ==NULL ) { + result = -ENOENT ; +kvm_err ("kvmi: follow_page() returned no page\n" ); + goto out_err; + } + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (req_page,"req_page before decoupling" ); + +/* Returns NULL when no page can be allocated. */ + new_page =alloc_page_vma (GFP_HIGHUSER_MOVABLE , map_vma, map_hva); +if (new_page ==NULL ) { + result = -ENOMEM ; + goto out_err; + } + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (new_page,"new_page after allocation" ); + +/* should fix the rmap tree */ + result =restore_rmap (map_vma, map_hva, req_page, new_page); +if (IS_ERR_VALUE ((long )result)) + goto out_err; + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (req_page,"req_page after decoupling" ); + +/* page table fixing here */ + result =host_unmap_fix_ptes (map_vma, map_hva, new_page); +if (IS_ERR_VALUE ((long )result)) + goto out_err; + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (new_page,"new_page after unmapping" ); + + goto out_finalize; + +out_err: +if (new_page !=NULL ) +put_page (new_page); + +out_finalize: +/* reference count was inc during get_user_pages_remote() */ +if (req_page !=NULL ) { +put_page (req_page); + +if (IS_ENABLED (CONFIG_DEBUG_VM )) +dump_page (req_page,"req_page after release" ); + } + +/* release semaphores in reverse order */ +up_write (&map_mm->mmap_sem); +up_write (&req_mm->mmap_sem); + +return result; +} + +int kvmi_host_mem_unmap (struct kvm_vcpu *vcpu,gpa_t map_gpa) +{ +struct kvm *target_kvm; +struct mm_struct *req_mm; + +struct host_map *map; +int result; + +gfn_t map_gfn; +hva_t map_hva; +struct mm_struct *map_mm = vcpu->kvm->mm; + +kvm_debug ("kvmi: unmap request for map_gpa %016llx\n" , map_gpa); + +/* get the struct kvm * corresponding to map_gpa */ + map =extract_from_list (map_gpa); +if (map ==NULL ) { +kvm_err ("kvmi: map_gpa %016llx not mapped\n" , map_gpa); +return -ENOENT ; + } + target_kvm = map->machine; +kvm_get_kvm (target_kvm); + req_mm = target_kvm->mm; + +kvm_debug ("kvmi: req_gpa %016llx of machine %016lx mapped in map_gpa %016llx\n" , + map->req_gpa, (unsigned long ) map->machine, map->map_gpa); + +/* address where we did the remapping */ + map_gfn =gpa_to_gfn (map_gpa); + map_hva =gfn_to_hva_safe (vcpu->kvm, map_gfn); +if (kvm_is_error_hva (map_hva)) { + result = -EFAULT ; +kvm_err ("kvmi: invalid HVA %016lx\n" , map_hva); + goto out; + } + +kvm_debug ("kvmi: map_gpa %016llx, map_gfn %016llx, map_hva %016lx\n" , + map_gpa, map_gfn, map_hva); + +/* go to step 2 */ + result =kvmi_unmap_action (req_mm, map_mm, map_hva); +if (IS_ERR_VALUE ((long )result)) + goto out; + +kvm_debug ("kvmi: unmap of map_gpa %016llx successful\n" , map_gpa); + +out: +kvm_put_kvm (target_kvm); + +/* remove entry whatever happens above */ +remove_entry (map); + +return result; +} + +void kvmi_mem_destroy_vm (struct kvm *kvm) +{ +kvm_debug ("kvmi: machine %016lx was torn down\n" , + (unsigned long ) kvm); + +remove_vm_from_list (kvm); +remove_vm_token (kvm); +} + + +int kvm_intro_host_init (void ) +{ +/* token database */ +INIT_LIST_HEAD (&token_list); +spin_lock_init (&token_lock); + +/* mapping database */ +INIT_LIST_HEAD (&mapping_list); +spin_lock_init (&mapping_lock); + +kvm_info ("kvmi: initialized host memory introspection\n" ); + +return 0 ; +} + +void kvm_intro_host_exit (void ) +{ +// ... +} + +module_init (kvm_intro_host_init) +module_exit (kvm_intro_host_exit)virt/kvm/kvmi_msg.c new file +656
-0@@ -0,0 +1,1134 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* KVM introspection +* +* Copyright (C) 2017 Bitdefender S.R.L. +* +*/ +#include <linux/file.h> +#include <linux/net.h> +#include <linux/kvm_host.h> +#include <linux/kvmi.h> +#include <asm/virtext.h> + +#include <uapi/linux/kvmi.h> +#include <uapi/asm/kvmi.h> + +#include "kvmi_int.h" + +#include <trace/events/kvmi.h> + +/* +* TODO: break these call paths +* kvmi.c work_cb +* kvmi_msg.c kvmi_dispatch_message +* kvmi.c kvmi_cmd_... / kvmi_make_request +* kvmi_msg.c kvmi_msg_reply +* +* kvmi.c kvmi_X_event +* kvmi_msg.c kvmi_send_event +* kvmi.c kvmi_handle_request +*/ + +/* TODO: move some of the code to arch/x86 */ + +static atomic_t seq_ev =ATOMIC_INIT (0 ); + +static u32 new_seq (void ) +{ +return atomic_inc_return (&seq_ev); +} + +static const char *event_str (unsigned int e) +{ +switch (e) { +case KVMI_EVENT_CR : +return "CR" ; +case KVMI_EVENT_MSR : +return "MSR" ; +case KVMI_EVENT_XSETBV : +return "XSETBV" ; +case KVMI_EVENT_BREAKPOINT : +return "BREAKPOINT" ; +case KVMI_EVENT_HYPERCALL : +return "HYPERCALL" ; +case KVMI_EVENT_PAGE_FAULT : +return "PAGE_FAULT" ; +case KVMI_EVENT_TRAP : +return "TRAP" ; +case KVMI_EVENT_DESCRIPTOR : +return "DESCRIPTOR" ; +case KVMI_EVENT_CREATE_VCPU : +return "CREATE_VCPU" ; +case KVMI_EVENT_PAUSE_VCPU : +return "PAUSE_VCPU" ; +default : +return "EVENT?" ; + } +} + +static const char *const msg_IDs[] = { + [KVMI_GET_VERSION ] ="KVMI_GET_VERSION" , + [KVMI_GET_GUEST_INFO ] ="KVMI_GET_GUEST_INFO" , + [KVMI_PAUSE_VCPU ] ="KVMI_PAUSE_VCPU" , + [KVMI_GET_REGISTERS ] ="KVMI_GET_REGISTERS" , + [KVMI_SET_REGISTERS ] ="KVMI_SET_REGISTERS" , + [KVMI_GET_PAGE_ACCESS ] ="KVMI_GET_PAGE_ACCESS" , + [KVMI_SET_PAGE_ACCESS ] ="KVMI_SET_PAGE_ACCESS" , + [KVMI_INJECT_EXCEPTION ] ="KVMI_INJECT_EXCEPTION" , + [KVMI_READ_PHYSICAL ] ="KVMI_READ_PHYSICAL" , + [KVMI_WRITE_PHYSICAL ] ="KVMI_WRITE_PHYSICAL" , + [KVMI_GET_MAP_TOKEN ] ="KVMI_GET_MAP_TOKEN" , + [KVMI_CONTROL_EVENTS ] ="KVMI_CONTROL_EVENTS" , + [KVMI_CONTROL_CR ] ="KVMI_CONTROL_CR" , + [KVMI_CONTROL_MSR ] ="KVMI_CONTROL_MSR" , + [KVMI_EVENT ] ="KVMI_EVENT" , + [KVMI_EVENT_REPLY ] ="KVMI_EVENT_REPLY" , + [KVMI_GET_CPUID ] ="KVMI_GET_CPUID" , + [KVMI_GET_XSAVE ] ="KVMI_GET_XSAVE" , +}; + +static size_t sizeof_get_registers (const void *r) +{ +const struct kvmi_get_registers *req = r; + +return sizeof (*req) +sizeof (req->msrs_idx[0 ]) * req->nmsrs; +} + +static size_t sizeof_get_page_access (const void *r) +{ +const struct kvmi_get_page_access *req = r; + +return sizeof (*req) +sizeof (req->gpa[0 ]) * req->count; +} + +static size_t sizeof_set_page_access (const void *r) +{ +const struct kvmi_set_page_access *req = r; + +return sizeof (*req) +sizeof (req->entries[0 ]) * req->count; +} + +static size_t sizeof_write_physical (const void *r) +{ +const struct kvmi_write_physical *req = r; + +return sizeof (*req) + req->size; +} + +static const struct { +size_t size; +size_t (*cbk_full_size)(const void *msg); +} msg_bytes[] = { + [KVMI_GET_VERSION ] = {0 ,NULL }, + [KVMI_GET_GUEST_INFO ] = {sizeof (struct kvmi_get_guest_info ),NULL }, + [KVMI_PAUSE_VCPU ] = {sizeof (struct kvmi_pause_vcpu ),NULL }, + [KVMI_GET_REGISTERS ] = {sizeof (struct kvmi_get_registers ), + sizeof_get_registers }, + [KVMI_SET_REGISTERS ] = {sizeof (struct kvmi_set_registers ),NULL }, + [KVMI_GET_PAGE_ACCESS ] = {sizeof (struct kvmi_get_page_access ), + sizeof_get_page_access }, + [KVMI_SET_PAGE_ACCESS ] = {sizeof (struct kvmi_set_page_access ), + sizeof_set_page_access }, + [KVMI_INJECT_EXCEPTION ] = {sizeof (struct kvmi_inject_exception ), +NULL }, + [KVMI_READ_PHYSICAL ] = {sizeof (struct kvmi_read_physical ),NULL }, + [KVMI_WRITE_PHYSICAL ] = {sizeof (struct kvmi_write_physical ), + sizeof_write_physical }, + [KVMI_GET_MAP_TOKEN ] = {0 ,NULL }, + [KVMI_CONTROL_EVENTS ] = {sizeof (struct kvmi_control_events ),NULL }, + [KVMI_CONTROL_CR ] = {sizeof (struct kvmi_control_cr ),NULL }, + [KVMI_CONTROL_MSR ] = {sizeof (struct kvmi_control_msr ),NULL }, + [KVMI_GET_CPUID ] = {sizeof (struct kvmi_get_cpuid ),NULL }, + [KVMI_GET_XSAVE ] = {sizeof (struct kvmi_get_xsave ),NULL }, +}; + +static int kvmi_sock_read (struct kvmi *ikvm,void *buf,size_t size) +{ +struct kvec i = { + .iov_base = buf, + .iov_len = size, + }; +struct msghdr m = { }; +int rc; + +read_lock (&ikvm->sock_lock); + +if (likely (ikvm->sock)) + rc =kernel_recvmsg (ikvm->sock, &m, &i,1 , size,MSG_WAITALL ); +else + rc = -EPIPE ; + +if (rc >0 ) +print_hex_dump_debug ("read: " ,DUMP_PREFIX_NONE ,32 ,1 , + buf, rc, false); + +read_unlock (&ikvm->sock_lock); + +if (unlikely (rc != size)) { +kvm_err ("kernel_recvmsg: %d\n" , rc); +if (rc >=0 ) + rc = -EPIPE ; +return rc; + } + +return 0 ; +} + +static int kvmi_sock_write (struct kvmi *ikvm,struct kvec *i,size_t n, +size_t size) +{ +struct msghdr m = { }; +int rc, k; + +read_lock (&ikvm->sock_lock); + +if (likely (ikvm->sock)) + rc =kernel_sendmsg (ikvm->sock, &m, i, n, size); +else + rc = -EPIPE ; + +for (k =0 ; k < n; k++) +print_hex_dump_debug ("write: " ,DUMP_PREFIX_NONE ,32 ,1 , + i[k].iov_base, i[k].iov_len, false); + +read_unlock (&ikvm->sock_lock); + +if (unlikely (rc != size)) { +kvm_err ("kernel_sendmsg: %d\n" , rc); +if (rc >=0 ) + rc = -EPIPE ; +return rc; + } + +return 0 ; +} + +static const char *id2str (int i) +{ +return (i <ARRAY_SIZE (msg_IDs) && msg_IDs[i] ? msg_IDs[i] :"unknown" ); +} + +static struct kvmi_vcpu *kvmi_vcpu_waiting_for_reply (struct kvm *kvm,u32 seq) +{ +struct kvmi_vcpu *found =NULL ; +struct kvm_vcpu *vcpu; +int i; + +mutex_lock (&kvm->lock); + +kvm_for_each_vcpu (i, vcpu, kvm) { +/* kvmi_send_event */ +smp_rmb (); +if (READ_ONCE (IVCPU (vcpu)->ev_rpl_waiting) + && seq ==IVCPU (vcpu)->ev_seq) { + found =IVCPU (vcpu); +break ; + } + } + +mutex_unlock (&kvm->lock); + +return found; +} + +static bool kvmi_msg_dispatch_reply (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg) +{ +struct kvmi_vcpu *ivcpu; +int err; + + ivcpu =kvmi_vcpu_waiting_for_reply (ikvm->kvm, msg->seq); +if (!ivcpu) { +kvm_err ("%s: unexpected event reply (seq=%u)\n" , __func__, + msg->seq); +return false; + } + +if (msg->size ==sizeof (ivcpu->ev_rpl) + ivcpu->ev_rpl_size) { + err =kvmi_sock_read (ikvm, &ivcpu->ev_rpl, +sizeof (ivcpu->ev_rpl)); +if (!err && ivcpu->ev_rpl_size) + err =kvmi_sock_read (ikvm, ivcpu->ev_rpl_ptr, + ivcpu->ev_rpl_size); + }else { +kvm_err ("%s: invalid event reply size (max=%zu, recv=%u, expected=%zu)\n" , + __func__, ivcpu->ev_rpl_size, msg->size, +sizeof (ivcpu->ev_rpl) + ivcpu->ev_rpl_size); + err =-1 ; + } + + ivcpu->ev_rpl_received = err ?-1 : ivcpu->ev_rpl_size; + +kvmi_make_request (ivcpu,REQ_REPLY ); + +return (err ==0 ); +} + +static bool consume_sock_bytes (struct kvmi *ikvm,size_t n) +{ +while (n) { +u8 buf[256 ]; +size_t chunk =min (n,sizeof (buf)); + +if (kvmi_sock_read (ikvm, buf, chunk) !=0 ) +return false; + + n -= chunk; + } + +return true; +} + +static int kvmi_msg_reply (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +int err,const void *rpl,size_t rpl_size) +{ +struct kvmi_error_code ec; +struct kvmi_msg_hdr h; +struct kvec vec[3 ] = { + {.iov_base = &h, .iov_len =sizeof (h) }, + {.iov_base = &ec, .iov_len =sizeof (ec)}, + {.iov_base = (void *) rpl, .iov_len = rpl_size }, + }; +size_t size =sizeof (h) +sizeof (ec) + (err ?0 : rpl_size); +size_t n = err ?ARRAY_SIZE (vec)-1 :ARRAY_SIZE (vec); + +memset (&h,0 ,sizeof (h)); + h.id = msg->id; + h.seq = msg->seq; + h.size = size -sizeof (h); + +memset (&ec,0 ,sizeof (ec)); + ec.err = err; + +return kvmi_sock_write (ikvm, vec, n, size); +} + +static int kvmi_msg_vcpu_reply (struct kvm_vcpu *vcpu, +const struct kvmi_msg_hdr *msg, +int err,const void *rpl,size_t size) +{ +/* +* As soon as we reply to this vCPU command, we can get another one, +* and we must signal that the incoming buffer (ivcpu->msg_buf) +* is ready by clearing this bit/request. +*/ +kvmi_clear_request (IVCPU (vcpu),REQ_CMD ); + +return kvmi_msg_reply (IKVM (vcpu->kvm), msg, err, rpl, size); +} + +bool kvmi_msg_init (struct kvmi *ikvm,int fd) +{ +struct socket *sock; +int r; + + sock =sockfd_lookup (fd, &r); + +if (!sock) { +kvm_err ("Invalid file handle: %d\n" , fd); +return false; + } + +WRITE_ONCE (ikvm->sock, sock); + +return true; +} + +void kvmi_msg_uninit (struct kvmi *ikvm) +{ +kvm_info ("Wake up the receiving thread\n" ); + +read_lock (&ikvm->sock_lock); + +if (ikvm->sock) +kernel_sock_shutdown (ikvm->sock,SHUT_RDWR ); + +read_unlock (&ikvm->sock_lock); + +kvm_info ("Wait for the receiving thread to complete\n" ); +wait_for_completion (&ikvm->finished); +} + +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_reply (ikvm, msg,0 , &rpl,sizeof (rpl)); +} + +static struct kvm_vcpu *kvmi_get_vcpu (struct kvmi *ikvm,int vcpu_id) +{ +struct kvm *kvm = ikvm->kvm; + +if (vcpu_id >=atomic_read (&kvm->online_vcpus)) +return NULL ; + +return kvm_get_vcpu (kvm, vcpu_id); +} + +static bool invalid_page_access (u64 gpa,u64 size) +{ +u64 off = gpa & ~PAGE_MASK ; + +return (size ==0 || size >PAGE_SIZE || off + size >PAGE_SIZE ); +} + +static int handle_read_physical (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +const struct kvmi_read_physical *req = _req; + +if (invalid_page_access (req->gpa, req->size)) +return -EINVAL ; + +return kvmi_cmd_read_physical (ikvm->kvm, req->gpa, req->size, + kvmi_msg_reply, msg); +} + +static int handle_write_physical (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg, +const void *_req) +{ +const struct kvmi_write_physical *req = _req; +int ec; + +if (invalid_page_access (req->gpa, req->size)) +return -EINVAL ; + + ec =kvmi_cmd_write_physical (ikvm->kvm, req->gpa, req->size, req->data); + +return kvmi_msg_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; + + ec =kvmi_cmd_alloc_token (ikvm->kvm, &rpl.token); + +return kvmi_msg_reply (ikvm, msg, ec, &rpl,sizeof (rpl)); +} + +static int handle_control_cr (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg,const void *_req) +{ +const struct kvmi_control_cr *req = _req; +int ec; + + ec =kvmi_cmd_control_cr (ikvm, req->enable, req->cr); + +return kvmi_msg_reply (ikvm, msg, ec,NULL ,0 ); +} + +static int handle_control_msr (struct kvmi *ikvm, +const struct kvmi_msg_hdr *msg,const void *_req) +{ +const struct kvmi_control_msr *req = _req; +int ec; + + ec =kvmi_cmd_control_msr (ikvm->kvm, req->enable, req->msr); + +return kvmi_msg_reply (ikvm, msg, ec,NULL ,0 ); +} +Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
virt/kvm/kvmi_msg.c line 453
Quoted 10 lines
virt/kvm/kvmi_msg.c new file +10
-0+/* +* These commands are executed on the receiving thread/worker. +*/ +static int (*const msg_vm[])(struct kvmi *,const struct kvmi_msg_hdr *, +const void *) = { + [KVMI_GET_VERSION ] = handle_get_version, + [KVMI_READ_PHYSICAL ] = handle_read_physical, + [KVMI_WRITE_PHYSICAL ] = handle_write_physical, + [KVMI_GET_MAP_TOKEN ] = handle_get_map_token, + [KVMI_CONTROL_CR ] = handle_control_cr,
Again, because the else block has braces, the if should too.
virt/kvm/kvmi_msg.c
Quoted 469 lines
virt/kvm/kvmi_msg.c new file +468
-0Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Part of the diff is not shown here. View the raw message.
Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
We've made changes in all the places pointed by you, but read below. Thanks again,
Adalbert
Quoted 44 lines · Patrick Colp
Quoted 43 lines · Adalber LazA?r
From: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
-kvmi_msg.c- ABI and socket related functions
-kvmi_mem.c- handle map/unmap requests from the introspector
-kvmi.c- all the otherThe new data used by this subsystem is attached to the 'kvm' and '
kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).Besides the KVMI system, this patch exports the
kvm_vcpu_ioctl_x86_get_xsave()and themm_find_pmd()functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.Signed-off-by Mihai DonE?u <[email protected]>Signed-off-by Adalbert LazA?r <[email protected]>Signed-off-by NicuE?or CA(R)E?u <[email protected]>Signed-off-by Mircea CA(R)rjaliu <[email protected]>Signed-off-by Marian Rotariu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-)create mode 100644
include/linux/kvmi.h
create mode 100644include/trace/events/kvmi.h
create mode 100644virt/kvm/kvmi.c
create mode 100644virt/kvm/kvmi_int.h
create mode 100644virt/kvm/kvmi_mem.c
create mode 100644virt/kvm/kvmi_msg.c
Quoted 12 lines
virt/kvm/kvmi.c line 125
Quoted 9 lines
virt/kvm/kvmi.c new file +9
-0+int kvmi_set_mem_access (struct kvm *kvm,u64 gpa,u8 access) +{ +struct kvmi_mem_access *m; +struct kvmi_mem_access *__m; +struct kvmi *ikvm =IKVM (kvm); +gfn_t gfn =gpa_to_gfn (gpa); + +if (kvm_is_error_hva (gfn_to_hva_safe (kvm, gfn))) +kvm_err ("Invalid gpa %llx (or memslot not available yet)" , gpa);If there's an error, should this not return or something instead of continuing as if nothing is wrong?
It was a debug message masqueraded as an error message to be logged in dmesg. The page will be tracked when the memslot becomes available.
Quoted 36 lines
Quoted 36 lines
virt/kvm/kvmi.c new file +36
-0+static bool alloc_kvmi (struct kvm *kvm) +{ +bool done; + +mutex_lock (&kvm->lock); + done = ( +maybe_delayed_init () ==0 && +IKVM (kvm) ==NULL && +__alloc_kvmi (kvm) == true + ); +mutex_unlock (&kvm->lock); + +return done; +} + +static void alloc_all_kvmi_vcpu (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu; +int i; + +mutex_lock (&kvm->lock); +kvm_for_each_vcpu (i, vcpu, kvm) +if (!IKVM (vcpu)) +__alloc_vcpu_kvmi (vcpu); +mutex_unlock (&kvm->lock); +} + +static bool setup_socket (struct kvm *kvm,struct kvm_introspection *qemu) +{ +struct kvmi *ikvm =IKVM (kvm); + +if (is_introspected (ikvm)) { +kvm_err ("Guest already introspected\n" ); +return false; + } +
Quoted 8 lines
virt/kvm/kvmi.c line 426
Quoted 2 lines
virt/kvm/kvmi.c new file +2
-0+if (!kvmi_msg_init (ikvm, qemu->fd)) +return false;
kvmi_msg_initassumes that ikvm is not NULL -- it makes no check and then does "WRITE_ONCE(ikvm->sock, sock)".is_introspected()does check if ikvm is NULL, but if it is, it returns false, which would still end up here. There should be a check that ikvm is not NULL before this if statement.
setup_socket() is called only when 'ikvm' is not NULL.
is_introspected() checks 'ikvm' because it is called from other contexts. The real check is ikvm->sock (to see if the 'command channel' is 'active').
Quoted 54 lines
Quoted 54 lines
virt/kvm/kvmi.c new file +54
-0+ + ikvm->cmd_allow_mask =-1 ;/* TODO: qemu->commands; */ + ikvm->event_allow_mask =-1 ;/* TODO: qemu->events; */ + +alloc_all_kvmi_vcpu (kvm); +queue_work (wq, &ikvm->work); + +return true; +} + +/* +* When called from outside a page fault handler, this call should +* return ~0ull +*/ +static u64 kvmi_mmu_fault_gla (struct kvm_vcpu *vcpu,gpa_t gpa) +{ +u64 gla; +u64 gla_val; +u64 v; + +if (!vcpu->arch.gpa_available) +return ~0ull ; + + gla =kvm_mmu_fault_gla (vcpu); +if (gla == ~0ull ) +return gla; + gla_val = gla; + +/* Handle the potential overflow by returning ~0ull */ +if (vcpu->arch.gpa_val > gpa) { + v = vcpu->arch.gpa_val - gpa; +if (v > gla) + gla = ~0ull ; +else + gla -= v; + }else { + v = gpa - vcpu->arch.gpa_val; +if (v > (U64_MAX - gla)) + gla = ~0ull ; +else + gla += v; + } + +return gla; +} + +static bool kvmi_track_preread (struct kvm_vcpu *vcpu,gpa_t gpa, +u8 *new, +int bytes, +struct kvm_page_track_notifier_node *node, +bool *data_ready) +{ +u64 gla; +struct kvmi_vcpu *ivcpu =IVCPU (vcpu);
Quoted 9 lines
virt/kvm/kvmi.c line 486
Quoted 6 lines
virt/kvm/kvmi.c new file +6
-0+bool ret = true; + +if (kvm_mmu_nested_guest_page_fault (vcpu)) +return ret; + gla =kvmi_mmu_fault_gla (vcpu, gpa); + ret =kvmi_page_fault_event (vcpu, gpa, gla,KVMI_PAGE_ACCESS_R );Should you not check the value of ret here before proceeding?
Indeed. These 'track' functions are new additions and aren't integrated well with kvmi_page_fault_event(). We'll change this. The code is ugly but 'safe' (ctx_size will be non-zero only with ret == true).
Quoted 18 lines
Quoted 18 lines
virt/kvm/kvmi.c new file +18
-0+if (ivcpu && ivcpu->ctx_size >0 ) { +int s =min_t (int, bytes, ivcpu->ctx_size); + +memcpy (new, ivcpu->ctx_data, s); + ivcpu->ctx_size =0 ; + +if (*data_ready) +kvm_err ("Override custom data" ); + + *data_ready = true; + } + +return ret; +} + +bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu) +{ +kvm_info ("Hooking vm with fd: %d\n" , qemu->fd);
Quoted 9 lines
virt/kvm/kvmi.c line 596
Quoted 4 lines
virt/kvm/kvmi.c new file +4
-0+ +kvm_page_track_register_notifier (kvm, &kptn_node); + +return (alloc_kvmi (kvm) &&setup_socket (kvm, qemu));Is this safe? It could return false if the alloc fails (in which case the caller has to do nothing) or if setting up the socket fails (in which case the caller needs to free the allocated kvmi).
If the socket fails for any reason (eg. the introspection tool is stopped == socket closed) 'the plan' is to signal QEMU to reconnect (and call kvmi_hook() again) or else let the introspected VM continue (and try to reconnect asynchronously).
I see that kvm_page_track_register_notifier() should not be called more than once.
Maybe we should rename this to kvmi_rehook() or kvmi_reconnect().
Quoted 7 lines
Quoted 7 lines
virt/kvm/kvmi.c new file +7
-0+bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva) +{ +u32 action; +u64 gpa; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_BREAKPOINT )) +/* qemu will automatically reinject the breakpoint */
Quoted 9 lines
virt/kvm/kvmi.c line 796
Quoted 6 lines
virt/kvm/kvmi.c new file +6
-0+return false; + + gpa =kvm_mmu_gva_to_gpa_read (vcpu, gva,NULL ); + +if (gpa ==UNMAPPED_GVA ) +kvm_err ("%s: invalid gva: %llx" , __func__, gva);If the gpa is unmapped, shouldn't it return false rather than proceeding?
This was just a debug message. I'm not sure if is possible for 'gpa' to be unmapped. Even so, the introspection tool should still be notified.
Quoted 9 lines
Quoted 9 lines
virt/kvm/kvmi.c new file +9
-0+ + action =kvmi_msg_send_bp (vcpu, gpa); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +case KVMI_EVENT_ACTION_RETRY : +/* rip was most likely adjusted past the INT 3 instruction */ +return true;
Quoted 8 lines
virt/kvm/kvmi.c line 813
Quoted 8 lines
virt/kvm/kvmi.c new file +8
-0+default : +handle_common_event_actions (vcpu, action); + } + +/* qemu will automatically reinject the breakpoint */ +return false; +} +EXPORT_SYMBOL (kvmi_breakpoint_event);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
Quoted 62 lines · Adalbert Laz����r
We've made changes in all the places pointed by you, but read below. Thanks again,
AdalbertQuoted 44 lines · Patrick Colp
Quoted 43 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
-kvmi_msg.c- ABI and socket related functions
-kvmi_mem.c- handle map/unmap requests from the introspector
-kvmi.c- all the otherThe new data used by this subsystem is attached to the 'kvm' and '
kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).Besides the KVMI system, this patch exports the
kvm_vcpu_ioctl_x86_get_xsave()and themm_find_pmd()functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.Signed-off-by Mihai Donțu <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Signed-off-by Mircea Cîrjaliu <[email protected]>Signed-off-by Marian Rotariu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-)create mode 100644
include/linux/kvmi.h
create mode 100644include/trace/events/kvmi.h
create mode 100644virt/kvm/kvmi.c
create mode 100644virt/kvm/kvmi_int.h
create mode 100644virt/kvm/kvmi_mem.c
create mode 100644virt/kvm/kvmi_msg.cQuoted 12 lines
virt/kvm/kvmi.c line 125
Quoted 9 lines
virt/kvm/kvmi.c new file +9
-0+int kvmi_set_mem_access (struct kvm *kvm,u64 gpa,u8 access) +{ +struct kvmi_mem_access *m; +struct kvmi_mem_access *__m; +struct kvmi *ikvm =IKVM (kvm); +gfn_t gfn =gpa_to_gfn (gpa); + +if (kvm_is_error_hva (gfn_to_hva_safe (kvm, gfn))) +kvm_err ("Invalid gpa %llx (or memslot not available yet)" , gpa);If there's an error, should this not return or something instead of continuing as if nothing is wrong?
Quoted 2 lines
It was a debug message masqueraded as an error message to be logged in dmesg. The page will be tracked when the memslot becomes available.
I began to wonder if that's what was going on afterwards (I saw kvm_err() used in some other places where it was more obvious that it was debug messages).
Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
virt/kvm/kvmi.c new file +36
-0+static bool alloc_kvmi (struct kvm *kvm) +{ +bool done; + +mutex_lock (&kvm->lock); + done = ( +maybe_delayed_init () ==0 && +IKVM (kvm) ==NULL && +__alloc_kvmi (kvm) == true + ); +mutex_unlock (&kvm->lock); + +return done; +} + +static void alloc_all_kvmi_vcpu (struct kvm *kvm) +{ +struct kvm_vcpu *vcpu; +int i; + +mutex_lock (&kvm->lock); +kvm_for_each_vcpu (i, vcpu, kvm) +if (!IKVM (vcpu)) +__alloc_vcpu_kvmi (vcpu); +mutex_unlock (&kvm->lock); +} + +static bool setup_socket (struct kvm *kvm,struct kvm_introspection *qemu) +{ +struct kvmi *ikvm =IKVM (kvm); + +if (is_introspected (ikvm)) { +kvm_err ("Guest already introspected\n" ); +return false; + } +Quoted 8 lines
virt/kvm/kvmi.c line 426
Quoted 2 lines
virt/kvm/kvmi.c new file +2
-0+if (!kvmi_msg_init (ikvm, qemu->fd)) +return false;
kvmi_msg_initassumes that ikvm is not NULL -- it makes no check and then does "WRITE_ONCE(ikvm->sock, sock)".is_introspected()does check if ikvm is NULL, but if it is, it returns false, which would still end up here. There should be a check that ikvm is not NULL before this if statement.
Quoted 1 line
setup_socket()is called only when 'ikvm' is not NULL.
Ah, right. Forgot to check that :)
Quoted 3 lines
is_introspected()checks 'ikvm' because it is called from other contexts. The real check isikvm->sock(to see if the 'command channel' is 'active').
Yes, that makes more sense.
Quoted 65 lines
Quoted 55 lines
Quoted 55 lines
virt/kvm/kvmi.c new file +54
-0+ + ikvm->cmd_allow_mask =-1 ;/* TODO: qemu->commands; */ + ikvm->event_allow_mask =-1 ;/* TODO: qemu->events; */ + +alloc_all_kvmi_vcpu (kvm); +queue_work (wq, &ikvm->work); + +return true; +} + +/* +* When called from outside a page fault handler, this call should +* return ~0ull +*/ +static u64 kvmi_mmu_fault_gla (struct kvm_vcpu *vcpu,gpa_t gpa) +{ +u64 gla; +u64 gla_val; +u64 v; + +if (!vcpu->arch.gpa_available) +return ~0ull ; + + gla =kvm_mmu_fault_gla (vcpu); +if (gla == ~0ull ) +return gla; + gla_val = gla; + +/* Handle the potential overflow by returning ~0ull */ +if (vcpu->arch.gpa_val > gpa) { + v = vcpu->arch.gpa_val - gpa; +if (v > gla) + gla = ~0ull ; +else + gla -= v; + }else { + v = gpa - vcpu->arch.gpa_val; +if (v > (U64_MAX - gla)) + gla = ~0ull ; +else + gla += v; + } + +return gla; +} + +static bool kvmi_track_preread (struct kvm_vcpu *vcpu,gpa_t gpa, +u8 *new, +int bytes, +struct kvm_page_track_notifier_node *node, +bool *data_ready) +{ +u64 gla; +struct kvmi_vcpu *ivcpu =IVCPU (vcpu);Quoted 9 lines
virt/kvm/kvmi.c line 486
Quoted 6 lines
virt/kvm/kvmi.c new file +6
-0+bool ret = true; + +if (kvm_mmu_nested_guest_page_fault (vcpu)) +return ret; + gla =kvmi_mmu_fault_gla (vcpu, gpa); + ret =kvmi_page_fault_event (vcpu, gpa, gla,KVMI_PAGE_ACCESS_R );Should you not check the value of ret here before proceeding?
Quoted 4 lines
Indeed. These 'track' functions are new additions and aren't integrated well with
kvmi_page_fault_event(). We'll change this. The code is ugly but 'safe' (ctx_sizewill be non-zero only with ret == true).
Ah, yes, I can see that now. Not the most obvious interaction.
Quoted 33 lines
Quoted 18 lines
Quoted 18 lines
virt/kvm/kvmi.c new file +18
-0+if (ivcpu && ivcpu->ctx_size >0 ) { +int s =min_t (int, bytes, ivcpu->ctx_size); + +memcpy (new, ivcpu->ctx_data, s); + ivcpu->ctx_size =0 ; + +if (*data_ready) +kvm_err ("Override custom data" ); + + *data_ready = true; + } + +return ret; +} + +bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu) +{ +kvm_info ("Hooking vm with fd: %d\n" , qemu->fd);Quoted 9 lines
virt/kvm/kvmi.c line 596
Quoted 4 lines
virt/kvm/kvmi.c new file +4
-0+ +kvm_page_track_register_notifier (kvm, &kptn_node); + +return (alloc_kvmi (kvm) &&setup_socket (kvm, qemu));Is this safe? It could return false if the alloc fails (in which case the caller has to do nothing) or if setting up the socket fails (in which case the caller needs to free the allocated kvmi).
If the socket fails for any reason (eg. the introspection tool is stopped == socket closed) 'the plan' is to signal QEMU to reconnect (and call
kvmi_hook()again) or else let the introspected VM continue (and try to reconnect asynchronously).
Quoted 4 lines
I see that
kvm_page_track_register_notifier()should not be called more than once.Maybe we should rename this to
kvmi_rehook()orkvmi_reconnect().
I assume that a kvmi_rehook() function would then not call kvm_page_track_register_notifier() or at least have some check to make sure it only calls it once?
One approach would be to have separate kvmi_hook() and kvmi_rehook() functions. Another possibility is to have kvmi_hook() take an extra argument that's a boolean to specify if it's the first attempt at hooking or not.
Quoted 29 lines
Quoted 7 lines
Quoted 7 lines
virt/kvm/kvmi.c new file +7
-0+bool kvmi_breakpoint_event (struct kvm_vcpu *vcpu,u64 gva) +{ +u32 action; +u64 gpa; + +if (!is_event_enabled (vcpu->kvm,KVMI_EVENT_BREAKPOINT )) +/* qemu will automatically reinject the breakpoint */ Quoted 9 lines
virt/kvm/kvmi.c line 796
Quoted 6 lines
virt/kvm/kvmi.c new file +6
-0+return false; + + gpa =kvm_mmu_gva_to_gpa_read (vcpu, gva,NULL ); + +if (gpa ==UNMAPPED_GVA ) +kvm_err ("%s: invalid gva: %llx" , __func__, gva);If the gpa is unmapped, shouldn't it return false rather than proceeding?
This was just a debug message. I'm not sure if is possible for 'gpa' to be unmapped. Even so, the introspection tool should still be notified.
Quoted 9 lines
Quoted 9 lines
virt/kvm/kvmi.c new file +9
-0+ + action =kvmi_msg_send_bp (vcpu, gpa); + +switch (action) { +case KVMI_EVENT_ACTION_CONTINUE : +break ; +case KVMI_EVENT_ACTION_RETRY : +/* rip was most likely adjusted past the INT 3 instruction */ +return true;
Quoted 8 lines
Quoted 8 lines
virt/kvm/kvmi.c line 813
Quoted 8 lines
virt/kvm/kvmi.c new file +8
-0+default : +handle_common_event_actions (vcpu, action); + } + +/* qemu will automatically reinject the breakpoint */ +return false; +} +EXPORT_SYMBOL (kvmi_breakpoint_event);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
Quoted 73 lines · Patrick Colp
Quoted 67 lines · On 2017-12-22 09:11 AM, Adalbert Lazi? 1/2 i? 1/2 i? 1/2 i? 1/2 r wrote:
We've made changes in all the places pointed by you, but read below. Thanks again,
AdalbertQuoted 44 lines · Patrick Colp
Quoted 43 lines · Adalber LazA?r
From: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
-kvmi_msg.c- ABI and socket related functions
-kvmi_mem.c- handle map/unmap requests from the introspector
-kvmi.c- all the otherThe new data used by this subsystem is attached to the 'kvm' and '
kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).Besides the KVMI system, this patch exports the
kvm_vcpu_ioctl_x86_get_xsave()and themm_find_pmd()functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.Signed-off-by Mihai DonE?u <[email protected]>Signed-off-by Adalbert LazA?r <[email protected]>Signed-off-by NicuE?or CA(R)E?u <[email protected]>Signed-off-by Mircea CA(R)rjaliu <[email protected]>Signed-off-by Marian Rotariu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-)create mode 100644
include/linux/kvmi.h
create mode 100644include/trace/events/kvmi.h
create mode 100644virt/kvm/kvmi.c
create mode 100644virt/kvm/kvmi_int.h
create mode 100644virt/kvm/kvmi_mem.c
create mode 100644virt/kvm/kvmi_msg.cQuoted 12 lines
virt/kvm/kvmi.c line 596
Quoted 7 lines
virt/kvm/kvmi.c new file +7
-0+bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu) +{ +kvm_info ("Hooking vm with fd: %d\n" , qemu->fd); + +kvm_page_track_register_notifier (kvm, &kptn_node); + +return (alloc_kvmi (kvm) &&setup_socket (kvm, qemu));Is this safe? It could return false if the alloc fails (in which case the caller has to do nothing) or if setting up the socket fails (in which case the caller needs to free the allocated kvmi).
If the socket fails for any reason (eg. the introspection tool is stopped == socket closed) 'the plan' is to signal QEMU to reconnect (and call
kvmi_hook()again) or else let the introspected VM continue (and try to reconnect asynchronously).Quoted 4 lines
I see that
kvm_page_track_register_notifier()should not be called more than once.Maybe we should rename this to
kvmi_rehook()orkvmi_reconnect().
Quoted 9 lines
I assume that a
kvmi_rehook()function would then not callkvm_page_track_register_notifier()or at least have some check to make sure it only calls it once?One approach would be to have separate
kvmi_hook()andkvmi_rehook()functions. Another possibility is to havekvmi_hook()take an extra argument that's a boolean to specify if it's the first attempt at hooking or not.
alloc_kvmi() didn't worked with a second kvmi_hook() call.
For the moment I've changed the code to:
kvmi_hook
return (alloc_kvmi && setup_socket)
alloc_kvmi
return (IKVM(kvm) || __alloc_kvmi)
__alloc_kvmi
kzalloc
kvm_page_track_register_notifier
At least it works as 'advertised' :)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
Quoted 91 lines · On 2017-12-22 10:51 AM, [email protected] wrote:
Quoted 73 lines · Patrick Colp
Quoted 67 lines · Adalbert Laz����r
We've made changes in all the places pointed by you, but read below. Thanks again,
AdalbertQuoted 44 lines · Patrick Colp
Quoted 43 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This subsystem is split into three source files:
-kvmi_msg.c- ABI and socket related functions
-kvmi_mem.c- handle map/unmap requests from the introspector
-kvmi.c- all the otherThe new data used by this subsystem is attached to the 'kvm' and '
kvm_vcpu' structures as opaque pointers (to 'kvmi' and 'kvmi_vcpu' structures).Besides the KVMI system, this patch exports the
kvm_vcpu_ioctl_x86_get_xsave()and themm_find_pmd()functions, adds a new vCPU request (KVM_REQ_INTROSPECTION) and a new VM ioctl (KVM_INTROSPECTION) used to pass the connection file handle from QEMU.Signed-off-by Mihai Donțu <[email protected]>Signed-off-by Adalbert Lazăr <[email protected]>Signed-off-by Nicușor Cîțu <[email protected]>Signed-off-by Mircea Cîrjaliu <[email protected]>Signed-off-by Marian Rotariu <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 4 +- include/linux/kvm_host.h | 4 + include/linux/kvmi.h | 32 + include/linux/mm.h | 3 + include/trace/events/kvmi.h | 174 +++++ include/uapi/linux/kvm.h | 8 + mm/internal.h | 5 - virt/kvm/kvmi.c | 1410 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvmi_int.h | 121 ++++ virt/kvm/kvmi_mem.c | 730 ++++++++++++++++++++ virt/kvm/kvmi_msg.c | 1134 +++++++++++++++++++++++++++++++ 13 files changed, 3620 insertions(+), 7 deletions(-) create mode 100644 include/linux/kvmi.h create mode 100644 include/trace/events/kvmi.h 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_msg.cQuoted 12 lines
virt/kvm/kvmi.c line 596
Quoted 7 lines
virt/kvm/kvmi.c new file +7
-0+bool kvmi_hook (struct kvm *kvm,struct kvm_introspection *qemu) +{ +kvm_info ("Hooking vm with fd: %d\n" , qemu->fd); + +kvm_page_track_register_notifier (kvm, &kptn_node); + +return (alloc_kvmi (kvm) &&setup_socket (kvm, qemu));Is this safe? It could return false if the alloc fails (in which case the caller has to do nothing) or if setting up the socket fails (in which case the caller needs to free the allocated kvmi).
If the socket fails for any reason (eg. the introspection tool is stopped == socket closed) 'the plan' is to signal QEMU to reconnect (and call
kvmi_hook()again) or else let the introspected VM continue (and try to reconnect asynchronously).Quoted 4 lines
I see that
kvm_page_track_register_notifier()should not be called more than once.Maybe we should rename this to
kvmi_rehook()orkvmi_reconnect().Quoted 9 lines
I assume that a
kvmi_rehook()function would then not callkvm_page_track_register_notifier()or at least have some check to make sure it only calls it once?One approach would be to have separate
kvmi_hook()andkvmi_rehook()functions. Another possibility is to havekvmi_hook()take an extra argument that's a boolean to specify if it's the first attempt at hooking or not.
alloc_kvmi()didn't worked with a secondkvmi_hook()call.For the moment I've changed the code to:
kvmi_hookreturn (alloc_kvmi && setup_socket)
Quoted 8 lines
alloc_kvmireturn (IKVM(kvm) || __alloc_kvmi)
__alloc_kvmi
kzalloc
kvm_page_track_register_notifierAt least it works as 'advertised' :)
Yes, that seems a lot more clear :)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerRe: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
In reply to Adalber Lazărvirt/kvm/kvmi_mem.c line 340
Quoted 4 lines · Adalber Lazăr
virt/kvm/kvmi_mem.c new file +4
-0+/* VMAs will be modified */ +down_write (&req_mm->mmap_sem); +down_write (&map_mm->mmap_sem); +
Is there a locking rule when locking multiple mmap_sems at the same time? As it's written, this can cause deadlocks.
Paolo
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RE: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
DQoNCj4gLS0tLS1PcmlnaW5hbCBNZXNzYWdlLS0tLS0NCj4gRnJvbTogUGFvbG8gQm9uemluaSBb bWFpbHRvOnBib256aW5pQHJlZGhhdC5jb21dDQo+IFNlbnQ6IEZyaWRheSwgMjIgRGVjZW1iZXIg MjAxNyAxODowMg0KPiBUbzogQWRhbGJlciBMYXrEg3IgPGFsYXphckBiaXRkZWZlbmRlci5jb20+ OyBrdm1Admdlci5rZXJuZWwub3JnDQo+IENjOiBsaW51eC1tbUBrdmFjay5vcmc7IFJhZGltIEty xI1tw6HFmSA8cmtyY21hckByZWRoYXQuY29tPjsgWGlhbw0KPiBHdWFuZ3JvbmcgPGd1YW5ncm9u Zy54aWFvQGxpbnV4LmludGVsLmNvbT47IE1paGFpIERvbsibdQ0KPiA8bWRvbnR1QGJpdGRlZmVu ZGVyLmNvbT47IE5pY3Vzb3IgQ0lUVSA8bmNpdHVAYml0ZGVmZW5kZXIuY29tPjsNCj4gTWlyY2Vh IENJUkpBTElVLU1FTElVIDxtY2lyamFsaXVAYml0ZGVmZW5kZXIuY29tPjsgTWFyaWFuIENyaXN0 aWFuDQo+IFJPVEFSSVUgPG1yb3Rhcml1QGJpdGRlZmVuZGVyLmNvbT4NCj4gU3ViamVjdDogUmU6 IFtSRkMgUEFUQ0ggdjQgMDgvMThdIGt2bTogYWRkIHRoZSBWTSBpbnRyb3NwZWN0aW9uIHN1YnN5 c3RlbQ0KPiANCj4gT24gMTgvMTIvMjAxNyAyMDowNiwgQWRhbGJlciBMYXrEg3Igd3JvdGU6DQo+ ID4gKwkvKiBWTUFzIHdpbGwgYmUgbW9kaWZpZWQgKi8NCj4gPiArCWRvd25fd3JpdGUoJnJlcV9t bS0+bW1hcF9zZW0pOw0KPiA+ICsJZG93bl93cml0ZSgmbWFwX21tLT5tbWFwX3NlbSk7DQo+ID4g Kw0KPiANCj4gSXMgdGhlcmUgYSBsb2NraW5nIHJ1bGUgd2hlbiBsb2NraW5nIG11bHRpcGxlIG1t YXBfc2VtcyBhdCB0aGUgc2FtZQ0KPiB0aW1lPyAgQXMgaXQncyB3cml0dGVuLCB0aGlzIGNhbiBj YXVzZSBkZWFkbG9ja3MuDQoNCkZpcnN0IHJlcV9tbSwgc2Vjb25kIG1hcF9tbS4NClRoZSBvdGhl ciBmdW5jdGlvbiB1c2VzIHRoZSBzYW1lIG5lc3RpbmcuDQoNCj4gDQo+IFBhb2xvDQo+IA0KPiBf X19fX19fX19fX19fX19fX19fX19fX18NCj4gVGhpcyBlbWFpbCB3YXMgc2Nhbm5lZCBieSBCaXRk ZWZlbmRlcg0K
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Paolo Bonzini
maintainerRe: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
Quoted 22 lines · Mircea CIRJALIU-MELIU
Quoted 13 lines
-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Friday, 22 December 2017 18:02
To: Adalber Lazăr <[email protected]>; [email protected]
Cc: [email protected]; Radim Krčmář <[email protected]>; Xiao Guangrong <[email protected]>; Mihai Donțu <[email protected]>; Nicusor CITU <[email protected]>; Mircea CIRJALIU-MELIU <[email protected]>; Marian Cristian ROTARIU <[email protected]>
Subject: Re: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystemQuoted 8 lines
virt/kvm/kvmi_mem.c line 340
Quoted 4 lines · Adalber Lazăr
virt/kvm/kvmi_mem.c new file +4
-0+/* VMAs will be modified */ +down_write (&req_mm->mmap_sem); +down_write (&map_mm->mmap_sem); +Is there a locking rule when locking multiple
mmap_semsat the same time? As it's written, this can cause deadlocks.
Quoted 2 lines
First
req_mm, secondmap_mm.
The other function uses the same nesting.
You could have two tasks, both of which register themselves as the introspector of the other. That would cause a deadlock. There may be also other cases in the kernel that lock two VMAs at the same time, and you have to be consistent with those.
Usually what you do is comparing pointers and locking the lowest address first. Alternatively, you could have a separate lock that is taken by everyone who needs more than one lock, that is:
down_write(&some_mm->mmap_sem);
but:
mutex_lock(&locking_many_mmaps); down_write(&some_mm->mmap_sem); down_write(&another_mm->mmap_sem); mutex_unlock(&locking_many_mmaps); ... up_write(&some_mm->mmap_sem); up_write(&another_mm->mmap_sem);
However, I'm not sure how it works for mmap_sem. We'll have to ask the mm guys, let me Cc a few of them randomly.
Paolo
Paolo Bonzini
maintainerRe: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
In reply to Adalber LazărQuoted 7 lines · Adalber Lazăr
virt/kvm/kvmi_mem.c new file +7
-0+print_hex_dump_debug ("kvmi: new token " ,DUMP_PREFIX_NONE , +32 ,1 , token,sizeof (struct kvmi_map_mem_token ), + false); + + tep =kmalloc (sizeof (struct token_entry ),GFP_KERNEL ); +if (tep ==NULL ) +return -ENOMEM ;
virt/kvm/kvmi_mem.c line 71
Quoted 10 lines
virt/kvm/kvmi_mem.c new file +10
-0+ +INIT_LIST_HEAD (&tep->token_list); +memcpy (&tep->token, token,sizeof (struct kvmi_map_mem_token )); + tep->kvm = kvm; + +spin_lock (&token_lock); +list_add_tail (&tep->token_list, &token_list); +spin_unlock (&token_lock); + +return 0 ;
This allows unlimited allocations on the host from the introspector guest. You must only allow a fixed number of unconsumed tokens (e.g. 64).
Thanks,
Paolo
RE: [RFC PATCH v4 08/18] kvm: add the VM introspection subsystem
DQoNCj4gLS0tLS1PcmlnaW5hbCBNZXNzYWdlLS0tLS0NCj4gRnJvbTogUGFvbG8gQm9uemluaSBb bWFpbHRvOnBib256aW5pQHJlZGhhdC5jb21dDQo+IFNlbnQ6IEZyaWRheSwgMjIgRGVjZW1iZXIg MjAxNyAxODowOQ0KPiBUbzogQWRhbGJlciBMYXrEg3IgPGFsYXphckBiaXRkZWZlbmRlci5jb20+ OyBrdm1Admdlci5rZXJuZWwub3JnDQo+IENjOiBsaW51eC1tbUBrdmFjay5vcmc7IFJhZGltIEty xI1tw6HFmSA8cmtyY21hckByZWRoYXQuY29tPjsgWGlhbw0KPiBHdWFuZ3JvbmcgPGd1YW5ncm9u Zy54aWFvQGxpbnV4LmludGVsLmNvbT47IE1paGFpIERvbsibdQ0KPiA8bWRvbnR1QGJpdGRlZmVu ZGVyLmNvbT47IE5pY3Vzb3IgQ0lUVSA8bmNpdHVAYml0ZGVmZW5kZXIuY29tPjsNCj4gTWlyY2Vh IENJUkpBTElVLU1FTElVIDxtY2lyamFsaXVAYml0ZGVmZW5kZXIuY29tPjsgTWFyaWFuIENyaXN0 aWFuDQo+IFJPVEFSSVUgPG1yb3Rhcml1QGJpdGRlZmVuZGVyLmNvbT4NCj4gU3ViamVjdDogUmU6 IFtSRkMgUEFUQ0ggdjQgMDgvMThdIGt2bTogYWRkIHRoZSBWTSBpbnRyb3NwZWN0aW9uIHN1YnN5 c3RlbQ0KPiANCj4gT24gMTgvMTIvMjAxNyAyMDowNiwgQWRhbGJlciBMYXrEg3Igd3JvdGU6DQo+ ID4gKwlwcmludF9oZXhfZHVtcF9kZWJ1Zygia3ZtaTogbmV3IHRva2VuICIsIERVTVBfUFJFRklY X05PTkUsDQo+ID4gKwkJCSAgICAgMzIsIDEsIHRva2VuLCBzaXplb2Yo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IHRoZXNlIHRva2Vucz8NCiAqIEhhdmUgb25seSBvbmUg Zm9yIGV2ZXJ5IFZNPw0KICovDQoNCkkgc3VnZ2VzdCB1c2luZyB0aGUgdG9rZW4gYXMgYW4gYXV0 aGVudGljYXRpb24ga2V5IHdpdGggZmluaXRlIGxpZmUtdGltZSAoc2ltaWxhciB0byBhIGJhbmtp bmcgdG9rZW4pLg0KVGhlIGludHJvc3BlY3RvciAocHJvY2Vzcy90aHJlYWQpIGNhbiByZXF1ZXN0 IGEgbmV3IHRva2VuIGFzIHNvb24gYXMgdGhlIG9sZCBvbmUgZXhwaXJlcy4NClRoZSBpbnRyb3Nw ZWN0ZWQgbWFjaGluZSBzaG91bGRuJ3QgYmUgYXNzb2NpYXRlZCB3aXRoIHRoZSB0b2tlbiBpbiB0 aGlzIGNhc2UuDQoNCj4gVGhhbmtzLA0KPiANCj4gUGFvbG8NCj4gDQo+IF9fX19fX19fX19fX19f X19fX19fX19fXw0KPiBUaGlzIGVtYWlsIHdhcyBzY2FubmVkIGJ5IEJpdGRlZmVuZGVyDQo=
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 9/18 kvm: hook in the VM introspection subsystem
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Handle the new KVM_INTROSPECTION ioctl and pass the socket from QEMU to the KVMI subsystem. Notify KVMI on vCPU create/destroy and VM destroy events. Also, the EPT AD bits feature is disabled by this patch.
--- arch/x86/kvm/vmx.c | 3 ++- virt/kvm/kvm_main.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx.c +2 -1
@@ -34,6 +34,7 @@#include <linux/tboot.h> #include <linux/hrtimer.h> #include <linux/frame.h> +#include <linux/kvmi.h> #include "kvm_cache_regs.h" #include "x86.h"
@@ -6785,7 +6786,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)
virt/kvm/kvm_main.c +19 -0
@@ -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>
@@ -298,6 +299,9 @@ 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; + +kvmi_vcpu_init (vcpu); +return 0 ; fail_free_run:
@@ -315,6 +319,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); }
@@ -711,6 +716,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);
@@ -3118,6 +3124,15 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_CHECK_EXTENSION : r =kvm_vm_ioctl_check_extension_generic (kvm, arg);break ; +case KVM_INTROSPECTION : { +struct kvm_introspection i; + + r = -EFAULT ; +if (copy_from_user (&i, argp,sizeof (i)) || !kvmi_hook (kvm, &i)) + goto out; + r =0 ; +break ; + }default : r =kvm_arch_vm_ioctl (filp, ioctl, arg); }
@@ -4072,6 +4087,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_undebugfs:
@@ -4100,6 +4118,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);
Re: [RFC PATCH v4 09/18] kvm: hook in the VM introspection subsystem
Quoted 72 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
Handle the new
KVM_INTROSPECTIONioctl and pass the socket from QEMU to the KVMI subsystem. Notify KVMI on vCPU create/destroy and VM destroy events. Also, the EPT AD bits feature is disabled by this patch.Signed-off-by Adalbert Lazăr <[email protected]>--- arch/x86/kvm/vmx.c | 3 ++- virt/kvm/kvm_main.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)arch/x86/kvm/vmx.c +2
-1@@ -34,6 +34,7 @@#include <linux/tboot.h> #include <linux/hrtimer.h> #include <linux/frame.h> +#include <linux/kvmi.h> #include "kvm_cache_regs.h" #include "x86.h" @@ -6785,7 +6786,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)virt/kvm/kvm_main.c +6
-0@@ -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> @@ -298,6 +299,9 @@ 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; + +kvmi_vcpu_init (vcpu); +return 0 ; fail_free_run:@@ -315,6 +319,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); }@@ -711,6 +716,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);
virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 3132
Quoted 9 lines
virt/kvm/kvm_main.c +6
-0@@ -3118,6 +3124,15 @@ static long kvm_vm_ioctl(struct file *filp,case KVM_CHECK_EXTENSION : r =kvm_vm_ioctl_check_extension_generic (kvm, arg);break ; +case KVM_INTROSPECTION : { +struct kvm_introspection i; + + r = -EFAULT ; +if (copy_from_user (&i, argp,sizeof (i)) || !kvmi_hook (kvm, &i)) + goto out;
Looking at this, I wonder if it would actually make more sense to have kvmi_hook() return an int? This can then be broken into two separate if checks. This way would match the rest of the kvm_vm_ioctl() code a bit better.
Quoted 18 lines
virt/kvm/kvm_main.c +6
-0+ r =0 ; +break ; + }default : r =kvm_arch_vm_ioctl (filp, ioctl, arg); }@@ -4072,6 +4087,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_undebugfs:
virt/kvm/kvm_main.c EXPORT_SYMBOL_GPL(kvm_init); · line 4124
Quoted 7 lines
virt/kvm/kvm_main.c +1
-0@@ -4100,6 +4118,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);
Patrick
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 10/18 kvm: x86: handle the new vCPU request (KVM_REQ_INTROSPECTION)
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
The thread/worker receiving vCPU commands (from the guest introspection tool) signals the vCPU by placing the command in a vCPU buffer, sets this vCPU request bit and kicks the vCPU.
This patch adds the call to the introspection handler that will:
- execute the command
- send the results back to the introspection tool
- pause the vCPU (if requested)
--- arch/x86/kvm/x86.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/x86.c +4 -0
@@ -20,6 +20,7 @@*/ #include <linux/kvm_host.h> +#include <linux/kvmi.h> #include "irq.h" #include "mmu.h" #include "i8254.h"
@@ -6904,6 +6905,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_request (vcpu); }if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 11/18 kvm: x86: hook in the page tracking
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Inform the guest introspection tool that a read/write/execute access is happening on a page of interest (configured via a KVMI_SET_PAGE_ACCESS request).
The introspection tool can respond to allow the emulation to continue, with or without custom input (for read access), retry to guest (if the tool has changed the program counter, has emulated the instruction or the page is no longer of interest).
--- arch/x86/include/asm/kvm_emulate.h | 1 + arch/x86/kvm/emulate.c | 9 +++++++- arch/x86/kvm/mmu.c | 3 +++ arch/x86/kvm/x86.c | 46 +++++++++++++++++++++++++++++--------- 4 files changed, 48 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_emulate.h +1 -0
@@ -438,6 +438,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_USER_EXIT 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 +8 -1
@@ -5263,7 +5263,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_USER_EXIT ; +else if (rc ==X86EMUL_CONTINUE ) +return EMULATION_OK ; +else +return EMULATION_FAILED ; }bool x86_page_table_writing_insn (struct x86_emulate_ctxt *ctxt)
@@ -5633,6 +5638,8 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)if (rc ==X86EMUL_INTERCEPTED )return EMULATION_INTERCEPTED ; +if (rc ==X86EMUL_RETRY_INSTR ) +return EMULATION_USER_EXIT ;if (rc ==X86EMUL_CONTINUE )writeback_registers (ctxt);
arch/x86/kvm/mmu.c +3 -0
@@ -5066,6 +5066,9 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,if (mmio_info_in_cache (vcpu, cr2, direct)) emulation_type =0 ; +if (kvm_page_track_is_active (vcpu,gpa_to_gfn (cr2), +KVM_PAGE_TRACK_PREEXEC )) + emulation_type =EMULTYPE_NO_REEXECUTE ; emulate: er =x86_emulate_instruction (vcpu, cr2, emulation_type, insn, insn_len);
arch/x86/kvm/x86.c +36 -10
89 lines not on this page · raw message
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 12/18 kvm: x86: hook in kvmi_breakpoint_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Inform the guest introspection tool that a breakpoint instruction (INT3) is being executed. These one-byte intructions are 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).
--- arch/x86/kvm/svm.c | 6 ++++++ arch/x86/kvm/vmx.c | 15 +++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
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 "irq.h" #include "mmu.h"
@@ -45,6 +46,7 @@#include <asm/debugreg.h> #include <asm/kvm_para.h> #include <asm/irq_remapping.h> +#include <asm/kvmi.h> #include <asm/virtext.h> #include "trace.h"
@@ -2194,6 +2196,10 @@ 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)) +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.c +11 -4
@@ -51,6 +51,7 @@#include <asm/apic.h> #include <asm/irq_remapping.h> #include <asm/mmu_context.h> +#include <asm/kvmi.h> #include "trace.h" #include "pmu.h"
@@ -5904,7 +5905,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;
@@ -5978,7 +5979,13 @@ 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); + +if (kvmi_breakpoint_event (vcpu, gva)) +return 1 ; +/* * Update instruction length as we may reinject #BP from * user space while in guest debugging mode. Reading it for
@@ -5987,10 +5994,10 @@ static int handle_exception(struct kvm_vcpu *vcpu) vmx->vcpu.arch.event_exit_inst_len =vmcs_read32 (VM_EXIT_INSTRUCTION_LEN ); 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;
RFC PATCH v4 13/18 kvm: x86: hook in kvmi_descriptor_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Inform the guest introspection tool that a system table pointer register (GDTR, IDTR, LDTR, TR) has been accessed.
--- arch/x86/kvm/svm.c | 41 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx.c | 27 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+)
arch/x86/kvm/svm.c +41 -0
@@ -4109,6 +4109,39 @@ static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)return ret; } +static int descriptor_access_interception (struct vcpu_svm *svm) +{ +struct kvm_vcpu *vcpu = &svm->vcpu; +struct vmcb_control_area *c = &svm->vmcb->control; + +switch (c->exit_code) { +case SVM_EXIT_IDTR_READ : +case SVM_EXIT_IDTR_WRITE : +kvmi_descriptor_event (vcpu, c->exit_info_1,0 , +KVMI_DESC_IDTR , c->exit_code ==SVM_EXIT_IDTR_WRITE ); +break ; +case SVM_EXIT_GDTR_READ : +case SVM_EXIT_GDTR_WRITE : +kvmi_descriptor_event (vcpu, c->exit_info_1,0 , +KVMI_DESC_GDTR , c->exit_code ==SVM_EXIT_GDTR_WRITE ); +break ; +case SVM_EXIT_LDTR_READ : +case SVM_EXIT_LDTR_WRITE : +kvmi_descriptor_event (vcpu, c->exit_info_1,0 , +KVMI_DESC_LDTR , c->exit_code ==SVM_EXIT_LDTR_WRITE ); +break ; +case SVM_EXIT_TR_READ : +case SVM_EXIT_TR_WRITE : +kvmi_descriptor_event (vcpu, c->exit_info_1,0 , +KVMI_DESC_TR , c->exit_code ==SVM_EXIT_TR_WRITE ); +break ; +default : +break ; + } + +return 1 ; +} +static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_READ_CR0 ] = cr_interception, [SVM_EXIT_READ_CR3 ] = cr_interception,
@@ -4173,6 +4206,14 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_RSM] = emulate_on_interception, [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, + [SVM_EXIT_IDTR_READ] = descriptor_access_interception, + [SVM_EXIT_GDTR_READ] = descriptor_access_interception, + [SVM_EXIT_LDTR_READ] = descriptor_access_interception, + [SVM_EXIT_TR_READ] = descriptor_access_interception, + [SVM_EXIT_IDTR_WRITE] = descriptor_access_interception, + [SVM_EXIT_GDTR_WRITE] = descriptor_access_interception, + [SVM_EXIT_LDTR_WRITE] = descriptor_access_interception, + [SVM_EXIT_TR_WRITE] = descriptor_access_interception, };static void dump_vmcb (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx.c +27 -0
@@ -8047,6 +8047,31 @@ static int handle_preemption_timer(struct kvm_vcpu *vcpu)return 1 ; } +static int handle_descriptor_access (struct kvm_vcpu *vcpu) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); +u32 exit_reason = vmx->exit_reason; +unsigned long exit_qualification =vmcs_readl (EXIT_QUALIFICATION ); +u32 vmx_instruction_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); +unsigned char store = (vmx_instruction_info >>29 ) &0x1 ; +unsigned char descriptor =0 ; + +if (exit_reason ==EXIT_REASON_GDTR_IDTR ) { +if ((vmx_instruction_info >>28 ) &0x1 ) + descriptor =KVMI_DESC_IDTR ; +else + descriptor =KVMI_DESC_GDTR ; + }else { +if ((vmx_instruction_info >>28 ) &0x1 ) + descriptor =KVMI_DESC_TR ; +else + descriptor =KVMI_DESC_LDTR ; + } + +return kvmi_descriptor_event (vcpu, vmx_instruction_info, + exit_qualification, descriptor, store); +} +static bool valid_ept_address (struct kvm_vcpu *vcpu,u64 address) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -8219,6 +8244,8 @@ static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { [EXIT_REASON_PML_FULL] = handle_pml_full, [EXIT_REASON_VMFUNC] = handle_vmfunc, [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, + [EXIT_REASON_GDTR_IDTR] = handle_descriptor_access, + [EXIT_REASON_LDTR_TR] = handle_descriptor_access, }; static const int kvm_vmx_max_exit_handlers =
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 14/18 kvm: x86: hook in kvmi_cr_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Notify the guest introspection tool that cr{0,3,4} is going to be changed. The function kvmi_cr_event() will load in crX the new value if the tool permits it.
--- arch/x86/kvm/x86.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +12 -1
@@ -676,6 +676,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 ) {
@@ -816,6 +819,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 ;
@@ -832,11 +838,13 @@ 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); +#ifdef CONFIG_X86_64 cr3 &= ~CR3_PCID_INVD ;#endif -if (cr3 ==kvm_read_cr3 (vcpu) && !pdptrs_changed (vcpu)) { +if (cr3 == old_cr3 && !pdptrs_changed (vcpu)) {kvm_mmu_sync_roots (vcpu);kvm_make_request (KVM_REQ_TLB_FLUSH , vcpu);return 0 ;
@@ -849,6 +857,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 ; + vcpu->arch.cr3 = cr3;__set_bit (VCPU_EXREG_CR3 , (ulong *)&vcpu->arch.regs_avail);kvm_mmu_new_cr3 (vcpu);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 15/18 kvm: x86: hook in kvmi_xsetbv_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Notify the guest introspection tool that the extended control register has been changed.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -765,6 +765,9 @@ 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) { +if (xcr != vcpu->arch.xcr0) +kvmi_xsetbv_event (vcpu); +if (kvm_x86_ops->get_cpl (vcpu) !=0 ||__kvm_set_xcr (vcpu, index, xcr)) {kvm_inject_gp (vcpu,0 );
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 16/18 kvm: x86: hook in kvmi_msr_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Inform the guest introspection tool that an MSR is going to be changed.
The kvmi_msr_event() function will check a bitmap of MSR-s of interest (configured via a KVMI_CONTROL_EVENTS(KVMI_MSR_CONTROL) request) and, if the new value differs from the previous one, it will generate a notification. 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.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -1111,6 +1111,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 :
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
RFC PATCH v4 17/18 kvm: x86: handle the introspection hypercalls
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Two hypercalls (KVM_HC_MEM_MAP, KVM_HC_MEM_UNMAP) are used by the introspection tool running in a VM to map/unmap memory from the introspected VM-s.
The third hypercall (KVM_HC_XEN_HVM_OP) 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/x86.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +12 -1
@@ -6333,7 +6333,8 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) r =kvm_skip_emulated_instruction (vcpu); -if (kvm_hv_hypercall_enabled (vcpu->kvm)) +if (kvm_hv_hypercall_enabled (vcpu->kvm) + && !kvmi_is_agent_hypercall (vcpu))return kvm_hv_hypercall (vcpu); nr =kvm_register_read (vcpu,VCPU_REGS_RAX );
@@ -6371,6 +6372,16 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) ret =kvm_pv_clock_pairing (vcpu, a0, a1);break ;#endif +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 : +kvmi_hypercall_event (vcpu); + ret =0 ; +break ;default : ret = -KVM_ENOSYS ;break ;
RFC PATCH v4 18/18 kvm: x86: hook in kvmi_trap_event()
In reply to Adalber LazărFrom: Adalbert Lazar <[email protected]>
Inform the guest introspection tool that the exception (from a previous KVMI_INJECT_EXCEPTION command) was not successfully injected.
It can happen for the tool to queue a pagefault but have it overwritten by an interrupt picked up during guest reentry. kvmi_trap_event() is used to inform the tool of all pending traps giving it a chance to determine if it should try again later.
--- arch/x86/kvm/x86.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/kvm/x86.c +9 -0
@@ -7058,6 +7058,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto cancel_injection; } +if (kvmi_lost_exception (vcpu)) { +local_irq_enable (); +preempt_enable (); + vcpu->srcu_idx =srcu_read_lock (&vcpu->kvm->srcu); + r =1 ; +kvmi_trap_event (vcpu); + goto cancel_injection; + } +kvm_load_guest_xcr0 (vcpu);if (req_immediate_exit) {
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines · Adalber LazA?r
From: Adalbert Lazar <[email protected]>
This patch series proposes a VM introspection subsystem for KVM (KVMI).
The previous RFC can be read here: https://marc.info/?l=kvm&m=150514457912721
These patches were tested on kvm/master,
commit43aabca38aa9668eee3c3c1206207034614c0901(Merge tag 'kvm-arm-fixes-for-v4.15-2' ofgit://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarminto HEAD).In this iteration we refactored the code based on the feedback received from Paolo and others.
I am thinking if we can define some check points in KVM where BPF programs are allowed to attach, then employ the policies in BPFs instead...
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Xiao Guangrong
Quoted 9 lines · Adalber Lazăr
From: Adalbert Lazar <[email protected]>
This patch series proposes a VM introspection subsystem for KVM (KVMI).
The previous RFC can be read here: https://marc.info/?l=kvm&m=150514457912721
These patches were tested on kvm/master,
commit43aabca38aa9668eee3c3c1206207034614c0901(Merge tag 'kvm-arm-fixes-for-v4.15-2' ofgit://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarminto HEAD).
Quoted 6 lines
Quoted 2 lines
In this iteration we refactored the code based on the feedback received from Paolo and others.
I am thinking if we can define some check points in KVM where BPF programs are allowed to attach, then employ the policies in BPFs instead...
That would be a nice feature to have. For example, we could use it to pre-filter the events (eg. drop EPT #PF events generated by A/D bit updates). Also, sure, given how BPF has evolved in Linux these past few years (see JIT) we could upload some pretty complex introspection logic.
Regards,
Signature · 1 line
Mihai Donțu
Quoted 1 line · Adalber LazA?r
This patch series proposes a VM introspection subsystem for KVM (KVMI).
...
Quoted 2 lines
We hope to make public our repositories (kernel, QEMU, userland/simple-introspector) in a couple of days ...
Thanks to Mathieu Tarral, these patches (updated with the Patrick's suggestions) can be found in the kvmi branch of the KVM-VMI project[1]. There is also a userland library and a simple demo/test program in tools/kvm/kvmi[2]. The QEMU patch has its own kvmi[3] branch/repo.
[1]: https://github.com/KVM-VMI/kvm/tree/kvmi
[2]: https://github.com/KVM-VMI/kvm/tree/kvmi/tools/kvm/kvmi
[3]: https://github.com/KVM-VMI/qemu/tree/kvmi
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>