Skip to content

Commit ed0a0ec

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull KVM fixes from Paolo Bonzini: "A somewhat bigger ARM update, and the usual smattering of x86 bug fixes" * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: kvm: vmx: Fix entry number check for add_atomic_switch_msr() KVM: x86: Recompute PID.ON when clearing PID.SN KVM: nVMX: Restore a preemption timer consistency check x86/kvm/nVMX: read from MSR_IA32_VMX_PROCBASED_CTLS2 only when it is available KVM: arm64: Forbid kprobing of the VHE world-switch code KVM: arm64: Relax the restriction on using stage2 PUD huge mapping arm: KVM: Add missing kvm_stage2_has_pmd() helper KVM: arm/arm64: vgic: Always initialize the group of private IRQs arm/arm64: KVM: Don't panic on failure to properly reset system registers arm/arm64: KVM: Allow a VCPU to fully reset itself KVM: arm/arm64: Reset the VCPU without preemption and vcpu state loaded arm64: KVM: Don't generate UNDEF when LORegion feature is present KVM: arm/arm64: vgic: Make vgic_cpu->ap_list_lock a raw_spinlock KVM: arm/arm64: vgic: Make vgic_dist->lpi_list_lock a raw_spinlock KVM: arm/arm64: vgic: Make vgic_irq->irq_lock a raw_spinlock
2 parents 64c0133 + 98ae70c commit ed0a0ec

File tree

26 files changed

+331
-193
lines changed

26 files changed

+331
-193
lines changed

arch/arm/include/asm/kvm_host.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define KVM_REQ_SLEEP \
4949
KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
5050
#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
51+
#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2)
5152

5253
DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
5354

@@ -147,6 +148,13 @@ struct kvm_cpu_context {
147148

148149
typedef struct kvm_cpu_context kvm_cpu_context_t;
149150

151+
struct vcpu_reset_state {
152+
unsigned long pc;
153+
unsigned long r0;
154+
bool be;
155+
bool reset;
156+
};
157+
150158
struct kvm_vcpu_arch {
151159
struct kvm_cpu_context ctxt;
152160

@@ -186,6 +194,8 @@ struct kvm_vcpu_arch {
186194
/* Cache some mmu pages needed inside spinlock regions */
187195
struct kvm_mmu_memory_cache mmu_page_cache;
188196

197+
struct vcpu_reset_state reset_state;
198+
189199
/* Detect first run of a vcpu */
190200
bool has_run_once;
191201
};

arch/arm/include/asm/stage2_pgtable.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ static inline bool kvm_stage2_has_pud(struct kvm *kvm)
7676
#define S2_PMD_MASK PMD_MASK
7777
#define S2_PMD_SIZE PMD_SIZE
7878

79+
static inline bool kvm_stage2_has_pmd(struct kvm *kvm)
80+
{
81+
return true;
82+
}
83+
7984
#endif /* __ARM_S2_PGTABLE_H_ */

arch/arm/kvm/coproc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,6 @@ void kvm_reset_coprocs(struct kvm_vcpu *vcpu)
14501450
reset_coproc_regs(vcpu, table, num);
14511451

14521452
for (num = 1; num < NR_CP15_REGS; num++)
1453-
if (vcpu_cp15(vcpu, num) == 0x42424242)
1454-
panic("Didn't reset vcpu_cp15(vcpu, %zi)", num);
1453+
WARN(vcpu_cp15(vcpu, num) == 0x42424242,
1454+
"Didn't reset vcpu_cp15(vcpu, %zi)", num);
14551455
}

arch/arm/kvm/reset.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <asm/cputype.h>
2727
#include <asm/kvm_arm.h>
2828
#include <asm/kvm_coproc.h>
29+
#include <asm/kvm_emulate.h>
2930

3031
#include <kvm/arm_arch_timer.h>
3132

@@ -69,6 +70,29 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
6970
/* Reset CP15 registers */
7071
kvm_reset_coprocs(vcpu);
7172

73+
/*
74+
* Additional reset state handling that PSCI may have imposed on us.
75+
* Must be done after all the sys_reg reset.
76+
*/
77+
if (READ_ONCE(vcpu->arch.reset_state.reset)) {
78+
unsigned long target_pc = vcpu->arch.reset_state.pc;
79+
80+
/* Gracefully handle Thumb2 entry point */
81+
if (target_pc & 1) {
82+
target_pc &= ~1UL;
83+
vcpu_set_thumb(vcpu);
84+
}
85+
86+
/* Propagate caller endianness */
87+
if (vcpu->arch.reset_state.be)
88+
kvm_vcpu_set_be(vcpu);
89+
90+
*vcpu_pc(vcpu) = target_pc;
91+
vcpu_set_reg(vcpu, 0, vcpu->arch.reset_state.r0);
92+
93+
vcpu->arch.reset_state.reset = false;
94+
}
95+
7296
/* Reset arch_timer context */
7397
return kvm_timer_vcpu_reset(vcpu);
7498
}

arch/arm64/include/asm/kvm_host.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define KVM_REQ_SLEEP \
4949
KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
5050
#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)
51+
#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2)
5152

5253
DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use);
5354

@@ -208,6 +209,13 @@ struct kvm_cpu_context {
208209

209210
typedef struct kvm_cpu_context kvm_cpu_context_t;
210211

212+
struct vcpu_reset_state {
213+
unsigned long pc;
214+
unsigned long r0;
215+
bool be;
216+
bool reset;
217+
};
218+
211219
struct kvm_vcpu_arch {
212220
struct kvm_cpu_context ctxt;
213221

@@ -297,6 +305,9 @@ struct kvm_vcpu_arch {
297305
/* Virtual SError ESR to restore when HCR_EL2.VSE is set */
298306
u64 vsesr_el2;
299307

308+
/* Additional reset state */
309+
struct vcpu_reset_state reset_state;
310+
300311
/* True when deferrable sysregs are loaded on the physical CPU,
301312
* see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */
302313
bool sysregs_loaded_on_cpu;

arch/arm64/kvm/hyp/switch.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <kvm/arm_psci.h>
2424

2525
#include <asm/cpufeature.h>
26+
#include <asm/kprobes.h>
2627
#include <asm/kvm_asm.h>
2728
#include <asm/kvm_emulate.h>
2829
#include <asm/kvm_host.h>
@@ -107,6 +108,7 @@ static void activate_traps_vhe(struct kvm_vcpu *vcpu)
107108

108109
write_sysreg(kvm_get_hyp_vector(), vbar_el1);
109110
}
111+
NOKPROBE_SYMBOL(activate_traps_vhe);
110112

111113
static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu)
112114
{
@@ -154,6 +156,7 @@ static void deactivate_traps_vhe(void)
154156
write_sysreg(CPACR_EL1_DEFAULT, cpacr_el1);
155157
write_sysreg(vectors, vbar_el1);
156158
}
159+
NOKPROBE_SYMBOL(deactivate_traps_vhe);
157160

158161
static void __hyp_text __deactivate_traps_nvhe(void)
159162
{
@@ -513,6 +516,7 @@ int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
513516

514517
return exit_code;
515518
}
519+
NOKPROBE_SYMBOL(kvm_vcpu_run_vhe);
516520

517521
/* Switch to the guest for legacy non-VHE systems */
518522
int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu)
@@ -620,6 +624,7 @@ static void __hyp_call_panic_vhe(u64 spsr, u64 elr, u64 par,
620624
read_sysreg_el2(esr), read_sysreg_el2(far),
621625
read_sysreg(hpfar_el2), par, vcpu);
622626
}
627+
NOKPROBE_SYMBOL(__hyp_call_panic_vhe);
623628

624629
void __hyp_text __noreturn hyp_panic(struct kvm_cpu_context *host_ctxt)
625630
{

arch/arm64/kvm/hyp/sysreg-sr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/compiler.h>
1919
#include <linux/kvm_host.h>
2020

21+
#include <asm/kprobes.h>
2122
#include <asm/kvm_asm.h>
2223
#include <asm/kvm_emulate.h>
2324
#include <asm/kvm_hyp.h>
@@ -98,12 +99,14 @@ void sysreg_save_host_state_vhe(struct kvm_cpu_context *ctxt)
9899
{
99100
__sysreg_save_common_state(ctxt);
100101
}
102+
NOKPROBE_SYMBOL(sysreg_save_host_state_vhe);
101103

102104
void sysreg_save_guest_state_vhe(struct kvm_cpu_context *ctxt)
103105
{
104106
__sysreg_save_common_state(ctxt);
105107
__sysreg_save_el2_return_state(ctxt);
106108
}
109+
NOKPROBE_SYMBOL(sysreg_save_guest_state_vhe);
107110

108111
static void __hyp_text __sysreg_restore_common_state(struct kvm_cpu_context *ctxt)
109112
{
@@ -188,12 +191,14 @@ void sysreg_restore_host_state_vhe(struct kvm_cpu_context *ctxt)
188191
{
189192
__sysreg_restore_common_state(ctxt);
190193
}
194+
NOKPROBE_SYMBOL(sysreg_restore_host_state_vhe);
191195

192196
void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt)
193197
{
194198
__sysreg_restore_common_state(ctxt);
195199
__sysreg_restore_el2_return_state(ctxt);
196200
}
201+
NOKPROBE_SYMBOL(sysreg_restore_guest_state_vhe);
197202

198203
void __hyp_text __sysreg32_save_state(struct kvm_vcpu *vcpu)
199204
{

arch/arm64/kvm/reset.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <asm/kvm_arm.h>
3333
#include <asm/kvm_asm.h>
3434
#include <asm/kvm_coproc.h>
35+
#include <asm/kvm_emulate.h>
3536
#include <asm/kvm_mmu.h>
3637

3738
/* Maximum phys_shift supported for any VM on this host */
@@ -105,16 +106,33 @@ int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)
105106
* This function finds the right table above and sets the registers on
106107
* the virtual CPU struct to their architecturally defined reset
107108
* values.
109+
*
110+
* Note: This function can be called from two paths: The KVM_ARM_VCPU_INIT
111+
* ioctl or as part of handling a request issued by another VCPU in the PSCI
112+
* handling code. In the first case, the VCPU will not be loaded, and in the
113+
* second case the VCPU will be loaded. Because this function operates purely
114+
* on the memory-backed valus of system registers, we want to do a full put if
115+
* we were loaded (handling a request) and load the values back at the end of
116+
* the function. Otherwise we leave the state alone. In both cases, we
117+
* disable preemption around the vcpu reset as we would otherwise race with
118+
* preempt notifiers which also call put/load.
108119
*/
109120
int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
110121
{
111122
const struct kvm_regs *cpu_reset;
123+
int ret = -EINVAL;
124+
bool loaded;
125+
126+
preempt_disable();
127+
loaded = (vcpu->cpu != -1);
128+
if (loaded)
129+
kvm_arch_vcpu_put(vcpu);
112130

113131
switch (vcpu->arch.target) {
114132
default:
115133
if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
116134
if (!cpu_has_32bit_el1())
117-
return -EINVAL;
135+
goto out;
118136
cpu_reset = &default_regs_reset32;
119137
} else {
120138
cpu_reset = &default_regs_reset;
@@ -129,6 +147,29 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
129147
/* Reset system registers */
130148
kvm_reset_sys_regs(vcpu);
131149

150+
/*
151+
* Additional reset state handling that PSCI may have imposed on us.
152+
* Must be done after all the sys_reg reset.
153+
*/
154+
if (vcpu->arch.reset_state.reset) {
155+
unsigned long target_pc = vcpu->arch.reset_state.pc;
156+
157+
/* Gracefully handle Thumb2 entry point */
158+
if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
159+
target_pc &= ~1UL;
160+
vcpu_set_thumb(vcpu);
161+
}
162+
163+
/* Propagate caller endianness */
164+
if (vcpu->arch.reset_state.be)
165+
kvm_vcpu_set_be(vcpu);
166+
167+
*vcpu_pc(vcpu) = target_pc;
168+
vcpu_set_reg(vcpu, 0, vcpu->arch.reset_state.r0);
169+
170+
vcpu->arch.reset_state.reset = false;
171+
}
172+
132173
/* Reset PMU */
133174
kvm_pmu_vcpu_reset(vcpu);
134175

@@ -137,7 +178,12 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
137178
vcpu->arch.workaround_flags |= VCPU_WORKAROUND_2_FLAG;
138179

139180
/* Reset timer */
140-
return kvm_timer_vcpu_reset(vcpu);
181+
ret = kvm_timer_vcpu_reset(vcpu);
182+
out:
183+
if (loaded)
184+
kvm_arch_vcpu_load(vcpu, smp_processor_id());
185+
preempt_enable();
186+
return ret;
141187
}
142188

143189
void kvm_set_ipa_limit(void)

arch/arm64/kvm/sys_regs.c

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,29 @@ static bool trap_raz_wi(struct kvm_vcpu *vcpu,
314314
return read_zero(vcpu, p);
315315
}
316316

317-
static bool trap_undef(struct kvm_vcpu *vcpu,
318-
struct sys_reg_params *p,
319-
const struct sys_reg_desc *r)
317+
/*
318+
* ARMv8.1 mandates at least a trivial LORegion implementation, where all the
319+
* RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0
320+
* system, these registers should UNDEF. LORID_EL1 being a RO register, we
321+
* treat it separately.
322+
*/
323+
static bool trap_loregion(struct kvm_vcpu *vcpu,
324+
struct sys_reg_params *p,
325+
const struct sys_reg_desc *r)
320326
{
321-
kvm_inject_undefined(vcpu);
322-
return false;
327+
u64 val = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
328+
u32 sr = sys_reg((u32)r->Op0, (u32)r->Op1,
329+
(u32)r->CRn, (u32)r->CRm, (u32)r->Op2);
330+
331+
if (!(val & (0xfUL << ID_AA64MMFR1_LOR_SHIFT))) {
332+
kvm_inject_undefined(vcpu);
333+
return false;
334+
}
335+
336+
if (p->is_write && sr == SYS_LORID_EL1)
337+
return write_to_read_only(vcpu, p, r);
338+
339+
return trap_raz_wi(vcpu, p, r);
323340
}
324341

325342
static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
@@ -1048,11 +1065,6 @@ static u64 read_id_reg(struct sys_reg_desc const *r, bool raz)
10481065
if (val & ptrauth_mask)
10491066
kvm_debug("ptrauth unsupported for guests, suppressing\n");
10501067
val &= ~ptrauth_mask;
1051-
} else if (id == SYS_ID_AA64MMFR1_EL1) {
1052-
if (val & (0xfUL << ID_AA64MMFR1_LOR_SHIFT))
1053-
kvm_debug("LORegions unsupported for guests, suppressing\n");
1054-
1055-
val &= ~(0xfUL << ID_AA64MMFR1_LOR_SHIFT);
10561068
}
10571069

10581070
return val;
@@ -1338,11 +1350,11 @@ static const struct sys_reg_desc sys_reg_descs[] = {
13381350
{ SYS_DESC(SYS_MAIR_EL1), access_vm_reg, reset_unknown, MAIR_EL1 },
13391351
{ SYS_DESC(SYS_AMAIR_EL1), access_vm_reg, reset_amair_el1, AMAIR_EL1 },
13401352

1341-
{ SYS_DESC(SYS_LORSA_EL1), trap_undef },
1342-
{ SYS_DESC(SYS_LOREA_EL1), trap_undef },
1343-
{ SYS_DESC(SYS_LORN_EL1), trap_undef },
1344-
{ SYS_DESC(SYS_LORC_EL1), trap_undef },
1345-
{ SYS_DESC(SYS_LORID_EL1), trap_undef },
1353+
{ SYS_DESC(SYS_LORSA_EL1), trap_loregion },
1354+
{ SYS_DESC(SYS_LOREA_EL1), trap_loregion },
1355+
{ SYS_DESC(SYS_LORN_EL1), trap_loregion },
1356+
{ SYS_DESC(SYS_LORC_EL1), trap_loregion },
1357+
{ SYS_DESC(SYS_LORID_EL1), trap_loregion },
13461358

13471359
{ SYS_DESC(SYS_VBAR_EL1), NULL, reset_val, VBAR_EL1, 0 },
13481360
{ SYS_DESC(SYS_DISR_EL1), NULL, reset_val, DISR_EL1, 0 },
@@ -2596,7 +2608,9 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
25962608
table = get_target_table(vcpu->arch.target, true, &num);
25972609
reset_sys_reg_descs(vcpu, table, num);
25982610

2599-
for (num = 1; num < NR_SYS_REGS; num++)
2600-
if (__vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
2601-
panic("Didn't reset __vcpu_sys_reg(%zi)", num);
2611+
for (num = 1; num < NR_SYS_REGS; num++) {
2612+
if (WARN(__vcpu_sys_reg(vcpu, num) == 0x4242424242424242,
2613+
"Didn't reset __vcpu_sys_reg(%zi)\n", num))
2614+
break;
2615+
}
26022616
}

arch/x86/kvm/vmx/nested.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,10 @@ static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu,
24732473
(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id))
24742474
return -EINVAL;
24752475

2476+
if (!nested_cpu_has_preemption_timer(vmcs12) &&
2477+
nested_cpu_has_save_preemption_timer(vmcs12))
2478+
return -EINVAL;
2479+
24762480
if (nested_cpu_has_ept(vmcs12) &&
24772481
!valid_ept_address(vcpu, vmcs12->ept_pointer))
24782482
return -EINVAL;
@@ -5557,9 +5561,11 @@ void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps,
55575561
* secondary cpu-based controls. Do not include those that
55585562
* depend on CPUID bits, they are added later by vmx_cpuid_update.
55595563
*/
5560-
rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
5561-
msrs->secondary_ctls_low,
5562-
msrs->secondary_ctls_high);
5564+
if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)
5565+
rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
5566+
msrs->secondary_ctls_low,
5567+
msrs->secondary_ctls_high);
5568+
55635569
msrs->secondary_ctls_low = 0;
55645570
msrs->secondary_ctls_high &=
55655571
SECONDARY_EXEC_DESC |

0 commit comments

Comments
 (0)