Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* Needed early for CONFIG_BSD etc. */ |
| 26 | #include "config-host.h" |
| 27 | |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 28 | #include "monitor/monitor.h" |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 29 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 30 | #include "qemu/error-report.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 31 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 32 | #include "exec/gdbstub.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 33 | #include "sysemu/dma.h" |
| 34 | #include "sysemu/kvm.h" |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 35 | #include "qmp-commands.h" |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 36 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/thread.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 38 | #include "sysemu/cpus.h" |
| 39 | #include "sysemu/qtest.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 40 | #include "qemu/main-loop.h" |
| 41 | #include "qemu/bitmap.h" |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 42 | #include "qemu/seqlock.h" |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 43 | #include "qapi-event.h" |
Alexey Kardashevskiy | 9cb805f | 2014-08-20 22:16:33 +1000 | [diff] [blame] | 44 | #include "hw/nmi.h" |
Jan Kiszka | 0ff0fc1 | 2011-06-23 10:15:55 +0200 | [diff] [blame] | 45 | |
| 46 | #ifndef _WIN32 |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 47 | #include "qemu/compatfd.h" |
Jan Kiszka | 0ff0fc1 | 2011-06-23 10:15:55 +0200 | [diff] [blame] | 48 | #endif |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 49 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 50 | #ifdef CONFIG_LINUX |
| 51 | |
| 52 | #include <sys/prctl.h> |
| 53 | |
Marcelo Tosatti | c0532a7 | 2010-10-11 15:31:21 -0300 | [diff] [blame] | 54 | #ifndef PR_MCE_KILL |
| 55 | #define PR_MCE_KILL 33 |
| 56 | #endif |
| 57 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 58 | #ifndef PR_MCE_KILL_SET |
| 59 | #define PR_MCE_KILL_SET 1 |
| 60 | #endif |
| 61 | |
| 62 | #ifndef PR_MCE_KILL_EARLY |
| 63 | #define PR_MCE_KILL_EARLY 1 |
| 64 | #endif |
| 65 | |
| 66 | #endif /* CONFIG_LINUX */ |
| 67 | |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 68 | static CPUState *next_cpu; |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 69 | int64_t max_delay; |
| 70 | int64_t max_advance; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 71 | |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame^] | 72 | /* vcpu throttling controls */ |
| 73 | static QEMUTimer *throttle_timer; |
| 74 | static unsigned int throttle_percentage; |
| 75 | |
| 76 | #define CPU_THROTTLE_PCT_MIN 1 |
| 77 | #define CPU_THROTTLE_PCT_MAX 99 |
| 78 | #define CPU_THROTTLE_TIMESLICE_NS 10000000 |
| 79 | |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 80 | bool cpu_is_stopped(CPUState *cpu) |
| 81 | { |
| 82 | return cpu->stopped || !runstate_is_running(); |
| 83 | } |
| 84 | |
Andreas Färber | a98ae1d | 2013-05-26 23:21:08 +0200 | [diff] [blame] | 85 | static bool cpu_thread_is_idle(CPUState *cpu) |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 86 | { |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 87 | if (cpu->stop || cpu->queued_work_first) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 88 | return false; |
| 89 | } |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 90 | if (cpu_is_stopped(cpu)) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 91 | return true; |
| 92 | } |
Andreas Färber | 8c2e1b0 | 2013-08-25 18:53:55 +0200 | [diff] [blame] | 93 | if (!cpu->halted || cpu_has_work(cpu) || |
Alexander Graf | 215e79c | 2013-04-24 22:24:12 +0200 | [diff] [blame] | 94 | kvm_halt_in_kernel()) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | static bool all_cpu_threads_idle(void) |
| 101 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 102 | CPUState *cpu; |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 103 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 104 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 105 | if (!cpu_thread_is_idle(cpu)) { |
Peter Maydell | ac873f1 | 2012-07-19 16:52:27 +0100 | [diff] [blame] | 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 112 | /***********************************************************/ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 113 | /* guest cycle counter */ |
| 114 | |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 115 | /* Protected by TimersState seqlock */ |
| 116 | |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 117 | static bool icount_sleep = true; |
Sebastian Tanase | 7146839 | 2014-07-23 11:47:50 +0200 | [diff] [blame] | 118 | static int64_t vm_clock_warp_start = -1; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 119 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
| 120 | static int icount_time_shift; |
| 121 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
| 122 | #define MAX_ICOUNT_SHIFT 10 |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 123 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 124 | static QEMUTimer *icount_rt_timer; |
| 125 | static QEMUTimer *icount_vm_timer; |
| 126 | static QEMUTimer *icount_warp_timer; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 127 | |
| 128 | typedef struct TimersState { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 129 | /* Protected by BQL. */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 130 | int64_t cpu_ticks_prev; |
| 131 | int64_t cpu_ticks_offset; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 132 | |
| 133 | /* cpu_clock_offset can be read out of BQL, so protect it with |
| 134 | * this lock. |
| 135 | */ |
| 136 | QemuSeqLock vm_clock_seqlock; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 137 | int64_t cpu_clock_offset; |
| 138 | int32_t cpu_ticks_enabled; |
| 139 | int64_t dummy; |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 140 | |
| 141 | /* Compensate for varying guest execution speed. */ |
| 142 | int64_t qemu_icount_bias; |
| 143 | /* Only written by TCG thread */ |
| 144 | int64_t qemu_icount; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 145 | } TimersState; |
| 146 | |
Liu Ping Fan | d9cd400 | 2013-07-21 08:43:00 +0000 | [diff] [blame] | 147 | static TimersState timers_state; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 148 | |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 149 | int64_t cpu_get_icount_raw(void) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 150 | { |
| 151 | int64_t icount; |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 152 | CPUState *cpu = current_cpu; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 153 | |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 154 | icount = timers_state.qemu_icount; |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 155 | if (cpu) { |
Paolo Bonzini | 414b15c | 2015-06-24 14:16:26 +0200 | [diff] [blame] | 156 | if (!cpu->can_do_io) { |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 157 | fprintf(stderr, "Bad icount read\n"); |
| 158 | exit(1); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 159 | } |
Andreas Färber | 28ecfd7 | 2013-08-26 05:51:49 +0200 | [diff] [blame] | 160 | icount -= (cpu->icount_decr.u16.low + cpu->icount_extra); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 161 | } |
Pavel Dovgalyuk | 2a62914 | 2014-12-08 10:53:45 +0300 | [diff] [blame] | 162 | return icount; |
| 163 | } |
| 164 | |
| 165 | /* Return the virtual CPU time, based on the instruction counter. */ |
| 166 | static int64_t cpu_get_icount_locked(void) |
| 167 | { |
| 168 | int64_t icount = cpu_get_icount_raw(); |
KONRAD Frederic | 3f03131 | 2014-08-01 01:37:15 +0200 | [diff] [blame] | 169 | return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 172 | int64_t cpu_get_icount(void) |
| 173 | { |
| 174 | int64_t icount; |
| 175 | unsigned start; |
| 176 | |
| 177 | do { |
| 178 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); |
| 179 | icount = cpu_get_icount_locked(); |
| 180 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); |
| 181 | |
| 182 | return icount; |
| 183 | } |
| 184 | |
KONRAD Frederic | 3f03131 | 2014-08-01 01:37:15 +0200 | [diff] [blame] | 185 | int64_t cpu_icount_to_ns(int64_t icount) |
| 186 | { |
| 187 | return icount << icount_time_shift; |
| 188 | } |
| 189 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 190 | /* return the host CPU cycle counter and handle stop/restart */ |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 191 | /* Caller must hold the BQL */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 192 | int64_t cpu_get_ticks(void) |
| 193 | { |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 194 | int64_t ticks; |
| 195 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 196 | if (use_icount) { |
| 197 | return cpu_get_icount(); |
| 198 | } |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 199 | |
| 200 | ticks = timers_state.cpu_ticks_offset; |
| 201 | if (timers_state.cpu_ticks_enabled) { |
| 202 | ticks += cpu_get_real_ticks(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 203 | } |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 204 | |
| 205 | if (timers_state.cpu_ticks_prev > ticks) { |
| 206 | /* Note: non increasing ticks may happen if the host uses |
| 207 | software suspend */ |
| 208 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
| 209 | ticks = timers_state.cpu_ticks_prev; |
| 210 | } |
| 211 | |
| 212 | timers_state.cpu_ticks_prev = ticks; |
| 213 | return ticks; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 216 | static int64_t cpu_get_clock_locked(void) |
| 217 | { |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 218 | int64_t ticks; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 219 | |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 220 | ticks = timers_state.cpu_clock_offset; |
| 221 | if (timers_state.cpu_ticks_enabled) { |
| 222 | ticks += get_clock(); |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 225 | return ticks; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 228 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 229 | int64_t cpu_get_clock(void) |
| 230 | { |
| 231 | int64_t ti; |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 232 | unsigned start; |
| 233 | |
| 234 | do { |
| 235 | start = seqlock_read_begin(&timers_state.vm_clock_seqlock); |
| 236 | ti = cpu_get_clock_locked(); |
| 237 | } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start)); |
| 238 | |
| 239 | return ti; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 242 | /* enable cpu_get_ticks() |
| 243 | * Caller must hold BQL which server as mutex for vm_clock_seqlock. |
| 244 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 245 | void cpu_enable_ticks(void) |
| 246 | { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 247 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
| 248 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 249 | if (!timers_state.cpu_ticks_enabled) { |
| 250 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 251 | timers_state.cpu_clock_offset -= get_clock(); |
| 252 | timers_state.cpu_ticks_enabled = 1; |
| 253 | } |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 254 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* 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] | 258 | * cpu_get_ticks() after that. |
| 259 | * Caller must hold BQL which server as mutex for vm_clock_seqlock. |
| 260 | */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 261 | void cpu_disable_ticks(void) |
| 262 | { |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 263 | /* Here, the really thing protected by seqlock is cpu_clock_offset. */ |
| 264 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 265 | if (timers_state.cpu_ticks_enabled) { |
Paolo Bonzini | 5f3e310 | 2013-10-28 17:32:18 +0100 | [diff] [blame] | 266 | timers_state.cpu_ticks_offset += cpu_get_real_ticks(); |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 267 | timers_state.cpu_clock_offset = cpu_get_clock_locked(); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 268 | timers_state.cpu_ticks_enabled = 0; |
| 269 | } |
Liu Ping Fan | cb36564 | 2013-09-25 14:20:58 +0800 | [diff] [blame] | 270 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* Correlation between real and virtual time is always going to be |
| 274 | fairly approximate, so ignore small variation. |
| 275 | When the guest is idle real and virtual time will be aligned in |
| 276 | the IO wait loop. */ |
| 277 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
| 278 | |
| 279 | static void icount_adjust(void) |
| 280 | { |
| 281 | int64_t cur_time; |
| 282 | int64_t cur_icount; |
| 283 | int64_t delta; |
Paolo Bonzini | a3270e1 | 2013-10-07 17:18:15 +0200 | [diff] [blame] | 284 | |
| 285 | /* Protected by TimersState mutex. */ |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 286 | static int64_t last_delta; |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 287 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 288 | /* If the VM is not running, then do nothing. */ |
| 289 | if (!runstate_is_running()) { |
| 290 | return; |
| 291 | } |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 292 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 293 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
| 294 | cur_time = cpu_get_clock_locked(); |
| 295 | cur_icount = cpu_get_icount_locked(); |
Paolo Bonzini | 468cc7c | 2013-10-07 17:21:51 +0200 | [diff] [blame] | 296 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 297 | delta = cur_icount - cur_time; |
| 298 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 299 | if (delta > 0 |
| 300 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 301 | && icount_time_shift > 0) { |
| 302 | /* The guest is getting too far ahead. Slow time down. */ |
| 303 | icount_time_shift--; |
| 304 | } |
| 305 | if (delta < 0 |
| 306 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 307 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 308 | /* The guest is getting too far behind. Speed time up. */ |
| 309 | icount_time_shift++; |
| 310 | } |
| 311 | last_delta = delta; |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 312 | timers_state.qemu_icount_bias = cur_icount |
| 313 | - (timers_state.qemu_icount << icount_time_shift); |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 314 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void icount_adjust_rt(void *opaque) |
| 318 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 319 | timer_mod(icount_rt_timer, |
Pavel Dovgalyuk | 1979b90 | 2015-01-12 15:00:43 +0300 | [diff] [blame] | 320 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 321 | icount_adjust(); |
| 322 | } |
| 323 | |
| 324 | static void icount_adjust_vm(void *opaque) |
| 325 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 326 | timer_mod(icount_vm_timer, |
| 327 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
| 328 | get_ticks_per_sec() / 10); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 329 | icount_adjust(); |
| 330 | } |
| 331 | |
| 332 | static int64_t qemu_icount_round(int64_t count) |
| 333 | { |
| 334 | return (count + (1 << icount_time_shift) - 1) >> icount_time_shift; |
| 335 | } |
| 336 | |
| 337 | static void icount_warp_rt(void *opaque) |
| 338 | { |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 339 | /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start |
| 340 | * changes from -1 to another value, so the race here is okay. |
| 341 | */ |
| 342 | if (atomic_read(&vm_clock_warp_start) == -1) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 346 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 347 | if (runstate_is_running()) { |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 348 | int64_t clock = cpu_get_clock_locked(); |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 349 | int64_t warp_delta; |
| 350 | |
| 351 | warp_delta = clock - vm_clock_warp_start; |
| 352 | if (use_icount == 2) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 353 | /* |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 354 | * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 355 | * far ahead of real time. |
| 356 | */ |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 357 | int64_t cur_icount = cpu_get_icount_locked(); |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 358 | int64_t delta = clock - cur_icount; |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 359 | warp_delta = MIN(warp_delta, delta); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 360 | } |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 361 | timers_state.qemu_icount_bias += warp_delta; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 362 | } |
| 363 | vm_clock_warp_start = -1; |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 364 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
Paolo Bonzini | 8ed961d | 2013-10-07 17:26:07 +0200 | [diff] [blame] | 365 | |
| 366 | if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) { |
| 367 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
| 368 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 371 | void qtest_clock_warp(int64_t dest) |
| 372 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 373 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 374 | AioContext *aio_context; |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 375 | assert(qtest_enabled()); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 376 | aio_context = qemu_get_aio_context(); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 377 | while (clock < dest) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 378 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Sergey Fedorov | c9299e2 | 2014-06-10 13:10:28 +0400 | [diff] [blame] | 379 | int64_t warp = qemu_soonest_timeout(dest - clock, deadline); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 380 | |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 381 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 382 | timers_state.qemu_icount_bias += warp; |
Paolo Bonzini | 17a15f1 | 2013-10-03 15:17:25 +0200 | [diff] [blame] | 383 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
| 384 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 385 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
Fam Zheng | efef88b | 2015-01-19 17:51:43 +0800 | [diff] [blame] | 386 | timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 387 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 388 | } |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 389 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 392 | void qemu_clock_warp(QEMUClockType type) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 393 | { |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 394 | int64_t clock; |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 395 | int64_t deadline; |
| 396 | |
| 397 | /* |
| 398 | * There are too many global variables to make the "warp" behavior |
| 399 | * applicable to other clocks. But a clock argument removes the |
| 400 | * need for if statements all over the place. |
| 401 | */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 402 | if (type != QEMU_CLOCK_VIRTUAL || !use_icount) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 406 | if (icount_sleep) { |
| 407 | /* |
| 408 | * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now. |
| 409 | * This ensures that the deadline for the timer is computed correctly |
| 410 | * below. |
| 411 | * This also makes sure that the insn counter is synchronized before |
| 412 | * the CPU starts running, in case the CPU is woken by an event other |
| 413 | * than the earliest QEMU_CLOCK_VIRTUAL timer. |
| 414 | */ |
| 415 | icount_warp_rt(NULL); |
| 416 | timer_del(icount_warp_timer); |
| 417 | } |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 418 | if (!all_cpu_threads_idle()) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 422 | if (qtest_enabled()) { |
| 423 | /* When testing, qtest commands advance icount. */ |
| 424 | return; |
| 425 | } |
| 426 | |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 427 | /* We want to use the earliest deadline from ALL vm_clocks */ |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 428 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 429 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 430 | if (deadline < 0) { |
Victor CLEMENT | d7a0f71 | 2015-05-29 17:14:06 +0200 | [diff] [blame] | 431 | static bool notified; |
| 432 | if (!icount_sleep && !notified) { |
| 433 | error_report("WARNING: icount sleep disabled and no active timers"); |
| 434 | notified = true; |
| 435 | } |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 436 | return; |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 439 | if (deadline > 0) { |
| 440 | /* |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 441 | * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 442 | * sleep. Otherwise, the CPU might be waiting for a future timer |
| 443 | * interrupt to wake it up, but the interrupt never comes because |
| 444 | * 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] | 445 | * QEMU_CLOCK_VIRTUAL. |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 446 | */ |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 447 | if (!icount_sleep) { |
| 448 | /* |
| 449 | * We never let VCPUs sleep in no sleep icount mode. |
| 450 | * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance |
| 451 | * to the next QEMU_CLOCK_VIRTUAL event and notify it. |
| 452 | * It is useful when we want a deterministic execution time, |
| 453 | * isolated from host latencies. |
| 454 | */ |
| 455 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
| 456 | timers_state.qemu_icount_bias += deadline; |
| 457 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
| 458 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
| 459 | } else { |
| 460 | /* |
| 461 | * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some |
| 462 | * "real" time, (related to the time left until the next event) has |
| 463 | * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this. |
| 464 | * This avoids that the warps are visible externally; for example, |
| 465 | * you will not be sending network packets continuously instead of |
| 466 | * every 100ms. |
| 467 | */ |
| 468 | seqlock_write_lock(&timers_state.vm_clock_seqlock); |
| 469 | if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) { |
| 470 | vm_clock_warp_start = clock; |
| 471 | } |
| 472 | seqlock_write_unlock(&timers_state.vm_clock_seqlock); |
| 473 | timer_mod_anticipate(icount_warp_timer, clock + deadline); |
Paolo Bonzini | ce78d18 | 2013-10-07 17:30:02 +0200 | [diff] [blame] | 474 | } |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 475 | } else if (deadline == 0) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 476 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 480 | static bool icount_state_needed(void *opaque) |
| 481 | { |
| 482 | return use_icount; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * This is a subsection for icount migration. |
| 487 | */ |
| 488 | static const VMStateDescription icount_vmstate_timers = { |
| 489 | .name = "timer/icount", |
| 490 | .version_id = 1, |
| 491 | .minimum_version_id = 1, |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 492 | .needed = icount_state_needed, |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 493 | .fields = (VMStateField[]) { |
| 494 | VMSTATE_INT64(qemu_icount_bias, TimersState), |
| 495 | VMSTATE_INT64(qemu_icount, TimersState), |
| 496 | VMSTATE_END_OF_LIST() |
| 497 | } |
| 498 | }; |
| 499 | |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 500 | static const VMStateDescription vmstate_timers = { |
| 501 | .name = "timer", |
| 502 | .version_id = 2, |
| 503 | .minimum_version_id = 1, |
Juan Quintela | 35d0845 | 2014-04-16 16:01:33 +0200 | [diff] [blame] | 504 | .fields = (VMStateField[]) { |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 505 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
| 506 | VMSTATE_INT64(dummy, TimersState), |
| 507 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 508 | VMSTATE_END_OF_LIST() |
KONRAD Frederic | d09eae3 | 2014-08-01 01:37:10 +0200 | [diff] [blame] | 509 | }, |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 510 | .subsections = (const VMStateDescription*[]) { |
| 511 | &icount_vmstate_timers, |
| 512 | NULL |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 513 | } |
| 514 | }; |
| 515 | |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame^] | 516 | static void cpu_throttle_thread(void *opaque) |
| 517 | { |
| 518 | CPUState *cpu = opaque; |
| 519 | double pct; |
| 520 | double throttle_ratio; |
| 521 | long sleeptime_ns; |
| 522 | |
| 523 | if (!cpu_throttle_get_percentage()) { |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | pct = (double)cpu_throttle_get_percentage()/100; |
| 528 | throttle_ratio = pct / (1 - pct); |
| 529 | sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS); |
| 530 | |
| 531 | qemu_mutex_unlock_iothread(); |
| 532 | atomic_set(&cpu->throttle_thread_scheduled, 0); |
| 533 | g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */ |
| 534 | qemu_mutex_lock_iothread(); |
| 535 | } |
| 536 | |
| 537 | static void cpu_throttle_timer_tick(void *opaque) |
| 538 | { |
| 539 | CPUState *cpu; |
| 540 | double pct; |
| 541 | |
| 542 | /* Stop the timer if needed */ |
| 543 | if (!cpu_throttle_get_percentage()) { |
| 544 | return; |
| 545 | } |
| 546 | CPU_FOREACH(cpu) { |
| 547 | if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) { |
| 548 | async_run_on_cpu(cpu, cpu_throttle_thread, cpu); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | pct = (double)cpu_throttle_get_percentage()/100; |
| 553 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + |
| 554 | CPU_THROTTLE_TIMESLICE_NS / (1-pct)); |
| 555 | } |
| 556 | |
| 557 | void cpu_throttle_set(int new_throttle_pct) |
| 558 | { |
| 559 | /* Ensure throttle percentage is within valid range */ |
| 560 | new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX); |
| 561 | new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN); |
| 562 | |
| 563 | atomic_set(&throttle_percentage, new_throttle_pct); |
| 564 | |
| 565 | timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) + |
| 566 | CPU_THROTTLE_TIMESLICE_NS); |
| 567 | } |
| 568 | |
| 569 | void cpu_throttle_stop(void) |
| 570 | { |
| 571 | atomic_set(&throttle_percentage, 0); |
| 572 | } |
| 573 | |
| 574 | bool cpu_throttle_active(void) |
| 575 | { |
| 576 | return (cpu_throttle_get_percentage() != 0); |
| 577 | } |
| 578 | |
| 579 | int cpu_throttle_get_percentage(void) |
| 580 | { |
| 581 | return atomic_read(&throttle_percentage); |
| 582 | } |
| 583 | |
Pavel Dovgalyuk | 4603ea0 | 2014-09-01 09:34:49 +0400 | [diff] [blame] | 584 | void cpu_ticks_init(void) |
| 585 | { |
| 586 | seqlock_init(&timers_state.vm_clock_seqlock, NULL); |
| 587 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
Jason J. Herne | 2adcc85 | 2015-09-08 13:12:33 -0400 | [diff] [blame^] | 588 | throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
| 589 | cpu_throttle_timer_tick, NULL); |
Pavel Dovgalyuk | 4603ea0 | 2014-09-01 09:34:49 +0400 | [diff] [blame] | 590 | } |
| 591 | |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 592 | void configure_icount(QemuOpts *opts, Error **errp) |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 593 | { |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 594 | const char *option; |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 595 | char *rem_str = NULL; |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 596 | |
Sebastian Tanase | 1ad9580 | 2014-07-25 11:56:28 +0200 | [diff] [blame] | 597 | option = qemu_opt_get(opts, "shift"); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 598 | if (!option) { |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 599 | if (qemu_opt_get(opts, "align") != NULL) { |
| 600 | error_setg(errp, "Please specify shift option when using align"); |
| 601 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 602 | return; |
| 603 | } |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 604 | |
| 605 | icount_sleep = qemu_opt_get_bool(opts, "sleep", true); |
Victor CLEMENT | 5045e9d9 | 2015-05-29 17:14:04 +0200 | [diff] [blame] | 606 | if (icount_sleep) { |
| 607 | icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT, |
| 608 | icount_warp_rt, NULL); |
| 609 | } |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 610 | |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 611 | icount_align_option = qemu_opt_get_bool(opts, "align", false); |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 612 | |
| 613 | if (icount_align_option && !icount_sleep) { |
| 614 | error_setg(errp, "align=on and sleep=no are incompatible"); |
| 615 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 616 | if (strcmp(option, "auto") != 0) { |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 617 | errno = 0; |
| 618 | icount_time_shift = strtol(option, &rem_str, 0); |
| 619 | if (errno != 0 || *rem_str != '\0' || !strlen(option)) { |
| 620 | error_setg(errp, "icount: Invalid shift value"); |
| 621 | } |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 622 | use_icount = 1; |
| 623 | return; |
Sebastian Tanase | a8bfac3 | 2014-07-25 11:56:29 +0200 | [diff] [blame] | 624 | } else if (icount_align_option) { |
| 625 | error_setg(errp, "shift=auto and align=on are incompatible"); |
Victor CLEMENT | f1f4b57 | 2015-05-29 17:14:05 +0200 | [diff] [blame] | 626 | } else if (!icount_sleep) { |
| 627 | error_setg(errp, "shift=auto and sleep=no are incompatible"); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | use_icount = 2; |
| 631 | |
| 632 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 633 | It will be corrected fairly quickly anyway. */ |
| 634 | icount_time_shift = 3; |
| 635 | |
| 636 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 637 | The realtime trigger catches emulated time passing too slowly, |
| 638 | the virtual time trigger catches emulated time passing too fast. |
| 639 | Realtime triggers occur even when idle, so use them less frequently |
| 640 | than VM triggers. */ |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 641 | icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT, |
| 642 | icount_adjust_rt, NULL); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 643 | timer_mod(icount_rt_timer, |
Pavel Dovgalyuk | bf2a7dd | 2014-11-26 13:40:55 +0300 | [diff] [blame] | 644 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 645 | icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
| 646 | icount_adjust_vm, NULL); |
| 647 | timer_mod(icount_vm_timer, |
| 648 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
| 649 | get_ticks_per_sec() / 10); |
Paolo Bonzini | 946fb27 | 2011-09-12 13:57:37 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /***********************************************************/ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 653 | void hw_error(const char *fmt, ...) |
| 654 | { |
| 655 | va_list ap; |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 656 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 657 | |
| 658 | va_start(ap, fmt); |
| 659 | fprintf(stderr, "qemu: hardware error: "); |
| 660 | vfprintf(stderr, fmt, ap); |
| 661 | fprintf(stderr, "\n"); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 662 | CPU_FOREACH(cpu) { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 663 | fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); |
Andreas Färber | 878096e | 2013-05-27 01:33:50 +0200 | [diff] [blame] | 664 | cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 665 | } |
| 666 | va_end(ap); |
| 667 | abort(); |
| 668 | } |
| 669 | |
| 670 | void cpu_synchronize_all_states(void) |
| 671 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 672 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 673 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 674 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 675 | cpu_synchronize_state(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | void cpu_synchronize_all_post_reset(void) |
| 680 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 681 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 682 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 683 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 684 | cpu_synchronize_post_reset(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
| 688 | void cpu_synchronize_all_post_init(void) |
| 689 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 690 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 691 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 692 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 693 | cpu_synchronize_post_init(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Marcelo Tosatti | de9d61e | 2014-09-05 10:52:46 -0300 | [diff] [blame] | 697 | void cpu_clean_all_dirty(void) |
| 698 | { |
| 699 | CPUState *cpu; |
| 700 | |
| 701 | CPU_FOREACH(cpu) { |
| 702 | cpu_clean_state(cpu); |
| 703 | } |
| 704 | } |
| 705 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 706 | static int do_vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 707 | { |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 708 | int ret = 0; |
| 709 | |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 710 | if (runstate_is_running()) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 711 | cpu_disable_ticks(); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 712 | pause_all_vcpus(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 713 | runstate_set(state); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 714 | vm_state_notify(0, state); |
Wenchao Xia | a4e15de | 2014-06-18 08:43:36 +0200 | [diff] [blame] | 715 | qapi_event_send_stop(&error_abort); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 716 | } |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 717 | |
Kevin Wolf | 594a45c | 2013-07-18 14:52:19 +0200 | [diff] [blame] | 718 | bdrv_drain_all(); |
| 719 | ret = bdrv_flush_all(); |
| 720 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 721 | return ret; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 724 | static bool cpu_can_run(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 725 | { |
Andreas Färber | 4fdeee7 | 2012-05-02 23:10:09 +0200 | [diff] [blame] | 726 | if (cpu->stop) { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 727 | return false; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 728 | } |
Tiejun Chen | 321bc0b | 2013-08-02 09:43:09 +0800 | [diff] [blame] | 729 | if (cpu_is_stopped(cpu)) { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 730 | return false; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 731 | } |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 732 | return true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Andreas Färber | 9132504 | 2013-05-27 02:07:49 +0200 | [diff] [blame] | 735 | static void cpu_handle_guest_debug(CPUState *cpu) |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 736 | { |
Andreas Färber | 64f6b34 | 2013-05-27 02:06:09 +0200 | [diff] [blame] | 737 | gdb_set_stop_cpu(cpu); |
Jan Kiszka | 8cf7171 | 2011-02-07 12:19:16 +0100 | [diff] [blame] | 738 | qemu_system_debug_request(); |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 739 | cpu->stopped = true; |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 742 | #ifdef CONFIG_LINUX |
| 743 | static void sigbus_reraise(void) |
| 744 | { |
| 745 | sigset_t set; |
| 746 | struct sigaction action; |
| 747 | |
| 748 | memset(&action, 0, sizeof(action)); |
| 749 | action.sa_handler = SIG_DFL; |
| 750 | if (!sigaction(SIGBUS, &action, NULL)) { |
| 751 | raise(SIGBUS); |
| 752 | sigemptyset(&set); |
| 753 | sigaddset(&set, SIGBUS); |
| 754 | sigprocmask(SIG_UNBLOCK, &set, NULL); |
| 755 | } |
| 756 | perror("Failed to re-raise SIGBUS!\n"); |
| 757 | abort(); |
| 758 | } |
| 759 | |
| 760 | static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo, |
| 761 | void *ctx) |
| 762 | { |
| 763 | if (kvm_on_sigbus(siginfo->ssi_code, |
| 764 | (void *)(intptr_t)siginfo->ssi_addr)) { |
| 765 | sigbus_reraise(); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | static void qemu_init_sigbus(void) |
| 770 | { |
| 771 | struct sigaction action; |
| 772 | |
| 773 | memset(&action, 0, sizeof(action)); |
| 774 | action.sa_flags = SA_SIGINFO; |
| 775 | action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler; |
| 776 | sigaction(SIGBUS, &action, NULL); |
| 777 | |
| 778 | prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); |
| 779 | } |
| 780 | |
Andreas Färber | 290adf3 | 2013-01-17 09:30:27 +0100 | [diff] [blame] | 781 | static void qemu_kvm_eat_signals(CPUState *cpu) |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 782 | { |
| 783 | struct timespec ts = { 0, 0 }; |
| 784 | siginfo_t siginfo; |
| 785 | sigset_t waitset; |
| 786 | sigset_t chkset; |
| 787 | int r; |
| 788 | |
| 789 | sigemptyset(&waitset); |
| 790 | sigaddset(&waitset, SIG_IPI); |
| 791 | sigaddset(&waitset, SIGBUS); |
| 792 | |
| 793 | do { |
| 794 | r = sigtimedwait(&waitset, &siginfo, &ts); |
| 795 | if (r == -1 && !(errno == EAGAIN || errno == EINTR)) { |
| 796 | perror("sigtimedwait"); |
| 797 | exit(1); |
| 798 | } |
| 799 | |
| 800 | switch (r) { |
| 801 | case SIGBUS: |
Andreas Färber | 290adf3 | 2013-01-17 09:30:27 +0100 | [diff] [blame] | 802 | if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) { |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 803 | sigbus_reraise(); |
| 804 | } |
| 805 | break; |
| 806 | default: |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | r = sigpending(&chkset); |
| 811 | if (r == -1) { |
| 812 | perror("sigpending"); |
| 813 | exit(1); |
| 814 | } |
| 815 | } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS)); |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 818 | #else /* !CONFIG_LINUX */ |
| 819 | |
| 820 | static void qemu_init_sigbus(void) |
| 821 | { |
| 822 | } |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 823 | |
Andreas Färber | 290adf3 | 2013-01-17 09:30:27 +0100 | [diff] [blame] | 824 | static void qemu_kvm_eat_signals(CPUState *cpu) |
Jan Kiszka | 1ab3c6c | 2011-03-15 12:26:12 +0100 | [diff] [blame] | 825 | { |
| 826 | } |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 827 | #endif /* !CONFIG_LINUX */ |
| 828 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 829 | #ifndef _WIN32 |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 830 | static void dummy_signal(int sig) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 831 | { |
| 832 | } |
| 833 | |
Andreas Färber | 13618e0 | 2013-05-26 23:41:00 +0200 | [diff] [blame] | 834 | static void qemu_kvm_init_cpu_signals(CPUState *cpu) |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 835 | { |
| 836 | int r; |
| 837 | sigset_t set; |
| 838 | struct sigaction sigact; |
| 839 | |
| 840 | memset(&sigact, 0, sizeof(sigact)); |
| 841 | sigact.sa_handler = dummy_signal; |
| 842 | sigaction(SIG_IPI, &sigact, NULL); |
| 843 | |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 844 | pthread_sigmask(SIG_BLOCK, NULL, &set); |
| 845 | sigdelset(&set, SIG_IPI); |
| 846 | sigdelset(&set, SIGBUS); |
Andreas Färber | 491d6e8 | 2013-05-26 23:38:10 +0200 | [diff] [blame] | 847 | r = kvm_set_signal_mask(cpu, &set); |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 848 | if (r) { |
| 849 | fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r)); |
| 850 | exit(1); |
| 851 | } |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 852 | } |
| 853 | |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 854 | #else /* _WIN32 */ |
Andreas Färber | 13618e0 | 2013-05-26 23:41:00 +0200 | [diff] [blame] | 855 | static void qemu_kvm_init_cpu_signals(CPUState *cpu) |
Paolo Bonzini | 714bd04 | 2011-03-12 17:44:06 +0100 | [diff] [blame] | 856 | { |
| 857 | abort(); |
| 858 | } |
Jan Kiszka | 55f8d6a | 2011-02-01 22:15:52 +0100 | [diff] [blame] | 859 | #endif /* _WIN32 */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 860 | |
Stefan Weil | b2532d8 | 2012-09-27 07:41:42 +0200 | [diff] [blame] | 861 | static QemuMutex qemu_global_mutex; |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 862 | static QemuCond qemu_io_proceeded_cond; |
Paolo Bonzini | 6b49809 | 2015-02-27 19:58:23 +0100 | [diff] [blame] | 863 | static unsigned iothread_requesting_mutex; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 864 | |
| 865 | static QemuThread io_thread; |
| 866 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 867 | /* cpu creation */ |
| 868 | static QemuCond qemu_cpu_cond; |
| 869 | /* system init */ |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 870 | static QemuCond qemu_pause_cond; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 871 | static QemuCond qemu_work_cond; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 872 | |
Paolo Bonzini | d3b12f5 | 2011-09-13 10:30:52 +0200 | [diff] [blame] | 873 | void qemu_init_cpu_loop(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 874 | { |
Jan Kiszka | 6d9cb73 | 2011-02-01 22:15:58 +0100 | [diff] [blame] | 875 | qemu_init_sigbus(); |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 876 | qemu_cond_init(&qemu_cpu_cond); |
Anthony Liguori | ed94592 | 2011-02-08 18:18:18 +0100 | [diff] [blame] | 877 | qemu_cond_init(&qemu_pause_cond); |
| 878 | qemu_cond_init(&qemu_work_cond); |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 879 | qemu_cond_init(&qemu_io_proceeded_cond); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 880 | qemu_mutex_init(&qemu_global_mutex); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 881 | |
Jan Kiszka | b7680cb | 2011-03-12 17:43:51 +0100 | [diff] [blame] | 882 | qemu_thread_get_self(&io_thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Andreas Färber | f100f0b | 2012-05-03 14:58:47 +0200 | [diff] [blame] | 885 | void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 886 | { |
| 887 | struct qemu_work_item wi; |
| 888 | |
Andreas Färber | 60e8257 | 2012-05-02 22:23:49 +0200 | [diff] [blame] | 889 | if (qemu_cpu_is_self(cpu)) { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 890 | func(data); |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | wi.func = func; |
| 895 | wi.data = data; |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 896 | wi.free = false; |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 897 | |
| 898 | qemu_mutex_lock(&cpu->work_mutex); |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 899 | if (cpu->queued_work_first == NULL) { |
| 900 | cpu->queued_work_first = &wi; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 901 | } else { |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 902 | cpu->queued_work_last->next = &wi; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 903 | } |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 904 | cpu->queued_work_last = &wi; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 905 | wi.next = NULL; |
| 906 | wi.done = false; |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 907 | qemu_mutex_unlock(&cpu->work_mutex); |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 908 | |
Andreas Färber | c08d742 | 2012-05-03 04:34:15 +0200 | [diff] [blame] | 909 | qemu_cpu_kick(cpu); |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 910 | while (!atomic_mb_read(&wi.done)) { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 911 | CPUState *self_cpu = current_cpu; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 912 | |
| 913 | qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex); |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 914 | current_cpu = self_cpu; |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 918 | void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data) |
| 919 | { |
| 920 | struct qemu_work_item *wi; |
| 921 | |
| 922 | if (qemu_cpu_is_self(cpu)) { |
| 923 | func(data); |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | wi = g_malloc0(sizeof(struct qemu_work_item)); |
| 928 | wi->func = func; |
| 929 | wi->data = data; |
| 930 | wi->free = true; |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 931 | |
| 932 | qemu_mutex_lock(&cpu->work_mutex); |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 933 | if (cpu->queued_work_first == NULL) { |
| 934 | cpu->queued_work_first = wi; |
| 935 | } else { |
| 936 | cpu->queued_work_last->next = wi; |
| 937 | } |
| 938 | cpu->queued_work_last = wi; |
| 939 | wi->next = NULL; |
| 940 | wi->done = false; |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 941 | qemu_mutex_unlock(&cpu->work_mutex); |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 942 | |
| 943 | qemu_cpu_kick(cpu); |
| 944 | } |
| 945 | |
Andreas Färber | 6d45b10 | 2012-05-03 02:13:22 +0200 | [diff] [blame] | 946 | static void flush_queued_work(CPUState *cpu) |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 947 | { |
| 948 | struct qemu_work_item *wi; |
| 949 | |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 950 | if (cpu->queued_work_first == NULL) { |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 951 | return; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 952 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 953 | |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 954 | qemu_mutex_lock(&cpu->work_mutex); |
| 955 | while (cpu->queued_work_first != NULL) { |
| 956 | wi = cpu->queued_work_first; |
Andreas Färber | c64ca81 | 2012-05-03 02:11:45 +0200 | [diff] [blame] | 957 | cpu->queued_work_first = wi->next; |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 958 | if (!cpu->queued_work_first) { |
| 959 | cpu->queued_work_last = NULL; |
| 960 | } |
| 961 | qemu_mutex_unlock(&cpu->work_mutex); |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 962 | wi->func(wi->data); |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 963 | qemu_mutex_lock(&cpu->work_mutex); |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 964 | if (wi->free) { |
| 965 | g_free(wi); |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 966 | } else { |
| 967 | atomic_mb_set(&wi->done, true); |
Chegu Vinod | 3c02270 | 2013-06-24 03:49:41 -0600 | [diff] [blame] | 968 | } |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 969 | } |
Paolo Bonzini | 376692b | 2015-07-10 12:32:32 +0200 | [diff] [blame] | 970 | qemu_mutex_unlock(&cpu->work_mutex); |
Marcelo Tosatti | e82bcec | 2010-05-04 09:45:22 -0300 | [diff] [blame] | 971 | qemu_cond_broadcast(&qemu_work_cond); |
| 972 | } |
| 973 | |
Andreas Färber | 509a0d7 | 2012-05-03 02:18:09 +0200 | [diff] [blame] | 974 | static void qemu_wait_io_event_common(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 975 | { |
Andreas Färber | 4fdeee7 | 2012-05-02 23:10:09 +0200 | [diff] [blame] | 976 | if (cpu->stop) { |
| 977 | cpu->stop = false; |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 978 | cpu->stopped = true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 979 | qemu_cond_signal(&qemu_pause_cond); |
| 980 | } |
Andreas Färber | 6d45b10 | 2012-05-03 02:13:22 +0200 | [diff] [blame] | 981 | flush_queued_work(cpu); |
Andreas Färber | 216fc9a | 2012-05-02 17:49:49 +0200 | [diff] [blame] | 982 | cpu->thread_kicked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 983 | } |
| 984 | |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 985 | static void qemu_tcg_wait_io_event(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 986 | { |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 987 | while (all_cpu_threads_idle()) { |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 988 | /* Start accounting real time to the virtual clock if the CPUs |
| 989 | are idle. */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 990 | qemu_clock_warp(QEMU_CLOCK_VIRTUAL); |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 991 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 992 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 993 | |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 994 | while (iothread_requesting_mutex) { |
| 995 | qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex); |
| 996 | } |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 997 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 998 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 999 | qemu_wait_io_event_common(cpu); |
Jan Kiszka | 6cabe1f | 2010-06-25 16:56:53 +0200 | [diff] [blame] | 1000 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Andreas Färber | fd529e8 | 2013-05-26 23:24:55 +0200 | [diff] [blame] | 1003 | static void qemu_kvm_wait_io_event(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1004 | { |
Andreas Färber | a98ae1d | 2013-05-26 23:21:08 +0200 | [diff] [blame] | 1005 | while (cpu_thread_is_idle(cpu)) { |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1006 | qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 1640032 | 2011-02-09 16:29:37 +0100 | [diff] [blame] | 1007 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1008 | |
Andreas Färber | 290adf3 | 2013-01-17 09:30:27 +0100 | [diff] [blame] | 1009 | qemu_kvm_eat_signals(cpu); |
Andreas Färber | 509a0d7 | 2012-05-03 02:18:09 +0200 | [diff] [blame] | 1010 | qemu_wait_io_event_common(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1013 | static void *qemu_kvm_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1014 | { |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1015 | CPUState *cpu = arg; |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 1016 | int r; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1017 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1018 | rcu_register_thread(); |
| 1019 | |
Paolo Bonzini | 2e7f7a3 | 2015-06-18 18:47:18 +0200 | [diff] [blame] | 1020 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1021 | qemu_thread_get_self(cpu->thread); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 1022 | cpu->thread_id = qemu_get_thread_id(); |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1023 | cpu->can_do_io = 1; |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1024 | current_cpu = cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1025 | |
Andreas Färber | 504134d | 2012-12-17 06:38:45 +0100 | [diff] [blame] | 1026 | r = kvm_init_vcpu(cpu); |
Jan Kiszka | 84b4915 | 2011-02-01 22:15:50 +0100 | [diff] [blame] | 1027 | if (r < 0) { |
| 1028 | fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r)); |
| 1029 | exit(1); |
| 1030 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1031 | |
Andreas Färber | 13618e0 | 2013-05-26 23:41:00 +0200 | [diff] [blame] | 1032 | qemu_kvm_init_cpu_signals(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1033 | |
| 1034 | /* signal CPU creation */ |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1035 | cpu->created = true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1036 | qemu_cond_signal(&qemu_cpu_cond); |
| 1037 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1038 | while (1) { |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1039 | if (cpu_can_run(cpu)) { |
Andreas Färber | 1458c36 | 2013-05-26 23:46:55 +0200 | [diff] [blame] | 1040 | r = kvm_cpu_exec(cpu); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 1041 | if (r == EXCP_DEBUG) { |
Andreas Färber | 9132504 | 2013-05-27 02:07:49 +0200 | [diff] [blame] | 1042 | cpu_handle_guest_debug(cpu); |
Jan Kiszka | 83f338f | 2011-02-07 12:19:17 +0100 | [diff] [blame] | 1043 | } |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1044 | } |
Andreas Färber | fd529e8 | 2013-05-26 23:24:55 +0200 | [diff] [blame] | 1045 | qemu_kvm_wait_io_event(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1051 | static void *qemu_dummy_cpu_thread_fn(void *arg) |
| 1052 | { |
| 1053 | #ifdef _WIN32 |
| 1054 | fprintf(stderr, "qtest is not supported under Windows\n"); |
| 1055 | exit(1); |
| 1056 | #else |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1057 | CPUState *cpu = arg; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1058 | sigset_t waitset; |
| 1059 | int r; |
| 1060 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1061 | rcu_register_thread(); |
| 1062 | |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1063 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1064 | qemu_thread_get_self(cpu->thread); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 1065 | cpu->thread_id = qemu_get_thread_id(); |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1066 | cpu->can_do_io = 1; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1067 | |
| 1068 | sigemptyset(&waitset); |
| 1069 | sigaddset(&waitset, SIG_IPI); |
| 1070 | |
| 1071 | /* signal CPU creation */ |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1072 | cpu->created = true; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1073 | qemu_cond_signal(&qemu_cpu_cond); |
| 1074 | |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1075 | current_cpu = cpu; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1076 | while (1) { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1077 | current_cpu = NULL; |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1078 | qemu_mutex_unlock_iothread(); |
| 1079 | do { |
| 1080 | int sig; |
| 1081 | r = sigwait(&waitset, &sig); |
| 1082 | } while (r == -1 && (errno == EAGAIN || errno == EINTR)); |
| 1083 | if (r == -1) { |
| 1084 | perror("sigwait"); |
| 1085 | exit(1); |
| 1086 | } |
| 1087 | qemu_mutex_lock_iothread(); |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1088 | current_cpu = cpu; |
Andreas Färber | 509a0d7 | 2012-05-03 02:18:09 +0200 | [diff] [blame] | 1089 | qemu_wait_io_event_common(cpu); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | return NULL; |
| 1093 | #endif |
| 1094 | } |
| 1095 | |
Jan Kiszka | bdb7ca6 | 2011-09-26 09:40:39 +0200 | [diff] [blame] | 1096 | static void tcg_exec_all(void); |
| 1097 | |
Jan Kiszka | 7e97cd8 | 2011-02-07 12:19:12 +0100 | [diff] [blame] | 1098 | static void *qemu_tcg_cpu_thread_fn(void *arg) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1099 | { |
Andreas Färber | c3586ba | 2012-05-03 01:41:24 +0200 | [diff] [blame] | 1100 | CPUState *cpu = arg; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1101 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 1102 | rcu_register_thread(); |
| 1103 | |
Paolo Bonzini | 2e7f7a3 | 2015-06-18 18:47:18 +0200 | [diff] [blame] | 1104 | qemu_mutex_lock_iothread(); |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1105 | qemu_thread_get_self(cpu->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1106 | |
Andreas Färber | 38fcbd3 | 2013-07-07 19:50:23 +0200 | [diff] [blame] | 1107 | CPU_FOREACH(cpu) { |
| 1108 | cpu->thread_id = qemu_get_thread_id(); |
| 1109 | cpu->created = true; |
Pavel Dovgalyuk | 626cf8f | 2014-12-08 10:53:17 +0300 | [diff] [blame] | 1110 | cpu->can_do_io = 1; |
Andreas Färber | 38fcbd3 | 2013-07-07 19:50:23 +0200 | [diff] [blame] | 1111 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1112 | qemu_cond_signal(&qemu_cpu_cond); |
| 1113 | |
Jan Kiszka | fa7d186 | 2011-08-22 18:35:25 +0200 | [diff] [blame] | 1114 | /* wait for initial kick-off after machine start */ |
Emilio G. Cota | c28e399 | 2015-04-27 12:45:28 -0400 | [diff] [blame] | 1115 | while (first_cpu->stopped) { |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 1116 | qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex); |
Jan Kiszka | 8e564b4 | 2012-02-17 18:31:15 +0100 | [diff] [blame] | 1117 | |
| 1118 | /* process any pending work */ |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1119 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1120 | qemu_wait_io_event_common(cpu); |
Jan Kiszka | 8e564b4 | 2012-02-17 18:31:15 +0100 | [diff] [blame] | 1121 | } |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1122 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1123 | |
Paolo Bonzini | 21618b3 | 2015-02-27 20:01:03 +0100 | [diff] [blame] | 1124 | /* process any pending work */ |
Paolo Bonzini | aed807c | 2015-08-18 06:43:15 -0700 | [diff] [blame] | 1125 | atomic_mb_set(&exit_request, 1); |
Paolo Bonzini | 21618b3 | 2015-02-27 20:01:03 +0100 | [diff] [blame] | 1126 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1127 | while (1) { |
Jan Kiszka | bdb7ca6 | 2011-09-26 09:40:39 +0200 | [diff] [blame] | 1128 | tcg_exec_all(); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1129 | |
| 1130 | if (use_icount) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1131 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1132 | |
| 1133 | if (deadline == 0) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1134 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1135 | } |
Paolo Bonzini | 3b2319a | 2011-04-13 10:03:43 +0200 | [diff] [blame] | 1136 | } |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 1137 | qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus)); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
Andreas Färber | 2ff09a4 | 2012-05-03 00:23:30 +0200 | [diff] [blame] | 1143 | static void qemu_cpu_kick_thread(CPUState *cpu) |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1144 | { |
| 1145 | #ifndef _WIN32 |
| 1146 | int err; |
| 1147 | |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1148 | if (cpu->thread_kicked) { |
| 1149 | return; |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1150 | } |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1151 | cpu->thread_kicked = true; |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1152 | err = pthread_kill(cpu->thread->thread, SIG_IPI); |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1153 | if (err) { |
| 1154 | fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); |
| 1155 | exit(1); |
| 1156 | } |
| 1157 | #else /* _WIN32 */ |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1158 | abort(); |
Paolo Bonzini | cc015e9 | 2011-03-12 17:44:08 +0100 | [diff] [blame] | 1159 | #endif |
| 1160 | } |
| 1161 | |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1162 | static void qemu_cpu_kick_no_halt(void) |
| 1163 | { |
| 1164 | CPUState *cpu; |
| 1165 | /* Ensure whatever caused the exit has reached the CPU threads before |
| 1166 | * writing exit_request. |
| 1167 | */ |
| 1168 | atomic_mb_set(&exit_request, 1); |
| 1169 | cpu = atomic_mb_read(&tcg_current_cpu); |
| 1170 | if (cpu) { |
| 1171 | cpu_exit(cpu); |
| 1172 | } |
| 1173 | } |
| 1174 | |
Andreas Färber | c08d742 | 2012-05-03 04:34:15 +0200 | [diff] [blame] | 1175 | void qemu_cpu_kick(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1176 | { |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1177 | qemu_cond_broadcast(cpu->halt_cond); |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1178 | if (tcg_enabled()) { |
| 1179 | qemu_cpu_kick_no_halt(); |
| 1180 | } else { |
| 1181 | qemu_cpu_kick_thread(cpu); |
| 1182 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Jan Kiszka | 46d62fa | 2011-02-01 22:15:59 +0100 | [diff] [blame] | 1185 | void qemu_cpu_kick_self(void) |
| 1186 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1187 | assert(current_cpu); |
Paolo Bonzini | 9102ded | 2015-08-18 06:52:09 -0700 | [diff] [blame] | 1188 | qemu_cpu_kick_thread(current_cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Andreas Färber | 60e8257 | 2012-05-02 22:23:49 +0200 | [diff] [blame] | 1191 | bool qemu_cpu_is_self(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1192 | { |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1193 | return qemu_thread_is_self(cpu->thread); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Paolo Bonzini | 79e2b9a | 2015-01-21 12:09:14 +0100 | [diff] [blame] | 1196 | bool qemu_in_vcpu_thread(void) |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1197 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1198 | return current_cpu && qemu_cpu_is_self(current_cpu); |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1201 | static __thread bool iothread_locked = false; |
| 1202 | |
| 1203 | bool qemu_mutex_iothread_locked(void) |
| 1204 | { |
| 1205 | return iothread_locked; |
| 1206 | } |
| 1207 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1208 | void qemu_mutex_lock_iothread(void) |
| 1209 | { |
Paolo Bonzini | 21618b3 | 2015-02-27 20:01:03 +0100 | [diff] [blame] | 1210 | atomic_inc(&iothread_requesting_mutex); |
Paolo Bonzini | 2e7f7a3 | 2015-06-18 18:47:18 +0200 | [diff] [blame] | 1211 | /* In the simple case there is no need to bump the VCPU thread out of |
| 1212 | * TCG code execution. |
| 1213 | */ |
| 1214 | if (!tcg_enabled() || qemu_in_vcpu_thread() || |
Aníbal Limón | 46036b2 | 2015-09-03 15:48:33 -0500 | [diff] [blame] | 1215 | !first_cpu || !first_cpu->created) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1216 | qemu_mutex_lock(&qemu_global_mutex); |
Paolo Bonzini | 21618b3 | 2015-02-27 20:01:03 +0100 | [diff] [blame] | 1217 | atomic_dec(&iothread_requesting_mutex); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1218 | } else { |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1219 | if (qemu_mutex_trylock(&qemu_global_mutex)) { |
Paolo Bonzini | e0c3821 | 2015-08-26 00:19:19 +0200 | [diff] [blame] | 1220 | qemu_cpu_kick_no_halt(); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1221 | qemu_mutex_lock(&qemu_global_mutex); |
| 1222 | } |
Paolo Bonzini | 6b49809 | 2015-02-27 19:58:23 +0100 | [diff] [blame] | 1223 | atomic_dec(&iothread_requesting_mutex); |
Paolo Bonzini | 46daff1 | 2011-06-09 13:10:24 +0200 | [diff] [blame] | 1224 | qemu_cond_broadcast(&qemu_io_proceeded_cond); |
Marcelo Tosatti | 1a28cac | 2010-05-04 09:45:20 -0300 | [diff] [blame] | 1225 | } |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1226 | iothread_locked = true; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | void qemu_mutex_unlock_iothread(void) |
| 1230 | { |
Paolo Bonzini | afbe705 | 2015-06-18 18:47:19 +0200 | [diff] [blame] | 1231 | iothread_locked = false; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1232 | qemu_mutex_unlock(&qemu_global_mutex); |
| 1233 | } |
| 1234 | |
| 1235 | static int all_vcpus_paused(void) |
| 1236 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1237 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1238 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1239 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1240 | if (!cpu->stopped) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1241 | return 0; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1242 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | return 1; |
| 1246 | } |
| 1247 | |
| 1248 | void pause_all_vcpus(void) |
| 1249 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1250 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1251 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1252 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1253 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1254 | cpu->stop = true; |
| 1255 | qemu_cpu_kick(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1258 | if (qemu_in_vcpu_thread()) { |
Jan Kiszka | d798e97 | 2012-02-17 18:31:16 +0100 | [diff] [blame] | 1259 | cpu_stop_current(); |
| 1260 | if (!kvm_enabled()) { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1261 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1262 | cpu->stop = false; |
| 1263 | cpu->stopped = true; |
Jan Kiszka | d798e97 | 2012-02-17 18:31:16 +0100 | [diff] [blame] | 1264 | } |
| 1265 | return; |
| 1266 | } |
| 1267 | } |
| 1268 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1269 | while (!all_vcpus_paused()) { |
Paolo Bonzini | be7d6c5 | 2011-03-12 17:44:02 +0100 | [diff] [blame] | 1270 | qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1271 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1272 | qemu_cpu_kick(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Igor Mammedov | 2993683 | 2013-04-23 10:29:37 +0200 | [diff] [blame] | 1277 | void cpu_resume(CPUState *cpu) |
| 1278 | { |
| 1279 | cpu->stop = false; |
| 1280 | cpu->stopped = false; |
| 1281 | qemu_cpu_kick(cpu); |
| 1282 | } |
| 1283 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1284 | void resume_all_vcpus(void) |
| 1285 | { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1286 | CPUState *cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1287 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1288 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1289 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1290 | cpu_resume(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1294 | /* For temporary buffers for forming a name */ |
| 1295 | #define VCPU_THREAD_NAME_SIZE 16 |
| 1296 | |
Andreas Färber | e5ab30a | 2012-05-03 01:50:44 +0200 | [diff] [blame] | 1297 | static void qemu_tcg_init_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1298 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1299 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
KONRAD Frederic | d5f8d61 | 2015-08-10 17:27:06 +0200 | [diff] [blame] | 1300 | static QemuCond *tcg_halt_cond; |
| 1301 | static QemuThread *tcg_cpu_thread; |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1302 | |
Edgar E. Iglesias | 09daed8 | 2013-12-17 13:06:51 +1000 | [diff] [blame] | 1303 | tcg_cpu_address_space_init(cpu, cpu->as); |
| 1304 | |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1305 | /* share a single thread for all cpus with TCG */ |
| 1306 | if (!tcg_cpu_thread) { |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1307 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1308 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1309 | qemu_cond_init(cpu->halt_cond); |
| 1310 | tcg_halt_cond = cpu->halt_cond; |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1311 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG", |
| 1312 | cpu->cpu_index); |
| 1313 | qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, |
| 1314 | cpu, QEMU_THREAD_JOINABLE); |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 1315 | #ifdef _WIN32 |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1316 | cpu->hThread = qemu_thread_get_handle(cpu->thread); |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 1317 | #endif |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1318 | while (!cpu->created) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1319 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1320 | } |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1321 | tcg_cpu_thread = cpu->thread; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1322 | } else { |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1323 | cpu->thread = tcg_cpu_thread; |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1324 | cpu->halt_cond = tcg_halt_cond; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1328 | static void qemu_kvm_start_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1329 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1330 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1331 | |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1332 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1333 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1334 | qemu_cond_init(cpu->halt_cond); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1335 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM", |
| 1336 | cpu->cpu_index); |
| 1337 | qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn, |
| 1338 | cpu, QEMU_THREAD_JOINABLE); |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1339 | while (!cpu->created) { |
Paolo Bonzini | 18a8572 | 2011-03-12 17:44:03 +0100 | [diff] [blame] | 1340 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1341 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1344 | static void qemu_dummy_start_vcpu(CPUState *cpu) |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1345 | { |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1346 | char thread_name[VCPU_THREAD_NAME_SIZE]; |
| 1347 | |
Andreas Färber | 814e612 | 2012-05-02 17:00:37 +0200 | [diff] [blame] | 1348 | cpu->thread = g_malloc0(sizeof(QemuThread)); |
Andreas Färber | f5c121b | 2012-05-03 01:22:49 +0200 | [diff] [blame] | 1349 | cpu->halt_cond = g_malloc0(sizeof(QemuCond)); |
| 1350 | qemu_cond_init(cpu->halt_cond); |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 1351 | snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY", |
| 1352 | cpu->cpu_index); |
| 1353 | 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] | 1354 | QEMU_THREAD_JOINABLE); |
Andreas Färber | 61a4621 | 2012-05-02 22:49:36 +0200 | [diff] [blame] | 1355 | while (!cpu->created) { |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1356 | qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); |
| 1357 | } |
| 1358 | } |
| 1359 | |
Andreas Färber | c643bed | 2013-05-27 03:23:24 +0200 | [diff] [blame] | 1360 | void qemu_init_vcpu(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1361 | { |
Andreas Färber | ce3960e | 2012-12-17 03:27:07 +0100 | [diff] [blame] | 1362 | cpu->nr_cores = smp_cores; |
| 1363 | cpu->nr_threads = smp_threads; |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 1364 | cpu->stopped = true; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1365 | if (kvm_enabled()) { |
Andreas Färber | 48a106b | 2013-05-27 02:20:39 +0200 | [diff] [blame] | 1366 | qemu_kvm_start_vcpu(cpu); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1367 | } else if (tcg_enabled()) { |
Andreas Färber | e5ab30a | 2012-05-03 01:50:44 +0200 | [diff] [blame] | 1368 | qemu_tcg_init_vcpu(cpu); |
Anthony Liguori | c7f0f3b | 2012-03-28 15:42:02 +0200 | [diff] [blame] | 1369 | } else { |
Andreas Färber | 10a9021 | 2013-05-27 02:24:35 +0200 | [diff] [blame] | 1370 | qemu_dummy_start_vcpu(cpu); |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1371 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1374 | void cpu_stop_current(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1375 | { |
Andreas Färber | 4917cf4 | 2013-05-27 05:17:50 +0200 | [diff] [blame] | 1376 | if (current_cpu) { |
| 1377 | current_cpu->stop = false; |
| 1378 | current_cpu->stopped = true; |
| 1379 | cpu_exit(current_cpu); |
Paolo Bonzini | 67bb172 | 2011-03-12 17:43:59 +0100 | [diff] [blame] | 1380 | qemu_cond_signal(&qemu_pause_cond); |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1381 | } |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1384 | int vm_stop(RunState state) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1385 | { |
Juan Quintela | aa723c2 | 2012-09-18 16:30:11 +0200 | [diff] [blame] | 1386 | if (qemu_in_vcpu_thread()) { |
Paolo Bonzini | 74892d2 | 2014-06-05 14:53:58 +0200 | [diff] [blame] | 1387 | qemu_system_vmstop_request_prepare(); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 1388 | qemu_system_vmstop_request(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * FIXME: should not return to device code in case |
| 1391 | * vm_stop() has been requested. |
| 1392 | */ |
Jan Kiszka | b4a3d96 | 2011-02-01 22:15:43 +0100 | [diff] [blame] | 1393 | cpu_stop_current(); |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1394 | return 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1395 | } |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1396 | |
| 1397 | return do_vm_stop(state); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 1400 | /* does a state transition even if the VM is already stopped, |
| 1401 | current state is forgotten forever */ |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1402 | int vm_stop_force_state(RunState state) |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 1403 | { |
| 1404 | if (runstate_is_running()) { |
Kevin Wolf | 5698346 | 2013-07-05 13:49:54 +0200 | [diff] [blame] | 1405 | return vm_stop(state); |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 1406 | } else { |
| 1407 | runstate_set(state); |
Kevin Wolf | 594a45c | 2013-07-18 14:52:19 +0200 | [diff] [blame] | 1408 | /* Make sure to return an error if the flush in a previous vm_stop() |
| 1409 | * failed. */ |
| 1410 | return bdrv_flush_all(); |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Peter Crosthwaite | 3d57f78 | 2015-06-23 19:31:17 -0700 | [diff] [blame] | 1414 | static int tcg_cpu_exec(CPUState *cpu) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1415 | { |
| 1416 | int ret; |
| 1417 | #ifdef CONFIG_PROFILER |
| 1418 | int64_t ti; |
| 1419 | #endif |
| 1420 | |
| 1421 | #ifdef CONFIG_PROFILER |
| 1422 | ti = profile_getclock(); |
| 1423 | #endif |
| 1424 | if (use_icount) { |
| 1425 | int64_t count; |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1426 | int64_t deadline; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1427 | int decr; |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 1428 | timers_state.qemu_icount -= (cpu->icount_decr.u16.low |
| 1429 | + cpu->icount_extra); |
Andreas Färber | 28ecfd7 | 2013-08-26 05:51:49 +0200 | [diff] [blame] | 1430 | cpu->icount_decr.u16.low = 0; |
Andreas Färber | efee734 | 2013-08-26 05:39:29 +0200 | [diff] [blame] | 1431 | cpu->icount_extra = 0; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1432 | deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1433 | |
| 1434 | /* Maintain prior (possibly buggy) behaviour where if no deadline |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1435 | * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 1436 | * INT32_MAX nanoseconds ahead, we still use INT32_MAX |
| 1437 | * nanoseconds. |
| 1438 | */ |
| 1439 | if ((deadline < 0) || (deadline > INT32_MAX)) { |
| 1440 | deadline = INT32_MAX; |
| 1441 | } |
| 1442 | |
| 1443 | count = qemu_icount_round(deadline); |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 1444 | timers_state.qemu_icount += count; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1445 | decr = (count > 0xffff) ? 0xffff : count; |
| 1446 | count -= decr; |
Andreas Färber | 28ecfd7 | 2013-08-26 05:51:49 +0200 | [diff] [blame] | 1447 | cpu->icount_decr.u16.low = decr; |
Andreas Färber | efee734 | 2013-08-26 05:39:29 +0200 | [diff] [blame] | 1448 | cpu->icount_extra = count; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1449 | } |
Peter Crosthwaite | ea3e984 | 2015-06-18 10:24:55 -0700 | [diff] [blame] | 1450 | ret = cpu_exec(cpu); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1451 | #ifdef CONFIG_PROFILER |
Alexey Kardashevskiy | 89d5cbd | 2015-03-16 14:57:38 +1100 | [diff] [blame] | 1452 | tcg_time += profile_getclock() - ti; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1453 | #endif |
| 1454 | if (use_icount) { |
| 1455 | /* Fold pending instructions back into the |
| 1456 | instruction counter, and clear the interrupt flag. */ |
KONRAD Frederic | c96778b | 2014-08-01 01:37:09 +0200 | [diff] [blame] | 1457 | timers_state.qemu_icount -= (cpu->icount_decr.u16.low |
| 1458 | + cpu->icount_extra); |
Andreas Färber | 28ecfd7 | 2013-08-26 05:51:49 +0200 | [diff] [blame] | 1459 | cpu->icount_decr.u32 = 0; |
Andreas Färber | efee734 | 2013-08-26 05:39:29 +0200 | [diff] [blame] | 1460 | cpu->icount_extra = 0; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1461 | } |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
Jan Kiszka | bdb7ca6 | 2011-09-26 09:40:39 +0200 | [diff] [blame] | 1465 | static void tcg_exec_all(void) |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1466 | { |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1467 | int r; |
| 1468 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1469 | /* Account partial waits to QEMU_CLOCK_VIRTUAL. */ |
| 1470 | qemu_clock_warp(QEMU_CLOCK_VIRTUAL); |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 1471 | |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1472 | if (next_cpu == NULL) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1473 | next_cpu = first_cpu; |
Jan Kiszka | 0ab07c6 | 2011-02-07 12:19:14 +0100 | [diff] [blame] | 1474 | } |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1475 | for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1476 | CPUState *cpu = next_cpu; |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1477 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 1478 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, |
Andreas Färber | ed2803d | 2013-06-21 20:20:45 +0200 | [diff] [blame] | 1479 | (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1480 | |
Andreas Färber | a1fcaa7 | 2012-05-02 23:42:26 +0200 | [diff] [blame] | 1481 | if (cpu_can_run(cpu)) { |
Peter Crosthwaite | 3d57f78 | 2015-06-23 19:31:17 -0700 | [diff] [blame] | 1482 | r = tcg_cpu_exec(cpu); |
Jan Kiszka | 9a36085 | 2011-02-01 22:15:55 +0100 | [diff] [blame] | 1483 | if (r == EXCP_DEBUG) { |
Andreas Färber | 9132504 | 2013-05-27 02:07:49 +0200 | [diff] [blame] | 1484 | cpu_handle_guest_debug(cpu); |
Jan Kiszka | 3c638d0 | 2010-06-25 16:56:56 +0200 | [diff] [blame] | 1485 | break; |
| 1486 | } |
Andreas Färber | f324e76 | 2012-05-02 23:26:21 +0200 | [diff] [blame] | 1487 | } else if (cpu->stop || cpu->stopped) { |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1488 | break; |
| 1489 | } |
| 1490 | } |
Paolo Bonzini | aed807c | 2015-08-18 06:43:15 -0700 | [diff] [blame] | 1491 | |
| 1492 | /* Pairs with smp_wmb in qemu_cpu_kick. */ |
| 1493 | atomic_mb_set(&exit_request, 0); |
Blue Swirl | 296af7c | 2010-03-29 19:23:50 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Stefan Weil | 9a78eea | 2010-10-22 23:03:33 +0200 | [diff] [blame] | 1496 | void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg) |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 1497 | { |
| 1498 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
Peter Maydell | e916cbf | 2012-09-05 17:41:08 -0300 | [diff] [blame] | 1499 | #if defined(cpu_list) |
| 1500 | cpu_list(f, cpu_fprintf); |
Blue Swirl | 262353c | 2010-05-04 19:55:35 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | } |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1503 | |
| 1504 | CpuInfoList *qmp_query_cpus(Error **errp) |
| 1505 | { |
| 1506 | CpuInfoList *head = NULL, *cur_item = NULL; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1507 | CPUState *cpu; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1508 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1509 | CPU_FOREACH(cpu) { |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1510 | CpuInfoList *info; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1511 | #if defined(TARGET_I386) |
| 1512 | X86CPU *x86_cpu = X86_CPU(cpu); |
| 1513 | CPUX86State *env = &x86_cpu->env; |
| 1514 | #elif defined(TARGET_PPC) |
| 1515 | PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu); |
| 1516 | CPUPPCState *env = &ppc_cpu->env; |
| 1517 | #elif defined(TARGET_SPARC) |
| 1518 | SPARCCPU *sparc_cpu = SPARC_CPU(cpu); |
| 1519 | CPUSPARCState *env = &sparc_cpu->env; |
| 1520 | #elif defined(TARGET_MIPS) |
| 1521 | MIPSCPU *mips_cpu = MIPS_CPU(cpu); |
| 1522 | CPUMIPSState *env = &mips_cpu->env; |
Bastian Koppelmann | 48e06fe | 2014-09-01 12:59:46 +0100 | [diff] [blame] | 1523 | #elif defined(TARGET_TRICORE) |
| 1524 | TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu); |
| 1525 | CPUTriCoreState *env = &tricore_cpu->env; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1526 | #endif |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1527 | |
Andreas Färber | cb446ec | 2013-05-01 14:24:52 +0200 | [diff] [blame] | 1528 | cpu_synchronize_state(cpu); |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1529 | |
| 1530 | info = g_malloc0(sizeof(*info)); |
| 1531 | info->value = g_malloc0(sizeof(*info->value)); |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1532 | info->value->CPU = cpu->cpu_index; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1533 | info->value->current = (cpu == first_cpu); |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 1534 | info->value->halted = cpu->halted; |
Eduardo Habkost | 58f88d4 | 2015-05-08 16:04:22 -0300 | [diff] [blame] | 1535 | info->value->qom_path = object_get_canonical_path(OBJECT(cpu)); |
Andreas Färber | 9f09e18 | 2012-05-03 06:59:07 +0200 | [diff] [blame] | 1536 | info->value->thread_id = cpu->thread_id; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1537 | #if defined(TARGET_I386) |
| 1538 | info->value->has_pc = true; |
| 1539 | info->value->pc = env->eip + env->segs[R_CS].base; |
| 1540 | #elif defined(TARGET_PPC) |
| 1541 | info->value->has_nip = true; |
| 1542 | info->value->nip = env->nip; |
| 1543 | #elif defined(TARGET_SPARC) |
| 1544 | info->value->has_pc = true; |
| 1545 | info->value->pc = env->pc; |
| 1546 | info->value->has_npc = true; |
| 1547 | info->value->npc = env->npc; |
| 1548 | #elif defined(TARGET_MIPS) |
| 1549 | info->value->has_PC = true; |
| 1550 | info->value->PC = env->active_tc.PC; |
Bastian Koppelmann | 48e06fe | 2014-09-01 12:59:46 +0100 | [diff] [blame] | 1551 | #elif defined(TARGET_TRICORE) |
| 1552 | info->value->has_PC = true; |
| 1553 | info->value->PC = env->PC; |
Luiz Capitulino | de0b36b | 2011-09-21 16:38:35 -0300 | [diff] [blame] | 1554 | #endif |
| 1555 | |
| 1556 | /* XXX: waiting for the qapi to support GSList */ |
| 1557 | if (!cur_item) { |
| 1558 | head = cur_item = info; |
| 1559 | } else { |
| 1560 | cur_item->next = info; |
| 1561 | cur_item = info; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | return head; |
| 1566 | } |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1567 | |
| 1568 | void qmp_memsave(int64_t addr, int64_t size, const char *filename, |
| 1569 | bool has_cpu, int64_t cpu_index, Error **errp) |
| 1570 | { |
| 1571 | FILE *f; |
| 1572 | uint32_t l; |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1573 | CPUState *cpu; |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1574 | uint8_t buf[1024]; |
Borislav Petkov | 0dc9daf | 2015-02-08 13:14:38 +0100 | [diff] [blame] | 1575 | int64_t orig_addr = addr, orig_size = size; |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1576 | |
| 1577 | if (!has_cpu) { |
| 1578 | cpu_index = 0; |
| 1579 | } |
| 1580 | |
Andreas Färber | 151d132 | 2013-02-15 15:41:49 +0100 | [diff] [blame] | 1581 | cpu = qemu_get_cpu(cpu_index); |
| 1582 | if (cpu == NULL) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1583 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", |
| 1584 | "a CPU number"); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | f = fopen(filename, "wb"); |
| 1589 | if (!f) { |
Luiz Capitulino | 618da85 | 2013-06-07 14:35:06 -0400 | [diff] [blame] | 1590 | error_setg_file_open(errp, errno, filename); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1591 | return; |
| 1592 | } |
| 1593 | |
| 1594 | while (size != 0) { |
| 1595 | l = sizeof(buf); |
| 1596 | if (l > size) |
| 1597 | l = size; |
Aneesh Kumar K.V | 2f4d0f5 | 2013-10-01 21:49:30 +0530 | [diff] [blame] | 1598 | if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) { |
Borislav Petkov | 0dc9daf | 2015-02-08 13:14:38 +0100 | [diff] [blame] | 1599 | error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64 |
| 1600 | " specified", orig_addr, orig_size); |
Aneesh Kumar K.V | 2f4d0f5 | 2013-10-01 21:49:30 +0530 | [diff] [blame] | 1601 | goto exit; |
| 1602 | } |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1603 | if (fwrite(buf, 1, l, f) != l) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1604 | error_setg(errp, QERR_IO_ERROR); |
Luiz Capitulino | 0cfd6a9 | 2011-11-22 16:32:37 -0200 | [diff] [blame] | 1605 | goto exit; |
| 1606 | } |
| 1607 | addr += l; |
| 1608 | size -= l; |
| 1609 | } |
| 1610 | |
| 1611 | exit: |
| 1612 | fclose(f); |
| 1613 | } |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 1614 | |
| 1615 | void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, |
| 1616 | Error **errp) |
| 1617 | { |
| 1618 | FILE *f; |
| 1619 | uint32_t l; |
| 1620 | uint8_t buf[1024]; |
| 1621 | |
| 1622 | f = fopen(filename, "wb"); |
| 1623 | if (!f) { |
Luiz Capitulino | 618da85 | 2013-06-07 14:35:06 -0400 | [diff] [blame] | 1624 | error_setg_file_open(errp, errno, filename); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 1625 | return; |
| 1626 | } |
| 1627 | |
| 1628 | while (size != 0) { |
| 1629 | l = sizeof(buf); |
| 1630 | if (l > size) |
| 1631 | l = size; |
Stefan Weil | eb6282f | 2014-04-07 20:28:23 +0200 | [diff] [blame] | 1632 | cpu_physical_memory_read(addr, buf, l); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 1633 | if (fwrite(buf, 1, l, f) != l) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1634 | error_setg(errp, QERR_IO_ERROR); |
Luiz Capitulino | 6d3962b | 2011-11-22 17:26:46 -0200 | [diff] [blame] | 1635 | goto exit; |
| 1636 | } |
| 1637 | addr += l; |
| 1638 | size -= l; |
| 1639 | } |
| 1640 | |
| 1641 | exit: |
| 1642 | fclose(f); |
| 1643 | } |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 1644 | |
| 1645 | void qmp_inject_nmi(Error **errp) |
| 1646 | { |
| 1647 | #if defined(TARGET_I386) |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1648 | CPUState *cs; |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 1649 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 1650 | CPU_FOREACH(cs) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1651 | X86CPU *cpu = X86_CPU(cs); |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1652 | |
Chen Fan | 02e5148 | 2013-12-23 17:04:02 +0800 | [diff] [blame] | 1653 | if (!cpu->apic_state) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 1654 | cpu_interrupt(cs, CPU_INTERRUPT_NMI); |
Jan Kiszka | 02c0919 | 2011-10-18 00:00:06 +0800 | [diff] [blame] | 1655 | } else { |
Chen Fan | 02e5148 | 2013-12-23 17:04:02 +0800 | [diff] [blame] | 1656 | apic_deliver_nmi(cpu->apic_state); |
Jan Kiszka | 02c0919 | 2011-10-18 00:00:06 +0800 | [diff] [blame] | 1657 | } |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 1658 | } |
| 1659 | #else |
Alexey Kardashevskiy | 9cb805f | 2014-08-20 22:16:33 +1000 | [diff] [blame] | 1660 | nmi_monitor_handle(monitor_get_cpu_index(), errp); |
Luiz Capitulino | ab49ab5 | 2011-11-23 12:55:53 -0200 | [diff] [blame] | 1661 | #endif |
| 1662 | } |
Sebastian Tanase | 27498be | 2014-07-25 11:56:33 +0200 | [diff] [blame] | 1663 | |
| 1664 | void dump_drift_info(FILE *f, fprintf_function cpu_fprintf) |
| 1665 | { |
| 1666 | if (!use_icount) { |
| 1667 | return; |
| 1668 | } |
| 1669 | |
| 1670 | cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n", |
| 1671 | (cpu_get_clock() - cpu_get_icount())/SCALE_MS); |
| 1672 | if (icount_align_option) { |
| 1673 | cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS); |
| 1674 | cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS); |
| 1675 | } else { |
| 1676 | cpu_fprintf(f, "Max guest delay NA\n"); |
| 1677 | cpu_fprintf(f, "Max guest advance NA\n"); |
| 1678 | } |
| 1679 | } |