1 | /* $Id: tstSSM.cpp 18456 2009-03-28 04:47:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Saved State Manager Testcase.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #include <VBox/ssm.h>
|
---|
27 | #include "../VMInternal.h" /* createFakeVM */
|
---|
28 | #include <VBox/vm.h>
|
---|
29 | #include <VBox/uvm.h>
|
---|
30 | #include <VBox/mm.h>
|
---|
31 | #include <VBox/stam.h>
|
---|
32 |
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/sup.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/param.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/initterm.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/stream.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/time.h>
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include <iprt/path.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | const uint8_t gabPage[PAGE_SIZE] = {0};
|
---|
48 |
|
---|
49 | const char gachMem1[] = "sdfg\1asdfa\177hjkl;sdfghjkl;dfghjkl;dfghjkl;\0\0asdf;kjasdf;lkjasd;flkjasd;lfkjasd\0;lfk";
|
---|
50 |
|
---|
51 | uint8_t gabBigMem[8*1024*1024];
|
---|
52 |
|
---|
53 | /** initializes gabBigMem with some non zero stuff. */
|
---|
54 | void initBigMem(void)
|
---|
55 | {
|
---|
56 | #if 0
|
---|
57 | uint32_t *puch = (uint32_t *)&gabBigMem[0];
|
---|
58 | uint32_t *puchEnd = (uint32_t *)&gabBigMem[sizeof(gabBigMem)];
|
---|
59 | uint32_t u32 = 0xdeadbeef;
|
---|
60 | for (; puch < puchEnd; puch++)
|
---|
61 | {
|
---|
62 | *puch = u32;
|
---|
63 | u32 += 19;
|
---|
64 | u32 = (u32 << 1) | (u32 >> 31);
|
---|
65 | }
|
---|
66 | #else
|
---|
67 | uint8_t *pb = &gabBigMem[0];
|
---|
68 | uint8_t *pbEnd = &gabBigMem[sizeof(gabBigMem)];
|
---|
69 | for (; pb < pbEnd; pb += 16)
|
---|
70 | {
|
---|
71 | char szTmp[17];
|
---|
72 | RTStrPrintf(szTmp, sizeof(szTmp), "aaaa%08Xzzzz", (uint32_t)(uintptr_t)pb);
|
---|
73 | memcpy(pb, szTmp, 16);
|
---|
74 | }
|
---|
75 | #endif
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Execute state save operation.
|
---|
80 | *
|
---|
81 | * @returns VBox status code.
|
---|
82 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
83 | * @param pSSM SSM operation handle.
|
---|
84 | */
|
---|
85 | DECLCALLBACK(int) Item01Save(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
86 | {
|
---|
87 | uint64_t u64Start = RTTimeNanoTS();
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Test writing some memory block.
|
---|
91 | */
|
---|
92 | int rc = SSMR3PutMem(pSSM, gachMem1, sizeof(gachMem1));
|
---|
93 | if (RT_FAILURE(rc))
|
---|
94 | {
|
---|
95 | RTPrintf("Item01: #1 - SSMR3PutMem -> %Rrc\n", rc);
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Test writing a zeroterminated string.
|
---|
101 | */
|
---|
102 | rc = SSMR3PutStrZ(pSSM, "String");
|
---|
103 | if (RT_FAILURE(rc))
|
---|
104 | {
|
---|
105 | RTPrintf("Item01: #1 - SSMR3PutMem -> %Rrc\n", rc);
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Test the individual integer put functions to see that they all work.
|
---|
112 | * (Testcases are also known as "The Land of The Ugly Code"...)
|
---|
113 | */
|
---|
114 | #define ITEM(suff,bits, val) \
|
---|
115 | rc = SSMR3Put##suff(pSSM, val); \
|
---|
116 | if (RT_FAILURE(rc)) \
|
---|
117 | { \
|
---|
118 | RTPrintf("Item01: #" #suff " - SSMR3Put" #suff "(," #val ") -> %Rrc\n", rc); \
|
---|
119 | return rc; \
|
---|
120 | }
|
---|
121 | /* copy & past with the load one! */
|
---|
122 | ITEM(U8, uint8_t, 0xff);
|
---|
123 | ITEM(U8, uint8_t, 0x0);
|
---|
124 | ITEM(U8, uint8_t, 1);
|
---|
125 | ITEM(U8, uint8_t, 42);
|
---|
126 | ITEM(U8, uint8_t, 230);
|
---|
127 | ITEM(S8, int8_t, -128);
|
---|
128 | ITEM(S8, int8_t, 127);
|
---|
129 | ITEM(S8, int8_t, 12);
|
---|
130 | ITEM(S8, int8_t, -76);
|
---|
131 | ITEM(U16, uint16_t, 0xffff);
|
---|
132 | ITEM(U16, uint16_t, 0x0);
|
---|
133 | ITEM(S16, int16_t, 32767);
|
---|
134 | ITEM(S16, int16_t, -32768);
|
---|
135 | ITEM(U32, uint32_t, 4294967295U);
|
---|
136 | ITEM(U32, uint32_t, 0);
|
---|
137 | ITEM(U32, uint32_t, 42);
|
---|
138 | ITEM(U32, uint32_t, 2342342344U);
|
---|
139 | ITEM(S32, int32_t, -2147483647-1);
|
---|
140 | ITEM(S32, int32_t, 2147483647);
|
---|
141 | ITEM(S32, int32_t, 42);
|
---|
142 | ITEM(S32, int32_t, 568459834);
|
---|
143 | ITEM(S32, int32_t, -58758999);
|
---|
144 | ITEM(U64, uint64_t, 18446744073709551615ULL);
|
---|
145 | ITEM(U64, uint64_t, 0);
|
---|
146 | ITEM(U64, uint64_t, 42);
|
---|
147 | ITEM(U64, uint64_t, 593023944758394234ULL);
|
---|
148 | ITEM(S64, int64_t, 9223372036854775807LL);
|
---|
149 | ITEM(S64, int64_t, -9223372036854775807LL - 1);
|
---|
150 | ITEM(S64, int64_t, 42);
|
---|
151 | ITEM(S64, int64_t, 21398723459873LL);
|
---|
152 | ITEM(S64, int64_t, -5848594593453453245LL);
|
---|
153 | #undef ITEM
|
---|
154 |
|
---|
155 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
156 | RTPrintf("tstSSM: Saved 1st item in %RI64 ns\n", u64Elapsed);
|
---|
157 | return 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Prepare state load operation.
|
---|
162 | *
|
---|
163 | * @returns VBox status code.
|
---|
164 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
165 | * @param pSSM SSM operation handle.
|
---|
166 | */
|
---|
167 | DECLCALLBACK(int) Item01Load(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
168 | {
|
---|
169 | /*
|
---|
170 | * Load the memory block.
|
---|
171 | */
|
---|
172 | char achTmp[sizeof(gachMem1)];
|
---|
173 | int rc = SSMR3GetMem(pSSM, achTmp, sizeof(gachMem1));
|
---|
174 | if (RT_FAILURE(rc))
|
---|
175 | {
|
---|
176 | RTPrintf("Item01: #1 - SSMR3GetMem -> %Rrc\n", rc);
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Load the string.
|
---|
182 | */
|
---|
183 | rc = SSMR3GetStrZ(pSSM, achTmp, sizeof(achTmp));
|
---|
184 | if (RT_FAILURE(rc))
|
---|
185 | {
|
---|
186 | RTPrintf("Item01: #2 - SSMR3GetStrZ -> %Rrc\n", rc);
|
---|
187 | return rc;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Test the individual integer put functions to see that they all work.
|
---|
192 | * (Testcases are also known as "The Land of The Ugly Code"...)
|
---|
193 | */
|
---|
194 | #define ITEM(suff, type, val) \
|
---|
195 | do { \
|
---|
196 | type var = {0}; \
|
---|
197 | rc = SSMR3Get##suff(pSSM, &var); \
|
---|
198 | if (RT_FAILURE(rc)) \
|
---|
199 | { \
|
---|
200 | RTPrintf("Item01: #" #suff " - SSMR3Get" #suff "(," #val ") -> %Rrc\n", rc); \
|
---|
201 | return rc; \
|
---|
202 | } \
|
---|
203 | if (var != val) \
|
---|
204 | { \
|
---|
205 | RTPrintf("Item01: #" #suff " - SSMR3Get" #suff "(," #val ") -> %d returned wrong value!\n", rc); \
|
---|
206 | return VERR_GENERAL_FAILURE; \
|
---|
207 | } \
|
---|
208 | } while (0)
|
---|
209 | /* copy & past with the load one! */
|
---|
210 | ITEM(U8, uint8_t, 0xff);
|
---|
211 | ITEM(U8, uint8_t, 0x0);
|
---|
212 | ITEM(U8, uint8_t, 1);
|
---|
213 | ITEM(U8, uint8_t, 42);
|
---|
214 | ITEM(U8, uint8_t, 230);
|
---|
215 | ITEM(S8, int8_t, -128);
|
---|
216 | ITEM(S8, int8_t, 127);
|
---|
217 | ITEM(S8, int8_t, 12);
|
---|
218 | ITEM(S8, int8_t, -76);
|
---|
219 | ITEM(U16, uint16_t, 0xffff);
|
---|
220 | ITEM(U16, uint16_t, 0x0);
|
---|
221 | ITEM(S16, int16_t, 32767);
|
---|
222 | ITEM(S16, int16_t, -32768);
|
---|
223 | ITEM(U32, uint32_t, 4294967295U);
|
---|
224 | ITEM(U32, uint32_t, 0);
|
---|
225 | ITEM(U32, uint32_t, 42);
|
---|
226 | ITEM(U32, uint32_t, 2342342344U);
|
---|
227 | ITEM(S32, int32_t, -2147483647-1);
|
---|
228 | ITEM(S32, int32_t, 2147483647);
|
---|
229 | ITEM(S32, int32_t, 42);
|
---|
230 | ITEM(S32, int32_t, 568459834);
|
---|
231 | ITEM(S32, int32_t, -58758999);
|
---|
232 | ITEM(U64, uint64_t, 18446744073709551615ULL);
|
---|
233 | ITEM(U64, uint64_t, 0);
|
---|
234 | ITEM(U64, uint64_t, 42);
|
---|
235 | ITEM(U64, uint64_t, 593023944758394234ULL);
|
---|
236 | ITEM(S64, int64_t, 9223372036854775807LL);
|
---|
237 | ITEM(S64, int64_t, -9223372036854775807LL - 1);
|
---|
238 | ITEM(S64, int64_t, 42);
|
---|
239 | ITEM(S64, int64_t, 21398723459873LL);
|
---|
240 | ITEM(S64, int64_t, -5848594593453453245LL);
|
---|
241 | #undef ITEM
|
---|
242 |
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Execute state save operation.
|
---|
249 | *
|
---|
250 | * @returns VBox status code.
|
---|
251 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
252 | * @param pSSM SSM operation handle.
|
---|
253 | */
|
---|
254 | DECLCALLBACK(int) Item02Save(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
255 | {
|
---|
256 | uint64_t u64Start = RTTimeNanoTS();
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Put the size.
|
---|
260 | */
|
---|
261 | uint32_t cb = sizeof(gabBigMem);
|
---|
262 | int rc = SSMR3PutU32(pSSM, cb);
|
---|
263 | if (RT_FAILURE(rc))
|
---|
264 | {
|
---|
265 | RTPrintf("Item02: PutU32 -> %Rrc\n", rc);
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 |
|
---|
269 | /*
|
---|
270 | * Put 8MB of memory to the file in 3 chunks.
|
---|
271 | */
|
---|
272 | uint8_t *pbMem = &gabBigMem[0];
|
---|
273 | uint32_t cbChunk = cb / 47;
|
---|
274 | rc = SSMR3PutMem(pSSM, pbMem, cbChunk);
|
---|
275 | if (RT_FAILURE(rc))
|
---|
276 | {
|
---|
277 | RTPrintf("Item02: PutMem(,%p,%#x) -> %Rrc\n", pbMem, cbChunk, rc);
|
---|
278 | return rc;
|
---|
279 | }
|
---|
280 | cb -= cbChunk;
|
---|
281 | pbMem += cbChunk;
|
---|
282 |
|
---|
283 | /* next piece. */
|
---|
284 | cbChunk *= 19;
|
---|
285 | rc = SSMR3PutMem(pSSM, pbMem, cbChunk);
|
---|
286 | if (RT_FAILURE(rc))
|
---|
287 | {
|
---|
288 | RTPrintf("Item02: PutMem(,%p,%#x) -> %Rrc\n", pbMem, cbChunk, rc);
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 | cb -= cbChunk;
|
---|
292 | pbMem += cbChunk;
|
---|
293 |
|
---|
294 | /* last piece. */
|
---|
295 | cbChunk = cb;
|
---|
296 | rc = SSMR3PutMem(pSSM, pbMem, cbChunk);
|
---|
297 | if (RT_FAILURE(rc))
|
---|
298 | {
|
---|
299 | RTPrintf("Item02: PutMem(,%p,%#x) -> %Rrc\n", pbMem, cbChunk, rc);
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
304 | RTPrintf("tstSSM: Saved 2nd item in %RI64 ns\n", u64Elapsed);
|
---|
305 | return 0;
|
---|
306 | }
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Prepare state load operation.
|
---|
310 | *
|
---|
311 | * @returns VBox status code.
|
---|
312 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
313 | * @param pSSM SSM operation handle.
|
---|
314 | */
|
---|
315 | DECLCALLBACK(int) Item02Load(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
316 | {
|
---|
317 | /*
|
---|
318 | * Load the size.
|
---|
319 | */
|
---|
320 | uint32_t cb;
|
---|
321 | int rc = SSMR3GetU32(pSSM, &cb);
|
---|
322 | if (RT_FAILURE(rc))
|
---|
323 | {
|
---|
324 | RTPrintf("Item02: SSMR3GetU32 -> %Rrc\n", rc);
|
---|
325 | return rc;
|
---|
326 | }
|
---|
327 | if (cb != sizeof(gabBigMem))
|
---|
328 | {
|
---|
329 | RTPrintf("Item02: loaded size doesn't match the real thing. %#x != %#x\n", cb, sizeof(gabBigMem));
|
---|
330 | return VERR_GENERAL_FAILURE;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /*
|
---|
334 | * Load the memory chunk by chunk.
|
---|
335 | */
|
---|
336 | uint8_t *pbMem = &gabBigMem[0];
|
---|
337 | char achTmp[16383];
|
---|
338 | uint32_t cbChunk = sizeof(achTmp);
|
---|
339 | while (cb > 0)
|
---|
340 | {
|
---|
341 | cbChunk -= 7;
|
---|
342 | if (cbChunk < 64)
|
---|
343 | cbChunk = sizeof(achTmp) - (cbChunk % 47);
|
---|
344 | if (cbChunk > cb)
|
---|
345 | cbChunk = cb;
|
---|
346 | rc = SSMR3GetMem(pSSM, &achTmp[0], cbChunk);
|
---|
347 | if (RT_FAILURE(rc))
|
---|
348 | {
|
---|
349 | RTPrintf("Item02: SSMR3GetMem(,,%#x) -> %d offset %#x\n", cbChunk, rc, pbMem - &gabBigMem[0]);
|
---|
350 | return rc;
|
---|
351 | }
|
---|
352 | if (memcmp(achTmp, pbMem, cbChunk))
|
---|
353 | {
|
---|
354 | RTPrintf("Item02: compare failed. mem offset=%#x cbChunk=%#x\n", pbMem - &gabBigMem[0], cbChunk);
|
---|
355 | return VERR_GENERAL_FAILURE;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* next */
|
---|
359 | pbMem += cbChunk;
|
---|
360 | cb -= cbChunk;
|
---|
361 | }
|
---|
362 |
|
---|
363 | return 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Execute state save operation.
|
---|
369 | *
|
---|
370 | * @returns VBox status code.
|
---|
371 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
372 | * @param pSSM SSM operation handle.
|
---|
373 | */
|
---|
374 | DECLCALLBACK(int) Item03Save(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
375 | {
|
---|
376 | uint64_t u64Start = RTTimeNanoTS();
|
---|
377 |
|
---|
378 | /*
|
---|
379 | * Put the size.
|
---|
380 | */
|
---|
381 | uint32_t cb = 512*_1M;
|
---|
382 | int rc = SSMR3PutU32(pSSM, cb);
|
---|
383 | if (RT_FAILURE(rc))
|
---|
384 | {
|
---|
385 | RTPrintf("Item03: PutU32 -> %Rrc\n", rc);
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /*
|
---|
390 | * Put 512 MB page by page.
|
---|
391 | */
|
---|
392 | const uint8_t *pu8Org = &gabBigMem[0];
|
---|
393 | while (cb > 0)
|
---|
394 | {
|
---|
395 | rc = SSMR3PutMem(pSSM, pu8Org, PAGE_SIZE);
|
---|
396 | if (RT_FAILURE(rc))
|
---|
397 | {
|
---|
398 | RTPrintf("Item03: PutMem(,%p,%#x) -> %Rrc\n", pu8Org, PAGE_SIZE, rc);
|
---|
399 | return rc;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /* next */
|
---|
403 | cb -= PAGE_SIZE;
|
---|
404 | pu8Org += PAGE_SIZE;
|
---|
405 | if (pu8Org > &gabBigMem[sizeof(gabBigMem)])
|
---|
406 | pu8Org = &gabBigMem[0];
|
---|
407 | }
|
---|
408 |
|
---|
409 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
410 | RTPrintf("tstSSM: Saved 3rd item in %RI64 ns\n", u64Elapsed);
|
---|
411 | return 0;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Prepare state load operation.
|
---|
416 | *
|
---|
417 | * @returns VBox status code.
|
---|
418 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
419 | * @param pSSM SSM operation handle.
|
---|
420 | */
|
---|
421 | DECLCALLBACK(int) Item03Load(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
422 | {
|
---|
423 | /*
|
---|
424 | * Load the size.
|
---|
425 | */
|
---|
426 | uint32_t cb;
|
---|
427 | int rc = SSMR3GetU32(pSSM, &cb);
|
---|
428 | if (RT_FAILURE(rc))
|
---|
429 | {
|
---|
430 | RTPrintf("Item03: SSMR3GetU32 -> %Rrc\n", rc);
|
---|
431 | return rc;
|
---|
432 | }
|
---|
433 | if (cb != 512*_1M)
|
---|
434 | {
|
---|
435 | RTPrintf("Item03: loaded size doesn't match the real thing. %#x != %#x\n", cb, 512*_1M);
|
---|
436 | return VERR_GENERAL_FAILURE;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /*
|
---|
440 | * Load the memory page by page.
|
---|
441 | */
|
---|
442 | const uint8_t *pu8Org = &gabBigMem[0];
|
---|
443 | while (cb > 0)
|
---|
444 | {
|
---|
445 | char achPage[PAGE_SIZE];
|
---|
446 | rc = SSMR3GetMem(pSSM, &achPage[0], PAGE_SIZE);
|
---|
447 | if (RT_FAILURE(rc))
|
---|
448 | {
|
---|
449 | RTPrintf("Item03: SSMR3GetMem(,,%#x) -> %Rrc offset %#x\n", PAGE_SIZE, rc, 512*_1M - cb);
|
---|
450 | return rc;
|
---|
451 | }
|
---|
452 | if (memcmp(achPage, pu8Org, PAGE_SIZE))
|
---|
453 | {
|
---|
454 | RTPrintf("Item03: compare failed. mem offset=%#x\n", 512*_1M - cb);
|
---|
455 | return VERR_GENERAL_FAILURE;
|
---|
456 | }
|
---|
457 |
|
---|
458 | /* next */
|
---|
459 | cb -= PAGE_SIZE;
|
---|
460 | pu8Org += PAGE_SIZE;
|
---|
461 | if (pu8Org > &gabBigMem[sizeof(gabBigMem)])
|
---|
462 | pu8Org = &gabBigMem[0];
|
---|
463 | }
|
---|
464 |
|
---|
465 | return 0;
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 | /**
|
---|
470 | * Execute state save operation.
|
---|
471 | *
|
---|
472 | * @returns VBox status code.
|
---|
473 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
474 | * @param pSSM SSM operation handle.
|
---|
475 | */
|
---|
476 | DECLCALLBACK(int) Item04Save(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
477 | {
|
---|
478 | uint64_t u64Start = RTTimeNanoTS();
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Put the size.
|
---|
482 | */
|
---|
483 | uint32_t cb = 512*_1M;
|
---|
484 | int rc = SSMR3PutU32(pSSM, cb);
|
---|
485 | if (RT_FAILURE(rc))
|
---|
486 | {
|
---|
487 | RTPrintf("Item04: PutU32 -> %Rrc\n", rc);
|
---|
488 | return rc;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * Put 512 MB page by page.
|
---|
493 | */
|
---|
494 | while (cb > 0)
|
---|
495 | {
|
---|
496 | rc = SSMR3PutMem(pSSM, gabPage, PAGE_SIZE);
|
---|
497 | if (RT_FAILURE(rc))
|
---|
498 | {
|
---|
499 | RTPrintf("Item04: PutMem(,%p,%#x) -> %Rrc\n", gabPage, PAGE_SIZE, rc);
|
---|
500 | return rc;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /* next */
|
---|
504 | cb -= PAGE_SIZE;
|
---|
505 | }
|
---|
506 |
|
---|
507 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
508 | RTPrintf("tstSSM: Saved 4th item in %RI64 ns\n", u64Elapsed);
|
---|
509 | return 0;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Prepare state load operation.
|
---|
514 | *
|
---|
515 | * @returns VBox status code.
|
---|
516 | * @param pDevIns Device instance of the device which registered the data unit.
|
---|
517 | * @param pSSM SSM operation handle.
|
---|
518 | */
|
---|
519 | DECLCALLBACK(int) Item04Load(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
520 | {
|
---|
521 | /*
|
---|
522 | * Load the size.
|
---|
523 | */
|
---|
524 | uint32_t cb;
|
---|
525 | int rc = SSMR3GetU32(pSSM, &cb);
|
---|
526 | if (RT_FAILURE(rc))
|
---|
527 | {
|
---|
528 | RTPrintf("Item04: SSMR3GetU32 -> %Rrc\n", rc);
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 | if (cb != 512*_1M)
|
---|
532 | {
|
---|
533 | RTPrintf("Item04: loaded size doesn't match the real thing. %#x != %#x\n", cb, 512*_1M);
|
---|
534 | return VERR_GENERAL_FAILURE;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * Load the memory page by page.
|
---|
539 | */
|
---|
540 | while (cb > 0)
|
---|
541 | {
|
---|
542 | char achPage[PAGE_SIZE];
|
---|
543 | rc = SSMR3GetMem(pSSM, &achPage[0], PAGE_SIZE);
|
---|
544 | if (RT_FAILURE(rc))
|
---|
545 | {
|
---|
546 | RTPrintf("Item04: SSMR3GetMem(,,%#x) -> %Rrc offset %#x\n", PAGE_SIZE, rc, 512*_1M - cb);
|
---|
547 | return rc;
|
---|
548 | }
|
---|
549 | if (memcmp(achPage, gabPage, PAGE_SIZE))
|
---|
550 | {
|
---|
551 | RTPrintf("Item04: compare failed. mem offset=%#x\n", 512*_1M - cb);
|
---|
552 | return VERR_GENERAL_FAILURE;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /* next */
|
---|
556 | cb -= PAGE_SIZE;
|
---|
557 | }
|
---|
558 |
|
---|
559 | return 0;
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Creates a mockup VM structure for testing SSM.
|
---|
565 | *
|
---|
566 | * @returns 0 on success, 1 on failure.
|
---|
567 | * @param ppVM Where to store the VM handle.
|
---|
568 | *
|
---|
569 | * @todo Move this to VMM/VM since it's stuff done by several testcases.
|
---|
570 | */
|
---|
571 | static int createFakeVM(PVM *ppVM)
|
---|
572 | {
|
---|
573 | /*
|
---|
574 | * Allocate and init the UVM structure.
|
---|
575 | */
|
---|
576 | PUVM pUVM = (PUVM)RTMemAllocZ(sizeof(*pUVM));
|
---|
577 | AssertReturn(pUVM, 1);
|
---|
578 | pUVM->u32Magic = UVM_MAGIC;
|
---|
579 | pUVM->vm.s.idxTLS = RTTlsAlloc();
|
---|
580 | int rc = RTTlsSet(pUVM->vm.s.idxTLS, &pUVM->aCpus[0]);
|
---|
581 | if (RT_SUCCESS(rc))
|
---|
582 | {
|
---|
583 | pUVM->aCpus[0].pUVM = pUVM;
|
---|
584 | pUVM->aCpus[0].vm.s.NativeThreadEMT = RTThreadNativeSelf();
|
---|
585 |
|
---|
586 | rc = STAMR3InitUVM(pUVM);
|
---|
587 | if (RT_SUCCESS(rc))
|
---|
588 | {
|
---|
589 | rc = MMR3InitUVM(pUVM);
|
---|
590 | if (RT_SUCCESS(rc))
|
---|
591 | {
|
---|
592 | /*
|
---|
593 | * Allocate and init the VM structure.
|
---|
594 | */
|
---|
595 | PVM pVM;
|
---|
596 | rc = SUPPageAlloc((sizeof(*pVM) + PAGE_SIZE - 1) >> PAGE_SHIFT, (void **)&pVM);
|
---|
597 | if (RT_SUCCESS(rc))
|
---|
598 | {
|
---|
599 | pVM->enmVMState = VMSTATE_CREATED;
|
---|
600 | pVM->pVMR3 = pVM;
|
---|
601 | pVM->pUVM = pUVM;
|
---|
602 | pVM->cCPUs = 1;
|
---|
603 | pVM->aCpus[0].pVMR3 = pVM;
|
---|
604 | pVM->aCpus[0].hNativeThread = RTThreadNativeSelf();
|
---|
605 |
|
---|
606 | pUVM->pVM = pVM;
|
---|
607 | *ppVM = pVM;
|
---|
608 | return 0;
|
---|
609 | }
|
---|
610 |
|
---|
611 | RTPrintf("Fatal error: failed to allocated pages for the VM structure, rc=%Rrc\n", rc);
|
---|
612 | }
|
---|
613 | else
|
---|
614 | RTPrintf("Fatal error: MMR3InitUVM failed, rc=%Rrc\n", rc);
|
---|
615 | }
|
---|
616 | else
|
---|
617 | RTPrintf("Fatal error: SSMR3InitUVM failed, rc=%Rrc\n", rc);
|
---|
618 | }
|
---|
619 | else
|
---|
620 | RTPrintf("Fatal error: RTTlsSet failed, rc=%Rrc\n", rc);
|
---|
621 |
|
---|
622 | *ppVM = NULL;
|
---|
623 | return 1;
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 | int main(int argc, char **argv)
|
---|
628 | {
|
---|
629 | /*
|
---|
630 | * Init runtime and static data.
|
---|
631 | */
|
---|
632 | RTR3InitAndSUPLib();
|
---|
633 | RTPrintf("tstSSM: TESTING...\n");
|
---|
634 | initBigMem();
|
---|
635 | const char *pszFilename = "SSMTestSave#1";
|
---|
636 |
|
---|
637 | /*
|
---|
638 | * Create an fake VM structure and init SSM.
|
---|
639 | */
|
---|
640 | int rc = SUPR3Init(NULL);
|
---|
641 | if (RT_FAILURE(rc))
|
---|
642 | {
|
---|
643 | RTPrintf("Fatal error: SUP Failure! rc=%Rrc\n", rc);
|
---|
644 | return 1;
|
---|
645 | }
|
---|
646 | PVM pVM;
|
---|
647 | if (createFakeVM(&pVM))
|
---|
648 | return 1;
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Register a few callbacks.
|
---|
652 | */
|
---|
653 | rc = SSMR3RegisterDevice(pVM, NULL, "SSM Testcase Data Item no.1 (all types)", 1, 0, 256,
|
---|
654 | NULL, Item01Save, NULL,
|
---|
655 | NULL, Item01Load, NULL);
|
---|
656 | if (RT_FAILURE(rc))
|
---|
657 | {
|
---|
658 | RTPrintf("SSMR3Register #1 -> %Rrc\n", rc);
|
---|
659 | return 1;
|
---|
660 | }
|
---|
661 |
|
---|
662 | rc = SSMR3RegisterDevice(pVM, NULL, "SSM Testcase Data Item no.2 (rand mem)", 2, 0, _1M * 8,
|
---|
663 | NULL, Item02Save, NULL,
|
---|
664 | NULL, Item02Load, NULL);
|
---|
665 | if (RT_FAILURE(rc))
|
---|
666 | {
|
---|
667 | RTPrintf("SSMR3Register #2 -> %Rrc\n", rc);
|
---|
668 | return 1;
|
---|
669 | }
|
---|
670 |
|
---|
671 | rc = SSMR3RegisterDevice(pVM, NULL, "SSM Testcase Data Item no.3 (big mem)", 0, 123, 512*_1M,
|
---|
672 | NULL, Item03Save, NULL,
|
---|
673 | NULL, Item03Load, NULL);
|
---|
674 | if (RT_FAILURE(rc))
|
---|
675 | {
|
---|
676 | RTPrintf("SSMR3Register #3 -> %Rrc\n", rc);
|
---|
677 | return 1;
|
---|
678 | }
|
---|
679 |
|
---|
680 | rc = SSMR3RegisterDevice(pVM, NULL, "SSM Testcase Data Item no.4 (big zero mem)", 0, 42, 512*_1M,
|
---|
681 | NULL, Item04Save, NULL,
|
---|
682 | NULL, Item04Load, NULL);
|
---|
683 | if (RT_FAILURE(rc))
|
---|
684 | {
|
---|
685 | RTPrintf("SSMR3Register #4 -> %Rrc\n", rc);
|
---|
686 | return 1;
|
---|
687 | }
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * Attempt a save.
|
---|
691 | */
|
---|
692 | uint64_t u64Start = RTTimeNanoTS();
|
---|
693 | rc = SSMR3Save(pVM, pszFilename, SSMAFTER_DESTROY, NULL, NULL);
|
---|
694 | if (RT_FAILURE(rc))
|
---|
695 | {
|
---|
696 | RTPrintf("SSMR3Save #1 -> %Rrc\n", rc);
|
---|
697 | return 1;
|
---|
698 | }
|
---|
699 | uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
700 | RTPrintf("tstSSM: Saved in %RI64 ns\n", u64Elapsed);
|
---|
701 |
|
---|
702 | RTFSOBJINFO Info;
|
---|
703 | rc = RTPathQueryInfo(pszFilename, &Info, RTFSOBJATTRADD_NOTHING);
|
---|
704 | if (RT_FAILURE(rc))
|
---|
705 | {
|
---|
706 | RTPrintf("tstSSM: failed to query file size: %Rrc\n", rc);
|
---|
707 | return 1;
|
---|
708 | }
|
---|
709 | RTPrintf("tstSSM: file size %RI64 bytes\n", Info.cbObject);
|
---|
710 |
|
---|
711 | /*
|
---|
712 | * Attempt a load.
|
---|
713 | */
|
---|
714 | u64Start = RTTimeNanoTS();
|
---|
715 | rc = SSMR3Load(pVM, pszFilename, SSMAFTER_RESUME, NULL, NULL);
|
---|
716 | if (RT_FAILURE(rc))
|
---|
717 | {
|
---|
718 | RTPrintf("SSMR3Load #1 -> %Rrc\n", rc);
|
---|
719 | return 1;
|
---|
720 | }
|
---|
721 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
722 | RTPrintf("tstSSM: Loaded in %RI64 ns\n", u64Elapsed);
|
---|
723 |
|
---|
724 | /*
|
---|
725 | * Validate it.
|
---|
726 | */
|
---|
727 | u64Start = RTTimeNanoTS();
|
---|
728 | rc = SSMR3ValidateFile(pszFilename);
|
---|
729 | if (RT_FAILURE(rc))
|
---|
730 | {
|
---|
731 | RTPrintf("SSMR3ValidateFile #1 -> %Rrc\n", rc);
|
---|
732 | return 1;
|
---|
733 | }
|
---|
734 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
735 | RTPrintf("tstSSM: Validated in %RI64 ns\n", u64Elapsed);
|
---|
736 |
|
---|
737 | /*
|
---|
738 | * Open it and read.
|
---|
739 | */
|
---|
740 | u64Start = RTTimeNanoTS();
|
---|
741 | PSSMHANDLE pSSM;
|
---|
742 | rc = SSMR3Open(pszFilename, 0, &pSSM);
|
---|
743 | if (RT_FAILURE(rc))
|
---|
744 | {
|
---|
745 | RTPrintf("SSMR3Open #1 -> %Rrc\n", rc);
|
---|
746 | return 1;
|
---|
747 | }
|
---|
748 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
749 | RTPrintf("tstSSM: Opened in %RI64 ns\n", u64Elapsed);
|
---|
750 |
|
---|
751 | /* negative */
|
---|
752 | u64Start = RTTimeNanoTS();
|
---|
753 | rc = SSMR3Seek(pSSM, "some unit that doesn't exist", 0, NULL);
|
---|
754 | if (rc != VERR_SSM_UNIT_NOT_FOUND)
|
---|
755 | {
|
---|
756 | RTPrintf("SSMR3Seek #1 negative -> %Rrc\n", rc);
|
---|
757 | return 1;
|
---|
758 | }
|
---|
759 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
760 | RTPrintf("tstSSM: Failed seek in %RI64 ns\n", u64Elapsed);
|
---|
761 |
|
---|
762 | /* 2nd unit */
|
---|
763 | rc = SSMR3Seek(pSSM, "SSM Testcase Data Item no.2 (rand mem)", 0, NULL);
|
---|
764 | if (RT_FAILURE(rc))
|
---|
765 | {
|
---|
766 | RTPrintf("SSMR3Seek #1 unit 2-> %Rrc\n", rc);
|
---|
767 | return 1;
|
---|
768 | }
|
---|
769 | uint32_t u32Version = 0xbadc0ded;
|
---|
770 | rc = SSMR3Seek(pSSM, "SSM Testcase Data Item no.2 (rand mem)", 0, &u32Version);
|
---|
771 | if (RT_FAILURE(rc))
|
---|
772 | {
|
---|
773 | RTPrintf("SSMR3Seek #1 unit 2-> %Rrc\n", rc);
|
---|
774 | return 1;
|
---|
775 | }
|
---|
776 | u64Start = RTTimeNanoTS();
|
---|
777 | rc = Item02Load(NULL, pSSM, u32Version);
|
---|
778 | if (RT_FAILURE(rc))
|
---|
779 | {
|
---|
780 | RTPrintf("Item02Load #1 -> %Rrc\n", rc);
|
---|
781 | return 1;
|
---|
782 | }
|
---|
783 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
784 | RTPrintf("tstSSM: Loaded 2nd item in %RI64 ns\n", u64Elapsed);
|
---|
785 |
|
---|
786 | /* 1st unit */
|
---|
787 | u32Version = 0xbadc0ded;
|
---|
788 | rc = SSMR3Seek(pSSM, "SSM Testcase Data Item no.1 (all types)", 0, &u32Version);
|
---|
789 | if (RT_FAILURE(rc))
|
---|
790 | {
|
---|
791 | RTPrintf("SSMR3Seek #1 unit 1 -> %Rrc\n", rc);
|
---|
792 | return 1;
|
---|
793 | }
|
---|
794 | u64Start = RTTimeNanoTS();
|
---|
795 | rc = Item01Load(NULL, pSSM, u32Version);
|
---|
796 | if (RT_FAILURE(rc))
|
---|
797 | {
|
---|
798 | RTPrintf("Item01Load #1 -> %Rrc\n", rc);
|
---|
799 | return 1;
|
---|
800 | }
|
---|
801 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
802 | RTPrintf("tstSSM: Loaded 1st item in %RI64 ns\n", u64Elapsed);
|
---|
803 |
|
---|
804 | /* 3st unit */
|
---|
805 | u32Version = 0xbadc0ded;
|
---|
806 | rc = SSMR3Seek(pSSM, "SSM Testcase Data Item no.3 (big mem)", 123, &u32Version);
|
---|
807 | if (RT_FAILURE(rc))
|
---|
808 | {
|
---|
809 | RTPrintf("SSMR3Seek #3 unit 1 -> %Rrc\n", rc);
|
---|
810 | return 1;
|
---|
811 | }
|
---|
812 | u64Start = RTTimeNanoTS();
|
---|
813 | rc = Item03Load(NULL, pSSM, u32Version);
|
---|
814 | if (RT_FAILURE(rc))
|
---|
815 | {
|
---|
816 | RTPrintf("Item01Load #3 -> %Rrc\n", rc);
|
---|
817 | return 1;
|
---|
818 | }
|
---|
819 | u64Elapsed = RTTimeNanoTS() - u64Start;
|
---|
820 | RTPrintf("tstSSM: Loaded 3rd item in %RI64 ns\n", u64Elapsed);
|
---|
821 |
|
---|
822 | /* close */
|
---|
823 | rc = SSMR3Close(pSSM);
|
---|
824 | if (RT_FAILURE(rc))
|
---|
825 | {
|
---|
826 | RTPrintf("SSMR3Close #1 -> %Rrc\n", rc);
|
---|
827 | return 1;
|
---|
828 | }
|
---|
829 |
|
---|
830 | RTPrintf("tstSSM: SUCCESS\n");
|
---|
831 | return 0;
|
---|
832 | }
|
---|
833 |
|
---|