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