master
c 108 lines 2.79 KB
Raw
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 "accel/accel-cpu-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
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 qemu_process_cpu_events(cpu);
46
47 if (cpu_can_run(cpu)) {
48 r = nvmm_vcpu_exec(cpu);
49 if (r == EXCP_DEBUG) {
50 cpu_handle_guest_debug(cpu);
51 }
52 }
53 } while (!cpu->unplug || cpu_can_run(cpu));
54
55 nvmm_destroy_vcpu(cpu);
56 cpu_thread_signal_destroyed(cpu);
57 bql_unlock();
58 rcu_unregister_thread();
59 return NULL;
60 }
61
62 static void nvmm_start_vcpu_thread(CPUState *cpu)
63 {
64 char thread_name[VCPU_THREAD_NAME_SIZE];
65
66 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/NVMM",
67 cpu->cpu_index);
68 qemu_thread_create(cpu->thread, thread_name, qemu_nvmm_cpu_thread_fn,
69 cpu, QEMU_THREAD_JOINABLE);
70 }
71
72 /*
73 * Abort the call to run the virtual processor by another thread, and to
74 * return the control to that thread.
75 */
76 static void nvmm_kick_vcpu_thread(CPUState *cpu)
77 {
78 qatomic_set(&cpu->exit_request, true);
79 cpus_kick_thread(cpu);
80 }
81
82 static void nvmm_accel_ops_class_init(ObjectClass *oc, const void *data)
83 {
84 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
85
86 ops->create_vcpu_thread = nvmm_start_vcpu_thread;
87 ops->kick_vcpu_thread = nvmm_kick_vcpu_thread;
88 ops->handle_interrupt = generic_handle_interrupt;
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);