]>
Commit | Line | Data |
---|---|---|
45e077d7 CF |
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" | |
83ecdb18 | 27 | #include "qemu/lockable.h" |
45e077d7 CF |
28 | #include "sysemu/tcg.h" |
29 | #include "sysemu/replay.h" | |
03ff4f8d | 30 | #include "sysemu/cpu-timers.h" |
45e077d7 | 31 | #include "qemu/main-loop.h" |
dd47a8f6 | 32 | #include "qemu/notify.h" |
45e077d7 CF |
33 | #include "qemu/guest-random.h" |
34 | #include "exec/exec-all.h" | |
d7ec12f8 | 35 | #include "tcg/startup.h" |
b86f59c7 CF |
36 | #include "tcg-accel-ops.h" |
37 | #include "tcg-accel-ops-rr.h" | |
38 | #include "tcg-accel-ops-icount.h" | |
45e077d7 CF |
39 | |
40 | /* Kick all RR vCPUs */ | |
9e2658d6 | 41 | void rr_kick_vcpu_thread(CPUState *unused) |
45e077d7 CF |
42 | { |
43 | CPUState *cpu; | |
44 | ||
45 | CPU_FOREACH(cpu) { | |
46 | cpu_exit(cpu); | |
47 | }; | |
48 | } | |
49 | ||
50 | /* | |
51 | * TCG vCPU kick timer | |
52 | * | |
53 | * The kick timer is responsible for moving single threaded vCPU | |
54 | * emulation on to the next vCPU. If more than one vCPU is running a | |
046ab3b8 | 55 | * timer event we force a cpu->exit so the next vCPU can get |
45e077d7 CF |
56 | * scheduled. |
57 | * | |
58 | * The timer is removed if all vCPUs are idle and restarted again once | |
59 | * idleness is complete. | |
60 | */ | |
61 | ||
9e2658d6 CF |
62 | static QEMUTimer *rr_kick_vcpu_timer; |
63 | static CPUState *rr_current_cpu; | |
45e077d7 | 64 | |
9e2658d6 | 65 | static inline int64_t rr_next_kick_time(void) |
45e077d7 CF |
66 | { |
67 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD; | |
68 | } | |
69 | ||
70 | /* Kick the currently round-robin scheduled vCPU to next */ | |
9e2658d6 | 71 | static void rr_kick_next_cpu(void) |
45e077d7 CF |
72 | { |
73 | CPUState *cpu; | |
74 | do { | |
2e739529 | 75 | cpu = qatomic_read(&rr_current_cpu); |
45e077d7 CF |
76 | if (cpu) { |
77 | cpu_exit(cpu); | |
78 | } | |
2e739529 PB |
79 | /* Finish kicking this cpu before reading again. */ |
80 | smp_mb(); | |
81 | } while (cpu != qatomic_read(&rr_current_cpu)); | |
45e077d7 CF |
82 | } |
83 | ||
9e2658d6 | 84 | static void rr_kick_thread(void *opaque) |
45e077d7 | 85 | { |
9e2658d6 CF |
86 | timer_mod(rr_kick_vcpu_timer, rr_next_kick_time()); |
87 | rr_kick_next_cpu(); | |
45e077d7 CF |
88 | } |
89 | ||
9e2658d6 | 90 | static void rr_start_kick_timer(void) |
45e077d7 | 91 | { |
9e2658d6 CF |
92 | if (!rr_kick_vcpu_timer && CPU_NEXT(first_cpu)) { |
93 | rr_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, | |
94 | rr_kick_thread, NULL); | |
45e077d7 | 95 | } |
9e2658d6 CF |
96 | if (rr_kick_vcpu_timer && !timer_pending(rr_kick_vcpu_timer)) { |
97 | timer_mod(rr_kick_vcpu_timer, rr_next_kick_time()); | |
45e077d7 CF |
98 | } |
99 | } | |
100 | ||
9e2658d6 | 101 | static void rr_stop_kick_timer(void) |
45e077d7 | 102 | { |
9e2658d6 CF |
103 | if (rr_kick_vcpu_timer && timer_pending(rr_kick_vcpu_timer)) { |
104 | timer_del(rr_kick_vcpu_timer); | |
45e077d7 CF |
105 | } |
106 | } | |
107 | ||
9e2658d6 | 108 | static void rr_wait_io_event(void) |
45e077d7 CF |
109 | { |
110 | CPUState *cpu; | |
111 | ||
112 | while (all_cpu_threads_idle()) { | |
9e2658d6 | 113 | rr_stop_kick_timer(); |
7c754c78 | 114 | qemu_cond_wait_bql(first_cpu->halt_cond); |
45e077d7 CF |
115 | } |
116 | ||
9e2658d6 | 117 | rr_start_kick_timer(); |
45e077d7 CF |
118 | |
119 | CPU_FOREACH(cpu) { | |
120 | qemu_wait_io_event_common(cpu); | |
121 | } | |
122 | } | |
123 | ||
124 | /* | |
125 | * Destroy any remaining vCPUs which have been unplugged and have | |
126 | * finished running | |
127 | */ | |
9e2658d6 | 128 | static void rr_deal_with_unplugged_cpus(void) |
45e077d7 CF |
129 | { |
130 | CPUState *cpu; | |
131 | ||
132 | CPU_FOREACH(cpu) { | |
133 | if (cpu->unplug && !cpu_can_run(cpu)) { | |
9e2658d6 | 134 | tcg_cpus_destroy(cpu); |
45e077d7 CF |
135 | break; |
136 | } | |
137 | } | |
138 | } | |
139 | ||
dd47a8f6 GK |
140 | static void rr_force_rcu(Notifier *notify, void *data) |
141 | { | |
142 | rr_kick_next_cpu(); | |
143 | } | |
144 | ||
83ecdb18 JI |
145 | /* |
146 | * Calculate the number of CPUs that we will process in a single iteration of | |
147 | * the main CPU thread loop so that we can fairly distribute the instruction | |
148 | * count across CPUs. | |
149 | * | |
150 | * The CPU count is cached based on the CPU list generation ID to avoid | |
151 | * iterating the list every time. | |
152 | */ | |
153 | static int rr_cpu_count(void) | |
154 | { | |
155 | static unsigned int last_gen_id = ~0; | |
156 | static int cpu_count; | |
157 | CPUState *cpu; | |
158 | ||
159 | QEMU_LOCK_GUARD(&qemu_cpu_list_lock); | |
160 | ||
161 | if (cpu_list_generation_id_get() != last_gen_id) { | |
162 | cpu_count = 0; | |
163 | CPU_FOREACH(cpu) { | |
164 | ++cpu_count; | |
165 | } | |
166 | last_gen_id = cpu_list_generation_id_get(); | |
167 | } | |
168 | ||
169 | return cpu_count; | |
170 | } | |
171 | ||
45e077d7 CF |
172 | /* |
173 | * In the single-threaded case each vCPU is simulated in turn. If | |
174 | * there is more than a single vCPU we create a simple timer to kick | |
175 | * the vCPU and ensure we don't get stuck in a tight loop in one vCPU. | |
176 | * This is done explicitly rather than relying on side-effects | |
177 | * elsewhere. | |
178 | */ | |
179 | ||
9e2658d6 | 180 | static void *rr_cpu_thread_fn(void *arg) |
45e077d7 | 181 | { |
dd47a8f6 | 182 | Notifier force_rcu; |
45e077d7 CF |
183 | CPUState *cpu = arg; |
184 | ||
0585105c | 185 | assert(tcg_enabled()); |
45e077d7 | 186 | rcu_register_thread(); |
dd47a8f6 GK |
187 | force_rcu.notify = rr_force_rcu; |
188 | rcu_add_force_rcu_notifier(&force_rcu); | |
45e077d7 CF |
189 | tcg_register_thread(); |
190 | ||
195801d7 | 191 | bql_lock(); |
45e077d7 CF |
192 | qemu_thread_get_self(cpu->thread); |
193 | ||
194 | cpu->thread_id = qemu_get_thread_id(); | |
464dacf6 | 195 | cpu->neg.can_do_io = true; |
45e077d7 CF |
196 | cpu_thread_signal_created(cpu); |
197 | qemu_guest_random_seed_thread_part2(cpu->random_seed); | |
198 | ||
199 | /* wait for initial kick-off after machine start */ | |
200 | while (first_cpu->stopped) { | |
7c754c78 | 201 | qemu_cond_wait_bql(first_cpu->halt_cond); |
45e077d7 CF |
202 | |
203 | /* process any pending work */ | |
204 | CPU_FOREACH(cpu) { | |
205 | current_cpu = cpu; | |
206 | qemu_wait_io_event_common(cpu); | |
207 | } | |
208 | } | |
209 | ||
9e2658d6 | 210 | rr_start_kick_timer(); |
45e077d7 CF |
211 | |
212 | cpu = first_cpu; | |
213 | ||
214 | /* process any pending work */ | |
215 | cpu->exit_request = 1; | |
216 | ||
217 | while (1) { | |
83ecdb18 JI |
218 | /* Only used for icount_enabled() */ |
219 | int64_t cpu_budget = 0; | |
220 | ||
195801d7 | 221 | bql_unlock(); |
45e077d7 | 222 | replay_mutex_lock(); |
195801d7 | 223 | bql_lock(); |
45e077d7 CF |
224 | |
225 | if (icount_enabled()) { | |
83ecdb18 JI |
226 | int cpu_count = rr_cpu_count(); |
227 | ||
45e077d7 CF |
228 | /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ |
229 | icount_account_warp_timer(); | |
230 | /* | |
231 | * Run the timers here. This is much more efficient than | |
232 | * waking up the I/O thread and waiting for completion. | |
233 | */ | |
9e2658d6 | 234 | icount_handle_deadline(); |
83ecdb18 JI |
235 | |
236 | cpu_budget = icount_percpu_budget(cpu_count); | |
45e077d7 CF |
237 | } |
238 | ||
239 | replay_mutex_unlock(); | |
240 | ||
241 | if (!cpu) { | |
242 | cpu = first_cpu; | |
243 | } | |
244 | ||
245 | while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) { | |
2e739529 | 246 | /* Store rr_current_cpu before evaluating cpu_can_run(). */ |
06831001 | 247 | qatomic_set_mb(&rr_current_cpu, cpu); |
2e739529 | 248 | |
45e077d7 CF |
249 | current_cpu = cpu; |
250 | ||
251 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, | |
252 | (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); | |
253 | ||
254 | if (cpu_can_run(cpu)) { | |
255 | int r; | |
256 | ||
195801d7 | 257 | bql_unlock(); |
45e077d7 | 258 | if (icount_enabled()) { |
83ecdb18 | 259 | icount_prepare_for_run(cpu, cpu_budget); |
45e077d7 | 260 | } |
9e2658d6 | 261 | r = tcg_cpus_exec(cpu); |
45e077d7 | 262 | if (icount_enabled()) { |
9e2658d6 | 263 | icount_process_data(cpu); |
45e077d7 | 264 | } |
195801d7 | 265 | bql_lock(); |
45e077d7 CF |
266 | |
267 | if (r == EXCP_DEBUG) { | |
268 | cpu_handle_guest_debug(cpu); | |
269 | break; | |
270 | } else if (r == EXCP_ATOMIC) { | |
195801d7 | 271 | bql_unlock(); |
45e077d7 | 272 | cpu_exec_step_atomic(cpu); |
195801d7 | 273 | bql_lock(); |
45e077d7 CF |
274 | break; |
275 | } | |
276 | } else if (cpu->stop) { | |
277 | if (cpu->unplug) { | |
278 | cpu = CPU_NEXT(cpu); | |
279 | } | |
280 | break; | |
281 | } | |
282 | ||
283 | cpu = CPU_NEXT(cpu); | |
284 | } /* while (cpu && !cpu->exit_request).. */ | |
285 | ||
2e739529 | 286 | /* Does not need a memory barrier because a spurious wakeup is okay. */ |
9e2658d6 | 287 | qatomic_set(&rr_current_cpu, NULL); |
45e077d7 CF |
288 | |
289 | if (cpu && cpu->exit_request) { | |
06831001 | 290 | qatomic_set_mb(&cpu->exit_request, 0); |
45e077d7 CF |
291 | } |
292 | ||
293 | if (icount_enabled() && all_cpu_threads_idle()) { | |
294 | /* | |
295 | * When all cpus are sleeping (e.g in WFI), to avoid a deadlock | |
296 | * in the main_loop, wake it up in order to start the warp timer. | |
297 | */ | |
298 | qemu_notify_event(); | |
299 | } | |
300 | ||
9e2658d6 CF |
301 | rr_wait_io_event(); |
302 | rr_deal_with_unplugged_cpus(); | |
45e077d7 CF |
303 | } |
304 | ||
dd47a8f6 | 305 | rcu_remove_force_rcu_notifier(&force_rcu); |
45e077d7 CF |
306 | rcu_unregister_thread(); |
307 | return NULL; | |
308 | } | |
309 | ||
37c2f9a7 CF |
310 | void rr_start_vcpu_thread(CPUState *cpu) |
311 | { | |
312 | char thread_name[VCPU_THREAD_NAME_SIZE]; | |
313 | static QemuCond *single_tcg_halt_cond; | |
314 | static QemuThread *single_tcg_cpu_thread; | |
315 | ||
0585105c PM |
316 | g_assert(tcg_enabled()); |
317 | tcg_cpu_init_cflags(cpu, false); | |
318 | ||
37c2f9a7 | 319 | if (!single_tcg_cpu_thread) { |
b21e2380 MA |
320 | cpu->thread = g_new0(QemuThread, 1); |
321 | cpu->halt_cond = g_new0(QemuCond, 1); | |
37c2f9a7 CF |
322 | qemu_cond_init(cpu->halt_cond); |
323 | ||
324 | /* share a single thread for all cpus with TCG */ | |
325 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG"); | |
326 | qemu_thread_create(cpu->thread, thread_name, | |
9e2658d6 | 327 | rr_cpu_thread_fn, |
37c2f9a7 CF |
328 | cpu, QEMU_THREAD_JOINABLE); |
329 | ||
330 | single_tcg_halt_cond = cpu->halt_cond; | |
331 | single_tcg_cpu_thread = cpu->thread; | |
37c2f9a7 CF |
332 | } else { |
333 | /* we share the thread */ | |
334 | cpu->thread = single_tcg_cpu_thread; | |
335 | cpu->halt_cond = single_tcg_halt_cond; | |
336 | cpu->thread_id = first_cpu->thread_id; | |
464dacf6 | 337 | cpu->neg.can_do_io = 1; |
37c2f9a7 CF |
338 | cpu->created = true; |
339 | } | |
340 | } |