xref: /kvmtool/arm/kvm.c (revision 0093df80d754e1a05b016e5a4ccd4b51a00c562c)
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"
63c8aec9eSAndre Przywara #include "kvm/fdt.h"
77c0e8b0cSWill Deacon 
87c0e8b0cSWill Deacon #include "arm-common/gic.h"
97c0e8b0cSWill Deacon 
107c0e8b0cSWill Deacon #include <linux/kernel.h>
117c0e8b0cSWill Deacon #include <linux/kvm.h>
129eb3dbcbSWill Deacon #include <linux/sizes.h>
137c0e8b0cSWill Deacon 
147c0e8b0cSWill Deacon struct kvm_ext kvm_req_ext[] = {
157c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_IRQCHIP) },
167c0e8b0cSWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ONE_REG) },
1761076240SWill Deacon 	{ DEFINE_KVM_EXT(KVM_CAP_ARM_PSCI) },
187c0e8b0cSWill Deacon 	{ 0, 0 },
197c0e8b0cSWill Deacon };
207c0e8b0cSWill Deacon 
217c0e8b0cSWill Deacon bool kvm__arch_cpu_supports_vm(void)
227c0e8b0cSWill Deacon {
237c0e8b0cSWill Deacon 	/* The KVM capability check is enough. */
247c0e8b0cSWill Deacon 	return true;
257c0e8b0cSWill Deacon }
267c0e8b0cSWill Deacon 
277c0e8b0cSWill Deacon void kvm__init_ram(struct kvm *kvm)
287c0e8b0cSWill Deacon {
297c0e8b0cSWill Deacon 	int err;
307c0e8b0cSWill Deacon 	u64 phys_start, phys_size;
317c0e8b0cSWill Deacon 	void *host_mem;
327c0e8b0cSWill Deacon 
337c0e8b0cSWill Deacon 	phys_start	= ARM_MEMORY_AREA;
347c0e8b0cSWill Deacon 	phys_size	= kvm->ram_size;
357c0e8b0cSWill Deacon 	host_mem	= kvm->ram_start;
367c0e8b0cSWill Deacon 
377c0e8b0cSWill Deacon 	err = kvm__register_mem(kvm, phys_start, phys_size, host_mem);
387c0e8b0cSWill Deacon 	if (err)
397c0e8b0cSWill Deacon 		die("Failed to register %lld bytes of memory at physical "
407c0e8b0cSWill Deacon 		    "address 0x%llx [err %d]", phys_size, phys_start, err);
417c0e8b0cSWill Deacon 
427c0e8b0cSWill Deacon 	kvm->arch.memory_guest_start = phys_start;
437c0e8b0cSWill Deacon }
447c0e8b0cSWill Deacon 
457c0e8b0cSWill Deacon void kvm__arch_delete_ram(struct kvm *kvm)
467c0e8b0cSWill Deacon {
479eb3dbcbSWill Deacon 	munmap(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size);
487c0e8b0cSWill Deacon }
497c0e8b0cSWill Deacon 
5012c406a8SJonathan Austin void kvm__arch_read_term(struct kvm *kvm)
517c0e8b0cSWill Deacon {
5297e65b5fSWill Deacon 	if (term_readable(0)) {
5397e65b5fSWill Deacon 		serial8250__update_consoles(kvm);
547c0e8b0cSWill Deacon 		virtio_console__inject_interrupt(kvm);
557c0e8b0cSWill Deacon 	}
5697e65b5fSWill Deacon }
577c0e8b0cSWill Deacon 
587c0e8b0cSWill Deacon void kvm__arch_set_cmdline(char *cmdline, bool video)
597c0e8b0cSWill Deacon {
607c0e8b0cSWill Deacon }
617c0e8b0cSWill Deacon 
627c0e8b0cSWill Deacon void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
637c0e8b0cSWill Deacon {
649eb3dbcbSWill Deacon 	/*
65b4d9ac64SMarc Zyngier 	 * Allocate guest memory. We must align our buffer to 64K to
669eb3dbcbSWill Deacon 	 * correlate with the maximum guest page size for virtio-mmio.
67b4d9ac64SMarc Zyngier 	 * If using THP, then our minimal alignment becomes 2M.
68b4d9ac64SMarc Zyngier 	 * 2M trumps 64K, so let's go with that.
699eb3dbcbSWill Deacon 	 */
701e0c135aSWill Deacon 	kvm->ram_size = min(ram_size, (u64)ARM_MAX_MEMORY(kvm));
71b4d9ac64SMarc Zyngier 	kvm->arch.ram_alloc_size = kvm->ram_size + SZ_2M;
729eb3dbcbSWill Deacon 	kvm->arch.ram_alloc_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path,
739eb3dbcbSWill Deacon 						kvm->arch.ram_alloc_size);
749eb3dbcbSWill Deacon 
759eb3dbcbSWill Deacon 	if (kvm->arch.ram_alloc_start == MAP_FAILED)
767c0e8b0cSWill Deacon 		die("Failed to map %lld bytes for guest memory (%d)",
779eb3dbcbSWill Deacon 		    kvm->arch.ram_alloc_size, errno);
789eb3dbcbSWill Deacon 
799eb3dbcbSWill Deacon 	kvm->ram_start = (void *)ALIGN((unsigned long)kvm->arch.ram_alloc_start,
80b4d9ac64SMarc Zyngier 					SZ_2M);
819eb3dbcbSWill Deacon 
829eb3dbcbSWill Deacon 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
83*0093df80SStefan Agner 		MADV_MERGEABLE);
84*0093df80SStefan Agner 
85*0093df80SStefan Agner 	madvise(kvm->arch.ram_alloc_start, kvm->arch.ram_alloc_size,
86*0093df80SStefan Agner 		MADV_HUGEPAGE);
877c0e8b0cSWill Deacon 
8869b9a17aSMarc Zyngier 	/* Create the virtual GIC. */
8943d2781cSAndre Przywara 	if (gic__create(kvm, kvm->cfg.arch.irqchip))
9069b9a17aSMarc Zyngier 		die("Failed to create virtual GIC");
917c0e8b0cSWill Deacon }
923c8aec9eSAndre Przywara 
933c8aec9eSAndre Przywara #define FDT_ALIGN	SZ_2M
943c8aec9eSAndre Przywara #define INITRD_ALIGN	4
953c8aec9eSAndre Przywara bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
963c8aec9eSAndre Przywara 				 const char *kernel_cmdline)
973c8aec9eSAndre Przywara {
983c8aec9eSAndre Przywara 	void *pos, *kernel_end, *limit;
993c8aec9eSAndre Przywara 	unsigned long guest_addr;
1003c8aec9eSAndre Przywara 	ssize_t file_size;
1013c8aec9eSAndre Przywara 
1023c8aec9eSAndre Przywara 	/*
1033c8aec9eSAndre Przywara 	 * Linux requires the initrd and dtb to be mapped inside lowmem,
1043c8aec9eSAndre Przywara 	 * so we can't just place them at the top of memory.
1053c8aec9eSAndre Przywara 	 */
1063c8aec9eSAndre Przywara 	limit = kvm->ram_start + min(kvm->ram_size, (u64)SZ_256M) - 1;
1073c8aec9eSAndre Przywara 
1083c8aec9eSAndre Przywara 	pos = kvm->ram_start + ARM_KERN_OFFSET(kvm);
1093c8aec9eSAndre Przywara 	kvm->arch.kern_guest_start = host_to_guest_flat(kvm, pos);
1103c8aec9eSAndre Przywara 	file_size = read_file(fd_kernel, pos, limit - pos);
1113c8aec9eSAndre Przywara 	if (file_size < 0) {
1123c8aec9eSAndre Przywara 		if (errno == ENOMEM)
1133c8aec9eSAndre Przywara 			die("kernel image too big to contain in guest memory.");
1143c8aec9eSAndre Przywara 
1153c8aec9eSAndre Przywara 		die_perror("kernel read");
1163c8aec9eSAndre Przywara 	}
1173c8aec9eSAndre Przywara 	kernel_end = pos + file_size;
1183c8aec9eSAndre Przywara 	pr_info("Loaded kernel to 0x%llx (%zd bytes)",
1193c8aec9eSAndre Przywara 		kvm->arch.kern_guest_start, file_size);
1203c8aec9eSAndre Przywara 
1213c8aec9eSAndre Przywara 	/*
1223c8aec9eSAndre Przywara 	 * Now load backwards from the end of memory so the kernel
1233c8aec9eSAndre Przywara 	 * decompressor has plenty of space to work with. First up is
1243c8aec9eSAndre Przywara 	 * the device tree blob...
1253c8aec9eSAndre Przywara 	 */
1263c8aec9eSAndre Przywara 	pos = limit;
1273c8aec9eSAndre Przywara 	pos -= (FDT_MAX_SIZE + FDT_ALIGN);
1283c8aec9eSAndre Przywara 	guest_addr = ALIGN(host_to_guest_flat(kvm, pos), FDT_ALIGN);
1293c8aec9eSAndre Przywara 	pos = guest_flat_to_host(kvm, guest_addr);
1303c8aec9eSAndre Przywara 	if (pos < kernel_end)
1313c8aec9eSAndre Przywara 		die("fdt overlaps with kernel image.");
1323c8aec9eSAndre Przywara 
1333c8aec9eSAndre Przywara 	kvm->arch.dtb_guest_start = guest_addr;
1343c8aec9eSAndre Przywara 	pr_info("Placing fdt at 0x%llx - 0x%llx",
1353c8aec9eSAndre Przywara 		kvm->arch.dtb_guest_start,
1363c8aec9eSAndre Przywara 		host_to_guest_flat(kvm, limit));
1373c8aec9eSAndre Przywara 	limit = pos;
1383c8aec9eSAndre Przywara 
1393c8aec9eSAndre Przywara 	/* ... and finally the initrd, if we have one. */
1403c8aec9eSAndre Przywara 	if (fd_initrd != -1) {
1413c8aec9eSAndre Przywara 		struct stat sb;
1423c8aec9eSAndre Przywara 		unsigned long initrd_start;
1433c8aec9eSAndre Przywara 
1443c8aec9eSAndre Przywara 		if (fstat(fd_initrd, &sb))
1453c8aec9eSAndre Przywara 			die_perror("fstat");
1463c8aec9eSAndre Przywara 
1473c8aec9eSAndre Przywara 		pos -= (sb.st_size + INITRD_ALIGN);
1483c8aec9eSAndre Przywara 		guest_addr = ALIGN(host_to_guest_flat(kvm, pos), INITRD_ALIGN);
1493c8aec9eSAndre Przywara 		pos = guest_flat_to_host(kvm, guest_addr);
1503c8aec9eSAndre Przywara 		if (pos < kernel_end)
1513c8aec9eSAndre Przywara 			die("initrd overlaps with kernel image.");
1523c8aec9eSAndre Przywara 
1533c8aec9eSAndre Przywara 		initrd_start = guest_addr;
1543c8aec9eSAndre Przywara 		file_size = read_file(fd_initrd, pos, limit - pos);
1553c8aec9eSAndre Przywara 		if (file_size == -1) {
1563c8aec9eSAndre Przywara 			if (errno == ENOMEM)
1573c8aec9eSAndre Przywara 				die("initrd too big to contain in guest memory.");
1583c8aec9eSAndre Przywara 
1593c8aec9eSAndre Przywara 			die_perror("initrd read");
1603c8aec9eSAndre Przywara 		}
1613c8aec9eSAndre Przywara 
1623c8aec9eSAndre Przywara 		kvm->arch.initrd_guest_start = initrd_start;
1633c8aec9eSAndre Przywara 		kvm->arch.initrd_size = file_size;
1643c8aec9eSAndre Przywara 		pr_info("Loaded initrd to 0x%llx (%llu bytes)",
1653c8aec9eSAndre Przywara 			kvm->arch.initrd_guest_start,
1663c8aec9eSAndre Przywara 			kvm->arch.initrd_size);
1673c8aec9eSAndre Przywara 	} else {
1683c8aec9eSAndre Przywara 		kvm->arch.initrd_size = 0;
1693c8aec9eSAndre Przywara 	}
1703c8aec9eSAndre Przywara 
1713c8aec9eSAndre Przywara 	return true;
1723c8aec9eSAndre Przywara }
173