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.
17 /* for instruction event */
19 /* for software interrupt */
21 /* for emulated exceptions */
23 /* for async events */
25 /* for shutdown request */
27 /* for character device write event */
29 /* for character device read all event */
31 EVENT_CHAR_READ_ALL_ERROR
,
32 /* for clock read/writes */
33 /* some of greater codes are reserved for clocks */
35 EVENT_CLOCK_LAST
= EVENT_CLOCK
+ REPLAY_CLOCK_COUNT
- 1,
36 /* for checkpoint event */
37 /* some of greater codes are reserved for checkpoints */
39 EVENT_CHECKPOINT_LAST
= EVENT_CHECKPOINT
+ CHECKPOINT_COUNT
- 1,
40 /* end of log event */
45 /* Asynchronous events IDs */
47 enum ReplayAsyncEventKind
{
48 REPLAY_ASYNC_EVENT_BH
,
49 REPLAY_ASYNC_EVENT_INPUT
,
50 REPLAY_ASYNC_EVENT_INPUT_SYNC
,
51 REPLAY_ASYNC_EVENT_CHAR_READ
,
52 REPLAY_ASYNC_EVENT_BLOCK
,
56 typedef enum ReplayAsyncEventKind ReplayAsyncEventKind
;
58 typedef struct ReplayState
{
59 /*! Cached clock values. */
60 int64_t cached_clock
[REPLAY_CLOCK_COUNT
];
61 /*! Current step - number of processed instructions and timer events. */
62 uint64_t current_step
;
63 /*! Number of instructions to be executed before other events happen. */
64 int instructions_count
;
65 /*! Type of the currently executed event. */
66 unsigned int data_kind
;
67 /*! Flag which indicates that event is not processed yet. */
68 unsigned int has_unread_data
;
69 /*! Temporary variable for saving current log offset. */
71 /*! Next block operation id.
72 This counter is global, because requests from different
73 block devices should not get overlapping ids. */
74 uint64_t block_request_id
;
76 extern ReplayState replay_state
;
78 /* File for replay writing */
79 extern FILE *replay_file
;
81 void replay_put_byte(uint8_t byte
);
82 void replay_put_event(uint8_t event
);
83 void replay_put_word(uint16_t word
);
84 void replay_put_dword(uint32_t dword
);
85 void replay_put_qword(int64_t qword
);
86 void replay_put_array(const uint8_t *buf
, size_t size
);
88 uint8_t replay_get_byte(void);
89 uint16_t replay_get_word(void);
90 uint32_t replay_get_dword(void);
91 int64_t replay_get_qword(void);
92 void replay_get_array(uint8_t *buf
, size_t *size
);
93 void replay_get_array_alloc(uint8_t **buf
, size_t *size
);
95 /* Mutex functions for protecting replay log file */
97 void replay_mutex_init(void);
98 void replay_mutex_destroy(void);
99 void replay_mutex_lock(void);
100 void replay_mutex_unlock(void);
102 /*! Checks error status of the file. */
103 void replay_check_error(void);
105 /*! Finishes processing of the replayed event and fetches
106 the next event from the log. */
107 void replay_finish_event(void);
108 /*! Reads data type from the file and stores it in the
109 data_kind variable. */
110 void replay_fetch_data_kind(void);
112 /*! Saves queued events (like instructions and sound). */
113 void replay_save_instructions(void);
115 /*! Skips async events until some sync event will be found.
116 \return true, if event was found */
117 bool replay_next_event_is(int event
);
119 /*! Reads next clock value from the file.
120 If clock kind read from the file is different from the parameter,
121 the value is not used. */
122 void replay_read_next_clock(unsigned int kind
);
124 /* Asynchronous events queue */
126 /*! Initializes events' processing internals */
127 void replay_init_events(void);
128 /*! Clears internal data structures for events handling */
129 void replay_finish_events(void);
130 /*! Flushes events queue */
131 void replay_flush_events(void);
132 /*! Clears events list before loading new VM state */
133 void replay_clear_events(void);
134 /*! Returns true if there are any unsaved events in the queue */
135 bool replay_has_events(void);
136 /*! Saves events from queue into the file */
137 void replay_save_events(int checkpoint
);
138 /*! Read events from the file into the input queue */
139 void replay_read_events(int checkpoint
);
140 /*! Adds specified async event to the queue */
141 void replay_add_event(ReplayAsyncEventKind event_kind
, void *opaque
,
142 void *opaque2
, uint64_t id
);
146 /*! Saves input event to the log */
147 void replay_save_input_event(InputEvent
*evt
);
148 /*! Reads input event from the log */
149 InputEvent
*replay_read_input_event(void);
150 /*! Adds input event to the queue */
151 void replay_add_input_event(struct InputEvent
*event
);
152 /*! Adds input sync event to the queue */
153 void replay_add_input_sync_event(void);
155 /* Character devices */
157 /*! Called to run char device read event. */
158 void replay_event_char_read_run(void *opaque
);
159 /*! Writes char read event to the file. */
160 void replay_event_char_read_save(void *opaque
);
161 /*! Reads char event read from the file. */
162 void *replay_event_char_read_load(void);
164 /* VMState-related functions */
166 /* Registers replay VMState.
167 Should be called before virtual devices initialization
168 to make cached timers available for post_load functions. */
169 void replay_vmstate_register(void);