]>
Commit | Line | Data |
---|---|---|
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. */ | |
24 | static QemuMutex lock; | |
25 | ||
c92079f4 | 26 | /* File for replay writing */ |
6dc0f529 | 27 | static bool write_error; |
c92079f4 PD |
28 | FILE *replay_file; |
29 | ||
6dc0f529 PD |
30 | static void replay_write_error(void) |
31 | { | |
32 | if (!write_error) { | |
33 | error_report("replay write error"); | |
34 | write_error = true; | |
35 | } | |
36 | } | |
37 | ||
0b570077 PM |
38 | static void replay_read_error(void) |
39 | { | |
40 | error_report("error reading the replay data"); | |
41 | exit(1); | |
42 | } | |
43 | ||
c92079f4 PD |
44 | void replay_put_byte(uint8_t byte) |
45 | { | |
46 | if (replay_file) { | |
6dc0f529 PD |
47 | if (putc(byte, replay_file) == EOF) { |
48 | replay_write_error(); | |
49 | } | |
c92079f4 PD |
50 | } |
51 | } | |
52 | ||
53 | void replay_put_event(uint8_t event) | |
54 | { | |
26bc60ac | 55 | assert(event < EVENT_COUNT); |
c92079f4 PD |
56 | replay_put_byte(event); |
57 | } | |
58 | ||
59 | ||
60 | void replay_put_word(uint16_t word) | |
61 | { | |
62 | replay_put_byte(word >> 8); | |
63 | replay_put_byte(word); | |
64 | } | |
65 | ||
66 | void replay_put_dword(uint32_t dword) | |
67 | { | |
68 | replay_put_word(dword >> 16); | |
69 | replay_put_word(dword); | |
70 | } | |
71 | ||
72 | void replay_put_qword(int64_t qword) | |
73 | { | |
74 | replay_put_dword(qword >> 32); | |
75 | replay_put_dword(qword); | |
76 | } | |
77 | ||
78 | void replay_put_array(const uint8_t *buf, size_t size) | |
79 | { | |
80 | if (replay_file) { | |
81 | replay_put_dword(size); | |
6dc0f529 PD |
82 | if (fwrite(buf, 1, size, replay_file) != size) { |
83 | replay_write_error(); | |
84 | } | |
c92079f4 PD |
85 | } |
86 | } | |
87 | ||
88 | uint8_t replay_get_byte(void) | |
89 | { | |
90 | uint8_t byte = 0; | |
91 | if (replay_file) { | |
0b570077 PM |
92 | int r = getc(replay_file); |
93 | if (r == EOF) { | |
94 | replay_read_error(); | |
95 | } | |
96 | byte = r; | |
c92079f4 PD |
97 | } |
98 | return byte; | |
99 | } | |
100 | ||
101 | uint16_t replay_get_word(void) | |
102 | { | |
103 | uint16_t word = 0; | |
104 | if (replay_file) { | |
105 | word = replay_get_byte(); | |
106 | word = (word << 8) + replay_get_byte(); | |
107 | } | |
108 | ||
109 | return word; | |
110 | } | |
111 | ||
112 | uint32_t replay_get_dword(void) | |
113 | { | |
114 | uint32_t dword = 0; | |
115 | if (replay_file) { | |
116 | dword = replay_get_word(); | |
117 | dword = (dword << 16) + replay_get_word(); | |
118 | } | |
119 | ||
120 | return dword; | |
121 | } | |
122 | ||
123 | int64_t replay_get_qword(void) | |
124 | { | |
125 | int64_t qword = 0; | |
126 | if (replay_file) { | |
127 | qword = replay_get_dword(); | |
128 | qword = (qword << 32) + replay_get_dword(); | |
129 | } | |
130 | ||
131 | return qword; | |
132 | } | |
133 | ||
134 | void replay_get_array(uint8_t *buf, size_t *size) | |
135 | { | |
136 | if (replay_file) { | |
137 | *size = replay_get_dword(); | |
138 | if (fread(buf, 1, *size, replay_file) != *size) { | |
0b570077 | 139 | replay_read_error(); |
c92079f4 PD |
140 | } |
141 | } | |
142 | } | |
143 | ||
144 | void replay_get_array_alloc(uint8_t **buf, size_t *size) | |
145 | { | |
146 | if (replay_file) { | |
147 | *size = replay_get_dword(); | |
148 | *buf = g_malloc(*size); | |
149 | if (fread(*buf, 1, *size, replay_file) != *size) { | |
0b570077 | 150 | replay_read_error(); |
c92079f4 PD |
151 | } |
152 | } | |
153 | } | |
154 | ||
155 | void replay_check_error(void) | |
156 | { | |
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); | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | void replay_fetch_data_kind(void) | |
171 | { | |
172 | if (replay_file) { | |
f186d64d PD |
173 | if (!replay_state.has_unread_data) { |
174 | replay_state.data_kind = replay_get_byte(); | |
175 | if (replay_state.data_kind == EVENT_INSTRUCTION) { | |
13f26713 | 176 | replay_state.instruction_count = replay_get_dword(); |
26bc60ac | 177 | } |
c92079f4 | 178 | replay_check_error(); |
f186d64d PD |
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); | |
26bc60ac PD |
183 | exit(1); |
184 | } | |
c92079f4 PD |
185 | } |
186 | } | |
187 | } | |
188 | ||
189 | void replay_finish_event(void) | |
190 | { | |
f186d64d | 191 | replay_state.has_unread_data = 0; |
c92079f4 PD |
192 | replay_fetch_data_kind(); |
193 | } | |
c16861ef | 194 | |
180d30be AB |
195 | static __thread bool replay_locked; |
196 | ||
c16861ef PD |
197 | void replay_mutex_init(void) |
198 | { | |
199 | qemu_mutex_init(&lock); | |
d759c951 AB |
200 | /* Hold the mutex while we start-up */ |
201 | qemu_mutex_lock(&lock); | |
202 | replay_locked = true; | |
c16861ef PD |
203 | } |
204 | ||
a36544d3 | 205 | bool replay_mutex_locked(void) |
180d30be AB |
206 | { |
207 | return replay_locked; | |
208 | } | |
209 | ||
d759c951 | 210 | /* Ordering constraints, replay_lock must be taken before BQL */ |
c16861ef PD |
211 | void replay_mutex_lock(void) |
212 | { | |
d759c951 AB |
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; | |
218 | } | |
c16861ef PD |
219 | } |
220 | ||
221 | void replay_mutex_unlock(void) | |
222 | { | |
d759c951 AB |
223 | if (replay_mode != REPLAY_MODE_NONE) { |
224 | g_assert(replay_mutex_locked()); | |
225 | replay_locked = false; | |
226 | qemu_mutex_unlock(&lock); | |
227 | } | |
c16861ef | 228 | } |
26bc60ac | 229 | |
13f26713 | 230 | void replay_advance_current_icount(uint64_t current_icount) |
74c0b816 | 231 | { |
13f26713 | 232 | int diff = (int)(current_icount - replay_state.current_icount); |
74c0b816 PB |
233 | |
234 | /* Time can only go forward */ | |
235 | assert(diff >= 0); | |
236 | ||
237 | if (diff > 0) { | |
238 | replay_put_event(EVENT_INSTRUCTION); | |
239 | replay_put_dword(diff); | |
13f26713 | 240 | replay_state.current_icount += diff; |
74c0b816 PB |
241 | } |
242 | } | |
243 | ||
26bc60ac PD |
244 | /*! Saves cached instructions. */ |
245 | void replay_save_instructions(void) | |
246 | { | |
247 | if (replay_file && replay_mode == REPLAY_MODE_RECORD) { | |
d759c951 | 248 | g_assert(replay_mutex_locked()); |
13f26713 | 249 | replay_advance_current_icount(replay_get_current_icount()); |
26bc60ac PD |
250 | } |
251 | } |