1 #ifndef REPLAY_INTERNAL_H
2 #define REPLAY_INTERNAL_H
7 * Copyright (c) 2010-2015 Institute for System Programming
8 * of the Russian Academy of Sciences.
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
15 /* Any changes to order/number of events will need to bump REPLAY_VERSION */
17 /* for instruction event */
19 /* for software interrupt */
21 /* for emulated exceptions */
23 /* for async events */
25 /* for shutdown requests, range allows recovery of ShutdownCause */
27 EVENT_SHUTDOWN_LAST
= EVENT_SHUTDOWN
+ SHUTDOWN_CAUSE__MAX
,
28 /* for character device write event */
30 /* for character device read all event */
32 EVENT_CHAR_READ_ALL_ERROR
,
33 /* for audio out event */
35 /* for audio in event */
37 /* for clock read/writes */
38 /* some of greater codes are reserved for clocks */
40 EVENT_CLOCK_LAST
= EVENT_CLOCK
+ REPLAY_CLOCK_COUNT
- 1,
41 /* for checkpoint event */
42 /* some of greater codes are reserved for checkpoints */
44 EVENT_CHECKPOINT_LAST
= EVENT_CHECKPOINT
+ CHECKPOINT_COUNT
- 1,
45 /* end of log event */
50 /* Asynchronous events IDs */
52 enum ReplayAsyncEventKind
{
53 REPLAY_ASYNC_EVENT_BH
,
54 REPLAY_ASYNC_EVENT_INPUT
,
55 REPLAY_ASYNC_EVENT_INPUT_SYNC
,
56 REPLAY_ASYNC_EVENT_CHAR_READ
,
57 REPLAY_ASYNC_EVENT_BLOCK
,
58 REPLAY_ASYNC_EVENT_NET
,
62 typedef enum ReplayAsyncEventKind ReplayAsyncEventKind
;
64 typedef struct ReplayState
{
65 /*! Cached clock values. */
66 int64_t cached_clock
[REPLAY_CLOCK_COUNT
];
67 /*! Current step - number of processed instructions and timer events. */
68 uint64_t current_step
;
69 /*! Number of instructions to be executed before other events happen. */
70 int instructions_count
;
71 /*! Type of the currently executed event. */
72 unsigned int data_kind
;
73 /*! Flag which indicates that event is not processed yet. */
74 unsigned int has_unread_data
;
75 /*! Temporary variable for saving current log offset. */
77 /*! Next block operation id.
78 This counter is global, because requests from different
79 block devices should not get overlapping ids. */
80 uint64_t block_request_id
;
81 /*! Prior value of the host clock */
82 uint64_t host_clock_last
;
83 /*! Asynchronous event type read from the log */
84 int32_t read_event_kind
;
85 /*! Asynchronous event id read from the log */
86 uint64_t read_event_id
;
87 /*! Asynchronous event checkpoint id read from the log */
88 int32_t read_event_checkpoint
;
90 extern ReplayState replay_state
;
92 /* File for replay writing */
93 extern FILE *replay_file
;
95 void replay_put_byte(uint8_t byte
);
96 void replay_put_event(uint8_t event
);
97 void replay_put_word(uint16_t word
);
98 void replay_put_dword(uint32_t dword
);
99 void replay_put_qword(int64_t qword
);
100 void replay_put_array(const uint8_t *buf
, size_t size
);
102 uint8_t replay_get_byte(void);
103 uint16_t replay_get_word(void);
104 uint32_t replay_get_dword(void);
105 int64_t replay_get_qword(void);
106 void replay_get_array(uint8_t *buf
, size_t *size
);
107 void replay_get_array_alloc(uint8_t **buf
, size_t *size
);
109 /* Mutex functions for protecting replay log file and ensuring
110 * synchronisation between vCPU and main-loop threads. */
112 void replay_mutex_init(void);
113 bool replay_mutex_locked(void);
115 /*! Checks error status of the file. */
116 void replay_check_error(void);
118 /*! Finishes processing of the replayed event and fetches
119 the next event from the log. */
120 void replay_finish_event(void);
121 /*! Reads data type from the file and stores it in the
122 data_kind variable. */
123 void replay_fetch_data_kind(void);
125 /*! Saves queued events (like instructions and sound). */
126 void replay_save_instructions(void);
128 /*! Skips async events until some sync event will be found.
129 \return true, if event was found */
130 bool replay_next_event_is(int event
);
132 /*! Reads next clock value from the file.
133 If clock kind read from the file is different from the parameter,
134 the value is not used. */
135 void replay_read_next_clock(unsigned int kind
);
137 /* Asynchronous events queue */
139 /*! Initializes events' processing internals */
140 void replay_init_events(void);
141 /*! Clears internal data structures for events handling */
142 void replay_finish_events(void);
143 /*! Flushes events queue */
144 void replay_flush_events(void);
145 /*! Clears events list before loading new VM state */
146 void replay_clear_events(void);
147 /*! Returns true if there are any unsaved events in the queue */
148 bool replay_has_events(void);
149 /*! Saves events from queue into the file */
150 void replay_save_events(int checkpoint
);
151 /*! Read events from the file into the input queue */
152 void replay_read_events(int checkpoint
);
153 /*! Adds specified async event to the queue */
154 void replay_add_event(ReplayAsyncEventKind event_kind
, void *opaque
,
155 void *opaque2
, uint64_t id
);
159 /*! Saves input event to the log */
160 void replay_save_input_event(InputEvent
*evt
);
161 /*! Reads input event from the log */
162 InputEvent
*replay_read_input_event(void);
163 /*! Adds input event to the queue */
164 void replay_add_input_event(struct InputEvent
*event
);
165 /*! Adds input sync event to the queue */
166 void replay_add_input_sync_event(void);
168 /* Character devices */
170 /*! Called to run char device read event. */
171 void replay_event_char_read_run(void *opaque
);
172 /*! Writes char read event to the file. */
173 void replay_event_char_read_save(void *opaque
);
174 /*! Reads char event read from the file. */
175 void *replay_event_char_read_load(void);
177 /* Network devices */
179 /*! Called to run network event. */
180 void replay_event_net_run(void *opaque
);
181 /*! Writes network event to the file. */
182 void replay_event_net_save(void *opaque
);
183 /*! Reads network from the file. */
184 void *replay_event_net_load(void);
186 /* VMState-related functions */
188 /* Registers replay VMState.
189 Should be called before virtual devices initialization
190 to make cached timers available for post_load functions. */
191 void replay_vmstate_register(void);