Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 1 | /* |
| 2 | * replay-internal.c |
| 3 | * |
| 4 | * Copyright (c) 2010-2015 Institute for System Programming |
| 5 | * of the Russian Academy of Sciences. |
| 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | * |
| 10 | */ |
| 11 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 12 | #include "qemu/osdep.h" |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 13 | #include "sysemu/replay.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 14 | #include "sysemu/runstate.h" |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 15 | #include "replay-internal.h" |
| 16 | #include "qemu/error-report.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 17 | #include "qemu/main-loop.h" |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 18 | |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 19 | /* Mutex to protect reading and writing events to the log. |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 20 | data_kind and has_unread_data are also protected |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 21 | by this mutex. |
| 22 | It also protects replay events queue which stores events to be |
| 23 | written or read to the log. */ |
| 24 | static QemuMutex lock; |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 25 | /* Condition and queue for fair ordering of mutex lock requests. */ |
| 26 | static QemuCond mutex_cond; |
| 27 | static unsigned long mutex_head, mutex_tail; |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 28 | |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 29 | /* File for replay writing */ |
Pavel Dovgalyuk | 6dc0f52 | 2018-02-27 12:52:59 +0300 | [diff] [blame] | 30 | static bool write_error; |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 31 | FILE *replay_file; |
| 32 | |
Pavel Dovgalyuk | 6dc0f52 | 2018-02-27 12:52:59 +0300 | [diff] [blame] | 33 | static void replay_write_error(void) |
| 34 | { |
| 35 | if (!write_error) { |
| 36 | error_report("replay write error"); |
| 37 | write_error = true; |
| 38 | } |
| 39 | } |
| 40 | |
Peter Maydell | 0b57007 | 2018-11-06 15:33:30 +0000 | [diff] [blame] | 41 | static void replay_read_error(void) |
| 42 | { |
| 43 | error_report("error reading the replay data"); |
| 44 | exit(1); |
| 45 | } |
| 46 | |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 47 | void replay_put_byte(uint8_t byte) |
| 48 | { |
| 49 | if (replay_file) { |
Pavel Dovgalyuk | 6dc0f52 | 2018-02-27 12:52:59 +0300 | [diff] [blame] | 50 | if (putc(byte, replay_file) == EOF) { |
| 51 | replay_write_error(); |
| 52 | } |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | void replay_put_event(uint8_t event) |
| 57 | { |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 58 | assert(event < EVENT_COUNT); |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 59 | replay_put_byte(event); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | void replay_put_word(uint16_t word) |
| 64 | { |
| 65 | replay_put_byte(word >> 8); |
| 66 | replay_put_byte(word); |
| 67 | } |
| 68 | |
| 69 | void replay_put_dword(uint32_t dword) |
| 70 | { |
| 71 | replay_put_word(dword >> 16); |
| 72 | replay_put_word(dword); |
| 73 | } |
| 74 | |
| 75 | void replay_put_qword(int64_t qword) |
| 76 | { |
| 77 | replay_put_dword(qword >> 32); |
| 78 | replay_put_dword(qword); |
| 79 | } |
| 80 | |
| 81 | void replay_put_array(const uint8_t *buf, size_t size) |
| 82 | { |
| 83 | if (replay_file) { |
| 84 | replay_put_dword(size); |
Pavel Dovgalyuk | 6dc0f52 | 2018-02-27 12:52:59 +0300 | [diff] [blame] | 85 | if (fwrite(buf, 1, size, replay_file) != size) { |
| 86 | replay_write_error(); |
| 87 | } |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | uint8_t replay_get_byte(void) |
| 92 | { |
| 93 | uint8_t byte = 0; |
| 94 | if (replay_file) { |
Peter Maydell | 0b57007 | 2018-11-06 15:33:30 +0000 | [diff] [blame] | 95 | int r = getc(replay_file); |
| 96 | if (r == EOF) { |
| 97 | replay_read_error(); |
| 98 | } |
| 99 | byte = r; |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 100 | } |
| 101 | return byte; |
| 102 | } |
| 103 | |
| 104 | uint16_t replay_get_word(void) |
| 105 | { |
| 106 | uint16_t word = 0; |
| 107 | if (replay_file) { |
| 108 | word = replay_get_byte(); |
| 109 | word = (word << 8) + replay_get_byte(); |
| 110 | } |
| 111 | |
| 112 | return word; |
| 113 | } |
| 114 | |
| 115 | uint32_t replay_get_dword(void) |
| 116 | { |
| 117 | uint32_t dword = 0; |
| 118 | if (replay_file) { |
| 119 | dword = replay_get_word(); |
| 120 | dword = (dword << 16) + replay_get_word(); |
| 121 | } |
| 122 | |
| 123 | return dword; |
| 124 | } |
| 125 | |
| 126 | int64_t replay_get_qword(void) |
| 127 | { |
| 128 | int64_t qword = 0; |
| 129 | if (replay_file) { |
| 130 | qword = replay_get_dword(); |
| 131 | qword = (qword << 32) + replay_get_dword(); |
| 132 | } |
| 133 | |
| 134 | return qword; |
| 135 | } |
| 136 | |
| 137 | void replay_get_array(uint8_t *buf, size_t *size) |
| 138 | { |
| 139 | if (replay_file) { |
| 140 | *size = replay_get_dword(); |
| 141 | if (fread(buf, 1, *size, replay_file) != *size) { |
Peter Maydell | 0b57007 | 2018-11-06 15:33:30 +0000 | [diff] [blame] | 142 | replay_read_error(); |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void replay_get_array_alloc(uint8_t **buf, size_t *size) |
| 148 | { |
| 149 | if (replay_file) { |
| 150 | *size = replay_get_dword(); |
| 151 | *buf = g_malloc(*size); |
| 152 | if (fread(*buf, 1, *size, replay_file) != *size) { |
Peter Maydell | 0b57007 | 2018-11-06 15:33:30 +0000 | [diff] [blame] | 153 | replay_read_error(); |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void replay_check_error(void) |
| 159 | { |
| 160 | if (replay_file) { |
| 161 | if (feof(replay_file)) { |
| 162 | error_report("replay file is over"); |
| 163 | qemu_system_vmstop_request_prepare(); |
| 164 | qemu_system_vmstop_request(RUN_STATE_PAUSED); |
| 165 | } else if (ferror(replay_file)) { |
| 166 | error_report("replay file is over or something goes wrong"); |
| 167 | qemu_system_vmstop_request_prepare(); |
| 168 | qemu_system_vmstop_request(RUN_STATE_INTERNAL_ERROR); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void replay_fetch_data_kind(void) |
| 174 | { |
| 175 | if (replay_file) { |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 176 | if (!replay_state.has_unread_data) { |
| 177 | replay_state.data_kind = replay_get_byte(); |
| 178 | if (replay_state.data_kind == EVENT_INSTRUCTION) { |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 179 | replay_state.instruction_count = replay_get_dword(); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 180 | } |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 181 | replay_check_error(); |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 182 | replay_state.has_unread_data = 1; |
| 183 | if (replay_state.data_kind >= EVENT_COUNT) { |
| 184 | error_report("Replay: unknown event kind %d", |
| 185 | replay_state.data_kind); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 186 | exit(1); |
| 187 | } |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | void replay_finish_event(void) |
| 193 | { |
Pavel Dovgalyuk | f186d64 | 2016-09-26 11:08:04 +0300 | [diff] [blame] | 194 | replay_state.has_unread_data = 0; |
Pavel Dovgalyuk | c92079f | 2015-09-17 19:23:43 +0300 | [diff] [blame] | 195 | replay_fetch_data_kind(); |
| 196 | } |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 197 | |
Alex Bennée | 180d30b | 2018-02-27 12:52:37 +0300 | [diff] [blame] | 198 | static __thread bool replay_locked; |
| 199 | |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 200 | void replay_mutex_init(void) |
| 201 | { |
| 202 | qemu_mutex_init(&lock); |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 203 | qemu_cond_init(&mutex_cond); |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 204 | /* Hold the mutex while we start-up */ |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 205 | replay_locked = true; |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 206 | ++mutex_tail; |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 207 | } |
| 208 | |
Alex Bennée | a36544d | 2018-02-27 12:52:42 +0300 | [diff] [blame] | 209 | bool replay_mutex_locked(void) |
Alex Bennée | 180d30b | 2018-02-27 12:52:37 +0300 | [diff] [blame] | 210 | { |
| 211 | return replay_locked; |
| 212 | } |
| 213 | |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 214 | /* Ordering constraints, replay_lock must be taken before BQL */ |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 215 | void replay_mutex_lock(void) |
| 216 | { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 217 | if (replay_mode != REPLAY_MODE_NONE) { |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 218 | unsigned long id; |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 219 | g_assert(!qemu_mutex_iothread_locked()); |
| 220 | g_assert(!replay_mutex_locked()); |
| 221 | qemu_mutex_lock(&lock); |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 222 | id = mutex_tail++; |
| 223 | while (id != mutex_head) { |
| 224 | qemu_cond_wait(&mutex_cond, &lock); |
| 225 | } |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 226 | replay_locked = true; |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 227 | qemu_mutex_unlock(&lock); |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 228 | } |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void replay_mutex_unlock(void) |
| 232 | { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 233 | if (replay_mode != REPLAY_MODE_NONE) { |
| 234 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 235 | qemu_mutex_lock(&lock); |
| 236 | ++mutex_head; |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 237 | replay_locked = false; |
Pavel Dovgalyuk | ddf63df | 2020-04-30 12:13:49 +0300 | [diff] [blame] | 238 | qemu_cond_broadcast(&mutex_cond); |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 239 | qemu_mutex_unlock(&lock); |
| 240 | } |
Pavel Dovgalyuk | c16861e | 2015-09-17 19:23:48 +0300 | [diff] [blame] | 241 | } |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 242 | |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 243 | void replay_advance_current_icount(uint64_t current_icount) |
Paolo Bonzini | 74c0b81 | 2018-10-08 13:24:14 +0200 | [diff] [blame] | 244 | { |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 245 | int diff = (int)(current_icount - replay_state.current_icount); |
Paolo Bonzini | 74c0b81 | 2018-10-08 13:24:14 +0200 | [diff] [blame] | 246 | |
| 247 | /* Time can only go forward */ |
| 248 | assert(diff >= 0); |
| 249 | |
| 250 | if (diff > 0) { |
| 251 | replay_put_event(EVENT_INSTRUCTION); |
| 252 | replay_put_dword(diff); |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 253 | replay_state.current_icount += diff; |
Paolo Bonzini | 74c0b81 | 2018-10-08 13:24:14 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 257 | /*! Saves cached instructions. */ |
| 258 | void replay_save_instructions(void) |
| 259 | { |
| 260 | if (replay_file && replay_mode == REPLAY_MODE_RECORD) { |
Alex Bennée | d759c95 | 2018-02-27 12:52:48 +0300 | [diff] [blame] | 261 | g_assert(replay_mutex_locked()); |
Pavel Dovgalyuk | 13f2671 | 2019-07-25 11:44:43 +0300 | [diff] [blame] | 262 | replay_advance_current_icount(replay_get_current_icount()); |
Pavel Dovgalyuk | 26bc60a | 2015-09-17 19:23:54 +0300 | [diff] [blame] | 263 | } |
| 264 | } |