]> git.proxmox.com Git - mirror_qemu.git/blob - accel/tcg/tcg-accel-ops-rr.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / accel / tcg / tcg-accel-ops-rr.c
1 /*
2 * QEMU TCG Single Threaded vCPUs implementation
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2014 Red Hat Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
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/guest-random.h"
32 #include "exec/exec-all.h"
33 #include "hw/boards.h"
34
35 #include "tcg-accel-ops.h"
36 #include "tcg-accel-ops-rr.h"
37 #include "tcg-accel-ops-icount.h"
38
39 /* Kick all RR vCPUs */
40 void rr_kick_vcpu_thread(CPUState *unused)
41 {
42 CPUState *cpu;
43
44 CPU_FOREACH(cpu) {
45 cpu_exit(cpu);
46 };
47 }
48
49 /*
50 * TCG vCPU kick timer
51 *
52 * The kick timer is responsible for moving single threaded vCPU
53 * emulation on to the next vCPU. If more than one vCPU is running a
54 * timer event with force a cpu->exit so the next vCPU can get
55 * scheduled.
56 *
57 * The timer is removed if all vCPUs are idle and restarted again once
58 * idleness is complete.
59 */
60
61 static QEMUTimer *rr_kick_vcpu_timer;
62 static CPUState *rr_current_cpu;
63
64 #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
65
66 static inline int64_t rr_next_kick_time(void)
67 {
68 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
69 }
70
71 /* Kick the currently round-robin scheduled vCPU to next */
72 static void rr_kick_next_cpu(void)
73 {
74 CPUState *cpu;
75 do {
76 cpu = qatomic_mb_read(&rr_current_cpu);
77 if (cpu) {
78 cpu_exit(cpu);
79 }
80 } while (cpu != qatomic_mb_read(&rr_current_cpu));
81 }
82
83 static void rr_kick_thread(void *opaque)
84 {
85 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
86 rr_kick_next_cpu();
87 }
88
89 static void rr_start_kick_timer(void)
90 {
91 if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
92 rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
93 rr_kick_thread, NULL);
94 }
95 if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) {
96 timer_mod(rr_kick_vcpu_timer, rr_next_kick_time());
97 }
98 }
99
100 static void rr_stop_kick_timer(void)
101 {
102 if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) {
103 timer_del(rr_kick_vcpu_timer);
104 }
105 }
106
107 static void rr_wait_io_event(void)
108 {
109 CPUState *cpu;
110
111 while (all_cpu_threads_idle()) {
112 rr_stop_kick_timer();
113 qemu_cond_wait_iothread(first_cpu->halt_cond);
114 }
115
116 rr_start_kick_timer();
117
118 CPU_FOREACH(cpu) {
119 qemu_wait_io_event_common(cpu);
120 }
121 }
122
123 /*
124 * Destroy any remaining vCPUs which have been unplugged and have
125 * finished running
126 */
127 static void rr_deal_with_unplugged_cpus(void)
128 {
129 CPUState *cpu;
130
131 CPU_FOREACH(cpu) {
132 if (cpu->unplug && !cpu_can_run(cpu)) {
133 tcg_cpus_destroy(cpu);
134 break;
135 }
136 }
137 }
138
139 /*
140 * In the single-threaded case each vCPU is simulated in turn. If
141 * there is more than a single vCPU we create a simple timer to kick
142 * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
143 * This is done explicitly rather than relying on side-effects
144 * elsewhere.
145 */
146
147 static void *rr_cpu_thread_fn(void *arg)
148 {
149 CPUState *cpu = arg;
150
151 assert(tcg_enabled());
152 rcu_register_thread();
153 tcg_register_thread();
154
155 qemu_mutex_lock_iothread();
156 qemu_thread_get_self(cpu->thread);
157
158 cpu->thread_id = qemu_get_thread_id();
159 cpu->can_do_io = 1;
160 cpu_thread_signal_created(cpu);
161 qemu_guest_random_seed_thread_part2(cpu->random_seed);
162
163 /* wait for initial kick-off after machine start */
164 while (first_cpu->stopped) {
165 qemu_cond_wait_iothread(first_cpu->halt_cond);
166
167 /* process any pending work */
168 CPU_FOREACH(cpu) {
169 current_cpu = cpu;
170 qemu_wait_io_event_common(cpu);
171 }
172 }
173
174 rr_start_kick_timer();
175
176 cpu = first_cpu;
177
178 /* process any pending work */
179 cpu->exit_request = 1;
180
181 while (1) {
182 qemu_mutex_unlock_iothread();
183 replay_mutex_lock();
184 qemu_mutex_lock_iothread();
185
186 if (icount_enabled()) {
187 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
188 icount_account_warp_timer();
189 /*
190 * Run the timers here. This is much more efficient than
191 * waking up the I/O thread and waiting for completion.
192 */
193 icount_handle_deadline();
194 }
195
196 replay_mutex_unlock();
197
198 if (!cpu) {
199 cpu = first_cpu;
200 }
201
202 while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
203
204 qatomic_mb_set(&rr_current_cpu, cpu);
205 current_cpu = cpu;
206
207 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
208 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
209
210 if (cpu_can_run(cpu)) {
211 int r;
212
213 qemu_mutex_unlock_iothread();
214 if (icount_enabled()) {
215 icount_prepare_for_run(cpu);
216 }
217 r = tcg_cpus_exec(cpu);
218 if (icount_enabled()) {
219 icount_process_data(cpu);
220 }
221 qemu_mutex_lock_iothread();
222
223 if (r == EXCP_DEBUG) {
224 cpu_handle_guest_debug(cpu);
225 break;
226 } else if (r == EXCP_ATOMIC) {
227 qemu_mutex_unlock_iothread();
228 cpu_exec_step_atomic(cpu);
229 qemu_mutex_lock_iothread();
230 break;
231 }
232 } else if (cpu->stop) {
233 if (cpu->unplug) {
234 cpu = CPU_NEXT(cpu);
235 }
236 break;
237 }
238
239 cpu = CPU_NEXT(cpu);
240 } /* while (cpu && !cpu->exit_request).. */
241
242 /* Does not need qatomic_mb_set because a spurious wakeup is okay. */
243 qatomic_set(&rr_current_cpu, NULL);
244
245 if (cpu && cpu->exit_request) {
246 qatomic_mb_set(&cpu->exit_request, 0);
247 }
248
249 if (icount_enabled() && all_cpu_threads_idle()) {
250 /*
251 * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
252 * in the main_loop, wake it up in order to start the warp timer.
253 */
254 qemu_notify_event();
255 }
256
257 rr_wait_io_event();
258 rr_deal_with_unplugged_cpus();
259 }
260
261 rcu_unregister_thread();
262 return NULL;
263 }
264
265 void rr_start_vcpu_thread(CPUState *cpu)
266 {
267 char thread_name[VCPU_THREAD_NAME_SIZE];
268 static QemuCond *single_tcg_halt_cond;
269 static QemuThread *single_tcg_cpu_thread;
270
271 g_assert(tcg_enabled());
272 tcg_cpu_init_cflags(cpu, false);
273
274 if (!single_tcg_cpu_thread) {
275 cpu->thread = g_malloc0(sizeof(QemuThread));
276 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
277 qemu_cond_init(cpu->halt_cond);
278
279 /* share a single thread for all cpus with TCG */
280 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
281 qemu_thread_create(cpu->thread, thread_name,
282 rr_cpu_thread_fn,
283 cpu, QEMU_THREAD_JOINABLE);
284
285 single_tcg_halt_cond = cpu->halt_cond;
286 single_tcg_cpu_thread = cpu->thread;
287 #ifdef _WIN32
288 cpu->hThread = qemu_thread_get_handle(cpu->thread);
289 #endif
290 } else {
291 /* we share the thread */
292 cpu->thread = single_tcg_cpu_thread;
293 cpu->halt_cond = single_tcg_halt_cond;
294 cpu->thread_id = first_cpu->thread_id;
295 cpu->can_do_io = 1;
296 cpu->created = true;
297 }
298 }