xref: /kvmtool/arm/kvm.c (revision 12c406a8019aecdd06be7670e44a6469b380a18f)
17c0e8b0cSWill Deacon #include "kvm/kvm.h"
27c0e8b0cSWill Deacon #include "kvm/term.h"
37c0e8b0cSWill Deacon #include "kvm/util.h"
497e65b5fSWill Deacon #include "kvm/8250-serial.h"
57c0e8b0cSWill Deacon #include "kvm/virtio-console.h"
67c0e8b0cSWill Deacon 
77c0e8b0cSWill Deacon #include "arm-common/gic.h"
87c0e8b0cSWill Deacon 
97c0e8b0cSWill Deacon #include <linux/kernel.h>
107c0e8b0cSWill Deacon #include <linux/kvm.h>
119eb3dbcbSWill Deacon #include <linux/sizes.h>
127c0e8b0cSWill Deacon 
137c0e8b0cSWill Deacon struct kvm_ext kvm_req_ext[] = {
147c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_IRQCHIP) },
157c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ONE_REG) },
1661076240SWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ARM_PSCI) },
177c0e8b0cSWill Deacon 	{ 0, 0 },
187c0e8b0cSWill Deacon };
197c0e8b0cSWill Deacon 
207c0e8b0cSWill Deacon bool kvm__arch_cpu_supports_vm(void)
217c0e8b0cSWill Deacon {
227c0e8b0cSWill Deacon 	/* The KVM capability check is enough. */
237c0e8b0cSWill Deacon 	return true;
247c0e8b0cSWill Deacon }
257c0e8b0cSWill Deacon 
267c0e8b0cSWill Deacon void kvm__init_ram(struct kvm *kvm)
277c0e8b0cSWill Deacon {
287c0e8b0cSWill Deacon 	int err;
297c0e8b0cSWill Deacon 	u64 phys_start, phys_size;
307c0e8b0cSWill Deacon 	void *host_mem;
317c0e8b0cSWill Deacon 
327c0e8b0cSWill Deacon 	phys_start	= ARM_MEMORY_AREA;
337c0e8b0cSWill Deacon 	phys_size	= kvm->ram_size;
347c0e8b0cSWill Deacon 	host_mem	= kvm->ram_start;
357c0e8b0cSWill Deacon 
367c0e8b0cSWill Deacon 	err = kvm__register_mem(kvm, phys_start, phys_size, host_mem);
377c0e8b0cSWill Deacon 	if (err)
387c0e8b0cSWill Deacon 		die("Failed to register %lld bytes of memory at physical "
397c0e8b0cSWill Deacon 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
407c0e8b0cSWill Deacon 
417c0e8b0cSWill Deacon 	kvm->arch.memory_guest_start = phys_start;
427c0e8b0cSWill Deacon }
437c0e8b0cSWill Deacon 
447c0e8b0cSWill Deacon void kvm__arch_delete_ram(struct kvm *kvm)
457c0e8b0cSWill Deacon {
469eb3dbcbSWill Deacon 	munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size);
477c0e8b0cSWill Deacon }
487c0e8b0cSWill Deacon 
49*12c406a8SJonathan Austin void kvm__arch_read_term(struct kvm *kvm)
507c0e8b0cSWill Deacon {
5197e65b5fSWill Deacon 	if (term_readable(0)) {
5297e65b5fSWill Deacon 		serial8250__update_consoles(kvm);
537c0e8b0cSWill Deacon 		virtio_console__inject_interrupt(kvm);
547c0e8b0cSWill Deacon 	}
5597e65b5fSWill Deacon }
567c0e8b0cSWill Deacon 
577c0e8b0cSWill Deacon void kvm__arch_set_cmdline(char *cmdline, bool video)
587c0e8b0cSWill Deacon {
597c0e8b0cSWill Deacon }
607c0e8b0cSWill Deacon 
617c0e8b0cSWill Deacon void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
627c0e8b0cSWill Deacon {
639eb3dbcbSWill Deacon 	/*
649eb3dbcbSWill Deacon 	 * Allocate guest memory. We must align out buffer to 64K to
659eb3dbcbSWill Deacon 	 * correlate with the maximum guest page size for virtio-mmio.
669eb3dbcbSWill Deacon 	 */
671e0c135aSWill Deacon 	kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm));
689eb3dbcbSWill Deacon 	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_64K;
699eb3dbcbSWill Deacon 	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path,
709eb3dbcbSWill Deacon 						kvm->arch.ram_alloc_size);
719eb3dbcbSWill Deacon 
729eb3dbcbSWill Deacon 	if (kvm->arch.ram_alloc_start == MAP_FAILED)
737c0e8b0cSWill Deacon 		die("Failed to map %lld bytes for guest memory (%d)",
749eb3dbcbSWill Deacon 		    kvm->arch.ram_alloc_size, errno);
759eb3dbcbSWill Deacon 
769eb3dbcbSWill Deacon 	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
779eb3dbcbSWill Deacon 					SZ_64K);
789eb3dbcbSWill Deacon 
799eb3dbcbSWill Deacon 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
809eb3dbcbSWill Deacon 		MADV_MERGEABLE);
817c0e8b0cSWill Deacon 
827c0e8b0cSWill Deacon 	/* Initialise the virtual GIC. */
837c0e8b0cSWill Deacon 	if (gic__init_irqchip(kvm))
847c0e8b0cSWill Deacon 		die("Failed to initialise virtual GIC");
857c0e8b0cSWill Deacon }
86