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