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