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