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