1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include "qemu/osdep.h"
51#include "qemu/error-report.h"
52#include "qemu/main-loop.h"
53#include "exec/address-spaces.h"
54#include "exec/exec-all.h"
55#include "sysemu/cpus.h"
56#include "sysemu/hvf.h"
57#include "sysemu/hvf_int.h"
58#include "sysemu/runstate.h"
59#include "qemu/guest-random.h"
60
61HVFState *hvf_state;
62
63#ifdef __aarch64__
64#define HV_VM_DEFAULT NULL
65#endif
66
67
68
69hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size)
70{
71 hvf_slot *slot;
72 int x;
73 for (x = 0; x < hvf_state->num_slots; ++x) {
74 slot = &hvf_state->slots[x];
75 if (slot->size && start < (slot->start + slot->size) &&
76 (start + size) > slot->start) {
77 return slot;
78 }
79 }
80 return NULL;
81}
82
83struct mac_slot {
84 int present;
85 uint64_t size;
86 uint64_t gpa_start;
87 uint64_t gva;
88};
89
90struct mac_slot mac_slots[32];
91
92static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
93{
94 struct mac_slot *macslot;
95 hv_return_t ret;
96
97 macslot = &mac_slots[slot->slot_id];
98
99 if (macslot->present) {
100 if (macslot->size != slot->size) {
101 macslot->present = 0;
102 ret = hv_vm_unmap(macslot->gpa_start, macslot->size);
103 assert_hvf_ok(ret);
104 }
105 }
106
107 if (!slot->size) {
108 return 0;
109 }
110
111 macslot->present = 1;
112 macslot->gpa_start = slot->start;
113 macslot->size = slot->size;
114 ret = hv_vm_map(slot->mem, slot->start, slot->size, flags);
115 assert_hvf_ok(ret);
116 return 0;
117}
118
119static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
120{
121 hvf_slot *mem;
122 MemoryRegion *area = section->mr;
123 bool writable = !area->readonly && !area->rom_device;
124 hv_memory_flags_t flags;
125 uint64_t page_size = qemu_real_host_page_size();
126
127 if (!memory_region_is_ram(area)) {
128 if (writable) {
129 return;
130 } else if (!memory_region_is_romd(area)) {
131
132
133
134
135 add = false;
136 }
137 }
138
139 if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) ||
140 !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size)) {
141
142 add = false;
143 }
144
145 mem = hvf_find_overlap_slot(
146 section->offset_within_address_space,
147 int128_get64(section->size));
148
149 if (mem && add) {
150 if (mem->size == int128_get64(section->size) &&
151 mem->start == section->offset_within_address_space &&
152 mem->mem == (memory_region_get_ram_ptr(area) +
153 section->offset_within_region)) {
154 return;
155 }
156 }
157
158
159 if (mem) {
160 mem->size = 0;
161 if (do_hvf_set_memory(mem, 0)) {
162 error_report("Failed to reset overlapping slot");
163 abort();
164 }
165 }
166
167 if (!add) {
168 return;
169 }
170
171 if (area->readonly ||
172 (!memory_region_is_ram(area) && memory_region_is_romd(area))) {
173 flags = HV_MEMORY_READ | HV_MEMORY_EXEC;
174 } else {
175 flags = HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC;
176 }
177
178
179 int x;
180
181 for (x = 0; x < hvf_state->num_slots; ++x) {
182 mem = &hvf_state->slots[x];
183 if (!mem->size) {
184 break;
185 }
186 }
187
188 if (x == hvf_state->num_slots) {
189 error_report("No free slots");
190 abort();
191 }
192
193 mem->size = int128_get64(section->size);
194 mem->mem = memory_region_get_ram_ptr(area) + section->offset_within_region;
195 mem->start = section->offset_within_address_space;
196 mem->region = area;
197
198 if (do_hvf_set_memory(mem, flags)) {
199 error_report("Error registering new memory slot");
200 abort();
201 }
202}
203
204static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
205{
206 if (!cpu->vcpu_dirty) {
207 hvf_get_registers(cpu);
208 cpu->vcpu_dirty = true;
209 }
210}
211
212static void hvf_cpu_synchronize_state(CPUState *cpu)
213{
214 if (!cpu->vcpu_dirty) {
215 run_on_cpu(cpu, do_hvf_cpu_synchronize_state, RUN_ON_CPU_NULL);
216 }
217}
218
219static void do_hvf_cpu_synchronize_set_dirty(CPUState *cpu,
220 run_on_cpu_data arg)
221{
222
223 cpu->vcpu_dirty = true;
224}
225
226static void hvf_cpu_synchronize_post_reset(CPUState *cpu)
227{
228 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
229}
230
231static void hvf_cpu_synchronize_post_init(CPUState *cpu)
232{
233 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
234}
235
236static void hvf_cpu_synchronize_pre_loadvm(CPUState *cpu)
237{
238 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
239}
240
241static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)
242{
243 hvf_slot *slot;
244
245 slot = hvf_find_overlap_slot(
246 section->offset_within_address_space,
247 int128_get64(section->size));
248
249
250 if (on) {
251 slot->flags |= HVF_SLOT_LOG;
252 hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size,
253 HV_MEMORY_READ | HV_MEMORY_EXEC);
254
255 } else {
256 slot->flags &= ~HVF_SLOT_LOG;
257 hv_vm_protect((uintptr_t)slot->start, (size_t)slot->size,
258 HV_MEMORY_READ | HV_MEMORY_WRITE | HV_MEMORY_EXEC);
259 }
260}
261
262static void hvf_log_start(MemoryListener *listener,
263 MemoryRegionSection *section, int old, int new)
264{
265 if (old != 0) {
266 return;
267 }
268
269 hvf_set_dirty_tracking(section, 1);
270}
271
272static void hvf_log_stop(MemoryListener *listener,
273 MemoryRegionSection *section, int old, int new)
274{
275 if (new != 0) {
276 return;
277 }
278
279 hvf_set_dirty_tracking(section, 0);
280}
281
282static void hvf_log_sync(MemoryListener *listener,
283 MemoryRegionSection *section)
284{
285
286
287
288
289 hvf_set_dirty_tracking(section, 1);
290}
291
292static void hvf_region_add(MemoryListener *listener,
293 MemoryRegionSection *section)
294{
295 hvf_set_phys_mem(section, true);
296}
297
298static void hvf_region_del(MemoryListener *listener,
299 MemoryRegionSection *section)
300{
301 hvf_set_phys_mem(section, false);
302}
303
304static MemoryListener hvf_memory_listener = {
305 .name = "hvf",
306 .priority = 10,
307 .region_add = hvf_region_add,
308 .region_del = hvf_region_del,
309 .log_start = hvf_log_start,
310 .log_stop = hvf_log_stop,
311 .log_sync = hvf_log_sync,
312};
313
314static void dummy_signal(int sig)
315{
316}
317
318bool hvf_allowed;
319
320static int hvf_accel_init(MachineState *ms)
321{
322 int x;
323 hv_return_t ret;
324 HVFState *s;
325
326 ret = hv_vm_create(HV_VM_DEFAULT);
327 assert_hvf_ok(ret);
328
329 s = g_new0(HVFState, 1);
330
331 s->num_slots = ARRAY_SIZE(s->slots);
332 for (x = 0; x < s->num_slots; ++x) {
333 s->slots[x].size = 0;
334 s->slots[x].slot_id = x;
335 }
336
337 hvf_state = s;
338 memory_listener_register(&hvf_memory_listener, &address_space_memory);
339
340 return hvf_arch_init();
341}
342
343static void hvf_accel_class_init(ObjectClass *oc, void *data)
344{
345 AccelClass *ac = ACCEL_CLASS(oc);
346 ac->name = "HVF";
347 ac->init_machine = hvf_accel_init;
348 ac->allowed = &hvf_allowed;
349}
350
351static const TypeInfo hvf_accel_type = {
352 .name = TYPE_HVF_ACCEL,
353 .parent = TYPE_ACCEL,
354 .class_init = hvf_accel_class_init,
355};
356
357static void hvf_type_init(void)
358{
359 type_register_static(&hvf_accel_type);
360}
361
362type_init(hvf_type_init);
363
364static void hvf_vcpu_destroy(CPUState *cpu)
365{
366 hv_return_t ret = hv_vcpu_destroy(cpu->hvf->fd);
367 assert_hvf_ok(ret);
368
369 hvf_arch_vcpu_destroy(cpu);
370 g_free(cpu->hvf);
371 cpu->hvf = NULL;
372}
373
374static int hvf_init_vcpu(CPUState *cpu)
375{
376 int r;
377
378 cpu->hvf = g_malloc0(sizeof(*cpu->hvf));
379
380
381 struct sigaction sigact;
382
383 memset(&sigact, 0, sizeof(sigact));
384 sigact.sa_handler = dummy_signal;
385 sigaction(SIG_IPI, &sigact, NULL);
386
387 pthread_sigmask(SIG_BLOCK, NULL, &cpu->hvf->unblock_ipi_mask);
388 sigdelset(&cpu->hvf->unblock_ipi_mask, SIG_IPI);
389
390#ifdef __aarch64__
391 r = hv_vcpu_create(&cpu->hvf->fd, (hv_vcpu_exit_t **)&cpu->hvf->exit, NULL);
392#else
393 r = hv_vcpu_create((hv_vcpuid_t *)&cpu->hvf->fd, HV_VCPU_DEFAULT);
394#endif
395 cpu->vcpu_dirty = 1;
396 assert_hvf_ok(r);
397
398 return hvf_arch_init_vcpu(cpu);
399}
400
401
402
403
404
405static void *hvf_cpu_thread_fn(void *arg)
406{
407 CPUState *cpu = arg;
408
409 int r;
410
411 assert(hvf_enabled());
412
413 rcu_register_thread();
414
415 qemu_mutex_lock_iothread();
416 qemu_thread_get_self(cpu->thread);
417
418 cpu->thread_id = qemu_get_thread_id();
419 cpu->can_do_io = 1;
420 current_cpu = cpu;
421
422 hvf_init_vcpu(cpu);
423
424
425 cpu_thread_signal_created(cpu);
426 qemu_guest_random_seed_thread_part2(cpu->random_seed);
427
428 do {
429 if (cpu_can_run(cpu)) {
430 r = hvf_vcpu_exec(cpu);
431 if (r == EXCP_DEBUG) {
432 cpu_handle_guest_debug(cpu);
433 }
434 }
435 qemu_wait_io_event(cpu);
436 } while (!cpu->unplug || cpu_can_run(cpu));
437
438 hvf_vcpu_destroy(cpu);
439 cpu_thread_signal_destroyed(cpu);
440 qemu_mutex_unlock_iothread();
441 rcu_unregister_thread();
442 return NULL;
443}
444
445static void hvf_start_vcpu_thread(CPUState *cpu)
446{
447 char thread_name[VCPU_THREAD_NAME_SIZE];
448
449
450
451
452
453 assert(hvf_enabled());
454
455 cpu->thread = g_malloc0(sizeof(QemuThread));
456 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
457 qemu_cond_init(cpu->halt_cond);
458
459 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
460 cpu->cpu_index);
461 qemu_thread_create(cpu->thread, thread_name, hvf_cpu_thread_fn,
462 cpu, QEMU_THREAD_JOINABLE);
463}
464
465static void hvf_accel_ops_class_init(ObjectClass *oc, void *data)
466{
467 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
468
469 ops->create_vcpu_thread = hvf_start_vcpu_thread;
470 ops->kick_vcpu_thread = hvf_kick_vcpu_thread;
471
472 ops->synchronize_post_reset = hvf_cpu_synchronize_post_reset;
473 ops->synchronize_post_init = hvf_cpu_synchronize_post_init;
474 ops->synchronize_state = hvf_cpu_synchronize_state;
475 ops->synchronize_pre_loadvm = hvf_cpu_synchronize_pre_loadvm;
476};
477static const TypeInfo hvf_accel_ops_type = {
478 .name = ACCEL_OPS_NAME("hvf"),
479
480 .parent = TYPE_ACCEL_OPS,
481 .class_init = hvf_accel_ops_class_init,
482 .abstract = true,
483};
484static void hvf_accel_ops_register_types(void)
485{
486 type_register_static(&hvf_accel_ops_type);
487}
488type_init(hvf_accel_ops_register_types);
489