1da668aa1SThomas Huth /*
2da668aa1SThomas Huth * Test code for VMState
3da668aa1SThomas Huth *
4da668aa1SThomas Huth * Copyright (c) 2013 Red Hat Inc.
5da668aa1SThomas Huth *
6da668aa1SThomas Huth * Permission is hereby granted, free of charge, to any person obtaining a copy
7da668aa1SThomas Huth * of this software and associated documentation files (the "Software"), to deal
8da668aa1SThomas Huth * in the Software without restriction, including without limitation the rights
9da668aa1SThomas Huth * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10da668aa1SThomas Huth * copies of the Software, and to permit persons to whom the Software is
11da668aa1SThomas Huth * furnished to do so, subject to the following conditions:
12da668aa1SThomas Huth *
13da668aa1SThomas Huth * The above copyright notice and this permission notice shall be included in
14da668aa1SThomas Huth * all copies or substantial portions of the Software.
15da668aa1SThomas Huth *
16da668aa1SThomas Huth * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17da668aa1SThomas Huth * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18da668aa1SThomas Huth * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19da668aa1SThomas Huth * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20da668aa1SThomas Huth * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21da668aa1SThomas Huth * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22da668aa1SThomas Huth * THE SOFTWARE.
23da668aa1SThomas Huth */
24da668aa1SThomas Huth
25da668aa1SThomas Huth #include "qemu/osdep.h"
26da668aa1SThomas Huth
27da668aa1SThomas Huth #include "migration/vmstate.h"
28da668aa1SThomas Huth #include "migration/qemu-file-types.h"
29da668aa1SThomas Huth #include "../migration/qemu-file.h"
30da668aa1SThomas Huth #include "../migration/savevm.h"
31da668aa1SThomas Huth #include "qemu/module.h"
32da668aa1SThomas Huth #include "io/channel-file.h"
33da668aa1SThomas Huth
34da668aa1SThomas Huth static int temp_fd;
35da668aa1SThomas Huth
36da668aa1SThomas Huth
37da668aa1SThomas Huth /* Duplicate temp_fd and seek to the beginning of the file */
open_test_file(bool write)38da668aa1SThomas Huth static QEMUFile *open_test_file(bool write)
39da668aa1SThomas Huth {
401c861885SPeter Maydell int fd;
41da668aa1SThomas Huth QIOChannel *ioc;
42da668aa1SThomas Huth QEMUFile *f;
43da668aa1SThomas Huth
441c861885SPeter Maydell fd = dup(temp_fd);
451c861885SPeter Maydell g_assert(fd >= 0);
46da668aa1SThomas Huth lseek(fd, 0, SEEK_SET);
47da668aa1SThomas Huth if (write) {
48da668aa1SThomas Huth g_assert_cmpint(ftruncate(fd, 0), ==, 0);
49da668aa1SThomas Huth }
50da668aa1SThomas Huth ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
51da668aa1SThomas Huth if (write) {
5277ef2dc1SDaniel P. Berrangé f = qemu_file_new_output(ioc);
53da668aa1SThomas Huth } else {
5477ef2dc1SDaniel P. Berrangé f = qemu_file_new_input(ioc);
55da668aa1SThomas Huth }
56da668aa1SThomas Huth object_unref(OBJECT(ioc));
57da668aa1SThomas Huth return f;
58da668aa1SThomas Huth }
59da668aa1SThomas Huth
60da668aa1SThomas Huth #define SUCCESS(val) \
61da668aa1SThomas Huth g_assert_cmpint((val), ==, 0)
62da668aa1SThomas Huth
63da668aa1SThomas Huth #define FAILURE(val) \
64da668aa1SThomas Huth g_assert_cmpint((val), !=, 0)
65da668aa1SThomas Huth
save_vmstate(const VMStateDescription * desc,void * obj)66da668aa1SThomas Huth static void save_vmstate(const VMStateDescription *desc, void *obj)
67da668aa1SThomas Huth {
68da668aa1SThomas Huth QEMUFile *f = open_test_file(true);
69da668aa1SThomas Huth
70da668aa1SThomas Huth /* Save file with vmstate */
71da668aa1SThomas Huth int ret = vmstate_save_state(f, desc, obj, NULL);
72da668aa1SThomas Huth g_assert(!ret);
73da668aa1SThomas Huth qemu_put_byte(f, QEMU_VM_EOF);
74da668aa1SThomas Huth g_assert(!qemu_file_get_error(f));
75da668aa1SThomas Huth qemu_fclose(f);
76da668aa1SThomas Huth }
77da668aa1SThomas Huth
save_buffer(const uint8_t * buf,size_t buf_size)78da668aa1SThomas Huth static void save_buffer(const uint8_t *buf, size_t buf_size)
79da668aa1SThomas Huth {
80da668aa1SThomas Huth QEMUFile *fsave = open_test_file(true);
81da668aa1SThomas Huth qemu_put_buffer(fsave, buf, buf_size);
82da668aa1SThomas Huth qemu_fclose(fsave);
83da668aa1SThomas Huth }
84da668aa1SThomas Huth
compare_vmstate(const uint8_t * wire,size_t size)85da668aa1SThomas Huth static void compare_vmstate(const uint8_t *wire, size_t size)
86da668aa1SThomas Huth {
87da668aa1SThomas Huth QEMUFile *f = open_test_file(false);
88972d325aSPhilippe Mathieu-Daudé g_autofree uint8_t *result = g_malloc(size);
89da668aa1SThomas Huth
90da668aa1SThomas Huth /* read back as binary */
91da668aa1SThomas Huth
92972d325aSPhilippe Mathieu-Daudé g_assert_cmpint(qemu_get_buffer(f, result, size), ==, size);
93da668aa1SThomas Huth g_assert(!qemu_file_get_error(f));
94da668aa1SThomas Huth
95da668aa1SThomas Huth /* Compare that what is on the file is the same that what we
96da668aa1SThomas Huth expected to be there */
97972d325aSPhilippe Mathieu-Daudé SUCCESS(memcmp(result, wire, size));
98da668aa1SThomas Huth
99da668aa1SThomas Huth /* Must reach EOF */
100da668aa1SThomas Huth qemu_get_byte(f);
101da668aa1SThomas Huth g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
102da668aa1SThomas Huth
103da668aa1SThomas Huth qemu_fclose(f);
104da668aa1SThomas Huth }
105da668aa1SThomas Huth
load_vmstate_one(const VMStateDescription * desc,void * obj,int version,const uint8_t * wire,size_t size)106da668aa1SThomas Huth static int load_vmstate_one(const VMStateDescription *desc, void *obj,
107da668aa1SThomas Huth int version, const uint8_t *wire, size_t size)
108da668aa1SThomas Huth {
109da668aa1SThomas Huth QEMUFile *f;
110da668aa1SThomas Huth int ret;
111da668aa1SThomas Huth
112da668aa1SThomas Huth f = open_test_file(true);
113da668aa1SThomas Huth qemu_put_buffer(f, wire, size);
114da668aa1SThomas Huth qemu_fclose(f);
115da668aa1SThomas Huth
116da668aa1SThomas Huth f = open_test_file(false);
117da668aa1SThomas Huth ret = vmstate_load_state(f, desc, obj, version);
118da668aa1SThomas Huth if (ret) {
119da668aa1SThomas Huth g_assert(qemu_file_get_error(f));
120da668aa1SThomas Huth } else{
121da668aa1SThomas Huth g_assert(!qemu_file_get_error(f));
122da668aa1SThomas Huth }
123da668aa1SThomas Huth qemu_fclose(f);
124da668aa1SThomas Huth return ret;
125da668aa1SThomas Huth }
126da668aa1SThomas Huth
127da668aa1SThomas Huth
load_vmstate(const VMStateDescription * desc,void * obj,void * obj_clone,void (* obj_copy)(void *,void *),int version,const uint8_t * wire,size_t size)128da668aa1SThomas Huth static int load_vmstate(const VMStateDescription *desc,
129da668aa1SThomas Huth void *obj, void *obj_clone,
130da668aa1SThomas Huth void (*obj_copy)(void *, void*),
131da668aa1SThomas Huth int version, const uint8_t *wire, size_t size)
132da668aa1SThomas Huth {
133da668aa1SThomas Huth /* We test with zero size */
134da668aa1SThomas Huth obj_copy(obj_clone, obj);
135da668aa1SThomas Huth FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
136da668aa1SThomas Huth
137da668aa1SThomas Huth /* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
138da668aa1SThomas Huth * able to test in the middle */
139da668aa1SThomas Huth
140da668aa1SThomas Huth if (size > 3) {
141da668aa1SThomas Huth
142da668aa1SThomas Huth /* We test with size - 2. We can't test size - 1 due to EOF tricks */
143da668aa1SThomas Huth obj_copy(obj, obj_clone);
144da668aa1SThomas Huth FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
145da668aa1SThomas Huth
146da668aa1SThomas Huth /* Test with size/2, first half of real state */
147da668aa1SThomas Huth obj_copy(obj, obj_clone);
148da668aa1SThomas Huth FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
149da668aa1SThomas Huth
150da668aa1SThomas Huth /* Test with size/2, second half of real state */
151da668aa1SThomas Huth obj_copy(obj, obj_clone);
152da668aa1SThomas Huth FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
153da668aa1SThomas Huth
154da668aa1SThomas Huth }
155da668aa1SThomas Huth obj_copy(obj, obj_clone);
156da668aa1SThomas Huth return load_vmstate_one(desc, obj, version, wire, size);
157da668aa1SThomas Huth }
158da668aa1SThomas Huth
159da668aa1SThomas Huth /* Test struct that we are going to use for our tests */
160da668aa1SThomas Huth
161da668aa1SThomas Huth typedef struct TestSimple {
162da668aa1SThomas Huth bool b_1, b_2;
163da668aa1SThomas Huth uint8_t u8_1;
164da668aa1SThomas Huth uint16_t u16_1;
165da668aa1SThomas Huth uint32_t u32_1;
166da668aa1SThomas Huth uint64_t u64_1;
167da668aa1SThomas Huth int8_t i8_1, i8_2;
168da668aa1SThomas Huth int16_t i16_1, i16_2;
169da668aa1SThomas Huth int32_t i32_1, i32_2;
170da668aa1SThomas Huth int64_t i64_1, i64_2;
171da668aa1SThomas Huth } TestSimple;
172da668aa1SThomas Huth
173da668aa1SThomas Huth /* Object instantiation, we are going to use it in more than one test */
174da668aa1SThomas Huth
175da668aa1SThomas Huth TestSimple obj_simple = {
176da668aa1SThomas Huth .b_1 = true,
177da668aa1SThomas Huth .b_2 = false,
178da668aa1SThomas Huth .u8_1 = 130,
179da668aa1SThomas Huth .u16_1 = 512,
180da668aa1SThomas Huth .u32_1 = 70000,
181da668aa1SThomas Huth .u64_1 = 12121212,
182da668aa1SThomas Huth .i8_1 = 65,
183da668aa1SThomas Huth .i8_2 = -65,
184da668aa1SThomas Huth .i16_1 = 512,
185da668aa1SThomas Huth .i16_2 = -512,
186da668aa1SThomas Huth .i32_1 = 70000,
187da668aa1SThomas Huth .i32_2 = -70000,
188da668aa1SThomas Huth .i64_1 = 12121212,
189da668aa1SThomas Huth .i64_2 = -12121212,
190da668aa1SThomas Huth };
191da668aa1SThomas Huth
192da668aa1SThomas Huth /* Description of the values. If you add a primitive type
193da668aa1SThomas Huth you are expected to add a test here */
194da668aa1SThomas Huth
195da668aa1SThomas Huth static const VMStateDescription vmstate_simple_primitive = {
196da668aa1SThomas Huth .name = "simple/primitive",
197da668aa1SThomas Huth .version_id = 1,
198da668aa1SThomas Huth .minimum_version_id = 1,
199*57c73988SRichard Henderson .fields = (const VMStateField[]) {
200da668aa1SThomas Huth VMSTATE_BOOL(b_1, TestSimple),
201da668aa1SThomas Huth VMSTATE_BOOL(b_2, TestSimple),
202da668aa1SThomas Huth VMSTATE_UINT8(u8_1, TestSimple),
203da668aa1SThomas Huth VMSTATE_UINT16(u16_1, TestSimple),
204da668aa1SThomas Huth VMSTATE_UINT32(u32_1, TestSimple),
205da668aa1SThomas Huth VMSTATE_UINT64(u64_1, TestSimple),
206da668aa1SThomas Huth VMSTATE_INT8(i8_1, TestSimple),
207da668aa1SThomas Huth VMSTATE_INT8(i8_2, TestSimple),
208da668aa1SThomas Huth VMSTATE_INT16(i16_1, TestSimple),
209da668aa1SThomas Huth VMSTATE_INT16(i16_2, TestSimple),
210da668aa1SThomas Huth VMSTATE_INT32(i32_1, TestSimple),
211da668aa1SThomas Huth VMSTATE_INT32(i32_2, TestSimple),
212da668aa1SThomas Huth VMSTATE_INT64(i64_1, TestSimple),
213da668aa1SThomas Huth VMSTATE_INT64(i64_2, TestSimple),
214da668aa1SThomas Huth VMSTATE_END_OF_LIST()
215da668aa1SThomas Huth }
216da668aa1SThomas Huth };
217da668aa1SThomas Huth
218da668aa1SThomas Huth /* It describes what goes through the wire. Our tests are basically:
219da668aa1SThomas Huth
220da668aa1SThomas Huth * save test
221da668aa1SThomas Huth - save a struct a vmstate to a file
222da668aa1SThomas Huth - read that file back (binary read, no vmstate)
223da668aa1SThomas Huth - compare it with what we expect to be on the wire
224da668aa1SThomas Huth * load test
225da668aa1SThomas Huth - save to the file what we expect to be on the wire
226da668aa1SThomas Huth - read struct back with vmstate in a different
227da668aa1SThomas Huth - compare back with the original struct
228da668aa1SThomas Huth */
229da668aa1SThomas Huth
230da668aa1SThomas Huth uint8_t wire_simple_primitive[] = {
231da668aa1SThomas Huth /* b_1 */ 0x01,
232da668aa1SThomas Huth /* b_2 */ 0x00,
233da668aa1SThomas Huth /* u8_1 */ 0x82,
234da668aa1SThomas Huth /* u16_1 */ 0x02, 0x00,
235da668aa1SThomas Huth /* u32_1 */ 0x00, 0x01, 0x11, 0x70,
236da668aa1SThomas Huth /* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
237da668aa1SThomas Huth /* i8_1 */ 0x41,
238da668aa1SThomas Huth /* i8_2 */ 0xbf,
239da668aa1SThomas Huth /* i16_1 */ 0x02, 0x00,
240da668aa1SThomas Huth /* i16_2 */ 0xfe, 0x0,
241da668aa1SThomas Huth /* i32_1 */ 0x00, 0x01, 0x11, 0x70,
242da668aa1SThomas Huth /* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
243da668aa1SThomas Huth /* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
244da668aa1SThomas Huth /* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
245da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
246da668aa1SThomas Huth };
247da668aa1SThomas Huth
obj_simple_copy(void * target,void * source)248da668aa1SThomas Huth static void obj_simple_copy(void *target, void *source)
249da668aa1SThomas Huth {
250da668aa1SThomas Huth memcpy(target, source, sizeof(TestSimple));
251da668aa1SThomas Huth }
252da668aa1SThomas Huth
test_simple_primitive(void)253da668aa1SThomas Huth static void test_simple_primitive(void)
254da668aa1SThomas Huth {
255da668aa1SThomas Huth TestSimple obj, obj_clone;
256da668aa1SThomas Huth
257da668aa1SThomas Huth memset(&obj, 0, sizeof(obj));
258da668aa1SThomas Huth save_vmstate(&vmstate_simple_primitive, &obj_simple);
259da668aa1SThomas Huth
260da668aa1SThomas Huth compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
261da668aa1SThomas Huth
262da668aa1SThomas Huth SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
263da668aa1SThomas Huth obj_simple_copy, 1, wire_simple_primitive,
264da668aa1SThomas Huth sizeof(wire_simple_primitive)));
265da668aa1SThomas Huth
266da668aa1SThomas Huth #define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
267da668aa1SThomas Huth
268da668aa1SThomas Huth FIELD_EQUAL(b_1);
269da668aa1SThomas Huth FIELD_EQUAL(b_2);
270da668aa1SThomas Huth FIELD_EQUAL(u8_1);
271da668aa1SThomas Huth FIELD_EQUAL(u16_1);
272da668aa1SThomas Huth FIELD_EQUAL(u32_1);
273da668aa1SThomas Huth FIELD_EQUAL(u64_1);
274da668aa1SThomas Huth FIELD_EQUAL(i8_1);
275da668aa1SThomas Huth FIELD_EQUAL(i8_2);
276da668aa1SThomas Huth FIELD_EQUAL(i16_1);
277da668aa1SThomas Huth FIELD_EQUAL(i16_2);
278da668aa1SThomas Huth FIELD_EQUAL(i32_1);
279da668aa1SThomas Huth FIELD_EQUAL(i32_2);
280da668aa1SThomas Huth FIELD_EQUAL(i64_1);
281da668aa1SThomas Huth FIELD_EQUAL(i64_2);
282da668aa1SThomas Huth }
283da668aa1SThomas Huth
284da668aa1SThomas Huth typedef struct TestSimpleArray {
285da668aa1SThomas Huth uint16_t u16_1[3];
286da668aa1SThomas Huth } TestSimpleArray;
287da668aa1SThomas Huth
288da668aa1SThomas Huth /* Object instantiation, we are going to use it in more than one test */
289da668aa1SThomas Huth
290da668aa1SThomas Huth TestSimpleArray obj_simple_arr = {
291da668aa1SThomas Huth .u16_1 = { 0x42, 0x43, 0x44 },
292da668aa1SThomas Huth };
293da668aa1SThomas Huth
294da668aa1SThomas Huth /* Description of the values. If you add a primitive type
295da668aa1SThomas Huth you are expected to add a test here */
296da668aa1SThomas Huth
297da668aa1SThomas Huth static const VMStateDescription vmstate_simple_arr = {
298da668aa1SThomas Huth .name = "simple/array",
299da668aa1SThomas Huth .version_id = 1,
300da668aa1SThomas Huth .minimum_version_id = 1,
301*57c73988SRichard Henderson .fields = (const VMStateField[]) {
302da668aa1SThomas Huth VMSTATE_UINT16_ARRAY(u16_1, TestSimpleArray, 3),
303da668aa1SThomas Huth VMSTATE_END_OF_LIST()
304da668aa1SThomas Huth }
305da668aa1SThomas Huth };
306da668aa1SThomas Huth
307da668aa1SThomas Huth uint8_t wire_simple_arr[] = {
308da668aa1SThomas Huth /* u16_1 */ 0x00, 0x42,
309da668aa1SThomas Huth /* u16_1 */ 0x00, 0x43,
310da668aa1SThomas Huth /* u16_1 */ 0x00, 0x44,
311da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
312da668aa1SThomas Huth };
313da668aa1SThomas Huth
obj_simple_arr_copy(void * target,void * source)314da668aa1SThomas Huth static void obj_simple_arr_copy(void *target, void *source)
315da668aa1SThomas Huth {
316da668aa1SThomas Huth memcpy(target, source, sizeof(TestSimpleArray));
317da668aa1SThomas Huth }
318da668aa1SThomas Huth
test_simple_array(void)319da668aa1SThomas Huth static void test_simple_array(void)
320da668aa1SThomas Huth {
321da668aa1SThomas Huth TestSimpleArray obj, obj_clone;
322da668aa1SThomas Huth
323da668aa1SThomas Huth memset(&obj, 0, sizeof(obj));
324da668aa1SThomas Huth save_vmstate(&vmstate_simple_arr, &obj_simple_arr);
325da668aa1SThomas Huth
326da668aa1SThomas Huth compare_vmstate(wire_simple_arr, sizeof(wire_simple_arr));
327da668aa1SThomas Huth
328da668aa1SThomas Huth SUCCESS(load_vmstate(&vmstate_simple_arr, &obj, &obj_clone,
329da668aa1SThomas Huth obj_simple_arr_copy, 1, wire_simple_arr,
330da668aa1SThomas Huth sizeof(wire_simple_arr)));
331da668aa1SThomas Huth }
332da668aa1SThomas Huth
333da668aa1SThomas Huth typedef struct TestStruct {
334da668aa1SThomas Huth uint32_t a, b, c, e;
335da668aa1SThomas Huth uint64_t d, f;
336da668aa1SThomas Huth bool skip_c_e;
337da668aa1SThomas Huth } TestStruct;
338da668aa1SThomas Huth
339da668aa1SThomas Huth static const VMStateDescription vmstate_versioned = {
340da668aa1SThomas Huth .name = "test/versioned",
341da668aa1SThomas Huth .version_id = 2,
342da668aa1SThomas Huth .minimum_version_id = 1,
343*57c73988SRichard Henderson .fields = (const VMStateField[]) {
344da668aa1SThomas Huth VMSTATE_UINT32(a, TestStruct),
345da668aa1SThomas Huth VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
346da668aa1SThomas Huth * we catch bugs more easily.
347da668aa1SThomas Huth */
348da668aa1SThomas Huth VMSTATE_UINT32(c, TestStruct),
349da668aa1SThomas Huth VMSTATE_UINT64(d, TestStruct),
350da668aa1SThomas Huth VMSTATE_UINT32_V(e, TestStruct, 2),
351da668aa1SThomas Huth VMSTATE_UINT64_V(f, TestStruct, 2),
352da668aa1SThomas Huth VMSTATE_END_OF_LIST()
353da668aa1SThomas Huth }
354da668aa1SThomas Huth };
355da668aa1SThomas Huth
test_load_v1(void)356da668aa1SThomas Huth static void test_load_v1(void)
357da668aa1SThomas Huth {
358da668aa1SThomas Huth uint8_t buf[] = {
359da668aa1SThomas Huth 0, 0, 0, 10, /* a */
360da668aa1SThomas Huth 0, 0, 0, 30, /* c */
361da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 40, /* d */
362da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
363da668aa1SThomas Huth };
364da668aa1SThomas Huth save_buffer(buf, sizeof(buf));
365da668aa1SThomas Huth
366da668aa1SThomas Huth QEMUFile *loading = open_test_file(false);
367da668aa1SThomas Huth TestStruct obj = { .b = 200, .e = 500, .f = 600 };
368da668aa1SThomas Huth vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
369da668aa1SThomas Huth g_assert(!qemu_file_get_error(loading));
370da668aa1SThomas Huth g_assert_cmpint(obj.a, ==, 10);
371da668aa1SThomas Huth g_assert_cmpint(obj.b, ==, 200);
372da668aa1SThomas Huth g_assert_cmpint(obj.c, ==, 30);
373da668aa1SThomas Huth g_assert_cmpint(obj.d, ==, 40);
374da668aa1SThomas Huth g_assert_cmpint(obj.e, ==, 500);
375da668aa1SThomas Huth g_assert_cmpint(obj.f, ==, 600);
376da668aa1SThomas Huth qemu_fclose(loading);
377da668aa1SThomas Huth }
378da668aa1SThomas Huth
test_load_v2(void)379da668aa1SThomas Huth static void test_load_v2(void)
380da668aa1SThomas Huth {
381da668aa1SThomas Huth uint8_t buf[] = {
382da668aa1SThomas Huth 0, 0, 0, 10, /* a */
383da668aa1SThomas Huth 0, 0, 0, 20, /* b */
384da668aa1SThomas Huth 0, 0, 0, 30, /* c */
385da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 40, /* d */
386da668aa1SThomas Huth 0, 0, 0, 50, /* e */
387da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 60, /* f */
388da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
389da668aa1SThomas Huth };
390da668aa1SThomas Huth save_buffer(buf, sizeof(buf));
391da668aa1SThomas Huth
392da668aa1SThomas Huth QEMUFile *loading = open_test_file(false);
393da668aa1SThomas Huth TestStruct obj;
394da668aa1SThomas Huth vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
395da668aa1SThomas Huth g_assert_cmpint(obj.a, ==, 10);
396da668aa1SThomas Huth g_assert_cmpint(obj.b, ==, 20);
397da668aa1SThomas Huth g_assert_cmpint(obj.c, ==, 30);
398da668aa1SThomas Huth g_assert_cmpint(obj.d, ==, 40);
399da668aa1SThomas Huth g_assert_cmpint(obj.e, ==, 50);
400da668aa1SThomas Huth g_assert_cmpint(obj.f, ==, 60);
401da668aa1SThomas Huth qemu_fclose(loading);
402da668aa1SThomas Huth }
403da668aa1SThomas Huth
test_skip(void * opaque,int version_id)404da668aa1SThomas Huth static bool test_skip(void *opaque, int version_id)
405da668aa1SThomas Huth {
406da668aa1SThomas Huth TestStruct *t = (TestStruct *)opaque;
407da668aa1SThomas Huth return !t->skip_c_e;
408da668aa1SThomas Huth }
409da668aa1SThomas Huth
410da668aa1SThomas Huth static const VMStateDescription vmstate_skipping = {
411da668aa1SThomas Huth .name = "test/skip",
412da668aa1SThomas Huth .version_id = 2,
413da668aa1SThomas Huth .minimum_version_id = 1,
414*57c73988SRichard Henderson .fields = (const VMStateField[]) {
415da668aa1SThomas Huth VMSTATE_UINT32(a, TestStruct),
416da668aa1SThomas Huth VMSTATE_UINT32(b, TestStruct),
417da668aa1SThomas Huth VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
418da668aa1SThomas Huth VMSTATE_UINT64(d, TestStruct),
419da668aa1SThomas Huth VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
420da668aa1SThomas Huth VMSTATE_UINT64_V(f, TestStruct, 2),
421da668aa1SThomas Huth VMSTATE_END_OF_LIST()
422da668aa1SThomas Huth }
423da668aa1SThomas Huth };
424da668aa1SThomas Huth
425da668aa1SThomas Huth
test_save_noskip(void)426da668aa1SThomas Huth static void test_save_noskip(void)
427da668aa1SThomas Huth {
428da668aa1SThomas Huth QEMUFile *fsave = open_test_file(true);
429da668aa1SThomas Huth TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
430da668aa1SThomas Huth .skip_c_e = false };
431da668aa1SThomas Huth int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
432da668aa1SThomas Huth g_assert(!ret);
433da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
434da668aa1SThomas Huth
435da668aa1SThomas Huth uint8_t expected[] = {
436da668aa1SThomas Huth 0, 0, 0, 1, /* a */
437da668aa1SThomas Huth 0, 0, 0, 2, /* b */
438da668aa1SThomas Huth 0, 0, 0, 3, /* c */
439da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 4, /* d */
440da668aa1SThomas Huth 0, 0, 0, 5, /* e */
441da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 6, /* f */
442da668aa1SThomas Huth };
443da668aa1SThomas Huth
444da668aa1SThomas Huth qemu_fclose(fsave);
445da668aa1SThomas Huth compare_vmstate(expected, sizeof(expected));
446da668aa1SThomas Huth }
447da668aa1SThomas Huth
test_save_skip(void)448da668aa1SThomas Huth static void test_save_skip(void)
449da668aa1SThomas Huth {
450da668aa1SThomas Huth QEMUFile *fsave = open_test_file(true);
451da668aa1SThomas Huth TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
452da668aa1SThomas Huth .skip_c_e = true };
453da668aa1SThomas Huth int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
454da668aa1SThomas Huth g_assert(!ret);
455da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
456da668aa1SThomas Huth
457da668aa1SThomas Huth uint8_t expected[] = {
458da668aa1SThomas Huth 0, 0, 0, 1, /* a */
459da668aa1SThomas Huth 0, 0, 0, 2, /* b */
460da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 4, /* d */
461da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 6, /* f */
462da668aa1SThomas Huth };
463da668aa1SThomas Huth
464da668aa1SThomas Huth qemu_fclose(fsave);
465da668aa1SThomas Huth compare_vmstate(expected, sizeof(expected));
466da668aa1SThomas Huth }
467da668aa1SThomas Huth
test_load_noskip(void)468da668aa1SThomas Huth static void test_load_noskip(void)
469da668aa1SThomas Huth {
470da668aa1SThomas Huth uint8_t buf[] = {
471da668aa1SThomas Huth 0, 0, 0, 10, /* a */
472da668aa1SThomas Huth 0, 0, 0, 20, /* b */
473da668aa1SThomas Huth 0, 0, 0, 30, /* c */
474da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 40, /* d */
475da668aa1SThomas Huth 0, 0, 0, 50, /* e */
476da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 60, /* f */
477da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
478da668aa1SThomas Huth };
479da668aa1SThomas Huth save_buffer(buf, sizeof(buf));
480da668aa1SThomas Huth
481da668aa1SThomas Huth QEMUFile *loading = open_test_file(false);
482da668aa1SThomas Huth TestStruct obj = { .skip_c_e = false };
483da668aa1SThomas Huth vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
484da668aa1SThomas Huth g_assert(!qemu_file_get_error(loading));
485da668aa1SThomas Huth g_assert_cmpint(obj.a, ==, 10);
486da668aa1SThomas Huth g_assert_cmpint(obj.b, ==, 20);
487da668aa1SThomas Huth g_assert_cmpint(obj.c, ==, 30);
488da668aa1SThomas Huth g_assert_cmpint(obj.d, ==, 40);
489da668aa1SThomas Huth g_assert_cmpint(obj.e, ==, 50);
490da668aa1SThomas Huth g_assert_cmpint(obj.f, ==, 60);
491da668aa1SThomas Huth qemu_fclose(loading);
492da668aa1SThomas Huth }
493da668aa1SThomas Huth
test_load_skip(void)494da668aa1SThomas Huth static void test_load_skip(void)
495da668aa1SThomas Huth {
496da668aa1SThomas Huth uint8_t buf[] = {
497da668aa1SThomas Huth 0, 0, 0, 10, /* a */
498da668aa1SThomas Huth 0, 0, 0, 20, /* b */
499da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 40, /* d */
500da668aa1SThomas Huth 0, 0, 0, 0, 0, 0, 0, 60, /* f */
501da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
502da668aa1SThomas Huth };
503da668aa1SThomas Huth save_buffer(buf, sizeof(buf));
504da668aa1SThomas Huth
505da668aa1SThomas Huth QEMUFile *loading = open_test_file(false);
506da668aa1SThomas Huth TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
507da668aa1SThomas Huth vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
508da668aa1SThomas Huth g_assert(!qemu_file_get_error(loading));
509da668aa1SThomas Huth g_assert_cmpint(obj.a, ==, 10);
510da668aa1SThomas Huth g_assert_cmpint(obj.b, ==, 20);
511da668aa1SThomas Huth g_assert_cmpint(obj.c, ==, 300);
512da668aa1SThomas Huth g_assert_cmpint(obj.d, ==, 40);
513da668aa1SThomas Huth g_assert_cmpint(obj.e, ==, 500);
514da668aa1SThomas Huth g_assert_cmpint(obj.f, ==, 60);
515da668aa1SThomas Huth qemu_fclose(loading);
516da668aa1SThomas Huth }
517da668aa1SThomas Huth
518da668aa1SThomas Huth typedef struct {
519da668aa1SThomas Huth int32_t i;
520da668aa1SThomas Huth } TestStructTriv;
521da668aa1SThomas Huth
522da668aa1SThomas Huth const VMStateDescription vmsd_tst = {
523da668aa1SThomas Huth .name = "test/tst",
524da668aa1SThomas Huth .version_id = 1,
525da668aa1SThomas Huth .minimum_version_id = 1,
526*57c73988SRichard Henderson .fields = (const VMStateField[]) {
527da668aa1SThomas Huth VMSTATE_INT32(i, TestStructTriv),
528da668aa1SThomas Huth VMSTATE_END_OF_LIST()
529da668aa1SThomas Huth }
530da668aa1SThomas Huth };
531da668aa1SThomas Huth
532da668aa1SThomas Huth /* test array migration */
533da668aa1SThomas Huth
534da668aa1SThomas Huth #define AR_SIZE 4
535da668aa1SThomas Huth
536da668aa1SThomas Huth typedef struct {
537da668aa1SThomas Huth TestStructTriv *ar[AR_SIZE];
538da668aa1SThomas Huth } TestArrayOfPtrToStuct;
539da668aa1SThomas Huth
540da668aa1SThomas Huth const VMStateDescription vmsd_arps = {
541da668aa1SThomas Huth .name = "test/arps",
542da668aa1SThomas Huth .version_id = 1,
543da668aa1SThomas Huth .minimum_version_id = 1,
544*57c73988SRichard Henderson .fields = (const VMStateField[]) {
545da668aa1SThomas Huth VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
546da668aa1SThomas Huth AR_SIZE, 0, vmsd_tst, TestStructTriv),
547da668aa1SThomas Huth VMSTATE_END_OF_LIST()
548da668aa1SThomas Huth }
549da668aa1SThomas Huth };
550da668aa1SThomas Huth
551da668aa1SThomas Huth static uint8_t wire_arr_ptr_no0[] = {
552da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00,
553da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x01,
554da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x02,
555da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x03,
556da668aa1SThomas Huth QEMU_VM_EOF
557da668aa1SThomas Huth };
558da668aa1SThomas Huth
test_arr_ptr_str_no0_save(void)559da668aa1SThomas Huth static void test_arr_ptr_str_no0_save(void)
560da668aa1SThomas Huth {
561da668aa1SThomas Huth TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
562da668aa1SThomas Huth TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
563da668aa1SThomas Huth
564da668aa1SThomas Huth save_vmstate(&vmsd_arps, &sample);
565da668aa1SThomas Huth compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
566da668aa1SThomas Huth }
567da668aa1SThomas Huth
test_arr_ptr_str_no0_load(void)568da668aa1SThomas Huth static void test_arr_ptr_str_no0_load(void)
569da668aa1SThomas Huth {
570da668aa1SThomas Huth TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
571da668aa1SThomas Huth TestStructTriv ar[AR_SIZE] = {};
572da668aa1SThomas Huth TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
573da668aa1SThomas Huth int idx;
574da668aa1SThomas Huth
575da668aa1SThomas Huth save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
576da668aa1SThomas Huth SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
577da668aa1SThomas Huth wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
578da668aa1SThomas Huth for (idx = 0; idx < AR_SIZE; ++idx) {
579da668aa1SThomas Huth /* compare the target array ar with the ground truth array ar_gt */
580da668aa1SThomas Huth g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
581da668aa1SThomas Huth }
582da668aa1SThomas Huth }
583da668aa1SThomas Huth
584da668aa1SThomas Huth static uint8_t wire_arr_ptr_0[] = {
585da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00,
586da668aa1SThomas Huth VMS_NULLPTR_MARKER,
587da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x02,
588da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x03,
589da668aa1SThomas Huth QEMU_VM_EOF
590da668aa1SThomas Huth };
591da668aa1SThomas Huth
test_arr_ptr_str_0_save(void)592da668aa1SThomas Huth static void test_arr_ptr_str_0_save(void)
593da668aa1SThomas Huth {
594da668aa1SThomas Huth TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
595da668aa1SThomas Huth TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
596da668aa1SThomas Huth
597da668aa1SThomas Huth save_vmstate(&vmsd_arps, &sample);
598da668aa1SThomas Huth compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
599da668aa1SThomas Huth }
600da668aa1SThomas Huth
test_arr_ptr_str_0_load(void)601da668aa1SThomas Huth static void test_arr_ptr_str_0_load(void)
602da668aa1SThomas Huth {
603da668aa1SThomas Huth TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
604da668aa1SThomas Huth TestStructTriv ar[AR_SIZE] = {};
605da668aa1SThomas Huth TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
606da668aa1SThomas Huth int idx;
607da668aa1SThomas Huth
608da668aa1SThomas Huth save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
609da668aa1SThomas Huth SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
610da668aa1SThomas Huth wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
611da668aa1SThomas Huth for (idx = 0; idx < AR_SIZE; ++idx) {
612da668aa1SThomas Huth /* compare the target array ar with the ground truth array ar_gt */
613da668aa1SThomas Huth g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
614da668aa1SThomas Huth }
615da668aa1SThomas Huth for (idx = 0; idx < AR_SIZE; ++idx) {
616da668aa1SThomas Huth if (idx == 1) {
617da668aa1SThomas Huth g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
618da668aa1SThomas Huth } else {
619da668aa1SThomas Huth g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
620da668aa1SThomas Huth }
621da668aa1SThomas Huth }
622da668aa1SThomas Huth }
623da668aa1SThomas Huth
624da668aa1SThomas Huth typedef struct TestArrayOfPtrToInt {
625da668aa1SThomas Huth int32_t *ar[AR_SIZE];
626da668aa1SThomas Huth } TestArrayOfPtrToInt;
627da668aa1SThomas Huth
628da668aa1SThomas Huth const VMStateDescription vmsd_arpp = {
629da668aa1SThomas Huth .name = "test/arps",
630da668aa1SThomas Huth .version_id = 1,
631da668aa1SThomas Huth .minimum_version_id = 1,
632*57c73988SRichard Henderson .fields = (const VMStateField[]) {
633da668aa1SThomas Huth VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt,
634da668aa1SThomas Huth AR_SIZE, 0, vmstate_info_int32, int32_t*),
635da668aa1SThomas Huth VMSTATE_END_OF_LIST()
636da668aa1SThomas Huth }
637da668aa1SThomas Huth };
638da668aa1SThomas Huth
test_arr_ptr_prim_0_save(void)639da668aa1SThomas Huth static void test_arr_ptr_prim_0_save(void)
640da668aa1SThomas Huth {
641da668aa1SThomas Huth int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
642da668aa1SThomas Huth TestArrayOfPtrToInt sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
643da668aa1SThomas Huth
644da668aa1SThomas Huth save_vmstate(&vmsd_arpp, &sample);
645da668aa1SThomas Huth compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
646da668aa1SThomas Huth }
647da668aa1SThomas Huth
test_arr_ptr_prim_0_load(void)648da668aa1SThomas Huth static void test_arr_ptr_prim_0_load(void)
649da668aa1SThomas Huth {
650da668aa1SThomas Huth int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3};
651da668aa1SThomas Huth int32_t ar[AR_SIZE] = {3 , 42, 1, 0};
652da668aa1SThomas Huth TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
653da668aa1SThomas Huth int idx;
654da668aa1SThomas Huth
655da668aa1SThomas Huth save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
656da668aa1SThomas Huth SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1,
657da668aa1SThomas Huth wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
658da668aa1SThomas Huth for (idx = 0; idx < AR_SIZE; ++idx) {
659da668aa1SThomas Huth /* compare the target array ar with the ground truth array ar_gt */
660da668aa1SThomas Huth if (idx == 1) {
661da668aa1SThomas Huth g_assert_cmpint(42, ==, ar[idx]);
662da668aa1SThomas Huth } else {
663da668aa1SThomas Huth g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
664da668aa1SThomas Huth }
665da668aa1SThomas Huth }
666da668aa1SThomas Huth }
667da668aa1SThomas Huth
668da668aa1SThomas Huth /* test QTAILQ migration */
669da668aa1SThomas Huth typedef struct TestQtailqElement TestQtailqElement;
670da668aa1SThomas Huth
671da668aa1SThomas Huth struct TestQtailqElement {
672da668aa1SThomas Huth bool b;
673da668aa1SThomas Huth uint8_t u8;
674da668aa1SThomas Huth QTAILQ_ENTRY(TestQtailqElement) next;
675da668aa1SThomas Huth };
676da668aa1SThomas Huth
677da668aa1SThomas Huth typedef struct TestQtailq {
678da668aa1SThomas Huth int16_t i16;
679da668aa1SThomas Huth QTAILQ_HEAD(, TestQtailqElement) q;
680da668aa1SThomas Huth int32_t i32;
681da668aa1SThomas Huth } TestQtailq;
682da668aa1SThomas Huth
683da668aa1SThomas Huth static const VMStateDescription vmstate_q_element = {
684da668aa1SThomas Huth .name = "test/queue-element",
685da668aa1SThomas Huth .version_id = 1,
686da668aa1SThomas Huth .minimum_version_id = 1,
687*57c73988SRichard Henderson .fields = (const VMStateField[]) {
688da668aa1SThomas Huth VMSTATE_BOOL(b, TestQtailqElement),
689da668aa1SThomas Huth VMSTATE_UINT8(u8, TestQtailqElement),
690da668aa1SThomas Huth VMSTATE_END_OF_LIST()
691da668aa1SThomas Huth },
692da668aa1SThomas Huth };
693da668aa1SThomas Huth
694da668aa1SThomas Huth static const VMStateDescription vmstate_q = {
695da668aa1SThomas Huth .name = "test/queue",
696da668aa1SThomas Huth .version_id = 1,
697da668aa1SThomas Huth .minimum_version_id = 1,
698*57c73988SRichard Henderson .fields = (const VMStateField[]) {
699da668aa1SThomas Huth VMSTATE_INT16(i16, TestQtailq),
700da668aa1SThomas Huth VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
701da668aa1SThomas Huth next),
702da668aa1SThomas Huth VMSTATE_INT32(i32, TestQtailq),
703da668aa1SThomas Huth VMSTATE_END_OF_LIST()
704da668aa1SThomas Huth }
705da668aa1SThomas Huth };
706da668aa1SThomas Huth
707da668aa1SThomas Huth uint8_t wire_q[] = {
708da668aa1SThomas Huth /* i16 */ 0xfe, 0x0,
709da668aa1SThomas Huth /* start of element 0 of q */ 0x01,
710da668aa1SThomas Huth /* .b */ 0x01,
711da668aa1SThomas Huth /* .u8 */ 0x82,
712da668aa1SThomas Huth /* start of element 1 of q */ 0x01,
713da668aa1SThomas Huth /* b */ 0x00,
714da668aa1SThomas Huth /* u8 */ 0x41,
715da668aa1SThomas Huth /* end of q */ 0x00,
716da668aa1SThomas Huth /* i32 */ 0x00, 0x01, 0x11, 0x70,
717da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
718da668aa1SThomas Huth };
719da668aa1SThomas Huth
test_save_q(void)720da668aa1SThomas Huth static void test_save_q(void)
721da668aa1SThomas Huth {
722da668aa1SThomas Huth TestQtailq obj_q = {
723da668aa1SThomas Huth .i16 = -512,
724da668aa1SThomas Huth .i32 = 70000,
725da668aa1SThomas Huth };
726da668aa1SThomas Huth
727da668aa1SThomas Huth TestQtailqElement obj_qe1 = {
728da668aa1SThomas Huth .b = true,
729da668aa1SThomas Huth .u8 = 130,
730da668aa1SThomas Huth };
731da668aa1SThomas Huth
732da668aa1SThomas Huth TestQtailqElement obj_qe2 = {
733da668aa1SThomas Huth .b = false,
734da668aa1SThomas Huth .u8 = 65,
735da668aa1SThomas Huth };
736da668aa1SThomas Huth
737da668aa1SThomas Huth QTAILQ_INIT(&obj_q.q);
738da668aa1SThomas Huth QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
739da668aa1SThomas Huth QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
740da668aa1SThomas Huth
741da668aa1SThomas Huth save_vmstate(&vmstate_q, &obj_q);
742da668aa1SThomas Huth compare_vmstate(wire_q, sizeof(wire_q));
743da668aa1SThomas Huth }
744da668aa1SThomas Huth
test_load_q(void)745da668aa1SThomas Huth static void test_load_q(void)
746da668aa1SThomas Huth {
747da668aa1SThomas Huth TestQtailq obj_q = {
748da668aa1SThomas Huth .i16 = -512,
749da668aa1SThomas Huth .i32 = 70000,
750da668aa1SThomas Huth };
751da668aa1SThomas Huth
752da668aa1SThomas Huth TestQtailqElement obj_qe1 = {
753da668aa1SThomas Huth .b = true,
754da668aa1SThomas Huth .u8 = 130,
755da668aa1SThomas Huth };
756da668aa1SThomas Huth
757da668aa1SThomas Huth TestQtailqElement obj_qe2 = {
758da668aa1SThomas Huth .b = false,
759da668aa1SThomas Huth .u8 = 65,
760da668aa1SThomas Huth };
761da668aa1SThomas Huth
762da668aa1SThomas Huth QTAILQ_INIT(&obj_q.q);
763da668aa1SThomas Huth QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
764da668aa1SThomas Huth QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
765da668aa1SThomas Huth
766da668aa1SThomas Huth QEMUFile *fsave = open_test_file(true);
767da668aa1SThomas Huth
768da668aa1SThomas Huth qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
769da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
770da668aa1SThomas Huth qemu_fclose(fsave);
771da668aa1SThomas Huth
772da668aa1SThomas Huth QEMUFile *fload = open_test_file(false);
773da668aa1SThomas Huth TestQtailq tgt;
774da668aa1SThomas Huth
775da668aa1SThomas Huth QTAILQ_INIT(&tgt.q);
776da668aa1SThomas Huth vmstate_load_state(fload, &vmstate_q, &tgt, 1);
777da668aa1SThomas Huth char eof = qemu_get_byte(fload);
778da668aa1SThomas Huth g_assert(!qemu_file_get_error(fload));
779da668aa1SThomas Huth g_assert_cmpint(tgt.i16, ==, obj_q.i16);
780da668aa1SThomas Huth g_assert_cmpint(tgt.i32, ==, obj_q.i32);
781da668aa1SThomas Huth g_assert_cmpint(eof, ==, QEMU_VM_EOF);
782da668aa1SThomas Huth
783da668aa1SThomas Huth TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
784da668aa1SThomas Huth TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q);
785da668aa1SThomas Huth TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
786da668aa1SThomas Huth TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q);
787da668aa1SThomas Huth
788da668aa1SThomas Huth while (1) {
789da668aa1SThomas Huth g_assert_cmpint(qele_to->b, ==, qele_from->b);
790da668aa1SThomas Huth g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
791da668aa1SThomas Huth if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
792da668aa1SThomas Huth break;
793da668aa1SThomas Huth }
794da668aa1SThomas Huth qele_from = QTAILQ_NEXT(qele_from, next);
795da668aa1SThomas Huth qele_to = QTAILQ_NEXT(qele_to, next);
796da668aa1SThomas Huth }
797da668aa1SThomas Huth
798da668aa1SThomas Huth g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
799da668aa1SThomas Huth g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
800da668aa1SThomas Huth
801da668aa1SThomas Huth /* clean up */
802da668aa1SThomas Huth TestQtailqElement *qele;
803da668aa1SThomas Huth while (!QTAILQ_EMPTY(&tgt.q)) {
804da668aa1SThomas Huth qele = QTAILQ_LAST(&tgt.q);
805da668aa1SThomas Huth QTAILQ_REMOVE(&tgt.q, qele, next);
806da668aa1SThomas Huth free(qele);
807da668aa1SThomas Huth qele = NULL;
808da668aa1SThomas Huth }
809da668aa1SThomas Huth qemu_fclose(fload);
810da668aa1SThomas Huth }
811da668aa1SThomas Huth
812da668aa1SThomas Huth /* interval (key) */
813da668aa1SThomas Huth typedef struct TestGTreeInterval {
814da668aa1SThomas Huth uint64_t low;
815da668aa1SThomas Huth uint64_t high;
816da668aa1SThomas Huth } TestGTreeInterval;
817da668aa1SThomas Huth
818da668aa1SThomas Huth #define VMSTATE_INTERVAL \
819da668aa1SThomas Huth { \
820da668aa1SThomas Huth .name = "interval", \
821da668aa1SThomas Huth .version_id = 1, \
822da668aa1SThomas Huth .minimum_version_id = 1, \
823*57c73988SRichard Henderson .fields = (const VMStateField[]) { \
824da668aa1SThomas Huth VMSTATE_UINT64(low, TestGTreeInterval), \
825da668aa1SThomas Huth VMSTATE_UINT64(high, TestGTreeInterval), \
826da668aa1SThomas Huth VMSTATE_END_OF_LIST() \
827da668aa1SThomas Huth } \
828da668aa1SThomas Huth }
829da668aa1SThomas Huth
830da668aa1SThomas Huth /* mapping (value) */
831da668aa1SThomas Huth typedef struct TestGTreeMapping {
832da668aa1SThomas Huth uint64_t phys_addr;
833da668aa1SThomas Huth uint32_t flags;
834da668aa1SThomas Huth } TestGTreeMapping;
835da668aa1SThomas Huth
836da668aa1SThomas Huth #define VMSTATE_MAPPING \
837da668aa1SThomas Huth { \
838da668aa1SThomas Huth .name = "mapping", \
839da668aa1SThomas Huth .version_id = 1, \
840da668aa1SThomas Huth .minimum_version_id = 1, \
841*57c73988SRichard Henderson .fields = (const VMStateField[]) { \
842da668aa1SThomas Huth VMSTATE_UINT64(phys_addr, TestGTreeMapping), \
843da668aa1SThomas Huth VMSTATE_UINT32(flags, TestGTreeMapping), \
844da668aa1SThomas Huth VMSTATE_END_OF_LIST() \
845da668aa1SThomas Huth }, \
846da668aa1SThomas Huth }
847da668aa1SThomas Huth
848da668aa1SThomas Huth static const VMStateDescription vmstate_interval_mapping[2] = {
849da668aa1SThomas Huth VMSTATE_MAPPING, /* value */
850da668aa1SThomas Huth VMSTATE_INTERVAL /* key */
851da668aa1SThomas Huth };
852da668aa1SThomas Huth
853da668aa1SThomas Huth typedef struct TestGTreeDomain {
854da668aa1SThomas Huth int32_t id;
855da668aa1SThomas Huth GTree *mappings;
856da668aa1SThomas Huth } TestGTreeDomain;
857da668aa1SThomas Huth
858da668aa1SThomas Huth typedef struct TestGTreeIOMMU {
859da668aa1SThomas Huth int32_t id;
860da668aa1SThomas Huth GTree *domains;
861da668aa1SThomas Huth } TestGTreeIOMMU;
862da668aa1SThomas Huth
863da668aa1SThomas Huth /* Interval comparison function */
interval_cmp(gconstpointer a,gconstpointer b,gpointer user_data)864da668aa1SThomas Huth static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
865da668aa1SThomas Huth {
866da668aa1SThomas Huth TestGTreeInterval *inta = (TestGTreeInterval *)a;
867da668aa1SThomas Huth TestGTreeInterval *intb = (TestGTreeInterval *)b;
868da668aa1SThomas Huth
869da668aa1SThomas Huth if (inta->high < intb->low) {
870da668aa1SThomas Huth return -1;
871da668aa1SThomas Huth } else if (intb->high < inta->low) {
872da668aa1SThomas Huth return 1;
873da668aa1SThomas Huth } else {
874da668aa1SThomas Huth return 0;
875da668aa1SThomas Huth }
876da668aa1SThomas Huth }
877da668aa1SThomas Huth
878da668aa1SThomas Huth /* ID comparison function */
int_cmp(gconstpointer a,gconstpointer b,gpointer user_data)879da668aa1SThomas Huth static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
880da668aa1SThomas Huth {
881da668aa1SThomas Huth guint ua = GPOINTER_TO_UINT(a);
882da668aa1SThomas Huth guint ub = GPOINTER_TO_UINT(b);
883da668aa1SThomas Huth return (ua > ub) - (ua < ub);
884da668aa1SThomas Huth }
885da668aa1SThomas Huth
destroy_domain(gpointer data)886da668aa1SThomas Huth static void destroy_domain(gpointer data)
887da668aa1SThomas Huth {
888da668aa1SThomas Huth TestGTreeDomain *domain = (TestGTreeDomain *)data;
889da668aa1SThomas Huth
890da668aa1SThomas Huth g_tree_destroy(domain->mappings);
891da668aa1SThomas Huth g_free(domain);
892da668aa1SThomas Huth }
893da668aa1SThomas Huth
domain_preload(void * opaque)894da668aa1SThomas Huth static int domain_preload(void *opaque)
895da668aa1SThomas Huth {
896da668aa1SThomas Huth TestGTreeDomain *domain = opaque;
897da668aa1SThomas Huth
898da668aa1SThomas Huth domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
899da668aa1SThomas Huth NULL, g_free, g_free);
900da668aa1SThomas Huth return 0;
901da668aa1SThomas Huth }
902da668aa1SThomas Huth
iommu_preload(void * opaque)903da668aa1SThomas Huth static int iommu_preload(void *opaque)
904da668aa1SThomas Huth {
905da668aa1SThomas Huth TestGTreeIOMMU *iommu = opaque;
906da668aa1SThomas Huth
907da668aa1SThomas Huth iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp,
908da668aa1SThomas Huth NULL, NULL, destroy_domain);
909da668aa1SThomas Huth return 0;
910da668aa1SThomas Huth }
911da668aa1SThomas Huth
912da668aa1SThomas Huth static const VMStateDescription vmstate_domain = {
913da668aa1SThomas Huth .name = "domain",
914da668aa1SThomas Huth .version_id = 1,
915da668aa1SThomas Huth .minimum_version_id = 1,
916da668aa1SThomas Huth .pre_load = domain_preload,
917*57c73988SRichard Henderson .fields = (const VMStateField[]) {
918da668aa1SThomas Huth VMSTATE_INT32(id, TestGTreeDomain),
919da668aa1SThomas Huth VMSTATE_GTREE_V(mappings, TestGTreeDomain, 1,
920da668aa1SThomas Huth vmstate_interval_mapping,
921da668aa1SThomas Huth TestGTreeInterval, TestGTreeMapping),
922da668aa1SThomas Huth VMSTATE_END_OF_LIST()
923da668aa1SThomas Huth }
924da668aa1SThomas Huth };
925da668aa1SThomas Huth
926da668aa1SThomas Huth /* test QLIST Migration */
927da668aa1SThomas Huth
928da668aa1SThomas Huth typedef struct TestQListElement {
929da668aa1SThomas Huth uint32_t id;
930da668aa1SThomas Huth QLIST_ENTRY(TestQListElement) next;
931da668aa1SThomas Huth } TestQListElement;
932da668aa1SThomas Huth
933da668aa1SThomas Huth typedef struct TestQListContainer {
934da668aa1SThomas Huth uint32_t id;
935da668aa1SThomas Huth QLIST_HEAD(, TestQListElement) list;
936da668aa1SThomas Huth } TestQListContainer;
937da668aa1SThomas Huth
938da668aa1SThomas Huth static const VMStateDescription vmstate_qlist_element = {
939da668aa1SThomas Huth .name = "test/queue list",
940da668aa1SThomas Huth .version_id = 1,
941da668aa1SThomas Huth .minimum_version_id = 1,
942*57c73988SRichard Henderson .fields = (const VMStateField[]) {
943da668aa1SThomas Huth VMSTATE_UINT32(id, TestQListElement),
944da668aa1SThomas Huth VMSTATE_END_OF_LIST()
945da668aa1SThomas Huth }
946da668aa1SThomas Huth };
947da668aa1SThomas Huth
948da668aa1SThomas Huth static const VMStateDescription vmstate_iommu = {
949da668aa1SThomas Huth .name = "iommu",
950da668aa1SThomas Huth .version_id = 1,
951da668aa1SThomas Huth .minimum_version_id = 1,
952da668aa1SThomas Huth .pre_load = iommu_preload,
953*57c73988SRichard Henderson .fields = (const VMStateField[]) {
954da668aa1SThomas Huth VMSTATE_INT32(id, TestGTreeIOMMU),
955da668aa1SThomas Huth VMSTATE_GTREE_DIRECT_KEY_V(domains, TestGTreeIOMMU, 1,
956da668aa1SThomas Huth &vmstate_domain, TestGTreeDomain),
957da668aa1SThomas Huth VMSTATE_END_OF_LIST()
958da668aa1SThomas Huth }
959da668aa1SThomas Huth };
960da668aa1SThomas Huth
961da668aa1SThomas Huth static const VMStateDescription vmstate_container = {
962da668aa1SThomas Huth .name = "test/container/qlist",
963da668aa1SThomas Huth .version_id = 1,
964da668aa1SThomas Huth .minimum_version_id = 1,
965*57c73988SRichard Henderson .fields = (const VMStateField[]) {
966da668aa1SThomas Huth VMSTATE_UINT32(id, TestQListContainer),
967da668aa1SThomas Huth VMSTATE_QLIST_V(list, TestQListContainer, 1, vmstate_qlist_element,
968da668aa1SThomas Huth TestQListElement, next),
969da668aa1SThomas Huth VMSTATE_END_OF_LIST()
970da668aa1SThomas Huth }
971da668aa1SThomas Huth };
972da668aa1SThomas Huth
973da668aa1SThomas Huth uint8_t first_domain_dump[] = {
974da668aa1SThomas Huth /* id */
975da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x6,
976da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x2, /* 2 mappings */
977da668aa1SThomas Huth 0x1, /* start of a */
978da668aa1SThomas Huth /* a */
979da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
980da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
981da668aa1SThomas Huth /* map_a */
982da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
983da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x01,
984da668aa1SThomas Huth 0x1, /* start of b */
985da668aa1SThomas Huth /* b */
986da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
987da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
988da668aa1SThomas Huth /* map_b */
989da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
990da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x02,
991da668aa1SThomas Huth 0x0, /* end of gtree */
992da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
993da668aa1SThomas Huth };
994da668aa1SThomas Huth
create_first_domain(void)995da668aa1SThomas Huth static TestGTreeDomain *create_first_domain(void)
996da668aa1SThomas Huth {
997da668aa1SThomas Huth TestGTreeDomain *domain;
998da668aa1SThomas Huth TestGTreeMapping *map_a, *map_b;
999da668aa1SThomas Huth TestGTreeInterval *a, *b;
1000da668aa1SThomas Huth
1001b21e2380SMarkus Armbruster domain = g_new0(TestGTreeDomain, 1);
1002da668aa1SThomas Huth domain->id = 6;
1003da668aa1SThomas Huth
1004b21e2380SMarkus Armbruster a = g_new0(TestGTreeInterval, 1);
1005da668aa1SThomas Huth a->low = 0x1000;
1006da668aa1SThomas Huth a->high = 0x1FFF;
1007da668aa1SThomas Huth
1008b21e2380SMarkus Armbruster b = g_new0(TestGTreeInterval, 1);
1009da668aa1SThomas Huth b->low = 0x4000;
1010da668aa1SThomas Huth b->high = 0x4FFF;
1011da668aa1SThomas Huth
1012b21e2380SMarkus Armbruster map_a = g_new0(TestGTreeMapping, 1);
1013da668aa1SThomas Huth map_a->phys_addr = 0xa000;
1014da668aa1SThomas Huth map_a->flags = 1;
1015da668aa1SThomas Huth
1016b21e2380SMarkus Armbruster map_b = g_new0(TestGTreeMapping, 1);
1017da668aa1SThomas Huth map_b->phys_addr = 0xe0000;
1018da668aa1SThomas Huth map_b->flags = 2;
1019da668aa1SThomas Huth
1020da668aa1SThomas Huth domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp, NULL,
1021da668aa1SThomas Huth (GDestroyNotify)g_free,
1022da668aa1SThomas Huth (GDestroyNotify)g_free);
1023da668aa1SThomas Huth g_tree_insert(domain->mappings, a, map_a);
1024da668aa1SThomas Huth g_tree_insert(domain->mappings, b, map_b);
1025da668aa1SThomas Huth return domain;
1026da668aa1SThomas Huth }
1027da668aa1SThomas Huth
test_gtree_save_domain(void)1028da668aa1SThomas Huth static void test_gtree_save_domain(void)
1029da668aa1SThomas Huth {
1030da668aa1SThomas Huth TestGTreeDomain *first_domain = create_first_domain();
1031da668aa1SThomas Huth
1032da668aa1SThomas Huth save_vmstate(&vmstate_domain, first_domain);
1033da668aa1SThomas Huth compare_vmstate(first_domain_dump, sizeof(first_domain_dump));
1034da668aa1SThomas Huth destroy_domain(first_domain);
1035da668aa1SThomas Huth }
1036da668aa1SThomas Huth
1037da668aa1SThomas Huth struct match_node_data {
1038da668aa1SThomas Huth GTree *tree;
1039da668aa1SThomas Huth gpointer key;
1040da668aa1SThomas Huth gpointer value;
1041da668aa1SThomas Huth };
1042da668aa1SThomas Huth
1043da668aa1SThomas Huth struct tree_cmp_data {
1044da668aa1SThomas Huth GTree *tree1;
1045da668aa1SThomas Huth GTree *tree2;
1046da668aa1SThomas Huth GTraverseFunc match_node;
1047da668aa1SThomas Huth };
1048da668aa1SThomas Huth
match_interval_mapping_node(gpointer key,gpointer value,gpointer data)1049da668aa1SThomas Huth static gboolean match_interval_mapping_node(gpointer key,
1050da668aa1SThomas Huth gpointer value, gpointer data)
1051da668aa1SThomas Huth {
1052da668aa1SThomas Huth TestGTreeMapping *map_a, *map_b;
1053da668aa1SThomas Huth TestGTreeInterval *a, *b;
1054da668aa1SThomas Huth struct match_node_data *d = (struct match_node_data *)data;
1055da668aa1SThomas Huth a = (TestGTreeInterval *)key;
1056da668aa1SThomas Huth b = (TestGTreeInterval *)d->key;
1057da668aa1SThomas Huth
1058da668aa1SThomas Huth map_a = (TestGTreeMapping *)value;
1059da668aa1SThomas Huth map_b = (TestGTreeMapping *)d->value;
1060da668aa1SThomas Huth
1061da668aa1SThomas Huth assert(a->low == b->low);
1062da668aa1SThomas Huth assert(a->high == b->high);
1063da668aa1SThomas Huth assert(map_a->phys_addr == map_b->phys_addr);
1064da668aa1SThomas Huth assert(map_a->flags == map_b->flags);
1065da668aa1SThomas Huth g_tree_remove(d->tree, key);
1066da668aa1SThomas Huth return true;
1067da668aa1SThomas Huth }
1068da668aa1SThomas Huth
diff_tree(gpointer key,gpointer value,gpointer data)1069da668aa1SThomas Huth static gboolean diff_tree(gpointer key, gpointer value, gpointer data)
1070da668aa1SThomas Huth {
1071da668aa1SThomas Huth struct tree_cmp_data *tp = (struct tree_cmp_data *)data;
1072da668aa1SThomas Huth struct match_node_data d = {tp->tree2, key, value};
1073da668aa1SThomas Huth
1074da668aa1SThomas Huth g_tree_foreach(tp->tree2, tp->match_node, &d);
1075da668aa1SThomas Huth return false;
1076da668aa1SThomas Huth }
1077da668aa1SThomas Huth
compare_trees(GTree * tree1,GTree * tree2,GTraverseFunc function)1078da668aa1SThomas Huth static void compare_trees(GTree *tree1, GTree *tree2,
1079da668aa1SThomas Huth GTraverseFunc function)
1080da668aa1SThomas Huth {
1081da668aa1SThomas Huth struct tree_cmp_data tp = {tree1, tree2, function};
1082da668aa1SThomas Huth
1083abe2c4bdSEric Auger assert(g_tree_nnodes(tree1) == g_tree_nnodes(tree2));
1084da668aa1SThomas Huth g_tree_foreach(tree1, diff_tree, &tp);
1085abe2c4bdSEric Auger g_tree_destroy(g_tree_ref(tree1));
1086da668aa1SThomas Huth }
1087da668aa1SThomas Huth
diff_domain(TestGTreeDomain * d1,TestGTreeDomain * d2)1088da668aa1SThomas Huth static void diff_domain(TestGTreeDomain *d1, TestGTreeDomain *d2)
1089da668aa1SThomas Huth {
1090da668aa1SThomas Huth assert(d1->id == d2->id);
1091da668aa1SThomas Huth compare_trees(d1->mappings, d2->mappings, match_interval_mapping_node);
1092da668aa1SThomas Huth }
1093da668aa1SThomas Huth
match_domain_node(gpointer key,gpointer value,gpointer data)1094da668aa1SThomas Huth static gboolean match_domain_node(gpointer key, gpointer value, gpointer data)
1095da668aa1SThomas Huth {
1096da668aa1SThomas Huth uint64_t id1, id2;
1097da668aa1SThomas Huth TestGTreeDomain *d1, *d2;
1098da668aa1SThomas Huth struct match_node_data *d = (struct match_node_data *)data;
1099da668aa1SThomas Huth
1100da668aa1SThomas Huth id1 = (uint64_t)(uintptr_t)key;
1101da668aa1SThomas Huth id2 = (uint64_t)(uintptr_t)d->key;
1102da668aa1SThomas Huth d1 = (TestGTreeDomain *)value;
1103da668aa1SThomas Huth d2 = (TestGTreeDomain *)d->value;
1104da668aa1SThomas Huth assert(id1 == id2);
1105da668aa1SThomas Huth diff_domain(d1, d2);
1106da668aa1SThomas Huth g_tree_remove(d->tree, key);
1107da668aa1SThomas Huth return true;
1108da668aa1SThomas Huth }
1109da668aa1SThomas Huth
diff_iommu(TestGTreeIOMMU * iommu1,TestGTreeIOMMU * iommu2)1110da668aa1SThomas Huth static void diff_iommu(TestGTreeIOMMU *iommu1, TestGTreeIOMMU *iommu2)
1111da668aa1SThomas Huth {
1112da668aa1SThomas Huth assert(iommu1->id == iommu2->id);
1113da668aa1SThomas Huth compare_trees(iommu1->domains, iommu2->domains, match_domain_node);
1114da668aa1SThomas Huth }
1115da668aa1SThomas Huth
test_gtree_load_domain(void)1116da668aa1SThomas Huth static void test_gtree_load_domain(void)
1117da668aa1SThomas Huth {
1118b21e2380SMarkus Armbruster TestGTreeDomain *dest_domain = g_new0(TestGTreeDomain, 1);
1119da668aa1SThomas Huth TestGTreeDomain *orig_domain = create_first_domain();
1120da668aa1SThomas Huth QEMUFile *fload, *fsave;
1121da668aa1SThomas Huth char eof;
1122da668aa1SThomas Huth
1123da668aa1SThomas Huth fsave = open_test_file(true);
1124da668aa1SThomas Huth qemu_put_buffer(fsave, first_domain_dump, sizeof(first_domain_dump));
1125da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
1126da668aa1SThomas Huth qemu_fclose(fsave);
1127da668aa1SThomas Huth
1128da668aa1SThomas Huth fload = open_test_file(false);
1129da668aa1SThomas Huth
1130da668aa1SThomas Huth vmstate_load_state(fload, &vmstate_domain, dest_domain, 1);
1131da668aa1SThomas Huth eof = qemu_get_byte(fload);
1132da668aa1SThomas Huth g_assert(!qemu_file_get_error(fload));
1133da668aa1SThomas Huth g_assert_cmpint(orig_domain->id, ==, dest_domain->id);
1134da668aa1SThomas Huth g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1135da668aa1SThomas Huth
1136da668aa1SThomas Huth diff_domain(orig_domain, dest_domain);
1137da668aa1SThomas Huth destroy_domain(orig_domain);
1138da668aa1SThomas Huth destroy_domain(dest_domain);
1139da668aa1SThomas Huth qemu_fclose(fload);
1140da668aa1SThomas Huth }
1141da668aa1SThomas Huth
1142da668aa1SThomas Huth uint8_t iommu_dump[] = {
1143da668aa1SThomas Huth /* iommu id */
1144da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x7,
1145da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x2, /* 2 domains */
1146da668aa1SThomas Huth 0x1,/* start of domain 5 */
1147da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x5, /* key = 5 */
1148da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x5, /* domain1 id */
1149da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x1, /* 1 mapping */
1150da668aa1SThomas Huth 0x1, /* start of mappings */
1151da668aa1SThomas Huth /* c */
1152da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1153da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF,
1154da668aa1SThomas Huth /* map_c */
1155da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00,
1156da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x3,
1157da668aa1SThomas Huth 0x0, /* end of domain1 mappings*/
1158da668aa1SThomas Huth 0x1,/* start of domain 6 */
1159da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x6, /* key = 6 */
1160da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x6, /* domain6 id */
1161da668aa1SThomas Huth 0x00, 0x0, 0x0, 0x2, /* 2 mappings */
1162da668aa1SThomas Huth 0x1, /* start of a */
1163da668aa1SThomas Huth /* a */
1164da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
1165da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
1166da668aa1SThomas Huth /* map_a */
1167da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
1168da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x01,
1169da668aa1SThomas Huth 0x1, /* start of b */
1170da668aa1SThomas Huth /* b */
1171da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
1172da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
1173da668aa1SThomas Huth /* map_b */
1174da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
1175da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x02,
1176da668aa1SThomas Huth 0x0, /* end of domain6 mappings*/
1177da668aa1SThomas Huth 0x0, /* end of domains */
1178da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1179da668aa1SThomas Huth };
1180da668aa1SThomas Huth
create_iommu(void)1181da668aa1SThomas Huth static TestGTreeIOMMU *create_iommu(void)
1182da668aa1SThomas Huth {
1183b21e2380SMarkus Armbruster TestGTreeIOMMU *iommu = g_new0(TestGTreeIOMMU, 1);
1184da668aa1SThomas Huth TestGTreeDomain *first_domain = create_first_domain();
1185da668aa1SThomas Huth TestGTreeDomain *second_domain;
1186da668aa1SThomas Huth TestGTreeMapping *map_c;
1187da668aa1SThomas Huth TestGTreeInterval *c;
1188da668aa1SThomas Huth
1189da668aa1SThomas Huth iommu->id = 7;
1190da668aa1SThomas Huth iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp, NULL,
1191da668aa1SThomas Huth NULL,
1192da668aa1SThomas Huth destroy_domain);
1193da668aa1SThomas Huth
1194b21e2380SMarkus Armbruster second_domain = g_new0(TestGTreeDomain, 1);
1195da668aa1SThomas Huth second_domain->id = 5;
1196da668aa1SThomas Huth second_domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
1197da668aa1SThomas Huth NULL,
1198da668aa1SThomas Huth (GDestroyNotify)g_free,
1199da668aa1SThomas Huth (GDestroyNotify)g_free);
1200da668aa1SThomas Huth
1201da668aa1SThomas Huth g_tree_insert(iommu->domains, GUINT_TO_POINTER(6), first_domain);
1202da668aa1SThomas Huth g_tree_insert(iommu->domains, (gpointer)0x0000000000000005, second_domain);
1203da668aa1SThomas Huth
1204b21e2380SMarkus Armbruster c = g_new0(TestGTreeInterval, 1);
1205da668aa1SThomas Huth c->low = 0x1000000;
1206da668aa1SThomas Huth c->high = 0x1FFFFFF;
1207da668aa1SThomas Huth
1208b21e2380SMarkus Armbruster map_c = g_new0(TestGTreeMapping, 1);
1209da668aa1SThomas Huth map_c->phys_addr = 0xF000000;
1210da668aa1SThomas Huth map_c->flags = 0x3;
1211da668aa1SThomas Huth
1212da668aa1SThomas Huth g_tree_insert(second_domain->mappings, c, map_c);
1213da668aa1SThomas Huth return iommu;
1214da668aa1SThomas Huth }
1215da668aa1SThomas Huth
destroy_iommu(TestGTreeIOMMU * iommu)1216da668aa1SThomas Huth static void destroy_iommu(TestGTreeIOMMU *iommu)
1217da668aa1SThomas Huth {
1218da668aa1SThomas Huth g_tree_destroy(iommu->domains);
1219da668aa1SThomas Huth g_free(iommu);
1220da668aa1SThomas Huth }
1221da668aa1SThomas Huth
test_gtree_save_iommu(void)1222da668aa1SThomas Huth static void test_gtree_save_iommu(void)
1223da668aa1SThomas Huth {
1224da668aa1SThomas Huth TestGTreeIOMMU *iommu = create_iommu();
1225da668aa1SThomas Huth
1226da668aa1SThomas Huth save_vmstate(&vmstate_iommu, iommu);
1227da668aa1SThomas Huth compare_vmstate(iommu_dump, sizeof(iommu_dump));
1228da668aa1SThomas Huth destroy_iommu(iommu);
1229da668aa1SThomas Huth }
1230da668aa1SThomas Huth
test_gtree_load_iommu(void)1231da668aa1SThomas Huth static void test_gtree_load_iommu(void)
1232da668aa1SThomas Huth {
1233b21e2380SMarkus Armbruster TestGTreeIOMMU *dest_iommu = g_new0(TestGTreeIOMMU, 1);
1234da668aa1SThomas Huth TestGTreeIOMMU *orig_iommu = create_iommu();
1235da668aa1SThomas Huth QEMUFile *fsave, *fload;
1236da668aa1SThomas Huth char eof;
1237da668aa1SThomas Huth
1238da668aa1SThomas Huth fsave = open_test_file(true);
1239da668aa1SThomas Huth qemu_put_buffer(fsave, iommu_dump, sizeof(iommu_dump));
1240da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
1241da668aa1SThomas Huth qemu_fclose(fsave);
1242da668aa1SThomas Huth
1243da668aa1SThomas Huth fload = open_test_file(false);
1244da668aa1SThomas Huth vmstate_load_state(fload, &vmstate_iommu, dest_iommu, 1);
1245da668aa1SThomas Huth eof = qemu_get_byte(fload);
1246da668aa1SThomas Huth g_assert(!qemu_file_get_error(fload));
1247da668aa1SThomas Huth g_assert_cmpint(orig_iommu->id, ==, dest_iommu->id);
1248da668aa1SThomas Huth g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1249da668aa1SThomas Huth
1250da668aa1SThomas Huth diff_iommu(orig_iommu, dest_iommu);
1251da668aa1SThomas Huth destroy_iommu(orig_iommu);
1252da668aa1SThomas Huth destroy_iommu(dest_iommu);
1253da668aa1SThomas Huth qemu_fclose(fload);
1254da668aa1SThomas Huth }
1255da668aa1SThomas Huth
1256da668aa1SThomas Huth static uint8_t qlist_dump[] = {
1257da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x01, /* container id */
1258da668aa1SThomas Huth 0x1, /* start of a */
1259da668aa1SThomas Huth 0x00, 0x00, 0x00, 0x0a,
1260da668aa1SThomas Huth 0x1, /* start of b */
1261da668aa1SThomas Huth 0x00, 0x00, 0x0b, 0x00,
1262da668aa1SThomas Huth 0x1, /* start of c */
1263da668aa1SThomas Huth 0x00, 0x0c, 0x00, 0x00,
1264da668aa1SThomas Huth 0x1, /* start of d */
1265da668aa1SThomas Huth 0x0d, 0x00, 0x00, 0x00,
1266da668aa1SThomas Huth 0x0, /* end of list */
1267da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1268da668aa1SThomas Huth };
1269da668aa1SThomas Huth
alloc_container(void)1270da668aa1SThomas Huth static TestQListContainer *alloc_container(void)
1271da668aa1SThomas Huth {
1272b21e2380SMarkus Armbruster TestQListElement *a = g_new(TestQListElement, 1);
1273b21e2380SMarkus Armbruster TestQListElement *b = g_new(TestQListElement, 1);
1274b21e2380SMarkus Armbruster TestQListElement *c = g_new(TestQListElement, 1);
1275b21e2380SMarkus Armbruster TestQListElement *d = g_new(TestQListElement, 1);
1276b21e2380SMarkus Armbruster TestQListContainer *container = g_new(TestQListContainer, 1);
1277da668aa1SThomas Huth
1278da668aa1SThomas Huth a->id = 0x0a;
1279da668aa1SThomas Huth b->id = 0x0b00;
1280da668aa1SThomas Huth c->id = 0xc0000;
1281da668aa1SThomas Huth d->id = 0xd000000;
1282da668aa1SThomas Huth container->id = 1;
1283da668aa1SThomas Huth
1284da668aa1SThomas Huth QLIST_INIT(&container->list);
1285da668aa1SThomas Huth QLIST_INSERT_HEAD(&container->list, d, next);
1286da668aa1SThomas Huth QLIST_INSERT_HEAD(&container->list, c, next);
1287da668aa1SThomas Huth QLIST_INSERT_HEAD(&container->list, b, next);
1288da668aa1SThomas Huth QLIST_INSERT_HEAD(&container->list, a, next);
1289da668aa1SThomas Huth return container;
1290da668aa1SThomas Huth }
1291da668aa1SThomas Huth
free_container(TestQListContainer * container)1292da668aa1SThomas Huth static void free_container(TestQListContainer *container)
1293da668aa1SThomas Huth {
1294da668aa1SThomas Huth TestQListElement *iter, *tmp;
1295da668aa1SThomas Huth
1296da668aa1SThomas Huth QLIST_FOREACH_SAFE(iter, &container->list, next, tmp) {
1297da668aa1SThomas Huth QLIST_REMOVE(iter, next);
1298da668aa1SThomas Huth g_free(iter);
1299da668aa1SThomas Huth }
1300da668aa1SThomas Huth g_free(container);
1301da668aa1SThomas Huth }
1302da668aa1SThomas Huth
compare_containers(TestQListContainer * c1,TestQListContainer * c2)1303da668aa1SThomas Huth static void compare_containers(TestQListContainer *c1, TestQListContainer *c2)
1304da668aa1SThomas Huth {
1305da668aa1SThomas Huth TestQListElement *first_item_c1, *first_item_c2;
1306da668aa1SThomas Huth
1307da668aa1SThomas Huth while (!QLIST_EMPTY(&c1->list)) {
1308da668aa1SThomas Huth first_item_c1 = QLIST_FIRST(&c1->list);
1309da668aa1SThomas Huth first_item_c2 = QLIST_FIRST(&c2->list);
1310da668aa1SThomas Huth assert(first_item_c2);
1311da668aa1SThomas Huth assert(first_item_c1->id == first_item_c2->id);
1312da668aa1SThomas Huth QLIST_REMOVE(first_item_c1, next);
1313da668aa1SThomas Huth QLIST_REMOVE(first_item_c2, next);
1314da668aa1SThomas Huth g_free(first_item_c1);
1315da668aa1SThomas Huth g_free(first_item_c2);
1316da668aa1SThomas Huth }
1317da668aa1SThomas Huth assert(QLIST_EMPTY(&c2->list));
1318da668aa1SThomas Huth }
1319da668aa1SThomas Huth
1320da668aa1SThomas Huth /*
1321da668aa1SThomas Huth * Check the prev & next fields are correct by doing list
1322da668aa1SThomas Huth * manipulations on the container. We will do that for both
1323da668aa1SThomas Huth * the source and the destination containers
1324da668aa1SThomas Huth */
manipulate_container(TestQListContainer * c)1325da668aa1SThomas Huth static void manipulate_container(TestQListContainer *c)
1326da668aa1SThomas Huth {
1327da668aa1SThomas Huth TestQListElement *prev = NULL, *iter = QLIST_FIRST(&c->list);
1328da668aa1SThomas Huth TestQListElement *elem;
1329da668aa1SThomas Huth
1330b21e2380SMarkus Armbruster elem = g_new(TestQListElement, 1);
1331da668aa1SThomas Huth elem->id = 0x12;
1332da668aa1SThomas Huth QLIST_INSERT_AFTER(iter, elem, next);
1333da668aa1SThomas Huth
1334b21e2380SMarkus Armbruster elem = g_new(TestQListElement, 1);
1335da668aa1SThomas Huth elem->id = 0x13;
1336da668aa1SThomas Huth QLIST_INSERT_HEAD(&c->list, elem, next);
1337da668aa1SThomas Huth
1338da668aa1SThomas Huth while (iter) {
1339da668aa1SThomas Huth prev = iter;
1340da668aa1SThomas Huth iter = QLIST_NEXT(iter, next);
1341da668aa1SThomas Huth }
1342da668aa1SThomas Huth
1343b21e2380SMarkus Armbruster elem = g_new(TestQListElement, 1);
1344da668aa1SThomas Huth elem->id = 0x14;
1345da668aa1SThomas Huth QLIST_INSERT_BEFORE(prev, elem, next);
1346da668aa1SThomas Huth
1347b21e2380SMarkus Armbruster elem = g_new(TestQListElement, 1);
1348da668aa1SThomas Huth elem->id = 0x15;
1349da668aa1SThomas Huth QLIST_INSERT_AFTER(prev, elem, next);
1350da668aa1SThomas Huth
1351da668aa1SThomas Huth QLIST_REMOVE(prev, next);
1352da668aa1SThomas Huth g_free(prev);
1353da668aa1SThomas Huth }
1354da668aa1SThomas Huth
test_save_qlist(void)1355da668aa1SThomas Huth static void test_save_qlist(void)
1356da668aa1SThomas Huth {
1357da668aa1SThomas Huth TestQListContainer *container = alloc_container();
1358da668aa1SThomas Huth
1359da668aa1SThomas Huth save_vmstate(&vmstate_container, container);
1360da668aa1SThomas Huth compare_vmstate(qlist_dump, sizeof(qlist_dump));
1361da668aa1SThomas Huth free_container(container);
1362da668aa1SThomas Huth }
1363da668aa1SThomas Huth
test_load_qlist(void)1364da668aa1SThomas Huth static void test_load_qlist(void)
1365da668aa1SThomas Huth {
1366da668aa1SThomas Huth QEMUFile *fsave, *fload;
1367da668aa1SThomas Huth TestQListContainer *orig_container = alloc_container();
1368b21e2380SMarkus Armbruster TestQListContainer *dest_container = g_new0(TestQListContainer, 1);
1369da668aa1SThomas Huth char eof;
1370da668aa1SThomas Huth
1371da668aa1SThomas Huth QLIST_INIT(&dest_container->list);
1372da668aa1SThomas Huth
1373da668aa1SThomas Huth fsave = open_test_file(true);
1374da668aa1SThomas Huth qemu_put_buffer(fsave, qlist_dump, sizeof(qlist_dump));
1375da668aa1SThomas Huth g_assert(!qemu_file_get_error(fsave));
1376da668aa1SThomas Huth qemu_fclose(fsave);
1377da668aa1SThomas Huth
1378da668aa1SThomas Huth fload = open_test_file(false);
1379da668aa1SThomas Huth vmstate_load_state(fload, &vmstate_container, dest_container, 1);
1380da668aa1SThomas Huth eof = qemu_get_byte(fload);
1381da668aa1SThomas Huth g_assert(!qemu_file_get_error(fload));
1382da668aa1SThomas Huth g_assert_cmpint(eof, ==, QEMU_VM_EOF);
1383da668aa1SThomas Huth manipulate_container(orig_container);
1384da668aa1SThomas Huth manipulate_container(dest_container);
1385da668aa1SThomas Huth compare_containers(orig_container, dest_container);
1386da668aa1SThomas Huth free_container(orig_container);
1387da668aa1SThomas Huth free_container(dest_container);
1388da668aa1SThomas Huth qemu_fclose(fload);
1389da668aa1SThomas Huth }
1390da668aa1SThomas Huth
1391da668aa1SThomas Huth typedef struct TmpTestStruct {
1392da668aa1SThomas Huth TestStruct *parent;
1393da668aa1SThomas Huth int64_t diff;
1394da668aa1SThomas Huth } TmpTestStruct;
1395da668aa1SThomas Huth
tmp_child_pre_save(void * opaque)1396da668aa1SThomas Huth static int tmp_child_pre_save(void *opaque)
1397da668aa1SThomas Huth {
1398da668aa1SThomas Huth struct TmpTestStruct *tts = opaque;
1399da668aa1SThomas Huth
1400da668aa1SThomas Huth tts->diff = tts->parent->b - tts->parent->a;
1401da668aa1SThomas Huth
1402da668aa1SThomas Huth return 0;
1403da668aa1SThomas Huth }
1404da668aa1SThomas Huth
tmp_child_post_load(void * opaque,int version_id)1405da668aa1SThomas Huth static int tmp_child_post_load(void *opaque, int version_id)
1406da668aa1SThomas Huth {
1407da668aa1SThomas Huth struct TmpTestStruct *tts = opaque;
1408da668aa1SThomas Huth
1409da668aa1SThomas Huth tts->parent->b = tts->parent->a + tts->diff;
1410da668aa1SThomas Huth
1411da668aa1SThomas Huth return 0;
1412da668aa1SThomas Huth }
1413da668aa1SThomas Huth
1414da668aa1SThomas Huth static const VMStateDescription vmstate_tmp_back_to_parent = {
1415da668aa1SThomas Huth .name = "test/tmp_child_parent",
1416*57c73988SRichard Henderson .fields = (const VMStateField[]) {
1417da668aa1SThomas Huth VMSTATE_UINT64(f, TestStruct),
1418da668aa1SThomas Huth VMSTATE_END_OF_LIST()
1419da668aa1SThomas Huth }
1420da668aa1SThomas Huth };
1421da668aa1SThomas Huth
1422da668aa1SThomas Huth static const VMStateDescription vmstate_tmp_child = {
1423da668aa1SThomas Huth .name = "test/tmp_child",
1424da668aa1SThomas Huth .pre_save = tmp_child_pre_save,
1425da668aa1SThomas Huth .post_load = tmp_child_post_load,
1426*57c73988SRichard Henderson .fields = (const VMStateField[]) {
1427da668aa1SThomas Huth VMSTATE_INT64(diff, TmpTestStruct),
1428da668aa1SThomas Huth VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
1429da668aa1SThomas Huth vmstate_tmp_back_to_parent, TestStruct),
1430da668aa1SThomas Huth VMSTATE_END_OF_LIST()
1431da668aa1SThomas Huth }
1432da668aa1SThomas Huth };
1433da668aa1SThomas Huth
1434da668aa1SThomas Huth static const VMStateDescription vmstate_with_tmp = {
1435da668aa1SThomas Huth .name = "test/with_tmp",
1436da668aa1SThomas Huth .version_id = 1,
1437*57c73988SRichard Henderson .fields = (const VMStateField[]) {
1438da668aa1SThomas Huth VMSTATE_UINT32(a, TestStruct),
1439da668aa1SThomas Huth VMSTATE_UINT64(d, TestStruct),
1440da668aa1SThomas Huth VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
1441da668aa1SThomas Huth VMSTATE_END_OF_LIST()
1442da668aa1SThomas Huth }
1443da668aa1SThomas Huth };
1444da668aa1SThomas Huth
obj_tmp_copy(void * target,void * source)1445da668aa1SThomas Huth static void obj_tmp_copy(void *target, void *source)
1446da668aa1SThomas Huth {
1447da668aa1SThomas Huth memcpy(target, source, sizeof(TestStruct));
1448da668aa1SThomas Huth }
1449da668aa1SThomas Huth
test_tmp_struct(void)1450da668aa1SThomas Huth static void test_tmp_struct(void)
1451da668aa1SThomas Huth {
1452da668aa1SThomas Huth TestStruct obj, obj_clone;
1453da668aa1SThomas Huth
1454da668aa1SThomas Huth uint8_t const wire_with_tmp[] = {
1455da668aa1SThomas Huth /* u32 a */ 0x00, 0x00, 0x00, 0x02,
1456da668aa1SThomas Huth /* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1457da668aa1SThomas Huth /* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
1458da668aa1SThomas Huth /* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
1459da668aa1SThomas Huth QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
1460da668aa1SThomas Huth };
1461da668aa1SThomas Huth
1462da668aa1SThomas Huth memset(&obj, 0, sizeof(obj));
1463da668aa1SThomas Huth obj.a = 2;
1464da668aa1SThomas Huth obj.b = 4;
1465da668aa1SThomas Huth obj.d = 1;
1466da668aa1SThomas Huth obj.f = 8;
1467da668aa1SThomas Huth save_vmstate(&vmstate_with_tmp, &obj);
1468da668aa1SThomas Huth
1469da668aa1SThomas Huth compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
1470da668aa1SThomas Huth
1471da668aa1SThomas Huth memset(&obj, 0, sizeof(obj));
1472da668aa1SThomas Huth SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
1473da668aa1SThomas Huth obj_tmp_copy, 1, wire_with_tmp,
1474da668aa1SThomas Huth sizeof(wire_with_tmp)));
1475da668aa1SThomas Huth g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
1476da668aa1SThomas Huth g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
1477da668aa1SThomas Huth g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
1478da668aa1SThomas Huth g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
1479da668aa1SThomas Huth }
1480da668aa1SThomas Huth
main(int argc,char ** argv)1481da668aa1SThomas Huth int main(int argc, char **argv)
1482da668aa1SThomas Huth {
1483da668aa1SThomas Huth g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
1484da668aa1SThomas Huth g_get_tmp_dir());
1485da668aa1SThomas Huth temp_fd = mkstemp(temp_file);
14861c861885SPeter Maydell g_assert(temp_fd >= 0);
1487da668aa1SThomas Huth
1488da668aa1SThomas Huth module_call_init(MODULE_INIT_QOM);
1489da668aa1SThomas Huth
1490da668aa1SThomas Huth g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1491da668aa1SThomas Huth
1492da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
1493da668aa1SThomas Huth g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
1494da668aa1SThomas Huth g_test_add_func("/vmstate/simple/array", test_simple_array);
1495da668aa1SThomas Huth g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
1496da668aa1SThomas Huth g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
1497da668aa1SThomas Huth g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
1498da668aa1SThomas Huth g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
1499da668aa1SThomas Huth g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
1500da668aa1SThomas Huth g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
1501da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/str/no0/save",
1502da668aa1SThomas Huth test_arr_ptr_str_no0_save);
1503da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/str/no0/load",
1504da668aa1SThomas Huth test_arr_ptr_str_no0_load);
1505da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
1506da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/str/0/load",
1507da668aa1SThomas Huth test_arr_ptr_str_0_load);
1508da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/prim/0/save",
1509da668aa1SThomas Huth test_arr_ptr_prim_0_save);
1510da668aa1SThomas Huth g_test_add_func("/vmstate/array/ptr/prim/0/load",
1511da668aa1SThomas Huth test_arr_ptr_prim_0_load);
1512da668aa1SThomas Huth g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
1513da668aa1SThomas Huth g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
1514da668aa1SThomas Huth g_test_add_func("/vmstate/gtree/save/savedomain", test_gtree_save_domain);
1515da668aa1SThomas Huth g_test_add_func("/vmstate/gtree/load/loaddomain", test_gtree_load_domain);
1516da668aa1SThomas Huth g_test_add_func("/vmstate/gtree/save/saveiommu", test_gtree_save_iommu);
1517da668aa1SThomas Huth g_test_add_func("/vmstate/gtree/load/loadiommu", test_gtree_load_iommu);
1518da668aa1SThomas Huth g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist);
1519da668aa1SThomas Huth g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist);
1520da668aa1SThomas Huth g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
1521da668aa1SThomas Huth g_test_run();
1522da668aa1SThomas Huth
1523da668aa1SThomas Huth close(temp_fd);
1524da668aa1SThomas Huth unlink(temp_file);
1525da668aa1SThomas Huth
1526da668aa1SThomas Huth return 0;
1527da668aa1SThomas Huth }
1528