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