xref: /linux/arch/loongarch/kvm/vm.c (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
4  */
5 
6 #include <linux/kvm_host.h>
7 #include <asm/kvm_mmu.h>
8 
9 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
10 	KVM_GENERIC_VM_STATS(),
11 	STATS_DESC_ICOUNTER(VM, pages),
12 	STATS_DESC_ICOUNTER(VM, hugepages),
13 };
14 
15 const struct kvm_stats_header kvm_vm_stats_header = {
16 	.name_size = KVM_STATS_NAME_SIZE,
17 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
18 	.id_offset =  sizeof(struct kvm_stats_header),
19 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
20 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
21 					sizeof(kvm_vm_stats_desc),
22 };
23 
24 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
25 {
26 	int i;
27 
28 	/* Allocate page table to map GPA -> RPA */
29 	kvm->arch.pgd = kvm_pgd_alloc();
30 	if (!kvm->arch.pgd)
31 		return -ENOMEM;
32 
33 	kvm->arch.phyid_map = kvzalloc(sizeof(struct kvm_phyid_map), GFP_KERNEL_ACCOUNT);
34 	if (!kvm->arch.phyid_map) {
35 		free_page((unsigned long)kvm->arch.pgd);
36 		kvm->arch.pgd = NULL;
37 		return -ENOMEM;
38 	}
39 	spin_lock_init(&kvm->arch.phyid_map_lock);
40 
41 	kvm_init_vmcs(kvm);
42 	kvm->arch.gpa_size = BIT(cpu_vabits - 1);
43 	kvm->arch.root_level = CONFIG_PGTABLE_LEVELS - 1;
44 	kvm->arch.invalid_ptes[0] = 0;
45 	kvm->arch.invalid_ptes[1] = (unsigned long)invalid_pte_table;
46 #if CONFIG_PGTABLE_LEVELS > 2
47 	kvm->arch.invalid_ptes[2] = (unsigned long)invalid_pmd_table;
48 #endif
49 #if CONFIG_PGTABLE_LEVELS > 3
50 	kvm->arch.invalid_ptes[3] = (unsigned long)invalid_pud_table;
51 #endif
52 	for (i = 0; i <= kvm->arch.root_level; i++)
53 		kvm->arch.pte_shifts[i] = PAGE_SHIFT + i * (PAGE_SHIFT - 3);
54 
55 	return 0;
56 }
57 
58 void kvm_arch_destroy_vm(struct kvm *kvm)
59 {
60 	kvm_destroy_vcpus(kvm);
61 	free_page((unsigned long)kvm->arch.pgd);
62 	kvm->arch.pgd = NULL;
63 	kvfree(kvm->arch.phyid_map);
64 	kvm->arch.phyid_map = NULL;
65 }
66 
67 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
68 {
69 	int r;
70 
71 	switch (ext) {
72 	case KVM_CAP_ONE_REG:
73 	case KVM_CAP_ENABLE_CAP:
74 	case KVM_CAP_READONLY_MEM:
75 	case KVM_CAP_SYNC_MMU:
76 	case KVM_CAP_IMMEDIATE_EXIT:
77 	case KVM_CAP_IOEVENTFD:
78 	case KVM_CAP_MP_STATE:
79 	case KVM_CAP_SET_GUEST_DEBUG:
80 		r = 1;
81 		break;
82 	case KVM_CAP_NR_VCPUS:
83 		r = num_online_cpus();
84 		break;
85 	case KVM_CAP_MAX_VCPUS:
86 		r = KVM_MAX_VCPUS;
87 		break;
88 	case KVM_CAP_MAX_VCPU_ID:
89 		r = KVM_MAX_VCPU_IDS;
90 		break;
91 	case KVM_CAP_NR_MEMSLOTS:
92 		r = KVM_USER_MEM_SLOTS;
93 		break;
94 	default:
95 		r = 0;
96 		break;
97 	}
98 
99 	return r;
100 }
101 
102 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
103 {
104 	return -ENOIOCTLCMD;
105 }
106