Skip to content

Commit 7a5189c

Browse files
committed
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull RISC-V kvm updates from Paolo Bonzini: - Allow unloading KVM module - Allow KVM user-space to set mvendorid, marchid, and mimpid - Several fixes and cleanups * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: RISC-V: KVM: Add ONE_REG interface for mvendorid, marchid, and mimpid RISC-V: KVM: Save mvendorid, marchid, and mimpid when creating VCPU RISC-V: Export sbi_get_mvendorid() and friends RISC-V: KVM: Move sbi related struct and functions to kvm_vcpu_sbi.h RISC-V: KVM: Use switch-case in kvm_riscv_vcpu_set/get_reg() RISC-V: KVM: Remove redundant includes of asm/csr.h RISC-V: KVM: Remove redundant includes of asm/kvm_vcpu_timer.h RISC-V: KVM: Fix reg_val check in kvm_riscv_vcpu_set_reg_config() RISC-V: KVM: Simplify kvm_arch_prepare_memory_region() RISC-V: KVM: Exit run-loop immediately if xfer_to_guest fails RISC-V: KVM: use vma_lookup() instead of find_vma_intersection() RISC-V: KVM: Add exit logic to main.c
2 parents 569c3a2 + 6ebbdec commit 7a5189c

File tree

11 files changed

+97
-44
lines changed

11 files changed

+97
-44
lines changed

arch/riscv/include/asm/kvm_host.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#include <linux/kvm.h>
1414
#include <linux/kvm_types.h>
1515
#include <linux/spinlock.h>
16-
#include <asm/csr.h>
1716
#include <asm/hwcap.h>
1817
#include <asm/kvm_vcpu_fp.h>
1918
#include <asm/kvm_vcpu_insn.h>
19+
#include <asm/kvm_vcpu_sbi.h>
2020
#include <asm/kvm_vcpu_timer.h>
2121

2222
#define KVM_MAX_VCPUS 1024
@@ -95,10 +95,6 @@ struct kvm_arch {
9595
struct kvm_guest_timer timer;
9696
};
9797

98-
struct kvm_sbi_context {
99-
int return_handled;
100-
};
101-
10298
struct kvm_cpu_trap {
10399
unsigned long sepc;
104100
unsigned long scause;
@@ -169,6 +165,11 @@ struct kvm_vcpu_arch {
169165
/* ISA feature bits (similar to MISA) */
170166
DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX);
171167

168+
/* Vendor, Arch, and Implementation details */
169+
unsigned long mvendorid;
170+
unsigned long marchid;
171+
unsigned long mimpid;
172+
172173
/* SSCRATCH, STVEC, and SCOUNTEREN of Host */
173174
unsigned long host_sscratch;
174175
unsigned long host_stvec;
@@ -217,7 +218,7 @@ struct kvm_vcpu_arch {
217218
struct kvm_csr_decode csr_decode;
218219

219220
/* SBI context */
220-
struct kvm_sbi_context sbi_context;
221+
struct kvm_vcpu_sbi_context sbi_context;
221222

222223
/* Cache pages needed to program page tables with spinlock held */
223224
struct kvm_mmu_memory_cache mmu_page_cache;
@@ -327,7 +328,4 @@ bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, unsigned long mask);
327328
void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu);
328329
void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu);
329330

330-
int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run);
331-
int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
332-
333331
#endif /* __RISCV_KVM_HOST_H__ */

arch/riscv/include/asm/kvm_vcpu_sbi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#define KVM_SBI_VERSION_MAJOR 1
1515
#define KVM_SBI_VERSION_MINOR 0
1616

17+
struct kvm_vcpu_sbi_context {
18+
int return_handled;
19+
};
20+
1721
struct kvm_vcpu_sbi_extension {
1822
unsigned long extid_start;
1923
unsigned long extid_end;
@@ -31,7 +35,9 @@ void kvm_riscv_vcpu_sbi_forward(struct kvm_vcpu *vcpu, struct kvm_run *run);
3135
void kvm_riscv_vcpu_sbi_system_reset(struct kvm_vcpu *vcpu,
3236
struct kvm_run *run,
3337
u32 type, u64 flags);
38+
int kvm_riscv_vcpu_sbi_return(struct kvm_vcpu *vcpu, struct kvm_run *run);
3439
const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext(unsigned long extid);
40+
int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
3541

3642
#ifdef CONFIG_RISCV_SBI_V01
3743
extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;

arch/riscv/include/uapi/asm/kvm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct kvm_sregs {
4949
struct kvm_riscv_config {
5050
unsigned long isa;
5151
unsigned long zicbom_block_size;
52+
unsigned long mvendorid;
53+
unsigned long marchid;
54+
unsigned long mimpid;
5255
};
5356

5457
/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */

arch/riscv/kernel/sbi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,16 +627,19 @@ long sbi_get_mvendorid(void)
627627
{
628628
return __sbi_base_ecall(SBI_EXT_BASE_GET_MVENDORID);
629629
}
630+
EXPORT_SYMBOL_GPL(sbi_get_mvendorid);
630631

631632
long sbi_get_marchid(void)
632633
{
633634
return __sbi_base_ecall(SBI_EXT_BASE_GET_MARCHID);
634635
}
636+
EXPORT_SYMBOL_GPL(sbi_get_marchid);
635637

636638
long sbi_get_mimpid(void)
637639
{
638640
return __sbi_base_ecall(SBI_EXT_BASE_GET_MIMPID);
639641
}
642+
EXPORT_SYMBOL_GPL(sbi_get_mimpid);
640643

641644
static void sbi_send_cpumask_ipi(const struct cpumask *target)
642645
{

arch/riscv/kvm/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,9 @@ static int __init riscv_kvm_init(void)
127127
return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
128128
}
129129
module_init(riscv_kvm_init);
130+
131+
static void __exit riscv_kvm_exit(void)
132+
{
133+
kvm_exit();
134+
}
135+
module_exit(riscv_kvm_exit);

arch/riscv/kvm/mmu.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
537537
if (change == KVM_MR_FLAGS_ONLY)
538538
goto out;
539539

540-
spin_lock(&kvm->mmu_lock);
541540
if (ret)
542-
gstage_unmap_range(kvm, base_gpa, size, false);
543-
spin_unlock(&kvm->mmu_lock);
541+
kvm_riscv_gstage_iounmap(kvm, base_gpa, size);
544542

545543
out:
546544
mmap_read_unlock(current->mm);
@@ -632,7 +630,7 @@ int kvm_riscv_gstage_map(struct kvm_vcpu *vcpu,
632630

633631
mmap_read_lock(current->mm);
634632

635-
vma = find_vma_intersection(current->mm, hva, hva + 1);
633+
vma = vma_lookup(current->mm, hva);
636634
if (unlikely(!vma)) {
637635
kvm_err("Failed to find VMA for hva 0x%lx\n", hva);
638636
mmap_read_unlock(current->mm);

arch/riscv/kvm/vcpu.c

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <asm/csr.h>
2222
#include <asm/cacheflush.h>
2323
#include <asm/hwcap.h>
24+
#include <asm/sbi.h>
2425

2526
const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
2627
KVM_GENERIC_VCPU_STATS(),
@@ -171,6 +172,11 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
171172
set_bit(host_isa, vcpu->arch.isa);
172173
}
173174

175+
/* Setup vendor, arch, and implementation details */
176+
vcpu->arch.mvendorid = sbi_get_mvendorid();
177+
vcpu->arch.marchid = sbi_get_marchid();
178+
vcpu->arch.mimpid = sbi_get_mimpid();
179+
174180
/* Setup VCPU hfence queue */
175181
spin_lock_init(&vcpu->arch.hfence_lock);
176182

@@ -270,6 +276,15 @@ static int kvm_riscv_vcpu_get_reg_config(struct kvm_vcpu *vcpu,
270276
return -EINVAL;
271277
reg_val = riscv_cbom_block_size;
272278
break;
279+
case KVM_REG_RISCV_CONFIG_REG(mvendorid):
280+
reg_val = vcpu->arch.mvendorid;
281+
break;
282+
case KVM_REG_RISCV_CONFIG_REG(marchid):
283+
reg_val = vcpu->arch.marchid;
284+
break;
285+
case KVM_REG_RISCV_CONFIG_REG(mimpid):
286+
reg_val = vcpu->arch.mimpid;
287+
break;
273288
default:
274289
return -EINVAL;
275290
}
@@ -296,12 +311,15 @@ static int kvm_riscv_vcpu_set_reg_config(struct kvm_vcpu *vcpu,
296311
if (copy_from_user(&reg_val, uaddr, KVM_REG_SIZE(reg->id)))
297312
return -EFAULT;
298313

299-
/* This ONE REG interface is only defined for single letter extensions */
300-
if (fls(reg_val) >= RISCV_ISA_EXT_BASE)
301-
return -EINVAL;
302-
303314
switch (reg_num) {
304315
case KVM_REG_RISCV_CONFIG_REG(isa):
316+
/*
317+
* This ONE REG interface is only defined for
318+
* single letter extensions.
319+
*/
320+
if (fls(reg_val) >= RISCV_ISA_EXT_BASE)
321+
return -EINVAL;
322+
305323
if (!vcpu->arch.ran_atleast_once) {
306324
/* Ignore the enable/disable request for certain extensions */
307325
for (i = 0; i < RISCV_ISA_EXT_BASE; i++) {
@@ -329,6 +347,24 @@ static int kvm_riscv_vcpu_set_reg_config(struct kvm_vcpu *vcpu,
329347
break;
330348
case KVM_REG_RISCV_CONFIG_REG(zicbom_block_size):
331349
return -EOPNOTSUPP;
350+
case KVM_REG_RISCV_CONFIG_REG(mvendorid):
351+
if (!vcpu->arch.ran_atleast_once)
352+
vcpu->arch.mvendorid = reg_val;
353+
else
354+
return -EBUSY;
355+
break;
356+
case KVM_REG_RISCV_CONFIG_REG(marchid):
357+
if (!vcpu->arch.ran_atleast_once)
358+
vcpu->arch.marchid = reg_val;
359+
else
360+
return -EBUSY;
361+
break;
362+
case KVM_REG_RISCV_CONFIG_REG(mimpid):
363+
if (!vcpu->arch.ran_atleast_once)
364+
vcpu->arch.mimpid = reg_val;
365+
else
366+
return -EBUSY;
367+
break;
332368
default:
333369
return -EINVAL;
334370
}
@@ -541,45 +577,53 @@ static int kvm_riscv_vcpu_set_reg_isa_ext(struct kvm_vcpu *vcpu,
541577
static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu,
542578
const struct kvm_one_reg *reg)
543579
{
544-
if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG)
580+
switch (reg->id & KVM_REG_RISCV_TYPE_MASK) {
581+
case KVM_REG_RISCV_CONFIG:
545582
return kvm_riscv_vcpu_set_reg_config(vcpu, reg);
546-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE)
583+
case KVM_REG_RISCV_CORE:
547584
return kvm_riscv_vcpu_set_reg_core(vcpu, reg);
548-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR)
585+
case KVM_REG_RISCV_CSR:
549586
return kvm_riscv_vcpu_set_reg_csr(vcpu, reg);
550-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_TIMER)
587+
case KVM_REG_RISCV_TIMER:
551588
return kvm_riscv_vcpu_set_reg_timer(vcpu, reg);
552-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_F)
589+
case KVM_REG_RISCV_FP_F:
553590
return kvm_riscv_vcpu_set_reg_fp(vcpu, reg,
554591
KVM_REG_RISCV_FP_F);
555-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D)
592+
case KVM_REG_RISCV_FP_D:
556593
return kvm_riscv_vcpu_set_reg_fp(vcpu, reg,
557594
KVM_REG_RISCV_FP_D);
558-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT)
595+
case KVM_REG_RISCV_ISA_EXT:
559596
return kvm_riscv_vcpu_set_reg_isa_ext(vcpu, reg);
597+
default:
598+
break;
599+
}
560600

561601
return -EINVAL;
562602
}
563603

564604
static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu,
565605
const struct kvm_one_reg *reg)
566606
{
567-
if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG)
607+
switch (reg->id & KVM_REG_RISCV_TYPE_MASK) {
608+
case KVM_REG_RISCV_CONFIG:
568609
return kvm_riscv_vcpu_get_reg_config(vcpu, reg);
569-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE)
610+
case KVM_REG_RISCV_CORE:
570611
return kvm_riscv_vcpu_get_reg_core(vcpu, reg);
571-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR)
612+
case KVM_REG_RISCV_CSR:
572613
return kvm_riscv_vcpu_get_reg_csr(vcpu, reg);
573-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_TIMER)
614+
case KVM_REG_RISCV_TIMER:
574615
return kvm_riscv_vcpu_get_reg_timer(vcpu, reg);
575-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_F)
616+
case KVM_REG_RISCV_FP_F:
576617
return kvm_riscv_vcpu_get_reg_fp(vcpu, reg,
577618
KVM_REG_RISCV_FP_F);
578-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_FP_D)
619+
case KVM_REG_RISCV_FP_D:
579620
return kvm_riscv_vcpu_get_reg_fp(vcpu, reg,
580621
KVM_REG_RISCV_FP_D);
581-
else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_ISA_EXT)
622+
case KVM_REG_RISCV_ISA_EXT:
582623
return kvm_riscv_vcpu_get_reg_isa_ext(vcpu, reg);
624+
default:
625+
break;
626+
}
583627

584628
return -EINVAL;
585629
}
@@ -984,8 +1028,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
9841028
while (ret > 0) {
9851029
/* Check conditions before entering the guest */
9861030
ret = xfer_to_guest_mode_handle_work(vcpu);
987-
if (!ret)
988-
ret = 1;
1031+
if (ret)
1032+
continue;
1033+
ret = 1;
9891034

9901035
kvm_riscv_gstage_vmid_update(vcpu);
9911036

arch/riscv/kvm/vcpu_sbi_base.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
#include <linux/err.h>
1111
#include <linux/kvm_host.h>
1212
#include <linux/version.h>
13-
#include <asm/csr.h>
1413
#include <asm/sbi.h>
15-
#include <asm/kvm_vcpu_timer.h>
1614
#include <asm/kvm_vcpu_sbi.h>
1715

1816
static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
@@ -21,7 +19,6 @@ static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
2119
{
2220
int ret = 0;
2321
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
24-
struct sbiret ecall_ret;
2522

2623
switch (cp->a6) {
2724
case SBI_EXT_BASE_GET_SPEC_VERSION:
@@ -50,13 +47,13 @@ static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
5047
*out_val = kvm_vcpu_sbi_find_ext(cp->a0) ? 1 : 0;
5148
break;
5249
case SBI_EXT_BASE_GET_MVENDORID:
50+
*out_val = vcpu->arch.mvendorid;
51+
break;
5352
case SBI_EXT_BASE_GET_MARCHID:
53+
*out_val = vcpu->arch.marchid;
54+
break;
5455
case SBI_EXT_BASE_GET_MIMPID:
55-
ecall_ret = sbi_ecall(SBI_EXT_BASE, cp->a6, 0, 0, 0, 0, 0, 0);
56-
if (!ecall_ret.error)
57-
*out_val = ecall_ret.value;
58-
/*TODO: We are unnecessarily converting the error twice */
59-
ret = sbi_err_map_linux_errno(ecall_ret.error);
56+
*out_val = vcpu->arch.mimpid;
6057
break;
6158
default:
6259
ret = -EOPNOTSUPP;

arch/riscv/kvm/vcpu_sbi_hsm.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <linux/errno.h>
1010
#include <linux/err.h>
1111
#include <linux/kvm_host.h>
12-
#include <asm/csr.h>
1312
#include <asm/sbi.h>
1413
#include <asm/kvm_vcpu_sbi.h>
1514

arch/riscv/kvm/vcpu_sbi_replace.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <linux/errno.h>
1010
#include <linux/err.h>
1111
#include <linux/kvm_host.h>
12-
#include <asm/csr.h>
1312
#include <asm/sbi.h>
1413
#include <asm/kvm_vcpu_timer.h>
1514
#include <asm/kvm_vcpu_sbi.h>

0 commit comments

Comments
 (0)