1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <setjmp.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/ipc.h>
#include <minix/com.h>
#include <minix/syslib.h>
#include <machine/stackframe.h>
#include "vassert.h"
VAssert_StateWrapper vassert_state ALIGNED(VASSERT_PAGE_SIZE);
#define TRUE 1
#define FALSE 0
#define MAGIC_CMD 0x564d5868
#define MAGIC_PORT 0x5658
#define HIGH_BAND_PORT 0x5659
#define BACKDOOR_PORT 51
#define BACKDOOR_HB_PORT 1
#define CMD_SET_ADDRESS BACKDOOR_PORT|(1<<16)
#define CMD_RETURN_REPLAY BACKDOOR_PORT|(2<<16)
#define CMD_GO_LIVE BACKDOOR_PORT|(3<<16)
#define CMD_LOG BACKDOOR_HB_PORT|(4<<16)
#define CMD_SET_RECORD 47
#define LOG_MAX 512
typedef char Bool;
typedef unsigned int uint32;
typedef unsigned long long uint64;
#ifdef VM_X86_64
typedef uint64 VA;
#else
typedef uint32 VA;
#endif
static sigjmp_buf segv_jmp;
void libvassert_process_backdoor(uint32, uint32, uint32, reg_t *, reg_t *,
reg_t *, reg_t *);
/*
*---------------------------------------------------------------------
*
* sig_segv --
*
* Customed SEGV signal handler for VAssert_IsInVM.
*
* Results:
*
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------
*/
static void __dead sig_segv(int sig_no)
{
/* jumping to error handling in VAssert_IsInVM. */
siglongjmp(segv_jmp, 1);
}
/*
*---------------------------------------------------------------------
*
* VAssert_IsInVM --
*
* Check if we are in virtual world.
*
* Results:
*
* Return TRUE on success, or FALSE on failure.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------
*/
static Bool VAssert_IsInVM(void)
{
uint32 eax, ebx, ecx, edx;
static Bool inVM = FALSE;
static Bool tested = FALSE;
if (!tested) {
/* Error handling. */
if (sigsetjmp(segv_jmp, 0) != 0) {
signal(SIGSEGV, SIG_DFL);
inVM = FALSE;
return inVM;
}
tested = TRUE;
/* Install custom handler. */
signal(SIGSEGV, sig_segv);
/* Test if we are in a VM. */
libvassert_process_backdoor(0x0a, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
signal(SIGSEGV, SIG_DFL);
inVM = TRUE;
}
return inVM;
}
/*
*---------------------------------------------------------------------
*
* VAssert_Init --
*
* Tell vmx that vassert is inited.
*
* Results:
*
* Return 0 on success, or -1 on failure.
*
* Side effects:
* None
*
*---------------------------------------------------------------------
*/
char VAssert_Init(void)
{
uint32 eax, ebx, ecx, edx;
VA page_address = (VA) &vassert_state.inReplay;
if (!VAssert_IsInVM()) {
return -1;
}
bzero((char*) &vassert_state, sizeof vassert_state);
#ifndef __minix
/* Lock the page. */
if (mlock(&vassert_state, sizeof vassert_state)) {
return -1;
}
#endif
libvassert_process_backdoor(CMD_SET_ADDRESS, page_address,
MAGIC_PORT|(1<<16), &eax, &ebx, &ecx, &edx);
return (eax != (uint32)-1) ? 0 : -1;
}
/*
*---------------------------------------------------------------------
*
* VAssert_Uninit --
*
* Tell vmx that vassert is finalized.
*
* Results:
*
* Return 0 on success, or -1 on failure.
*
* Side effects:
* None
*
*---------------------------------------------------------------------
*/
char VAssert_Uninit(void)
{
unsigned int eax, ebx, ecx, edx;
if (!VAssert_IsInVM()) {
return -1;
}
libvassert_process_backdoor(CMD_SET_ADDRESS, 0, MAGIC_PORT|(0<<16), &eax, &ebx, &ecx, &edx);
return (eax != (unsigned int)-1) ? 0 : 1;
}
/*
*---------------------------------------------------------------------
*
* VAssert_LogMain --
*
* Print message to a text file on host side.
*
* Results:
* None
*
* Side effects:
* Write to a text file with fixed name.
* If the file exists, host UI will ask for append/replace/ignore
*
*---------------------------------------------------------------------
*/
void VAssert_LogMain(const char *format, ...)
{
unsigned int eax, ebx, ecx, edx;
char buf[LOG_MAX];
unsigned int len = 0;
va_list ap;
va_start(ap, format);
len = vsnprintf(buf, LOG_MAX, format, ap);
va_end(ap);
__asm__ __volatile__("cld; rep outsb;"
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "0"(MAGIC_CMD), "1"(CMD_LOG), "2"(len), "d"(HIGH_BAND_PORT), "S"(buf)
: "memory"
);
}
/*
*---------------------------------------------------------------------
*
* VAssert_GoLiveMain --
*
* Make the vm which is in replay exit replay.
*
* Results:
* None
*
* Side effects:
* Replay is stopped.
*
*---------------------------------------------------------------------
*/
void VAssert_GoLiveMain(void)
{
unsigned int eax, ebx, ecx, edx;
vassert_state.inReplay = 0;
libvassert_process_backdoor(CMD_GO_LIVE, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
}
/*
*---------------------------------------------------------------------
*
* VAssert_ReturnToReplayMain --
*
* Called after the custom work is done, and replay is to continue.
*
* Results:
* None
*
* Side effects:
* Replay is continued from pause.
*
*---------------------------------------------------------------------
*/
void VAssert_ReturnToReplayMain(void)
{
unsigned int eax, ebx, ecx, edx;
libvassert_process_backdoor(CMD_RETURN_REPLAY, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
}
/*
*---------------------------------------------------------------------
*
* VAssert_SetRecordingMain --
*
* Ask vmx for starting or stopping recording.
*
* Results:
*
* Return TRUE on success, or FALSE on failure.
*
* Side effects:
* Recording is started or stopped.
*
*---------------------------------------------------------------------
*/
char VAssert_SetRecordingMain(char start)
{
uint32 eax, ebx, ecx, edx;
if (!VAssert_IsInVM()) {
return FALSE;
}
libvassert_process_backdoor(CMD_SET_RECORD, start ? 1 : 2, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
return (eax == 1) ? TRUE : FALSE;
}
|