Skip to content

Commit 8dcf578

Browse files
committed
Merge branch 'kvm-updates-2.6.26' of git://git.kernel.org/pub/scm/linux/kernel/git/avi/kvm
* 'kvm-updates-2.6.26' of git://git.kernel.org/pub/scm/linux/kernel/git/avi/kvm: x86: KVM geust: make setup_secondary_clock definition dependent on local apic KVM: MMU: Allow more than PAGES_PER_HPAGE write protections per large page KVM: avoid fx_init() schedule in atomic KVM: Avoid spurious execeptions after setting registers KVM: PIT: support mode 4 KVM: x86 emulator: disable writeback on lmsw KVM: ppc: deliver INTERRUPT_FP_UNAVAIL to the guest KVM: ppc: Handle guest idle by emulating MSR[WE] writes KVM: x86: task switch: fix wrong bit setting for the busy flag KVM: VMX: Enable EPT feature for KVM KVM: VMX: Prepare an identity page table for EPT in real mode KVM: Export necessary function for EPT KVM: MMU: Remove #ifdef CONFIG_X86_64 to support 4 level EPT KVM: MMU: Add EPT support KVM: Add kvm_x86_ops get_tdp_level() KVM: MMU: Move some definitions to a header file KVM: VMX: EPT Feature Detection
2 parents e73b65f + b8ba5f1 commit 8dcf578

15 files changed

Lines changed: 542 additions & 79 deletions

File tree

‎arch/powerpc/kvm/booke_guest.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
4949
{ "inst_emu", VCPU_STAT(emulated_inst_exits) },
5050
{ "dec", VCPU_STAT(dec_exits) },
5151
{ "ext_intr", VCPU_STAT(ext_intr_exits) },
52+
{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
5253
{ NULL }
5354
};
5455

@@ -338,6 +339,11 @@ int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
338339
}
339340
break;
340341

342+
case BOOKE_INTERRUPT_FP_UNAVAIL:
343+
kvmppc_queue_exception(vcpu, exit_nr);
344+
r = RESUME_GUEST;
345+
break;
346+
341347
case BOOKE_INTERRUPT_DATA_STORAGE:
342348
vcpu->arch.dear = vcpu->arch.fault_dear;
343349
vcpu->arch.esr = vcpu->arch.fault_esr;

‎arch/powerpc/kvm/powerpc.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
3636

3737
int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
3838
{
39-
/* XXX implement me */
40-
return 0;
39+
return !!(v->arch.pending_exceptions);
4140
}
4241

4342
int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
4443
{
45-
return 1;
44+
return !(v->arch.msr & MSR_WE);
4645
}
4746

4847

@@ -214,6 +213,11 @@ static void kvmppc_decrementer_func(unsigned long data)
214213
struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
215214

216215
kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
216+
217+
if (waitqueue_active(&vcpu->wq)) {
218+
wake_up_interruptible(&vcpu->wq);
219+
vcpu->stat.halt_wakeup++;
220+
}
217221
}
218222

219223
int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
@@ -339,6 +343,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
339343
int r;
340344
sigset_t sigsaved;
341345

346+
vcpu_load(vcpu);
347+
342348
if (vcpu->sigset_active)
343349
sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
344350

@@ -363,12 +369,20 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
363369
if (vcpu->sigset_active)
364370
sigprocmask(SIG_SETMASK, &sigsaved, NULL);
365371

372+
vcpu_put(vcpu);
373+
366374
return r;
367375
}
368376

369377
int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
370378
{
371379
kvmppc_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
380+
381+
if (waitqueue_active(&vcpu->wq)) {
382+
wake_up_interruptible(&vcpu->wq);
383+
vcpu->stat.halt_wakeup++;
384+
}
385+
372386
return 0;
373387
}
374388

‎arch/x86/kernel/kvmclock.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static int kvm_register_clock(void)
133133
return native_write_msr_safe(MSR_KVM_SYSTEM_TIME, low, high);
134134
}
135135

136+
#ifdef CONFIG_X86_LOCAL_APIC
136137
static void kvm_setup_secondary_clock(void)
137138
{
138139
/*
@@ -143,6 +144,7 @@ static void kvm_setup_secondary_clock(void)
143144
/* ok, done with our trickery, call native */
144145
setup_secondary_APIC_clock();
145146
}
147+
#endif
146148

147149
/*
148150
* After the clock is registered, the host will keep writing to the
@@ -177,7 +179,9 @@ void __init kvmclock_init(void)
177179
pv_time_ops.get_wallclock = kvm_get_wallclock;
178180
pv_time_ops.set_wallclock = kvm_set_wallclock;
179181
pv_time_ops.sched_clock = kvm_clock_read;
182+
#ifdef CONFIG_X86_LOCAL_APIC
180183
pv_apic_ops.setup_secondary_clock = kvm_setup_secondary_clock;
184+
#endif
181185
machine_ops.shutdown = kvm_shutdown;
182186
#ifdef CONFIG_KEXEC
183187
machine_ops.crash_shutdown = kvm_crash_shutdown;

‎arch/x86/kvm/i8254.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ static void pit_load_count(struct kvm *kvm, int channel, u32 val)
288288
* mode 1 is one shot, mode 2 is period, otherwise del timer */
289289
switch (ps->channels[0].mode) {
290290
case 1:
291+
/* FIXME: enhance mode 4 precision */
292+
case 4:
291293
create_pit_timer(&ps->pit_timer, val, 0);
292294
break;
293295
case 2:

‎arch/x86/kvm/mmu.c‎

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,6 @@ static int dbg = 1;
7979
}
8080
#endif
8181

82-
#define PT64_PT_BITS 9
83-
#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
84-
#define PT32_PT_BITS 10
85-
#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
86-
87-
#define PT_WRITABLE_SHIFT 1
88-
89-
#define PT_PRESENT_MASK (1ULL << 0)
90-
#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
91-
#define PT_USER_MASK (1ULL << 2)
92-
#define PT_PWT_MASK (1ULL << 3)
93-
#define PT_PCD_MASK (1ULL << 4)
94-
#define PT_ACCESSED_MASK (1ULL << 5)
95-
#define PT_DIRTY_MASK (1ULL << 6)
96-
#define PT_PAGE_SIZE_MASK (1ULL << 7)
97-
#define PT_PAT_MASK (1ULL << 7)
98-
#define PT_GLOBAL_MASK (1ULL << 8)
99-
#define PT64_NX_SHIFT 63
100-
#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
101-
102-
#define PT_PAT_SHIFT 7
103-
#define PT_DIR_PAT_SHIFT 12
104-
#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
105-
106-
#define PT32_DIR_PSE36_SIZE 4
107-
#define PT32_DIR_PSE36_SHIFT 13
108-
#define PT32_DIR_PSE36_MASK \
109-
(((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
110-
111-
11282
#define PT_FIRST_AVAIL_BITS_SHIFT 9
11383
#define PT64_SECOND_AVAIL_BITS_SHIFT 52
11484

@@ -154,10 +124,6 @@ static int dbg = 1;
154124
#define PFERR_USER_MASK (1U << 2)
155125
#define PFERR_FETCH_MASK (1U << 4)
156126

157-
#define PT64_ROOT_LEVEL 4
158-
#define PT32_ROOT_LEVEL 2
159-
#define PT32E_ROOT_LEVEL 3
160-
161127
#define PT_DIRECTORY_LEVEL 2
162128
#define PT_PAGE_TABLE_LEVEL 1
163129

@@ -186,6 +152,12 @@ static struct kmem_cache *mmu_page_header_cache;
186152

187153
static u64 __read_mostly shadow_trap_nonpresent_pte;
188154
static u64 __read_mostly shadow_notrap_nonpresent_pte;
155+
static u64 __read_mostly shadow_base_present_pte;
156+
static u64 __read_mostly shadow_nx_mask;
157+
static u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */
158+
static u64 __read_mostly shadow_user_mask;
159+
static u64 __read_mostly shadow_accessed_mask;
160+
static u64 __read_mostly shadow_dirty_mask;
189161

190162
void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
191163
{
@@ -194,6 +166,23 @@ void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte)
194166
}
195167
EXPORT_SYMBOL_GPL(kvm_mmu_set_nonpresent_ptes);
196168

169+
void kvm_mmu_set_base_ptes(u64 base_pte)
170+
{
171+
shadow_base_present_pte = base_pte;
172+
}
173+
EXPORT_SYMBOL_GPL(kvm_mmu_set_base_ptes);
174+
175+
void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
176+
u64 dirty_mask, u64 nx_mask, u64 x_mask)
177+
{
178+
shadow_user_mask = user_mask;
179+
shadow_accessed_mask = accessed_mask;
180+
shadow_dirty_mask = dirty_mask;
181+
shadow_nx_mask = nx_mask;
182+
shadow_x_mask = x_mask;
183+
}
184+
EXPORT_SYMBOL_GPL(kvm_mmu_set_mask_ptes);
185+
197186
static int is_write_protection(struct kvm_vcpu *vcpu)
198187
{
199188
return vcpu->arch.cr0 & X86_CR0_WP;
@@ -232,7 +221,7 @@ static int is_writeble_pte(unsigned long pte)
232221

233222
static int is_dirty_pte(unsigned long pte)
234223
{
235-
return pte & PT_DIRTY_MASK;
224+
return pte & shadow_dirty_mask;
236225
}
237226

238227
static int is_rmap_pte(u64 pte)
@@ -387,7 +376,6 @@ static void account_shadowed(struct kvm *kvm, gfn_t gfn)
387376

388377
write_count = slot_largepage_idx(gfn, gfn_to_memslot(kvm, gfn));
389378
*write_count += 1;
390-
WARN_ON(*write_count > KVM_PAGES_PER_HPAGE);
391379
}
392380

393381
static void unaccount_shadowed(struct kvm *kvm, gfn_t gfn)
@@ -547,7 +535,7 @@ static void rmap_remove(struct kvm *kvm, u64 *spte)
547535
return;
548536
sp = page_header(__pa(spte));
549537
pfn = spte_to_pfn(*spte);
550-
if (*spte & PT_ACCESSED_MASK)
538+
if (*spte & shadow_accessed_mask)
551539
kvm_set_pfn_accessed(pfn);
552540
if (is_writeble_pte(*spte))
553541
kvm_release_pfn_dirty(pfn);
@@ -1073,17 +1061,17 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *shadow_pte,
10731061
* whether the guest actually used the pte (in order to detect
10741062
* demand paging).
10751063
*/
1076-
spte = PT_PRESENT_MASK | PT_DIRTY_MASK;
1064+
spte = shadow_base_present_pte | shadow_dirty_mask;
10771065
if (!speculative)
10781066
pte_access |= PT_ACCESSED_MASK;
10791067
if (!dirty)
10801068
pte_access &= ~ACC_WRITE_MASK;
1081-
if (!(pte_access & ACC_EXEC_MASK))
1082-
spte |= PT64_NX_MASK;
1083-
1084-
spte |= PT_PRESENT_MASK;
1069+
if (pte_access & ACC_EXEC_MASK)
1070+
spte |= shadow_x_mask;
1071+
else
1072+
spte |= shadow_nx_mask;
10851073
if (pte_access & ACC_USER_MASK)
1086-
spte |= PT_USER_MASK;
1074+
spte |= shadow_user_mask;
10871075
if (largepage)
10881076
spte |= PT_PAGE_SIZE_MASK;
10891077

@@ -1188,8 +1176,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t v, int write,
11881176
return -ENOMEM;
11891177
}
11901178

1191-
table[index] = __pa(new_table->spt) | PT_PRESENT_MASK
1192-
| PT_WRITABLE_MASK | PT_USER_MASK;
1179+
table[index] = __pa(new_table->spt)
1180+
| PT_PRESENT_MASK | PT_WRITABLE_MASK
1181+
| shadow_user_mask | shadow_x_mask;
11931182
}
11941183
table_addr = table[index] & PT64_BASE_ADDR_MASK;
11951184
}
@@ -1244,7 +1233,6 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)
12441233
if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
12451234
return;
12461235
spin_lock(&vcpu->kvm->mmu_lock);
1247-
#ifdef CONFIG_X86_64
12481236
if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
12491237
hpa_t root = vcpu->arch.mmu.root_hpa;
12501238

@@ -1256,7 +1244,6 @@ static void mmu_free_roots(struct kvm_vcpu *vcpu)
12561244
spin_unlock(&vcpu->kvm->mmu_lock);
12571245
return;
12581246
}
1259-
#endif
12601247
for (i = 0; i < 4; ++i) {
12611248
hpa_t root = vcpu->arch.mmu.pae_root[i];
12621249

@@ -1282,7 +1269,6 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
12821269

12831270
root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
12841271

1285-
#ifdef CONFIG_X86_64
12861272
if (vcpu->arch.mmu.shadow_root_level == PT64_ROOT_LEVEL) {
12871273
hpa_t root = vcpu->arch.mmu.root_hpa;
12881274

@@ -1297,7 +1283,6 @@ static void mmu_alloc_roots(struct kvm_vcpu *vcpu)
12971283
vcpu->arch.mmu.root_hpa = root;
12981284
return;
12991285
}
1300-
#endif
13011286
metaphysical = !is_paging(vcpu);
13021287
if (tdp_enabled)
13031288
metaphysical = 1;
@@ -1377,7 +1362,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,
13771362
spin_lock(&vcpu->kvm->mmu_lock);
13781363
kvm_mmu_free_some_pages(vcpu);
13791364
r = __direct_map(vcpu, gpa, error_code & PFERR_WRITE_MASK,
1380-
largepage, gfn, pfn, TDP_ROOT_LEVEL);
1365+
largepage, gfn, pfn, kvm_x86_ops->get_tdp_level());
13811366
spin_unlock(&vcpu->kvm->mmu_lock);
13821367

13831368
return r;
@@ -1484,7 +1469,7 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)
14841469
context->page_fault = tdp_page_fault;
14851470
context->free = nonpaging_free;
14861471
context->prefetch_page = nonpaging_prefetch_page;
1487-
context->shadow_root_level = TDP_ROOT_LEVEL;
1472+
context->shadow_root_level = kvm_x86_ops->get_tdp_level();
14881473
context->root_hpa = INVALID_PAGE;
14891474

14901475
if (!is_paging(vcpu)) {
@@ -1633,7 +1618,7 @@ static bool last_updated_pte_accessed(struct kvm_vcpu *vcpu)
16331618
{
16341619
u64 *spte = vcpu->arch.last_pte_updated;
16351620

1636-
return !!(spte && (*spte & PT_ACCESSED_MASK));
1621+
return !!(spte && (*spte & shadow_accessed_mask));
16371622
}
16381623

16391624
static void mmu_guess_page_from_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,

‎arch/x86/kvm/mmu.h‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,38 @@
33

44
#include <linux/kvm_host.h>
55

6-
#ifdef CONFIG_X86_64
7-
#define TDP_ROOT_LEVEL PT64_ROOT_LEVEL
8-
#else
9-
#define TDP_ROOT_LEVEL PT32E_ROOT_LEVEL
10-
#endif
6+
#define PT64_PT_BITS 9
7+
#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
8+
#define PT32_PT_BITS 10
9+
#define PT32_ENT_PER_PAGE (1 << PT32_PT_BITS)
10+
11+
#define PT_WRITABLE_SHIFT 1
12+
13+
#define PT_PRESENT_MASK (1ULL << 0)
14+
#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT)
15+
#define PT_USER_MASK (1ULL << 2)
16+
#define PT_PWT_MASK (1ULL << 3)
17+
#define PT_PCD_MASK (1ULL << 4)
18+
#define PT_ACCESSED_MASK (1ULL << 5)
19+
#define PT_DIRTY_MASK (1ULL << 6)
20+
#define PT_PAGE_SIZE_MASK (1ULL << 7)
21+
#define PT_PAT_MASK (1ULL << 7)
22+
#define PT_GLOBAL_MASK (1ULL << 8)
23+
#define PT64_NX_SHIFT 63
24+
#define PT64_NX_MASK (1ULL << PT64_NX_SHIFT)
25+
26+
#define PT_PAT_SHIFT 7
27+
#define PT_DIR_PAT_SHIFT 12
28+
#define PT_DIR_PAT_MASK (1ULL << PT_DIR_PAT_SHIFT)
29+
30+
#define PT32_DIR_PSE36_SIZE 4
31+
#define PT32_DIR_PSE36_SHIFT 13
32+
#define PT32_DIR_PSE36_MASK \
33+
(((1ULL << PT32_DIR_PSE36_SIZE) - 1) << PT32_DIR_PSE36_SHIFT)
34+
35+
#define PT64_ROOT_LEVEL 4
36+
#define PT32_ROOT_LEVEL 2
37+
#define PT32E_ROOT_LEVEL 3
1138

1239
static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
1340
{

‎arch/x86/kvm/svm.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,15 @@ static bool svm_cpu_has_accelerated_tpr(void)
18631863
return false;
18641864
}
18651865

1866+
static int get_npt_level(void)
1867+
{
1868+
#ifdef CONFIG_X86_64
1869+
return PT64_ROOT_LEVEL;
1870+
#else
1871+
return PT32E_ROOT_LEVEL;
1872+
#endif
1873+
}
1874+
18661875
static struct kvm_x86_ops svm_x86_ops = {
18671876
.cpu_has_kvm_support = has_svm,
18681877
.disabled_by_bios = is_disabled,
@@ -1920,6 +1929,7 @@ static struct kvm_x86_ops svm_x86_ops = {
19201929
.inject_pending_vectors = do_interrupt_requests,
19211930

19221931
.set_tss_addr = svm_set_tss_addr,
1932+
.get_tdp_level = get_npt_level,
19231933
};
19241934

19251935
static int __init svm_init(void)

0 commit comments

Comments
 (0)