xref: /openbmc/linux/arch/riscv/kvm/vm.c (revision c4a11bf4)
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_stage2_alloc_pgd(kvm);
35 	if (r)
36 		return r;
37 
38 	r = kvm_riscv_stage2_vmid_init(kvm);
39 	if (r) {
40 		kvm_riscv_stage2_free_pgd(kvm);
41 		return r;
42 	}
43 
44 	return kvm_riscv_guest_timer_init(kvm);
45 }
46 
47 void kvm_arch_destroy_vm(struct kvm *kvm)
48 {
49 	int i;
50 
51 	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
52 		if (kvm->vcpus[i]) {
53 			kvm_vcpu_destroy(kvm->vcpus[i]);
54 			kvm->vcpus[i] = NULL;
55 		}
56 	}
57 	atomic_set(&kvm->online_vcpus, 0);
58 }
59 
60 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
61 {
62 	int r;
63 
64 	switch (ext) {
65 	case KVM_CAP_IOEVENTFD:
66 	case KVM_CAP_DEVICE_CTRL:
67 	case KVM_CAP_USER_MEMORY:
68 	case KVM_CAP_SYNC_MMU:
69 	case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
70 	case KVM_CAP_ONE_REG:
71 	case KVM_CAP_READONLY_MEM:
72 	case KVM_CAP_MP_STATE:
73 	case KVM_CAP_IMMEDIATE_EXIT:
74 		r = 1;
75 		break;
76 	case KVM_CAP_NR_VCPUS:
77 		r = num_online_cpus();
78 		break;
79 	case KVM_CAP_MAX_VCPUS:
80 		r = KVM_MAX_VCPUS;
81 		break;
82 	case KVM_CAP_NR_MEMSLOTS:
83 		r = KVM_USER_MEM_SLOTS;
84 		break;
85 	default:
86 		r = 0;
87 		break;
88 	}
89 
90 	return r;
91 }
92 
93 long kvm_arch_vm_ioctl(struct file *filp,
94 		       unsigned int ioctl, unsigned long arg)
95 {
96 	return -EINVAL;
97 }
98