xref: /kvmtool/arm/kvm.c (revision 9eb3dbcbc85b98b4c8dc2c2275665fe6670967d1)
17c0e8b0cSWill Deacon #include "kvm/kvm.h"
27c0e8b0cSWill Deacon #include "kvm/term.h"
37c0e8b0cSWill Deacon #include "kvm/util.h"
47c0e8b0cSWill Deacon #include "kvm/virtio-console.h"
57c0e8b0cSWill Deacon 
67c0e8b0cSWill Deacon #include "arm-common/gic.h"
77c0e8b0cSWill Deacon 
87c0e8b0cSWill Deacon #include <linux/kernel.h>
97c0e8b0cSWill Deacon #include <linux/kvm.h>
10*9eb3dbcbSWill Deacon #include <linux/sizes.h>
117c0e8b0cSWill Deacon 
127c0e8b0cSWill Deacon struct kvm_ext kvm_req_ext[] = {
137c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_IRQCHIP) },
147c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ONE_REG) },
1561076240SWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ARM_PSCI) },
167c0e8b0cSWill Deacon 	{ 0, 0 },
177c0e8b0cSWill Deacon };
187c0e8b0cSWill Deacon 
197c0e8b0cSWill Deacon bool kvm__arch_cpu_supports_vm(void)
207c0e8b0cSWill Deacon {
217c0e8b0cSWill Deacon 	/* The KVM capability check is enough. */
227c0e8b0cSWill Deacon 	return true;
237c0e8b0cSWill Deacon }
247c0e8b0cSWill Deacon 
257c0e8b0cSWill Deacon void kvm__init_ram(struct kvm *kvm)
267c0e8b0cSWill Deacon {
277c0e8b0cSWill Deacon 	int err;
287c0e8b0cSWill Deacon 	u64 phys_start, phys_size;
297c0e8b0cSWill Deacon 	void *host_mem;
307c0e8b0cSWill Deacon 
317c0e8b0cSWill Deacon 	phys_start	= ARM_MEMORY_AREA;
327c0e8b0cSWill Deacon 	phys_size	= kvm->ram_size;
337c0e8b0cSWill Deacon 	host_mem	= kvm->ram_start;
347c0e8b0cSWill Deacon 
357c0e8b0cSWill Deacon 	err = kvm__register_mem(kvm, phys_start, phys_size, host_mem);
367c0e8b0cSWill Deacon 	if (err)
377c0e8b0cSWill Deacon 		die("Failed to register %lld bytes of memory at physical "
387c0e8b0cSWill Deacon 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
397c0e8b0cSWill Deacon 
407c0e8b0cSWill Deacon 	kvm->arch.memory_guest_start = phys_start;
417c0e8b0cSWill Deacon }
427c0e8b0cSWill Deacon 
437c0e8b0cSWill Deacon void kvm__arch_delete_ram(struct kvm *kvm)
447c0e8b0cSWill Deacon {
45*9eb3dbcbSWill Deacon 	munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size);
467c0e8b0cSWill Deacon }
477c0e8b0cSWill Deacon 
487c0e8b0cSWill Deacon void kvm__arch_periodic_poll(struct kvm *kvm)
497c0e8b0cSWill Deacon {
507c0e8b0cSWill Deacon 	if (term_readable(0))
517c0e8b0cSWill Deacon 		virtio_console__inject_interrupt(kvm);
527c0e8b0cSWill Deacon }
537c0e8b0cSWill Deacon 
547c0e8b0cSWill Deacon void kvm__arch_set_cmdline(char *cmdline, bool video)
557c0e8b0cSWill Deacon {
567c0e8b0cSWill Deacon }
577c0e8b0cSWill Deacon 
587c0e8b0cSWill Deacon void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
597c0e8b0cSWill Deacon {
60*9eb3dbcbSWill Deacon 	/*
61*9eb3dbcbSWill Deacon 	 * Allocate guest memory. We must align out buffer to 64K to
62*9eb3dbcbSWill Deacon 	 * correlate with the maximum guest page size for virtio-mmio.
63*9eb3dbcbSWill Deacon 	 */
641e0c135aSWill Deacon 	kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm));
65*9eb3dbcbSWill Deacon 	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_64K;
66*9eb3dbcbSWill Deacon 	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path,
67*9eb3dbcbSWill Deacon 						kvm->arch.ram_alloc_size);
68*9eb3dbcbSWill Deacon 
69*9eb3dbcbSWill Deacon 	if (kvm->arch.ram_alloc_start == MAP_FAILED)
707c0e8b0cSWill Deacon 		die("Failed to map %lld bytes for guest memory (%d)",
71*9eb3dbcbSWill Deacon 		    kvm->arch.ram_alloc_size, errno);
72*9eb3dbcbSWill Deacon 
73*9eb3dbcbSWill Deacon 	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
74*9eb3dbcbSWill Deacon 					SZ_64K);
75*9eb3dbcbSWill Deacon 
76*9eb3dbcbSWill Deacon 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
77*9eb3dbcbSWill Deacon 		MADV_MERGEABLE);
787c0e8b0cSWill Deacon 
797c0e8b0cSWill Deacon 	/* Initialise the virtual GIC. */
807c0e8b0cSWill Deacon 	if (gic__init_irqchip(kvm))
817c0e8b0cSWill Deacon 		die("Failed to initialise virtual GIC");
827c0e8b0cSWill Deacon }
83