python/aqmp: copy type definitions from qmp
[qemu.git] / accel / hvf / hvf-accel-ops.c
blob54457c76c2f3a45ee95eecf57dd2e202f51c17d1
1 /*
2 * Copyright 2008 IBM Corporation
3 * 2008 Red Hat, Inc.
4 * Copyright 2011 Intel Corporation
5 * Copyright 2016 Veertu, Inc.
6 * Copyright 2017 The Android Open Source Project
8 * QEMU Hypervisor.framework support
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * This file contain code under public domain from the hvdos project:
23 * https://github.com/mist64/hvdos
25 * Parts Copyright (c) 2011 NetApp, Inc.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
37 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
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"
61 HVFState *hvf_state;
63 #ifdef __aarch64__
64 #define HV_VM_DEFAULT NULL
65 #endif
67 /* Memory slots */
69 hvf_slot *hvf_find_overlap_slot(uint64_t start, uint64_t size)
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;
80 return NULL;
83 struct mac_slot {
84 int present;
85 uint64_t size;
86 uint64_t gpa_start;
87 uint64_t gva;
90 struct mac_slot mac_slots[32];
92 static int do_hvf_set_memory(hvf_slot *slot, hv_memory_flags_t flags)
94 struct mac_slot *macslot;
95 hv_return_t ret;
97 macslot = &mac_slots[slot->slot_id];
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);
107 if (!slot->size) {
108 return 0;
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;
119 static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
121 hvf_slot *mem;
122 MemoryRegion *area = section->mr;
123 bool writeable = !area->readonly && !area->rom_device;
124 hv_memory_flags_t flags;
125 uint64_t page_size = qemu_real_host_page_size;
127 if (!memory_region_is_ram(area)) {
128 if (writeable) {
129 return;
130 } else if (!memory_region_is_romd(area)) {
132 * If the memory device is not in romd_mode, then we actually want
133 * to remove the hvf memory slot so all accesses will trap.
135 add = false;
139 if (!QEMU_IS_ALIGNED(int128_get64(section->size), page_size) ||
140 !QEMU_IS_ALIGNED(section->offset_within_address_space, page_size)) {
141 /* Not page aligned, so we can not map as RAM */
142 add = false;
145 mem = hvf_find_overlap_slot(
146 section->offset_within_address_space,
147 int128_get64(section->size));
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; /* Same region was attempted to register, go away. */
158 /* Region needs to be reset. set the size to 0 and remap it. */
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();
167 if (!add) {
168 return;
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;
178 /* Now make a new slot. */
179 int x;
181 for (x = 0; x < hvf_state->num_slots; ++x) {
182 mem = &hvf_state->slots[x];
183 if (!mem->size) {
184 break;
188 if (x == hvf_state->num_slots) {
189 error_report("No free slots");
190 abort();
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;
198 if (do_hvf_set_memory(mem, flags)) {
199 error_report("Error registering new memory slot");
200 abort();
204 static void do_hvf_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
206 if (!cpu->vcpu_dirty) {
207 hvf_get_registers(cpu);
208 cpu->vcpu_dirty = true;
212 static void hvf_cpu_synchronize_state(CPUState *cpu)
214 if (!cpu->vcpu_dirty) {
215 run_on_cpu(cpu, do_hvf_cpu_synchronize_state, RUN_ON_CPU_NULL);
219 static void do_hvf_cpu_synchronize_set_dirty(CPUState *cpu,
220 run_on_cpu_data arg)
222 /* QEMU state is the reference, push it to HVF now and on next entry */
223 cpu->vcpu_dirty = true;
226 static void hvf_cpu_synchronize_post_reset(CPUState *cpu)
228 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
231 static void hvf_cpu_synchronize_post_init(CPUState *cpu)
233 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
236 static void hvf_cpu_synchronize_pre_loadvm(CPUState *cpu)
238 run_on_cpu(cpu, do_hvf_cpu_synchronize_set_dirty, RUN_ON_CPU_NULL);
241 static void hvf_set_dirty_tracking(MemoryRegionSection *section, bool on)
243 hvf_slot *slot;
245 slot = hvf_find_overlap_slot(
246 section->offset_within_address_space,
247 int128_get64(section->size));
249 /* protect region against writes; begin tracking it */
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 /* stop tracking region*/
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);
262 static void hvf_log_start(MemoryListener *listener,
263 MemoryRegionSection *section, int old, int new)
265 if (old != 0) {
266 return;
269 hvf_set_dirty_tracking(section, 1);
272 static void hvf_log_stop(MemoryListener *listener,
273 MemoryRegionSection *section, int old, int new)
275 if (new != 0) {
276 return;
279 hvf_set_dirty_tracking(section, 0);
282 static void hvf_log_sync(MemoryListener *listener,
283 MemoryRegionSection *section)
286 * sync of dirty pages is handled elsewhere; just make sure we keep
287 * tracking the region.
289 hvf_set_dirty_tracking(section, 1);
292 static void hvf_region_add(MemoryListener *listener,
293 MemoryRegionSection *section)
295 hvf_set_phys_mem(section, true);
298 static void hvf_region_del(MemoryListener *listener,
299 MemoryRegionSection *section)
301 hvf_set_phys_mem(section, false);
304 static 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,
314 static void dummy_signal(int sig)
318 bool hvf_allowed;
320 static int hvf_accel_init(MachineState *ms)
322 int x;
323 hv_return_t ret;
324 HVFState *s;
326 ret = hv_vm_create(HV_VM_DEFAULT);
327 assert_hvf_ok(ret);
329 s = g_new0(HVFState, 1);
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;
337 hvf_state = s;
338 memory_listener_register(&hvf_memory_listener, &address_space_memory);
340 return hvf_arch_init();
343 static void hvf_accel_class_init(ObjectClass *oc, void *data)
345 AccelClass *ac = ACCEL_CLASS(oc);
346 ac->name = "HVF";
347 ac->init_machine = hvf_accel_init;
348 ac->allowed = &hvf_allowed;
351 static const TypeInfo hvf_accel_type = {
352 .name = TYPE_HVF_ACCEL,
353 .parent = TYPE_ACCEL,
354 .class_init = hvf_accel_class_init,
357 static void hvf_type_init(void)
359 type_register_static(&hvf_accel_type);
362 type_init(hvf_type_init);
364 static void hvf_vcpu_destroy(CPUState *cpu)
366 hv_return_t ret = hv_vcpu_destroy(cpu->hvf->fd);
367 assert_hvf_ok(ret);
369 hvf_arch_vcpu_destroy(cpu);
370 g_free(cpu->hvf);
371 cpu->hvf = NULL;
374 static int hvf_init_vcpu(CPUState *cpu)
376 int r;
378 cpu->hvf = g_malloc0(sizeof(*cpu->hvf));
380 /* init cpu signals */
381 struct sigaction sigact;
383 memset(&sigact, 0, sizeof(sigact));
384 sigact.sa_handler = dummy_signal;
385 sigaction(SIG_IPI, &sigact, NULL);
387 pthread_sigmask(SIG_BLOCK, NULL, &cpu->hvf->unblock_ipi_mask);
388 sigdelset(&cpu->hvf->unblock_ipi_mask, SIG_IPI);
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);
398 return hvf_arch_init_vcpu(cpu);
402 * The HVF-specific vCPU thread function. This one should only run when the host
403 * CPU supports the VMX "unrestricted guest" feature.
405 static void *hvf_cpu_thread_fn(void *arg)
407 CPUState *cpu = arg;
409 int r;
411 assert(hvf_enabled());
413 rcu_register_thread();
415 qemu_mutex_lock_iothread();
416 qemu_thread_get_self(cpu->thread);
418 cpu->thread_id = qemu_get_thread_id();
419 cpu->can_do_io = 1;
420 current_cpu = cpu;
422 hvf_init_vcpu(cpu);
424 /* signal CPU creation */
425 cpu_thread_signal_created(cpu);
426 qemu_guest_random_seed_thread_part2(cpu->random_seed);
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);
435 qemu_wait_io_event(cpu);
436 } while (!cpu->unplug || cpu_can_run(cpu));
438 hvf_vcpu_destroy(cpu);
439 cpu_thread_signal_destroyed(cpu);
440 qemu_mutex_unlock_iothread();
441 rcu_unregister_thread();
442 return NULL;
445 static void hvf_start_vcpu_thread(CPUState *cpu)
447 char thread_name[VCPU_THREAD_NAME_SIZE];
450 * HVF currently does not support TCG, and only runs in
451 * unrestricted-guest mode.
453 assert(hvf_enabled());
455 cpu->thread = g_malloc0(sizeof(QemuThread));
456 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
457 qemu_cond_init(cpu->halt_cond);
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);
465 static void hvf_accel_ops_class_init(ObjectClass *oc, void *data)
467 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
469 ops->create_vcpu_thread = hvf_start_vcpu_thread;
470 ops->kick_vcpu_thread = hvf_kick_vcpu_thread;
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;
477 static const TypeInfo hvf_accel_ops_type = {
478 .name = ACCEL_OPS_NAME("hvf"),
480 .parent = TYPE_ACCEL_OPS,
481 .class_init = hvf_accel_ops_class_init,
482 .abstract = true,
484 static void hvf_accel_ops_register_types(void)
486 type_register_static(&hvf_accel_ops_type);
488 type_init(hvf_accel_ops_register_types);