kvm-cpu.c 6.72 KB
Newer Older
1
2
#include "kvm/kvm-cpu.h"

3
#include "kvm/symbol.h"
4
5
#include "kvm/util.h"
#include "kvm/kvm.h"
6
#include "kvm/virtio.h"
7
8
#include "kvm/mutex.h"
#include "kvm/barrier.h"
9
10
11

#include <sys/ioctl.h>
#include <sys/mman.h>
12
#include <sys/eventfd.h>
13
#include <signal.h>
14
#include <stdlib.h>
15
#include <string.h>
16
17
18
#include <errno.h>
#include <stdio.h>

19
20
extern __thread struct kvm_cpu *current_kvm_cpu;

21
22
23
24
25
int __attribute__((weak)) kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
{
	return VIRTIO_ENDIAN_HOST;
}

26
void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
27
28
29
30
31
{
	struct kvm_guest_debug debug = {
		.control	= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP,
	};

32
	if (ioctl(vcpu->vcpu_fd, KVM_SET_GUEST_DEBUG, &debug) < 0)
33
		pr_warning("KVM_SET_GUEST_DEBUG failed");
34
35
}

36
void kvm_cpu__run(struct kvm_cpu *vcpu)
37
38
39
{
	int err;

40
41
42
	if (!vcpu->is_running)
		return;

43
	err = ioctl(vcpu->vcpu_fd, KVM_RUN, 0);
44
	if (err < 0 && (errno != EINTR && errno != EAGAIN))
45
46
		die_perror("KVM_RUN failed");
}
47

48
static void kvm_cpu_signal_handler(int signum)
49
{
50
	if (signum == SIGKVMEXIT) {
51
		if (current_kvm_cpu && current_kvm_cpu->is_running)
52
53
54
			current_kvm_cpu->is_running = false;
	} else if (signum == SIGKVMPAUSE) {
		current_kvm_cpu->paused = 1;
55
	}
56
57

	/* For SIGKVMTASK cpu->task is already set */
58
59
}

60
61
62
63
64
65
static void kvm_cpu__handle_coalesced_mmio(struct kvm_cpu *cpu)
{
	if (cpu->ring) {
		while (cpu->ring->first != cpu->ring->last) {
			struct kvm_coalesced_mmio *m;
			m = &cpu->ring->coalesced_mmio[cpu->ring->first];
66
			kvm_cpu__emulate_mmio(cpu,
67
68
69
70
					      m->phys_addr,
					      m->data,
					      m->len,
					      1);
71
72
73
74
75
			cpu->ring->first = (cpu->ring->first + 1) % KVM_COALESCED_MMIO_MAX;
		}
	}
}

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
static DEFINE_MUTEX(task_lock);
static int task_eventfd;

static void kvm_cpu__run_task(struct kvm_cpu *cpu)
{
	u64 inc = 1;

	pr_debug("Running task %p on cpu %lu", cpu->task, cpu->cpu_id);

	/* Make sure we see the store to cpu->task */
	rmb();
	cpu->task->func(cpu, cpu->task->data);

	/* Clear task before we signal completion */
	cpu->task = NULL;
	wmb();

	if (write(task_eventfd, &inc, sizeof(inc)) < 0)
		die("Failed notifying of completed task.");
}

void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task)
{
	int i, done = 0;

	pr_debug("Running task %p on all cpus", task);

	mutex_lock(&task_lock);

	for (i = 0; i < kvm->nrcpus; i++) {
		if (kvm->cpus[i]->task) {
			/* Should never happen */
			die("CPU %d already has a task pending!", i);
		}

		kvm->cpus[i]->task = task;
		wmb();

		if (kvm->cpus[i] == current_kvm_cpu)
			kvm_cpu__run_task(current_kvm_cpu);
		else
			pthread_kill(kvm->cpus[i]->thread, SIGKVMTASK);
	}

	while (done < kvm->nrcpus) {
		u64 count;

		if (read(task_eventfd, &count, sizeof(count)) < 0)
			die("Failed reading task eventfd");

		done += count;
	}

	mutex_unlock(&task_lock);
}

132
133
int kvm_cpu__start(struct kvm_cpu *cpu)
{
134
135
136
137
138
139
140
	sigset_t sigset;

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGALRM);

	pthread_sigmask(SIG_BLOCK, &sigset, NULL);

141
142
	signal(SIGKVMEXIT, kvm_cpu_signal_handler);
	signal(SIGKVMPAUSE, kvm_cpu_signal_handler);
143
	signal(SIGKVMTASK, kvm_cpu_signal_handler);
144

145
146
	kvm_cpu__reset_vcpu(cpu);

147
	if (cpu->kvm->cfg.single_step)
148
149
		kvm_cpu__enable_singlestep(cpu);

150
	while (cpu->is_running) {
151
		if (cpu->paused)
152
153
			kvm__notify_paused();

154
155
156
157
158
		if (cpu->needs_nmi) {
			kvm_cpu__arch_nmi(cpu);
			cpu->needs_nmi = 0;
		}

159
160
161
		if (cpu->task)
			kvm_cpu__run_task(cpu);

162
163
164
		kvm_cpu__run(cpu);

		switch (cpu->kvm_run->exit_reason) {
Sasha Levin's avatar
Sasha Levin committed
165
166
		case KVM_EXIT_UNKNOWN:
			break;
167
168
169
170
171
172
173
		case KVM_EXIT_DEBUG:
			kvm_cpu__show_registers(cpu);
			kvm_cpu__show_code(cpu);
			break;
		case KVM_EXIT_IO: {
			bool ret;

174
			ret = kvm_cpu__emulate_io(cpu,
175
176
177
178
179
180
						  cpu->kvm_run->io.port,
						  (u8 *)cpu->kvm_run +
						  cpu->kvm_run->io.data_offset,
						  cpu->kvm_run->io.direction,
						  cpu->kvm_run->io.size,
						  cpu->kvm_run->io.count);
181
182
183
184
185
186
187
188

			if (!ret)
				goto panic_kvm;
			break;
		}
		case KVM_EXIT_MMIO: {
			bool ret;

189
190
191
192
193
194
			/*
			 * If we had MMIO exit, coalesced ring should be processed
			 * *before* processing the exit itself
			 */
			kvm_cpu__handle_coalesced_mmio(cpu);

195
			ret = kvm_cpu__emulate_mmio(cpu,
196
197
198
199
						    cpu->kvm_run->mmio.phys_addr,
						    cpu->kvm_run->mmio.data,
						    cpu->kvm_run->mmio.len,
						    cpu->kvm_run->mmio.is_write);
200
201
202
203
204

			if (!ret)
				goto panic_kvm;
			break;
		}
205
		case KVM_EXIT_INTR:
206
207
			if (cpu->is_running)
				break;
208
			goto exit_kvm;
209
210
		case KVM_EXIT_SHUTDOWN:
			goto exit_kvm;
211
212
213
214
215
216
217
218
219
		case KVM_EXIT_SYSTEM_EVENT:
			/*
			 * Print the type of system event and
			 * treat all system events as shutdown request.
			 */
			switch (cpu->kvm_run->system_event.type) {
			default:
				pr_warning("unknown system event type %d",
					   cpu->kvm_run->system_event.type);
220
221
222
223
224
225
226
227
				/* fall through for now */
			case KVM_SYSTEM_EVENT_RESET:
				/* Fall through for now */
			case KVM_SYSTEM_EVENT_SHUTDOWN:
				/*
				 * Ensure that all VCPUs are torn down,
				 * regardless of which CPU generated the event.
				 */
228
				kvm__reboot(cpu->kvm);
229
230
231
				goto exit_kvm;
			};
			break;
232
233
234
235
236
237
238
239
		default: {
			bool ret;

			ret = kvm_cpu__handle_exit(cpu);
			if (!ret)
				goto panic_kvm;
			break;
		}
240
		}
241
		kvm_cpu__handle_coalesced_mmio(cpu);
242
243
244
245
246
247
248
249
	}

exit_kvm:
	return 0;

panic_kvm:
	return 1;
}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

int kvm_cpu__init(struct kvm *kvm)
{
	int max_cpus, recommended_cpus, i;

	max_cpus = kvm__max_cpus(kvm);
	recommended_cpus = kvm__recommended_cpus(kvm);

	if (kvm->cfg.nrcpus > max_cpus) {
		printf("  # Limit the number of CPUs to %d\n", max_cpus);
		kvm->cfg.nrcpus = max_cpus;
	} else if (kvm->cfg.nrcpus > recommended_cpus) {
		printf("  # Warning: The maximum recommended amount of VCPUs"
			" is %d\n", recommended_cpus);
	}

	kvm->nrcpus = kvm->cfg.nrcpus;

268
269
270
271
272
273
	task_eventfd = eventfd(0, 0);
	if (task_eventfd < 0) {
		pr_warning("Couldn't create task_eventfd");
		return task_eventfd;
	}

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
	/* Alloc one pointer too many, so array ends up 0-terminated */
	kvm->cpus = calloc(kvm->nrcpus + 1, sizeof(void *));
	if (!kvm->cpus) {
		pr_warning("Couldn't allocate array for %d CPUs", kvm->nrcpus);
		return -ENOMEM;
	}

	for (i = 0; i < kvm->nrcpus; i++) {
		kvm->cpus[i] = kvm_cpu__arch_init(kvm, i);
		if (!kvm->cpus[i]) {
			pr_warning("unable to initialize KVM VCPU");
			goto fail_alloc;
		}
	}

	return 0;

fail_alloc:
	for (i = 0; i < kvm->nrcpus; i++)
		free(kvm->cpus[i]);
	return -ENOMEM;
}
296
base_init(kvm_cpu__init);
297
298
299
300
301
302
303
304
305

int kvm_cpu__exit(struct kvm *kvm)
{
	int i, r;
	void *ret = NULL;

	kvm_cpu__delete(kvm->cpus[0]);
	kvm->cpus[0] = NULL;

306
	kvm__pause(kvm);
307
308
309
310
311
312
313
314
315
316
	for (i = 1; i < kvm->nrcpus; i++) {
		if (kvm->cpus[i]->is_running) {
			pthread_kill(kvm->cpus[i]->thread, SIGKVMEXIT);
			if (pthread_join(kvm->cpus[i]->thread, &ret) != 0)
				die("pthread_join");
			kvm_cpu__delete(kvm->cpus[i]);
		}
		if (ret == NULL)
			r = 0;
	}
317
	kvm__continue(kvm);
318
319
320
321
322

	free(kvm->cpus);

	kvm->nrcpus = 0;

323
324
	close(task_eventfd);

325
326
	return r;
}