Skip to content

Commit 56d8a38

Browse files
committed
RISC-V: KVM: Allow some SBI extensions to be disabled by default
Currently, all SBI extensions are enabled by default which is problematic for SBI extensions (such as DBCN) which are forwarded to the KVM user-space because we might have an older KVM user-space which is not aware/ready to handle newer SBI extensions. Ideally, the SBI extensions forwarded to the KVM user-space must be disabled by default. To address above, we allow certain SBI extensions to be disabled by default so that KVM user-space must explicitly enable such SBI extensions to receive forwarded calls from Guest VCPU. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Anup Patel <[email protected]>
1 parent b88e87a commit 56d8a38

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

arch/riscv/include/asm/kvm_vcpu_sbi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct kvm_vcpu_sbi_return {
3535
struct kvm_vcpu_sbi_extension {
3636
unsigned long extid_start;
3737
unsigned long extid_end;
38+
39+
bool default_unavail;
40+
3841
/**
3942
* SBI extension handler. It can be defined for a given extension or group of
4043
* extension. But it should always return linux error codes rather than SBI
@@ -59,6 +62,7 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,
5962
const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
6063
struct kvm_vcpu *vcpu, unsigned long extid);
6164
int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
65+
void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu);
6266

6367
#ifdef CONFIG_RISCV_SBI_V01
6468
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;

arch/riscv/kvm/vcpu.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
141141
if (rc)
142142
return rc;
143143

144+
/*
145+
* Setup SBI extensions
146+
* NOTE: This must be the last thing to be initialized.
147+
*/
148+
kvm_riscv_vcpu_sbi_init(vcpu);
149+
144150
/* Reset VCPU */
145151
kvm_riscv_reset_vcpu(vcpu);
146152

arch/riscv/kvm/vcpu_sbi.c

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,8 @@ static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu,
155155
if (!sext)
156156
return -ENOENT;
157157

158-
/*
159-
* We can't set the extension status to available here, since it may
160-
* have a probe() function which needs to confirm availability first,
161-
* but it may be too early to call that here. We can set the status to
162-
* unavailable, though.
163-
*/
164-
if (!reg_val)
165-
scontext->ext_status[sext->ext_idx] =
158+
scontext->ext_status[sext->ext_idx] = (reg_val) ?
159+
KVM_RISCV_SBI_EXT_AVAILABLE :
166160
KVM_RISCV_SBI_EXT_UNAVAILABLE;
167161

168162
return 0;
@@ -188,16 +182,8 @@ static int riscv_vcpu_get_sbi_ext_single(struct kvm_vcpu *vcpu,
188182
if (!sext)
189183
return -ENOENT;
190184

191-
/*
192-
* If the extension status is still uninitialized, then we should probe
193-
* to determine if it's available, but it may be too early to do that
194-
* here. The best we can do is report that the extension has not been
195-
* disabled, i.e. we return 1 when the extension is available and also
196-
* when it only may be available.
197-
*/
198-
*reg_val = scontext->ext_status[sext->ext_idx] !=
199-
KVM_RISCV_SBI_EXT_UNAVAILABLE;
200-
185+
*reg_val = scontext->ext_status[sext->ext_idx] ==
186+
KVM_RISCV_SBI_EXT_AVAILABLE;
201187
return 0;
202188
}
203189

@@ -337,18 +323,8 @@ const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(
337323
scontext->ext_status[entry->ext_idx] ==
338324
KVM_RISCV_SBI_EXT_AVAILABLE)
339325
return ext;
340-
if (scontext->ext_status[entry->ext_idx] ==
341-
KVM_RISCV_SBI_EXT_UNAVAILABLE)
342-
return NULL;
343-
if (ext->probe && !ext->probe(vcpu)) {
344-
scontext->ext_status[entry->ext_idx] =
345-
KVM_RISCV_SBI_EXT_UNAVAILABLE;
346-
return NULL;
347-
}
348326

349-
scontext->ext_status[entry->ext_idx] =
350-
KVM_RISCV_SBI_EXT_AVAILABLE;
351-
return ext;
327+
return NULL;
352328
}
353329
}
354330

@@ -419,3 +395,26 @@ int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run)
419395

420396
return ret;
421397
}
398+
399+
void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu)
400+
{
401+
struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
402+
const struct kvm_riscv_sbi_extension_entry *entry;
403+
const struct kvm_vcpu_sbi_extension *ext;
404+
int i;
405+
406+
for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
407+
entry = &sbi_ext[i];
408+
ext = entry->ext_ptr;
409+
410+
if (ext->probe && !ext->probe(vcpu)) {
411+
scontext->ext_status[entry->ext_idx] =
412+
KVM_RISCV_SBI_EXT_UNAVAILABLE;
413+
continue;
414+
}
415+
416+
scontext->ext_status[entry->ext_idx] = ext->default_unavail ?
417+
KVM_RISCV_SBI_EXT_UNAVAILABLE :
418+
KVM_RISCV_SBI_EXT_AVAILABLE;
419+
}
420+
}

0 commit comments

Comments
 (0)