KVM updates for 2.6.23
cover KVM updates for 2.6.23
Following is my patchqueue for the 2.6.23 merge window, not including the cpu hotplug fixes posted earlier. The changes include performance improvements, guest smp, random fixes, and cleanups. Comments welcome.
Anthony Liguori (1):
KVM: SVM: Allow direct guest access to PC debug port
Avi Kivity (44):
KVM: Assume that writes smaller than 4 bytes are to non-pagetable pages
KVM: Avoid saving and restoring some host CPU state on lightweight vmexit
KVM: Unindent some code
KVM: Reduce misfirings of the fork detector
KVM: Be more careful restoring fs on lightweight vmexit
KVM: Unify kvm_mmu_pre_write() and kvm_mmu_post_write()
KVM: MMU: Respect nonpae pagetable quadrant when zapping ptes
KVM: Update shadow pte on write to guest pte
KVM: Increase mmu shadow cache to 1024 pages
KVM: Fix potential guest state leak into host
KVM: Move some more msr mangling into vmx_save_host_state()
KVM: Rationalize exception bitmap usage
KVM: Consolidate guest fpu activation and deactivation
KVM: Set cr0.mp for guests
KVM: MMU: Simplify kvm_mmu_free_page() a tiny bit
KVM: MMU: Store shadow page tables as kernel virtual addresses, not physical
KVM: VMX: Only reload guest msrs if they are already loaded
KVM: Avoid corrupting tr in real mode
KVM: Fix vmx I/O bitmap initialization on highmem systems
KVM: VMX: Use local labels in inline assembly
KVM: x86 emulator: implement wbinvd
KVM: MMU: Use slab caches for shadow pages and their headers
KVM: MMU: Simplify fetch() a little bit
KVM: MMU: Move set_pte_common() to pte width dependent code
KVM: MMU: Pass the guest pde to set_pte_common
KVM: MMU: Fold fix_read_pf() into set_pte_common()
KVM: MMU: Fold fix_write_pf() into set_pte_common()
KVM: Move shadow pte modifications from set_pte/set_pde to set_pde_common()
KVM: Make shadow pte updates atomic
KVM: MMU: Make setting shadow ptes atomic on i386
KVM: MMU: Remove cr0.wp tricks
KVM: MMU: Simpify accessed/dirty/present/nx bit handling
KVM: MMU: Don't cache guest access bits in the shadow page table
KVM: MMU: Remove unused large page marker
KVM: Lazy guest cr3 switching
KVM: Fix vcpu freeing for guest smp
KVM: Fix adding an smp virtual machine to the vm list
KVM: Enable guest smp
KVM: Move duplicate halt handling code into kvm_main.c
KVM: Emulate hlt on real mode for Intel
KVM: Keep an upper bound of initialized vcpus
KVM: Flush remote tlbs when reducing shadow pte permissions
KVM: Initialize the BSP bit in the APIC_BASE msr correctly
KVM: VMX: Ensure vcpu time stamp counter is monotonous
Eddie Dong (4):
KVM: VMX: Avoid saving and restoring msrs on lightweight vmexit
KVM: VMX: Cleanup redundant code in MSR set
KVM: VMX: Avoid saving and restoring msr_efer on lightweight vmexit
KVM: Use symbolic constants instead of magic numbers
He, Qing (1):
KVM: VMX: Enable io bitmaps to avoid IO port 0x80 VMEXITs
Jan Engelhardt (1):
Use menuconfig objects II - KVM/Virt
Markus Rechberger (1):
KVM: Fix includes
Matthew Gregan (1):
KVM: Implement IA32_EBL_CR_POWERON msr
Nguyen Anh Quynh (1):
KVM: Remove unnecessary initialization and checks in mark_page_dirty()
Nitin A Kamble (1):
KVM: VMX: Handle #SS faults from real mode
Robert P. J. Day (1):
KVM: Replace C code with call to ARRAY_SIZE() macro.
Shani Moideen (2):
KVM: SVM: Replace memset(<addr>, 0, PAGESIZE) with clear_page(<addr>)
KVM: VMX: Replace memset(<addr>, 0, PAGESIZE) with clear_page(<addr>)
drivers/kvm/Kconfig | 9 +- drivers/kvm/kvm.h | 53 ++++- drivers/kvm/kvm_main.c | 115 ++++++++- drivers/kvm/mmu.c | 284 +++++++++----------- drivers/kvm/paging_tmpl.h | 273 ++++++++++--------- drivers/kvm/svm.c | 46 ++-- drivers/kvm/vmx.c | 640 ++++++++++++++++++++++++++++----------------- drivers/kvm/x86_emulate.c | 10 +- 8 files changed, 868 insertions(+), 562 deletions(-)
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
PATCH 1/58 KVM: VMX: Enable io bitmaps to avoid IO port 0x80 VMEXITs
From: He, Qing <[email protected]>
This patch enables IO bitmaps control on vmx and unmask the 0x80 port to avoid VMEXITs caused by accessing port 0x80. 0x80 is used as delays (see include/asm/io.h), and handling VMEXITs on its access is unnecessary but slows things down. This patch improves kernel build test at around 3%~5%.
Because every VM uses the same io bitmap, it is shared between
all VMs rather than a per-VM data structure.
--- drivers/kvm/vmx.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 46 insertions(+), 4 deletions(-)
drivers/kvm/vmx.c +46 -4
@@ -34,6 +34,9 @@ MODULE_LICENSE("GPL"); static DEFINE_PER_CPU(struct vmcs *, vmxarea); static DEFINE_PER_CPU(struct vmcs *, current_vmcs); +static struct page *vmx_io_bitmap_a; +static struct page *vmx_io_bitmap_b; +#ifdef CONFIG_X86_64 #define HOST_IS_64 1#else
@@ -1129,8 +1132,8 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)vmcs_write32 (GUEST_PENDING_DBG_EXCEPTIONS ,0 );/* I/O */ -vmcs_write64 (IO_BITMAP_A ,0 ); -vmcs_write64 (IO_BITMAP_B ,0 ); +vmcs_write64 (IO_BITMAP_A ,page_to_phys (vmx_io_bitmap_a)); +vmcs_write64 (IO_BITMAP_B ,page_to_phys (vmx_io_bitmap_b));guest_write_tsc (0 );
@@ -1150,7 +1153,7 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)CPU_BASED_HLT_EXITING /* 20.6.2 */ |CPU_BASED_CR8_LOAD_EXITING /* 20.6.2 */ |CPU_BASED_CR8_STORE_EXITING /* 20.6.2 */ - |CPU_BASED_UNCOND_IO_EXITING /* 20.6.2 */ + |CPU_BASED_ACTIVATE_IO_BITMAP /* 20.6.2 */ |CPU_BASED_MOV_DR_EXITING |CPU_BASED_USE_TSC_OFFSETING /* 21.3 */ );
@@ -2188,11 +2191,50 @@ static struct kvm_arch_ops vmx_arch_ops = {static int __init vmx_init (void ) { -return kvm_init_arch (&vmx_arch_ops,THIS_MODULE ); +void *iova; +int r; + + vmx_io_bitmap_a =alloc_page (GFP_KERNEL | __GFP_HIGHMEM); +if (!vmx_io_bitmap_a) +return -ENOMEM ; + + vmx_io_bitmap_b =alloc_page (GFP_KERNEL | __GFP_HIGHMEM); +if (!vmx_io_bitmap_b) { + r = -ENOMEM ; + goto out; + } + +/* +* Allow direct access to the PC debug port (it is often used for I/O +* delays, but the vmexits simply slow things down). +*/ + iova =kmap (vmx_io_bitmap_a); +memset (iova,0xff ,PAGE_SIZE ); +clear_bit (0x80 , iova); +kunmap (iova); + + iova =kmap (vmx_io_bitmap_b); +memset (iova,0xff ,PAGE_SIZE ); +kunmap (iova); + + r =kvm_init_arch (&vmx_arch_ops,THIS_MODULE ); +if (r) + goto out1; + +return 0 ; + +out1: +__free_page (vmx_io_bitmap_b); +out: +__free_page (vmx_io_bitmap_a); +return r; } static void __exit vmx_exit(void) { +__free_page (vmx_io_bitmap_b); +__free_page (vmx_io_bitmap_a); +kvm_exit_arch (); }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 2/58 KVM: SVM: Allow direct guest access to PC debug port
In reply to Avi KivityFrom: Anthony Liguori <aliguori-r/[email protected]>
The PC debug port is used for IO delay and does not require emulation.
--- drivers/kvm/svm.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-)
drivers/kvm/svm.c +5 -3
@@ -378,7 +378,7 @@ static __init int svm_hardware_setup(void)int cpu;struct page *iopm_pages;struct page *msrpm_pages; -void *msrpm_va; +void *iopm_va, *msrpm_va;int r;kvm_emulator_want_group7_invlpg ();
@@ -387,8 +387,10 @@ static __init int svm_hardware_setup(void)if (!iopm_pages)return -ENOMEM ; -memset (page_address (iopm_pages),0xff , -PAGE_SIZE * (1 <<IOPM_ALLOC_ORDER )); + + iopm_va =page_address (iopm_pages); +memset (iopm_va,0xff ,PAGE_SIZE * (1 <<IOPM_ALLOC_ORDER )); +clear_bit (0x80 , iopm_va);/* allow direct access to PC debug port */ iopm_base =page_to_pfn (iopm_pages) <<PAGE_SHIFT ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 3/58 KVM: Assume that writes smaller than 4 bytes are to non-pagetable pages
In reply to Avi KivityThis allows us to remove write protection earlier than otherwise. Should some mad OS choose to use byte writes to update pagetables, it will suffer a performance hit, but still work correctly.
--- drivers/kvm/mmu.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/kvm/mmu.c +1 -0
@@ -1169,6 +1169,7 @@ void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes)continue ; pte_size = page->role.glevels ==PT32_ROOT_LEVEL ?4 :8 ; misaligned = (offset ^ (offset + bytes -1 )) & ~(pte_size -1 ); + misaligned |= bytes <4 ;if (misaligned || flooded) {/* * Misaligned accesses are too much trouble to fix
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 4/58 KVM: Avoid saving and restoring some host CPU state on lightweight vmexit
In reply to Avi KivityMany msrs and the like will only be used by the host if we schedule() or return to userspace. Therefore, we avoid saving them if we handle the exit within the kernel, and if a reschedule is not requested.
Based on a patch from Eddie Dong <[email protected]> with a couple of fixes by me.
--- drivers/kvm/kvm.h | 1 + drivers/kvm/kvm_main.c | 1 + drivers/kvm/vmx.c | 105 +++++++++++++++++++++++++++-------------------- 3 files changed, 62 insertions(+), 45 deletions(-)
drivers/kvm/kvm.h +1 -0
@@ -252,6 +252,7 @@ struct kvm_stat {u32 halt_exits;u32 request_irq_exits;u32 irq_exits; +u32 light_exits; };struct kvm_vcpu {
drivers/kvm/kvm_main.c +1 -0
@@ -72,6 +72,7 @@ static struct kvm_stats_debugfs_item { { "halt_exits", STAT_OFFSET(halt_exits) }, { "request_irq", STAT_OFFSET(request_irq_exits) }, { "irq_exits", STAT_OFFSET(irq_exits) }, + { "light_exits", STAT_OFFSET(light_exits) }, { NULL } };
drivers/kvm/vmx.c +60 -45
152 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 5/58 KVM: Unindent some code
In reply to Avi Kivity--- drivers/kvm/vmx.c | 58 ++++++++++++++++++++++++++-------------------------- 1 files changed, 29 insertions(+), 29 deletions(-)
drivers/kvm/vmx.c +29 -29
@@ -1998,39 +1998,39 @@ again: asm ("mov %0, %%ds; mov %0, %%es" : :"r" (__USER_DS)); -if (fail) { +if (unlikely (fail)) { kvm_run->exit_reason =KVM_EXIT_FAIL_ENTRY ; kvm_run->fail_entry.hardware_entry_failure_reason =vmcs_read32 (VM_INSTRUCTION_ERROR ); r =0 ; - }else { -/* -* Profile KVM exit RIPs: -*/ -if (unlikely (prof_on ==KVM_PROFILING )) -profile_hit (KVM_PROFILING , (void *)vmcs_readl (GUEST_RIP )); - - vcpu->launched =1 ; - r =kvm_handle_exit (kvm_run, vcpu); -if (r >0 ) { -/* Give scheduler a change to reschedule. */ -if (signal_pending (current)) { - r = -EINTR ; - kvm_run->exit_reason =KVM_EXIT_INTR ; - ++vcpu->stat.signal_exits; - goto out; - } - -if (dm_request_for_irq_injection (vcpu, kvm_run)) { - r = -EINTR ; - kvm_run->exit_reason =KVM_EXIT_INTR ; - ++vcpu->stat.request_irq_exits; - goto out; - } -if (!need_resched ()) { - ++vcpu->stat.light_exits; - goto again; - } + goto out; + } +/* +* Profile KVM exit RIPs: +*/ +if (unlikely (prof_on ==KVM_PROFILING )) +profile_hit (KVM_PROFILING , (void *)vmcs_readl (GUEST_RIP )); + + vcpu->launched =1 ; + r =kvm_handle_exit (kvm_run, vcpu); +if (r >0 ) { +/* Give scheduler a change to reschedule. */ +if (signal_pending (current)) { + r = -EINTR ; + kvm_run->exit_reason =KVM_EXIT_INTR ; + ++vcpu->stat.signal_exits; + goto out; + } + +if (dm_request_for_irq_injection (vcpu, kvm_run)) { + r = -EINTR ; + kvm_run->exit_reason =KVM_EXIT_INTR ; + ++vcpu->stat.request_irq_exits; + goto out; + } +if (!need_resched ()) { + ++vcpu->stat.light_exits; + goto again; } }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 6/58 KVM: Reduce misfirings of the fork detector
In reply to Avi KivityThe kvm mmu tries to detects forks by looking for repeated writes to a page table. If it sees a fork, it unshadows the page table so the page table copying can proceed at native speed instead of being emulated.
However, the detector also triggered on simple demand paging access patterns:
a linear walk of memory would of course cause repeated writes to the same pagetable page, causing it to unshadow prematurely.
Fix by resetting the fork detector if we detect a demand fault.
--- drivers/kvm/paging_tmpl.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
drivers/kvm/paging_tmpl.h +4 -0
@@ -421,6 +421,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,pgprintk ("%s: guest page fault\n" , __FUNCTION__);inject_page_fault (vcpu, addr, walker.error_code);FNAME (release_walker)(&walker); + vcpu->last_pt_write_count =0 ;/* reset fork detector */ return 0 ; }
@@ -442,6 +443,9 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,FNAME (release_walker)(&walker); +if (!write_pt) + vcpu->last_pt_write_count =0 ;/* reset fork detector */ +/* * mmio: emulate if accessible, otherwise its a guest fault. */
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 7/58 KVM: Be more careful restoring fs on lightweight vmexit
In reply to Avi Kivityi386 wants fs for accessing the pda even on a lightweight exit, so ensure we can always restore it. This fixes a regression on i386 introduced by the lightweight vmexit patch.
--- drivers/kvm/vmx.c | 22 +++++++++++----------- 1 files changed, 11 insertions(+), 11 deletions(-)
drivers/kvm/vmx.c +11 -11
@@ -1832,16 +1832,21 @@ preempted:* Set host fs and gs selectors. Unfortunately, 22.2.3 does not * allow segment selectors with cpl > 0 or ti == 1. */ - fs_sel =read_fs (); - gs_sel =read_gs (); ldt_sel =read_ldt (); - fs_gs_ldt_reload_needed = (fs_sel &7 ) | (gs_sel &7 ) | ldt_sel; -if (!fs_gs_ldt_reload_needed) { + fs_gs_ldt_reload_needed = ldt_sel; + fs_sel =read_fs (); +if (!(fs_sel &7 ))vmcs_write16 (HOST_FS_SELECTOR , fs_sel); -vmcs_write16 (HOST_GS_SELECTOR , gs_sel); - }else { +else {vmcs_write16 (HOST_FS_SELECTOR ,0 ); + fs_gs_ldt_reload_needed =1 ; + } + gs_sel =read_gs (); +if (!(gs_sel &7 )) +vmcs_write16 (HOST_GS_SELECTOR , gs_sel); +else {vmcs_write16 (HOST_GS_SELECTOR ,0 ); + fs_gs_ldt_reload_needed =1 ; }#ifdef CONFIG_X86_64
@@ -2035,11 +2040,6 @@ again: } out: -/* -* Reload segment selectors ASAP. (it's needed for a functional -* kernel: x86 relies on having __KERNEL_PDA in %fs and x86_64 -* relies on having 0 in %gs for the CPU PDA to work.) -*/ if (fs_gs_ldt_reload_needed) {load_ldt (ldt_sel);load_fs (fs_sel);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 8/58 KVM: Unify kvm_mmu_pre_write() and kvm_mmu_post_write()
In reply to Avi KivityInstead of calling two functions and repeating expensive checks, call one function and provide it with before/after information.
--- drivers/kvm/kvm.h | 4 ++-- drivers/kvm/kvm_main.c | 4 ++-- drivers/kvm/mmu.c | 11 ++++------- 3 files changed, 8 insertions(+), 11 deletions(-)
drivers/kvm/kvm.h +2 -2
@@ -525,8 +525,8 @@ int kvm_write_guest(struct kvm_vcpu *vcpu,unsigned long segment_base (u16 selector); -void kvm_mmu_pre_write (struct kvm_vcpu *vcpu,gpa_t gpa,int bytes); -void kvm_mmu_post_write (struct kvm_vcpu *vcpu,gpa_t gpa,int bytes); +void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, +const u8 *old,const u8 *new,int bytes);int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva);void kvm_mmu_free_some_pages (struct kvm_vcpu *vcpu);
drivers/kvm/kvm_main.c +2 -2
@@ -1071,18 +1071,18 @@ static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, {struct page *page;void *virt; +unsigned offset =offset_in_page (gpa);if (((gpa + bytes -1 ) >>PAGE_SHIFT ) != (gpa >>PAGE_SHIFT ))return 0 ; page =gfn_to_page (vcpu->kvm, gpa >>PAGE_SHIFT );if (!page)return 0 ; -kvm_mmu_pre_write (vcpu, gpa, bytes);mark_page_dirty (vcpu->kvm, gpa >>PAGE_SHIFT ); virt =kmap_atomic (page,KM_USER0 ); +kvm_mmu_pte_write (vcpu, gpa, virt + offset, val, bytes);memcpy (virt +offset_in_page (gpa), val, bytes);kunmap_atomic (virt,KM_USER0 ); -kvm_mmu_post_write (vcpu, gpa, bytes);return 1 ; }
drivers/kvm/mmu.c +4 -7
@@ -1118,7 +1118,7 @@ out:return r; } -static void mmu_pre_write_zap_pte (struct kvm_vcpu *vcpu, +static void mmu_pte_write_zap_pte (struct kvm_vcpu *vcpu,struct kvm_mmu_page *page,u64 *spte) {
@@ -1137,7 +1137,8 @@ static void mmu_pre_write_zap_pte(struct kvm_vcpu *vcpu, *spte =0 ; } -void kvm_mmu_pre_write (struct kvm_vcpu *vcpu,gpa_t gpa,int bytes) +void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa, +const u8 *old,const u8 *new,int bytes) {gfn_t gfn = gpa >>PAGE_SHIFT ;struct kvm_mmu_page *page;
@@ -1206,16 +1207,12 @@ void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes) spte =__va (page->page_hpa); spte += page_offset /sizeof (*spte);while (npte--) { -mmu_pre_write_zap_pte (vcpu, page, spte); +mmu_pte_write_zap_pte (vcpu, page, spte); ++spte; } } } -void kvm_mmu_post_write (struct kvm_vcpu *vcpu,gpa_t gpa,int bytes) -{ -} -int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva) {gpa_t gpa = vcpu->mmu.gva_to_gpa (vcpu, gva);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 9/58 KVM: MMU: Respect nonpae pagetable quadrant when zapping ptes
In reply to Avi KivityWhen a guest writes to a page that has an mmu shadow, we have to clear the shadow pte corresponding to the memory location touched by the guest.
Now, in nonpae mode, a single guest page may have two or four shadow pages (because a nonpae page maps 4MB or 4GB, whereas the pae shadow maps 2MB or 1GB), so we when we look up the page we find up to three additional aliases for the page. Since we _clear_ the shadow pte, it doesn't matter except for a slight performance penalty, but if we want to _update_ the shadow pte instead of clearing it, it is vital that we don't modify the aliases.
Fortunately, exactly which page is needed (the "quadrant") is easily computed, and is accessible in the shadow page header. All we need is to ignore shadow pages from the wrong quadrants.
--- drivers/kvm/mmu.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
drivers/kvm/mmu.c +4 -0
@@ -1150,6 +1150,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,unsigned pte_size;unsigned page_offset;unsigned misaligned; +unsigned quadrant;int level;int flooded =0 ;int npte;
@@ -1202,7 +1203,10 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, page_offset <<=1 ; npte =2 ; } + quadrant = page_offset >>PAGE_SHIFT ; page_offset &= ~PAGE_MASK ; +if (quadrant != page->role.quadrant) +continue ; } spte =__va (page->page_hpa); spte += page_offset /sizeof (*spte);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 10/58 KVM: Update shadow pte on write to guest pte
In reply to Avi KivityA typical demand page/copy on write pattern is:
- page fault on vaddr
- kvm propagates fault to guest
- guest handles fault, updates pte
- kvm traps write, clears shadow pte, resumes guest
- guest returns to userspace, re-faults on same vaddr
- kvm installs shadow pte, resumes guest
- guest continues
So, three vmexits for a single guest page fault. But if instead of clearing the page table entry, we update to correspond to the value that the guest has just written, we eliminate the third vmexit.
This patch does exactly that, reducing kbuild time by about 10%.
--- drivers/kvm/mmu.c | 15 +++++++++++++++ drivers/kvm/paging_tmpl.h | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 0 deletions(-)
drivers/kvm/mmu.c +15 -0
@@ -1137,6 +1137,20 @@ static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, *spte =0 ; } +static void mmu_pte_write_new_pte (struct kvm_vcpu *vcpu, +struct kvm_mmu_page *page, +u64 *spte, +const void *new,int bytes) +{ +if (page->role.level !=PT_PAGE_TABLE_LEVEL ) +return ; + +if (page->role.glevels ==PT32_ROOT_LEVEL ) +paging32_update_pte (vcpu, page, spte, new, bytes); +else +paging64_update_pte (vcpu, page, spte, new, bytes); +} +void kvm_mmu_pte_write (struct kvm_vcpu *vcpu,gpa_t gpa,const u8 *old,const u8 *new,int bytes) {
@@ -1212,6 +1226,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, spte += page_offset /sizeof (*spte);while (npte--) {mmu_pte_write_zap_pte (vcpu, page, spte); +mmu_pte_write_new_pte (vcpu, page, spte, new, bytes); ++spte; } }
drivers/kvm/paging_tmpl.h +15 -0
@@ -202,6 +202,21 @@ static void FNAME(set_pte)(struct kvm_vcpu *vcpu, u64 guest_pte, guest_pte & PT_DIRTY_MASK, access_bits, gfn); } +static void FNAME (update_pte )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *page, +u64 *spte,const void *pte,int bytes) +{ +pt_element_t gpte; + +if (bytes <sizeof (pt_element_t)) +return ; + gpte = *(const pt_element_t *)pte; +if (~gpte & (PT_PRESENT_MASK |PT_ACCESSED_MASK )) +return ; +pgprintk ("%s: gpte %llx spte %p\n" , __FUNCTION__, (u64 )gpte, spte); +FNAME (set_pte)(vcpu, gpte, spte,6 , + (gpte &PT_BASE_ADDR_MASK ) >>PAGE_SHIFT ); +} +static void FNAME (set_pde )(struct kvm_vcpu *vcpu,u64 guest_pde,u64 *shadow_pte,u64 access_bits,gfn_t gfn) {
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 11/58 KVM: Increase mmu shadow cache to 1024 pages
In reply to Avi KivityThis improves kbuild times by about 10%, bringing it within a respectable 25% of native.
--- drivers/kvm/kvm.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/kvm/kvm.h +1 -1
@@ -54,7 +54,7 @@#define KVM_MAX_VCPUS 1#define KVM_ALIAS_SLOTS 4#define KVM_MEMORY_SLOTS 4 -#define KVM_NUM_MMU_PAGES 256 +#define KVM_NUM_MMU_PAGES 1024#define KVM_MIN_FREE_MMU_PAGES 5#define KVM_REFILL_PAGES 25#define KVM_MAX_CPUID_ENTRIES 40
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 12/58 KVM: Fix potential guest state leak into host
In reply to Avi KivityThe lightweight vmexit path avoids saving and reloading certain host state. However in certain cases lightweight vmexit handling can schedule() which requires reloading the host state.
So we store the host state in the vcpu structure, and reloaded it if we relinquish the vcpu.
--- drivers/kvm/kvm.h | 5 ++ drivers/kvm/vmx.c | 160 +++++++++++++++++++++++++++++----------------------- 2 files changed, 94 insertions(+), 71 deletions(-)
drivers/kvm/kvm.h +5 -0
@@ -306,6 +306,11 @@ struct kvm_vcpu {char *guest_fx_image;int fpu_active;int guest_fpu_loaded; +struct vmx_host_state { +int loaded; +u16 fs_sel, gs_sel, ldt_sel; +int fs_gs_ldt_reload_needed; + } vmx_host_state;int mmio_needed;int mmio_read_completed;
drivers/kvm/vmx.c +89 -71
199 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 13/58 KVM: Move some more msr mangling into vmx_save_host_state()
In reply to Avi Kivity--- drivers/kvm/vmx.c | 14 +++++++------- 1 files changed, 7 insertions(+), 7 deletions(-)
drivers/kvm/vmx.c +7 -7
@@ -290,6 +290,13 @@ static void vmx_save_host_state(struct kvm_vcpu *vcpu)vmcs_writel (HOST_FS_BASE ,segment_base (hs->fs_sel));vmcs_writel (HOST_GS_BASE ,segment_base (hs->gs_sel));#endif + +#ifdef CONFIG_X86_64 +if (is_long_mode (vcpu)) { +save_msrs (vcpu->host_msrs + msr_offset_kernel_gs_base,1 ); +load_msrs (vcpu->guest_msrs,NR_BAD_MSRS ); + } +#endif }static void vmx_load_host_state (struct kvm_vcpu *vcpu)
@@ -1903,13 +1910,6 @@ preempted:if (vcpu->guest_debug.enabled)kvm_guest_debug_pre (vcpu); -#ifdef CONFIG_X86_64 -if (is_long_mode (vcpu)) { -save_msrs (vcpu->host_msrs + msr_offset_kernel_gs_base,1 ); -load_msrs (vcpu->guest_msrs,NR_BAD_MSRS ); - } -#endif - again:vmx_save_host_state (vcpu);kvm_load_guest_fpu (vcpu);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 14/58 KVM: Rationalize exception bitmap usage
In reply to Avi KivityEveryone owns a piece of the exception bitmap, but they happily write to the entire thing like there's no tomorrow. Centralize handling in update_exception_bitmap() and have everyone call that.
--- drivers/kvm/vmx.c | 42 +++++++++++++++++++++--------------------- 1 files changed, 21 insertions(+), 21 deletions(-)
drivers/kvm/vmx.c +21 -21
@@ -237,6 +237,20 @@ static void vmcs_set_bits(unsigned long field, u32 mask)vmcs_writel (field,vmcs_readl (field) | mask); } +static void update_exception_bitmap (struct kvm_vcpu *vcpu) +{ +u32 eb; + + eb =1u <<PF_VECTOR ; +if (!vcpu->fpu_active) + eb |=1u <<NM_VECTOR ; +if (vcpu->guest_debug.enabled) + eb |=1u <<1 ; +if (vcpu->rmode.active) + eb = ~0 ; +vmcs_write32 (EXCEPTION_BITMAP , eb); +} +static void reload_tss (void ) {#ifndef CONFIG_X86_64
@@ -618,10 +632,8 @@ static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)static int set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_debug_guest *dbg) {unsigned long dr7 =0x400 ; -u32 exception_bitmap;int old_singlestep; - exception_bitmap =vmcs_read32 (EXCEPTION_BITMAP ); old_singlestep = vcpu->guest_debug.singlestep; vcpu->guest_debug.enabled = dbg->enabled;
@@ -637,13 +649,9 @@ static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) dr7 |=0 << (i*4 +16 );/* execution breakpoint */ } - exception_bitmap |= (1u <<1 );/* Trap debug exceptions */ - vcpu->guest_debug.singlestep = dbg->singlestep; - }else { - exception_bitmap &= ~(1u <<1 );/* Ignore debug exceptions */ + }else vcpu->guest_debug.singlestep =0 ; - }if (old_singlestep && !vcpu->guest_debug.singlestep) {unsigned long flags;
@@ -653,7 +661,7 @@ static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)vmcs_writel (GUEST_RFLAGS , flags); } -vmcs_write32 (EXCEPTION_BITMAP , exception_bitmap); +update_exception_bitmap (vcpu);vmcs_writel (GUEST_DR7 , dr7);return 0 ;
@@ -767,14 +775,6 @@ static __exit void hardware_unsetup(void)free_kvm_area (); } -static void update_exception_bitmap (struct kvm_vcpu *vcpu) -{ -if (vcpu->rmode.active) -vmcs_write32 (EXCEPTION_BITMAP , ~0 ); -else -vmcs_write32 (EXCEPTION_BITMAP ,1 <<PF_VECTOR ); -} -static void fix_pmode_dataseg (int seg,struct kvm_save_segment *save) {struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
@@ -942,7 +942,7 @@ static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)if (!(cr0 &CR0_TS_MASK )) { vcpu->fpu_active =1 ; -vmcs_clear_bits (EXCEPTION_BITMAP ,CR0_TS_MASK ); +update_exception_bitmap (vcpu); }vmcs_writel (CR0_READ_SHADOW , cr0);
@@ -958,7 +958,7 @@ static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)if (!(vcpu->cr0 &CR0_TS_MASK )) { vcpu->fpu_active =0 ;vmcs_set_bits (GUEST_CR0 ,CR0_TS_MASK ); -vmcs_set_bits (EXCEPTION_BITMAP ,1 <<NM_VECTOR ); +update_exception_bitmap (vcpu); } }
@@ -1243,7 +1243,6 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu) | CPU_BASED_USE_TSC_OFFSETING /* 21.3 */ ); -vmcs_write32 (EXCEPTION_BITMAP ,1 <<PF_VECTOR );vmcs_write32 (PAGE_FAULT_ERROR_CODE_MASK ,0 );vmcs_write32 (PAGE_FAULT_ERROR_CODE_MATCH ,0 );vmcs_write32 (CR3_TARGET_COUNT ,0 );/* 22.2.1 */
@@ -1329,6 +1328,7 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)#ifdef CONFIG_X86_64 vmx_set_efer (vcpu,0 );#endif +update_exception_bitmap (vcpu);return 0 ;
@@ -1489,7 +1489,7 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)if (is_no_device (intr_info)) { vcpu->fpu_active =1 ; -vmcs_clear_bits (EXCEPTION_BITMAP ,1 <<NM_VECTOR ); +update_exception_bitmap (vcpu);if (!(vcpu->cr0 &CR0_TS_MASK ))vmcs_clear_bits (GUEST_CR0 ,CR0_TS_MASK );return 1 ;
@@ -1684,7 +1684,7 @@ static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)case 2 :/* clts */ vcpu_load_rsp_rip (vcpu); vcpu->fpu_active =1 ; -vmcs_clear_bits (EXCEPTION_BITMAP ,1 <<NM_VECTOR ); +update_exception_bitmap (vcpu);vmcs_clear_bits (GUEST_CR0 ,CR0_TS_MASK ); vcpu->cr0 &= ~CR0_TS_MASK ;vmcs_writel (CR0_READ_SHADOW , vcpu->cr0);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 15/58 KVM: Consolidate guest fpu activation and deactivation
In reply to Avi KivityEasier to keep track of where the fpu is this way.
--- drivers/kvm/kvm.h | 2 +- drivers/kvm/vmx.c | 50 +++++++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 20 deletions(-)
drivers/kvm/kvm.h +1 -1
@@ -42,7 +42,7 @@ (CR0_PG_MASK |CR0_PE_MASK |CR0_WP_MASK |CR0_NE_MASK \ | CR0_NW_MASK | CR0_CD_MASK)#define KVM_VM_CR0_ALWAYS_ON \ - (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK) + (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK | CR0_TS_MASK)#define KVM_GUEST_CR4_MASK \ (CR4_PSE_MASK | CR4_PAE_MASK | CR4_PGE_MASK | CR4_VMXE_MASK | CR4_VME_MASK)#define KVM_PMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK)
drivers/kvm/vmx.c +31 -19
103 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 16/58 KVM: Set cr0.mp for guests
In reply to Avi KivityThis allows fwait instructions to be trapped when the guest fpu is not loaded.
--- drivers/kvm/kvm.h | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
drivers/kvm/kvm.h +3 -1
@@ -18,6 +18,7 @@#include <linux/kvm_para.h> #define CR0_PE_MASK (1ULL << 0) +#define CR0_MP_MASK (1ULL << 1)#define CR0_TS_MASK (1ULL << 3)#define CR0_NE_MASK (1ULL << 5)#define CR0_WP_MASK (1ULL << 16)
@@ -42,7 +43,8 @@ (CR0_PG_MASK |CR0_PE_MASK |CR0_WP_MASK |CR0_NE_MASK \ | CR0_NW_MASK | CR0_CD_MASK)#define KVM_VM_CR0_ALWAYS_ON \ - (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK | CR0_TS_MASK) + (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK | CR0_TS_MASK \ + | CR0_MP_MASK)#define KVM_GUEST_CR4_MASK \ (CR4_PSE_MASK | CR4_PAE_MASK | CR4_PGE_MASK | CR4_VMXE_MASK | CR4_VME_MASK)#define KVM_PMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK)
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 17/58 KVM: Implement IA32_EBL_CR_POWERON msr
In reply to Avi KivityFrom: Matthew Gregan <[email protected]>
Attempting to boot the default 'bsd' kernel of OpenBSD 4.1 i386 in a guest fails early in the kernel init inside p3_get_bus_clock while trying to read the IA32_EBL_CR_POWERON MSR. KVM logs an 'unhandled MSR' message and the guest kernel faults.
This patch is sufficient to allow OpenBSD to boot, after which it seems to run fine. I'm not sure if this is the correct solution for dealing with this particular MSR, but it works for me.
--- drivers/kvm/kvm_main.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
drivers/kvm/kvm_main.c +1 -0
@@ -1470,6 +1470,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)case MSR_IA32_MC0_MISC +16 :case MSR_IA32_UCODE_REV :case MSR_IA32_PERF_STATUS : +case MSR_IA32_EBL_CR_POWERON :/* MTRR registers */ case 0xfe : case 0x200 ... 0x2ff:
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 18/58 KVM: MMU: Simplify kvm_mmu_free_page() a tiny bit
In reply to Avi Kivity--- drivers/kvm/mmu.c | 10 ++++------ 1 files changed, 4 insertions(+), 6 deletions(-)
drivers/kvm/mmu.c +4 -6
@@ -455,12 +455,10 @@ static int is_empty_shadow_page(hpa_t page_hpa) }#endif -static void kvm_mmu_free_page (struct kvm_vcpu *vcpu,hpa_t page_hpa) +static void kvm_mmu_free_page (struct kvm_vcpu *vcpu, +struct kvm_mmu_page *page_head) { -struct kvm_mmu_page *page_head =page_header (page_hpa); - -ASSERT (is_empty_shadow_page (page_hpa)); - page_head->page_hpa = page_hpa; +ASSERT (is_empty_shadow_page (page_head->page_hpa));list_move (&page_head->link, &vcpu->free_pages); ++vcpu->kvm->n_free_mmu_pages; }
@@ -690,7 +688,7 @@ static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu,kvm_mmu_page_unlink_children (vcpu, page);if (!page->root_count) {hlist_del (&page->hash_link); -kvm_mmu_free_page (vcpu, page->page_hpa); +kvm_mmu_free_page (vcpu, page); }else list_move (&page->link, &vcpu->kvm->active_mmu_pages); }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 19/58 KVM: MMU: Store shadow page tables as kernel virtual addresses, not physical
In reply to Avi KivitySimpifies things a bit.
--- drivers/kvm/kvm.h | 2 +- drivers/kvm/mmu.c | 32 +++++++++++++++----------------- drivers/kvm/paging_tmpl.h | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-)
drivers/kvm/kvm.h +1 -1
@@ -139,7 +139,7 @@ struct kvm_mmu_page {gfn_t gfn;union kvm_mmu_page_role role; -hpa_t page_hpa; +u64 *spt;unsigned long slot_bitmap;/* One bit set per slot which has memory * in this shadow page. */
drivers/kvm/mmu.c +15 -17
@@ -439,13 +439,12 @@ static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn) }#ifdef MMU_DEBUG -static int is_empty_shadow_page (hpa_t page_hpa) +static int is_empty_shadow_page (u64 *spt) {u64 *pos;u64 *end; -for (pos =__va (page_hpa), end = pos +PAGE_SIZE /sizeof (u64); - pos != end; pos++) +for (pos = spt, end = pos +PAGE_SIZE /sizeof (u64); pos != end; pos++)if (*pos !=0 ) {printk (KERN_ERR "%s: %p %llx\n" , __FUNCTION__, pos, *pos);
@@ -458,7 +457,7 @@ static int is_empty_shadow_page(hpa_t page_hpa)static void kvm_mmu_free_page (struct kvm_vcpu *vcpu,struct kvm_mmu_page *page_head) { -ASSERT (is_empty_shadow_page (page_head->page_hpa)); +ASSERT (is_empty_shadow_page (page_head->spt));list_move (&page_head->link, &vcpu->free_pages); ++vcpu->kvm->n_free_mmu_pages; }
@@ -478,7 +477,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_page(struct kvm_vcpu *vcpu, page = list_entry(vcpu->free_pages.next, struct kvm_mmu_page, link);list_move (&page->link, &vcpu->kvm->active_mmu_pages); -ASSERT (is_empty_shadow_page (page->page_hpa)); +ASSERT (is_empty_shadow_page (page->spt)); page->slot_bitmap =0 ; page->multimapped =0 ; page->parent_pte = parent_pte;
@@ -636,7 +635,7 @@ static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,u64 *pt;u64 ent; - pt =__va (page->page_hpa); + pt = page->spt;if (page->role.level ==PT_PAGE_TABLE_LEVEL ) {for (i =0 ; i <PT64_ENT_PER_PAGE ; ++i) {
@@ -803,7 +802,7 @@ static int nonpaging_map(struct kvm_vcpu *vcpu, gva_t v, hpa_t p)return -ENOMEM ; } - table[index] = new_table->page_hpa |PT_PRESENT_MASK + table[index] =__pa (new_table->spt) |PT_PRESENT_MASK |PT_WRITABLE_MASK |PT_USER_MASK ; } table_addr = table[index] &PT64_BASE_ADDR_MASK ;
@@ -855,7 +854,7 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu)ASSERT (!VALID_PAGE (root)); page =kvm_mmu_get_page (vcpu, root_gfn,0 ,PT64_ROOT_LEVEL ,0 ,0 ,NULL ); - root = page->page_hpa; + root =__pa (page->spt); ++page->root_count; vcpu->mmu.root_hpa = root;return ;
@@ -876,7 +875,7 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu) page =kvm_mmu_get_page (vcpu, root_gfn, i <<30 ,PT32_ROOT_LEVEL , !is_paging (vcpu),0 ,NULL ); - root = page->page_hpa; + root =__pa (page->spt); ++page->root_count; vcpu->mmu.pae_root[i] = root |PT_PRESENT_MASK ; }
@@ -1220,8 +1219,7 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,if (quadrant != page->role.quadrant)continue ; } - spte =__va (page->page_hpa); - spte += page_offset /sizeof (*spte); + spte = &page->spt[page_offset /sizeof (*spte)];while (npte--) {mmu_pte_write_zap_pte (vcpu, page, spte);mmu_pte_write_new_pte (vcpu, page, spte, new, bytes);
@@ -1262,8 +1260,8 @@ static void free_mmu_pages(struct kvm_vcpu *vcpu) page =list_entry (vcpu->free_pages.next, struct kvm_mmu_page, link);list_del (&page->link); -__free_page (pfn_to_page (page->page_hpa >>PAGE_SHIFT )); - page->page_hpa =INVALID_PAGE ; +free_page ((unsigned long )page->spt); + page->spt =NULL ; }free_page ((unsigned long )vcpu->mmu.pae_root); }
@@ -1282,8 +1280,8 @@ static int alloc_mmu_pages(struct kvm_vcpu *vcpu)if ((page =alloc_page (GFP_KERNEL )) ==NULL ) goto error_1;set_page_private (page, (unsigned long )page_header); - page_header->page_hpa = (hpa_t )page_to_pfn (page) <<PAGE_SHIFT ; -memset (__va (page_header->page_hpa),0 ,PAGE_SIZE ); + page_header->spt =page_address (page); +memset (page_header->spt,0 ,PAGE_SIZE );list_add (&page_header->link, &vcpu->free_pages); ++vcpu->kvm->n_free_mmu_pages; }
@@ -1346,7 +1344,7 @@ void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot)if (!test_bit (slot, &page->slot_bitmap))continue ; - pt =__va (page->page_hpa); + pt = page->spt;for (i =0 ; i <PT64_ENT_PER_PAGE ; ++i)/* avoid RMW */ if (pt[i] &PT_WRITABLE_MASK ) {
@@ -1497,7 +1495,7 @@ static int count_writable_mappings(struct kvm_vcpu *vcpu)int i;list_for_each_entry (page, &vcpu->kvm->active_mmu_pages, link) { -u64 *pt =__va (page->page_hpa); +u64 *pt = page->spt;if (page->role.level !=PT_PAGE_TABLE_LEVEL )continue ;
drivers/kvm/paging_tmpl.h +1 -1
@@ -304,7 +304,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, shadow_page =kvm_mmu_get_page (vcpu, table_gfn, addr, level-1 , metaphysical, hugepage_access, shadow_ent); - shadow_addr = shadow_page->page_hpa; + shadow_addr =__pa (shadow_page->spt); shadow_pte = shadow_addr |PT_PRESENT_MASK |PT_ACCESSED_MASK |PT_WRITABLE_MASK |PT_USER_MASK ; *shadow_ent = shadow_pte;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 20/58 KVM: VMX: Only reload guest msrs if they are already loaded
In reply to Avi KivityIf we set an msr via an ioctl() instead of by handling a guest exit, we have the host state loaded, so reloading the msrs would clobber host state instead of guest state.
This fixes a host oops (and loss of a cpu) on a guest reboot.
--- drivers/kvm/vmx.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/kvm/vmx.c +2 -1
@@ -600,7 +600,8 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) msr =find_msr_entry (vcpu, msr_index);if (msr) msr->data = data; -load_msrs (vcpu->guest_msrs,NR_BAD_MSRS ); +if (vcpu->vmx_host_state.loaded) +load_msrs (vcpu->guest_msrs,NR_BAD_MSRS );break ;#endif case MSR_IA32_SYSENTER_CS :
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 21/58 KVM: Avoid corrupting tr in real mode
In reply to Avi KivityThe real mode tr needs to be set to a specific tss so that I/O instructions can function. Divert the new tr values to the real mode save area from where they will be restored on transition to protected mode.
This fixes some crashes on reboot when the bios accesses an I/O instruction.
--- drivers/kvm/vmx.c | 45 +++++++++++++++++++++++++++++++-------------- 1 files changed, 31 insertions(+), 14 deletions(-)
drivers/kvm/vmx.c +31 -14
@@ -1042,23 +1042,11 @@ static void vmx_get_segment(struct kvm_vcpu *vcpu, var->unusable = (ar >>16 ) &1 ; } -static void vmx_set_segment (struct kvm_vcpu *vcpu, -struct kvm_segment *var,int seg) +static u32 vmx_segment_access_rights (struct kvm_segment *var) { -struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];u32 ar; -vmcs_writel (sf->base, var->base); -vmcs_write32 (sf->limit, var->limit); -vmcs_write16 (sf->selector, var->selector); -if (vcpu->rmode.active && var->s) { -/* -* Hack real-mode segments into vm86 compatibility. -*/ -if (var->base ==0xffff0000 && var->selector ==0xf000 ) -vmcs_writel (sf->base,0xf0000 ); - ar =0xf3 ; - }else if (var->unusable) +if (var->unusable) ar =1 <<16 ;else { ar = var->type &15 ;
@@ -1072,6 +1060,35 @@ static void vmx_set_segment(struct kvm_vcpu *vcpu, }if (ar ==0 )/* a 0 value means unusable */ ar =AR_UNUSABLE_MASK ; + +return ar; +} + +static void vmx_set_segment (struct kvm_vcpu *vcpu, +struct kvm_segment *var,int seg) +{ +struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; +u32 ar; + +if (vcpu->rmode.active && seg ==VCPU_SREG_TR ) { + vcpu->rmode.tr.selector = var->selector; + vcpu->rmode.tr.base = var->base; + vcpu->rmode.tr.limit = var->limit; + vcpu->rmode.tr.ar =vmx_segment_access_rights (var); +return ; + } +vmcs_writel (sf->base, var->base); +vmcs_write32 (sf->limit, var->limit); +vmcs_write16 (sf->selector, var->selector); +if (vcpu->rmode.active && var->s) { +/* +* Hack real-mode segments into vm86 compatibility. +*/ +if (var->base ==0xffff0000 && var->selector ==0xf000 ) +vmcs_writel (sf->base,0xf0000 ); + ar =0xf3 ; + }else + ar =vmx_segment_access_rights (var);vmcs_write32 (sf->ar_bytes, ar); }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 22/58 KVM: Fix vmx I/O bitmap initialization on highmem systems
In reply to Avi Kivitykunmap() expects a struct page, not a virtual address. Fixes an oops loading kvm-intel.ko on i386 with CONFIG_HIGHMEM.
Thanks to Michael Ivanov <[email protected]> for reporting.
--- drivers/kvm/vmx.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/kvm/vmx.c +2 -2
@@ -2274,11 +2274,11 @@ static int __init vmx_init(void) iova =kmap (vmx_io_bitmap_a);memset (iova,0xff ,PAGE_SIZE );clear_bit (0x80 , iova); -kunmap (iova); +kunmap (vmx_io_bitmap_a); iova =kmap (vmx_io_bitmap_b);memset (iova,0xff ,PAGE_SIZE ); -kunmap (iova); +kunmap (vmx_io_bitmap_b); r =kvm_init_arch (&vmx_arch_ops,THIS_MODULE );if (r)
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 23/58 KVM: VMX: Use local labels in inline assembly
In reply to Avi KivityThis makes oprofile dumps and disassebly easier to read.
--- drivers/kvm/vmx.c | 15 +++++++-------- 1 files changed, 7 insertions(+), 8 deletions(-)
drivers/kvm/vmx.c +7 -8
@@ -1188,7 +1188,7 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)struct descriptor_table dt;int i;int ret =0 ; - extern asmlinkage void kvm_vmx_return(void); +unsigned long kvm_vmx_return;if (!init_rmode_tss (vcpu->kvm)) { ret = -ENOMEM ;
@@ -1306,8 +1306,8 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)get_idt (&dt);vmcs_writel (HOST_IDTR_BASE , dt.base);/* 22.2.4 */ - -vmcs_writel (HOST_RIP , (unsigned long )kvm_vmx_return);/* 22.2.5 */ + asm ("mov $.Lkvm_vmx_return, %0" :"=r" (kvm_vmx_return)); +vmcs_writel (HOST_RIP , kvm_vmx_return);/* 22.2.5 */ rdmsr (MSR_IA32_SYSENTER_CS , host_sysenter_cs, junk);vmcs_write32 (HOST_IA32_SYSENTER_CS , host_sysenter_cs);
@@ -1997,12 +1997,11 @@ again:"mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */ #endif/* Enter guest mode */ -"jne launched \n\t" +"jne .Llaunched \n\t" ASM_VMX_VMLAUNCH "\n\t" -"jmp kvm_vmx_return \n\t" -"launched: " ASM_VMX_VMRESUME "\n\t" -".globl kvm_vmx_return \n\t" -"kvm_vmx_return: " +"jmp .Lkvm_vmx_return \n\t" +".Llaunched: " ASM_VMX_VMRESUME "\n\t" +".Lkvm_vmx_return: " /* Save guest registers, load host registers, keep flags */ #ifdef CONFIG_X86_64"xchg %3, (%%rsp) \n\t"
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 24/58 KVM: VMX: Handle #SS faults from real mode
In reply to Avi KivityFrom: Nitin A Kamble <[email protected]>
Instructions with address size override prefix opcode 0x67 Cause the #SS fault with 0 error code in VM86 mode. Forward them to the emulator.
--- drivers/kvm/vmx.c | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)
drivers/kvm/vmx.c +5 -1
@@ -1488,7 +1488,11 @@ static int handle_rmode_exception(struct kvm_vcpu *vcpu,if (!vcpu->rmode.active)return 0 ; -if (vec ==GP_VECTOR && err_code ==0 ) +/* +* Instruction with address size override prefix opcode 0x67 +* Cause the #SS fault with 0 error code in VM86 mode. +*/ +if (((vec ==GP_VECTOR ) || (vec ==SS_VECTOR )) && err_code ==0 )if (emulate_instruction (vcpu,NULL ,0 ,0 ) ==EMULATE_DONE )return 1 ;return 0 ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 25/58 KVM: VMX: Avoid saving and restoring msrs on lightweight vmexit
In reply to Avi KivityFrom: Eddie Dong <[email protected]>
In a lightweight exit (where we exit and reenter the guest without scheduling or exiting to userspace in between), we don't need various msrs on the host, and avoiding shuffling them around reduces raw exit time by 8%.
i386 compile fix by Daniel Hecken <dh-LcvzS2Pvy/[email protected]>.
--- drivers/kvm/kvm.h | 4 ++ drivers/kvm/vmx.c | 128 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 76 insertions(+), 56 deletions(-)
drivers/kvm/kvm.h +4 -0
@@ -288,6 +288,10 @@ struct kvm_vcpu {u64 apic_base;u64 ia32_misc_enable_msr;int nmsrs; +int save_nmsrs; +#ifdef CONFIG_X86_64 +int msr_offset_kernel_gs_base; +#endif struct vmx_msr_entry *guest_msrs;struct vmx_msr_entry *host_msrs;
drivers/kvm/vmx.c +72 -56
197 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 26/58 KVM: VMX: Cleanup redundant code in MSR set
In reply to Avi KivityFrom: Eddie Dong <[email protected]>
--- drivers/kvm/vmx.c | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
drivers/kvm/vmx.c +0 -2
@@ -643,8 +643,6 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)break ; }return kvm_set_msr_common (vcpu, msr_index, data); - msr->data = data; -break ; }return 0 ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 27/58 KVM: VMX: Avoid saving and restoring msr_efer on lightweight vmexit
In reply to Avi KivityFrom: Eddie Dong <[email protected]>
MSR_EFER.LME/LMA bits are automatically save/restored by VMX hardware, KVM only needs to save NX/SCE bits at time of heavy weight VM Exit. But clearing NX bits in host envirnment may cause system hang if the host page table is using EXB bits, thus we leave NX bits as it is. If Host NX=1 and guest NX=0, we can do guest page table EXB bits check before inserting a shadow pte (though no guest is expecting to see this kind of gp fault). If host NX=0, we present guest no Execute-Disable feature to guest, thus no host NX=0, guest NX=1 combination.
This patch reduces raw vmexit time by ~27%.
Me: fix compile warnings on i386.
--- drivers/kvm/kvm.h | 2 + drivers/kvm/kvm_main.c | 23 ++++++++++++++++ drivers/kvm/vmx.c | 67 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 21 deletions(-)
drivers/kvm/kvm.h +2 -0
@@ -255,6 +255,7 @@ struct kvm_stat {u32 request_irq_exits;u32 irq_exits;u32 light_exits; +u32 efer_reload; };struct kvm_vcpu {
@@ -289,6 +290,7 @@ struct kvm_vcpu {u64 ia32_misc_enable_msr;int nmsrs;int save_nmsrs; +int msr_offset_efer;#ifdef CONFIG_X86_64 int msr_offset_kernel_gs_base;#endif
drivers/kvm/kvm_main.c +23 -0
@@ -73,6 +73,7 @@ static struct kvm_stats_debugfs_item { { "request_irq", STAT_OFFSET(request_irq_exits) }, { "irq_exits", STAT_OFFSET(irq_exits) }, { "light_exits", STAT_OFFSET(light_exits) }, + { "efer_reload", STAT_OFFSET(efer_reload) }, { NULL } };
@@ -2378,6 +2379,27 @@ out:return r; } +static void cpuid_fix_nx_cap (struct kvm_vcpu *vcpu) +{ +u64 efer; +int i; +struct kvm_cpuid_entry *e, *entry; + +rdmsrl (MSR_EFER , efer); + entry =NULL ; +for (i =0 ; i < vcpu->cpuid_nent; ++i) { + e = &vcpu->cpuid_entries[i]; +if (e->function ==0x80000001 ) { + entry = e; +break ; + } + } +if (entry && (entry->edx &EFER_NX ) && !(efer &EFER_NX )) { + entry->edx &= ~(1 <<20 ); +printk (KERN_INFO ": guest NX capability removed\n" ); + } +} +static int kvm_vcpu_ioctl_set_cpuid (struct kvm_vcpu *vcpu,struct kvm_cpuid *cpuid,struct kvm_cpuid_entry __user *entries)
@@ -2392,6 +2414,7 @@ static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, cpuid->nent * sizeof(struct kvm_cpuid_entry))) goto out; vcpu->cpuid_nent = cpuid->nent; +cpuid_fix_nx_cap (vcpu);return 0 ; out:
drivers/kvm/vmx.c +46 -21
138 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 28/58 Use menuconfig objects II - KVM/Virt
In reply to Avi KivityFrom: Jan Engelhardt <jengelh-CujU1KeUx2fb/[email protected]>
Make a "menuconfig" out of the Kconfig objects "menu, ..., endmenu", so that the user can disable all the options in that menu at once instead of having to disable each option separately.
--- drivers/kvm/Kconfig | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-)
drivers/kvm/Kconfig +6 -2
@@ -1,8 +1,12 @@ # # KVM configuration # -menu "Virtualization" +menuconfig VIRTUALIZATION + bool "Virtualization" depends on X86 + default y + +if VIRTUALIZATION config KVM tristate "Kernel-based Virtual Machine (KVM) support"
@@ -35,4 +39,4 @@ config KVM_AMD Provides support for KVM on AMD processors equipped with the AMD-V (SVM) extensions. -endmenu +endif # VIRTUALIZATION
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 29/58 KVM: x86 emulator: implement wbinvd
In reply to Avi KivityVista seems to trigger it.
--- drivers/kvm/x86_emulate.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
drivers/kvm/x86_emulate.c +3 -1
@@ -152,7 +152,7 @@ static u8 opcode_table[256] = {static u16 twobyte_table[256 ] = {/* 0x00 - 0x0F */ 0 , SrcMem | ModRM | DstReg,0 ,0 ,0 ,0 , ImplicitOps,0 , -0 ,0 ,0 ,0 ,0 , ImplicitOps | ModRM,0 ,0 , +0 , ImplicitOps,0 ,0 ,0 , ImplicitOps | ModRM,0 ,0 ,/* 0x10 - 0x1F */ 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , ImplicitOps | ModRM,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 0x20 - 0x2F */
@@ -1304,6 +1304,8 @@ twobyte_special_insn:/* Disable writeback. */ dst.orig_val = dst.val;switch (b) { +case 0x09 :/* wbinvd */ +break ;case 0x0d :/* GrpP (prefetch) */ case 0x18 :/* Grp16 (prefetch/nop) */ break ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 30/58 KVM: Fix includes
In reply to Avi KivityFrom: Markus Rechberger <[email protected]>
KVM compilation fails for some .configs. This fixes it.
--- drivers/kvm/kvm.h | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/kvm/kvm.h +2 -0
@@ -10,6 +10,8 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/spinlock.h> +#include <linux/signal.h> +#include <linux/sched.h> #include <linux/mm.h> #include <asm/signal.h>
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 31/58 KVM: Use symbolic constants instead of magic numbers
In reply to Avi KivityFrom: Eddie Dong <[email protected]>
--- drivers/kvm/paging_tmpl.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/kvm/paging_tmpl.h +1 -1
@@ -213,7 +213,7 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,if (~gpte & (PT_PRESENT_MASK |PT_ACCESSED_MASK ))return ;pgprintk ("%s: gpte %llx spte %p\n" , __FUNCTION__, (u64 )gpte, spte); -FNAME (set_pte)(vcpu, gpte, spte,6 , +FNAME (set_pte)(vcpu, gpte, spte,PT_USER_MASK |PT_WRITABLE_MASK , (gpte &PT_BASE_ADDR_MASK ) >>PAGE_SHIFT ); }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 32/58 KVM: MMU: Use slab caches for shadow pages and their headers
In reply to Avi KivityUse slab caches instead of a simple custom list.
--- drivers/kvm/kvm.h | 4 +- drivers/kvm/kvm_main.c | 1 - drivers/kvm/mmu.c | 64 +++++++++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 28 deletions(-)
drivers/kvm/kvm.h +2 -2
@@ -299,12 +299,12 @@ struct kvm_vcpu {struct vmx_msr_entry *guest_msrs;struct vmx_msr_entry *host_msrs; -struct list_head free_pages; -struct kvm_mmu_page page_header_buf[KVM_NUM_MMU_PAGES ];struct kvm_mmu mmu;struct kvm_mmu_memory_cache mmu_pte_chain_cache;struct kvm_mmu_memory_cache mmu_rmap_desc_cache; +struct kvm_mmu_memory_cache mmu_page_cache; +struct kvm_mmu_memory_cache mmu_page_header_cache;gfn_t last_pt_write_gfn;int last_pt_write_count;
drivers/kvm/kvm_main.c +0 -1
@@ -326,7 +326,6 @@ static struct kvm *kvm_create_vm(void) vcpu->cpu =-1 ; vcpu->kvm = kvm; vcpu->mmu.root_hpa =INVALID_PAGE ; -INIT_LIST_HEAD (&vcpu->free_pages);spin_lock (&kvm_lock);list_add (&kvm->vm_list, &vm_list);spin_unlock (&kvm_lock);
drivers/kvm/mmu.c +39 -25
138 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 33/58 KVM: MMU: Simplify fetch() a little bit
In reply to Avi Kivity--- drivers/kvm/paging_tmpl.h | 34 +++++++++++++++++----------------- 1 files changed, 17 insertions(+), 17 deletions(-)
drivers/kvm/paging_tmpl.h +17 -17
@@ -241,6 +241,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, {hpa_t shadow_addr;int level; +u64 *shadow_ent;u64 *prev_shadow_ent =NULL ;pt_element_t *guest_ent = walker->ptep;
@@ -257,13 +258,13 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,for (; ; level--) {u32 index =SHADOW_PT_INDEX (addr, level); -u64 *shadow_ent = ((u64 *)__va (shadow_addr)) + index;struct kvm_mmu_page *shadow_page;u64 shadow_pte;int metaphysical;gfn_t table_gfn;unsigned hugepage_access =0 ; + shadow_ent = ((u64 *)__va (shadow_addr)) + index;if (is_present_pte (*shadow_ent) ||is_io_pte (*shadow_ent)) {if (level ==PT_PAGE_TABLE_LEVEL )return shadow_ent;
@@ -272,22 +273,8 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,continue ; } -if (level ==PT_PAGE_TABLE_LEVEL ) { - -if (walker->level ==PT_DIRECTORY_LEVEL ) { -if (prev_shadow_ent) - *prev_shadow_ent |=PT_SHADOW_PS_MARK ; -FNAME (set_pde)(vcpu, *guest_ent, shadow_ent, - walker->inherited_ar, - walker->gfn); - }else { -ASSERT (walker->level ==PT_PAGE_TABLE_LEVEL ); -FNAME (set_pte)(vcpu, *guest_ent, shadow_ent, - walker->inherited_ar, - walker->gfn); - } -return shadow_ent; - } +if (level ==PT_PAGE_TABLE_LEVEL ) +break ;if (level -1 ==PT_PAGE_TABLE_LEVEL && walker->level ==PT_DIRECTORY_LEVEL ) {
@@ -310,6 +297,19 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, *shadow_ent = shadow_pte; prev_shadow_ent = shadow_ent; } + +if (walker->level ==PT_DIRECTORY_LEVEL ) { +if (prev_shadow_ent) + *prev_shadow_ent |=PT_SHADOW_PS_MARK ; +FNAME (set_pde)(vcpu, *guest_ent, shadow_ent, + walker->inherited_ar, walker->gfn); + }else { +ASSERT (walker->level ==PT_PAGE_TABLE_LEVEL ); +FNAME (set_pte)(vcpu, *guest_ent, shadow_ent, + walker->inherited_ar, + walker->gfn); + } +return shadow_ent; }/*
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 34/58 KVM: MMU: Move set_pte_common() to pte width dependent code
In reply to Avi KivityIn preparation of some modifications.
--- drivers/kvm/mmu.c | 48 -------------------------------------- drivers/kvm/paging_tmpl.h | 56 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 52 deletions(-)
drivers/kvm/mmu.c +0 -48
@@ -965,54 +965,6 @@ static void paging_new_cr3(struct kvm_vcpu *vcpu) kvm_arch_ops->set_cr3 (vcpu, vcpu->mmu.root_hpa); } -static inline void set_pte_common (struct kvm_vcpu *vcpu, -u64 *shadow_pte, -gpa_t gaddr, -int dirty, -u64 access_bits, -gfn_t gfn) -{ -hpa_t paddr; - - *shadow_pte |= access_bits <<PT_SHADOW_BITS_OFFSET ; -if (!dirty) - access_bits &= ~PT_WRITABLE_MASK ; - - paddr =gpa_to_hpa (vcpu, gaddr &PT64_BASE_ADDR_MASK ); - - *shadow_pte |= access_bits; - -if (is_error_hpa (paddr)) { - *shadow_pte |= gaddr; - *shadow_pte |=PT_SHADOW_IO_MARK ; - *shadow_pte &= ~PT_PRESENT_MASK ; -return ; - } - - *shadow_pte |= paddr; - -if (access_bits &PT_WRITABLE_MASK ) { -struct kvm_mmu_page *shadow; - - shadow =kvm_mmu_lookup_page (vcpu, gfn); -if (shadow) { -pgprintk ("%s: found shadow page for %lx, marking ro\n" , - __FUNCTION__, gfn); - access_bits &= ~PT_WRITABLE_MASK ; -if (is_writeble_pte (*shadow_pte)) { - *shadow_pte &= ~PT_WRITABLE_MASK ; - kvm_arch_ops->tlb_flush (vcpu); - } - } - } - -if (access_bits &PT_WRITABLE_MASK ) -mark_page_dirty (vcpu->kvm, gaddr >>PAGE_SHIFT ); - -page_header_update_slot (vcpu->kvm, shadow_pte, gaddr); -rmap_add (vcpu, shadow_pte); -} -static void inject_page_fault (struct kvm_vcpu *vcpu,u64 addr,u32 err_code)
drivers/kvm/paging_tmpl.h +52 -4
74 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 35/58 KVM: MMU: Pass the guest pde to set_pte_common
In reply to Avi KivityWe will need the accessed bit (in addition to the dirty bit) and also write access (for setting the dirty bit) in a future patch.
--- drivers/kvm/paging_tmpl.h | 29 +++++++++++++++-------------- 1 files changed, 15 insertions(+), 14 deletions(-)
drivers/kvm/paging_tmpl.h +15 -14
@@ -195,11 +195,12 @@ static void FNAME(mark_pagetable_dirty)(struct kvm *kvm,static void FNAME (set_pte_common )(struct kvm_vcpu *vcpu,u64 *shadow_pte,gpa_t gaddr, -int dirty, +pt_element_t *gpte,u64 access_bits,gfn_t gfn) {hpa_t paddr; +int dirty = *gpte &PT_DIRTY_MASK ; *shadow_pte |= access_bits <<PT_SHADOW_BITS_OFFSET ;if (!dirty)
@@ -240,14 +241,14 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,rmap_add (vcpu, shadow_pte); } -static void FNAME (set_pte )(struct kvm_vcpu *vcpu,u64 guest_pte, +static void FNAME (set_pte )(struct kvm_vcpu *vcpu,pt_element_t *gpte,u64 *shadow_pte,u64 access_bits,gfn_t gfn) {ASSERT (*shadow_pte ==0 ); - access_bits &= guest_pte; - *shadow_pte = (guest_pte &PT_PTE_COPY_MASK ); -FNAME (set_pte_common)(vcpu, shadow_pte, guest_pte &PT_BASE_ADDR_MASK , - guest_pte &PT_DIRTY_MASK , access_bits, gfn); + access_bits &= *gpte; + *shadow_pte = (*gpte &PT_PTE_COPY_MASK ); +FNAME (set_pte_common)(vcpu, shadow_pte, *gpte &PT_BASE_ADDR_MASK , + gpte, access_bits, gfn); } static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
@@ -261,24 +262,24 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,if (~gpte & (PT_PRESENT_MASK |PT_ACCESSED_MASK ))return ;pgprintk ("%s: gpte %llx spte %p\n" , __FUNCTION__, (u64 )gpte, spte); -FNAME (set_pte)(vcpu, gpte, spte,PT_USER_MASK |PT_WRITABLE_MASK , +FNAME (set_pte)(vcpu, &gpte, spte,PT_USER_MASK |PT_WRITABLE_MASK , (gpte &PT_BASE_ADDR_MASK ) >>PAGE_SHIFT ); } -static void FNAME (set_pde )(struct kvm_vcpu *vcpu,u64 guest_pde, +static void FNAME (set_pde )(struct kvm_vcpu *vcpu,pt_element_t *gpde,u64 *shadow_pte,u64 access_bits,gfn_t gfn) {gpa_t gaddr;ASSERT (*shadow_pte ==0 ); - access_bits &= guest_pde; + access_bits &= *gpde; gaddr = (gpa_t )gfn <<PAGE_SHIFT ;if (PTTYPE ==32 &&is_cpuid_PSE36 ()) - gaddr |= (guest_pde &PT32_DIR_PSE36_MASK ) << + gaddr |= (*gpde &PT32_DIR_PSE36_MASK ) << (32 -PT32_DIR_PSE36_SHIFT ); - *shadow_pte = guest_pde &PT_PTE_COPY_MASK ; + *shadow_pte = *gpde &PT_PTE_COPY_MASK ;FNAME (set_pte_common)(vcpu, shadow_pte, gaddr, - guest_pde &PT_DIRTY_MASK , access_bits, gfn); + gpde, access_bits, gfn); }/*
@@ -349,11 +350,11 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,if (walker->level ==PT_DIRECTORY_LEVEL ) {if (prev_shadow_ent) *prev_shadow_ent |=PT_SHADOW_PS_MARK ; -FNAME (set_pde)(vcpu, *guest_ent, shadow_ent, +FNAME (set_pde)(vcpu, guest_ent, shadow_ent, walker->inherited_ar, walker->gfn); }else {ASSERT (walker->level ==PT_PAGE_TABLE_LEVEL ); -FNAME (set_pte)(vcpu, *guest_ent, shadow_ent, +FNAME (set_pte)(vcpu, guest_ent, shadow_ent, walker->inherited_ar, walker->gfn); }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 36/58 KVM: MMU: Fold fix_read_pf() into set_pte_common()
In reply to Avi Kivity--- drivers/kvm/mmu.c | 17 ----------------- drivers/kvm/paging_tmpl.h | 34 +++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 28 deletions(-)
drivers/kvm/mmu.c +0 -17
@@ -972,23 +972,6 @@ static void inject_page_fault(struct kvm_vcpu *vcpu, kvm_arch_ops->inject_page_fault (vcpu, addr, err_code); } -static inline int fix_read_pf (u64 *shadow_ent) -{ -if ((*shadow_ent &PT_SHADOW_USER_MASK ) && - !(*shadow_ent &PT_USER_MASK )) { -/* -* If supervisor write protect is disabled, we shadow kernel -* pages as user pages so we can trap the write access. -*/ - *shadow_ent |=PT_USER_MASK ; - *shadow_ent &= ~PT_WRITABLE_MASK ; - -return 1 ; - - } -return 0 ; -} -static void paging_free (struct kvm_vcpu *vcpu) {nonpaging_free (vcpu);
drivers/kvm/paging_tmpl.h +23 -11
@@ -197,6 +197,7 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,gpa_t gaddr, pt_element_t *gpte, u64 access_bits, + int write_fault, gfn_t gfn) { hpa_t paddr;
@@ -219,6 +220,17 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, *shadow_pte |= paddr; +if (!write_fault && (*shadow_pte &PT_SHADOW_USER_MASK ) && + !(*shadow_pte &PT_USER_MASK )) { +/* +* If supervisor write protect is disabled, we shadow kernel +* pages as user pages so we can trap the write access. +*/ + *shadow_pte |=PT_USER_MASK ; + *shadow_pte &= ~PT_WRITABLE_MASK ; + access_bits &= ~PT_WRITABLE_MASK ; + } +if (access_bits &PT_WRITABLE_MASK ) {struct kvm_mmu_page *shadow;
@@ -242,13 +254,14 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, }static void FNAME (set_pte )(struct kvm_vcpu *vcpu,pt_element_t *gpte, -u64 *shadow_pte,u64 access_bits,gfn_t gfn) +u64 *shadow_pte,u64 access_bits, +int write_fault,gfn_t gfn) {ASSERT (*shadow_pte ==0 ); access_bits &= *gpte; *shadow_pte = (*gpte &PT_PTE_COPY_MASK );FNAME (set_pte_common)(vcpu, shadow_pte, *gpte &PT_BASE_ADDR_MASK , - gpte, access_bits, gfn); + gpte, access_bits, write_fault, gfn); } static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,
@@ -262,12 +275,13 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *page,if (~gpte & (PT_PRESENT_MASK |PT_ACCESSED_MASK ))return ;pgprintk ("%s: gpte %llx spte %p\n" , __FUNCTION__, (u64 )gpte, spte); -FNAME (set_pte)(vcpu, &gpte, spte,PT_USER_MASK |PT_WRITABLE_MASK , +FNAME (set_pte)(vcpu, &gpte, spte,PT_USER_MASK |PT_WRITABLE_MASK ,0 , (gpte &PT_BASE_ADDR_MASK ) >>PAGE_SHIFT ); }static void FNAME (set_pde )(struct kvm_vcpu *vcpu,pt_element_t *gpde, -u64 *shadow_pte,u64 access_bits,gfn_t gfn) +u64 *shadow_pte,u64 access_bits,int write_fault, +gfn_t gfn) {gpa_t gaddr;
@@ -279,14 +293,14 @@ static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t *gpde, (32 -PT32_DIR_PSE36_SHIFT ); *shadow_pte = *gpde &PT_PTE_COPY_MASK ;FNAME (set_pte_common)(vcpu, shadow_pte, gaddr, - gpde, access_bits, gfn); + gpde, access_bits, write_fault, gfn); }/* * Fetch a shadow pte for a specific level in the paging hierarchy. */ static u64 *FNAME (fetch )(struct kvm_vcpu *vcpu,gva_t addr, -struct guest_walker *walker) +struct guest_walker *walker,int write_fault) {hpa_t shadow_addr;int level;
@@ -351,12 +365,12 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,if (prev_shadow_ent) *prev_shadow_ent |=PT_SHADOW_PS_MARK ;FNAME (set_pde)(vcpu, guest_ent, shadow_ent, - walker->inherited_ar, walker->gfn); + walker->inherited_ar, write_fault, walker->gfn); }else {ASSERT (walker->level ==PT_PAGE_TABLE_LEVEL );FNAME (set_pte)(vcpu, guest_ent, shadow_ent, walker->inherited_ar, - walker->gfn); + write_fault, walker->gfn); }return shadow_ent; }
@@ -489,7 +503,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,return 0 ; } - shadow_pte =FNAME (fetch)(vcpu, addr, &walker); + shadow_pte =FNAME (fetch)(vcpu, addr, &walker, write_fault);pgprintk ("%s: shadow pte %p %llx\n" , __FUNCTION__, shadow_pte, *shadow_pte);
@@ -499,8 +513,6 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,if (write_fault) fixed =FNAME (fix_write_pf)(vcpu, shadow_pte, &walker, addr, user_fault, &write_pt); -else - fixed =fix_read_pf (shadow_pte);pgprintk ("%s: updated shadow pte %p %llx\n" , __FUNCTION__, shadow_pte, *shadow_pte);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 37/58 KVM: MMU: Fold fix_write_pf() into set_pte_common()
In reply to Avi KivityThis prevents some work from being performed twice, and, more importantly, reduces the number of places where we modify shadow ptes.
--- drivers/kvm/mmu.c | 11 +++ drivers/kvm/paging_tmpl.h | 168 +++++++++++++++----------------------------- 2 files changed, 68 insertions(+), 111 deletions(-)
drivers/kvm/mmu.c +11 -0
@@ -731,6 +731,17 @@ static int kvm_mmu_unprotect_page(struct kvm_vcpu *vcpu, gfn_t gfn)return r; } +static void mmu_unshadow (struct kvm_vcpu *vcpu,gfn_t gfn) +{ +struct kvm_mmu_page *page; + +while ((page =kvm_mmu_lookup_page (vcpu, gfn)) !=NULL ) { +pgprintk ("%s: zap %lx %x\n" , + __FUNCTION__, gfn, page->role.word); +kvm_mmu_zap_page (vcpu, page); + } +} +static void page_header_update_slot (struct kvm *kvm,void *pte,gpa_t gpa) {int slot =memslot_id (kvm,gfn_to_memslot (kvm, gpa >>PAGE_SHIFT ));
drivers/kvm/paging_tmpl.h +57 -111
271 lines not on this page · raw message
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 38/58 KVM: Move shadow pte modifications from set_pte/set_pde to set_pde_common()
In reply to Avi KivityWe want all shadow pte modifications in one place.
--- drivers/kvm/paging_tmpl.h | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/kvm/paging_tmpl.h +1 -2
@@ -218,6 +218,7 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,FNAME (mark_pagetable_dirty)(vcpu->kvm, walker); } + *shadow_pte |= *gpte &PT_PTE_COPY_MASK ; *shadow_pte |= access_bits <<PT_SHADOW_BITS_OFFSET ;if (!dirty) access_bits &= ~PT_WRITABLE_MASK ;
@@ -288,7 +289,6 @@ static void FNAME(set_pte)(struct kvm_vcpu *vcpu, pt_element_t *gpte, struct guest_walker *walker, gfn_t gfn) { access_bits &= *gpte; - *shadow_pte |= (*gpte &PT_PTE_COPY_MASK );FNAME (set_pte_common)(vcpu, shadow_pte, *gpte &PT_BASE_ADDR_MASK , gpte, access_bits, user_fault, write_fault, ptwrite, walker, gfn);
@@ -322,7 +322,6 @@ static void FNAME(set_pde)(struct kvm_vcpu *vcpu, pt_element_t *gpde,if (PTTYPE ==32 &&is_cpuid_PSE36 ()) gaddr |= (*gpde &PT32_DIR_PSE36_MASK ) << (32 -PT32_DIR_PSE36_SHIFT ); - *shadow_pte |= *gpde &PT_PTE_COPY_MASK ;FNAME (set_pte_common)(vcpu, shadow_pte, gaddr, gpde, access_bits, user_fault, write_fault, ptwrite, walker, gfn);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 39/58 KVM: Make shadow pte updates atomic
In reply to Avi KivityWith guest smp, a second vcpu might see partial updates when the first vcpu services a page fault. So delay all updates until we have figured out what the pte should look like.
Note that on i386, this is still not completely atomic as a 64-bit write will be split into two on a 32-bit machine.
--- drivers/kvm/paging_tmpl.h | 37 ++++++++++++++++++++----------------- 1 files changed, 20 insertions(+), 17 deletions(-)
drivers/kvm/paging_tmpl.h +20 -17
@@ -205,11 +205,12 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, {hpa_t paddr;int dirty = *gpte &PT_DIRTY_MASK ; -int was_rmapped =is_rmap_pte (*shadow_pte); +u64 spte = *shadow_pte; +int was_rmapped =is_rmap_pte (spte);pgprintk ("%s: spte %llx gpte %llx access %llx write_fault %d" " user_fault %d gfn %lx\n" , - __FUNCTION__, *shadow_pte, (u64)*gpte, access_bits, + __FUNCTION__, spte, (u64)*gpte, access_bits, write_fault, user_fault, gfn);if (write_fault && !dirty) {
@@ -218,34 +219,35 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,FNAME (mark_pagetable_dirty)(vcpu->kvm, walker); } - *shadow_pte |= *gpte &PT_PTE_COPY_MASK ; - *shadow_pte |= access_bits <<PT_SHADOW_BITS_OFFSET ; + spte |= *gpte &PT_PTE_COPY_MASK ; + spte |= access_bits <<PT_SHADOW_BITS_OFFSET ;if (!dirty) access_bits &= ~PT_WRITABLE_MASK ; paddr =gpa_to_hpa (vcpu, gaddr &PT64_BASE_ADDR_MASK ); - *shadow_pte |=PT_PRESENT_MASK ; + spte |=PT_PRESENT_MASK ;if (access_bits &PT_USER_MASK ) - *shadow_pte |=PT_USER_MASK ; + spte |=PT_USER_MASK ;if (is_error_hpa (paddr)) { - *shadow_pte |= gaddr; - *shadow_pte |=PT_SHADOW_IO_MARK ; - *shadow_pte &= ~PT_PRESENT_MASK ; + spte |= gaddr; + spte |=PT_SHADOW_IO_MARK ; + spte &= ~PT_PRESENT_MASK ; + *shadow_pte = spte;return ; } - *shadow_pte |= paddr; + spte |= paddr; -if (!write_fault && (*shadow_pte &PT_SHADOW_USER_MASK ) && - !(*shadow_pte &PT_USER_MASK )) { +if (!write_fault && (spte &PT_SHADOW_USER_MASK ) && + !(spte &PT_USER_MASK )) {/* * If supervisor write protect is disabled, we shadow kernel * pages as user pages so we can trap the write access. */ - *shadow_pte |=PT_USER_MASK ; - *shadow_pte &= ~PT_WRITABLE_MASK ; + spte |=PT_USER_MASK ; + spte &= ~PT_WRITABLE_MASK ; access_bits &= ~PT_WRITABLE_MASK ; }
@@ -253,7 +255,7 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, || (write_fault && !is_write_protection(vcpu) && !user_fault)) {struct kvm_mmu_page *shadow; - *shadow_pte |=PT_WRITABLE_MASK ; + spte |=PT_WRITABLE_MASK ;if (user_fault) {mmu_unshadow (vcpu, gfn); goto unshadowed;
@@ -264,8 +266,8 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,pgprintk ("%s: found shadow page for %lx, marking ro\n" , __FUNCTION__, gfn); access_bits &= ~PT_WRITABLE_MASK ; -if (is_writeble_pte (*shadow_pte)) { - *shadow_pte &= ~PT_WRITABLE_MASK ; +if (is_writeble_pte (spte)) { + spte &= ~PT_WRITABLE_MASK ; kvm_arch_ops->tlb_flush (vcpu); }if (write_fault)
@@ -278,6 +280,7 @@ unshadowed:if (access_bits &PT_WRITABLE_MASK )mark_page_dirty (vcpu->kvm, gaddr >>PAGE_SHIFT ); + *shadow_pte = spte;page_header_update_slot (vcpu->kvm, shadow_pte, gaddr);if (!was_rmapped)rmap_add (vcpu, shadow_pte);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 40/58 KVM: MMU: Make setting shadow ptes atomic on i386
In reply to Avi Kivity--- drivers/kvm/Kconfig | 1 + drivers/kvm/mmu.c | 14 ++++++++++++-- drivers/kvm/paging_tmpl.h | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-)
drivers/kvm/Kconfig +1 -0
@@ -11,6 +11,7 @@ if VIRTUALIZATION config KVM tristate "Kernel-based Virtual Machine (KVM) support" depends on X86 && EXPERIMENTAL + depends on X86_CMPXCHG64 || 64BIT ---help--- Support hosting fully virtualized guest machines using hardware virtualization extensions. You will need a fairly recent
drivers/kvm/mmu.c +12 -2
@@ -22,6 +22,7 @@#include <linux/mm.h> #include <linux/highmem.h> #include <linux/module.h> +#include <asm/cmpxchg.h> #include "vmx.h" #include "kvm.h"
@@ -204,6 +205,15 @@ static int is_rmap_pte(u64 pte) == (PT_WRITABLE_MASK | PT_PRESENT_MASK); } +static void set_shadow_pte (u64 *sptep,u64 spte) +{ +#ifdef CONFIG_X86_64 +set_64bit ((unsigned long *)sptep, spte); +#else +set_64bit ((unsigned long long *)sptep, spte); +#endif +} +static int mmu_topup_memory_cache (struct kvm_mmu_memory_cache *cache,struct kmem_cache *base_cache,int min,gfp_t gfp_flags)
@@ -446,7 +456,7 @@ static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)rmap_printk ("rmap_write_protect: spte %p %llx\n" , spte, *spte);rmap_remove (vcpu, spte); kvm_arch_ops->tlb_flush (vcpu); - *spte &= ~(u64 )PT_WRITABLE_MASK ; +set_shadow_pte (spte, *spte & ~PT_WRITABLE_MASK ); } }
@@ -699,7 +709,7 @@ static void kvm_mmu_zap_page(struct kvm_vcpu *vcpu, }BUG_ON (!parent_pte);kvm_mmu_put_page (vcpu, page, parent_pte); - *parent_pte =0 ; +set_shadow_pte (parent_pte,0 ); }kvm_mmu_page_unlink_children (vcpu, page);if (!page->root_count) {
drivers/kvm/paging_tmpl.h +2 -2
@@ -234,7 +234,7 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, spte |= gaddr; spte |=PT_SHADOW_IO_MARK ; spte &= ~PT_PRESENT_MASK ; - *shadow_pte = spte; +set_shadow_pte (shadow_pte, spte);return ; }
@@ -280,7 +280,7 @@ unshadowed:if (access_bits &PT_WRITABLE_MASK )mark_page_dirty (vcpu->kvm, gaddr >>PAGE_SHIFT ); - *shadow_pte = spte; +set_shadow_pte (shadow_pte, spte);page_header_update_slot (vcpu->kvm, shadow_pte, gaddr);if (!was_rmapped)rmap_add (vcpu, shadow_pte);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 41/58 KVM: MMU: Remove cr0.wp tricks
In reply to Avi KivityNo longer needed as we do everything in one place.
--- drivers/kvm/paging_tmpl.h | 11 ----------- 1 files changed, 0 insertions(+), 11 deletions(-)
drivers/kvm/paging_tmpl.h +0 -11
@@ -240,17 +240,6 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, spte |= paddr; -if (!write_fault && (spte &PT_SHADOW_USER_MASK ) && - !(spte &PT_USER_MASK )) { -/* -* If supervisor write protect is disabled, we shadow kernel -* pages as user pages so we can trap the write access. -*/ - spte |=PT_USER_MASK ; - spte &= ~PT_WRITABLE_MASK ; - access_bits &= ~PT_WRITABLE_MASK ; - } -if ((access_bits &PT_WRITABLE_MASK ) || (write_fault && !is_write_protection (vcpu) && !user_fault)) {struct kvm_mmu_page *shadow;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 42/58 KVM: MMU: Simpify accessed/dirty/present/nx bit handling
In reply to Avi KivityAlways set the accessed and dirty bit (since having them cleared causes a read-modify-write cycle), always set the present bit, and copy the nx bit from the guest.
--- drivers/kvm/mmu.c | 5 ----- drivers/kvm/paging_tmpl.h | 7 ++----- 2 files changed, 2 insertions(+), 10 deletions(-)
drivers/kvm/mmu.c +0 -5
@@ -91,11 +91,6 @@ static int dbg = 1;#define PT32_DIR_PSE36_MASK (((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT) -#define PT32_PTE_COPY_MASK \ - (PT_PRESENT_MASK | PT_ACCESSED_MASK | PT_DIRTY_MASK | PT_GLOBAL_MASK) - -#define PT64_PTE_COPY_MASK (PT64_NX_MASK | PT32_PTE_COPY_MASK) -#define PT_FIRST_AVAIL_BITS_SHIFT 9#define PT64_SECOND_AVAIL_BITS_SHIFT 52
drivers/kvm/paging_tmpl.h +2 -5
@@ -31,7 +31,6 @@#define PT_INDEX (addr, level) PT64_INDEX(addr, level)#define SHADOW_PT_INDEX (addr, level) PT64_INDEX(addr, level)#define PT_LEVEL_MASK (level) PT64_LEVEL_MASK(level) -#define PT_PTE_COPY_MASK PT64_PTE_COPY_MASK#ifdef CONFIG_X86_64 #define PT_MAX_FULL_LEVELS 4#else
@@ -46,7 +45,6 @@#define PT_INDEX (addr, level) PT32_INDEX(addr, level)#define SHADOW_PT_INDEX (addr, level) PT64_INDEX(addr, level)#define PT_LEVEL_MASK (level) PT32_LEVEL_MASK(level) -#define PT_PTE_COPY_MASK PT32_PTE_COPY_MASK#define PT_MAX_FULL_LEVELS 2#else #error Invalid PTTYPE value
@@ -219,7 +217,8 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu,FNAME (mark_pagetable_dirty)(vcpu->kvm, walker); } - spte |= *gpte &PT_PTE_COPY_MASK ; + spte |=PT_PRESENT_MASK |PT_ACCESSED_MASK |PT_DIRTY_MASK ; + spte |= *gpte &PT64_NX_MASK ; spte |= access_bits <<PT_SHADOW_BITS_OFFSET ;if (!dirty) access_bits &= ~PT_WRITABLE_MASK ;
@@ -495,7 +494,5 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t vaddr)#undef PT_INDEX#undef SHADOW_PT_INDEX#undef PT_LEVEL_MASK -#undef PT_PTE_COPY_MASK -#undef PT_NON_PTE_COPY_MASK#undef PT_DIR_BASE_ADDR_MASK#undef PT_MAX_FULL_LEVELS
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 43/58 KVM: MMU: Don't cache guest access bits in the shadow page table
In reply to Avi KivityThis was once used to avoid accessing the guest pte when upgrading the shadow pte from read-only to read-write. But usually we need to set the guest pte dirty or accessed bits anyway, so this wasn't really exploited.
--- drivers/kvm/mmu.c | 8 -------- drivers/kvm/paging_tmpl.h | 1 - 2 files changed, 0 insertions(+), 9 deletions(-)
drivers/kvm/mmu.c +0 -8
@@ -97,14 +97,6 @@ static int dbg = 1;#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT) -#define PT_SHADOW_WRITABLE_SHIFT (PT_FIRST_AVAIL_BITS_SHIFT + 1) -#define PT_SHADOW_WRITABLE_MASK (1ULL << PT_SHADOW_WRITABLE_SHIFT) - -#define PT_SHADOW_USER_SHIFT (PT_SHADOW_WRITABLE_SHIFT + 1) -#define PT_SHADOW_USER_MASK (1ULL << (PT_SHADOW_USER_SHIFT)) - -#define PT_SHADOW_BITS_OFFSET (PT_SHADOW_WRITABLE_SHIFT - PT_WRITABLE_SHIFT) -#define VALID_PAGE (x) ((x) != INVALID_PAGE)#define PT64_LEVEL_BITS 9
drivers/kvm/paging_tmpl.h +0 -1
@@ -219,7 +219,6 @@ static void FNAME(set_pte_common)(struct kvm_vcpu *vcpu, spte |=PT_PRESENT_MASK |PT_ACCESSED_MASK |PT_DIRTY_MASK ; spte |= *gpte &PT64_NX_MASK ; - spte |= access_bits <<PT_SHADOW_BITS_OFFSET ;if (!dirty) access_bits &= ~PT_WRITABLE_MASK ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 44/58 KVM: MMU: Remove unused large page marker
In reply to Avi KivityThis has not been used for some time, as the same information is available in the page header.
--- drivers/kvm/mmu.c | 1 - drivers/kvm/paging_tmpl.h | 2 -- 2 files changed, 0 insertions(+), 3 deletions(-)
drivers/kvm/mmu.c +0 -1
@@ -94,7 +94,6 @@ static int dbg = 1;#define PT_FIRST_AVAIL_BITS_SHIFT 9#define PT64_SECOND_AVAIL_BITS_SHIFT 52 -#define PT_SHADOW_PS_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)#define PT_SHADOW_IO_MARK (1ULL << PT_FIRST_AVAIL_BITS_SHIFT)#define VALID_PAGE (x) ((x) != INVALID_PAGE)
drivers/kvm/paging_tmpl.h +0 -2
@@ -384,8 +384,6 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, }if (walker->level ==PT_DIRECTORY_LEVEL ) { -if (prev_shadow_ent) - *prev_shadow_ent |=PT_SHADOW_PS_MARK ;FNAME (set_pde)(vcpu, guest_ent, shadow_ent, walker->inherited_ar, user_fault, write_fault, ptwrite, walker, walker->gfn);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 45/58 KVM: Lazy guest cr3 switching
In reply to Avi KivitySwitch guest paging context may require us to allocate memory, which might fail. Instead of wiring up error paths everywhere, make context switching lazy and actually do the switch before the next guest entry, where we can return an error if allocation fails.
--- drivers/kvm/kvm.h | 10 ++++++++++ drivers/kvm/mmu.c | 43 ++++++++++++++++++++++--------------------- drivers/kvm/svm.c | 4 ++++ drivers/kvm/vmx.c | 4 ++++ 4 files changed, 40 insertions(+), 21 deletions(-)
drivers/kvm/kvm.h +10 -0
@@ -544,6 +544,8 @@ void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *old, const u8 *new, int bytes);int kvm_mmu_unprotect_page_virt (struct kvm_vcpu *vcpu,gva_t gva);void kvm_mmu_free_some_pages (struct kvm_vcpu *vcpu); +int kvm_mmu_load (struct kvm_vcpu *vcpu); +void kvm_mmu_unload (struct kvm_vcpu *vcpu);int kvm_hypercall (struct kvm_vcpu *vcpu,struct kvm_run *run);
@@ -555,6 +557,14 @@ static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,return vcpu->mmu.page_fault (vcpu, gva, error_code); } +static inline int kvm_mmu_reload (struct kvm_vcpu *vcpu) +{ +if (likely (vcpu->mmu.root_hpa !=INVALID_PAGE )) +return 0 ; + +return kvm_mmu_load (vcpu); +} +static inline int is_long_mode (struct kvm_vcpu *vcpu) {#ifdef CONFIG_X86_64
drivers/kvm/mmu.c +22 -21
85 lines not on this page · raw message
drivers/kvm/svm.c +4 -0
@@ -1483,6 +1483,10 @@ static int svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)int r; again: + r =kvm_mmu_reload (vcpu); +if (unlikely (r)) +return r; +if (!vcpu->mmio_read_completed)do_interrupt_requests (vcpu, kvm_run);
drivers/kvm/vmx.c +4 -0
@@ -1988,6 +1988,10 @@ again:vmx_save_host_state (vcpu);kvm_load_guest_fpu (vcpu); + r =kvm_mmu_reload (vcpu); +if (unlikely (r)) + goto out; +/* * Loading guest fpu may have cleared host cr0.ts */
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
A message that is not in the archive (sent off-list or not yet received): 1182073479890-git-send-email-avi-atKUWr5tajBWk0Htik3J/[email protected]
PATCH 46/58 KVM: Replace C code with call to ARRAY_SIZE() macro.
From: Robert P. J. Day <[email protected]>
--- drivers/kvm/vmx.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/kvm/vmx.c +1 -1
@@ -1932,7 +1932,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, };static const int kvm_vmx_max_exit_handlers = -sizeof (kvm_vmx_exit_handlers) /sizeof (*kvm_vmx_exit_handlers); +ARRAY_SIZE (kvm_vmx_exit_handlers);/* * The guest has exited. See if we can fix it or if we need userspace
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 47/58 KVM: Remove unnecessary initialization and checks in mark_page_dirty()
In reply to Avi KivityFrom: Nguyen Anh Quynh <[email protected]>
--- drivers/kvm/kvm_main.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/kvm/kvm_main.c +2 -2
@@ -970,7 +970,7 @@ EXPORT_SYMBOL_GPL(gfn_to_page);void mark_page_dirty (struct kvm *kvm,gfn_t gfn) {int i; -struct kvm_memory_slot *memslot =NULL ; +struct kvm_memory_slot *memslot;unsigned long rel_gfn;for (i =0 ; i < kvm->nmemslots; ++i) {
@@ -979,7 +979,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn)if (gfn >= memslot->base_gfn && gfn < memslot->base_gfn + memslot->npages) { -if (!memslot || !memslot->dirty_bitmap) +if (!memslot->dirty_bitmap)return ; rel_gfn = gfn - memslot->base_gfn;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 48/58 KVM: Fix vcpu freeing for guest smp
In reply to Avi KivityA vcpu can pin up to four mmu shadow pages, which means the freeing loop will never terminate. Fix by first unpinning shadow pages on all vcpus, then freeing shadow pages.
--- drivers/kvm/kvm_main.c | 15 +++++++++++++++ drivers/kvm/mmu.c | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-)
drivers/kvm/kvm_main.c +15 -0
@@ -381,6 +381,16 @@ static void free_pio_guest_pages(struct kvm_vcpu *vcpu) } } +static void kvm_unload_vcpu_mmu (struct kvm_vcpu *vcpu) +{ +if (!vcpu->vmcs) +return ; + +vcpu_load (vcpu); +kvm_mmu_unload (vcpu); +vcpu_put (vcpu); +} +static void kvm_free_vcpu (struct kvm_vcpu *vcpu) {if (!vcpu->vmcs)
@@ -401,6 +411,11 @@ static void kvm_free_vcpus(struct kvm *kvm) {unsigned int i; +/* +* Unpin any mmu pages first. +*/ +for (i =0 ; i <KVM_MAX_VCPUS ; ++i) +kvm_unload_vcpu_mmu (&kvm->vcpus[i]);for (i =0 ; i <KVM_MAX_VCPUS ; ++i)kvm_free_vcpu (&kvm->vcpus[i]); }
drivers/kvm/mmu.c +2 -2
@@ -838,11 +838,12 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)int i;struct kvm_mmu_page *page; +if (!VALID_PAGE (vcpu->mmu.root_hpa)) +return ;#ifdef CONFIG_X86_64 if (vcpu->mmu.shadow_root_level ==PT64_ROOT_LEVEL ) {hpa_t root = vcpu->mmu.root_hpa; -ASSERT (VALID_PAGE (root)); page =page_header (root); --page->root_count; vcpu->mmu.root_hpa =INVALID_PAGE ;
@@ -853,7 +854,6 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)hpa_t root = vcpu->mmu.pae_root[i];if (root) { -ASSERT (VALID_PAGE (root)); root &=PT64_BASE_ADDR_MASK ; page =page_header (root); --page->root_count;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 49/58 KVM: Fix adding an smp virtual machine to the vm list
In reply to Avi KivityIf we add the vm once per vcpu, we corrupt the list if the guest has multiple vcpus.
--- drivers/kvm/kvm_main.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/kvm/kvm_main.c +3 -3
@@ -319,6 +319,9 @@ static struct kvm *kvm_create_vm(void)spin_lock_init (&kvm->lock);INIT_LIST_HEAD (&kvm->active_mmu_pages); +spin_lock (&kvm_lock); +list_add (&kvm->vm_list, &vm_list); +spin_unlock (&kvm_lock);for (i =0 ; i <KVM_MAX_VCPUS ; ++i) {struct kvm_vcpu *vcpu = &kvm->vcpus[i];
@@ -326,9 +329,6 @@ static struct kvm *kvm_create_vm(void) vcpu->cpu =-1 ; vcpu->kvm = kvm; vcpu->mmu.root_hpa =INVALID_PAGE ; -spin_lock (&kvm_lock); -list_add (&kvm->vm_list, &vm_list); -spin_unlock (&kvm_lock); }return kvm; }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 50/58 KVM: Enable guest smp
In reply to Avi KivityAs we don't support guest tlb shootdown yet, this is only reliable for real-mode guests.
--- drivers/kvm/kvm.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/kvm/kvm.h +1 -1
@@ -55,7 +55,7 @@#define INVALID_PAGE (~(hpa_t)0)#define UNMAPPED_GVA (~(gpa_t)0) -#define KVM_MAX_VCPUS 1 +#define KVM_MAX_VCPUS 4#define KVM_ALIAS_SLOTS 4#define KVM_MEMORY_SLOTS 4#define KVM_NUM_MMU_PAGES 1024
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 51/58 KVM: Move duplicate halt handling code into kvm_main.c
In reply to Avi KivityWill soon have a thid user.
--- drivers/kvm/kvm.h | 1 + drivers/kvm/kvm_main.c | 11 +++++++++++ drivers/kvm/svm.c | 7 +------ drivers/kvm/vmx.c | 7 +------ 4 files changed, 14 insertions(+), 12 deletions(-)
drivers/kvm/kvm.h +1 -0
@@ -504,6 +504,7 @@ int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in, int size, unsigned long count, int string, int down, gva_t address, int rep, unsigned port);void kvm_emulate_cpuid (struct kvm_vcpu *vcpu); +int kvm_emulate_halt (struct kvm_vcpu *vcpu);int emulate_invlpg (struct kvm_vcpu *vcpu,gva_t address);int emulate_clts (struct kvm_vcpu *vcpu); int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr,
drivers/kvm/kvm_main.c +11 -0
@@ -1285,6 +1285,17 @@ int emulate_instruction(struct kvm_vcpu *vcpu, }EXPORT_SYMBOL_GPL (emulate_instruction); +int kvm_emulate_halt (struct kvm_vcpu *vcpu) +{ +if (vcpu->irq_summary) +return 1 ; + + vcpu->run->exit_reason =KVM_EXIT_HLT ; + ++vcpu->stat.halt_exits; +return 0 ; +} +EXPORT_SYMBOL_GPL (kvm_emulate_halt); +int kvm_hypercall (struct kvm_vcpu *vcpu,struct kvm_run *run) {unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
drivers/kvm/svm.c +1 -6
@@ -1115,12 +1115,7 @@ static int halt_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) { vcpu->svm->next_rip = vcpu->svm->vmcb->save.rip +1 ;skip_emulated_instruction (vcpu); -if (vcpu->irq_summary) -return 1 ; - - kvm_run->exit_reason =KVM_EXIT_HLT ; - ++vcpu->stat.halt_exits; -return 0 ; +return kvm_emulate_halt (vcpu); }static int vmmcall_interception (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run)
drivers/kvm/vmx.c +1 -6
@@ -1896,12 +1896,7 @@ static int handle_interrupt_window(struct kvm_vcpu *vcpu,static int handle_halt (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run) {skip_emulated_instruction (vcpu); -if (vcpu->irq_summary) -return 1 ; - - kvm_run->exit_reason =KVM_EXIT_HLT ; - ++vcpu->stat.halt_exits; -return 0 ; +return kvm_emulate_halt (vcpu); }static int handle_vmcall (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run)
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 52/58 KVM: Emulate hlt on real mode for Intel
In reply to Avi KivityThis has two use cases: the bios can't boot from disk, and guest smp bootstrap.
--- drivers/kvm/kvm.h | 1 + drivers/kvm/vmx.c | 7 ++++++- drivers/kvm/x86_emulate.c | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-)
drivers/kvm/kvm.h +1 -0
@@ -347,6 +347,7 @@ struct kvm_vcpu {u32 ar; } tr, es, ds, fs, gs; } rmode; +int halt_request;/* real mode on Intel only */ int cpuid_nent;struct kvm_cpuid_entry cpuid_entries[KVM_MAX_CPUID_ENTRIES ];
drivers/kvm/vmx.c +6 -1
@@ -1608,8 +1608,13 @@ static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)if (vcpu->rmode.active &&handle_rmode_exception (vcpu, intr_info &INTR_INFO_VECTOR_MASK , - error_code)) + error_code)) { +if (vcpu->halt_request) { + vcpu->halt_request =0 ; +return kvm_emulate_halt (vcpu); + }return 1 ; + }if ((intr_info & (INTR_INFO_INTR_TYPE_MASK |INTR_INFO_VECTOR_MASK )) == (INTR_TYPE_EXCEPTION |1 )) { kvm_run->exit_reason =KVM_EXIT_DEBUG ;
drivers/kvm/x86_emulate.c +5 -1
@@ -143,7 +143,8 @@ static u8 opcode_table[256] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,/* 0xF0 - 0xF7 */ 0 ,0 ,0 ,0 , -0 ,0 , ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM, + ImplicitOps,0 , + ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM,/* 0xF8 - 0xFF */ 0 ,0 ,0 ,0 ,0 ,0 , ByteOp | DstMem | SrcNone | ModRM, DstMem | SrcNone | ModRM
@@ -1149,6 +1150,9 @@ special_insn: case 0xae ... 0xaf: /* scas */DPRINTF ("Urk! I don't handle SCAS.\n" ); goto cannot_emulate; +case 0xf4 :/* hlt */ + ctxt->vcpu->halt_request =1 ; + goto done; } goto writeback;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 53/58 KVM: Keep an upper bound of initialized vcpus
In reply to Avi KivityThat way, we don't need to loop for KVM_MAX_VCPUS for a single vcpu vm.
--- drivers/kvm/kvm.h | 1 + drivers/kvm/kvm_main.c | 5 +++++ 2 files changed, 6 insertions(+), 0 deletions(-)
drivers/kvm/kvm.h +1 -0
@@ -379,6 +379,7 @@ struct kvm {struct list_head active_mmu_pages;int n_free_mmu_pages;struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES ]; +int nvcpus;struct kvm_vcpu vcpus[KVM_MAX_VCPUS ];int memory_config_version;int busy;
drivers/kvm/kvm_main.c +5 -0
@@ -2391,6 +2391,11 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)if (r <0 ) goto out_free_vcpus; +spin_lock (&kvm_lock); +if (n >= kvm->nvcpus) + kvm->nvcpus = n +1 ; +spin_unlock (&kvm_lock); +return r; out_free_vcpus:
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 54/58 KVM: Flush remote tlbs when reducing shadow pte permissions
In reply to Avi KivityWhen a vcpu causes a shadow tlb entry to have reduced permissions, it must also clear the tlb on remote vcpus. We do that by:
- setting a bit on the vcpu that requests a tlb flush before the next entry
- if the vcpu is currently executing, we send an ipi to make sure it exits before we continue
--- drivers/kvm/kvm.h | 8 ++++++++ drivers/kvm/kvm_main.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ drivers/kvm/mmu.c | 8 +++++--- drivers/kvm/svm.c | 17 ++++++++++++----- drivers/kvm/vmx.c | 22 +++++++++++++++------- 5 files changed, 84 insertions(+), 15 deletions(-)
drivers/kvm/kvm.h +8 -0
@@ -84,6 +84,11 @@#define KVM_PIO_PAGE_OFFSET 1/* +* vcpu->requests bit members +*/ +#define KVM_TLB_FLUSH 0 + +/* * Address types: * * gva - guest virtual address
@@ -272,6 +277,8 @@ struct kvm_vcpu {u64 host_tsc;struct kvm_run *run;int interrupt_window_open; +int guest_mode; +unsigned long requests;unsigned long irq_summary;/* bit vector: 1 per word in irq_pending */ #define NR_IRQ_WORDS KVM_IRQ_BITMAP_SIZE(unsigned long)unsigned long irq_pending[NR_IRQ_WORDS ];
@@ -530,6 +537,7 @@ void save_msrs(struct vmx_msr_entry *e, int n);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); int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr,
drivers/kvm/kvm_main.c +44 -0
56 lines not on this page · raw message
drivers/kvm/mmu.c +5 -3
@@ -441,7 +441,7 @@ static void rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)BUG_ON (!(*spte &PT_WRITABLE_MASK ));rmap_printk ("rmap_write_protect: spte %p %llx\n" , spte, *spte);rmap_remove (vcpu, spte); - kvm_arch_ops->tlb_flush (vcpu); +kvm_flush_remote_tlbs (vcpu->kvm);set_shadow_pte (spte, *spte & ~PT_WRITABLE_MASK ); } }
@@ -656,7 +656,7 @@ static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu,rmap_remove (vcpu, &pt[i]); pt[i] =0 ; } - kvm_arch_ops->tlb_flush (vcpu); +kvm_flush_remote_tlbs (vcpu->kvm);return ; }
@@ -669,6 +669,7 @@ static void kvm_mmu_page_unlink_children(struct kvm_vcpu *vcpu, ent &=PT64_BASE_ADDR_MASK ;mmu_page_remove_parent_pte (vcpu,page_header (ent), &pt[i]); } +kvm_flush_remote_tlbs (vcpu->kvm); } static void kvm_mmu_put_page(struct kvm_vcpu *vcpu,
@@ -1093,6 +1094,7 @@ static void mmu_pte_write_zap_pte(struct kvm_vcpu *vcpu, } } *spte =0 ; +kvm_flush_remote_tlbs (vcpu->kvm); } static void mmu_pte_write_new_pte(struct kvm_vcpu *vcpu,
@@ -1308,7 +1310,7 @@ void kvm_mmu_zap_all(struct kvm_vcpu *vcpu) }mmu_free_memory_caches (vcpu); - kvm_arch_ops->tlb_flush (vcpu); +kvm_flush_remote_tlbs (vcpu->kvm);init_kvm_mmu (vcpu); }
drivers/kvm/svm.c +12 -5
@@ -1470,6 +1470,11 @@ static void load_db_regs(unsigned long *db_regs) asmvolatile ("mov %0, %%dr3" : :"r" (db_regs[3 ])); } +static void svm_flush_tlb (struct kvm_vcpu *vcpu) +{ +force_new_asid (vcpu); +} +static int svm_vcpu_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run) {u16 fs_selector;
@@ -1487,6 +1492,11 @@ again:clgi (); + vcpu->guest_mode =1 ; +if (vcpu->requests) +if (test_and_clear_bit (KVM_TLB_FLUSH , &vcpu->requests)) +svm_flush_tlb (vcpu); +pre_svm_run (vcpu);save_host_msrs (vcpu);
@@ -1618,6 +1628,8 @@ again:#endif : "cc", "memory" ); + vcpu->guest_mode =0 ; +if (vcpu->fpu_active) {fx_save (vcpu->guest_fx_image);fx_restore (vcpu->host_fx_image);
@@ -1682,11 +1694,6 @@ again:return r; } -static void svm_flush_tlb (struct kvm_vcpu *vcpu) -{ -force_new_asid (vcpu); -} -static void svm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long root) { vcpu->svm->vmcb->save.cr3 = root;
drivers/kvm/vmx.c +15 -7
@@ -1972,6 +1972,11 @@ static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF)); } +static void vmx_flush_tlb (struct kvm_vcpu *vcpu) +{ +vmcs_writel (GUEST_CR3 ,vmcs_readl (GUEST_CR3 )); +} +static int vmx_vcpu_run (struct kvm_vcpu *vcpu,struct kvm_run *kvm_run) {u8 fail;
@@ -1997,9 +2002,15 @@ again:*/ vmcs_writel (HOST_CR0 ,read_cr0 ()); +local_irq_disable (); + + vcpu->guest_mode =1 ; +if (vcpu->requests) +if (test_and_clear_bit (KVM_TLB_FLUSH , &vcpu->requests)) +vmx_flush_tlb (vcpu); + asm (/* Store host registers */ -"pushf \n\t" #ifdef CONFIG_X86_64 "push %%rax; push %%rbx; push %%rdx;" "push %%rsi; push %%rdi; push %%rbp;"
@@ -2091,7 +2102,6 @@ again: "pop %%ecx; popa \n\t" #endif "setbe %0 \n\t" - "popf \n\t" : "=q" (fail) : "r"(vcpu->launched), "d"((unsigned long)HOST_RSP), "c"(vcpu),
@@ -2115,6 +2125,9 @@ again: [cr2]"i"(offsetof(struct kvm_vcpu, cr2)) : "cc", "memory" ); + vcpu->guest_mode =0 ; +local_irq_enable (); + ++vcpu->stat.exits; vcpu->interrupt_window_open = (vmcs_read32 (GUEST_INTERRUPTIBILITY_INFO ) &3 ) ==0 ;
@@ -2167,11 +2180,6 @@ out:return r; } -static void vmx_flush_tlb (struct kvm_vcpu *vcpu) -{ -vmcs_writel (GUEST_CR3 ,vmcs_readl (GUEST_CR3 )); -} -static void vmx_inject_page_fault (struct kvm_vcpu *vcpu,unsigned long addr,u32 err_code)
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 55/58 KVM: SVM: Replace memset(<addr>, 0, PAGESIZE) with clear_page(<addr>)
In reply to Avi KivityFrom: Shani Moideen <[email protected]>
--- drivers/kvm/svm.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/kvm/svm.c +2 -2
@@ -581,7 +581,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) goto out2; vcpu->svm->vmcb =page_address (page); -memset (vcpu->svm->vmcb,0 ,PAGE_SIZE ); +clear_page (vcpu->svm->vmcb); vcpu->svm->vmcb_pa =page_to_pfn (page) <<PAGE_SHIFT ; vcpu->svm->asid_generation =0 ;memset (vcpu->svm->db_regs,0 ,sizeof (vcpu->svm->db_regs));
@@ -957,7 +957,7 @@ static int shutdown_interception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)* VMCB is undefined after a SHUTDOWN intercept * so reinitialize it. */ -memset (vcpu->svm->vmcb,0 ,PAGE_SIZE ); +clear_page (vcpu->svm->vmcb);init_vmcb (vcpu->svm->vmcb); kvm_run->exit_reason =KVM_EXIT_SHUTDOWN ;
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 56/58 KVM: VMX: Replace memset(<addr>, 0, PAGESIZE) with clear_page(<addr>)
In reply to Avi KivityFrom: Shani Moideen <[email protected]>
--- drivers/kvm/vmx.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/kvm/vmx.c +3 -3
@@ -1180,16 +1180,16 @@ static int init_rmode_tss(struct kvm* kvm) } page =kmap_atomic (p1,KM_USER0 ); -memset (page,0 ,PAGE_SIZE ); +clear_page (page); *(u16 *)(page +0x66 ) =TSS_BASE_SIZE +TSS_REDIRECTION_SIZE ;kunmap_atomic (page,KM_USER0 ); page =kmap_atomic (p2,KM_USER0 ); -memset (page,0 ,PAGE_SIZE ); +clear_page (page);kunmap_atomic (page,KM_USER0 ); page =kmap_atomic (p3,KM_USER0 ); -memset (page,0 ,PAGE_SIZE ); +clear_page (page); *(page +RMODE_TSS_SIZE -2 *PAGE_SIZE -1 ) = ~0 ;kunmap_atomic (page,KM_USER0 );
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 57/58 KVM: Initialize the BSP bit in the APIC_BASE msr correctly
In reply to Avi KivityNeeds to be set on vcpu 0 only.
--- drivers/kvm/svm.c | 6 +++--- drivers/kvm/vmx.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/kvm/svm.c +3 -3
@@ -589,9 +589,9 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)fx_init (vcpu); vcpu->fpu_active =1 ; - vcpu->apic_base =0xfee00000 | -/*for vcpu 0*/ MSR_IA32_APICBASE_BSP | -MSR_IA32_APICBASE_ENABLE ; + vcpu->apic_base =0xfee00000 |MSR_IA32_APICBASE_ENABLE ; +if (vcpu == &vcpu->kvm->vcpus[0 ]) + vcpu->apic_base |=MSR_IA32_APICBASE_BSP ;return 0 ;
drivers/kvm/vmx.c +3 -3
@@ -1238,9 +1238,9 @@ static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)memset (vcpu->regs,0 ,sizeof (vcpu->regs)); vcpu->regs[VCPU_REGS_RDX ] =get_rdx_init_val (); vcpu->cr8 =0 ; - vcpu->apic_base =0xfee00000 | -/*for vcpu 0*/ MSR_IA32_APICBASE_BSP | -MSR_IA32_APICBASE_ENABLE ; + vcpu->apic_base =0xfee00000 |MSR_IA32_APICBASE_ENABLE ; +if (vcpu == &vcpu->kvm->vcpus[0 ]) + vcpu->apic_base |=MSR_IA32_APICBASE_BSP ;fx_init (vcpu);
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/
PATCH 58/58 KVM: VMX: Ensure vcpu time stamp counter is monotonous
In reply to Avi KivityIf the time stamp counter goes backwards, a guest delay loop can become infinite. This can happen if a vcpu is migrated to another cpu, where the counter has a lower value than the first cpu.
Since we're doing an IPI to the first cpu anyway, we can use that to pick up the old tsc, and use that to calculate the adjustment we need to make to the tsc offset.
--- drivers/kvm/vmx.c | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-)
drivers/kvm/vmx.c +9 -0
@@ -160,6 +160,7 @@ static void __vcpu_clear(void *arg)vmcs_clear (vcpu->vmcs);if (per_cpu (current_vmcs, cpu) == vcpu->vmcs)per_cpu (current_vmcs, cpu) =NULL ; +rdtscll (vcpu->host_tsc); }static void vcpu_clear (struct kvm_vcpu *vcpu)
@@ -376,6 +377,7 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu) {u64 phys_addr =__pa (vcpu->vmcs);int cpu; +u64 tsc_this, delta; cpu =get_cpu ();
@@ -409,6 +411,13 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu)rdmsrl (MSR_IA32_SYSENTER_ESP , sysenter_esp);vmcs_writel (HOST_IA32_SYSENTER_ESP , sysenter_esp);/* 22.2.3 */ + +/* +* Make sure the time stamp counter is monotonous. +*/ +rdtscll (tsc_this); + delta = vcpu->host_tsc - tsc_this; +vmcs_write64 (TSC_OFFSET ,vmcs_read64 (TSC_OFFSET ) + delta); } }
Signature · 8 lines
1.5.0.6 ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/