Skip to content

Commit 5294afd

Browse files
author
Marc Zyngier
committed
KVM: arm64: Exclude FP ownership from kvm_vcpu_arch
In retrospect, it is fairly obvious that the FP state ownership is only meaningful for a given CPU, and that locating this information in the vcpu was just a mistake. Move the ownership tracking into the host data structure, and rename it from fp_state to fp_owner, which is a better description (name suggested by Mark Brown). Reviewed-by: Mark Brown <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent 51e09b5 commit 5294afd

File tree

8 files changed

+19
-27
lines changed

8 files changed

+19
-27
lines changed

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,15 +588,15 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu)
588588
val = (CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN);
589589

590590
if (!vcpu_has_sve(vcpu) ||
591-
(vcpu->arch.fp_state != FP_STATE_GUEST_OWNED))
591+
(*host_data_ptr(fp_owner) != FP_STATE_GUEST_OWNED))
592592
val |= CPACR_EL1_ZEN_EL1EN | CPACR_EL1_ZEN_EL0EN;
593593
if (cpus_have_final_cap(ARM64_SME))
594594
val |= CPACR_EL1_SMEN_EL1EN | CPACR_EL1_SMEN_EL0EN;
595595
} else {
596596
val = CPTR_NVHE_EL2_RES1;
597597

598598
if (vcpu_has_sve(vcpu) &&
599-
(vcpu->arch.fp_state == FP_STATE_GUEST_OWNED))
599+
(*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED))
600600
val |= CPTR_EL2_TZ;
601601
if (cpus_have_final_cap(ARM64_SME))
602602
val &= ~CPTR_EL2_TSM;

arch/arm64/include/asm/kvm_host.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ struct kvm_host_data {
545545
struct kvm_cpu_context host_ctxt;
546546
struct user_fpsimd_state *fpsimd_state; /* hyp VA */
547547

548+
/* Ownership of the FP regs */
549+
enum {
550+
FP_STATE_FREE,
551+
FP_STATE_HOST_OWNED,
552+
FP_STATE_GUEST_OWNED,
553+
} fp_owner;
554+
548555
/*
549556
* host_debug_state contains the host registers which are
550557
* saved and restored during world switches.
@@ -622,13 +629,6 @@ struct kvm_vcpu_arch {
622629
/* Exception Information */
623630
struct kvm_vcpu_fault_info fault;
624631

625-
/* Ownership of the FP regs */
626-
enum {
627-
FP_STATE_FREE,
628-
FP_STATE_HOST_OWNED,
629-
FP_STATE_GUEST_OWNED,
630-
} fp_state;
631-
632632
/* Configuration flags, set once and for all before the vcpu can run */
633633
u8 cflags;
634634

arch/arm64/kvm/arm.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,6 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
378378

379379
vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO;
380380

381-
/*
382-
* Default value for the FP state, will be overloaded at load
383-
* time if we support FP (pretty likely)
384-
*/
385-
vcpu->arch.fp_state = FP_STATE_FREE;
386-
387381
/* Set up the timer */
388382
kvm_timer_vcpu_init(vcpu);
389383

arch/arm64/kvm/fpsimd.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
8484
* guest in kvm_arch_vcpu_ctxflush_fp() and override this to
8585
* FP_STATE_FREE if the flag set.
8686
*/
87-
vcpu->arch.fp_state = FP_STATE_HOST_OWNED;
87+
*host_data_ptr(fp_owner) = FP_STATE_HOST_OWNED;
8888
*host_data_ptr(fpsimd_state) = kern_hyp_va(&current->thread.uw.fpsimd_state);
8989

9090
vcpu_clear_flag(vcpu, HOST_SVE_ENABLED);
@@ -109,7 +109,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
109109
* been saved, this is very unlikely to happen.
110110
*/
111111
if (read_sysreg_s(SYS_SVCR) & (SVCR_SM_MASK | SVCR_ZA_MASK)) {
112-
vcpu->arch.fp_state = FP_STATE_FREE;
112+
*host_data_ptr(fp_owner) = FP_STATE_FREE;
113113
fpsimd_save_and_flush_cpu_state();
114114
}
115115
}
@@ -125,7 +125,7 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu)
125125
void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu)
126126
{
127127
if (test_thread_flag(TIF_FOREIGN_FPSTATE))
128-
vcpu->arch.fp_state = FP_STATE_FREE;
128+
*host_data_ptr(fp_owner) = FP_STATE_FREE;
129129
}
130130

131131
/*
@@ -141,7 +141,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)
141141

142142
WARN_ON_ONCE(!irqs_disabled());
143143

144-
if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
144+
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
145145

146146
/*
147147
* Currently we do not support SME guests so SVCR is
@@ -195,7 +195,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)
195195
isb();
196196
}
197197

198-
if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED) {
198+
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED) {
199199
if (vcpu_has_sve(vcpu)) {
200200
__vcpu_sys_reg(vcpu, ZCR_EL1) = read_sysreg_el1(SYS_ZCR);
201201

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern struct kvm_exception_table_entry __stop___kvm_ex_table;
4242
/* Check whether the FP regs are owned by the guest */
4343
static inline bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
4444
{
45-
return vcpu->arch.fp_state == FP_STATE_GUEST_OWNED;
45+
return *host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED;
4646
}
4747

4848
/* Save the 32-bit only FPSIMD system register state */
@@ -376,7 +376,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
376376
isb();
377377

378378
/* Write out the host state if it's in the registers */
379-
if (vcpu->arch.fp_state == FP_STATE_HOST_OWNED)
379+
if (*host_data_ptr(fp_owner) == FP_STATE_HOST_OWNED)
380380
__fpsimd_save_state(*host_data_ptr(fpsimd_state));
381381

382382
/* Restore the guest state */
@@ -389,7 +389,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
389389
if (!(read_sysreg(hcr_el2) & HCR_RW))
390390
write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
391391

392-
vcpu->arch.fp_state = FP_STATE_GUEST_OWNED;
392+
*host_data_ptr(fp_owner) = FP_STATE_GUEST_OWNED;
393393

394394
return true;
395395
}

arch/arm64/kvm/hyp/nvhe/hyp-main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
3939
hyp_vcpu->vcpu.arch.cptr_el2 = host_vcpu->arch.cptr_el2;
4040

4141
hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags;
42-
hyp_vcpu->vcpu.arch.fp_state = host_vcpu->arch.fp_state;
4342

4443
hyp_vcpu->vcpu.arch.debug_ptr = kern_hyp_va(host_vcpu->arch.debug_ptr);
4544

@@ -63,7 +62,6 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
6362
host_vcpu->arch.fault = hyp_vcpu->vcpu.arch.fault;
6463

6564
host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags;
66-
host_vcpu->arch.fp_state = hyp_vcpu->vcpu.arch.fp_state;
6765

6866
host_cpu_if->vgic_hcr = hyp_cpu_if->vgic_hcr;
6967
for (i = 0; i < hyp_cpu_if->used_lrs; ++i)

arch/arm64/kvm/hyp/nvhe/switch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
337337

338338
__sysreg_restore_state_nvhe(host_ctxt);
339339

340-
if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
340+
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
341341
__fpsimd_save_fpexc32(vcpu);
342342

343343
__debug_switch_to_host(vcpu);

arch/arm64/kvm/hyp/vhe/switch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)
258258

259259
sysreg_restore_host_state_vhe(host_ctxt);
260260

261-
if (vcpu->arch.fp_state == FP_STATE_GUEST_OWNED)
261+
if (*host_data_ptr(fp_owner) == FP_STATE_GUEST_OWNED)
262262
__fpsimd_save_fpexc32(vcpu);
263263

264264
__debug_switch_to_host(vcpu);

0 commit comments

Comments
 (0)