xref: /linux/arch/loongarch/kvm/vm.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
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_init_vmcs(kvm);
34 	kvm->arch.gpa_size = BIT(cpu_vabits - 1);
35 	kvm->arch.root_level = CONFIG_PGTABLE_LEVELS - 1;
36 	kvm->arch.invalid_ptes[0] = 0;
37 	kvm->arch.invalid_ptes[1] = (unsigned long)invalid_pte_table;
38 #if CONFIG_PGTABLE_LEVELS > 2
39 	kvm->arch.invalid_ptes[2] = (unsigned long)invalid_pmd_table;
40 #endif
41 #if CONFIG_PGTABLE_LEVELS > 3
42 	kvm->arch.invalid_ptes[3] = (unsigned long)invalid_pud_table;
43 #endif
44 	for (i = 0; i <= kvm->arch.root_level; i++)
45 		kvm->arch.pte_shifts[i] = PAGE_SHIFT + i * (PAGE_SHIFT - 3);
46 
47 	return 0;
48 }
49 
50 void kvm_arch_destroy_vm(struct kvm *kvm)
51 {
52 	kvm_destroy_vcpus(kvm);
53 	free_page((unsigned long)kvm->arch.pgd);
54 	kvm->arch.pgd = NULL;
55 }
56 
57 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
58 {
59 	int r;
60 
61 	switch (ext) {
62 	case KVM_CAP_ONE_REG:
63 	case KVM_CAP_ENABLE_CAP:
64 	case KVM_CAP_READONLY_MEM:
65 	case KVM_CAP_SYNC_MMU:
66 	case KVM_CAP_IMMEDIATE_EXIT:
67 	case KVM_CAP_IOEVENTFD:
68 	case KVM_CAP_MP_STATE:
69 		r = 1;
70 		break;
71 	case KVM_CAP_NR_VCPUS:
72 		r = num_online_cpus();
73 		break;
74 	case KVM_CAP_MAX_VCPUS:
75 		r = KVM_MAX_VCPUS;
76 		break;
77 	case KVM_CAP_MAX_VCPU_ID:
78 		r = KVM_MAX_VCPU_IDS;
79 		break;
80 	case KVM_CAP_NR_MEMSLOTS:
81 		r = KVM_USER_MEM_SLOTS;
82 		break;
83 	default:
84 		r = 0;
85 		break;
86 	}
87 
88 	return r;
89 }
90 
91 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
92 {
93 	return -ENOIOCTLCMD;
94 }
95