Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
Peter Maydell | 7b31bbc | 2016-01-26 18:16:56 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 26 | #include "qemu/config-file.h" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 27 | #include "cpu.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 28 | #include "monitor/monitor.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 30 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 31 | #include "qemu/error-report.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 32 | #include "sysemu/sysemu.h" |
Max Reitz | da31d59 | 2016-03-16 19:54:32 +0100 | [diff] [blame] | 33 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 34 | #include "exec/gdbstub.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/dma.h" |
Vincent Palatin | b394662 | 2017-01-10 11:59:55 +0100 | [diff] [blame] | 36 | #include "sysemu/hw_accel.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 37 | #include "sysemu/kvm.h" |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 38 | #include "sysemu/hax.h" |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 39 | #include "sysemu/hvf.h" |
Justin Terry (VM) | 1930680 | 2018-01-22 13:07:49 -0800 | [diff] [blame] | 40 | #include "sysemu/whpx.h" |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 41 | #include "qmp-commands.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 42 | #include "exec/exec-all.h" |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 43 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 44 | #include "qemu/thread.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 45 | #include "sysemu/cpus.h" |
| 46 | #include "sysemu/qtest.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 47 | #include "qemu/main-loop.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 48 | #include "qemu/option.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 49 | #include "qemu/bitmap.h" |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 50 | #include "qemu/seqlock.h" |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 51 | #include "tcg.h" |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 52 | #include "qapi-event.h" |
Alexey Kardashevskiy | 9cb805f | 2014-08-20 22:16:33 +1000 | [diff] [blame] | 53 | #include "hw/nmi.h" |
Pavel Dovgalyuk | 8b42704 | 2015-09-17 19:24:05 +0300 | [diff] [blame] | 54 | #include "sysemu/replay.h" |
Igor Mammedov | afed5a5 | 2017-05-10 13:29:55 +0200 | [diff] [blame] | 55 | #include "hw/boards.h" |
Jan Kiszka | 0ff0fc1 | 2011-06-23 10:15:55 +0200 | [diff] [blame] | 56 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_LINUX |
| 58 | |
| 59 | #include <sys/prctl.h> |
| 60 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 61 | #ifndef PR_MCE_KILL |
| 62 | #define PR_MCE_KILL 33 |
| 63 | #endif |
| 64 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 65 | #ifndef PR_MCE_KILL_SET |
| 66 | #define PR_MCE_KILL_SET 1 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef PR_MCE_KILL_EARLY |
| 70 | #define PR_MCE_KILL_EARLY 1 |
| 71 | #endif |
| 72 | |
| 73 | #endif /* CONFIG_LINUX */ |
| 74 | |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 75 | int64_t max_delay; |
| 76 | int64_t max_advance; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 77 | |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 78 | /* vcpu throttling controls */ |
| 79 | static QEMUTimer *throttle_timer; |
| 80 | static unsigned int throttle_percentage; |
| 81 | |
| 82 | #define CPU_THROTTLE_PCT_MIN 1 |
| 83 | #define CPU_THROTTLE_PCT_MAX 99 |
| 84 | #define CPU_THROTTLE_TIMESLICE_NS 10000000 |
| 85 | |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 86 | bool cpu_is_stopped(CPUState *cpu) |
| 87 | { |
| 88 | return cpu->stopped || !runstate_is_running(); |
| 89 | } |
| 90 | |
Andreas Färber | a98ae1d | 2013-05-26 23:21:08 +0200 | [diff] [blame] | 91 | static bool cpu_thread_is_idle(CPUState *cpu) |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 92 | { |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 93 | if (cpu->stop || cpu->queued_work_first) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 94 | return false; |
| 95 | } |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 96 | if (cpu_is_stopped(cpu)) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 97 | return true; |
| 98 | } |
Andreas Färber | 8c2e1b0 | 2013-08-25 18:53:55 +0200 | [diff] [blame] | 99 | if (!cpu->halted || cpu_has_work(cpu) || |
Alexander Graf | 215e79c | 2013-04-24 22:24:12 +0200 | [diff] [blame] | 100 | kvm_halt_in_kernel()) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static bool all_cpu_threads_idle(void) |
| 107 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 108 | CPUState *cpu; |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 109 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 110 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 111 | if (!cpu_thread_is_idle(cpu)) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 118 | /***********************************************************/ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 119 | /* guest cycle counter */ |
| 120 | |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 121 | /* Protected by TimersState seqlock */ |
| 122 | |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 123 | static bool icount_sleep = true; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 124 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
| 125 | static int icount_time_shift; |
| 126 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
| 127 | #define MAX_ICOUNT_SHIFT 10 |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 128 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 129 | typedef struct TimersState { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 130 | /* Protected by BQL. */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 131 | int64_t cpu_ticks_prev; |
| 132 | int64_t cpu_ticks_offset; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 133 | |
| 134 | /* cpu_clock_offset can be read out of BQL, so protect it with |
| 135 | * this lock. |
| 136 | */ |
| 137 | QemuSeqLock vm_clock_seqlock; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 138 | int64_t cpu_clock_offset; |
| 139 | int32_t cpu_ticks_enabled; |
| 140 | int64_t dummy; |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 141 | |
| 142 | /* Compensate for varying guest execution speed. */ |
| 143 | int64_t qemu_icount_bias; |
| 144 | /* Only written by TCG thread */ |
| 145 | int64_t qemu_icount; |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 146 | /* for adjusting icount */ |
| 147 | int64_t vm_clock_warp_start; |
| 148 | QEMUTimer *icount_rt_timer; |
| 149 | QEMUTimer *icount_vm_timer; |
| 150 | QEMUTimer *icount_warp_timer; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 151 | } TimersState; |
| 152 | |
Liu Ping Fan | d9cd400 | 2013-07-21 08:43:00 +0000 | [diff] [blame] | 153 | static TimersState timers_state; |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 154 | bool mttcg_enabled; |
| 155 | |
| 156 | /* |
| 157 | * We default to false if we know other options have been enabled |
| 158 | * which are currently incompatible with MTTCG. Otherwise when each |
| 159 | * guest (target) has been updated to support: |
| 160 | * - atomic instructions |
| 161 | * - memory ordering primitives (barriers) |
| 162 | * they can set the appropriate CONFIG flags in ${target}-softmmu.mak |
| 163 | * |
| 164 | * Once a guest architecture has been converted to the new primitives |
| 165 | * there are two remaining limitations to check. |
| 166 | * |
| 167 | * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host) |
| 168 | * - The host must have a stronger memory order than the guest |
| 169 | * |
| 170 | * It may be possible in future to support strong guests on weak hosts |
| 171 | * but that will require tagging all load/stores in a guest with their |
| 172 | * implicit memory order requirements which would likely slow things |
| 173 | * down a lot. |
| 174 | */ |
| 175 | |
| 176 | static bool check_tcg_memory_orders_compatible(void) |
| 177 | { |
| 178 | #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO) |
| 179 | return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0; |
| 180 | #else |
| 181 | return false; |
| 182 | #endif |
| 183 | } |
| 184 | |
| 185 | static bool default_mttcg_enabled(void) |
| 186 | { |
Alex Bennée | 83fd962 | 2017-02-27 17:09:01 +0000 | [diff] [blame] | 187 | if (use_icount || TCG_OVERSIZED_GUEST) { |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } else { |
| 190 | #ifdef TARGET_SUPPORTS_MTTCG |
| 191 | return check_tcg_memory_orders_compatible(); |
| 192 | #else |
| 193 | return false; |
| 194 | #endif |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void qemu_tcg_configure(QemuOpts *opts, Error **errp) |
| 199 | { |
| 200 | const char *t = qemu_opt_get(opts, "thread"); |
| 201 | if (t) { |
| 202 | if (strcmp(t, "multi") == 0) { |
| 203 | if (TCG_OVERSIZED_GUEST) { |
| 204 | error_setg(errp, "No MTTCG when guest word size > hosts"); |
Alex Bennée | 83fd962 | 2017-02-27 17:09:01 +0000 | [diff] [blame] | 205 | } else if (use_icount) { |
| 206 | error_setg(errp, "No MTTCG when icount is enabled"); |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 207 | } else { |
Nikunj A Dadhania | 8695350 | 2017-04-10 11:36:55 +0530 | [diff] [blame] | 208 | #ifndef TARGET_SUPPORTS_MTTCG |
Alex Bennée | c34c762 | 2017-02-28 14:40:17 +0000 | [diff] [blame] | 209 | error_report("Guest not yet converted to MTTCG - " |
| 210 | "you may get unexpected results"); |
| 211 | #endif |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 212 | if (!check_tcg_memory_orders_compatible()) { |
| 213 | error_report("Guest expects a stronger memory ordering " |
| 214 | "than the host provides"); |
Pranith Kumar | 8cfef89 | 2017-03-25 16:19:23 -0400 | [diff] [blame] | 215 | error_printf("This may cause strange/hard to debug errors\n"); |
KONRAD Frederic | 8d4e914 | 2017-02-23 18:29:08 +0000 | [diff] [blame] | 216 | } |
| 217 | mttcg_enabled = true; |
| 218 | } |
| 219 | } else if (strcmp(t, "single") == 0) { |
| 220 | mttcg_enabled = false; |
| 221 | } else { |
| 222 | error_setg(errp, "Invalid 'thread' setting %s", t); |
| 223 | } |
| 224 | } else { |
| 225 | mttcg_enabled = default_mttcg_enabled(); |
| 226 | } |
| 227 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 228 | |
Alex Bennée | e4cd965 | 2017-03-31 16:09:42 +0100 | [diff] [blame] | 229 | /* The current number of executed instructions is based on what we |
| 230 | * originally budgeted minus the current state of the decrementing |
| 231 | * icount counters in extra/u16.low. |
| 232 | */ |
| 233 | static int64_t cpu_get_icount_executed(CPUState *cpu) |
| 234 | { |
| 235 | return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra); |
| 236 | } |
| 237 | |
Alex Bennée | 512d3c8 | 2017-04-05 12:32:37 +0100 | [diff] [blame] | 238 | /* |
| 239 | * Update the global shared timer_state.qemu_icount to take into |
| 240 | * account executed instructions. This is done by the TCG vCPU |
| 241 | * thread so the main-loop can see time has moved forward. |
| 242 | */ |
| 243 | void cpu_update_icount(CPUState *cpu) |
| 244 | { |
| 245 | int64_t executed = cpu_get_icount_executed(cpu); |
| 246 | cpu->icount_budget -= executed; |
| 247 | |
| 248 | #ifdef CONFIG_ATOMIC64 |
| 249 | atomic_set__nocheck(&timers_state.qemu_icount, |
| 250 | atomic_read__nocheck(&timers_state.qemu_icount) + |
| 251 | executed); |
| 252 | #else /* FIXME: we need 64bit atomics to do this safely */ |
| 253 | timers_state.qemu_icount += executed; |
| 254 | #endif |
| 255 | } |
| 256 | |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 257 | int64_t cpu_get_icount_raw(void) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 258 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 259 | CPUState *cpu = current_cpu; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 260 | |
Alex Bennée | 243c5f7 | 2017-03-30 18:49:22 +0100 | [diff] [blame] | 261 | if (cpu && cpu->running) { |
Paolo Bonzini | 414b15c | 2015-06-24 14:16:26 +0200 | [diff] [blame] | 262 | if (!cpu->can_do_io) { |
Alistair Francis | 493d89b | 2018-02-03 09:43:14 +0100 | [diff] [blame] | 263 | error_report("Bad icount read"); |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 264 | exit(1); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 265 | } |
Alex Bennée | e4cd965 | 2017-03-31 16:09:42 +0100 | [diff] [blame] | 266 | /* Take into account what has run */ |
Alex Bennée | 1d05906 | 2017-04-05 10:53:47 +0100 | [diff] [blame] | 267 | cpu_update_icount(cpu); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 268 | } |
Alex Bennée | 1d05906 | 2017-04-05 10:53:47 +0100 | [diff] [blame] | 269 | #ifdef CONFIG_ATOMIC64 |
| 270 | return atomic_read__nocheck(&timers_state.qemu_icount); |
| 271 | #else /* FIXME: we need 64bit atomics to do this safely */ |
| 272 | return timers_state.qemu_icount; |
| 273 | #endif |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 277 | static int64_t cpu_get_icount_locked(void) |
| 278 | { |
| 279 | int64_t icount = cpu_get_icount_raw(); |
KONRAD Frederic | 3f03131 | 2014-08-01 01:37:15 +0200 | [diff] [blame] | 280 | return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 283 | int64_t cpu_get_icount(void) |
| 284 | { |
| 285 | int64_t icount; |
| 286 | unsigned start; |
| 287 | |
| 288 | do { |
| 289 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); |
| 290 | icount = cpu_get_icount_locked(); |
| 291 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); |
| 292 | |
| 293 | return icount; |
| 294 | } |
| 295 | |
KONRAD Frederic | 3f03131 | 2014-08-01 01:37:15 +0200 | [diff] [blame] | 296 | int64_t cpu_icount_to_ns(int64_t icount) |
| 297 | { |
| 298 | return icount << icount_time_shift; |
| 299 | } |
| 300 | |
Cao jin | d90f3cc | 2016-07-29 19:05:38 +0800 | [diff] [blame] | 301 | /* return the time elapsed in VM between vm_start and vm_stop. Unless |
| 302 | * icount is active, cpu_get_ticks() uses units of the host CPU cycle |
| 303 | * counter. |
| 304 | * |
| 305 | * Caller must hold the BQL |
| 306 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 307 | int64_t cpu_get_ticks(void) |
| 308 | { |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 309 | int64_t ticks; |
| 310 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 311 | if (use_icount) { |
| 312 | return cpu_get_icount(); |
| 313 | } |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 314 | |
| 315 | ticks = timers_state.cpu_ticks_offset; |
| 316 | if (timers_state.cpu_ticks_enabled) { |
Christopher Covington | 4a7428c | 2015-09-25 10:42:21 -0400 | [diff] [blame] | 317 | ticks += cpu_get_host_ticks(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 318 | } |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 319 | |
| 320 | if (timers_state.cpu_ticks_prev > ticks) { |
| 321 | /* Note: non increasing ticks may happen if the host uses |
| 322 | software suspend */ |
| 323 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
| 324 | ticks = timers_state.cpu_ticks_prev; |
| 325 | } |
| 326 | |
| 327 | timers_state.cpu_ticks_prev = ticks; |
| 328 | return ticks; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 331 | static int64_t cpu_get_clock_locked(void) |
| 332 | { |
Cao jin | 1d45cea | 2016-07-29 19:05:37 +0800 | [diff] [blame] | 333 | int64_t time; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 334 | |
Cao jin | 1d45cea | 2016-07-29 19:05:37 +0800 | [diff] [blame] | 335 | time = timers_state.cpu_clock_offset; |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 336 | if (timers_state.cpu_ticks_enabled) { |
Cao jin | 1d45cea | 2016-07-29 19:05:37 +0800 | [diff] [blame] | 337 | time += get_clock(); |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 338 | } |
| 339 | |
Cao jin | 1d45cea | 2016-07-29 19:05:37 +0800 | [diff] [blame] | 340 | return time; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Cao jin | d90f3cc | 2016-07-29 19:05:38 +0800 | [diff] [blame] | 343 | /* Return the monotonic time elapsed in VM, i.e., |
Peter Maydell | 8212ff8 | 2016-09-15 10:24:22 +0100 | [diff] [blame] | 344 | * the time between vm_start and vm_stop |
| 345 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 346 | int64_t cpu_get_clock(void) |
| 347 | { |
| 348 | int64_t ti; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 349 | unsigned start; |
| 350 | |
| 351 | do { |
| 352 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); |
| 353 | ti = cpu_get_clock_locked(); |
| 354 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); |
| 355 | |
| 356 | return ti; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 359 | /* enable cpu_get_ticks() |
Cao jin | 3224e87 | 2016-07-08 18:31:37 +0800 | [diff] [blame] | 360 | * Caller must hold BQL which serves as mutex for vm_clock_seqlock. |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 361 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 362 | void cpu_enable_ticks(void) |
| 363 | { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 364 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 365 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 366 | if (!timers_state.cpu_ticks_enabled) { |
Christopher Covington | 4a7428c | 2015-09-25 10:42:21 -0400 | [diff] [blame] | 367 | timers_state.cpu_ticks_offset -= cpu_get_host_ticks(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 368 | timers_state.cpu_clock_offset -= get_clock(); |
| 369 | timers_state.cpu_ticks_enabled = 1; |
| 370 | } |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 371 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 375 | * cpu_get_ticks() after that. |
Cao jin | 3224e87 | 2016-07-08 18:31:37 +0800 | [diff] [blame] | 376 | * Caller must hold BQL which serves as mutex for vm_clock_seqlock. |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 377 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 378 | void cpu_disable_ticks(void) |
| 379 | { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 380 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 381 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 382 | if (timers_state.cpu_ticks_enabled) { |
Christopher Covington | 4a7428c | 2015-09-25 10:42:21 -0400 | [diff] [blame] | 383 | timers_state.cpu_ticks_offset += cpu_get_host_ticks(); |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 384 | timers_state.cpu_clock_offset = cpu_get_clock_locked(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 385 | timers_state.cpu_ticks_enabled = 0; |
| 386 | } |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 387 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* Correlation between real and virtual time is always going to be |
| 391 | fairly approximate, so ignore small variation. |
| 392 | When the guest is idle real and virtual time will be aligned in |
| 393 | the IO wait loop. */ |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 394 | #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 395 | |
| 396 | static void icount_adjust(void) |
| 397 | { |
| 398 | int64_t cur_time; |
| 399 | int64_t cur_icount; |
| 400 | int64_t delta; |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 401 | |
| 402 | /* Protected by TimersState mutex. */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 403 | static int64_t last_delta; |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 404 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 405 | /* If the VM is not running, then do nothing. */ |
| 406 | if (!runstate_is_running()) { |
| 407 | return; |
| 408 | } |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 409 | |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 410 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 411 | cur_time = cpu_get_clock_locked(); |
| 412 | cur_icount = cpu_get_icount_locked(); |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 413 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 414 | delta = cur_icount - cur_time; |
| 415 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 416 | if (delta > 0 |
| 417 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 418 | && icount_time_shift > 0) { |
| 419 | /* The guest is getting too far ahead. Slow time down. */ |
| 420 | icount_time_shift--; |
| 421 | } |
| 422 | if (delta < 0 |
| 423 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 424 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 425 | /* The guest is getting too far behind. Speed time up. */ |
| 426 | icount_time_shift++; |
| 427 | } |
| 428 | last_delta = delta; |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 429 | timers_state.qemu_icount_bias = cur_icount |
| 430 | - (timers_state.qemu_icount << icount_time_shift); |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 431 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void icount_adjust_rt(void *opaque) |
| 435 | { |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 436 | timer_mod(timers_state.icount_rt_timer, |
Pavel Dovgalyuk | 1979b90 | 2015-01-12 15:00:43 +0300 | [diff] [blame] | 437 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 438 | icount_adjust(); |
| 439 | } |
| 440 | |
| 441 | static void icount_adjust_vm(void *opaque) |
| 442 | { |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 443 | timer_mod(timers_state.icount_vm_timer, |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 444 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 445 | NANOSECONDS_PER_SECOND / 10); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 446 | icount_adjust(); |
| 447 | } |
| 448 | |
| 449 | static int64_t qemu_icount_round(int64_t count) |
| 450 | { |
| 451 | return (count + (1 << icount_time_shift) - 1) >> icount_time_shift; |
| 452 | } |
| 453 | |
Pavel Dovgalyuk | efab87c | 2015-09-17 19:24:39 +0300 | [diff] [blame] | 454 | static void icount_warp_rt(void) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 455 | { |
Alex Bennée | ccffff4 | 2016-04-04 15:35:48 +0100 | [diff] [blame] | 456 | unsigned seq; |
| 457 | int64_t warp_start; |
| 458 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 459 | /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start |
| 460 | * changes from -1 to another value, so the race here is okay. |
| 461 | */ |
Alex Bennée | ccffff4 | 2016-04-04 15:35:48 +0100 | [diff] [blame] | 462 | do { |
| 463 | seq = seqlock_read_begin(&timers_state.vm_clock_seqlock); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 464 | warp_start = timers_state.vm_clock_warp_start; |
Alex Bennée | ccffff4 | 2016-04-04 15:35:48 +0100 | [diff] [blame] | 465 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq)); |
| 466 | |
| 467 | if (warp_start == -1) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 471 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 472 | if (runstate_is_running()) { |
Pavel Dovgalyuk | 8eda206 | 2015-09-17 19:24:28 +0300 | [diff] [blame] | 473 | int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, |
| 474 | cpu_get_clock_locked()); |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 475 | int64_t warp_delta; |
| 476 | |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 477 | warp_delta = clock - timers_state.vm_clock_warp_start; |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 478 | if (use_icount == 2) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 479 | /* |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 480 | * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 481 | * far ahead of real time. |
| 482 | */ |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 483 | int64_t cur_icount = cpu_get_icount_locked(); |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 484 | int64_t delta = clock - cur_icount; |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 485 | warp_delta = MIN(warp_delta, delta); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 486 | } |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 487 | timers_state.qemu_icount_bias += warp_delta; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 488 | } |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 489 | timers_state.vm_clock_warp_start = -1; |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 490 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 491 | |
| 492 | if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) { |
| 493 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
| 494 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 497 | static void icount_timer_cb(void *opaque) |
Pavel Dovgalyuk | efab87c | 2015-09-17 19:24:39 +0300 | [diff] [blame] | 498 | { |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 499 | /* No need for a checkpoint because the timer already synchronizes |
| 500 | * with CHECKPOINT_CLOCK_VIRTUAL_RT. |
| 501 | */ |
| 502 | icount_warp_rt(); |
Pavel Dovgalyuk | efab87c | 2015-09-17 19:24:39 +0300 | [diff] [blame] | 503 | } |
| 504 | |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 505 | void qtest_clock_warp(int64_t dest) |
| 506 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 507 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 508 | AioContext *aio_context; |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 509 | assert(qtest_enabled()); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 510 | aio_context = qemu_get_aio_context(); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 511 | while (clock < dest) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 512 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Sergey Fedorov | c9299e2 | 2014-06-10 13:10:28 +0400 | [diff] [blame] | 513 | int64_t warp = qemu_soonest_timeout(dest - clock, deadline); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 514 | |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 515 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 516 | timers_state.qemu_icount_bias += warp; |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 517 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 518 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 519 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 520 | timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 521 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 522 | } |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 523 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 526 | void qemu_start_warp_timer(void) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 527 | { |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 528 | int64_t clock; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 529 | int64_t deadline; |
| 530 | |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 531 | if (!use_icount) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 535 | /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers |
| 536 | * do not fire, so computing the deadline does not make sense. |
| 537 | */ |
| 538 | if (!runstate_is_running()) { |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | /* warp clock deterministically in record/replay mode */ |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 543 | if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) { |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 547 | if (!all_cpu_threads_idle()) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 548 | return; |
| 549 | } |
| 550 | |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 551 | if (qtest_enabled()) { |
| 552 | /* When testing, qtest commands advance icount. */ |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 553 | return; |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 556 | /* We want to use the earliest deadline from ALL vm_clocks */ |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 557 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 558 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 559 | if (deadline < 0) { |
Victor CLEMENT | d7a0f71 | 2015-05-29 17:14:06 +0200 | [diff] [blame] | 560 | static bool notified; |
| 561 | if (!icount_sleep && !notified) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 562 | warn_report("icount sleep disabled and no active timers"); |
Victor CLEMENT | d7a0f71 | 2015-05-29 17:14:06 +0200 | [diff] [blame] | 563 | notified = true; |
| 564 | } |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 565 | return; |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 568 | if (deadline > 0) { |
| 569 | /* |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 570 | * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 571 | * sleep. Otherwise, the CPU might be waiting for a future timer |
| 572 | * interrupt to wake it up, but the interrupt never comes because |
| 573 | * the vCPU isn't running any insns and thus doesn't advance the |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 574 | * QEMU_CLOCK_VIRTUAL. |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 575 | */ |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 576 | if (!icount_sleep) { |
| 577 | /* |
| 578 | * We never let VCPUs sleep in no sleep icount mode. |
| 579 | * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance |
| 580 | * to the next QEMU_CLOCK_VIRTUAL event and notify it. |
| 581 | * It is useful when we want a deterministic execution time, |
| 582 | * isolated from host latencies. |
| 583 | */ |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 584 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 585 | timers_state.qemu_icount_bias += deadline; |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 586 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 587 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
| 588 | } else { |
| 589 | /* |
| 590 | * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some |
| 591 | * "real" time, (related to the time left until the next event) has |
| 592 | * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this. |
| 593 | * This avoids that the warps are visible externally; for example, |
| 594 | * you will not be sending network packets continuously instead of |
| 595 | * every 100ms. |
| 596 | */ |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 597 | seqlock_write_begin(&timers_state.vm_clock_seqlock); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 598 | if (timers_state.vm_clock_warp_start == -1 |
| 599 | || timers_state.vm_clock_warp_start > clock) { |
| 600 | timers_state.vm_clock_warp_start = clock; |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 601 | } |
Emilio G. Cota | 03719e4 | 2016-06-08 14:55:21 -0400 | [diff] [blame] | 602 | seqlock_write_end(&timers_state.vm_clock_seqlock); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 603 | timer_mod_anticipate(timers_state.icount_warp_timer, |
| 604 | clock + deadline); |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 605 | } |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 606 | } else if (deadline == 0) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 607 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 611 | static void qemu_account_warp_timer(void) |
| 612 | { |
| 613 | if (!use_icount || !icount_sleep) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers |
| 618 | * do not fire, so computing the deadline does not make sense. |
| 619 | */ |
| 620 | if (!runstate_is_running()) { |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | /* warp clock deterministically in record/replay mode */ |
| 625 | if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) { |
| 626 | return; |
| 627 | } |
| 628 | |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 629 | timer_del(timers_state.icount_warp_timer); |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 630 | icount_warp_rt(); |
| 631 | } |
| 632 | |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 633 | static bool icount_state_needed(void *opaque) |
| 634 | { |
| 635 | return use_icount; |
| 636 | } |
| 637 | |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 638 | static bool warp_timer_state_needed(void *opaque) |
| 639 | { |
| 640 | TimersState *s = opaque; |
| 641 | return s->icount_warp_timer != NULL; |
| 642 | } |
| 643 | |
| 644 | static bool adjust_timers_state_needed(void *opaque) |
| 645 | { |
| 646 | TimersState *s = opaque; |
| 647 | return s->icount_rt_timer != NULL; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Subsection for warp timer migration is optional, because may not be created |
| 652 | */ |
| 653 | static const VMStateDescription icount_vmstate_warp_timer = { |
| 654 | .name = "timer/icount/warp_timer", |
| 655 | .version_id = 1, |
| 656 | .minimum_version_id = 1, |
| 657 | .needed = warp_timer_state_needed, |
| 658 | .fields = (VMStateField[]) { |
| 659 | VMSTATE_INT64(vm_clock_warp_start, TimersState), |
| 660 | VMSTATE_TIMER_PTR(icount_warp_timer, TimersState), |
| 661 | VMSTATE_END_OF_LIST() |
| 662 | } |
| 663 | }; |
| 664 | |
| 665 | static const VMStateDescription icount_vmstate_adjust_timers = { |
| 666 | .name = "timer/icount/timers", |
| 667 | .version_id = 1, |
| 668 | .minimum_version_id = 1, |
| 669 | .needed = adjust_timers_state_needed, |
| 670 | .fields = (VMStateField[]) { |
| 671 | VMSTATE_TIMER_PTR(icount_rt_timer, TimersState), |
| 672 | VMSTATE_TIMER_PTR(icount_vm_timer, TimersState), |
| 673 | VMSTATE_END_OF_LIST() |
| 674 | } |
| 675 | }; |
| 676 | |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 677 | /* |
| 678 | * This is a subsection for icount migration. |
| 679 | */ |
| 680 | static const VMStateDescription icount_vmstate_timers = { |
| 681 | .name = "timer/icount", |
| 682 | .version_id = 1, |
| 683 | .minimum_version_id = 1, |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 684 | .needed = icount_state_needed, |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 685 | .fields = (VMStateField[]) { |
| 686 | VMSTATE_INT64(qemu_icount_bias, TimersState), |
| 687 | VMSTATE_INT64(qemu_icount, TimersState), |
| 688 | VMSTATE_END_OF_LIST() |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 689 | }, |
| 690 | .subsections = (const VMStateDescription*[]) { |
| 691 | &icount_vmstate_warp_timer, |
| 692 | &icount_vmstate_adjust_timers, |
| 693 | NULL |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 694 | } |
| 695 | }; |
| 696 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 697 | static const VMStateDescription vmstate_timers = { |
| 698 | .name = "timer", |
| 699 | .version_id = 2, |
| 700 | .minimum_version_id = 1, |
Juan Quintela | 35d0845 | 2014-04-16 16:01:33 +0200 | [diff] [blame] | 701 | .fields = (VMStateField[]) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 702 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
| 703 | VMSTATE_INT64(dummy, TimersState), |
| 704 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 705 | VMSTATE_END_OF_LIST() |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 706 | }, |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 707 | .subsections = (const VMStateDescription*[]) { |
| 708 | &icount_vmstate_timers, |
| 709 | NULL |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 710 | } |
| 711 | }; |
| 712 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 713 | static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 714 | { |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 715 | double pct; |
| 716 | double throttle_ratio; |
| 717 | long sleeptime_ns; |
| 718 | |
| 719 | if (!cpu_throttle_get_percentage()) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | pct = (double)cpu_throttle_get_percentage()/100; |
| 724 | throttle_ratio = pct / (1 - pct); |
| 725 | sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS); |
| 726 | |
| 727 | qemu_mutex_unlock_iothread(); |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 728 | g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */ |
| 729 | qemu_mutex_lock_iothread(); |
Felipe Franciosi | 90bb0c0 | 2017-05-19 22:29:50 +0100 | [diff] [blame] | 730 | atomic_set(&cpu->throttle_thread_scheduled, 0); |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static void cpu_throttle_timer_tick(void *opaque) |
| 734 | { |
| 735 | CPUState *cpu; |
| 736 | double pct; |
| 737 | |
| 738 | /* Stop the timer if needed */ |
| 739 | if (!cpu_throttle_get_percentage()) { |
| 740 | return; |
| 741 | } |
| 742 | CPU_FOREACH(cpu) { |
| 743 | if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) { |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 744 | async_run_on_cpu(cpu, cpu_throttle_thread, |
| 745 | RUN_ON_CPU_NULL); |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | pct = (double)cpu_throttle_get_percentage()/100; |
| 750 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + |
| 751 | CPU_THROTTLE_TIMESLICE_NS / (1-pct)); |
| 752 | } |
| 753 | |
| 754 | void cpu_throttle_set(int new_throttle_pct) |
| 755 | { |
| 756 | /* Ensure throttle percentage is within valid range */ |
| 757 | new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX); |
| 758 | new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN); |
| 759 | |
| 760 | atomic_set(&throttle_percentage, new_throttle_pct); |
| 761 | |
| 762 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + |
| 763 | CPU_THROTTLE_TIMESLICE_NS); |
| 764 | } |
| 765 | |
| 766 | void cpu_throttle_stop(void) |
| 767 | { |
| 768 | atomic_set(&throttle_percentage, 0); |
| 769 | } |
| 770 | |
| 771 | bool cpu_throttle_active(void) |
| 772 | { |
| 773 | return (cpu_throttle_get_percentage() != 0); |
| 774 | } |
| 775 | |
| 776 | int cpu_throttle_get_percentage(void) |
| 777 | { |
| 778 | return atomic_read(&throttle_percentage); |
| 779 | } |
| 780 | |
Pavel Dovgalyuk | 4603ea0 | 2014-09-01 09:34:49 +0400 | [diff] [blame] | 781 | void cpu_ticks_init(void) |
| 782 | { |
Emilio G. Cota | ccdb3c1 | 2016-06-08 14:55:20 -0400 | [diff] [blame] | 783 | seqlock_init(&timers_state.vm_clock_seqlock); |
Pavel Dovgalyuk | 4603ea0 | 2014-09-01 09:34:49 +0400 | [diff] [blame] | 784 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame] | 785 | throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
| 786 | cpu_throttle_timer_tick, NULL); |
Pavel Dovgalyuk | 4603ea0 | 2014-09-01 09:34:49 +0400 | [diff] [blame] | 787 | } |
| 788 | |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 789 | void configure_icount(QemuOpts *opts, Error **errp) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 790 | { |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 791 | const char *option; |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 792 | char *rem_str = NULL; |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 793 | |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 794 | option = qemu_opt_get(opts, "shift"); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 795 | if (!option) { |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 796 | if (qemu_opt_get(opts, "align") != NULL) { |
| 797 | error_setg(errp, "Please specify shift option when using align"); |
| 798 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 799 | return; |
| 800 | } |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 801 | |
| 802 | icount_sleep = qemu_opt_get_bool(opts, "sleep", true); |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 803 | if (icount_sleep) { |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 804 | timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
Pavel Dovgalyuk | e76d179 | 2016-03-10 14:56:09 +0300 | [diff] [blame] | 805 | icount_timer_cb, NULL); |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 806 | } |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 807 | |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 808 | icount_align_option = qemu_opt_get_bool(opts, "align", false); |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 809 | |
| 810 | if (icount_align_option && !icount_sleep) { |
Pranith Kumar | 778d9f9 | 2016-02-26 10:16:51 -0500 | [diff] [blame] | 811 | error_setg(errp, "align=on and sleep=off are incompatible"); |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 812 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 813 | if (strcmp(option, "auto") != 0) { |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 814 | errno = 0; |
| 815 | icount_time_shift = strtol(option, &rem_str, 0); |
| 816 | if (errno != 0 || *rem_str != '\0' || !strlen(option)) { |
| 817 | error_setg(errp, "icount: Invalid shift value"); |
| 818 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 819 | use_icount = 1; |
| 820 | return; |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 821 | } else if (icount_align_option) { |
| 822 | error_setg(errp, "shift=auto and align=on are incompatible"); |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 823 | } else if (!icount_sleep) { |
Pranith Kumar | 778d9f9 | 2016-02-26 10:16:51 -0500 | [diff] [blame] | 824 | error_setg(errp, "shift=auto and sleep=off are incompatible"); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | use_icount = 2; |
| 828 | |
| 829 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 830 | It will be corrected fairly quickly anyway. */ |
| 831 | icount_time_shift = 3; |
| 832 | |
| 833 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 834 | The realtime trigger catches emulated time passing too slowly, |
| 835 | the virtual time trigger catches emulated time passing too fast. |
| 836 | Realtime triggers occur even when idle, so use them less frequently |
| 837 | than VM triggers. */ |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 838 | timers_state.vm_clock_warp_start = -1; |
| 839 | timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT, |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 840 | icount_adjust_rt, NULL); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 841 | timer_mod(timers_state.icount_rt_timer, |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 842 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 843 | timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 844 | icount_adjust_vm, NULL); |
Pavel Dovgalyuk | b39e3f3 | 2018-01-11 11:26:10 +0300 | [diff] [blame] | 845 | timer_mod(timers_state.icount_vm_timer, |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 846 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
Rutuja Shah | 73bcb24 | 2016-03-21 21:32:30 +0530 | [diff] [blame] | 847 | NANOSECONDS_PER_SECOND / 10); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | /***********************************************************/ |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 851 | /* TCG vCPU kick timer |
| 852 | * |
| 853 | * The kick timer is responsible for moving single threaded vCPU |
| 854 | * emulation on to the next vCPU. If more than one vCPU is running a |
| 855 | * timer event with force a cpu->exit so the next vCPU can get |
| 856 | * scheduled. |
| 857 | * |
| 858 | * The timer is removed if all vCPUs are idle and restarted again once |
| 859 | * idleness is complete. |
| 860 | */ |
| 861 | |
| 862 | static QEMUTimer *tcg_kick_vcpu_timer; |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 863 | static CPUState *tcg_current_rr_cpu; |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 864 | |
| 865 | #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10) |
| 866 | |
| 867 | static inline int64_t qemu_tcg_next_kick(void) |
| 868 | { |
| 869 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD; |
| 870 | } |
| 871 | |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 872 | /* Kick the currently round-robin scheduled vCPU */ |
| 873 | static void qemu_cpu_kick_rr_cpu(void) |
| 874 | { |
| 875 | CPUState *cpu; |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 876 | do { |
| 877 | cpu = atomic_mb_read(&tcg_current_rr_cpu); |
| 878 | if (cpu) { |
| 879 | cpu_exit(cpu); |
| 880 | } |
| 881 | } while (cpu != atomic_mb_read(&tcg_current_rr_cpu)); |
| 882 | } |
| 883 | |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 884 | static void do_nothing(CPUState *cpu, run_on_cpu_data unused) |
| 885 | { |
| 886 | } |
| 887 | |
Paolo Bonzini | 3f53bc6 | 2017-03-03 11:50:29 +0100 | [diff] [blame] | 888 | void qemu_timer_notify_cb(void *opaque, QEMUClockType type) |
| 889 | { |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 890 | if (!use_icount || type != QEMU_CLOCK_VIRTUAL) { |
| 891 | qemu_notify_event(); |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | if (!qemu_in_vcpu_thread() && first_cpu) { |
| 896 | /* qemu_cpu_kick is not enough to kick a halted CPU out of |
| 897 | * qemu_tcg_wait_io_event. async_run_on_cpu, instead, |
| 898 | * causes cpu_thread_is_idle to return false. This way, |
| 899 | * handle_icount_deadline can run. |
| 900 | */ |
| 901 | async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL); |
| 902 | } |
Paolo Bonzini | 3f53bc6 | 2017-03-03 11:50:29 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 905 | static void kick_tcg_thread(void *opaque) |
| 906 | { |
| 907 | timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick()); |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 908 | qemu_cpu_kick_rr_cpu(); |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | static void start_tcg_kick_timer(void) |
| 912 | { |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 913 | assert(!mttcg_enabled); |
| 914 | if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) { |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 915 | tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
| 916 | kick_tcg_thread, NULL); |
| 917 | timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick()); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | static void stop_tcg_kick_timer(void) |
| 922 | { |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 923 | assert(!mttcg_enabled); |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 924 | if (tcg_kick_vcpu_timer) { |
| 925 | timer_del(tcg_kick_vcpu_timer); |
| 926 | tcg_kick_vcpu_timer = NULL; |
| 927 | } |
| 928 | } |
| 929 | |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 930 | /***********************************************************/ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 931 | void hw_error(const char *fmt, ...) |
| 932 | { |
| 933 | va_list ap; |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 934 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 935 | |
| 936 | va_start(ap, fmt); |
| 937 | fprintf(stderr, "qemu: hardware error: "); |
| 938 | vfprintf(stderr, fmt, ap); |
| 939 | fprintf(stderr, "\n"); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 940 | CPU_FOREACH(cpu) { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 941 | fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); |
Andreas Färber | 878096e | 2013-05-27 01:33:50 +0200 | [diff] [blame] | 942 | cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 943 | } |
| 944 | va_end(ap); |
| 945 | abort(); |
| 946 | } |
| 947 | |
| 948 | void cpu_synchronize_all_states(void) |
| 949 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 950 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 951 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 952 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 953 | cpu_synchronize_state(cpu); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 954 | /* TODO: move to cpu_synchronize_state() */ |
| 955 | if (hvf_enabled()) { |
| 956 | hvf_cpu_synchronize_state(cpu); |
| 957 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
| 961 | void cpu_synchronize_all_post_reset(void) |
| 962 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 963 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 964 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 965 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 966 | cpu_synchronize_post_reset(cpu); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 967 | /* TODO: move to cpu_synchronize_post_reset() */ |
| 968 | if (hvf_enabled()) { |
| 969 | hvf_cpu_synchronize_post_reset(cpu); |
| 970 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
| 974 | void cpu_synchronize_all_post_init(void) |
| 975 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 976 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 977 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 978 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 979 | cpu_synchronize_post_init(cpu); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 980 | /* TODO: move to cpu_synchronize_post_init() */ |
| 981 | if (hvf_enabled()) { |
| 982 | hvf_cpu_synchronize_post_init(cpu); |
| 983 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
David Gibson | 75e972d | 2017-05-26 14:46:28 +1000 | [diff] [blame] | 987 | void cpu_synchronize_all_pre_loadvm(void) |
| 988 | { |
| 989 | CPUState *cpu; |
| 990 | |
| 991 | CPU_FOREACH(cpu) { |
| 992 | cpu_synchronize_pre_loadvm(cpu); |
| 993 | } |
| 994 | } |
| 995 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 996 | static int do_vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 997 | { |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 998 | int ret = 0; |
| 999 | |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 1000 | if (runstate_is_running()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1001 | cpu_disable_ticks(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1002 | pause_all_vcpus(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 1003 | runstate_set(state); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1004 | vm_state_notify(0, state); |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 1005 | qapi_event_send_stop(&error_abort); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1006 | } |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1007 | |
Kevin Wolf | 594a45c | 2013-07-18 14:52:19 +0200 | [diff] [blame] | 1008 | bdrv_drain_all(); |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 1009 | replay_disable_events(); |
John Snow | 22af08e | 2016-09-22 21:45:51 -0400 | [diff] [blame] | 1010 | ret = bdrv_flush_all(); |
Kevin Wolf | 594a45c | 2013-07-18 14:52:19 +0200 | [diff] [blame] | 1011 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1012 | return ret; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1015 | static bool cpu_can_run(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1016 | { |
Andreas Färber | 4fdeee7 | 2012-05-02 23:10:09 +0200 | [diff] [blame] | 1017 | if (cpu->stop) { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1018 | return false; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1019 | } |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 1020 | if (cpu_is_stopped(cpu)) { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1021 | return false; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1022 | } |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1023 | return true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Andreas Färber | 9132504 | 2013-05-27 02:07:49 +0200 | [diff] [blame] | 1026 | static void cpu_handle_guest_debug(CPUState *cpu) |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 1027 | { |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 1028 | gdb_set_stop_cpu(cpu); |
Jan Kiszka | 8cf7171 | 2011-02-07 12:19:16 +0100 | [diff] [blame] | 1029 | qemu_system_debug_request(); |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 1030 | cpu->stopped = true; |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1033 | #ifdef CONFIG_LINUX |
| 1034 | static void sigbus_reraise(void) |
| 1035 | { |
| 1036 | sigset_t set; |
| 1037 | struct sigaction action; |
| 1038 | |
| 1039 | memset(&action, 0, sizeof(action)); |
| 1040 | action.sa_handler = SIG_DFL; |
| 1041 | if (!sigaction(SIGBUS, &action, NULL)) { |
| 1042 | raise(SIGBUS); |
| 1043 | sigemptyset(&set); |
| 1044 | sigaddset(&set, SIGBUS); |
Peter Maydell | a2d1761 | 2016-05-16 18:33:59 +0100 | [diff] [blame] | 1045 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1046 | } |
| 1047 | perror("Failed to re-raise SIGBUS!\n"); |
| 1048 | abort(); |
| 1049 | } |
| 1050 | |
Paolo Bonzini | d98d407 | 2017-02-08 13:22:12 +0100 | [diff] [blame] | 1051 | static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1052 | { |
Paolo Bonzini | a16fc07 | 2017-02-09 09:50:02 +0100 | [diff] [blame] | 1053 | if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) { |
| 1054 | sigbus_reraise(); |
| 1055 | } |
| 1056 | |
Paolo Bonzini | 2ae41db | 2017-02-08 12:48:54 +0100 | [diff] [blame] | 1057 | if (current_cpu) { |
| 1058 | /* Called asynchronously in VCPU thread. */ |
| 1059 | if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) { |
| 1060 | sigbus_reraise(); |
| 1061 | } |
| 1062 | } else { |
| 1063 | /* Called synchronously (via signalfd) in main thread. */ |
| 1064 | if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { |
| 1065 | sigbus_reraise(); |
| 1066 | } |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | static void qemu_init_sigbus(void) |
| 1071 | { |
| 1072 | struct sigaction action; |
| 1073 | |
| 1074 | memset(&action, 0, sizeof(action)); |
| 1075 | action.sa_flags = SA_SIGINFO; |
Paolo Bonzini | d98d407 | 2017-02-08 13:22:12 +0100 | [diff] [blame] | 1076 | action.sa_sigaction = sigbus_handler; |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1077 | sigaction(SIGBUS, &action, NULL); |
| 1078 | |
| 1079 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); |
| 1080 | } |
Paolo Bonzini | a16fc07 | 2017-02-09 09:50:02 +0100 | [diff] [blame] | 1081 | #else /* !CONFIG_LINUX */ |
| 1082 | static void qemu_init_sigbus(void) |
| 1083 | { |
| 1084 | } |
Paolo Bonzini | a16fc07 | 2017-02-09 09:50:02 +0100 | [diff] [blame] | 1085 | #endif /* !CONFIG_LINUX */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1086 | |
Stefan Weil | b2532d8 | 2012-09-27 07:41:42 +0200 | [diff] [blame] | 1087 | static QemuMutex qemu_global_mutex; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1088 | |
| 1089 | static QemuThread io_thread; |
| 1090 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1091 | /* cpu creation */ |
| 1092 | static QemuCond qemu_cpu_cond; |
| 1093 | /* system init */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1094 | static QemuCond qemu_pause_cond; |
| 1095 | |
Paolo Bonzini | d3b12f5 | 2011-09-13 10:30:52 +0200 | [diff] [blame] | 1096 | void qemu_init_cpu_loop(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1097 | { |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 1098 | qemu_init_sigbus(); |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 1099 | qemu_cond_init(&qemu_cpu_cond); |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 1100 | qemu_cond_init(&qemu_pause_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1101 | qemu_mutex_init(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1102 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 1103 | qemu_thread_get_self(&io_thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Paolo Bonzini | 14e6fe1 | 2016-10-31 10:36:08 +0100 | [diff] [blame] | 1106 | void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 1107 | { |
Sergey Fedorov | d148d90 | 2016-08-29 09:51:00 +0200 | [diff] [blame] | 1108 | do_run_on_cpu(cpu, func, data, &qemu_global_mutex); |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 1109 | } |
| 1110 | |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1111 | static void qemu_kvm_destroy_vcpu(CPUState *cpu) |
| 1112 | { |
| 1113 | if (kvm_destroy_vcpu(cpu) < 0) { |
| 1114 | error_report("kvm_destroy_vcpu failed"); |
| 1115 | exit(EXIT_FAILURE); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | static void qemu_tcg_destroy_vcpu(CPUState *cpu) |
| 1120 | { |
| 1121 | } |
| 1122 | |
David Hildenbrand | ebd05fe | 2017-11-29 20:12:15 +0100 | [diff] [blame] | 1123 | static void qemu_cpu_stop(CPUState *cpu, bool exit) |
| 1124 | { |
| 1125 | g_assert(qemu_cpu_is_self(cpu)); |
| 1126 | cpu->stop = false; |
| 1127 | cpu->stopped = true; |
| 1128 | if (exit) { |
| 1129 | cpu_exit(cpu); |
| 1130 | } |
| 1131 | qemu_cond_broadcast(&qemu_pause_cond); |
| 1132 | } |
| 1133 | |
Andreas Färber | 509a0d7 | 2012-05-03 02:18:09 +0200 | [diff] [blame] | 1134 | static void qemu_wait_io_event_common(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1135 | { |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1136 | atomic_mb_set(&cpu->thread_kicked, false); |
Andreas Färber | 4fdeee7 | 2012-05-02 23:10:09 +0200 | [diff] [blame] | 1137 | if (cpu->stop) { |
David Hildenbrand | ebd05fe | 2017-11-29 20:12:15 +0100 | [diff] [blame] | 1138 | qemu_cpu_stop(cpu, false); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1139 | } |
Sergey Fedorov | a5403c6 | 2016-08-02 18:27:36 +0100 | [diff] [blame] | 1140 | process_queued_cpu_work(cpu); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1143 | static void qemu_tcg_rr_wait_io_event(CPUState *cpu) |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1144 | { |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1145 | while (all_cpu_threads_idle()) { |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 1146 | stop_tcg_kick_timer(); |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 1147 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 1148 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1149 | |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 1150 | start_tcg_kick_timer(); |
| 1151 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1152 | qemu_wait_io_event_common(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1155 | static void qemu_wait_io_event(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1156 | { |
Andreas Färber | a98ae1d | 2013-05-26 23:21:08 +0200 | [diff] [blame] | 1157 | while (cpu_thread_is_idle(cpu)) { |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1158 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 1159 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1160 | |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1161 | #ifdef _WIN32 |
| 1162 | /* Eat dummy APC queued by qemu_cpu_kick_thread. */ |
| 1163 | if (!tcg_enabled()) { |
| 1164 | SleepEx(0, TRUE); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1165 | } |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1166 | #endif |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1167 | qemu_wait_io_event_common(cpu); |
| 1168 | } |
| 1169 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1170 | static void *qemu_kvm_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1171 | { |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1172 | CPUState *cpu = arg; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 1173 | int r; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1174 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1175 | rcu_register_thread(); |
| 1176 | |
Paolo Bonzini | 2e7f7a3 | 2015-06-18 18:47:18 +0200 | [diff] [blame] | 1177 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1178 | qemu_thread_get_self(cpu->thread); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 1179 | cpu->thread_id = qemu_get_thread_id(); |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1180 | cpu->can_do_io = 1; |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1181 | current_cpu = cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1182 | |
Andreas Färber | 504134d | 2012-12-17 06:38:45 +0100 | [diff] [blame] | 1183 | r = kvm_init_vcpu(cpu); |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 1184 | if (r < 0) { |
Alistair Francis | 493d89b | 2018-02-03 09:43:14 +0100 | [diff] [blame] | 1185 | error_report("kvm_init_vcpu failed: %s", strerror(-r)); |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 1186 | exit(1); |
| 1187 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1188 | |
Paolo Bonzini | 18268b6 | 2017-02-09 09:41:14 +0100 | [diff] [blame] | 1189 | kvm_init_cpu_signals(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1190 | |
| 1191 | /* signal CPU creation */ |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1192 | cpu->created = true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1193 | qemu_cond_signal(&qemu_cpu_cond); |
| 1194 | |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1195 | do { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1196 | if (cpu_can_run(cpu)) { |
Andreas Färber | 1458c36 | 2013-05-26 23:46:55 +0200 | [diff] [blame] | 1197 | r = kvm_cpu_exec(cpu); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 1198 | if (r == EXCP_DEBUG) { |
Andreas Färber | 9132504 | 2013-05-27 02:07:49 +0200 | [diff] [blame] | 1199 | cpu_handle_guest_debug(cpu); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 1200 | } |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1201 | } |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1202 | qemu_wait_io_event(cpu); |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1203 | } while (!cpu->unplug || cpu_can_run(cpu)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1204 | |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1205 | qemu_kvm_destroy_vcpu(cpu); |
Bharata B Rao | 2c57904 | 2016-05-12 09:18:14 +0530 | [diff] [blame] | 1206 | cpu->created = false; |
| 1207 | qemu_cond_signal(&qemu_cpu_cond); |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1208 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | 57615ed | 2018-01-30 11:04:36 -0500 | [diff] [blame] | 1209 | rcu_unregister_thread(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1210 | return NULL; |
| 1211 | } |
| 1212 | |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1213 | static void *qemu_dummy_cpu_thread_fn(void *arg) |
| 1214 | { |
| 1215 | #ifdef _WIN32 |
Alistair Francis | 493d89b | 2018-02-03 09:43:14 +0100 | [diff] [blame] | 1216 | error_report("qtest is not supported under Windows"); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1217 | exit(1); |
| 1218 | #else |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1219 | CPUState *cpu = arg; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1220 | sigset_t waitset; |
| 1221 | int r; |
| 1222 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1223 | rcu_register_thread(); |
| 1224 | |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1225 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1226 | qemu_thread_get_self(cpu->thread); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 1227 | cpu->thread_id = qemu_get_thread_id(); |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1228 | cpu->can_do_io = 1; |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1229 | current_cpu = cpu; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1230 | |
| 1231 | sigemptyset(&waitset); |
| 1232 | sigaddset(&waitset, SIG_IPI); |
| 1233 | |
| 1234 | /* signal CPU creation */ |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1235 | cpu->created = true; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1236 | qemu_cond_signal(&qemu_cpu_cond); |
| 1237 | |
Paolo Bonzini | d2831ab | 2018-01-30 11:04:53 -0500 | [diff] [blame] | 1238 | do { |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1239 | qemu_mutex_unlock_iothread(); |
| 1240 | do { |
| 1241 | int sig; |
| 1242 | r = sigwait(&waitset, &sig); |
| 1243 | } while (r == -1 && (errno == EAGAIN || errno == EINTR)); |
| 1244 | if (r == -1) { |
| 1245 | perror("sigwait"); |
| 1246 | exit(1); |
| 1247 | } |
| 1248 | qemu_mutex_lock_iothread(); |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1249 | qemu_wait_io_event(cpu); |
Paolo Bonzini | d2831ab | 2018-01-30 11:04:53 -0500 | [diff] [blame] | 1250 | } while (!cpu->unplug); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1251 | |
Paolo Bonzini | d2831ab | 2018-01-30 11:04:53 -0500 | [diff] [blame] | 1252 | rcu_unregister_thread(); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1253 | return NULL; |
| 1254 | #endif |
| 1255 | } |
| 1256 | |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1257 | static int64_t tcg_get_icount_limit(void) |
| 1258 | { |
| 1259 | int64_t deadline; |
| 1260 | |
| 1261 | if (replay_mode != REPLAY_MODE_PLAY) { |
| 1262 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
| 1263 | |
| 1264 | /* Maintain prior (possibly buggy) behaviour where if no deadline |
| 1265 | * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than |
| 1266 | * INT32_MAX nanoseconds ahead, we still use INT32_MAX |
| 1267 | * nanoseconds. |
| 1268 | */ |
| 1269 | if ((deadline < 0) || (deadline > INT32_MAX)) { |
| 1270 | deadline = INT32_MAX; |
| 1271 | } |
| 1272 | |
| 1273 | return qemu_icount_round(deadline); |
| 1274 | } else { |
| 1275 | return replay_get_instructions(); |
| 1276 | } |
| 1277 | } |
| 1278 | |
Alex Bennée | 12e9700 | 2016-10-27 16:10:14 +0100 | [diff] [blame] | 1279 | static void handle_icount_deadline(void) |
| 1280 | { |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 1281 | assert(qemu_in_vcpu_thread()); |
Alex Bennée | 12e9700 | 2016-10-27 16:10:14 +0100 | [diff] [blame] | 1282 | if (use_icount) { |
| 1283 | int64_t deadline = |
| 1284 | qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
| 1285 | |
| 1286 | if (deadline == 0) { |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 1287 | /* Wake up other AioContexts. */ |
Alex Bennée | 12e9700 | 2016-10-27 16:10:14 +0100 | [diff] [blame] | 1288 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 1289 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
Alex Bennée | 12e9700 | 2016-10-27 16:10:14 +0100 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1294 | static void prepare_icount_for_run(CPUState *cpu) |
| 1295 | { |
| 1296 | if (use_icount) { |
Alex Bennée | eda5f7c | 2017-04-05 12:35:48 +0100 | [diff] [blame] | 1297 | int insns_left; |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1298 | |
| 1299 | /* These should always be cleared by process_icount_data after |
| 1300 | * each vCPU execution. However u16.high can be raised |
| 1301 | * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt |
| 1302 | */ |
| 1303 | g_assert(cpu->icount_decr.u16.low == 0); |
| 1304 | g_assert(cpu->icount_extra == 0); |
| 1305 | |
Alex Bennée | eda5f7c | 2017-04-05 12:35:48 +0100 | [diff] [blame] | 1306 | cpu->icount_budget = tcg_get_icount_limit(); |
| 1307 | insns_left = MIN(0xffff, cpu->icount_budget); |
| 1308 | cpu->icount_decr.u16.low = insns_left; |
| 1309 | cpu->icount_extra = cpu->icount_budget - insns_left; |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | static void process_icount_data(CPUState *cpu) |
| 1314 | { |
| 1315 | if (use_icount) { |
Alex Bennée | e4cd965 | 2017-03-31 16:09:42 +0100 | [diff] [blame] | 1316 | /* Account for executed instructions */ |
Alex Bennée | 512d3c8 | 2017-04-05 12:32:37 +0100 | [diff] [blame] | 1317 | cpu_update_icount(cpu); |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1318 | |
| 1319 | /* Reset the counters */ |
| 1320 | cpu->icount_decr.u16.low = 0; |
| 1321 | cpu->icount_extra = 0; |
Alex Bennée | e4cd965 | 2017-03-31 16:09:42 +0100 | [diff] [blame] | 1322 | cpu->icount_budget = 0; |
| 1323 | |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1324 | replay_account_executed_instructions(); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1329 | static int tcg_cpu_exec(CPUState *cpu) |
| 1330 | { |
| 1331 | int ret; |
| 1332 | #ifdef CONFIG_PROFILER |
| 1333 | int64_t ti; |
| 1334 | #endif |
| 1335 | |
| 1336 | #ifdef CONFIG_PROFILER |
| 1337 | ti = profile_getclock(); |
| 1338 | #endif |
Jan Kiszka | 8d04fb5 | 2017-02-23 18:29:11 +0000 | [diff] [blame] | 1339 | qemu_mutex_unlock_iothread(); |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1340 | cpu_exec_start(cpu); |
| 1341 | ret = cpu_exec(cpu); |
| 1342 | cpu_exec_end(cpu); |
Jan Kiszka | 8d04fb5 | 2017-02-23 18:29:11 +0000 | [diff] [blame] | 1343 | qemu_mutex_lock_iothread(); |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1344 | #ifdef CONFIG_PROFILER |
| 1345 | tcg_time += profile_getclock() - ti; |
| 1346 | #endif |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1347 | return ret; |
| 1348 | } |
| 1349 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1350 | /* Destroy any remaining vCPUs which have been unplugged and have |
| 1351 | * finished running |
| 1352 | */ |
| 1353 | static void deal_with_unplugged_cpus(void) |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1354 | { |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1355 | CPUState *cpu; |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1356 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1357 | CPU_FOREACH(cpu) { |
| 1358 | if (cpu->unplug && !cpu_can_run(cpu)) { |
| 1359 | qemu_tcg_destroy_vcpu(cpu); |
| 1360 | cpu->created = false; |
| 1361 | qemu_cond_signal(&qemu_cpu_cond); |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1362 | break; |
| 1363 | } |
| 1364 | } |
Alex Bennée | 1be7fcb | 2016-10-27 16:10:08 +0100 | [diff] [blame] | 1365 | } |
Jan Kiszka | bdb7ca6 | 2011-09-26 09:40:39 +0200 | [diff] [blame] | 1366 | |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 1367 | /* Single-threaded TCG |
| 1368 | * |
| 1369 | * In the single-threaded case each vCPU is simulated in turn. If |
| 1370 | * there is more than a single vCPU we create a simple timer to kick |
| 1371 | * the vCPU and ensure we don't get stuck in a tight loop in one vCPU. |
| 1372 | * This is done explicitly rather than relying on side-effects |
| 1373 | * elsewhere. |
| 1374 | */ |
| 1375 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1376 | static void *qemu_tcg_rr_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1377 | { |
Andreas Färber | c3586ba | 2012-05-03 01:41:24 +0200 | [diff] [blame] | 1378 | CPUState *cpu = arg; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1379 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1380 | rcu_register_thread(); |
Emilio G. Cota | 3468b59 | 2017-07-19 18:57:58 -0400 | [diff] [blame] | 1381 | tcg_register_thread(); |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1382 | |
Paolo Bonzini | 2e7f7a3 | 2015-06-18 18:47:18 +0200 | [diff] [blame] | 1383 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1384 | qemu_thread_get_self(cpu->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1385 | |
Andreas Färber | 38fcbd3 | 2013-07-07 19:50:23 +0200 | [diff] [blame] | 1386 | CPU_FOREACH(cpu) { |
| 1387 | cpu->thread_id = qemu_get_thread_id(); |
| 1388 | cpu->created = true; |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1389 | cpu->can_do_io = 1; |
Andreas Färber | 38fcbd3 | 2013-07-07 19:50:23 +0200 | [diff] [blame] | 1390 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1391 | qemu_cond_signal(&qemu_cpu_cond); |
| 1392 | |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 1393 | /* wait for initial kick-off after machine start */ |
Emilio G. Cota | c28e399 | 2015-04-27 12:45:28 -0400 | [diff] [blame] | 1394 | while (first_cpu->stopped) { |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 1395 | qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 8e564b4 | 2012-02-17 18:31:15 +0100 | [diff] [blame] | 1396 | |
| 1397 | /* process any pending work */ |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1398 | CPU_FOREACH(cpu) { |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1399 | current_cpu = cpu; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1400 | qemu_wait_io_event_common(cpu); |
Jan Kiszka | 8e564b4 | 2012-02-17 18:31:15 +0100 | [diff] [blame] | 1401 | } |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1402 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1403 | |
Alex Bennée | 6546706 | 2017-02-23 18:29:09 +0000 | [diff] [blame] | 1404 | start_tcg_kick_timer(); |
| 1405 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1406 | cpu = first_cpu; |
| 1407 | |
Alex Bennée | e5143e3 | 2017-02-23 18:29:12 +0000 | [diff] [blame] | 1408 | /* process any pending work */ |
| 1409 | cpu->exit_request = 1; |
| 1410 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1411 | while (1) { |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1412 | /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ |
| 1413 | qemu_account_warp_timer(); |
| 1414 | |
Paolo Bonzini | 6b8f018 | 2017-03-02 19:56:40 +0100 | [diff] [blame] | 1415 | /* Run the timers here. This is much more efficient than |
| 1416 | * waking up the I/O thread and waiting for completion. |
| 1417 | */ |
| 1418 | handle_icount_deadline(); |
| 1419 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1420 | if (!cpu) { |
| 1421 | cpu = first_cpu; |
| 1422 | } |
| 1423 | |
Alex Bennée | e5143e3 | 2017-02-23 18:29:12 +0000 | [diff] [blame] | 1424 | while (cpu && !cpu->queued_work_first && !cpu->exit_request) { |
| 1425 | |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 1426 | atomic_mb_set(&tcg_current_rr_cpu, cpu); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1427 | current_cpu = cpu; |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1428 | |
| 1429 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, |
| 1430 | (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); |
| 1431 | |
| 1432 | if (cpu_can_run(cpu)) { |
| 1433 | int r; |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1434 | |
| 1435 | prepare_icount_for_run(cpu); |
| 1436 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1437 | r = tcg_cpu_exec(cpu); |
Alex Bennée | 0524838 | 2017-03-29 16:46:59 +0100 | [diff] [blame] | 1438 | |
| 1439 | process_icount_data(cpu); |
| 1440 | |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1441 | if (r == EXCP_DEBUG) { |
| 1442 | cpu_handle_guest_debug(cpu); |
| 1443 | break; |
Pranith Kumar | 08e73c4 | 2017-02-23 18:29:15 +0000 | [diff] [blame] | 1444 | } else if (r == EXCP_ATOMIC) { |
| 1445 | qemu_mutex_unlock_iothread(); |
| 1446 | cpu_exec_step_atomic(cpu); |
| 1447 | qemu_mutex_lock_iothread(); |
| 1448 | break; |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1449 | } |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1450 | } else if (cpu->stop) { |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1451 | if (cpu->unplug) { |
| 1452 | cpu = CPU_NEXT(cpu); |
| 1453 | } |
| 1454 | break; |
| 1455 | } |
| 1456 | |
Alex Bennée | e5143e3 | 2017-02-23 18:29:12 +0000 | [diff] [blame] | 1457 | cpu = CPU_NEXT(cpu); |
| 1458 | } /* while (cpu && !cpu->exit_request).. */ |
| 1459 | |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 1460 | /* Does not need atomic_mb_set because a spurious wakeup is okay. */ |
| 1461 | atomic_set(&tcg_current_rr_cpu, NULL); |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1462 | |
Alex Bennée | e5143e3 | 2017-02-23 18:29:12 +0000 | [diff] [blame] | 1463 | if (cpu && cpu->exit_request) { |
| 1464 | atomic_mb_set(&cpu->exit_request, 0); |
| 1465 | } |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1466 | |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1467 | qemu_tcg_rr_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus)); |
Alex Bennée | c93bbbe | 2016-10-27 16:10:09 +0100 | [diff] [blame] | 1468 | deal_with_unplugged_cpus(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Paolo Bonzini | 9b0605f | 2018-01-30 11:05:06 -0500 | [diff] [blame] | 1471 | rcu_unregister_thread(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1472 | return NULL; |
| 1473 | } |
| 1474 | |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1475 | static void *qemu_hax_cpu_thread_fn(void *arg) |
| 1476 | { |
| 1477 | CPUState *cpu = arg; |
| 1478 | int r; |
Vincent Palatin | b3d3a42 | 2017-03-20 11:15:49 +0100 | [diff] [blame] | 1479 | |
Paolo Bonzini | 9857c2d | 2018-01-30 16:28:49 +0100 | [diff] [blame] | 1480 | rcu_register_thread(); |
Vincent Palatin | b3d3a42 | 2017-03-20 11:15:49 +0100 | [diff] [blame] | 1481 | qemu_mutex_lock_iothread(); |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1482 | qemu_thread_get_self(cpu->thread); |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1483 | |
| 1484 | cpu->thread_id = qemu_get_thread_id(); |
| 1485 | cpu->created = true; |
| 1486 | cpu->halted = 0; |
| 1487 | current_cpu = cpu; |
| 1488 | |
| 1489 | hax_init_vcpu(cpu); |
| 1490 | qemu_cond_signal(&qemu_cpu_cond); |
| 1491 | |
Paolo Bonzini | 9857c2d | 2018-01-30 16:28:49 +0100 | [diff] [blame] | 1492 | do { |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1493 | if (cpu_can_run(cpu)) { |
| 1494 | r = hax_smp_cpu_exec(cpu); |
| 1495 | if (r == EXCP_DEBUG) { |
| 1496 | cpu_handle_guest_debug(cpu); |
| 1497 | } |
| 1498 | } |
| 1499 | |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1500 | qemu_wait_io_event(cpu); |
Paolo Bonzini | 9857c2d | 2018-01-30 16:28:49 +0100 | [diff] [blame] | 1501 | } while (!cpu->unplug || cpu_can_run(cpu)); |
| 1502 | rcu_unregister_thread(); |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1503 | return NULL; |
| 1504 | } |
| 1505 | |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1506 | /* The HVF-specific vCPU thread function. This one should only run when the host |
| 1507 | * CPU supports the VMX "unrestricted guest" feature. */ |
| 1508 | static void *qemu_hvf_cpu_thread_fn(void *arg) |
| 1509 | { |
| 1510 | CPUState *cpu = arg; |
| 1511 | |
| 1512 | int r; |
| 1513 | |
| 1514 | assert(hvf_enabled()); |
| 1515 | |
| 1516 | rcu_register_thread(); |
| 1517 | |
| 1518 | qemu_mutex_lock_iothread(); |
| 1519 | qemu_thread_get_self(cpu->thread); |
| 1520 | |
| 1521 | cpu->thread_id = qemu_get_thread_id(); |
| 1522 | cpu->can_do_io = 1; |
| 1523 | current_cpu = cpu; |
| 1524 | |
| 1525 | hvf_init_vcpu(cpu); |
| 1526 | |
| 1527 | /* signal CPU creation */ |
| 1528 | cpu->created = true; |
| 1529 | qemu_cond_signal(&qemu_cpu_cond); |
| 1530 | |
| 1531 | do { |
| 1532 | if (cpu_can_run(cpu)) { |
| 1533 | r = hvf_vcpu_exec(cpu); |
| 1534 | if (r == EXCP_DEBUG) { |
| 1535 | cpu_handle_guest_debug(cpu); |
| 1536 | } |
| 1537 | } |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1538 | qemu_wait_io_event(cpu); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1539 | } while (!cpu->unplug || cpu_can_run(cpu)); |
| 1540 | |
| 1541 | hvf_vcpu_destroy(cpu); |
| 1542 | cpu->created = false; |
| 1543 | qemu_cond_signal(&qemu_cpu_cond); |
| 1544 | qemu_mutex_unlock_iothread(); |
Paolo Bonzini | 8178e63 | 2018-01-30 11:05:21 -0500 | [diff] [blame] | 1545 | rcu_unregister_thread(); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1546 | return NULL; |
| 1547 | } |
| 1548 | |
Justin Terry (VM) | 1930680 | 2018-01-22 13:07:49 -0800 | [diff] [blame] | 1549 | static void *qemu_whpx_cpu_thread_fn(void *arg) |
| 1550 | { |
| 1551 | CPUState *cpu = arg; |
| 1552 | int r; |
| 1553 | |
| 1554 | rcu_register_thread(); |
| 1555 | |
| 1556 | qemu_mutex_lock_iothread(); |
| 1557 | qemu_thread_get_self(cpu->thread); |
| 1558 | cpu->thread_id = qemu_get_thread_id(); |
| 1559 | current_cpu = cpu; |
| 1560 | |
| 1561 | r = whpx_init_vcpu(cpu); |
| 1562 | if (r < 0) { |
| 1563 | fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r)); |
| 1564 | exit(1); |
| 1565 | } |
| 1566 | |
| 1567 | /* signal CPU creation */ |
| 1568 | cpu->created = true; |
| 1569 | qemu_cond_signal(&qemu_cpu_cond); |
| 1570 | |
| 1571 | do { |
| 1572 | if (cpu_can_run(cpu)) { |
| 1573 | r = whpx_vcpu_exec(cpu); |
| 1574 | if (r == EXCP_DEBUG) { |
| 1575 | cpu_handle_guest_debug(cpu); |
| 1576 | } |
| 1577 | } |
| 1578 | while (cpu_thread_is_idle(cpu)) { |
| 1579 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
| 1580 | } |
| 1581 | qemu_wait_io_event_common(cpu); |
| 1582 | } while (!cpu->unplug || cpu_can_run(cpu)); |
| 1583 | |
| 1584 | whpx_destroy_vcpu(cpu); |
| 1585 | cpu->created = false; |
| 1586 | qemu_cond_signal(&qemu_cpu_cond); |
| 1587 | qemu_mutex_unlock_iothread(); |
| 1588 | rcu_unregister_thread(); |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1589 | return NULL; |
| 1590 | } |
| 1591 | |
| 1592 | #ifdef _WIN32 |
| 1593 | static void CALLBACK dummy_apc_func(ULONG_PTR unused) |
| 1594 | { |
| 1595 | } |
| 1596 | #endif |
| 1597 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1598 | /* Multi-threaded TCG |
| 1599 | * |
| 1600 | * In the multi-threaded case each vCPU has its own thread. The TLS |
| 1601 | * variable current_cpu can be used deep in the code to find the |
| 1602 | * current CPUState for a given thread. |
| 1603 | */ |
| 1604 | |
| 1605 | static void *qemu_tcg_cpu_thread_fn(void *arg) |
| 1606 | { |
| 1607 | CPUState *cpu = arg; |
| 1608 | |
Alex Bennée | bf51c72 | 2017-03-30 18:32:29 +0100 | [diff] [blame] | 1609 | g_assert(!use_icount); |
| 1610 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1611 | rcu_register_thread(); |
Emilio G. Cota | 3468b59 | 2017-07-19 18:57:58 -0400 | [diff] [blame] | 1612 | tcg_register_thread(); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1613 | |
| 1614 | qemu_mutex_lock_iothread(); |
| 1615 | qemu_thread_get_self(cpu->thread); |
| 1616 | |
| 1617 | cpu->thread_id = qemu_get_thread_id(); |
| 1618 | cpu->created = true; |
| 1619 | cpu->can_do_io = 1; |
| 1620 | current_cpu = cpu; |
| 1621 | qemu_cond_signal(&qemu_cpu_cond); |
| 1622 | |
| 1623 | /* process any pending work */ |
| 1624 | cpu->exit_request = 1; |
| 1625 | |
| 1626 | while (1) { |
| 1627 | if (cpu_can_run(cpu)) { |
| 1628 | int r; |
| 1629 | r = tcg_cpu_exec(cpu); |
| 1630 | switch (r) { |
| 1631 | case EXCP_DEBUG: |
| 1632 | cpu_handle_guest_debug(cpu); |
| 1633 | break; |
| 1634 | case EXCP_HALTED: |
| 1635 | /* during start-up the vCPU is reset and the thread is |
| 1636 | * kicked several times. If we don't ensure we go back |
| 1637 | * to sleep in the halted state we won't cleanly |
| 1638 | * start-up when the vCPU is enabled. |
| 1639 | * |
| 1640 | * cpu->halted should ensure we sleep in wait_io_event |
| 1641 | */ |
| 1642 | g_assert(cpu->halted); |
| 1643 | break; |
Pranith Kumar | 08e73c4 | 2017-02-23 18:29:15 +0000 | [diff] [blame] | 1644 | case EXCP_ATOMIC: |
| 1645 | qemu_mutex_unlock_iothread(); |
| 1646 | cpu_exec_step_atomic(cpu); |
| 1647 | qemu_mutex_lock_iothread(); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1648 | default: |
| 1649 | /* Ignore everything else? */ |
| 1650 | break; |
| 1651 | } |
| 1652 | } |
| 1653 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1654 | atomic_mb_set(&cpu->exit_request, 0); |
Paolo Bonzini | db08b68 | 2018-01-11 13:53:12 +0100 | [diff] [blame] | 1655 | qemu_wait_io_event(cpu); |
Paolo Bonzini | 9b0605f | 2018-01-30 11:05:06 -0500 | [diff] [blame] | 1656 | } while (!cpu->unplug || cpu_can_run(cpu)); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1657 | |
Paolo Bonzini | 9b0605f | 2018-01-30 11:05:06 -0500 | [diff] [blame] | 1658 | qemu_tcg_destroy_vcpu(cpu); |
| 1659 | cpu->created = false; |
| 1660 | qemu_cond_signal(&qemu_cpu_cond); |
| 1661 | qemu_mutex_unlock_iothread(); |
| 1662 | rcu_unregister_thread(); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1663 | return NULL; |
| 1664 | } |
| 1665 | |
Andreas Färber | 2ff09a4 | 2012-05-03 00:23:30 +0200 | [diff] [blame] | 1666 | static void qemu_cpu_kick_thread(CPUState *cpu) |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1667 | { |
| 1668 | #ifndef _WIN32 |
| 1669 | int err; |
| 1670 | |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1671 | if (cpu->thread_kicked) { |
| 1672 | return; |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1673 | } |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1674 | cpu->thread_kicked = true; |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1675 | err = pthread_kill(cpu->thread->thread, SIG_IPI); |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1676 | if (err) { |
| 1677 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); |
| 1678 | exit(1); |
| 1679 | } |
| 1680 | #else /* _WIN32 */ |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1681 | if (!qemu_cpu_is_self(cpu)) { |
Justin Terry (VM) | 1930680 | 2018-01-22 13:07:49 -0800 | [diff] [blame] | 1682 | if (whpx_enabled()) { |
| 1683 | whpx_vcpu_kick(cpu); |
| 1684 | } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) { |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1685 | fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n", |
| 1686 | __func__, GetLastError()); |
| 1687 | exit(1); |
| 1688 | } |
| 1689 | } |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1690 | #endif |
| 1691 | } |
| 1692 | |
Andreas Färber | c08d742 | 2012-05-03 04:34:15 +0200 | [diff] [blame] | 1693 | void qemu_cpu_kick(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1694 | { |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1695 | qemu_cond_broadcast(cpu->halt_cond); |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1696 | if (tcg_enabled()) { |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 1697 | cpu_exit(cpu); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1698 | /* NOP unless doing single-thread RR */ |
Alex Bennée | 791158d | 2017-02-23 18:29:10 +0000 | [diff] [blame] | 1699 | qemu_cpu_kick_rr_cpu(); |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1700 | } else { |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1701 | if (hax_enabled()) { |
| 1702 | /* |
| 1703 | * FIXME: race condition with the exit_request check in |
| 1704 | * hax_vcpu_hax_exec |
| 1705 | */ |
| 1706 | cpu->exit_request = 1; |
| 1707 | } |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1708 | qemu_cpu_kick_thread(cpu); |
| 1709 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 1712 | void qemu_cpu_kick_self(void) |
| 1713 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1714 | assert(current_cpu); |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1715 | qemu_cpu_kick_thread(current_cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Andreas Färber | 60e8257 | 2012-05-02 22:23:49 +0200 | [diff] [blame] | 1718 | bool qemu_cpu_is_self(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1719 | { |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1720 | return qemu_thread_is_self(cpu->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Paolo Bonzini | 79e2b9a | 2015-01-21 12:09:14 +0100 | [diff] [blame] | 1723 | bool qemu_in_vcpu_thread(void) |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1724 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1725 | return current_cpu && qemu_cpu_is_self(current_cpu); |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1726 | } |
| 1727 | |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1728 | static __thread bool iothread_locked = false; |
| 1729 | |
| 1730 | bool qemu_mutex_iothread_locked(void) |
| 1731 | { |
| 1732 | return iothread_locked; |
| 1733 | } |
| 1734 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1735 | void qemu_mutex_lock_iothread(void) |
| 1736 | { |
Jan Kiszka | 8d04fb5 | 2017-02-23 18:29:11 +0000 | [diff] [blame] | 1737 | g_assert(!qemu_mutex_iothread_locked()); |
| 1738 | qemu_mutex_lock(&qemu_global_mutex); |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1739 | iothread_locked = true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | void qemu_mutex_unlock_iothread(void) |
| 1743 | { |
Jan Kiszka | 8d04fb5 | 2017-02-23 18:29:11 +0000 | [diff] [blame] | 1744 | g_assert(qemu_mutex_iothread_locked()); |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1745 | iothread_locked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1746 | qemu_mutex_unlock(&qemu_global_mutex); |
| 1747 | } |
| 1748 | |
Alex Bennée | e8faee0 | 2016-10-27 16:09:58 +0100 | [diff] [blame] | 1749 | static bool all_vcpus_paused(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1750 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1751 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1752 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1753 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1754 | if (!cpu->stopped) { |
Alex Bennée | e8faee0 | 2016-10-27 16:09:58 +0100 | [diff] [blame] | 1755 | return false; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1756 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Alex Bennée | e8faee0 | 2016-10-27 16:09:58 +0100 | [diff] [blame] | 1759 | return true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | void pause_all_vcpus(void) |
| 1763 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1764 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1765 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1766 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1767 | CPU_FOREACH(cpu) { |
David Hildenbrand | ebd05fe | 2017-11-29 20:12:15 +0100 | [diff] [blame] | 1768 | if (qemu_cpu_is_self(cpu)) { |
| 1769 | qemu_cpu_stop(cpu, true); |
| 1770 | } else { |
| 1771 | cpu->stop = true; |
| 1772 | qemu_cpu_kick(cpu); |
| 1773 | } |
Jan Kiszka | d798e97 | 2012-02-17 18:31:16 +0100 | [diff] [blame] | 1774 | } |
| 1775 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1776 | while (!all_vcpus_paused()) { |
Paolo Bonzini | be7d6c5 | 2011-03-12 17:44:02 +0100 | [diff] [blame] | 1777 | qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1778 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1779 | qemu_cpu_kick(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
Igor Mammedov | 2993683 | 2013-04-23 10:29:37 +0200 | [diff] [blame] | 1784 | void cpu_resume(CPUState *cpu) |
| 1785 | { |
| 1786 | cpu->stop = false; |
| 1787 | cpu->stopped = false; |
| 1788 | qemu_cpu_kick(cpu); |
| 1789 | } |
| 1790 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1791 | void resume_all_vcpus(void) |
| 1792 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1793 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1794 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1795 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1796 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1797 | cpu_resume(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
Paolo Bonzini | dbadee4 | 2018-01-30 16:40:12 +0100 | [diff] [blame] | 1801 | void cpu_remove_sync(CPUState *cpu) |
Gu Zheng | 4c055ab | 2016-05-12 09:18:13 +0530 | [diff] [blame] | 1802 | { |
| 1803 | cpu->stop = true; |
| 1804 | cpu->unplug = true; |
| 1805 | qemu_cpu_kick(cpu); |
Paolo Bonzini | dbadee4 | 2018-01-30 16:40:12 +0100 | [diff] [blame] | 1806 | qemu_mutex_unlock_iothread(); |
| 1807 | qemu_thread_join(cpu->thread); |
| 1808 | qemu_mutex_lock_iothread(); |
Bharata B Rao | 2c57904 | 2016-05-12 09:18:14 +0530 | [diff] [blame] | 1809 | } |
| 1810 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1811 | /* For temporary buffers for forming a name */ |
| 1812 | #define VCPU_THREAD_NAME_SIZE 16 |
| 1813 | |
Andreas Färber | e5ab30a | 2012-05-03 01:50:44 +0200 | [diff] [blame] | 1814 | static void qemu_tcg_init_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1815 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1816 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1817 | static QemuCond *single_tcg_halt_cond; |
| 1818 | static QemuThread *single_tcg_cpu_thread; |
Emilio G. Cota | e8feb96 | 2017-07-07 19:24:20 -0400 | [diff] [blame] | 1819 | static int tcg_region_inited; |
| 1820 | |
| 1821 | /* |
| 1822 | * Initialize TCG regions--once. Now is a good time, because: |
| 1823 | * (1) TCG's init context, prologue and target globals have been set up. |
| 1824 | * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the |
| 1825 | * -accel flag is processed, so the check doesn't work then). |
| 1826 | */ |
| 1827 | if (!tcg_region_inited) { |
| 1828 | tcg_region_inited = 1; |
| 1829 | tcg_region_init(); |
| 1830 | } |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1831 | |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1832 | if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) { |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1833 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1834 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1835 | qemu_cond_init(cpu->halt_cond); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1836 | |
| 1837 | if (qemu_tcg_mttcg_enabled()) { |
| 1838 | /* create a thread per vCPU with TCG (MTTCG) */ |
| 1839 | parallel_cpus = true; |
| 1840 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG", |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1841 | cpu->cpu_index); |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1842 | |
| 1843 | qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, |
| 1844 | cpu, QEMU_THREAD_JOINABLE); |
| 1845 | |
| 1846 | } else { |
| 1847 | /* share a single thread for all cpus with TCG */ |
| 1848 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG"); |
| 1849 | qemu_thread_create(cpu->thread, thread_name, |
| 1850 | qemu_tcg_rr_cpu_thread_fn, |
| 1851 | cpu, QEMU_THREAD_JOINABLE); |
| 1852 | |
| 1853 | single_tcg_halt_cond = cpu->halt_cond; |
| 1854 | single_tcg_cpu_thread = cpu->thread; |
| 1855 | } |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 1856 | #ifdef _WIN32 |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1857 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 1858 | #endif |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1859 | while (!cpu->created) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1860 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1861 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1862 | } else { |
Alex Bennée | 3725794 | 2017-02-23 18:29:14 +0000 | [diff] [blame] | 1863 | /* For non-MTTCG cases we share the thread */ |
| 1864 | cpu->thread = single_tcg_cpu_thread; |
| 1865 | cpu->halt_cond = single_tcg_halt_cond; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1869 | static void qemu_hax_start_vcpu(CPUState *cpu) |
| 1870 | { |
| 1871 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1872 | |
| 1873 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
| 1874 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1875 | qemu_cond_init(cpu->halt_cond); |
| 1876 | |
| 1877 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX", |
| 1878 | cpu->cpu_index); |
| 1879 | qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn, |
| 1880 | cpu, QEMU_THREAD_JOINABLE); |
| 1881 | #ifdef _WIN32 |
| 1882 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
| 1883 | #endif |
| 1884 | while (!cpu->created) { |
| 1885 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
| 1886 | } |
| 1887 | } |
| 1888 | |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1889 | static void qemu_kvm_start_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1890 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1891 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1892 | |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1893 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1894 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1895 | qemu_cond_init(cpu->halt_cond); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1896 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM", |
| 1897 | cpu->cpu_index); |
| 1898 | qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn, |
| 1899 | cpu, QEMU_THREAD_JOINABLE); |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1900 | while (!cpu->created) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1901 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1902 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1905 | static void qemu_hvf_start_vcpu(CPUState *cpu) |
| 1906 | { |
| 1907 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1908 | |
| 1909 | /* HVF currently does not support TCG, and only runs in |
| 1910 | * unrestricted-guest mode. */ |
| 1911 | assert(hvf_enabled()); |
| 1912 | |
| 1913 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
| 1914 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1915 | qemu_cond_init(cpu->halt_cond); |
| 1916 | |
| 1917 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF", |
| 1918 | cpu->cpu_index); |
| 1919 | qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn, |
| 1920 | cpu, QEMU_THREAD_JOINABLE); |
| 1921 | while (!cpu->created) { |
| 1922 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
| 1923 | } |
| 1924 | } |
| 1925 | |
Justin Terry (VM) | 1930680 | 2018-01-22 13:07:49 -0800 | [diff] [blame] | 1926 | static void qemu_whpx_start_vcpu(CPUState *cpu) |
| 1927 | { |
| 1928 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1929 | |
| 1930 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
| 1931 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1932 | qemu_cond_init(cpu->halt_cond); |
| 1933 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX", |
| 1934 | cpu->cpu_index); |
| 1935 | qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn, |
| 1936 | cpu, QEMU_THREAD_JOINABLE); |
| 1937 | #ifdef _WIN32 |
| 1938 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
| 1939 | #endif |
| 1940 | while (!cpu->created) { |
| 1941 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
| 1942 | } |
| 1943 | } |
| 1944 | |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1945 | static void qemu_dummy_start_vcpu(CPUState *cpu) |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1946 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1947 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1948 | |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1949 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1950 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1951 | qemu_cond_init(cpu->halt_cond); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1952 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY", |
| 1953 | cpu->cpu_index); |
| 1954 | qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu, |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1955 | QEMU_THREAD_JOINABLE); |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1956 | while (!cpu->created) { |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1957 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
| 1958 | } |
| 1959 | } |
| 1960 | |
Andreas Färber | c643bed | 2013-05-27 03:23:24 +0200 | [diff] [blame] | 1961 | void qemu_init_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1962 | { |
Andreas Färber | ce3960e | 2012-12-17 03:27:07 +0100 | [diff] [blame] | 1963 | cpu->nr_cores = smp_cores; |
| 1964 | cpu->nr_threads = smp_threads; |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 1965 | cpu->stopped = true; |
Peter Maydell | 56943e8 | 2016-01-21 14:15:04 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (!cpu->as) { |
| 1968 | /* If the target cpu hasn't set up any address spaces itself, |
| 1969 | * give it the default one. |
| 1970 | */ |
Peter Maydell | 12ebc9a | 2016-01-21 14:15:04 +0000 | [diff] [blame] | 1971 | cpu->num_ases = 1; |
Peter Xu | 80ceb07 | 2017-11-23 17:23:32 +0800 | [diff] [blame] | 1972 | cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory); |
Peter Maydell | 56943e8 | 2016-01-21 14:15:04 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1975 | if (kvm_enabled()) { |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1976 | qemu_kvm_start_vcpu(cpu); |
Vincent Palatin | b0cb0a6 | 2017-01-10 11:59:57 +0100 | [diff] [blame] | 1977 | } else if (hax_enabled()) { |
| 1978 | qemu_hax_start_vcpu(cpu); |
Sergio Andres Gomez Del Real | c97d6d2 | 2017-09-13 04:05:09 -0500 | [diff] [blame] | 1979 | } else if (hvf_enabled()) { |
| 1980 | qemu_hvf_start_vcpu(cpu); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1981 | } else if (tcg_enabled()) { |
Andreas Färber | e5ab30a | 2012-05-03 01:50:44 +0200 | [diff] [blame] | 1982 | qemu_tcg_init_vcpu(cpu); |
Justin Terry (VM) | 1930680 | 2018-01-22 13:07:49 -0800 | [diff] [blame] | 1983 | } else if (whpx_enabled()) { |
| 1984 | qemu_whpx_start_vcpu(cpu); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1985 | } else { |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1986 | qemu_dummy_start_vcpu(cpu); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1987 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1990 | void cpu_stop_current(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1991 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1992 | if (current_cpu) { |
David Hildenbrand | ebd05fe | 2017-11-29 20:12:15 +0100 | [diff] [blame] | 1993 | qemu_cpu_stop(current_cpu, true); |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1994 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1997 | int vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1998 | { |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1999 | if (qemu_in_vcpu_thread()) { |
Paolo Bonzini | 74892d2 | 2014-06-05 14:53:58 +0200 | [diff] [blame] | 2000 | qemu_system_vmstop_request_prepare(); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 2001 | qemu_system_vmstop_request(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 2002 | /* |
| 2003 | * FIXME: should not return to device code in case |
| 2004 | * vm_stop() has been requested. |
| 2005 | */ |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 2006 | cpu_stop_current(); |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 2007 | return 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 2008 | } |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 2009 | |
| 2010 | return do_vm_stop(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Claudio Imbrenda | 2d76e82 | 2017-02-14 18:07:47 +0100 | [diff] [blame] | 2013 | /** |
| 2014 | * Prepare for (re)starting the VM. |
| 2015 | * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already |
| 2016 | * running or in case of an error condition), 0 otherwise. |
| 2017 | */ |
| 2018 | int vm_prepare_start(void) |
| 2019 | { |
| 2020 | RunState requested; |
| 2021 | int res = 0; |
| 2022 | |
| 2023 | qemu_vmstop_requested(&requested); |
| 2024 | if (runstate_is_running() && requested == RUN_STATE__MAX) { |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
| 2028 | /* Ensure that a STOP/RESUME pair of events is emitted if a |
| 2029 | * vmstop request was pending. The BLOCK_IO_ERROR event, for |
| 2030 | * example, according to documentation is always followed by |
| 2031 | * the STOP event. |
| 2032 | */ |
| 2033 | if (runstate_is_running()) { |
| 2034 | qapi_event_send_stop(&error_abort); |
| 2035 | res = -1; |
| 2036 | } else { |
| 2037 | replay_enable_events(); |
| 2038 | cpu_enable_ticks(); |
| 2039 | runstate_set(RUN_STATE_RUNNING); |
| 2040 | vm_state_notify(1, RUN_STATE_RUNNING); |
| 2041 | } |
| 2042 | |
| 2043 | /* We are sending this now, but the CPUs will be resumed shortly later */ |
| 2044 | qapi_event_send_resume(&error_abort); |
| 2045 | return res; |
| 2046 | } |
| 2047 | |
| 2048 | void vm_start(void) |
| 2049 | { |
| 2050 | if (!vm_prepare_start()) { |
| 2051 | resume_all_vcpus(); |
| 2052 | } |
| 2053 | } |
| 2054 | |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 2055 | /* does a state transition even if the VM is already stopped, |
| 2056 | current state is forgotten forever */ |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 2057 | int vm_stop_force_state(RunState state) |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 2058 | { |
| 2059 | if (runstate_is_running()) { |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 2060 | return vm_stop(state); |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 2061 | } else { |
| 2062 | runstate_set(state); |
Wen Congyang | b2780d3 | 2015-11-20 17:34:38 +0800 | [diff] [blame] | 2063 | |
| 2064 | bdrv_drain_all(); |
Kevin Wolf | 594a45c | 2013-07-18 14:52:19 +0200 | [diff] [blame] | 2065 | /* Make sure to return an error if the flush in a previous vm_stop() |
| 2066 | * failed. */ |
John Snow | 22af08e | 2016-09-22 21:45:51 -0400 | [diff] [blame] | 2067 | return bdrv_flush_all(); |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 2071 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 2072 | { |
| 2073 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
Peter Maydell | e916cbf | 2012-09-05 17:41:08 -0300 | [diff] [blame] | 2074 | #if defined(cpu_list) |
| 2075 | cpu_list(f, cpu_fprintf); |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 2076 | #endif |
| 2077 | } |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2078 | |
| 2079 | CpuInfoList *qmp_query_cpus(Error **errp) |
| 2080 | { |
Igor Mammedov | afed5a5 | 2017-05-10 13:29:55 +0200 | [diff] [blame] | 2081 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 2082 | MachineClass *mc = MACHINE_GET_CLASS(ms); |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2083 | CpuInfoList *head = NULL, *cur_item = NULL; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 2084 | CPUState *cpu; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2085 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 2086 | CPU_FOREACH(cpu) { |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2087 | CpuInfoList *info; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 2088 | #if defined(TARGET_I386) |
| 2089 | X86CPU *x86_cpu = X86_CPU(cpu); |
| 2090 | CPUX86State *env = &x86_cpu->env; |
| 2091 | #elif defined(TARGET_PPC) |
| 2092 | PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu); |
| 2093 | CPUPPCState *env = &ppc_cpu->env; |
| 2094 | #elif defined(TARGET_SPARC) |
| 2095 | SPARCCPU *sparc_cpu = SPARC_CPU(cpu); |
| 2096 | CPUSPARCState *env = &sparc_cpu->env; |
| 2097 | #elif defined(TARGET_MIPS) |
| 2098 | MIPSCPU *mips_cpu = MIPS_CPU(cpu); |
| 2099 | CPUMIPSState *env = &mips_cpu->env; |
Bastian Koppelmann | 48e06fe | 2014-09-01 12:59:46 +0100 | [diff] [blame] | 2100 | #elif defined(TARGET_TRICORE) |
| 2101 | TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu); |
| 2102 | CPUTriCoreState *env = &tricore_cpu->env; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 2103 | #endif |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2104 | |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 2105 | cpu_synchronize_state(cpu); |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2106 | |
| 2107 | info = g_malloc0(sizeof(*info)); |
| 2108 | info->value = g_malloc0(sizeof(*info->value)); |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 2109 | info->value->CPU = cpu->cpu_index; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 2110 | info->value->current = (cpu == first_cpu); |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 2111 | info->value->halted = cpu->halted; |
Eduardo Habkost | 58f88d4 | 2015-05-08 16:04:22 -0300 | [diff] [blame] | 2112 | info->value->qom_path = object_get_canonical_path(OBJECT(cpu)); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 2113 | info->value->thread_id = cpu->thread_id; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2114 | #if defined(TARGET_I386) |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2115 | info->value->arch = CPU_INFO_ARCH_X86; |
Eric Blake | 544a373 | 2016-02-17 23:48:27 -0700 | [diff] [blame] | 2116 | info->value->u.x86.pc = env->eip + env->segs[R_CS].base; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2117 | #elif defined(TARGET_PPC) |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2118 | info->value->arch = CPU_INFO_ARCH_PPC; |
Eric Blake | 544a373 | 2016-02-17 23:48:27 -0700 | [diff] [blame] | 2119 | info->value->u.ppc.nip = env->nip; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2120 | #elif defined(TARGET_SPARC) |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2121 | info->value->arch = CPU_INFO_ARCH_SPARC; |
Eric Blake | 544a373 | 2016-02-17 23:48:27 -0700 | [diff] [blame] | 2122 | info->value->u.q_sparc.pc = env->pc; |
| 2123 | info->value->u.q_sparc.npc = env->npc; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2124 | #elif defined(TARGET_MIPS) |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2125 | info->value->arch = CPU_INFO_ARCH_MIPS; |
Eric Blake | 544a373 | 2016-02-17 23:48:27 -0700 | [diff] [blame] | 2126 | info->value->u.q_mips.PC = env->active_tc.PC; |
Bastian Koppelmann | 48e06fe | 2014-09-01 12:59:46 +0100 | [diff] [blame] | 2127 | #elif defined(TARGET_TRICORE) |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2128 | info->value->arch = CPU_INFO_ARCH_TRICORE; |
Eric Blake | 544a373 | 2016-02-17 23:48:27 -0700 | [diff] [blame] | 2129 | info->value->u.tricore.PC = env->PC; |
Eric Blake | 86f4b68 | 2015-11-18 01:52:59 -0700 | [diff] [blame] | 2130 | #else |
| 2131 | info->value->arch = CPU_INFO_ARCH_OTHER; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2132 | #endif |
Igor Mammedov | afed5a5 | 2017-05-10 13:29:55 +0200 | [diff] [blame] | 2133 | info->value->has_props = !!mc->cpu_index_to_instance_props; |
| 2134 | if (info->value->has_props) { |
| 2135 | CpuInstanceProperties *props; |
| 2136 | props = g_malloc0(sizeof(*props)); |
| 2137 | *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index); |
| 2138 | info->value->props = props; |
| 2139 | } |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 2140 | |
| 2141 | /* XXX: waiting for the qapi to support GSList */ |
| 2142 | if (!cur_item) { |
| 2143 | head = cur_item = info; |
| 2144 | } else { |
| 2145 | cur_item->next = info; |
| 2146 | cur_item = info; |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | return head; |
| 2151 | } |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2152 | |
| 2153 | void qmp_memsave(int64_t addr, int64_t size, const char *filename, |
| 2154 | bool has_cpu, int64_t cpu_index, Error **errp) |
| 2155 | { |
| 2156 | FILE *f; |
| 2157 | uint32_t l; |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 2158 | CPUState *cpu; |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2159 | uint8_t buf[1024]; |
Borislav Petkov | 0dc9daf | 2015-02-08 13:14:38 +0100 | [diff] [blame] | 2160 | int64_t orig_addr = addr, orig_size = size; |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2161 | |
| 2162 | if (!has_cpu) { |
| 2163 | cpu_index = 0; |
| 2164 | } |
| 2165 | |
Andreas Färber | 151d132 | 2013-02-15 15:41:49 +0100 | [diff] [blame] | 2166 | cpu = qemu_get_cpu(cpu_index); |
| 2167 | if (cpu == NULL) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2168 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
| 2169 | "a CPU number"); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2170 | return; |
| 2171 | } |
| 2172 | |
| 2173 | f = fopen(filename, "wb"); |
| 2174 | if (!f) { |
Luiz Capitulino | 618da85 | 2013-06-07 14:35:06 -0400 | [diff] [blame] | 2175 | error_setg_file_open(errp, errno, filename); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2176 | return; |
| 2177 | } |
| 2178 | |
| 2179 | while (size != 0) { |
| 2180 | l = sizeof(buf); |
| 2181 | if (l > size) |
| 2182 | l = size; |
Aneesh Kumar K.V | 2f4d0f5 | 2013-10-01 21:49:30 +0530 | [diff] [blame] | 2183 | if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) { |
Borislav Petkov | 0dc9daf | 2015-02-08 13:14:38 +0100 | [diff] [blame] | 2184 | error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64 |
| 2185 | " specified", orig_addr, orig_size); |
Aneesh Kumar K.V | 2f4d0f5 | 2013-10-01 21:49:30 +0530 | [diff] [blame] | 2186 | goto exit; |
| 2187 | } |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2188 | if (fwrite(buf, 1, l, f) != l) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2189 | error_setg(errp, QERR_IO_ERROR); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 2190 | goto exit; |
| 2191 | } |
| 2192 | addr += l; |
| 2193 | size -= l; |
| 2194 | } |
| 2195 | |
| 2196 | exit: |
| 2197 | fclose(f); |
| 2198 | } |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 2199 | |
| 2200 | void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, |
| 2201 | Error **errp) |
| 2202 | { |
| 2203 | FILE *f; |
| 2204 | uint32_t l; |
| 2205 | uint8_t buf[1024]; |
| 2206 | |
| 2207 | f = fopen(filename, "wb"); |
| 2208 | if (!f) { |
Luiz Capitulino | 618da85 | 2013-06-07 14:35:06 -0400 | [diff] [blame] | 2209 | error_setg_file_open(errp, errno, filename); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 2210 | return; |
| 2211 | } |
| 2212 | |
| 2213 | while (size != 0) { |
| 2214 | l = sizeof(buf); |
| 2215 | if (l > size) |
| 2216 | l = size; |
Stefan Weil | eb6282f | 2014-04-07 20:28:23 +0200 | [diff] [blame] | 2217 | cpu_physical_memory_read(addr, buf, l); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 2218 | if (fwrite(buf, 1, l, f) != l) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2219 | error_setg(errp, QERR_IO_ERROR); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 2220 | goto exit; |
| 2221 | } |
| 2222 | addr += l; |
| 2223 | size -= l; |
| 2224 | } |
| 2225 | |
| 2226 | exit: |
| 2227 | fclose(f); |
| 2228 | } |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 2229 | |
| 2230 | void qmp_inject_nmi(Error **errp) |
| 2231 | { |
Alexey Kardashevskiy | 9cb805f | 2014-08-20 22:16:33 +1000 | [diff] [blame] | 2232 | nmi_monitor_handle(monitor_get_cpu_index(), errp); |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 2233 | } |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 2234 | |
| 2235 | void dump_drift_info(FILE *f, fprintf_function cpu_fprintf) |
| 2236 | { |
| 2237 | if (!use_icount) { |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n", |
| 2242 | (cpu_get_clock() - cpu_get_icount())/SCALE_MS); |
| 2243 | if (icount_align_option) { |
| 2244 | cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS); |
| 2245 | cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS); |
| 2246 | } else { |
| 2247 | cpu_fprintf(f, "Max guest delay NA\n"); |
| 2248 | cpu_fprintf(f, "Max guest advance NA\n"); |
| 2249 | } |
| 2250 | } |