xref: /openbmc/qemu/accel/hvf/hvf-accel-ops.c (revision 89fc45d5)
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
7  *
8  * QEMU Hypervisor.framework support
9  *
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.
13  *
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.
18  *
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/>.
21  *
22  * This file contain code under public domain from the hvdos project:
23  * https://github.com/mist64/hvdos
24  *
25  * Parts Copyright (c) 2011 NetApp, Inc.
26  * All rights reserved.
27  *
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.
36  *
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.
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 
61 HVFState *hvf_state;
62 
63 #ifdef __aarch64__
64 #define HV_VM_DEFAULT NULL
65 #endif
66 
67 /* Memory slots */
68 
69 hvf_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 
83 struct mac_slot {
84     int present;
85     uint64_t size;
86     uint64_t gpa_start;
87     uint64_t gva;
88 };
89 
90 struct mac_slot mac_slots[32];
91 
92 static 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 
119 static void hvf_set_phys_mem(MemoryRegionSection *section, bool add)
120 {
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();
126 
127     if (!memory_region_is_ram(area)) {
128         if (writeable) {
129             return;
130         } else if (!memory_region_is_romd(area)) {
131             /*
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.
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         /* Not page aligned, so we can not map as RAM */
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; /* Same region was attempted to register, go away. */
155         }
156     }
157 
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();
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     /* Now make a new slot. */
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 
204 static 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 
212 static 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 
219 static void do_hvf_cpu_synchronize_set_dirty(CPUState *cpu,
220                                              run_on_cpu_data arg)
221 {
222     /* QEMU state is the reference, push it to HVF now and on next entry */
223     cpu->vcpu_dirty = true;
224 }
225 
226 static 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 
231 static 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 
236 static 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 
241 static 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     /* 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);
259     }
260 }
261 
262 static 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 
272 static 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 
282 static void hvf_log_sync(MemoryListener *listener,
283                          MemoryRegionSection *section)
284 {
285     /*
286      * sync of dirty pages is handled elsewhere; just make sure we keep
287      * tracking the region.
288      */
289     hvf_set_dirty_tracking(section, 1);
290 }
291 
292 static void hvf_region_add(MemoryListener *listener,
293                            MemoryRegionSection *section)
294 {
295     hvf_set_phys_mem(section, true);
296 }
297 
298 static void hvf_region_del(MemoryListener *listener,
299                            MemoryRegionSection *section)
300 {
301     hvf_set_phys_mem(section, false);
302 }
303 
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,
312 };
313 
314 static void dummy_signal(int sig)
315 {
316 }
317 
318 bool hvf_allowed;
319 
320 static 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 
343 static 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 
351 static const TypeInfo hvf_accel_type = {
352     .name = TYPE_HVF_ACCEL,
353     .parent = TYPE_ACCEL,
354     .class_init = hvf_accel_class_init,
355 };
356 
357 static void hvf_type_init(void)
358 {
359     type_register_static(&hvf_accel_type);
360 }
361 
362 type_init(hvf_type_init);
363 
364 static 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 
374 static int hvf_init_vcpu(CPUState *cpu)
375 {
376     int r;
377 
378     cpu->hvf = g_malloc0(sizeof(*cpu->hvf));
379 
380     /* init cpu signals */
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  * The HVF-specific vCPU thread function. This one should only run when the host
403  * CPU supports the VMX "unrestricted guest" feature.
404  */
405 static 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     /* signal CPU creation */
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 
445 static void hvf_start_vcpu_thread(CPUState *cpu)
446 {
447     char thread_name[VCPU_THREAD_NAME_SIZE];
448 
449     /*
450      * HVF currently does not support TCG, and only runs in
451      * unrestricted-guest mode.
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 
465 static 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 };
477 static 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 };
484 static void hvf_accel_ops_register_types(void)
485 {
486     type_register_static(&hvf_accel_ops_type);
487 }
488 type_init(hvf_accel_ops_register_types);
489