xref: /linux/arch/riscv/kvm/vm.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
4  *
5  * Authors:
6  *     Anup Patel <anup.patel@wdc.com>
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/kvm_host.h>
14 
15 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
16 	KVM_GENERIC_VM_STATS()
17 };
18 static_assert(ARRAY_SIZE(kvm_vm_stats_desc) ==
19 		sizeof(struct kvm_vm_stat) / sizeof(u64));
20 
21 const struct kvm_stats_header kvm_vm_stats_header = {
22 	.name_size = KVM_STATS_NAME_SIZE,
23 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
24 	.id_offset =  sizeof(struct kvm_stats_header),
25 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
26 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
27 		       sizeof(kvm_vm_stats_desc),
28 };
29 
30 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
31 {
32 	int r;
33 
34 	r = kvm_riscv_gstage_alloc_pgd(kvm);
35 	if (r)
36 		return r;
37 
38 	r = kvm_riscv_gstage_vmid_init(kvm);
39 	if (r) {
40 		kvm_riscv_gstage_free_pgd(kvm);
41 		return r;
42 	}
43 
44 	kvm_riscv_guest_timer_init(kvm);
45 
46 	return 0;
47 }
48 
49 void kvm_arch_destroy_vm(struct kvm *kvm)
50 {
51 	kvm_destroy_vcpus(kvm);
52 }
53 
54 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
55 {
56 	int r;
57 
58 	switch (ext) {
59 	case KVM_CAP_IOEVENTFD:
60 	case KVM_CAP_DEVICE_CTRL:
61 	case KVM_CAP_USER_MEMORY:
62 	case KVM_CAP_SYNC_MMU:
63 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
64 	case KVM_CAP_ONE_REG:
65 	case KVM_CAP_READONLY_MEM:
66 	case KVM_CAP_MP_STATE:
67 	case KVM_CAP_IMMEDIATE_EXIT:
68 		r = 1;
69 		break;
70 	case KVM_CAP_NR_VCPUS:
71 		r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
72 		break;
73 	case KVM_CAP_MAX_VCPUS:
74 		r = KVM_MAX_VCPUS;
75 		break;
76 	case KVM_CAP_NR_MEMSLOTS:
77 		r = KVM_USER_MEM_SLOTS;
78 		break;
79 	case KVM_CAP_VM_GPA_BITS:
80 		r = kvm_riscv_gstage_gpa_bits();
81 		break;
82 	default:
83 		r = 0;
84 		break;
85 	}
86 
87 	return r;
88 }
89 
90 long kvm_arch_vm_ioctl(struct file *filp,
91 		       unsigned int ioctl, unsigned long arg)
92 {
93 	return -EINVAL;
94 }
95