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