xref: /kvmtool/kvm-cpu.c (revision 4123ca555b1d466b08c630763a378b4d53de9ec5)
1 #include "kvm/kvm-cpu.h"
2 
3 #include "kvm/symbol.h"
4 #include "kvm/util.h"
5 #include "kvm/kvm.h"
6 #include "kvm/virtio.h"
7 
8 #include <sys/ioctl.h>
9 #include <sys/mman.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <stdio.h>
15 
16 extern __thread struct kvm_cpu *current_kvm_cpu;
17 
18 int __attribute__((weak)) kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
19 {
20 	return VIRTIO_ENDIAN_HOST;
21 }
22 
23 void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
24 {
25 	struct kvm_guest_debug debug = {
26 		.control	= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
27 	};
28 
29 	if (ioctl(vcpu->vcpu_fd, KVM_SET_GUEST_DEBUG, &debug) < 0)
30 		pr_warning("KVM_SET_GUEST_DEBUG failed");
31 }
32 
33 void kvm_cpu__run(struct kvm_cpu *vcpu)
34 {
35 	int err;
36 
37 	if (!vcpu->is_running)
38 		return;
39 
40 	err = ioctl(vcpu->vcpu_fd, KVM_RUN, 0);
41 	if (err < 0 && (errno != EINTR && errno != EAGAIN))
42 		die_perror("KVM_RUN failed");
43 }
44 
45 static void kvm_cpu_signal_handler(int signum)
46 {
47 	if (signum == SIGKVMEXIT) {
48 		if (current_kvm_cpu && current_kvm_cpu->is_running) {
49 			current_kvm_cpu->is_running = false;
50 			kvm__continue(current_kvm_cpu->kvm);
51 		}
52 	} else if (signum == SIGKVMPAUSE) {
53 		current_kvm_cpu->paused = 1;
54 	}
55 }
56 
57 static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
58 {
59 	if (cpu->ring) {
60 		while (cpu->ring->first != cpu->ring->last) {
61 			struct kvm_coalesced_mmio *m;
62 			m = &cpu->ring->coalesced_mmio[cpu->ring->first];
63 			kvm_cpu__emulate_mmio(cpu,
64 					      m->phys_addr,
65 					      m->data,
66 					      m->len,
67 					      1);
68 			cpu->ring->first = (cpu->ring->first + 1) % KVM_COALESCED_MMIO_MAX;
69 		}
70 	}
71 }
72 
73 void kvm_cpu__reboot(struct kvm *kvm)
74 {
75 	int i;
76 
77 	/* The kvm->cpus array contains a null pointer in the last location */
78 	for (i = 0; ; i++) {
79 		if (kvm->cpus[i])
80 			pthread_kill(kvm->cpus[i]->thread, SIGKVMEXIT);
81 		else
82 			break;
83 	}
84 }
85 
86 int kvm_cpu__start(struct kvm_cpu *cpu)
87 {
88 	sigset_t sigset;
89 
90 	sigemptyset(&sigset);
91 	sigaddset(&sigset, SIGALRM);
92 
93 	pthread_sigmask(SIG_BLOCK, &sigset, NULL);
94 
95 	signal(SIGKVMEXIT, kvm_cpu_signal_handler);
96 	signal(SIGKVMPAUSE, kvm_cpu_signal_handler);
97 
98 	kvm_cpu__reset_vcpu(cpu);
99 
100 	if (cpu->kvm->cfg.single_step)
101 		kvm_cpu__enable_singlestep(cpu);
102 
103 	while (cpu->is_running) {
104 		if (cpu->paused) {
105 			kvm__notify_paused();
106 			cpu->paused = 0;
107 		}
108 
109 		if (cpu->needs_nmi) {
110 			kvm_cpu__arch_nmi(cpu);
111 			cpu->needs_nmi = 0;
112 		}
113 
114 		kvm_cpu__run(cpu);
115 
116 		switch (cpu->kvm_run->exit_reason) {
117 		case KVM_EXIT_UNKNOWN:
118 			break;
119 		case KVM_EXIT_DEBUG:
120 			kvm_cpu__show_registers(cpu);
121 			kvm_cpu__show_code(cpu);
122 			break;
123 		case KVM_EXIT_IO: {
124 			bool ret;
125 
126 			ret = kvm_cpu__emulate_io(cpu,
127 						  cpu->kvm_run->io.port,
128 						  (u8 *)cpu->kvm_run +
129 						  cpu->kvm_run->io.data_offset,
130 						  cpu->kvm_run->io.direction,
131 						  cpu->kvm_run->io.size,
132 						  cpu->kvm_run->io.count);
133 
134 			if (!ret)
135 				goto panic_kvm;
136 			break;
137 		}
138 		case KVM_EXIT_MMIO: {
139 			bool ret;
140 
141 			/*
142 			 * If we had MMIO exit, coalesced ring should be processed
143 			 * *before* processing the exit itself
144 			 */
145 			kvm_cpu__handle_coalesced_mmio(cpu);
146 
147 			ret = kvm_cpu__emulate_mmio(cpu,
148 						    cpu->kvm_run->mmio.phys_addr,
149 						    cpu->kvm_run->mmio.data,
150 						    cpu->kvm_run->mmio.len,
151 						    cpu->kvm_run->mmio.is_write);
152 
153 			if (!ret)
154 				goto panic_kvm;
155 			break;
156 		}
157 		case KVM_EXIT_INTR:
158 			if (cpu->is_running)
159 				break;
160 			goto exit_kvm;
161 		case KVM_EXIT_SHUTDOWN:
162 			goto exit_kvm;
163 		default: {
164 			bool ret;
165 
166 			ret = kvm_cpu__handle_exit(cpu);
167 			if (!ret)
168 				goto panic_kvm;
169 			break;
170 		}
171 		}
172 		kvm_cpu__handle_coalesced_mmio(cpu);
173 	}
174 
175 exit_kvm:
176 	return 0;
177 
178 panic_kvm:
179 	return 1;
180 }
181 
182 int kvm_cpu__init(struct kvm *kvm)
183 {
184 	int max_cpus, recommended_cpus, i;
185 
186 	max_cpus = kvm__max_cpus(kvm);
187 	recommended_cpus = kvm__recommended_cpus(kvm);
188 
189 	if (kvm->cfg.nrcpus > max_cpus) {
190 		printf("  # Limit the number of CPUs to %d\n", max_cpus);
191 		kvm->cfg.nrcpus = max_cpus;
192 	} else if (kvm->cfg.nrcpus > recommended_cpus) {
193 		printf("  # Warning: The maximum recommended amount of VCPUs"
194 			" is %d\n", recommended_cpus);
195 	}
196 
197 	kvm->nrcpus = kvm->cfg.nrcpus;
198 
199 	/* Alloc one pointer too many, so array ends up 0-terminated */
200 	kvm->cpus = calloc(kvm->nrcpus + 1, sizeof(void *));
201 	if (!kvm->cpus) {
202 		pr_warning("Couldn't allocate array for %d CPUs", kvm->nrcpus);
203 		return -ENOMEM;
204 	}
205 
206 	for (i = 0; i < kvm->nrcpus; i++) {
207 		kvm->cpus[i] = kvm_cpu__arch_init(kvm, i);
208 		if (!kvm->cpus[i]) {
209 			pr_warning("unable to initialize KVM VCPU");
210 			goto fail_alloc;
211 		}
212 	}
213 
214 	return 0;
215 
216 fail_alloc:
217 	for (i = 0; i < kvm->nrcpus; i++)
218 		free(kvm->cpus[i]);
219 	return -ENOMEM;
220 }
221 base_init(kvm_cpu__init);
222 
223 int kvm_cpu__exit(struct kvm *kvm)
224 {
225 	int i, r;
226 	void *ret = NULL;
227 
228 	kvm_cpu__delete(kvm->cpus[0]);
229 	kvm->cpus[0] = NULL;
230 
231 	for (i = 1; i < kvm->nrcpus; i++) {
232 		if (kvm->cpus[i]->is_running) {
233 			pthread_kill(kvm->cpus[i]->thread, SIGKVMEXIT);
234 			if (pthread_join(kvm->cpus[i]->thread, &ret) != 0)
235 				die("pthread_join");
236 			kvm_cpu__delete(kvm->cpus[i]);
237 		}
238 		if (ret == NULL)
239 			r = 0;
240 	}
241 
242 	free(kvm->cpus);
243 
244 	kvm->nrcpus = 0;
245 
246 	return r;
247 }
248 late_exit(kvm_cpu__exit);
249