]> git.proxmox.com Git - mirror_qemu.git/blame - replay/replay-internal.c
target/loongarch/kvm: Enable LSX/LASX extension
[mirror_qemu.git] / replay / replay-internal.c
CommitLineData
c92079f4
PD
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
d38ea87a 12#include "qemu/osdep.h"
26bc60ac 13#include "sysemu/replay.h"
54d31236 14#include "sysemu/runstate.h"
c92079f4
PD
15#include "replay-internal.h"
16#include "qemu/error-report.h"
db725815 17#include "qemu/main-loop.h"
c92079f4 18
c16861ef 19/* Mutex to protect reading and writing events to the log.
f186d64d 20 data_kind and has_unread_data are also protected
c16861ef
PD
21 by this mutex.
22 It also protects replay events queue which stores events to be
23 written or read to the log. */
24static QemuMutex lock;
ddf63df7
PD
25/* Condition and queue for fair ordering of mutex lock requests. */
26static QemuCond mutex_cond;
27static unsigned long mutex_head, mutex_tail;
c16861ef 28
c92079f4 29/* File for replay writing */
6dc0f529 30static bool write_error;
c92079f4
PD
31FILE *replay_file;
32
6dc0f529
PD
33static void replay_write_error(void)
34{
35 if (!write_error) {
36 error_report("replay write error");
37 write_error = true;
38 }
39}
40
0b570077
PM
41static void replay_read_error(void)
42{
43 error_report("error reading the replay data");
44 exit(1);
45}
46
c92079f4
PD
47void replay_put_byte(uint8_t byte)
48{
49 if (replay_file) {
6dc0f529
PD
50 if (putc(byte, replay_file) == EOF) {
51 replay_write_error();
52 }
c92079f4
PD
53 }
54}
55
56void replay_put_event(uint8_t event)
57{
26bc60ac 58 assert(event < EVENT_COUNT);
c92079f4
PD
59 replay_put_byte(event);
60}
61
62
63void replay_put_word(uint16_t word)
64{
65 replay_put_byte(word >> 8);
66 replay_put_byte(word);
67}
68
69void replay_put_dword(uint32_t dword)
70{
71 replay_put_word(dword >> 16);
72 replay_put_word(dword);
73}
74
75void replay_put_qword(int64_t qword)
76{
77 replay_put_dword(qword >> 32);
78 replay_put_dword(qword);
79}
80
81void replay_put_array(const uint8_t *buf, size_t size)
82{
83 if (replay_file) {
84 replay_put_dword(size);
6dc0f529
PD
85 if (fwrite(buf, 1, size, replay_file) != size) {
86 replay_write_error();
87 }
c92079f4
PD
88 }
89}
90
91uint8_t replay_get_byte(void)
92{
93 uint8_t byte = 0;
94 if (replay_file) {
0b570077
PM
95 int r = getc(replay_file);
96 if (r == EOF) {
97 replay_read_error();
98 }
99 byte = r;
c92079f4
PD
100 }
101 return byte;
102}
103
104uint16_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
115uint32_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
126int64_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
137void 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) {
0b570077 142 replay_read_error();
c92079f4
PD
143 }
144 }
145}
146
147void 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) {
0b570077 153 replay_read_error();
c92079f4
PD
154 }
155 }
156}
157
158void 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
173void replay_fetch_data_kind(void)
174{
175 if (replay_file) {
f186d64d
PD
176 if (!replay_state.has_unread_data) {
177 replay_state.data_kind = replay_get_byte();
dcda7321 178 replay_state.current_event++;
f186d64d 179 if (replay_state.data_kind == EVENT_INSTRUCTION) {
13f26713 180 replay_state.instruction_count = replay_get_dword();
26bc60ac 181 }
c92079f4 182 replay_check_error();
2b7a58b6 183 replay_state.has_unread_data = true;
f186d64d
PD
184 if (replay_state.data_kind >= EVENT_COUNT) {
185 error_report("Replay: unknown event kind %d",
186 replay_state.data_kind);
26bc60ac
PD
187 exit(1);
188 }
c92079f4
PD
189 }
190 }
191}
192
193void replay_finish_event(void)
194{
2b7a58b6 195 replay_state.has_unread_data = false;
c92079f4
PD
196 replay_fetch_data_kind();
197}
c16861ef 198
180d30be
AB
199static __thread bool replay_locked;
200
c16861ef
PD
201void replay_mutex_init(void)
202{
203 qemu_mutex_init(&lock);
ddf63df7 204 qemu_cond_init(&mutex_cond);
d759c951 205 /* Hold the mutex while we start-up */
d759c951 206 replay_locked = true;
ddf63df7 207 ++mutex_tail;
c16861ef
PD
208}
209
a36544d3 210bool replay_mutex_locked(void)
180d30be
AB
211{
212 return replay_locked;
213}
214
d759c951 215/* Ordering constraints, replay_lock must be taken before BQL */
c16861ef
PD
216void replay_mutex_lock(void)
217{
d759c951 218 if (replay_mode != REPLAY_MODE_NONE) {
ddf63df7 219 unsigned long id;
195801d7 220 g_assert(!bql_locked());
d759c951
AB
221 g_assert(!replay_mutex_locked());
222 qemu_mutex_lock(&lock);
ddf63df7
PD
223 id = mutex_tail++;
224 while (id != mutex_head) {
225 qemu_cond_wait(&mutex_cond, &lock);
226 }
d759c951 227 replay_locked = true;
ddf63df7 228 qemu_mutex_unlock(&lock);
d759c951 229 }
c16861ef
PD
230}
231
232void replay_mutex_unlock(void)
233{
d759c951
AB
234 if (replay_mode != REPLAY_MODE_NONE) {
235 g_assert(replay_mutex_locked());
ddf63df7
PD
236 qemu_mutex_lock(&lock);
237 ++mutex_head;
d759c951 238 replay_locked = false;
ddf63df7 239 qemu_cond_broadcast(&mutex_cond);
d759c951
AB
240 qemu_mutex_unlock(&lock);
241 }
c16861ef 242}
26bc60ac 243
13f26713 244void replay_advance_current_icount(uint64_t current_icount)
74c0b816 245{
13f26713 246 int diff = (int)(current_icount - replay_state.current_icount);
74c0b816
PB
247
248 /* Time can only go forward */
249 assert(diff >= 0);
250
366a85e4
PD
251 if (replay_mode == REPLAY_MODE_RECORD) {
252 if (diff > 0) {
253 replay_put_event(EVENT_INSTRUCTION);
254 replay_put_dword(diff);
255 replay_state.current_icount += diff;
256 }
257 } else if (replay_mode == REPLAY_MODE_PLAY) {
258 if (diff > 0) {
259 replay_state.instruction_count -= diff;
260 replay_state.current_icount += diff;
261 if (replay_state.instruction_count == 0) {
262 assert(replay_state.data_kind == EVENT_INSTRUCTION);
263 replay_finish_event();
264 /* Wake up iothread. This is required because
265 timers will not expire until clock counters
266 will be read from the log. */
267 qemu_notify_event();
268 }
269 }
270 /* Execution reached the break step */
271 if (replay_break_icount == replay_state.current_icount) {
272 /* Cannot make callback directly from the vCPU thread */
273 timer_mod_ns(replay_break_timer,
274 qemu_clock_get_ns(QEMU_CLOCK_REALTIME));
275 }
74c0b816
PB
276 }
277}
278
26bc60ac
PD
279/*! Saves cached instructions. */
280void replay_save_instructions(void)
281{
282 if (replay_file && replay_mode == REPLAY_MODE_RECORD) {
d759c951 283 g_assert(replay_mutex_locked());
13f26713 284 replay_advance_current_icount(replay_get_current_icount());
26bc60ac
PD
285 }
286}