KVM paravirt MMU updates and cr3 caching (v2)
Subsystems (4)
cover KVM paravirt MMU updates and cr3 caching (v2)
The following patchset, based on earlier work by Anthony and Ingo, adds paravirt_ops support for KVM guests enabling hypercall based pte updates, hypercall batching and cr3 caching.
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 1/5 KVM: add basic paravirt support (v2)
Add basic KVM paravirt support. Avoid vm-exits on IO delays.
Add KVM_GET_PARA_FEATURES ioctl so paravirt features can be reported in a single bitmask. This allows the host to disable features on runtime if appropriate, which would require one ioctl per feature otherwise.
The limit of 32 features can be extended to 64 if needed, beyond that a new MSR is required.
v1->v2:
- replace KVM_CAP_CLOCKSOURCE with KVM_CAP_PARA_FEATURES
- cover FEATURE_CLOCKSOURCE
arch/x86/Kconfig +8 -0
@@ -382,6 +382,14 @@ config KVM_CLOCK provides the guest with timing infrastructure such as time of day, and system time +config KVM_GUEST + bool "KVM Guest support" + select PARAVIRT + depends on !(X86_VISWS || X86_VOYAGER) + help + This option enables various optimizations for running under the KVM + hypervisor. + source "arch/x86/lguest/Kconfig" config PARAVIRT
arch/x86/kernel/Makefile +1 -0
@@ -69,6 +69,7 @@ obj-$(CONFIG_DEBUG_RODATA_TEST) += test_ obj-$(CONFIG_DEBUG_NX_TEST) += test_nx.o obj-$(CONFIG_VMI) += vmi_32.o vmiclock_32.o +obj-$(CONFIG_KVM_GUEST) += kvm.o obj-$(CONFIG_KVM_CLOCK) += kvmclock.o obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o
arch/x86/kernel/kvm.c new file +52 -0
52 lines not on this page · raw message
arch/x86/kernel/setup_32.c +1 -0
@@ -784,6 +784,7 @@ void __init setup_arch(char **cmdline_p)*/ vmi_init ();#endif +kvm_guest_init ();/* * NOTE: before this point _nobody_ is allowed to allocate
arch/x86/kernel/setup_64.c +2 -0
@@ -452,6 +452,8 @@ void __init setup_arch(char **cmdline_p)init_apic_mappings ();ioapic_init_mappings (); +kvm_guest_init (); +/* * We trust e820 completely. No explicit ROM probing in memory. */
arch/x86/kvm/x86.c +10 -1
@@ -788,7 +788,7 @@ int kvm_dev_ioctl_check_extension(long ecase KVM_CAP_USER_MEMORY :case KVM_CAP_SET_TSS_ADDR :case KVM_CAP_EXT_CPUID : -case KVM_CAP_CLOCKSOURCE : +case KVM_CAP_PARA_FEATURES : r =1 ;break ;case KVM_CAP_VAPIC :
@@ -854,6 +854,15 @@ long kvm_arch_dev_ioctl(struct file *fil r =0 ;break ; } +case KVM_GET_PARA_FEATURES : { +__u32 para_features =KVM_PARA_FEATURES ; + + r = -EFAULT ; +if (copy_to_user (argp, ¶_features,sizeof para_features)) + goto out; + r =0 ; +break ; + }default : r = -EINVAL ; }
include/asm-x86/kvm_para.h +5 -1
@@ -10,7 +10,8 @@* paravirtualization, the appropriate feature bit should be checked. */ #define KVM_CPUID_FEATURES 0x40000001 -#define KVM_FEATURE_CLOCKSOURCE 0 +#define KVM_FEATURE_CLOCKSOURCE 0 +#define KVM_FEATURE_NOP_IO_DELAY 1#define MSR_KVM_WALL_CLOCK 0x11#define MSR_KVM_SYSTEM_TIME 0x12
@@ -39,6 +40,9 @@ struct kvm_wall_clock {extern void kvmclock_init (void ); +#define KVM_PARA_FEATURES ((1UL << KVM_FEATURE_NOP_IO_DELAY) | \ + (1UL << KVM_FEATURE_CLOCKSOURCE)) +/* This instruction is vmcall. On non-VT architectures, it will generate a * trap that we will then rewrite to the appropriate instruction. */
include/linux/kvm.h +2 -1
@@ -222,6 +222,7 @@ struct kvm_vapic_addr {*/ #define KVM_GET_VCPU_MMAP_SIZE _IO(KVMIO, 0x04)/* in bytes */ #define KVM_GET_SUPPORTED_CPUID _IOWR(KVMIO, 0x05, struct kvm_cpuid2) +#define KVM_GET_PARA_FEATURES _IOR(KVMIO, 0x05, __u32)/* * Extension capability list.
@@ -233,7 +234,7 @@ struct kvm_vapic_addr {#define KVM_CAP_SET_TSS_ADDR 4#define KVM_CAP_VAPIC 6#define KVM_CAP_EXT_CPUID 7 -#define KVM_CAP_CLOCKSOURCE 8 +#define KVM_CAP_PARA_FEATURES 8/* * ioctls for VM fds
include/linux/kvm_para.h +6 -0
@@ -20,6 +20,12 @@#include <asm/kvm_para.h> #ifdef __KERNEL__ +#ifdef CONFIG_KVM_GUEST +void __init kvm_guest_init (void ); +#else +#define kvm_guest_init () do { } while (0) +#endif +static inline int kvm_para_has_feature (unsigned int feature) {if (kvm_arch_para_features () & (1UL << feature))
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 1/5] KVM: add basic paravirt support (v2)
Quoted 6 lines · Marcelo Tosatti
Add basic KVM paravirt support. Avoid vm-exits on IO delays.
Add
KVM_GET_PARA_FEATURESioctl so paravirt features can be reported in a single bitmask. This allows the host to disable features on runtime if appropriate, which would require one ioctl per feature otherwise.
Quoted 9 lines
The limit of 32 features can be extended to 64 if needed, beyond that a new MSR is required.
v1->v2:
- replaceKVM_CAP_CLOCKSOURCEwithKVM_CAP_PARA_FEATURES
- coverFEATURE_CLOCKSOURCE
I don't understand the motivation for this. A handful of ioctl()s at init time are hardly time consuming.
There is the advantage that paravirt kernel advances are reflected automatically without changes in userspace, but sometimes this is a disadvantage (it means there is no way to disable it, for instance).
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 1/5] KVM: add basic paravirt support (v2)
Quoted 17 lines · Avi Kivity
Quoted 6 lines · Marcelo Tosatti
Add basic KVM paravirt support. Avoid vm-exits on IO delays.
Add
KVM_GET_PARA_FEATURESioctl so paravirt features can be reported in a single bitmask. This allows the host to disable features on runtime if appropriate, which would require one ioctl per feature otherwise.Quoted 9 lines
The limit of 32 features can be extended to 64 if needed, beyond that a new MSR is required.
v1->v2:
- replaceKVM_CAP_CLOCKSOURCEwithKVM_CAP_PARA_FEATURES
- coverFEATURE_CLOCKSOURCE
Quoted 6 lines
I don't understand the motivation for this. A handful of ioctl()s at init time are hardly time consuming.
There is the advantage that paravirt kernel advances are reflected automatically without changes in userspace, but sometimes this is a disadvantage (it means there is no way to disable it, for instance).
One ioctl per feature sounded like unnecessary code duplication. I have no problems with it though. So you prefer that way?
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 1/5] KVM: add basic paravirt support (v2)
Quoted 3 lines · Marcelo Tosatti
One ioctl per feature sounded like unnecessary code duplication. I have no problems with it though. So you prefer that way?
Yes. I think we can avoid code duplication.
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 2/5 KVM: hypercall based pte updates and TLB flushes (v2)
In reply to Marcelo TosattiHypercall based pte updates are faster than faults, and also allow use of the lazy MMU mode to batch operations.
Don't report the feature if two dimensional paging is enabled.
v1->v2:
- guest passes physical destination addr, which is cheaper than doing v->p translation in the host.
- infer size of pte from guest mode
arch/x86/kernel/kvm.c +103 -0
@@ -33,6 +33,91 @@ static void kvm_io_delay(void) { } +static void kvm_mmu_write (void *dest,const void *src,size_t size) +{ +const uint8_t *p = src; +unsigned long a0 = *(unsigned long *)p; +unsigned long a1 =0 ; + +#ifdef CONFIG_X86_32 + size >>=2 ; +if (size ==2 ) + a1 = *(u32 *)&p[4 ]; +#endif +kvm_hypercall3 (KVM_HYPERCALL_MMU_WRITE , (unsigned long )__pa (dest), a0, + a1); +} + +/* +* We only need to hook operations that are MMU writes. We hook these so that +* we can use lazy MMU mode to batch these operations. We could probably +* improve the performance of the host code if we used some of the information +* here to simplify processing of batched writes. +*/ +static void kvm_set_pte (pte_t *ptep,pte_t pte) +{ +kvm_mmu_write (ptep, &pte,sizeof (pte)); +} + +static void kvm_set_pte_at (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte) +{ +kvm_mmu_write (ptep, &pte,sizeof (pte)); +} + +static void kvm_set_pmd (pmd_t *pmdp,pmd_t pmd) +{ +kvm_mmu_write (pmdp, &pmd,sizeof (pmd)); +} + +#if PAGETABLE_LEVELS >=3 +#ifdef CONFIG_X86_PAE +static void kvm_set_pte_atomic (pte_t *ptep,pte_t pte) +{ +kvm_mmu_write (ptep, &pte,sizeof (pte)); +} + +static void kvm_set_pte_present (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte) +{ +kvm_mmu_write (ptep, &pte,sizeof (pte)); +} + +static void kvm_pte_clear (struct mm_struct *mm, +unsigned long addr,pte_t *ptep) +{ +pte_t pte =__pte (0 ); +kvm_mmu_write (ptep, &pte,sizeof (pte)); +} + +static void kvm_pmd_clear (pmd_t *pmdp) +{ +pmd_t pmd =__pmd (0 ); +kvm_mmu_write (pmdp, &pmd,sizeof (pmd)); +} +#endif + +static void kvm_set_pgd (pgd_t *pgdp,pgd_t pgd) +{ +kvm_mmu_write (pgdp, &pgd,sizeof (pgd)); +} + +static void kvm_set_pud (pud_t *pudp,pud_t pud) +{ +kvm_mmu_write (pudp, &pud,sizeof (pud)); +} +#endif /* PAGETABLE_LEVELS >= 3 */ + +static void kvm_flush_tlb (void ) +{ +kvm_hypercall0 (KVM_HYPERCALL_FLUSH_TLB ); +} + +static void kvm_release_pt (u32 pfn) +{ +kvm_hypercall1 (KVM_HYPERCALL_RELEASE_PT , pfn <<PAGE_SHIFT ); +} +static void paravirt_ops_setup (void ) { pv_info.name ="KVM" ;
@@ -41,6 +126,24 @@ static void paravirt_ops_setup(void)if (kvm_para_has_feature (KVM_FEATURE_NOP_IO_DELAY )) pv_cpu_ops.io_delay = kvm_io_delay; +if (kvm_para_has_feature (KVM_FEATURE_MMU_WRITE )) { + pv_mmu_ops.set_pte = kvm_set_pte; + pv_mmu_ops.set_pte_at = kvm_set_pte_at; + pv_mmu_ops.set_pmd = kvm_set_pmd; +#if PAGETABLE_LEVELS >=3 +#ifdef CONFIG_X86_PAE + pv_mmu_ops.set_pte_atomic = kvm_set_pte_atomic; + pv_mmu_ops.set_pte_present = kvm_set_pte_present; + pv_mmu_ops.pte_clear = kvm_pte_clear; + pv_mmu_ops.pmd_clear = kvm_pmd_clear; +#endif + pv_mmu_ops.set_pud = kvm_set_pud; + pv_mmu_ops.set_pgd = kvm_set_pgd; +#endif + pv_mmu_ops.flush_tlb_user = kvm_flush_tlb; + pv_mmu_ops.release_pt = kvm_release_pt; + pv_mmu_ops.release_pd = kvm_release_pt; + } }void __init kvm_guest_init (void )
arch/x86/kvm/mmu.c +3 -3
@@ -39,7 +39,7 @@* 2. while doing 1. it walks guest-physical to host-physical * If the hardware supports that we don't need to do shadow paging. */ -static bool tdp_enabled = false; +bool tdp_enabled = false;#undef MMU_DEBUG
@@ -288,7 +288,7 @@ static void mmu_free_memory_cache_page(sfree_page ((unsigned long )mc->objects[--mc->nobjs]); } -static int mmu_topup_memory_caches (struct kvm_vcpu *vcpu) +int mmu_topup_memory_caches (struct kvm_vcpu *vcpu) {int r;
@@ -857,7 +857,7 @@ static int kvm_mmu_unprotect_page(structreturn r; } -static void mmu_unshadow (struct kvm *kvm,gfn_t gfn) +void mmu_unshadow (struct kvm *kvm,gfn_t gfn) {struct kvm_mmu_page *sp;
arch/x86/kvm/mmu.h +3 -0
@@ -47,4 +47,7 @@ static inline int is_paging(struct kvm_v return vcpu->arch.cr0 & X86_CR0_PG; } +void mmu_unshadow (struct kvm *kvm,gfn_t gfn); +int mmu_topup_memory_caches (struct kvm_vcpu *vcpu); +#endif
arch/x86/kvm/x86.c +50 -0
68 lines not on this page · raw message
include/asm-x86/kvm_para.h +3 -1
@@ -12,6 +12,7 @@#define KVM_CPUID_FEATURES 0x40000001#define KVM_FEATURE_CLOCKSOURCE 0#define KVM_FEATURE_NOP_IO_DELAY 1 +#define KVM_FEATURE_MMU_WRITE 2#define MSR_KVM_WALL_CLOCK 0x11#define MSR_KVM_SYSTEM_TIME 0x12
@@ -41,7 +42,8 @@ extern void kvmclock_init(void);#define KVM_PARA_FEATURES ((1UL << KVM_FEATURE_NOP_IO_DELAY) | \ - (1UL << KVM_FEATURE_CLOCKSOURCE)) + (1UL << KVM_FEATURE_CLOCKSOURCE) | \ + (1UL << KVM_FEATURE_MMU_WRITE))/* This instruction is vmcall. On non-VT architectures, it will generate a * trap that we will then rewrite to the appropriate instruction.
include/linux/kvm_para.h +6 -1
@@ -11,8 +11,13 @@/* Return values for hypercalls */ #define KVM_ENOSYS 1000 +#define KVM_EFAULT EFAULT +#define KVM_E2BIG E2BIG -#define KVM_HC_VAPIC_POLL_IRQ 1 +#define KVM_HC_VAPIC_POLL_IRQ 1 +#define KVM_HYPERCALL_MMU_WRITE 2 +#define KVM_HYPERCALL_FLUSH_TLB 3 +#define KVM_HYPERCALL_RELEASE_PT 4/* * hypercalls use architecture specific
include/asm-x86/kvm_host.h +2 -0
@@ -419,6 +419,8 @@ void kvm_mmu_change_mmu_pages(struct kvmint load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3); +extern bool tdp_enabled; +enum emulation_result {EMULATE_DONE ,/* no further processing */ EMULATE_DO_MMIO ,/* kvm_run filled with mmio request */
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 2/5] KVM: hypercall based pte updates and TLB flushes (v2)
arch/x86/kvm/x86.c int kvm_emulate_halt(struct kvm_vcpu *vc · line 2368
Quoted 9 lines · Marcelo Tosatti
Hypercall based pte updates are faster than faults, and also allow use of the lazy MMU mode to batch operations.
Don't report the feature if two dimensional paging is enabled.
arch/x86/kvm/x86.c +2
-0+static int kvm_hypercall_mmu_write (struct kvm_vcpu *vcpu,gpa_t addr, +unsigned long a1,unsigned long a2)
Putting this and the other mmu hypercalls in mmu.c will reduce namespace pollution (esp. as the names you expose don't have the kvm_ prefix).
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 3/5 KVM: hypercall batching (v2)
In reply to Marcelo TosattiBatch pte updates and tlb flushes in lazy MMU mode.
v1->v2:
- report individual hypercall error code, have multicall return number of processed entries.
- cover entire multicall duration with slots_lock instead of acquiring/reacquiring.
arch/x86/kernel/kvm.c +99 -4
@@ -25,6 +25,77 @@#include <linux/kvm_para.h> #include <linux/cpu.h> #include <linux/mm.h> +#include <linux/hardirq.h> + +#define MAX_MULTICALL_NR (PAGE_SIZE / sizeof(struct kvm_multicall_entry)) + +struct kvm_para_state { +struct kvm_multicall_entry queue[MAX_MULTICALL_NR ]; +int queue_index; +enum paravirt_lazy_mode mode; +}; + +static DEFINE_PER_CPU(struct kvm_para_state, para_state); + +static int can_defer_hypercall (struct kvm_para_state *state,unsigned int nr) +{ +if (state->mode ==PARAVIRT_LAZY_MMU ) { +switch (nr) { +case KVM_HYPERCALL_MMU_WRITE : +case KVM_HYPERCALL_FLUSH_TLB : +return 1 ; + } + } +return 0 ; +} + +static void hypercall_queue_flush (struct kvm_para_state *state) +{ +long ret; + +if (state->queue_index) { + ret =kvm_hypercall2 (KVM_HYPERCALL_MULTICALL , +__pa (&state->queue), state->queue_index); +WARN_ON (ret != state->queue_index); + state->queue_index =0 ; + } +} + +static void kvm_hypercall_defer (struct kvm_para_state *state, +unsigned int nr, +unsigned long a0,unsigned long a1, +unsigned long a2,unsigned long a3) +{ +struct kvm_multicall_entry *entry; + +BUG_ON (preemptible ()); + +if (state->queue_index ==MAX_MULTICALL_NR ) +hypercall_queue_flush (state); + + entry = &state->queue[state->queue_index++]; + entry->nr = nr; + entry->a0 = a0; + entry->a1 = a1; + entry->a2 = a2; + entry->a3 = a3; +} + +static long kvm_hypercall (unsigned int nr,unsigned long a0, +unsigned long a1,unsigned long a2, +unsigned long a3) +{ +struct kvm_para_state *state = &get_cpu_var (para_state); +long ret =0 ; + +if (can_defer_hypercall (state, nr)) +kvm_hypercall_defer (state, nr, a0, a1, a2, a3); +else + ret =kvm_hypercall4 (nr, a0, a1, a2, a3); + +put_cpu_var (para_state); +return ret; +}/* * No need for any "IO delay" on KVM
@@ -44,8 +115,8 @@ static void kvm_mmu_write(void *dest, coif (size ==2 ) a1 = *(u32 *)&p[4 ];#endif -kvm_hypercall3 (KVM_HYPERCALL_MMU_WRITE , (unsigned long )__pa (dest), a0, - a1); +kvm_hypercall (KVM_HYPERCALL_MMU_WRITE , (unsigned long )__pa (dest), a0, + a1,0 ); }/*
@@ -110,12 +181,31 @@ static void kvm_set_pud(pud_t *pudp, pudstatic void kvm_flush_tlb (void ) { -kvm_hypercall0 (KVM_HYPERCALL_FLUSH_TLB ); +kvm_hypercall (KVM_HYPERCALL_FLUSH_TLB ,0 ,0 ,0 ,0 ); }static void kvm_release_pt (u32 pfn) { -kvm_hypercall1 (KVM_HYPERCALL_RELEASE_PT , pfn <<PAGE_SHIFT ); +kvm_hypercall (KVM_HYPERCALL_RELEASE_PT , pfn <<PAGE_SHIFT ,0 ,0 ,0 ); +} + +static void kvm_enter_lazy_mmu (void ) +{ +struct kvm_para_state *state + = &per_cpu (para_state,smp_processor_id ()); + +paravirt_enter_lazy_mmu (); + state->mode =paravirt_get_lazy_mode (); +} + +static void kvm_leave_lazy_mmu (void ) +{ +struct kvm_para_state *state + = &per_cpu (para_state,smp_processor_id ()); + +hypercall_queue_flush (state); +paravirt_leave_lazy (paravirt_get_lazy_mode ()); + state->mode =paravirt_get_lazy_mode (); }static void paravirt_ops_setup (void )
@@ -144,6 +234,11 @@ static void paravirt_ops_setup(void) pv_mmu_ops.release_pt = kvm_release_pt; pv_mmu_ops.release_pd = kvm_release_pt; } + +if (kvm_para_has_feature (KVM_FEATURE_MULTICALL )) { + pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu; + pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu; + } }void __init kvm_guest_init (void )
arch/x86/kvm/x86.c +80 -24
145 lines not on this page · raw message
include/asm-x86/kvm_host.h +2 -0
@@ -327,6 +327,8 @@ struct kvm_vcpu_stat {u32 fpu_reload;u32 insn_emulation;u32 insn_emulation_fail; +u32 multicall; +u32 multicall_nr; };struct descriptor_table {
include/asm-x86/kvm_para.h +11 -1
@@ -13,6 +13,7 @@#define KVM_FEATURE_CLOCKSOURCE 0#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_WRITE 2 +#define KVM_FEATURE_MULTICALL 3#define MSR_KVM_WALL_CLOCK 0x11#define MSR_KVM_SYSTEM_TIME 0x12
@@ -37,13 +38,22 @@ struct kvm_wall_clock {uint32_t wc_nsec; } __attribute__((__packed__)); +struct kvm_multicall_entry { +u32 nr; +u32 error_code; +u64 a0; +u64 a1; +u64 a2; +u64 a3; +};extern void kvmclock_init (void );#define KVM_PARA_FEATURES ((1UL << KVM_FEATURE_NOP_IO_DELAY) | \ (1UL << KVM_FEATURE_CLOCKSOURCE) | \ - (1UL << KVM_FEATURE_MMU_WRITE)) + (1UL << KVM_FEATURE_MMU_WRITE) | \ + (1UL << KVM_FEATURE_MULTICALL))/* This instruction is vmcall. On non-VT architectures, it will generate a * trap that we will then rewrite to the appropriate instruction.
include/linux/kvm_para.h +2 -0
@@ -13,11 +13,13 @@#define KVM_ENOSYS 1000#define KVM_EFAULT EFAULT#define KVM_E2BIG E2BIG +#define KVM_EINVAL EINVAL#define KVM_HC_VAPIC_POLL_IRQ 1#define KVM_HYPERCALL_MMU_WRITE 2#define KVM_HYPERCALL_FLUSH_TLB 3#define KVM_HYPERCALL_RELEASE_PT 4 +#define KVM_HYPERCALL_MULTICALL 5/* * hypercalls use architecture specific
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 3/5] KVM: hypercall batching (v2)
Quoted 8 lines · Marcelo Tosatti
Batch pte updates and tlb flushes in lazy MMU mode.
v1->v2:
- report individual hypercall error code, have multicall return number of processed entries.
- cover entire multicall duration withslots_lockinstead of acquiring/reacquiring.
But not all hypercalls want slots_lock.
I suggested earlier switching to a "multiple mmu operation" hypercall (and not have individual mmu hypercalls). What do think about that?
I think hypercalls will be quite diverse in the future and batching them will not make sense always.
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 3/5] KVM: hypercall batching (v2)
Quoted 11 lines · Avi Kivity
Quoted 8 lines · Marcelo Tosatti
Batch pte updates and tlb flushes in lazy MMU mode.
v1->v2:
- report individual hypercall error code, have multicall return number of processed entries.
- cover entire multicall duration withslots_lockinstead of acquiring/reacquiring.But not all hypercalls want
slots_lock.
But slots_lock is required for kvm_read_guest() (and write_guest()). So even if the underlying hypercall handling does not require slots_lock, it still makes sense.
Quoted 2 lines
I suggested earlier switching to a "multiple mmu operation" hypercall (and not have individual mmu hypercalls). What do think about that?
We need to support different MMU operations in the same multicall, for example normal pte writes, masked pte updates and tlb flushes. So an array of
"operation, parameters"
is required.
Quoted 2 lines
I think hypercalls will be quite diverse in the future and batching them will not make sense always.
I agree. While the infrastructure is generic allowing batching of any kind of hypercall, we explicitly select which ones can be batched (in the guest):
static int can_defer_hypercall(struct kvm_para_state *state, unsigned int nr)
{
if (state->mode == PARAVIRT_LAZY_MMU) {
switch (nr) {
case KVM_HYPERCALL_MMU_WRITE:
case KVM_HYPERCALL_FLUSH_TLB:
return 1;
}
}
return 0;
}Perhaps you want to move that enforcement to the host.
This allows batching of future hypercalls (if appropriate) to be easy.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 3/5] KVM: hypercall batching (v2)
Quoted 13 lines · Marcelo Tosatti
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Marcelo Tosatti
Batch pte updates and tlb flushes in lazy MMU mode.
v1->v2:
- report individual hypercall error code, have multicall return number of processed entries.
- cover entire multicall duration withslots_lockinstead of acquiring/reacquiring.
Quoted 8 lines
Quoted 2 lines
But not all hypercalls want
slots_lock.But
slots_lockis required forkvm_read_guest()(andwrite_guest()). So even if the underlying hypercall handling does not requireslots_lock, it still makes sense.
Right. I guess it is cheap enough so we can keep this.
Quoted 33 lines
Quoted 3 lines
I suggested earlier switching to a "multiple mmu operation" hypercall (and not have individual mmu hypercalls). What do think about that?
We need to support different MMU operations in the same multicall, for example normal pte writes, masked pte updates and tlb flushes. So an array of
"operation, parameters"
is required.
Quoted 3 lines
I think hypercalls will be quite diverse in the future and batching them will not make sense always.
I agree. While the infrastructure is generic allowing batching of any kind of hypercall, we explicitly select which ones can be batched (in the guest):
static int
can_defer_hypercall(struct kvm_para_state *state, unsigned int nr){ if (state->mode == PARAVIRT_LAZY_MMU) { switch (nr) { case KVM_HYPERCALL_MMU_WRITE: case KVM_HYPERCALL_FLUSH_TLB: return 1; } } return 0; }
Quoted 5 lines
Perhaps you want to move that enforcement to the host.
This allows batching of future hypercalls (if appropriate) to be easy.
I'm still uneasy about it, though I have no rational reasons left now.
Oh, there is one: with a MMU_OP hypercall you can take the mmu spinlock once per batch (dropping it once in a while to let another vcpu make progress or to inject an interrupt).
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 3/5] KVM: hypercall batching (v2)
Quoted 11 lines · Avi Kivity
Quoted 5 lines
Perhaps you want to move that enforcement to the host.
This allows batching of future hypercalls (if appropriate) to be easy.
I'm still uneasy about it, though I have no rational reasons left now.
Oh, there is one: with a
MMU_OPhypercall you can take the mmu spinlock once per batch (dropping it once in a while to let another vcpu make progress or to inject an interrupt).
emulator_write_phys() needs to do blocking work for each pte (mmu_guess_page_from_pte), so that optimization would need quite some work (separate mmu_guess_page_from_pte from kvm_mmu_pte_write).
And you can do this optimization even without MMU_OP, just check in advance how many operations will take the mmu lock, do the non-blocking part of them, and then manipulate the mmu lock protected shadow data.
Also, holding the spinlock for a longer period is not necessarily an improvement (with the slots_lock it clearly is because there is no write contention).
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Re: [patch 3/5] KVM: hypercall batching (v2)
Quoted 19 lines · Marcelo Tosatti
Quoted 6 lines · Avi Kivity
Quoted 6 lines
Perhaps you want to move that enforcement to the host.
This allows batching of future hypercalls (if appropriate) to be easy.
Quoted 6 lines
I'm still uneasy about it, though I have no rational reasons left now.
Oh, there is one: with a
MMU_OPhypercall you can take the mmu spinlock once per batch (dropping it once in a while to let another vcpu make progress or to inject an interrupt).
emulator_write_phys()needs to do blocking work for each pte (mmu_guess_page_from_pte), so that optimization would need quite some work (separatemmu_guess_page_from_ptefromkvm_mmu_pte_write).
Quoted 9 lines
And you can do this optimization even without
MMU_OP, just check in advance how many operations will take the mmu lock, do the non-blocking part of them, and then manipulate the mmu lock protected shadow data.Also, holding the spinlock for a longer period is not necessarily an improvement (with the
slots_lockit clearly is because there is no write contention).
Yes, you are right.
Signature · 7 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic. ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 4/5 KVM: ignore zapped root pagetables (v2)
In reply to Marcelo TosattiMark zapped root pagetables as invalid and ignore such pages during lookup.
This is a problem with the cr3-target feature, where a zapped root table fools the faulting code into creating a read-only mapping. The result is a lockup if the instruction can't be emulated.
v1->v2:
- reload mmu of remote cpu's on root invalidation
arch/x86/kvm/mmu.c +10 -2
@@ -668,7 +668,8 @@ static struct kvm_mmu_page *kvm_mmu_look index =kvm_page_table_hashfn (gfn); bucket = &kvm->arch.mmu_page_hash[index];hlist_for_each_entry (sp, node, bucket, hash_link) -if (sp->gfn == gfn && !sp->role.metaphysical) { +if (sp->gfn == gfn && !sp->role.metaphysical + && !sp->role.invalid) {pgprintk ("%s: found role %x\n" , __FUNCTION__, sp->role.word);return sp;
@@ -796,8 +797,11 @@ static void kvm_mmu_zap_page(struct kvmif (!sp->root_count) {hlist_del (&sp->hash_link);kvm_mmu_free_page (kvm, sp); - }else + }else {list_move (&sp->link, &kvm->arch.active_mmu_pages); + sp->role.invalid =1 ; +kvm_reload_remote_mmus (kvm); + }kvm_mmu_reset_last_pte_updated (kvm); }
@@ -1067,6 +1071,8 @@ static void mmu_free_roots(struct kvm_vc sp =page_header (root); --sp->root_count; +if (!sp->root_count && sp->role.invalid) +kvm_mmu_zap_page (vcpu->kvm, sp); vcpu->arch.mmu.root_hpa =INVALID_PAGE ;spin_unlock (&vcpu->kvm->mmu_lock);return ;
@@ -1079,6 +1085,8 @@ static void mmu_free_roots(struct kvm_vc root &=PT64_BASE_ADDR_MASK ; sp =page_header (root); --sp->root_count; +if (!sp->root_count && sp->role.invalid) +kvm_mmu_zap_page (vcpu->kvm, sp); } vcpu->arch.mmu.pae_root[i] =INVALID_PAGE ; }
include/asm-x86/kvm_host.h +1 -0
@@ -140,6 +140,7 @@ union kvm_mmu_page_role { unsigned pad_for_nice_hex_output : 6; unsigned metaphysical : 1; unsigned access : 3; + unsigned invalid : 1; }; };
arch/x86/kvm/x86.c +12 -0
@@ -2743,6 +2743,10 @@ preempted: kvm_x86_ops->guest_debug_pre (vcpu); again: +if (vcpu->requests) +if (test_and_clear_bit (KVM_REQ_MMU_RELOAD , &vcpu->requests)) +kvm_mmu_unload (vcpu); + r =kvm_mmu_reload (vcpu);if (unlikely (r)) goto out;
@@ -2774,6 +2778,14 @@ again: goto out; } +if (vcpu->requests) +if (test_bit (KVM_REQ_MMU_RELOAD , &vcpu->requests)) { +local_irq_enable (); +preempt_enable (); + r =1 ; + goto out; + } +if (signal_pending (current)) {local_irq_enable ();preempt_enable ();
include/linux/kvm_host.h +2 -0
@@ -37,6 +37,7 @@#define KVM_REQ_TLB_FLUSH 0#define KVM_REQ_MIGRATE_TIMER 1#define KVM_REQ_REPORT_TPR_ACCESS 2 +#define KVM_REQ_MMU_RELOAD 3struct kvm_vcpu ;extern struct kmem_cache *kvm_vcpu_cache;
@@ -190,6 +191,7 @@ void kvm_resched(struct kvm_vcpu *vcpu);void kvm_load_guest_fpu (struct kvm_vcpu *vcpu);void kvm_put_guest_fpu (struct kvm_vcpu *vcpu);void kvm_flush_remote_tlbs (struct kvm *kvm); +void kvm_reload_remote_mmus (struct kvm *kvm);long kvm_arch_dev_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg);
virt/kvm/kvm_main.c +23 -0
@@ -119,6 +119,29 @@ void kvm_flush_remote_tlbs(struct kvm *ksmp_call_function_mask (cpus, ack_flush,NULL ,1 ); } +void kvm_reload_remote_mmus (struct kvm *kvm) +{ +int i, cpu; +cpumask_t cpus; +struct kvm_vcpu *vcpu; + +cpus_clear (cpus); +for (i =0 ; i <KVM_MAX_VCPUS ; ++i) { + vcpu = kvm->vcpus[i]; +if (!vcpu) +continue ; +if (test_and_set_bit (KVM_REQ_MMU_RELOAD , &vcpu->requests)) +continue ; + cpu = vcpu->cpu; +if (cpu !=-1 && cpu !=raw_smp_processor_id ()) +cpu_set (cpu, cpus); + } +if (cpus_empty (cpus)) +return ; +smp_call_function_mask (cpus, ack_flush,NULL ,1 ); +} + +int kvm_vcpu_init (struct kvm_vcpu *vcpu,struct kvm *kvm,unsigned id) {struct page *page;
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
Avi Kivity
AppliedRe: [patch 4/5] KVM: ignore zapped root pagetables (v2)
Quoted 7 lines · Marcelo Tosatti
Mark zapped root pagetables as invalid and ignore such pages during lookup.
This is a problem with the cr3-target feature, where a zapped root table fools the faulting code into creating a read-only mapping. The result is a lockup if the instruction can't be emulated.
Applied this patch, as it is independent; thanks.
Signature · 7 lines
error compiling committee.c: too many arguments to function ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
PATCH 5/5 KVM: VMX cr3 cache support (v2)
In reply to Marcelo TosattiAdd support for the cr3 cache feature on Intel VMX CPU's. This avoids vmexits on context switch if the cr3 value is cached in one of the entries (currently 4 are present).
This is especially important for Xenner, where each guest syscall involves a cr3 switch.
v1->v2:
- handle the race which happens when the guest has the cache cleared in the middle of kvm_write_cr3 by injecting a GP and trapping it to fallback to hypercall variant (suggested by Avi).
arch/x86/kernel/kvm.c +139 -1
@@ -26,14 +26,17 @@#include <linux/cpu.h> #include <linux/mm.h> #include <linux/hardirq.h> +#include <asm/tlbflush.h> +#include <asm/asm.h> #define MAX_MULTICALL_NR (PAGE_SIZE / sizeof(struct kvm_multicall_entry))struct kvm_para_state { +struct kvm_cr3_cache cr3_cache;struct kvm_multicall_entry queue[MAX_MULTICALL_NR ];int queue_index;enum paravirt_lazy_mode mode; -}; +} __attribute__ ((aligned (PAGE_SIZE ))); static DEFINE_PER_CPU(struct kvm_para_state, para_state);
@@ -104,6 +107,116 @@ static void kvm_io_delay(void) { } +static void kvm_new_cr3 (unsigned long cr3) +{ +kvm_hypercall1 (KVM_HYPERCALL_SET_CR3 , cr3); +} + +static unsigned long __force_order; + +/* +* Special, register-to-cr3 instruction based hypercall API +* variant to the KVM host. This utilizes the cr3 filter capability +* of the hardware - if this works out then no VM exit happens, +* if a VM exit happens then KVM will get the virtual address too. +*/ +static void kvm_write_cr3 (unsigned long guest_cr3) +{ +struct kvm_para_state *para_state = &get_cpu_var (para_state); +struct kvm_cr3_cache *cache = ¶_state->cr3_cache; +int idx; + +/* +* Check the cache (maintained by the host) for a matching +* guest_cr3 => host_cr3 mapping. Use it if found: +*/ +for (idx =0 ; idx < cache->max_idx; idx++) { +if (cache->entry[idx].guest_cr3 == guest_cr3) { +unsigned long trap; + +/* +* Cache-hit: we load the cached host-CR3 value. +* Fallback to hypercall variant if it raced with +* the host clearing the cache after guest_cr3 +* comparison. +*/ + __asm__ __volatile__ ( +" mov %2, %0\n" +"0: mov %3, %%cr3\n" +"1:\n" +".section .fixup,\"ax\"\n" +"2: mov %1, %0\n" +" jmp 1b\n" +".previous\n" + _ASM_EXTABLE(0b, 2b) + : "=&r" (trap) + : "n" (1UL), "n" (0UL), +" b" (cache->entry[idx].host_cr3), + "m" (__force_order)); + if (!trap) + goto out; +break ; + } + } + +/* +* Cache-miss. Tell the host the new cr3 via hypercall (to avoid +* aliasing problems with a cached host_cr3 == guest_cr3). +*/ +kvm_new_cr3 (guest_cr3); +out: +put_cpu_var (para_state); +} + +/* +* Avoid the VM exit upon cr3 load by using the cached +* ->active_mm->pgd value: +*/ +static void kvm_flush_tlb_user (void ) +{ +kvm_write_cr3 (__pa (current->active_mm->pgd)); +} + +/* +* Disable global pages, do a flush, then enable global pages: +*/ +static void kvm_flush_tlb_kernel (void ) +{ +unsigned long orig_cr4 =read_cr4 (); + +write_cr4 (orig_cr4 & ~X86_CR4_PGE ); +kvm_flush_tlb_user (); +write_cr4 (orig_cr4); +} + +static void register_cr3_cache (void *cache) +{ +struct kvm_para_state *state; + + state = &per_cpu (para_state,raw_smp_processor_id ()); +wrmsrl (KVM_MSR_SET_CR3_CACHE ,__pa (&state->cr3_cache)); +} + +static unsigned __init kvm_patch (u8 type,u16 clobbers,void *ibuf, +unsigned long addr,unsigned len) +{ +switch (type) { +case PARAVIRT_PATCH (pv_mmu_ops.write_cr3): +return paravirt_patch_default (type, clobbers, ibuf, addr, len); +default : +return native_patch (type, clobbers, ibuf, addr, len); + } +} + +static void __init setup_guest_cr3_cache (void ) +{ +on_each_cpu (register_cr3_cache,NULL ,0 ,1 ); + + pv_mmu_ops.write_cr3 = kvm_write_cr3; + pv_mmu_ops.flush_tlb_user = kvm_flush_tlb_user; + pv_mmu_ops.flush_tlb_kernel = kvm_flush_tlb_kernel; +} +static void kvm_mmu_write (void *dest,const void *src,size_t size) {const uint8_t *p = src;
@@ -120,6 +233,28 @@ static void kvm_mmu_write(void *dest, co }/* +* CR3 cache initialization uses on_each_cpu(), so it can't +* happen at kvm_guest_init time. +*/ +int __init kvm_cr3_cache_init (void ) +{ +unsigned long flags; + +if (!kvm_para_available ()) +return -ENOSYS ; + +if (kvm_para_has_feature (KVM_FEATURE_CR3_CACHE )) { +setup_guest_cr3_cache (); +local_irq_save (flags); +apply_paravirt (__parainstructions, __parainstructions_end); +local_irq_restore (flags); + } + +return 0 ; +} +module_init (kvm_cr3_cache_init); + +/* * We only need to hook operations that are MMU writes. We hook these so that * we can use lazy MMU mode to batch these operations. We could probably * improve the performance of the host code if we used some of the information
@@ -239,6 +374,9 @@ static void paravirt_ops_setup(void) pv_mmu_ops.lazy_mode.enter = kvm_enter_lazy_mmu; pv_mmu_ops.lazy_mode.leave = kvm_leave_lazy_mmu; } + +if (kvm_para_has_feature (KVM_FEATURE_CR3_CACHE )) + pv_init_ops.patch = kvm_patch; }void __init kvm_guest_init (void )
arch/x86/kvm/mmu.c +129 -58
405 lines not on this page · raw message
arch/x86/kvm/mmu.h +2 -1
@@ -17,7 +17,8 @@ static inline void kvm_mmu_free_some_pagstatic inline int kvm_mmu_reload (struct kvm_vcpu *vcpu) { -if (likely (vcpu->arch.mmu.root_hpa !=INVALID_PAGE )) +int idx = vcpu->arch.cr3_cache_idx; +if (likely (vcpu->arch.mmu.root_hpa[idx] !=INVALID_PAGE ))return 0 ;return kvm_mmu_load (vcpu);
arch/x86/kvm/paging_tmpl.h +2 -2
@@ -283,10 +283,10 @@ static u64 *FNAME(fetch)(struct kvm_vcpuif (!is_present_pte (walker->ptes[walker->level -1 ]))return NULL ; - shadow_addr = vcpu->arch.mmu.root_hpa; + shadow_addr = vcpu->arch.mmu.root_hpa[vcpu->arch.cr3_cache_idx]; level = vcpu->arch.mmu.shadow_root_level;if (level ==PT32E_ROOT_LEVEL ) { - shadow_addr = vcpu->arch.mmu.pae_root[(addr >>30 ) &3 ]; + shadow_addr = vcpu->arch.mmu.pae_root[vcpu->arch.cr3_cache_idx][(addr >>30 ) &3 ]; shadow_addr &=PT64_BASE_ADDR_MASK ; --level; }
arch/x86/kvm/vmx.c +148 -4
233 lines not on this page · raw message
arch/x86/kvm/x86.c +15 -2
@@ -81,6 +81,7 @@ struct kvm_stats_debugfs_item debugfs_en { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) }, { "multicall", VCPU_STAT(multicall) }, { "multicall_nr", VCPU_STAT(multicall_nr) }, + { "cr3_cache_synced", VCPU_STAT(cr3_cache_synced) }, { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) }, { "mmu_pte_write", VM_STAT(mmu_pte_write) }, { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
@@ -858,10 +859,13 @@ long kvm_arch_dev_ioctl(struct file *fil }case KVM_GET_PARA_FEATURES : {__u32 para_features =KVM_PARA_FEATURES ; +if (tdp_enabled) { para_features &= ~(1UL <<KVM_FEATURE_MMU_WRITE ); para_features &= ~(1UL <<KVM_FEATURE_MULTICALL ); } +if (!kvm_x86_ops->cpu_has_cr3_cache ()) + para_features &= ~(1UL <<KVM_FEATURE_CR3_CACHE ); r = -EFAULT ;if (copy_to_user (argp, ¶_features,sizeof para_features))
@@ -2416,6 +2420,12 @@ static int kvm_hypercall_release_pt(strureturn 0 ; } +static int kvm_hypercall_set_cr3 (struct kvm_vcpu *vcpu,gpa_t cr3) +{ +set_cr3 (vcpu, cr3); +return 0 ; +} +static int dispatch_hypercall (struct kvm_vcpu *vcpu,unsigned long nr,unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3)
@@ -2429,6 +2439,8 @@ static int dispatch_hypercall(struct kvmreturn kvm_hypercall_flush_tlb (vcpu);case KVM_HYPERCALL_RELEASE_PT :return kvm_hypercall_release_pt (vcpu, a0); +case KVM_HYPERCALL_SET_CR3 : +return kvm_hypercall_set_cr3 (vcpu, a0); }return -KVM_ENOSYS ;
@@ -3361,12 +3373,13 @@ int kvm_arch_vcpu_init(struct kvm_vcpu * {struct page *page;struct kvm *kvm; -int r; +int r, i;BUG_ON (vcpu->kvm ==NULL ); kvm = vcpu->kvm; - vcpu->arch.mmu.root_hpa =INVALID_PAGE ; +for (i =0 ; i <KVM_CR3_CACHE_SIZE ; i++) + vcpu->arch.mmu.root_hpa[i] =INVALID_PAGE ;if (!irqchip_in_kernel (kvm) || vcpu->vcpu_id ==0 ) vcpu->arch.mp_state =VCPU_MP_STATE_RUNNABLE ;else
include/asm-x86/kvm_host.h +7 -2
@@ -181,11 +181,11 @@ struct kvm_mmu {gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,gva_t gva);void (*prefetch_page)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *page); -hpa_t root_hpa; +hpa_t root_hpa[KVM_CR3_CACHE_SIZE ];int root_level;int shadow_root_level; -u64 *pae_root; +u64 *pae_root[KVM_CR3_CACHE_SIZE ]; };struct kvm_vcpu_arch {
@@ -199,6 +199,9 @@ struct kvm_vcpu_arch {unsigned long cr0;unsigned long cr2;unsigned long cr3; +struct kvm_cr3_cache *cr3_cache; +unsigned int cr3_cache_idx; +unsigned int cr3_cache_limit;unsigned long cr4;unsigned long cr8;u64 pdptrs[4 ];/* pae */
@@ -330,6 +333,7 @@ struct kvm_vcpu_stat {u32 insn_emulation_fail;u32 multicall;u32 multicall_nr; +u32 cr3_cache_synced; };struct descriptor_table {
@@ -346,6 +350,7 @@ struct kvm_x86_ops {int (*hardware_setup)(void );/* __init */ void (*hardware_unsetup)(void );/* __exit */ bool (*cpu_has_accelerated_tpr)(void ); +bool (*cpu_has_cr3_cache)(void );/* Create, but do not attach this VCPU */ struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm,unsigned id);
include/asm-x86/kvm_para.h +17 -1
@@ -14,6 +14,7 @@#define KVM_FEATURE_NOP_IO_DELAY 1#define KVM_FEATURE_MMU_WRITE 2#define KVM_FEATURE_MULTICALL 3 +#define KVM_FEATURE_CR3_CACHE 4#define MSR_KVM_WALL_CLOCK 0x11#define MSR_KVM_SYSTEM_TIME 0x12
@@ -53,7 +54,10 @@ extern void kvmclock_init(void);#define KVM_PARA_FEATURES ((1UL << KVM_FEATURE_NOP_IO_DELAY) | \ (1UL << KVM_FEATURE_CLOCKSOURCE) | \ (1UL << KVM_FEATURE_MMU_WRITE) | \ - (1UL << KVM_FEATURE_MULTICALL)) + (1UL << KVM_FEATURE_MULTICALL) | \ + (1UL << KVM_FEATURE_CR3_CACHE)) + +#define KVM_MSR_SET_CR3_CACHE 0x87655678/* This instruction is vmcall. On non-VT architectures, it will generate a * trap that we will then rewrite to the appropriate instruction.
@@ -143,4 +147,16 @@ static inline unsigned int kvm_arch_para#endif +#define KVM_CR3_CACHE_SIZE 4 + +struct kvm_cr3_cache_entry { +__u64 guest_cr3; +__u64 host_cr3; +}; + +struct kvm_cr3_cache { +struct kvm_cr3_cache_entry entry[KVM_CR3_CACHE_SIZE ]; +__u32 max_idx; +}; +#endif
include/linux/kvm_para.h +1 -0
@@ -20,6 +20,7 @@#define KVM_HYPERCALL_FLUSH_TLB 3#define KVM_HYPERCALL_RELEASE_PT 4#define KVM_HYPERCALL_MULTICALL 5 +#define KVM_HYPERCALL_SET_CR3 6/* * hypercalls use architecture specific
arch/x86/kvm/svm.c +6 -0
@@ -1801,6 +1801,11 @@ static bool svm_cpu_has_accelerated_tpr(return false; } +static bool cpu_has_cr3_cache (void ) +{ +return false; +} +static struct kvm_x86_ops svm_x86_ops = { .cpu_has_kvm_support = has_svm, .disabled_by_bios = is_disabled,
@@ -1810,6 +1815,7 @@ static struct kvm_x86_ops svm_x86_ops = .hardware_enable = svm_hardware_enable, .hardware_disable = svm_hardware_disable, .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, + .cpu_has_cr3_cache = cpu_has_cr3_cache, .vcpu_create = svm_create_vcpu, .vcpu_free = svm_free_vcpu,
Signature · 4 lines
------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/