1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include "qemu-common.h"
28#include "sysemu/tcg.h"
29#include "sysemu/replay.h"
30#include "qemu/main-loop.h"
31#include "qemu/notify.h"
32#include "qemu/guest-random.h"
33#include "exec/exec-all.h"
34#include "hw/boards.h"
35
36#include "tcg-accel-ops.h"
37#include "tcg-accel-ops-mttcg.h"
38
39typedef struct MttcgForceRcuNotifier {
40 Notifier notifier;
41 CPUState *cpu;
42} MttcgForceRcuNotifier;
43
44static void do_nothing(CPUState *cpu, run_on_cpu_data d)
45{
46}
47
48static void mttcg_force_rcu(Notifier *notify, void *data)
49{
50 CPUState *cpu = container_of(notify, MttcgForceRcuNotifier, notifier)->cpu;
51
52
53
54
55
56 async_run_on_cpu(cpu, do_nothing, RUN_ON_CPU_NULL);
57}
58
59
60
61
62
63
64
65static void *mttcg_cpu_thread_fn(void *arg)
66{
67 MttcgForceRcuNotifier force_rcu;
68 CPUState *cpu = arg;
69
70 assert(tcg_enabled());
71 g_assert(!icount_enabled());
72
73 rcu_register_thread();
74 force_rcu.notifier.notify = mttcg_force_rcu;
75 force_rcu.cpu = cpu;
76 rcu_add_force_rcu_notifier(&force_rcu.notifier);
77 tcg_register_thread();
78
79 qemu_mutex_lock_iothread();
80 qemu_thread_get_self(cpu->thread);
81
82 cpu->thread_id = qemu_get_thread_id();
83 cpu->can_do_io = 1;
84 current_cpu = cpu;
85 cpu_thread_signal_created(cpu);
86 qemu_guest_random_seed_thread_part2(cpu->random_seed);
87
88
89 cpu->exit_request = 1;
90
91 do {
92 if (cpu_can_run(cpu)) {
93 int r;
94 qemu_mutex_unlock_iothread();
95 r = tcg_cpus_exec(cpu);
96 qemu_mutex_lock_iothread();
97 switch (r) {
98 case EXCP_DEBUG:
99 cpu_handle_guest_debug(cpu);
100 break;
101 case EXCP_HALTED:
102
103
104
105
106
107
108
109
110 g_assert(cpu->halted);
111 break;
112 case EXCP_ATOMIC:
113 qemu_mutex_unlock_iothread();
114 cpu_exec_step_atomic(cpu);
115 qemu_mutex_lock_iothread();
116 default:
117
118 break;
119 }
120 }
121
122 qatomic_mb_set(&cpu->exit_request, 0);
123 qemu_wait_io_event(cpu);
124 } while (!cpu->unplug || cpu_can_run(cpu));
125
126 tcg_cpus_destroy(cpu);
127 qemu_mutex_unlock_iothread();
128 rcu_remove_force_rcu_notifier(&force_rcu.notifier);
129 rcu_unregister_thread();
130 return NULL;
131}
132
133void mttcg_kick_vcpu_thread(CPUState *cpu)
134{
135 cpu_exit(cpu);
136}
137
138void mttcg_start_vcpu_thread(CPUState *cpu)
139{
140 char thread_name[VCPU_THREAD_NAME_SIZE];
141
142 g_assert(tcg_enabled());
143 tcg_cpu_init_cflags(cpu, current_machine->smp.max_cpus > 1);
144
145 cpu->thread = g_malloc0(sizeof(QemuThread));
146 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
147 qemu_cond_init(cpu->halt_cond);
148
149
150 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
151 cpu->cpu_index);
152
153 qemu_thread_create(cpu->thread, thread_name, mttcg_cpu_thread_fn,
154 cpu, QEMU_THREAD_JOINABLE);
155
156#ifdef _WIN32
157 cpu->hThread = qemu_thread_get_handle(cpu->thread);
158#endif
159}
160