xref: /kvmtool/arm/kvm-cpu.c (revision 7d4671e5d372a8507c126b59540ce69069e53d7f)
17c0e8b0cSWill Deacon #include "kvm/kvm.h"
27c0e8b0cSWill Deacon #include "kvm/kvm-cpu.h"
37c0e8b0cSWill Deacon 
47c0e8b0cSWill Deacon static int debug_fd;
57c0e8b0cSWill Deacon 
67c0e8b0cSWill Deacon void kvm_cpu__set_debug_fd(int fd)
77c0e8b0cSWill Deacon {
87c0e8b0cSWill Deacon 	debug_fd = fd;
97c0e8b0cSWill Deacon }
107c0e8b0cSWill Deacon 
117c0e8b0cSWill Deacon int kvm_cpu__get_debug_fd(void)
127c0e8b0cSWill Deacon {
137c0e8b0cSWill Deacon 	return debug_fd;
147c0e8b0cSWill Deacon }
157c0e8b0cSWill Deacon 
1685bd726aSAnup Patel static struct kvm_arm_target *kvm_arm_generic_target;
177c0e8b0cSWill Deacon static struct kvm_arm_target *kvm_arm_targets[KVM_ARM_NUM_TARGETS];
1885bd726aSAnup Patel 
1985bd726aSAnup Patel void kvm_cpu__set_kvm_arm_generic_target(struct kvm_arm_target *target)
2085bd726aSAnup Patel {
2185bd726aSAnup Patel 	kvm_arm_generic_target = target;
2285bd726aSAnup Patel }
2385bd726aSAnup Patel 
247c0e8b0cSWill Deacon int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target)
257c0e8b0cSWill Deacon {
267c0e8b0cSWill Deacon 	unsigned int i = 0;
277c0e8b0cSWill Deacon 
287c0e8b0cSWill Deacon 	for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) {
297c0e8b0cSWill Deacon 		if (!kvm_arm_targets[i]) {
307c0e8b0cSWill Deacon 			kvm_arm_targets[i] = target;
317c0e8b0cSWill Deacon 			return 0;
327c0e8b0cSWill Deacon 		}
337c0e8b0cSWill Deacon 	}
347c0e8b0cSWill Deacon 
357c0e8b0cSWill Deacon 	return -ENOSPC;
367c0e8b0cSWill Deacon }
377c0e8b0cSWill Deacon 
387c0e8b0cSWill Deacon struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
397c0e8b0cSWill Deacon {
409b47146bSMarc Zyngier 	struct kvm_arm_target *target;
417c0e8b0cSWill Deacon 	struct kvm_cpu *vcpu;
427c0e8b0cSWill Deacon 	int coalesced_offset, mmap_size, err = -1;
437c0e8b0cSWill Deacon 	unsigned int i;
4485bd726aSAnup Patel 	struct kvm_vcpu_init preferred_init;
4561076240SWill Deacon 	struct kvm_vcpu_init vcpu_init = {
4661076240SWill Deacon 		.features = ARM_VCPU_FEATURE_FLAGS(kvm, cpu_id)
4761076240SWill Deacon 	};
487c0e8b0cSWill Deacon 
49351d931fSSuzuki K Poulose 	if (kvm->cfg.arch.aarch32_guest &&
50351d931fSSuzuki K Poulose 	    !kvm__supports_extension(kvm, KVM_CAP_ARM_EL1_32BIT))
51351d931fSSuzuki K Poulose 		die("32bit guests are not supported\n");
52351d931fSSuzuki K Poulose 
53415f92c3SAlexandru Elisei 	if (kvm->cfg.arch.has_pmuv3 &&
54415f92c3SAlexandru Elisei 	    !kvm__supports_extension(kvm, KVM_CAP_ARM_PMU_V3))
55415f92c3SAlexandru Elisei 		die("PMUv3 is not supported");
56415f92c3SAlexandru Elisei 
577c0e8b0cSWill Deacon 	vcpu = calloc(1, sizeof(struct kvm_cpu));
587c0e8b0cSWill Deacon 	if (!vcpu)
597c0e8b0cSWill Deacon 		return NULL;
607c0e8b0cSWill Deacon 
617c0e8b0cSWill Deacon 	vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
627c0e8b0cSWill Deacon 	if (vcpu->vcpu_fd < 0)
637c0e8b0cSWill Deacon 		die_perror("KVM_CREATE_VCPU ioctl");
647c0e8b0cSWill Deacon 
657c0e8b0cSWill Deacon 	mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
667c0e8b0cSWill Deacon 	if (mmap_size < 0)
677c0e8b0cSWill Deacon 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
687c0e8b0cSWill Deacon 
697c0e8b0cSWill Deacon 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED,
707c0e8b0cSWill Deacon 			     vcpu->vcpu_fd, 0);
717c0e8b0cSWill Deacon 	if (vcpu->kvm_run == MAP_FAILED)
727c0e8b0cSWill Deacon 		die("unable to mmap vcpu fd");
737c0e8b0cSWill Deacon 
74c2dad402SAnup Patel 	/* Set KVM_ARM_VCPU_PSCI_0_2 if available */
75c2dad402SAnup Patel 	if (kvm__supports_extension(kvm, KVM_CAP_ARM_PSCI_0_2)) {
76c2dad402SAnup Patel 		vcpu_init.features[0] |= (1UL << KVM_ARM_VCPU_PSCI_0_2);
77c2dad402SAnup Patel 	}
78c2dad402SAnup Patel 
7974c5e7b2SDave Martin 	kvm_cpu__select_features(kvm, &vcpu_init);
80254cb189SAmit Daniel Kachhap 
8185bd726aSAnup Patel 	/*
8285bd726aSAnup Patel 	 * If the preferred target ioctl is successful then
8385bd726aSAnup Patel 	 * use preferred target else try each and every target type
8485bd726aSAnup Patel 	 */
8585bd726aSAnup Patel 	err = ioctl(kvm->vm_fd, KVM_ARM_PREFERRED_TARGET, &preferred_init);
8685bd726aSAnup Patel 	if (!err) {
8785bd726aSAnup Patel 		/* Match preferred target CPU type. */
8885bd726aSAnup Patel 		target = NULL;
8985bd726aSAnup Patel 		for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) {
9085bd726aSAnup Patel 			if (!kvm_arm_targets[i])
9185bd726aSAnup Patel 				continue;
9285bd726aSAnup Patel 			if (kvm_arm_targets[i]->id == preferred_init.target) {
9385bd726aSAnup Patel 				target = kvm_arm_targets[i];
9485bd726aSAnup Patel 				break;
9585bd726aSAnup Patel 			}
9685bd726aSAnup Patel 		}
9785bd726aSAnup Patel 		if (!target) {
9885bd726aSAnup Patel 			target = kvm_arm_generic_target;
9985bd726aSAnup Patel 			vcpu_init.target = preferred_init.target;
10085bd726aSAnup Patel 		} else {
10185bd726aSAnup Patel 			vcpu_init.target = target->id;
10285bd726aSAnup Patel 		}
10385bd726aSAnup Patel 		err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init);
10485bd726aSAnup Patel 	} else {
1057c0e8b0cSWill Deacon 		/* Find an appropriate target CPU type. */
1067c0e8b0cSWill Deacon 		for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) {
1078d0413d2SMarc Zyngier 			if (!kvm_arm_targets[i])
1088d0413d2SMarc Zyngier 				continue;
1099b47146bSMarc Zyngier 			target = kvm_arm_targets[i];
1109b47146bSMarc Zyngier 			vcpu_init.target = target->id;
1117c0e8b0cSWill Deacon 			err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init);
1127c0e8b0cSWill Deacon 			if (!err)
1137c0e8b0cSWill Deacon 				break;
1147c0e8b0cSWill Deacon 		}
11585bd726aSAnup Patel 		if (err)
11685bd726aSAnup Patel 			die("Unable to find matching target");
11785bd726aSAnup Patel 	}
1187c0e8b0cSWill Deacon 
119ff695820SSebastian Ene 	/* Populate the vcpu structure. */
120ff695820SSebastian Ene 	vcpu->kvm		= kvm;
121ff695820SSebastian Ene 	vcpu->cpu_id		= cpu_id;
122ff695820SSebastian Ene 	vcpu->cpu_type		= vcpu_init.target;
123ff695820SSebastian Ene 	vcpu->cpu_compatible	= target->compatible;
124ff695820SSebastian Ene 	vcpu->is_running	= true;
125ff695820SSebastian Ene 
1269b47146bSMarc Zyngier 	if (err || target->init(vcpu))
12785bd726aSAnup Patel 		die("Unable to initialise vcpu");
1287c0e8b0cSWill Deacon 
1297c0e8b0cSWill Deacon 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION,
1307c0e8b0cSWill Deacon 				 KVM_CAP_COALESCED_MMIO);
1317c0e8b0cSWill Deacon 	if (coalesced_offset)
1327c0e8b0cSWill Deacon 		vcpu->ring = (void *)vcpu->kvm_run +
1337c0e8b0cSWill Deacon 			     (coalesced_offset * PAGE_SIZE);
1347c0e8b0cSWill Deacon 
135a0eab49aSDave Martin 	if (kvm_cpu__configure_features(vcpu))
136a0eab49aSDave Martin 		die("Unable to configure requested vcpu features");
137a0eab49aSDave Martin 
1387c0e8b0cSWill Deacon 	return vcpu;
1397c0e8b0cSWill Deacon }
1407c0e8b0cSWill Deacon 
1417c0e8b0cSWill Deacon void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
1427c0e8b0cSWill Deacon {
1437c0e8b0cSWill Deacon }
1447c0e8b0cSWill Deacon 
1457c0e8b0cSWill Deacon void kvm_cpu__delete(struct kvm_cpu *vcpu)
1467c0e8b0cSWill Deacon {
147*7d4671e5SSebastian Ene 	kvm_cpu__teardown_pvtime(vcpu->kvm);
1487c0e8b0cSWill Deacon 	free(vcpu);
1497c0e8b0cSWill Deacon }
1507c0e8b0cSWill Deacon 
1517c0e8b0cSWill Deacon bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
1527c0e8b0cSWill Deacon {
1537c0e8b0cSWill Deacon 	return false;
1547c0e8b0cSWill Deacon }
1557c0e8b0cSWill Deacon 
1567c0e8b0cSWill Deacon void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
1577c0e8b0cSWill Deacon {
1587c0e8b0cSWill Deacon }
159