spapr_pci: Fix extended config space accesses
[qemu/ar7.git] / replay / replay-internal.c
blob8f87e9b957e12c1fe82de93df2919ab09c709c88
1 /*
2 * replay-internal.c
4 * Copyright (c) 2010-2015 Institute for System Programming
5 * of the Russian Academy of Sciences.
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.
12 #include "qemu/osdep.h"
13 #include "qemu-common.h"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "qemu/error-report.h"
17 #include "sysemu/sysemu.h"
19 /* Mutex to protect reading and writing events to the log.
20 data_kind and has_unread_data are also protected
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;
26 /* File for replay writing */
27 static bool write_error;
28 FILE *replay_file;
30 static void replay_write_error(void)
32 if (!write_error) {
33 error_report("replay write error");
34 write_error = true;
38 static void replay_read_error(void)
40 error_report("error reading the replay data");
41 exit(1);
44 void replay_put_byte(uint8_t byte)
46 if (replay_file) {
47 if (putc(byte, replay_file) == EOF) {
48 replay_write_error();
53 void replay_put_event(uint8_t event)
55 assert(event < EVENT_COUNT);
56 replay_put_byte(event);
60 void replay_put_word(uint16_t word)
62 replay_put_byte(word >> 8);
63 replay_put_byte(word);
66 void replay_put_dword(uint32_t dword)
68 replay_put_word(dword >> 16);
69 replay_put_word(dword);
72 void replay_put_qword(int64_t qword)
74 replay_put_dword(qword >> 32);
75 replay_put_dword(qword);
78 void replay_put_array(const uint8_t *buf, size_t size)
80 if (replay_file) {
81 replay_put_dword(size);
82 if (fwrite(buf, 1, size, replay_file) != size) {
83 replay_write_error();
88 uint8_t replay_get_byte(void)
90 uint8_t byte = 0;
91 if (replay_file) {
92 int r = getc(replay_file);
93 if (r == EOF) {
94 replay_read_error();
96 byte = r;
98 return byte;
101 uint16_t replay_get_word(void)
103 uint16_t word = 0;
104 if (replay_file) {
105 word = replay_get_byte();
106 word = (word << 8) + replay_get_byte();
109 return word;
112 uint32_t replay_get_dword(void)
114 uint32_t dword = 0;
115 if (replay_file) {
116 dword = replay_get_word();
117 dword = (dword << 16) + replay_get_word();
120 return dword;
123 int64_t replay_get_qword(void)
125 int64_t qword = 0;
126 if (replay_file) {
127 qword = replay_get_dword();
128 qword = (qword << 32) + replay_get_dword();
131 return qword;
134 void replay_get_array(uint8_t *buf, size_t *size)
136 if (replay_file) {
137 *size = replay_get_dword();
138 if (fread(buf, 1, *size, replay_file) != *size) {
139 replay_read_error();
144 void replay_get_array_alloc(uint8_t **buf, size_t *size)
146 if (replay_file) {
147 *size = replay_get_dword();
148 *buf = g_malloc(*size);
149 if (fread(*buf, 1, *size, replay_file) != *size) {
150 replay_read_error();
155 void replay_check_error(void)
157 if (replay_file) {
158 if (feof(replay_file)) {
159 error_report("replay file is over");
160 qemu_system_vmstop_request_prepare();
161 qemu_system_vmstop_request(RUN_STATE_PAUSED);
162 } else if (ferror(replay_file)) {
163 error_report("replay file is over or something goes wrong");
164 qemu_system_vmstop_request_prepare();
165 qemu_system_vmstop_request(RUN_STATE_INTERNAL_ERROR);
170 void replay_fetch_data_kind(void)
172 if (replay_file) {
173 if (!replay_state.has_unread_data) {
174 replay_state.data_kind = replay_get_byte();
175 if (replay_state.data_kind == EVENT_INSTRUCTION) {
176 replay_state.instructions_count = replay_get_dword();
178 replay_check_error();
179 replay_state.has_unread_data = 1;
180 if (replay_state.data_kind >= EVENT_COUNT) {
181 error_report("Replay: unknown event kind %d",
182 replay_state.data_kind);
183 exit(1);
189 void replay_finish_event(void)
191 replay_state.has_unread_data = 0;
192 replay_fetch_data_kind();
195 static __thread bool replay_locked;
197 void replay_mutex_init(void)
199 qemu_mutex_init(&lock);
200 /* Hold the mutex while we start-up */
201 qemu_mutex_lock(&lock);
202 replay_locked = true;
205 bool replay_mutex_locked(void)
207 return replay_locked;
210 /* Ordering constraints, replay_lock must be taken before BQL */
211 void replay_mutex_lock(void)
213 if (replay_mode != REPLAY_MODE_NONE) {
214 g_assert(!qemu_mutex_iothread_locked());
215 g_assert(!replay_mutex_locked());
216 qemu_mutex_lock(&lock);
217 replay_locked = true;
221 void replay_mutex_unlock(void)
223 if (replay_mode != REPLAY_MODE_NONE) {
224 g_assert(replay_mutex_locked());
225 replay_locked = false;
226 qemu_mutex_unlock(&lock);
230 void replay_advance_current_step(uint64_t current_step)
232 int diff = (int)(replay_get_current_step() - replay_state.current_step);
234 /* Time can only go forward */
235 assert(diff >= 0);
237 if (diff > 0) {
238 replay_put_event(EVENT_INSTRUCTION);
239 replay_put_dword(diff);
240 replay_state.current_step += diff;
244 /*! Saves cached instructions. */
245 void replay_save_instructions(void)
247 if (replay_file && replay_mode == REPLAY_MODE_RECORD) {
248 g_assert(replay_mutex_locked());
249 replay_advance_current_step(replay_get_current_step());