xref: /kvmtool/arm/kvm-cpu.c (revision 7c0e8b0c5560ce2f500fa4d7ba7865e7360e7991)
1 #include "kvm/kvm.h"
2 #include "kvm/kvm-cpu.h"
3 
4 static int debug_fd;
5 
6 void kvm_cpu__set_debug_fd(int fd)
7 {
8 	debug_fd = fd;
9 }
10 
11 int kvm_cpu__get_debug_fd(void)
12 {
13 	return debug_fd;
14 }
15 
16 static struct kvm_arm_target *kvm_arm_targets[KVM_ARM_NUM_TARGETS];
17 int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target)
18 {
19 	unsigned int i = 0;
20 
21 	for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) {
22 		if (!kvm_arm_targets[i]) {
23 			kvm_arm_targets[i] = target;
24 			return 0;
25 		}
26 	}
27 
28 	return -ENOSPC;
29 }
30 
31 struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
32 {
33 	struct kvm_cpu *vcpu;
34 	int coalesced_offset, mmap_size, err = -1;
35 	unsigned int i;
36 	struct kvm_vcpu_init vcpu_init = { };
37 
38 	vcpu = calloc(1, sizeof(struct kvm_cpu));
39 	if (!vcpu)
40 		return NULL;
41 
42 	vcpu->vcpu_fd = ioctl(kvm->vm_fd, KVM_CREATE_VCPU, cpu_id);
43 	if (vcpu->vcpu_fd < 0)
44 		die_perror("KVM_CREATE_VCPU ioctl");
45 
46 	mmap_size = ioctl(kvm->sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
47 	if (mmap_size < 0)
48 		die_perror("KVM_GET_VCPU_MMAP_SIZE ioctl");
49 
50 	vcpu->kvm_run = mmap(NULL, mmap_size, PROT_RW, MAP_SHARED,
51 			     vcpu->vcpu_fd, 0);
52 	if (vcpu->kvm_run == MAP_FAILED)
53 		die("unable to mmap vcpu fd");
54 
55 	/* Find an appropriate target CPU type. */
56 	for (i = 0; i < ARRAY_SIZE(kvm_arm_targets); ++i) {
57 		vcpu_init.target = kvm_arm_targets[i]->id;
58 		err = ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu_init);
59 		if (!err)
60 			break;
61 	}
62 
63 	if (err || kvm_arm_targets[i]->init(vcpu))
64 		die("Unable to initialise ARM vcpu");
65 
66 	coalesced_offset = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION,
67 				 KVM_CAP_COALESCED_MMIO);
68 	if (coalesced_offset)
69 		vcpu->ring = (void *)vcpu->kvm_run +
70 			     (coalesced_offset * PAGE_SIZE);
71 
72 	/* Populate the vcpu structure. */
73 	vcpu->kvm		= kvm;
74 	vcpu->cpu_id		= cpu_id;
75 	vcpu->cpu_type		= vcpu_init.target;
76 	vcpu->is_running	= true;
77 	return vcpu;
78 }
79 
80 void kvm_cpu__arch_nmi(struct kvm_cpu *cpu)
81 {
82 }
83 
84 void kvm_cpu__delete(struct kvm_cpu *vcpu)
85 {
86 	free(vcpu);
87 }
88 
89 bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
90 {
91 	return false;
92 }
93 
94 bool kvm_cpu__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len,
95 			   u8 is_write)
96 {
97 	if (arm_addr_in_virtio_mmio_region(phys_addr))
98 		return kvm__emulate_mmio(kvm, phys_addr, data, len, is_write);
99 	else if (arm_addr_in_pci_mmio_region(phys_addr))
100 		die("PCI emulation not supported on ARM!");
101 
102 	return false;
103 }
104 
105 void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
106 {
107 }
108