xref: /qemu/replay/replay-internal.h (revision 29b62a10)
1 #ifndef REPLAY_INTERNAL_H
2 #define REPLAY_INTERNAL_H
3 
4 /*
5  * replay-internal.h
6  *
7  * Copyright (c) 2010-2015 Institute for System Programming
8  *                         of the Russian Academy of Sciences.
9  *
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.
12  *
13  */
14 
15 /* Asynchronous events IDs */
16 
17 typedef enum ReplayAsyncEventKind {
18     REPLAY_ASYNC_EVENT_BH,
19     REPLAY_ASYNC_EVENT_BH_ONESHOT,
20     REPLAY_ASYNC_EVENT_INPUT,
21     REPLAY_ASYNC_EVENT_INPUT_SYNC,
22     REPLAY_ASYNC_EVENT_CHAR_READ,
23     REPLAY_ASYNC_EVENT_BLOCK,
24     REPLAY_ASYNC_EVENT_NET,
25     REPLAY_ASYNC_COUNT
26 } ReplayAsyncEventKind;
27 
28 /* Any changes to order/number of events will need to bump REPLAY_VERSION */
29 enum ReplayEvents {
30     /* for instruction event */
31     EVENT_INSTRUCTION,
32     /* for software interrupt */
33     EVENT_INTERRUPT,
34     /* for emulated exceptions */
35     EVENT_EXCEPTION,
36     /* for async events */
37     EVENT_ASYNC,
38     EVENT_ASYNC_LAST = EVENT_ASYNC + REPLAY_ASYNC_COUNT - 1,
39     /* for shutdown requests, range allows recovery of ShutdownCause */
40     EVENT_SHUTDOWN,
41     EVENT_SHUTDOWN_LAST = EVENT_SHUTDOWN + SHUTDOWN_CAUSE__MAX,
42     /* for character device write event */
43     EVENT_CHAR_WRITE,
44     /* for character device read all event */
45     EVENT_CHAR_READ_ALL,
46     EVENT_CHAR_READ_ALL_ERROR,
47     /* for audio out event */
48     EVENT_AUDIO_OUT,
49     /* for audio in event */
50     EVENT_AUDIO_IN,
51     /* for random number generator */
52     EVENT_RANDOM,
53     /* for clock read/writes */
54     /* some of greater codes are reserved for clocks */
55     EVENT_CLOCK,
56     EVENT_CLOCK_LAST = EVENT_CLOCK + REPLAY_CLOCK_COUNT - 1,
57     /* for checkpoint event */
58     /* some of greater codes are reserved for checkpoints */
59     EVENT_CHECKPOINT,
60     EVENT_CHECKPOINT_LAST = EVENT_CHECKPOINT + CHECKPOINT_COUNT - 1,
61     /* end of log event */
62     EVENT_END,
63     EVENT_COUNT
64 };
65 
66 typedef struct ReplayState {
67     /*! Cached clock values. */
68     int64_t cached_clock[REPLAY_CLOCK_COUNT];
69     /*! Current icount - number of processed instructions. */
70     uint64_t current_icount;
71     /*! Number of instructions to be executed before other events happen. */
72     int instruction_count;
73     /*! Type of the currently executed event. */
74     unsigned int data_kind;
75     /*! Flag which indicates that event is not processed yet. */
76     unsigned int has_unread_data;
77     /*! Temporary variable for saving current log offset. */
78     uint64_t file_offset;
79     /*! Next block operation id.
80         This counter is global, because requests from different
81         block devices should not get overlapping ids. */
82     uint64_t block_request_id;
83     /*! Prior value of the host clock */
84     uint64_t host_clock_last;
85     /*! Asynchronous event id read from the log */
86     uint64_t read_event_id;
87 } ReplayState;
88 extern ReplayState replay_state;
89 
90 /* File for replay writing */
91 extern FILE *replay_file;
92 /* Instruction count of the replay breakpoint */
93 extern uint64_t replay_break_icount;
94 /* Timer for the replay breakpoint callback */
95 extern QEMUTimer *replay_break_timer;
96 
97 void replay_put_byte(uint8_t byte);
98 void replay_put_event(uint8_t event);
99 void replay_put_word(uint16_t word);
100 void replay_put_dword(uint32_t dword);
101 void replay_put_qword(int64_t qword);
102 void replay_put_array(const uint8_t *buf, size_t size);
103 
104 uint8_t replay_get_byte(void);
105 uint16_t replay_get_word(void);
106 uint32_t replay_get_dword(void);
107 int64_t replay_get_qword(void);
108 void replay_get_array(uint8_t *buf, size_t *size);
109 void replay_get_array_alloc(uint8_t **buf, size_t *size);
110 
111 /* Mutex functions for protecting replay log file and ensuring
112  * synchronisation between vCPU and main-loop threads. */
113 
114 void replay_mutex_init(void);
115 bool replay_mutex_locked(void);
116 
117 /*! Checks error status of the file. */
118 void replay_check_error(void);
119 
120 /*! Finishes processing of the replayed event and fetches
121     the next event from the log. */
122 void replay_finish_event(void);
123 /*! Reads data type from the file and stores it in the
124     data_kind variable. */
125 void replay_fetch_data_kind(void);
126 
127 /*! Advance replay_state.current_icount to the specified value. */
128 void replay_advance_current_icount(uint64_t current_icount);
129 /*! Saves queued events (like instructions and sound). */
130 void replay_save_instructions(void);
131 
132 /*! Skips async events until some sync event will be found.
133     \return true, if event was found */
134 bool replay_next_event_is(int event);
135 
136 /*! Reads next clock value from the file.
137     If clock kind read from the file is different from the parameter,
138     the value is not used. */
139 void replay_read_next_clock(ReplayClockKind kind);
140 
141 /* Asynchronous events queue */
142 
143 /*! Initializes events' processing internals */
144 void replay_init_events(void);
145 /*! Clears internal data structures for events handling */
146 void replay_finish_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(void);
151 /*! Read events from the file into the input queue */
152 void replay_read_events(void);
153 /*! Adds specified async event to the queue */
154 void replay_add_event(ReplayAsyncEventKind event_kind, void *opaque,
155                       void *opaque2, uint64_t id);
156 
157 /* Input events */
158 
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);
167 
168 /* Character devices */
169 
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);
176 
177 /* Network devices */
178 
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);
185 
186 /* VMState-related functions */
187 
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);
192 
193 #endif
194