xref: /qemu/target/i386/nvmm/nvmm-accel-ops.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
1 /*
2  * Copyright (c) 2018-2019 Maxime Villard, All rights reserved.
3  *
4  * NetBSD Virtual Machine Monitor (NVMM) accelerator for QEMU.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "system/kvm_int.h"
12 #include "qemu/main-loop.h"
13 #include "system/accel-ops.h"
14 #include "system/cpus.h"
15 #include "qemu/guest-random.h"
16 
17 #include "system/nvmm.h"
18 #include "nvmm-accel-ops.h"
19 
qemu_nvmm_cpu_thread_fn(void * arg)20 static void *qemu_nvmm_cpu_thread_fn(void *arg)
21 {
22     CPUState *cpu = arg;
23     int r;
24 
25     assert(nvmm_enabled());
26 
27     rcu_register_thread();
28 
29     bql_lock();
30     qemu_thread_get_self(cpu->thread);
31     cpu->thread_id = qemu_get_thread_id();
32     current_cpu = cpu;
33 
34     r = nvmm_init_vcpu(cpu);
35     if (r < 0) {
36         fprintf(stderr, "nvmm_init_vcpu failed: %s\n", strerror(-r));
37         exit(1);
38     }
39 
40     /* signal CPU creation */
41     cpu_thread_signal_created(cpu);
42     qemu_guest_random_seed_thread_part2(cpu->random_seed);
43 
44     do {
45         if (cpu_can_run(cpu)) {
46             r = nvmm_vcpu_exec(cpu);
47             if (r == EXCP_DEBUG) {
48                 cpu_handle_guest_debug(cpu);
49             }
50         }
51         while (cpu_thread_is_idle(cpu)) {
52             qemu_cond_wait_bql(cpu->halt_cond);
53         }
54         qemu_wait_io_event_common(cpu);
55     } while (!cpu->unplug || cpu_can_run(cpu));
56 
57     nvmm_destroy_vcpu(cpu);
58     cpu_thread_signal_destroyed(cpu);
59     bql_unlock();
60     rcu_unregister_thread();
61     return NULL;
62 }
63 
nvmm_start_vcpu_thread(CPUState * cpu)64 static void nvmm_start_vcpu_thread(CPUState *cpu)
65 {
66     char thread_name[VCPU_THREAD_NAME_SIZE];
67 
68     snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/NVMM",
69              cpu->cpu_index);
70     qemu_thread_create(cpu->thread, thread_name, qemu_nvmm_cpu_thread_fn,
71                        cpu, QEMU_THREAD_JOINABLE);
72 }
73 
74 /*
75  * Abort the call to run the virtual processor by another thread, and to
76  * return the control to that thread.
77  */
nvmm_kick_vcpu_thread(CPUState * cpu)78 static void nvmm_kick_vcpu_thread(CPUState *cpu)
79 {
80     cpu->exit_request = 1;
81     cpus_kick_thread(cpu);
82 }
83 
nvmm_accel_ops_class_init(ObjectClass * oc,const void * data)84 static void nvmm_accel_ops_class_init(ObjectClass *oc, const void *data)
85 {
86     AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
87 
88     ops->create_vcpu_thread = nvmm_start_vcpu_thread;
89     ops->kick_vcpu_thread = nvmm_kick_vcpu_thread;
90 
91     ops->synchronize_post_reset = nvmm_cpu_synchronize_post_reset;
92     ops->synchronize_post_init = nvmm_cpu_synchronize_post_init;
93     ops->synchronize_state = nvmm_cpu_synchronize_state;
94     ops->synchronize_pre_loadvm = nvmm_cpu_synchronize_pre_loadvm;
95 }
96 
97 static const TypeInfo nvmm_accel_ops_type = {
98     .name = ACCEL_OPS_NAME("nvmm"),
99 
100     .parent = TYPE_ACCEL_OPS,
101     .class_init = nvmm_accel_ops_class_init,
102     .abstract = true,
103 };
104 
nvmm_accel_ops_register_types(void)105 static void nvmm_accel_ops_register_types(void)
106 {
107     type_register_static(&nvmm_accel_ops_type);
108 }
109 type_init(nvmm_accel_ops_register_types);
110