2013-11-28 15:01:18 +01:00
|
|
|
/*
|
|
|
|
* Test code for VMState
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-11-28 15:01:18 +01:00
|
|
|
|
|
|
|
#include "migration/vmstate.h"
|
2017-04-20 18:52:18 +02:00
|
|
|
#include "migration/qemu-file-types.h"
|
|
|
|
#include "../migration/qemu-file.h"
|
2017-04-24 17:36:46 +02:00
|
|
|
#include "../migration/savevm.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2016-04-27 12:05:08 +02:00
|
|
|
#include "io/channel-file.h"
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-07-07 21:03:38 +02:00
|
|
|
static int temp_fd;
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-10-10 13:39:05 +02:00
|
|
|
|
2013-11-28 15:01:18 +01:00
|
|
|
/* Duplicate temp_fd and seek to the beginning of the file */
|
2014-04-05 18:34:36 +02:00
|
|
|
static QEMUFile *open_test_file(bool write)
|
2013-11-28 15:01:18 +01:00
|
|
|
{
|
2021-05-25 15:44:58 +02:00
|
|
|
int fd;
|
2016-04-27 12:05:08 +02:00
|
|
|
QIOChannel *ioc;
|
2016-07-07 20:48:06 +02:00
|
|
|
QEMUFile *f;
|
|
|
|
|
2021-05-25 15:44:58 +02:00
|
|
|
fd = dup(temp_fd);
|
|
|
|
g_assert(fd >= 0);
|
2013-11-28 15:01:18 +01:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2014-04-05 18:34:36 +02:00
|
|
|
if (write) {
|
2013-11-28 15:01:18 +01:00
|
|
|
g_assert_cmpint(ftruncate(fd, 0), ==, 0);
|
|
|
|
}
|
2016-04-27 12:05:08 +02:00
|
|
|
ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd));
|
|
|
|
if (write) {
|
2022-06-20 13:02:05 +02:00
|
|
|
f = qemu_file_new_output(ioc);
|
2016-04-27 12:05:08 +02:00
|
|
|
} else {
|
2022-06-20 13:02:05 +02:00
|
|
|
f = qemu_file_new_input(ioc);
|
2016-04-27 12:05:08 +02:00
|
|
|
}
|
2016-07-07 20:48:06 +02:00
|
|
|
object_unref(OBJECT(ioc));
|
|
|
|
return f;
|
2013-11-28 15:01:18 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
#define SUCCESS(val) \
|
|
|
|
g_assert_cmpint((val), ==, 0)
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
#define FAILURE(val) \
|
|
|
|
g_assert_cmpint((val), !=, 0)
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
static void save_vmstate(const VMStateDescription *desc, void *obj)
|
|
|
|
{
|
|
|
|
QEMUFile *f = open_test_file(true);
|
|
|
|
|
|
|
|
/* Save file with vmstate */
|
2017-09-25 13:29:17 +02:00
|
|
|
int ret = vmstate_save_state(f, desc, obj, NULL);
|
|
|
|
g_assert(!ret);
|
2014-04-05 20:12:51 +02:00
|
|
|
qemu_put_byte(f, QEMU_VM_EOF);
|
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
qemu_fclose(f);
|
|
|
|
}
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2016-10-21 16:37:38 +02:00
|
|
|
static void save_buffer(const uint8_t *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
QEMUFile *fsave = open_test_file(true);
|
|
|
|
qemu_put_buffer(fsave, buf, buf_size);
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:06:50 +01:00
|
|
|
static void compare_vmstate(const uint8_t *wire, size_t size)
|
2013-11-28 15:01:18 +01:00
|
|
|
{
|
2014-04-05 20:12:51 +02:00
|
|
|
QEMUFile *f = open_test_file(false);
|
2022-08-19 17:39:31 +02:00
|
|
|
g_autofree uint8_t *result = g_malloc(size);
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
/* read back as binary */
|
|
|
|
|
2022-08-19 17:39:31 +02:00
|
|
|
g_assert_cmpint(qemu_get_buffer(f, result, size), ==, size);
|
2014-04-05 20:12:51 +02:00
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
|
|
|
|
/* Compare that what is on the file is the same that what we
|
|
|
|
expected to be there */
|
2022-08-19 17:39:31 +02:00
|
|
|
SUCCESS(memcmp(result, wire, size));
|
2013-11-28 15:01:18 +01:00
|
|
|
|
|
|
|
/* Must reach EOF */
|
2014-04-05 20:12:51 +02:00
|
|
|
qemu_get_byte(f);
|
|
|
|
g_assert_cmpint(qemu_file_get_error(f), ==, -EIO);
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
qemu_fclose(f);
|
2013-11-28 15:01:18 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
static int load_vmstate_one(const VMStateDescription *desc, void *obj,
|
2017-02-03 17:06:50 +01:00
|
|
|
int version, const uint8_t *wire, size_t size)
|
2013-11-28 15:01:18 +01:00
|
|
|
{
|
2014-04-05 20:12:51 +02:00
|
|
|
QEMUFile *f;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
f = open_test_file(true);
|
|
|
|
qemu_put_buffer(f, wire, size);
|
|
|
|
qemu_fclose(f);
|
|
|
|
|
|
|
|
f = open_test_file(false);
|
|
|
|
ret = vmstate_load_state(f, desc, obj, version);
|
|
|
|
if (ret) {
|
|
|
|
g_assert(qemu_file_get_error(f));
|
|
|
|
} else{
|
|
|
|
g_assert(!qemu_file_get_error(f));
|
|
|
|
}
|
|
|
|
qemu_fclose(f);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
|
|
|
|
static int load_vmstate(const VMStateDescription *desc,
|
|
|
|
void *obj, void *obj_clone,
|
|
|
|
void (*obj_copy)(void *, void*),
|
2017-02-03 17:06:50 +01:00
|
|
|
int version, const uint8_t *wire, size_t size)
|
2014-04-05 20:12:51 +02:00
|
|
|
{
|
|
|
|
/* We test with zero size */
|
|
|
|
obj_copy(obj_clone, obj);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, 0));
|
|
|
|
|
|
|
|
/* Stream ends with QEMU_EOF, so we need at least 3 bytes to be
|
|
|
|
* able to test in the middle */
|
|
|
|
|
|
|
|
if (size > 3) {
|
|
|
|
|
|
|
|
/* We test with size - 2. We can't test size - 1 due to EOF tricks */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, size - 2));
|
|
|
|
|
|
|
|
/* Test with size/2, first half of real state */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire, size/2));
|
|
|
|
|
|
|
|
/* Test with size/2, second half of real state */
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
FAILURE(load_vmstate_one(desc, obj, version, wire + (size/2), size/2));
|
|
|
|
|
|
|
|
}
|
|
|
|
obj_copy(obj, obj_clone);
|
|
|
|
return load_vmstate_one(desc, obj, version, wire, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test struct that we are going to use for our tests */
|
|
|
|
|
|
|
|
typedef struct TestSimple {
|
|
|
|
bool b_1, b_2;
|
|
|
|
uint8_t u8_1;
|
|
|
|
uint16_t u16_1;
|
|
|
|
uint32_t u32_1;
|
|
|
|
uint64_t u64_1;
|
|
|
|
int8_t i8_1, i8_2;
|
|
|
|
int16_t i16_1, i16_2;
|
|
|
|
int32_t i32_1, i32_2;
|
|
|
|
int64_t i64_1, i64_2;
|
|
|
|
} TestSimple;
|
|
|
|
|
|
|
|
/* Object instantiation, we are going to use it in more than one test */
|
|
|
|
|
|
|
|
TestSimple obj_simple = {
|
|
|
|
.b_1 = true,
|
|
|
|
.b_2 = false,
|
|
|
|
.u8_1 = 130,
|
|
|
|
.u16_1 = 512,
|
|
|
|
.u32_1 = 70000,
|
|
|
|
.u64_1 = 12121212,
|
|
|
|
.i8_1 = 65,
|
|
|
|
.i8_2 = -65,
|
|
|
|
.i16_1 = 512,
|
|
|
|
.i16_2 = -512,
|
|
|
|
.i32_1 = 70000,
|
|
|
|
.i32_2 = -70000,
|
|
|
|
.i64_1 = 12121212,
|
|
|
|
.i64_2 = -12121212,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Description of the values. If you add a primitive type
|
|
|
|
you are expected to add a test here */
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_simple_primitive = {
|
|
|
|
.name = "simple/primitive",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2014-04-05 20:12:51 +02:00
|
|
|
VMSTATE_BOOL(b_1, TestSimple),
|
|
|
|
VMSTATE_BOOL(b_2, TestSimple),
|
|
|
|
VMSTATE_UINT8(u8_1, TestSimple),
|
|
|
|
VMSTATE_UINT16(u16_1, TestSimple),
|
|
|
|
VMSTATE_UINT32(u32_1, TestSimple),
|
|
|
|
VMSTATE_UINT64(u64_1, TestSimple),
|
|
|
|
VMSTATE_INT8(i8_1, TestSimple),
|
|
|
|
VMSTATE_INT8(i8_2, TestSimple),
|
|
|
|
VMSTATE_INT16(i16_1, TestSimple),
|
|
|
|
VMSTATE_INT16(i16_2, TestSimple),
|
|
|
|
VMSTATE_INT32(i32_1, TestSimple),
|
|
|
|
VMSTATE_INT32(i32_2, TestSimple),
|
|
|
|
VMSTATE_INT64(i64_1, TestSimple),
|
|
|
|
VMSTATE_INT64(i64_2, TestSimple),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* It describes what goes through the wire. Our tests are basically:
|
|
|
|
|
|
|
|
* save test
|
|
|
|
- save a struct a vmstate to a file
|
|
|
|
- read that file back (binary read, no vmstate)
|
|
|
|
- compare it with what we expect to be on the wire
|
|
|
|
* load test
|
|
|
|
- save to the file what we expect to be on the wire
|
|
|
|
- read struct back with vmstate in a different
|
|
|
|
- compare back with the original struct
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint8_t wire_simple_primitive[] = {
|
|
|
|
/* b_1 */ 0x01,
|
|
|
|
/* b_2 */ 0x00,
|
|
|
|
/* u8_1 */ 0x82,
|
|
|
|
/* u16_1 */ 0x02, 0x00,
|
|
|
|
/* u32_1 */ 0x00, 0x01, 0x11, 0x70,
|
|
|
|
/* u64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
|
|
|
|
/* i8_1 */ 0x41,
|
|
|
|
/* i8_2 */ 0xbf,
|
|
|
|
/* i16_1 */ 0x02, 0x00,
|
|
|
|
/* i16_2 */ 0xfe, 0x0,
|
|
|
|
/* i32_1 */ 0x00, 0x01, 0x11, 0x70,
|
|
|
|
/* i32_2 */ 0xff, 0xfe, 0xee, 0x90,
|
|
|
|
/* i64_1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0xf4, 0x7c,
|
|
|
|
/* i64_2 */ 0xff, 0xff, 0xff, 0xff, 0xff, 0x47, 0x0b, 0x84,
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void obj_simple_copy(void *target, void *source)
|
|
|
|
{
|
|
|
|
memcpy(target, source, sizeof(TestSimple));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_simple_primitive(void)
|
|
|
|
{
|
|
|
|
TestSimple obj, obj_clone;
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
|
|
|
save_vmstate(&vmstate_simple_primitive, &obj_simple);
|
|
|
|
|
|
|
|
compare_vmstate(wire_simple_primitive, sizeof(wire_simple_primitive));
|
|
|
|
|
|
|
|
SUCCESS(load_vmstate(&vmstate_simple_primitive, &obj, &obj_clone,
|
|
|
|
obj_simple_copy, 1, wire_simple_primitive,
|
|
|
|
sizeof(wire_simple_primitive)));
|
|
|
|
|
|
|
|
#define FIELD_EQUAL(name) g_assert_cmpint(obj.name, ==, obj_simple.name)
|
|
|
|
|
|
|
|
FIELD_EQUAL(b_1);
|
|
|
|
FIELD_EQUAL(b_2);
|
|
|
|
FIELD_EQUAL(u8_1);
|
|
|
|
FIELD_EQUAL(u16_1);
|
|
|
|
FIELD_EQUAL(u32_1);
|
|
|
|
FIELD_EQUAL(u64_1);
|
|
|
|
FIELD_EQUAL(i8_1);
|
|
|
|
FIELD_EQUAL(i8_2);
|
|
|
|
FIELD_EQUAL(i16_1);
|
|
|
|
FIELD_EQUAL(i16_2);
|
|
|
|
FIELD_EQUAL(i32_1);
|
|
|
|
FIELD_EQUAL(i32_2);
|
|
|
|
FIELD_EQUAL(i64_1);
|
|
|
|
FIELD_EQUAL(i64_2);
|
2013-11-28 15:01:18 +01:00
|
|
|
}
|
2014-04-05 20:12:51 +02:00
|
|
|
|
2018-11-14 14:21:30 +01:00
|
|
|
typedef struct TestSimpleArray {
|
|
|
|
uint16_t u16_1[3];
|
|
|
|
} TestSimpleArray;
|
|
|
|
|
|
|
|
/* Object instantiation, we are going to use it in more than one test */
|
|
|
|
|
|
|
|
TestSimpleArray obj_simple_arr = {
|
|
|
|
.u16_1 = { 0x42, 0x43, 0x44 },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Description of the values. If you add a primitive type
|
|
|
|
you are expected to add a test here */
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_simple_arr = {
|
|
|
|
.name = "simple/array",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2018-11-14 14:21:30 +01:00
|
|
|
VMSTATE_UINT16_ARRAY(u16_1, TestSimpleArray, 3),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t wire_simple_arr[] = {
|
|
|
|
/* u16_1 */ 0x00, 0x42,
|
|
|
|
/* u16_1 */ 0x00, 0x43,
|
|
|
|
/* u16_1 */ 0x00, 0x44,
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void obj_simple_arr_copy(void *target, void *source)
|
|
|
|
{
|
|
|
|
memcpy(target, source, sizeof(TestSimpleArray));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_simple_array(void)
|
|
|
|
{
|
|
|
|
TestSimpleArray obj, obj_clone;
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
|
|
|
save_vmstate(&vmstate_simple_arr, &obj_simple_arr);
|
|
|
|
|
|
|
|
compare_vmstate(wire_simple_arr, sizeof(wire_simple_arr));
|
|
|
|
|
|
|
|
SUCCESS(load_vmstate(&vmstate_simple_arr, &obj, &obj_clone,
|
|
|
|
obj_simple_arr_copy, 1, wire_simple_arr,
|
|
|
|
sizeof(wire_simple_arr)));
|
|
|
|
}
|
|
|
|
|
2014-04-05 20:12:51 +02:00
|
|
|
typedef struct TestStruct {
|
|
|
|
uint32_t a, b, c, e;
|
|
|
|
uint64_t d, f;
|
|
|
|
bool skip_c_e;
|
|
|
|
} TestStruct;
|
2013-11-28 15:01:18 +01:00
|
|
|
|
|
|
|
static const VMStateDescription vmstate_versioned = {
|
2014-04-05 20:12:51 +02:00
|
|
|
.name = "test/versioned",
|
2013-11-28 15:01:18 +01:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2013-11-28 15:01:18 +01:00
|
|
|
VMSTATE_UINT32(a, TestStruct),
|
|
|
|
VMSTATE_UINT32_V(b, TestStruct, 2), /* Versioned field in the middle, so
|
|
|
|
* we catch bugs more easily.
|
|
|
|
*/
|
|
|
|
VMSTATE_UINT32(c, TestStruct),
|
|
|
|
VMSTATE_UINT64(d, TestStruct),
|
|
|
|
VMSTATE_UINT32_V(e, TestStruct, 2),
|
|
|
|
VMSTATE_UINT64_V(f, TestStruct, 2),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_load_v1(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 16:37:38 +02:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 18:34:36 +02:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj = { .b = 200, .e = 500, .f = 600 };
|
|
|
|
vmstate_load_state(loading, &vmstate_versioned, &obj, 1);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 200);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 500);
|
|
|
|
g_assert_cmpint(obj.f, ==, 600);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_v2(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 50, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 16:37:38 +02:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2014-04-05 18:34:36 +02:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj;
|
|
|
|
vmstate_load_state(loading, &vmstate_versioned, &obj, 2);
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 50);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_skip(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
TestStruct *t = (TestStruct *)opaque;
|
|
|
|
return !t->skip_c_e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_skipping = {
|
2014-04-05 20:12:51 +02:00
|
|
|
.name = "test/skip",
|
2013-11-28 15:01:18 +01:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2013-11-28 15:01:18 +01:00
|
|
|
VMSTATE_UINT32(a, TestStruct),
|
|
|
|
VMSTATE_UINT32(b, TestStruct),
|
|
|
|
VMSTATE_UINT32_TEST(c, TestStruct, test_skip),
|
|
|
|
VMSTATE_UINT64(d, TestStruct),
|
|
|
|
VMSTATE_UINT32_TEST(e, TestStruct, test_skip),
|
|
|
|
VMSTATE_UINT64_V(f, TestStruct, 2),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void test_save_noskip(void)
|
|
|
|
{
|
2016-04-27 12:04:53 +02:00
|
|
|
QEMUFile *fsave = open_test_file(true);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
|
|
|
|
.skip_c_e = false };
|
2017-09-25 13:29:17 +02:00
|
|
|
int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
|
|
|
|
g_assert(!ret);
|
2013-11-28 15:01:18 +01:00
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
|
|
|
|
uint8_t expected[] = {
|
|
|
|
0, 0, 0, 1, /* a */
|
|
|
|
0, 0, 0, 2, /* b */
|
|
|
|
0, 0, 0, 3, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 4, /* d */
|
|
|
|
0, 0, 0, 5, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 6, /* f */
|
|
|
|
};
|
2016-04-27 12:04:53 +02:00
|
|
|
|
2014-10-10 13:39:05 +02:00
|
|
|
qemu_fclose(fsave);
|
2016-04-27 12:04:53 +02:00
|
|
|
compare_vmstate(expected, sizeof(expected));
|
2013-11-28 15:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_save_skip(void)
|
|
|
|
{
|
2016-04-27 12:04:53 +02:00
|
|
|
QEMUFile *fsave = open_test_file(true);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
|
|
|
|
.skip_c_e = true };
|
2017-09-25 13:29:17 +02:00
|
|
|
int ret = vmstate_save_state(fsave, &vmstate_skipping, &obj, NULL);
|
|
|
|
g_assert(!ret);
|
2013-11-28 15:01:18 +01:00
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
|
|
|
|
uint8_t expected[] = {
|
|
|
|
0, 0, 0, 1, /* a */
|
|
|
|
0, 0, 0, 2, /* b */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 4, /* d */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 6, /* f */
|
|
|
|
};
|
|
|
|
|
2014-10-10 13:39:05 +02:00
|
|
|
qemu_fclose(fsave);
|
2016-04-27 12:04:53 +02:00
|
|
|
compare_vmstate(expected, sizeof(expected));
|
2013-11-28 15:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_noskip(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 30, /* c */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 50, /* e */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 16:37:38 +02:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2016-04-27 12:04:53 +02:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj = { .skip_c_e = false };
|
|
|
|
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 30);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 50);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_skip(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[] = {
|
|
|
|
0, 0, 0, 10, /* a */
|
|
|
|
0, 0, 0, 20, /* b */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 40, /* d */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 60, /* f */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
2016-10-21 16:37:38 +02:00
|
|
|
save_buffer(buf, sizeof(buf));
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2016-04-27 12:04:53 +02:00
|
|
|
QEMUFile *loading = open_test_file(false);
|
2013-11-28 15:01:18 +01:00
|
|
|
TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
|
|
|
|
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
|
|
|
|
g_assert(!qemu_file_get_error(loading));
|
|
|
|
g_assert_cmpint(obj.a, ==, 10);
|
|
|
|
g_assert_cmpint(obj.b, ==, 20);
|
|
|
|
g_assert_cmpint(obj.c, ==, 300);
|
|
|
|
g_assert_cmpint(obj.d, ==, 40);
|
|
|
|
g_assert_cmpint(obj.e, ==, 500);
|
|
|
|
g_assert_cmpint(obj.f, ==, 60);
|
|
|
|
qemu_fclose(loading);
|
|
|
|
}
|
|
|
|
|
2016-10-21 16:37:39 +02:00
|
|
|
typedef struct {
|
|
|
|
int32_t i;
|
|
|
|
} TestStructTriv;
|
|
|
|
|
|
|
|
const VMStateDescription vmsd_tst = {
|
|
|
|
.name = "test/tst",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2016-10-21 16:37:39 +02:00
|
|
|
VMSTATE_INT32(i, TestStructTriv),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-22 17:01:18 +01:00
|
|
|
/* test array migration */
|
|
|
|
|
2016-10-21 16:37:39 +02:00
|
|
|
#define AR_SIZE 4
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
TestStructTriv *ar[AR_SIZE];
|
|
|
|
} TestArrayOfPtrToStuct;
|
|
|
|
|
|
|
|
const VMStateDescription vmsd_arps = {
|
|
|
|
.name = "test/arps",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2016-10-21 16:37:39 +02:00
|
|
|
VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(ar, TestArrayOfPtrToStuct,
|
|
|
|
AR_SIZE, 0, vmsd_tst, TestStructTriv),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2017-02-22 17:01:18 +01:00
|
|
|
|
|
|
|
static uint8_t wire_arr_ptr_no0[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x00, 0x00, 0x00, 0x03,
|
|
|
|
QEMU_VM_EOF
|
|
|
|
};
|
|
|
|
|
2016-10-21 16:37:39 +02:00
|
|
|
static void test_arr_ptr_str_no0_save(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
|
|
|
|
TestArrayOfPtrToStuct sample = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
|
|
|
|
|
|
|
|
save_vmstate(&vmsd_arps, &sample);
|
2017-02-22 17:01:18 +01:00
|
|
|
compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
|
2016-10-21 16:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_arr_ptr_str_no0_load(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
|
|
|
|
TestStructTriv ar[AR_SIZE] = {};
|
|
|
|
TestArrayOfPtrToStuct obj = {.ar = {&ar[0], &ar[1], &ar[2], &ar[3]} };
|
|
|
|
int idx;
|
|
|
|
|
2017-02-22 17:01:18 +01:00
|
|
|
save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0));
|
|
|
|
SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
|
|
|
|
wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)));
|
|
|
|
for (idx = 0; idx < AR_SIZE; ++idx) {
|
|
|
|
/* compare the target array ar with the ground truth array ar_gt */
|
|
|
|
g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t wire_arr_ptr_0[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
VMS_NULLPTR_MARKER,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x00, 0x00, 0x00, 0x03,
|
|
|
|
QEMU_VM_EOF
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_arr_ptr_str_0_save(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar[AR_SIZE] = {{.i = 0}, {.i = 1}, {.i = 2}, {.i = 3} };
|
|
|
|
TestArrayOfPtrToStuct sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
|
|
|
|
|
|
|
|
save_vmstate(&vmsd_arps, &sample);
|
|
|
|
compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_arr_ptr_str_0_load(void)
|
|
|
|
{
|
|
|
|
TestStructTriv ar_gt[AR_SIZE] = {{.i = 0}, {.i = 0}, {.i = 2}, {.i = 3} };
|
|
|
|
TestStructTriv ar[AR_SIZE] = {};
|
|
|
|
TestArrayOfPtrToStuct obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
|
2016-10-21 16:37:39 +02:00
|
|
|
SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1,
|
2017-02-22 17:01:18 +01:00
|
|
|
wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
|
2016-10-21 16:37:39 +02:00
|
|
|
for (idx = 0; idx < AR_SIZE; ++idx) {
|
|
|
|
/* compare the target array ar with the ground truth array ar_gt */
|
|
|
|
g_assert_cmpint(ar_gt[idx].i, ==, ar[idx].i);
|
|
|
|
}
|
2017-02-22 17:01:18 +01:00
|
|
|
for (idx = 0; idx < AR_SIZE; ++idx) {
|
|
|
|
if (idx == 1) {
|
|
|
|
g_assert_cmpint((uintptr_t)(obj.ar[idx]), ==, 0);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpint((uintptr_t)(obj.ar[idx]), !=, 0);
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 16:37:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-22 17:01:19 +01:00
|
|
|
typedef struct TestArrayOfPtrToInt {
|
|
|
|
int32_t *ar[AR_SIZE];
|
|
|
|
} TestArrayOfPtrToInt;
|
|
|
|
|
|
|
|
const VMStateDescription vmsd_arpp = {
|
|
|
|
.name = "test/arps",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-02-22 17:01:19 +01:00
|
|
|
VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt,
|
|
|
|
AR_SIZE, 0, vmstate_info_int32, int32_t*),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_arr_ptr_prim_0_save(void)
|
|
|
|
{
|
|
|
|
int32_t ar[AR_SIZE] = {0 , 1, 2, 3};
|
|
|
|
TestArrayOfPtrToInt sample = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
|
|
|
|
|
|
|
|
save_vmstate(&vmsd_arpp, &sample);
|
|
|
|
compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_arr_ptr_prim_0_load(void)
|
|
|
|
{
|
|
|
|
int32_t ar_gt[AR_SIZE] = {0, 1, 2, 3};
|
|
|
|
int32_t ar[AR_SIZE] = {3 , 42, 1, 0};
|
|
|
|
TestArrayOfPtrToInt obj = {.ar = {&ar[0], NULL, &ar[2], &ar[3]} };
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0));
|
|
|
|
SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1,
|
|
|
|
wire_arr_ptr_0, sizeof(wire_arr_ptr_0)));
|
|
|
|
for (idx = 0; idx < AR_SIZE; ++idx) {
|
|
|
|
/* compare the target array ar with the ground truth array ar_gt */
|
|
|
|
if (idx == 1) {
|
|
|
|
g_assert_cmpint(42, ==, ar[idx]);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpint(ar_gt[idx], ==, ar[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 20:00:52 +01:00
|
|
|
/* test QTAILQ migration */
|
|
|
|
typedef struct TestQtailqElement TestQtailqElement;
|
|
|
|
|
|
|
|
struct TestQtailqElement {
|
|
|
|
bool b;
|
|
|
|
uint8_t u8;
|
|
|
|
QTAILQ_ENTRY(TestQtailqElement) next;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct TestQtailq {
|
|
|
|
int16_t i16;
|
2018-12-06 13:10:34 +01:00
|
|
|
QTAILQ_HEAD(, TestQtailqElement) q;
|
2017-01-19 20:00:52 +01:00
|
|
|
int32_t i32;
|
|
|
|
} TestQtailq;
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_q_element = {
|
|
|
|
.name = "test/queue-element",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-01-19 20:00:52 +01:00
|
|
|
VMSTATE_BOOL(b, TestQtailqElement),
|
|
|
|
VMSTATE_UINT8(u8, TestQtailqElement),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_q = {
|
|
|
|
.name = "test/queue",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-01-19 20:00:52 +01:00
|
|
|
VMSTATE_INT16(i16, TestQtailq),
|
|
|
|
VMSTATE_QTAILQ_V(q, TestQtailq, 1, vmstate_q_element, TestQtailqElement,
|
|
|
|
next),
|
|
|
|
VMSTATE_INT32(i32, TestQtailq),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t wire_q[] = {
|
|
|
|
/* i16 */ 0xfe, 0x0,
|
|
|
|
/* start of element 0 of q */ 0x01,
|
|
|
|
/* .b */ 0x01,
|
|
|
|
/* .u8 */ 0x82,
|
|
|
|
/* start of element 1 of q */ 0x01,
|
|
|
|
/* b */ 0x00,
|
|
|
|
/* u8 */ 0x41,
|
|
|
|
/* end of q */ 0x00,
|
|
|
|
/* i32 */ 0x00, 0x01, 0x11, 0x70,
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void test_save_q(void)
|
|
|
|
{
|
|
|
|
TestQtailq obj_q = {
|
|
|
|
.i16 = -512,
|
|
|
|
.i32 = 70000,
|
|
|
|
};
|
|
|
|
|
|
|
|
TestQtailqElement obj_qe1 = {
|
|
|
|
.b = true,
|
|
|
|
.u8 = 130,
|
|
|
|
};
|
|
|
|
|
|
|
|
TestQtailqElement obj_qe2 = {
|
|
|
|
.b = false,
|
|
|
|
.u8 = 65,
|
|
|
|
};
|
|
|
|
|
|
|
|
QTAILQ_INIT(&obj_q.q);
|
|
|
|
QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
|
|
|
|
QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
|
|
|
|
|
|
|
|
save_vmstate(&vmstate_q, &obj_q);
|
|
|
|
compare_vmstate(wire_q, sizeof(wire_q));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_q(void)
|
|
|
|
{
|
|
|
|
TestQtailq obj_q = {
|
|
|
|
.i16 = -512,
|
|
|
|
.i32 = 70000,
|
|
|
|
};
|
|
|
|
|
|
|
|
TestQtailqElement obj_qe1 = {
|
|
|
|
.b = true,
|
|
|
|
.u8 = 130,
|
|
|
|
};
|
|
|
|
|
|
|
|
TestQtailqElement obj_qe2 = {
|
|
|
|
.b = false,
|
|
|
|
.u8 = 65,
|
|
|
|
};
|
|
|
|
|
|
|
|
QTAILQ_INIT(&obj_q.q);
|
|
|
|
QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe1, next);
|
|
|
|
QTAILQ_INSERT_TAIL(&obj_q.q, &obj_qe2, next);
|
|
|
|
|
|
|
|
QEMUFile *fsave = open_test_file(true);
|
|
|
|
|
|
|
|
qemu_put_buffer(fsave, wire_q, sizeof(wire_q));
|
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
|
|
|
|
QEMUFile *fload = open_test_file(false);
|
|
|
|
TestQtailq tgt;
|
|
|
|
|
|
|
|
QTAILQ_INIT(&tgt.q);
|
|
|
|
vmstate_load_state(fload, &vmstate_q, &tgt, 1);
|
|
|
|
char eof = qemu_get_byte(fload);
|
|
|
|
g_assert(!qemu_file_get_error(fload));
|
|
|
|
g_assert_cmpint(tgt.i16, ==, obj_q.i16);
|
|
|
|
g_assert_cmpint(tgt.i32, ==, obj_q.i32);
|
|
|
|
g_assert_cmpint(eof, ==, QEMU_VM_EOF);
|
|
|
|
|
|
|
|
TestQtailqElement *qele_from = QTAILQ_FIRST(&obj_q.q);
|
2018-12-06 13:10:34 +01:00
|
|
|
TestQtailqElement *qlast_from = QTAILQ_LAST(&obj_q.q);
|
2017-01-19 20:00:52 +01:00
|
|
|
TestQtailqElement *qele_to = QTAILQ_FIRST(&tgt.q);
|
2018-12-06 13:10:34 +01:00
|
|
|
TestQtailqElement *qlast_to = QTAILQ_LAST(&tgt.q);
|
2017-01-19 20:00:52 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
g_assert_cmpint(qele_to->b, ==, qele_from->b);
|
|
|
|
g_assert_cmpint(qele_to->u8, ==, qele_from->u8);
|
|
|
|
if ((qele_from == qlast_from) || (qele_to == qlast_to)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
qele_from = QTAILQ_NEXT(qele_from, next);
|
|
|
|
qele_to = QTAILQ_NEXT(qele_to, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_cmpint((uintptr_t) qele_from, ==, (uintptr_t) qlast_from);
|
|
|
|
g_assert_cmpint((uintptr_t) qele_to, ==, (uintptr_t) qlast_to);
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
TestQtailqElement *qele;
|
|
|
|
while (!QTAILQ_EMPTY(&tgt.q)) {
|
2018-12-06 13:10:34 +01:00
|
|
|
qele = QTAILQ_LAST(&tgt.q);
|
2017-01-19 20:00:52 +01:00
|
|
|
QTAILQ_REMOVE(&tgt.q, qele, next);
|
|
|
|
free(qele);
|
|
|
|
qele = NULL;
|
|
|
|
}
|
|
|
|
qemu_fclose(fload);
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:17:24 +02:00
|
|
|
/* interval (key) */
|
|
|
|
typedef struct TestGTreeInterval {
|
|
|
|
uint64_t low;
|
|
|
|
uint64_t high;
|
|
|
|
} TestGTreeInterval;
|
|
|
|
|
|
|
|
#define VMSTATE_INTERVAL \
|
|
|
|
{ \
|
|
|
|
.name = "interval", \
|
|
|
|
.version_id = 1, \
|
|
|
|
.minimum_version_id = 1, \
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) { \
|
2019-10-11 14:17:24 +02:00
|
|
|
VMSTATE_UINT64(low, TestGTreeInterval), \
|
|
|
|
VMSTATE_UINT64(high, TestGTreeInterval), \
|
|
|
|
VMSTATE_END_OF_LIST() \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mapping (value) */
|
|
|
|
typedef struct TestGTreeMapping {
|
|
|
|
uint64_t phys_addr;
|
|
|
|
uint32_t flags;
|
|
|
|
} TestGTreeMapping;
|
|
|
|
|
|
|
|
#define VMSTATE_MAPPING \
|
|
|
|
{ \
|
|
|
|
.name = "mapping", \
|
|
|
|
.version_id = 1, \
|
|
|
|
.minimum_version_id = 1, \
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) { \
|
2019-10-11 14:17:24 +02:00
|
|
|
VMSTATE_UINT64(phys_addr, TestGTreeMapping), \
|
|
|
|
VMSTATE_UINT32(flags, TestGTreeMapping), \
|
|
|
|
VMSTATE_END_OF_LIST() \
|
|
|
|
}, \
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_interval_mapping[2] = {
|
|
|
|
VMSTATE_MAPPING, /* value */
|
|
|
|
VMSTATE_INTERVAL /* key */
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct TestGTreeDomain {
|
|
|
|
int32_t id;
|
|
|
|
GTree *mappings;
|
|
|
|
} TestGTreeDomain;
|
|
|
|
|
|
|
|
typedef struct TestGTreeIOMMU {
|
|
|
|
int32_t id;
|
|
|
|
GTree *domains;
|
|
|
|
} TestGTreeIOMMU;
|
|
|
|
|
|
|
|
/* Interval comparison function */
|
|
|
|
static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
{
|
|
|
|
TestGTreeInterval *inta = (TestGTreeInterval *)a;
|
|
|
|
TestGTreeInterval *intb = (TestGTreeInterval *)b;
|
|
|
|
|
|
|
|
if (inta->high < intb->low) {
|
|
|
|
return -1;
|
|
|
|
} else if (intb->high < inta->low) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ID comparison function */
|
|
|
|
static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
{
|
2020-09-05 08:38:13 +02:00
|
|
|
guint ua = GPOINTER_TO_UINT(a);
|
|
|
|
guint ub = GPOINTER_TO_UINT(b);
|
2019-10-11 14:17:24 +02:00
|
|
|
return (ua > ub) - (ua < ub);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_domain(gpointer data)
|
|
|
|
{
|
|
|
|
TestGTreeDomain *domain = (TestGTreeDomain *)data;
|
|
|
|
|
|
|
|
g_tree_destroy(domain->mappings);
|
|
|
|
g_free(domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int domain_preload(void *opaque)
|
|
|
|
{
|
|
|
|
TestGTreeDomain *domain = opaque;
|
|
|
|
|
|
|
|
domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
|
|
|
|
NULL, g_free, g_free);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iommu_preload(void *opaque)
|
|
|
|
{
|
|
|
|
TestGTreeIOMMU *iommu = opaque;
|
|
|
|
|
|
|
|
iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp,
|
|
|
|
NULL, NULL, destroy_domain);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_domain = {
|
|
|
|
.name = "domain",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.pre_load = domain_preload,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2019-10-11 14:17:24 +02:00
|
|
|
VMSTATE_INT32(id, TestGTreeDomain),
|
|
|
|
VMSTATE_GTREE_V(mappings, TestGTreeDomain, 1,
|
|
|
|
vmstate_interval_mapping,
|
|
|
|
TestGTreeInterval, TestGTreeMapping),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-13 14:48:23 +01:00
|
|
|
/* test QLIST Migration */
|
|
|
|
|
|
|
|
typedef struct TestQListElement {
|
|
|
|
uint32_t id;
|
|
|
|
QLIST_ENTRY(TestQListElement) next;
|
|
|
|
} TestQListElement;
|
|
|
|
|
|
|
|
typedef struct TestQListContainer {
|
|
|
|
uint32_t id;
|
|
|
|
QLIST_HEAD(, TestQListElement) list;
|
|
|
|
} TestQListContainer;
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_qlist_element = {
|
|
|
|
.name = "test/queue list",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2020-01-13 14:48:23 +01:00
|
|
|
VMSTATE_UINT32(id, TestQListElement),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-11 14:17:24 +02:00
|
|
|
static const VMStateDescription vmstate_iommu = {
|
|
|
|
.name = "iommu",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.pre_load = iommu_preload,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2019-10-11 14:17:24 +02:00
|
|
|
VMSTATE_INT32(id, TestGTreeIOMMU),
|
|
|
|
VMSTATE_GTREE_DIRECT_KEY_V(domains, TestGTreeIOMMU, 1,
|
|
|
|
&vmstate_domain, TestGTreeDomain),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-13 14:48:23 +01:00
|
|
|
static const VMStateDescription vmstate_container = {
|
|
|
|
.name = "test/container/qlist",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2020-01-13 14:48:23 +01:00
|
|
|
VMSTATE_UINT32(id, TestQListContainer),
|
|
|
|
VMSTATE_QLIST_V(list, TestQListContainer, 1, vmstate_qlist_element,
|
|
|
|
TestQListElement, next),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-11 14:17:24 +02:00
|
|
|
uint8_t first_domain_dump[] = {
|
|
|
|
/* id */
|
|
|
|
0x00, 0x0, 0x0, 0x6,
|
|
|
|
0x00, 0x0, 0x0, 0x2, /* 2 mappings */
|
|
|
|
0x1, /* start of a */
|
|
|
|
/* a */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
|
|
|
|
/* map_a */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x1, /* start of b */
|
|
|
|
/* b */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
|
|
|
|
/* map_b */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x0, /* end of gtree */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static TestGTreeDomain *create_first_domain(void)
|
|
|
|
{
|
|
|
|
TestGTreeDomain *domain;
|
|
|
|
TestGTreeMapping *map_a, *map_b;
|
|
|
|
TestGTreeInterval *a, *b;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
domain = g_new0(TestGTreeDomain, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
domain->id = 6;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
a = g_new0(TestGTreeInterval, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
a->low = 0x1000;
|
|
|
|
a->high = 0x1FFF;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
b = g_new0(TestGTreeInterval, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
b->low = 0x4000;
|
|
|
|
b->high = 0x4FFF;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
map_a = g_new0(TestGTreeMapping, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
map_a->phys_addr = 0xa000;
|
|
|
|
map_a->flags = 1;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
map_b = g_new0(TestGTreeMapping, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
map_b->phys_addr = 0xe0000;
|
|
|
|
map_b->flags = 2;
|
|
|
|
|
|
|
|
domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp, NULL,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
|
|
|
g_tree_insert(domain->mappings, a, map_a);
|
|
|
|
g_tree_insert(domain->mappings, b, map_b);
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gtree_save_domain(void)
|
|
|
|
{
|
|
|
|
TestGTreeDomain *first_domain = create_first_domain();
|
|
|
|
|
|
|
|
save_vmstate(&vmstate_domain, first_domain);
|
|
|
|
compare_vmstate(first_domain_dump, sizeof(first_domain_dump));
|
|
|
|
destroy_domain(first_domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct match_node_data {
|
|
|
|
GTree *tree;
|
|
|
|
gpointer key;
|
|
|
|
gpointer value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tree_cmp_data {
|
|
|
|
GTree *tree1;
|
|
|
|
GTree *tree2;
|
|
|
|
GTraverseFunc match_node;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean match_interval_mapping_node(gpointer key,
|
|
|
|
gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
TestGTreeMapping *map_a, *map_b;
|
|
|
|
TestGTreeInterval *a, *b;
|
|
|
|
struct match_node_data *d = (struct match_node_data *)data;
|
|
|
|
a = (TestGTreeInterval *)key;
|
|
|
|
b = (TestGTreeInterval *)d->key;
|
|
|
|
|
|
|
|
map_a = (TestGTreeMapping *)value;
|
|
|
|
map_b = (TestGTreeMapping *)d->value;
|
|
|
|
|
|
|
|
assert(a->low == b->low);
|
|
|
|
assert(a->high == b->high);
|
|
|
|
assert(map_a->phys_addr == map_b->phys_addr);
|
|
|
|
assert(map_a->flags == map_b->flags);
|
|
|
|
g_tree_remove(d->tree, key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean diff_tree(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
struct tree_cmp_data *tp = (struct tree_cmp_data *)data;
|
|
|
|
struct match_node_data d = {tp->tree2, key, value};
|
|
|
|
|
|
|
|
g_tree_foreach(tp->tree2, tp->match_node, &d);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compare_trees(GTree *tree1, GTree *tree2,
|
|
|
|
GTraverseFunc function)
|
|
|
|
{
|
|
|
|
struct tree_cmp_data tp = {tree1, tree2, function};
|
|
|
|
|
2023-02-28 10:29:44 +01:00
|
|
|
assert(g_tree_nnodes(tree1) == g_tree_nnodes(tree2));
|
2019-10-11 14:17:24 +02:00
|
|
|
g_tree_foreach(tree1, diff_tree, &tp);
|
2023-02-28 10:29:44 +01:00
|
|
|
g_tree_destroy(g_tree_ref(tree1));
|
2019-10-11 14:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void diff_domain(TestGTreeDomain *d1, TestGTreeDomain *d2)
|
|
|
|
{
|
|
|
|
assert(d1->id == d2->id);
|
|
|
|
compare_trees(d1->mappings, d2->mappings, match_interval_mapping_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean match_domain_node(gpointer key, gpointer value, gpointer data)
|
|
|
|
{
|
|
|
|
uint64_t id1, id2;
|
|
|
|
TestGTreeDomain *d1, *d2;
|
|
|
|
struct match_node_data *d = (struct match_node_data *)data;
|
|
|
|
|
|
|
|
id1 = (uint64_t)(uintptr_t)key;
|
|
|
|
id2 = (uint64_t)(uintptr_t)d->key;
|
|
|
|
d1 = (TestGTreeDomain *)value;
|
|
|
|
d2 = (TestGTreeDomain *)d->value;
|
|
|
|
assert(id1 == id2);
|
|
|
|
diff_domain(d1, d2);
|
|
|
|
g_tree_remove(d->tree, key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void diff_iommu(TestGTreeIOMMU *iommu1, TestGTreeIOMMU *iommu2)
|
|
|
|
{
|
|
|
|
assert(iommu1->id == iommu2->id);
|
|
|
|
compare_trees(iommu1->domains, iommu2->domains, match_domain_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gtree_load_domain(void)
|
|
|
|
{
|
2022-03-15 15:41:56 +01:00
|
|
|
TestGTreeDomain *dest_domain = g_new0(TestGTreeDomain, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
TestGTreeDomain *orig_domain = create_first_domain();
|
|
|
|
QEMUFile *fload, *fsave;
|
|
|
|
char eof;
|
|
|
|
|
|
|
|
fsave = open_test_file(true);
|
|
|
|
qemu_put_buffer(fsave, first_domain_dump, sizeof(first_domain_dump));
|
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
|
|
|
|
fload = open_test_file(false);
|
|
|
|
|
|
|
|
vmstate_load_state(fload, &vmstate_domain, dest_domain, 1);
|
|
|
|
eof = qemu_get_byte(fload);
|
|
|
|
g_assert(!qemu_file_get_error(fload));
|
|
|
|
g_assert_cmpint(orig_domain->id, ==, dest_domain->id);
|
|
|
|
g_assert_cmpint(eof, ==, QEMU_VM_EOF);
|
|
|
|
|
|
|
|
diff_domain(orig_domain, dest_domain);
|
|
|
|
destroy_domain(orig_domain);
|
|
|
|
destroy_domain(dest_domain);
|
|
|
|
qemu_fclose(fload);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t iommu_dump[] = {
|
|
|
|
/* iommu id */
|
|
|
|
0x00, 0x0, 0x0, 0x7,
|
|
|
|
0x00, 0x0, 0x0, 0x2, /* 2 domains */
|
|
|
|
0x1,/* start of domain 5 */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x5, /* key = 5 */
|
|
|
|
0x00, 0x0, 0x0, 0x5, /* domain1 id */
|
|
|
|
0x00, 0x0, 0x0, 0x1, /* 1 mapping */
|
|
|
|
0x1, /* start of mappings */
|
|
|
|
/* c */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF,
|
|
|
|
/* map_c */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x0, 0x0, 0x3,
|
|
|
|
0x0, /* end of domain1 mappings*/
|
|
|
|
0x1,/* start of domain 6 */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x0, 0x6, /* key = 6 */
|
|
|
|
0x00, 0x0, 0x0, 0x6, /* domain6 id */
|
|
|
|
0x00, 0x0, 0x0, 0x2, /* 2 mappings */
|
|
|
|
0x1, /* start of a */
|
|
|
|
/* a */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF,
|
|
|
|
/* map_a */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
0x1, /* start of b */
|
|
|
|
/* b */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0xFF,
|
|
|
|
/* map_b */
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
0x0, /* end of domain6 mappings*/
|
|
|
|
0x0, /* end of domains */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static TestGTreeIOMMU *create_iommu(void)
|
|
|
|
{
|
2022-03-15 15:41:56 +01:00
|
|
|
TestGTreeIOMMU *iommu = g_new0(TestGTreeIOMMU, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
TestGTreeDomain *first_domain = create_first_domain();
|
|
|
|
TestGTreeDomain *second_domain;
|
|
|
|
TestGTreeMapping *map_c;
|
|
|
|
TestGTreeInterval *c;
|
|
|
|
|
|
|
|
iommu->id = 7;
|
|
|
|
iommu->domains = g_tree_new_full((GCompareDataFunc)int_cmp, NULL,
|
|
|
|
NULL,
|
|
|
|
destroy_domain);
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
second_domain = g_new0(TestGTreeDomain, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
second_domain->id = 5;
|
|
|
|
second_domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
|
|
|
|
NULL,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
|
|
|
|
|
|
|
g_tree_insert(iommu->domains, GUINT_TO_POINTER(6), first_domain);
|
|
|
|
g_tree_insert(iommu->domains, (gpointer)0x0000000000000005, second_domain);
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
c = g_new0(TestGTreeInterval, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
c->low = 0x1000000;
|
|
|
|
c->high = 0x1FFFFFF;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
map_c = g_new0(TestGTreeMapping, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
map_c->phys_addr = 0xF000000;
|
|
|
|
map_c->flags = 0x3;
|
|
|
|
|
|
|
|
g_tree_insert(second_domain->mappings, c, map_c);
|
|
|
|
return iommu;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_iommu(TestGTreeIOMMU *iommu)
|
|
|
|
{
|
|
|
|
g_tree_destroy(iommu->domains);
|
|
|
|
g_free(iommu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gtree_save_iommu(void)
|
|
|
|
{
|
|
|
|
TestGTreeIOMMU *iommu = create_iommu();
|
|
|
|
|
|
|
|
save_vmstate(&vmstate_iommu, iommu);
|
|
|
|
compare_vmstate(iommu_dump, sizeof(iommu_dump));
|
|
|
|
destroy_iommu(iommu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_gtree_load_iommu(void)
|
|
|
|
{
|
2022-03-15 15:41:56 +01:00
|
|
|
TestGTreeIOMMU *dest_iommu = g_new0(TestGTreeIOMMU, 1);
|
2019-10-11 14:17:24 +02:00
|
|
|
TestGTreeIOMMU *orig_iommu = create_iommu();
|
|
|
|
QEMUFile *fsave, *fload;
|
|
|
|
char eof;
|
|
|
|
|
|
|
|
fsave = open_test_file(true);
|
|
|
|
qemu_put_buffer(fsave, iommu_dump, sizeof(iommu_dump));
|
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
|
|
|
|
fload = open_test_file(false);
|
|
|
|
vmstate_load_state(fload, &vmstate_iommu, dest_iommu, 1);
|
|
|
|
eof = qemu_get_byte(fload);
|
2020-02-20 14:41:03 +01:00
|
|
|
g_assert(!qemu_file_get_error(fload));
|
2019-10-11 14:17:24 +02:00
|
|
|
g_assert_cmpint(orig_iommu->id, ==, dest_iommu->id);
|
|
|
|
g_assert_cmpint(eof, ==, QEMU_VM_EOF);
|
|
|
|
|
|
|
|
diff_iommu(orig_iommu, dest_iommu);
|
|
|
|
destroy_iommu(orig_iommu);
|
|
|
|
destroy_iommu(dest_iommu);
|
|
|
|
qemu_fclose(fload);
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:48:23 +01:00
|
|
|
static uint8_t qlist_dump[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x01, /* container id */
|
|
|
|
0x1, /* start of a */
|
|
|
|
0x00, 0x00, 0x00, 0x0a,
|
|
|
|
0x1, /* start of b */
|
|
|
|
0x00, 0x00, 0x0b, 0x00,
|
|
|
|
0x1, /* start of c */
|
|
|
|
0x00, 0x0c, 0x00, 0x00,
|
|
|
|
0x1, /* start of d */
|
|
|
|
0x0d, 0x00, 0x00, 0x00,
|
|
|
|
0x0, /* end of list */
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
static TestQListContainer *alloc_container(void)
|
|
|
|
{
|
2022-03-15 15:41:56 +01:00
|
|
|
TestQListElement *a = g_new(TestQListElement, 1);
|
|
|
|
TestQListElement *b = g_new(TestQListElement, 1);
|
|
|
|
TestQListElement *c = g_new(TestQListElement, 1);
|
|
|
|
TestQListElement *d = g_new(TestQListElement, 1);
|
|
|
|
TestQListContainer *container = g_new(TestQListContainer, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
|
|
|
|
a->id = 0x0a;
|
|
|
|
b->id = 0x0b00;
|
|
|
|
c->id = 0xc0000;
|
|
|
|
d->id = 0xd000000;
|
|
|
|
container->id = 1;
|
|
|
|
|
|
|
|
QLIST_INIT(&container->list);
|
|
|
|
QLIST_INSERT_HEAD(&container->list, d, next);
|
|
|
|
QLIST_INSERT_HEAD(&container->list, c, next);
|
|
|
|
QLIST_INSERT_HEAD(&container->list, b, next);
|
|
|
|
QLIST_INSERT_HEAD(&container->list, a, next);
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_container(TestQListContainer *container)
|
|
|
|
{
|
|
|
|
TestQListElement *iter, *tmp;
|
|
|
|
|
|
|
|
QLIST_FOREACH_SAFE(iter, &container->list, next, tmp) {
|
|
|
|
QLIST_REMOVE(iter, next);
|
|
|
|
g_free(iter);
|
|
|
|
}
|
|
|
|
g_free(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void compare_containers(TestQListContainer *c1, TestQListContainer *c2)
|
|
|
|
{
|
|
|
|
TestQListElement *first_item_c1, *first_item_c2;
|
|
|
|
|
|
|
|
while (!QLIST_EMPTY(&c1->list)) {
|
|
|
|
first_item_c1 = QLIST_FIRST(&c1->list);
|
|
|
|
first_item_c2 = QLIST_FIRST(&c2->list);
|
|
|
|
assert(first_item_c2);
|
|
|
|
assert(first_item_c1->id == first_item_c2->id);
|
|
|
|
QLIST_REMOVE(first_item_c1, next);
|
|
|
|
QLIST_REMOVE(first_item_c2, next);
|
|
|
|
g_free(first_item_c1);
|
|
|
|
g_free(first_item_c2);
|
|
|
|
}
|
|
|
|
assert(QLIST_EMPTY(&c2->list));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the prev & next fields are correct by doing list
|
|
|
|
* manipulations on the container. We will do that for both
|
|
|
|
* the source and the destination containers
|
|
|
|
*/
|
|
|
|
static void manipulate_container(TestQListContainer *c)
|
|
|
|
{
|
|
|
|
TestQListElement *prev = NULL, *iter = QLIST_FIRST(&c->list);
|
|
|
|
TestQListElement *elem;
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
elem = g_new(TestQListElement, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
elem->id = 0x12;
|
|
|
|
QLIST_INSERT_AFTER(iter, elem, next);
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
elem = g_new(TestQListElement, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
elem->id = 0x13;
|
|
|
|
QLIST_INSERT_HEAD(&c->list, elem, next);
|
|
|
|
|
|
|
|
while (iter) {
|
|
|
|
prev = iter;
|
|
|
|
iter = QLIST_NEXT(iter, next);
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
elem = g_new(TestQListElement, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
elem->id = 0x14;
|
|
|
|
QLIST_INSERT_BEFORE(prev, elem, next);
|
|
|
|
|
2022-03-15 15:41:56 +01:00
|
|
|
elem = g_new(TestQListElement, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
elem->id = 0x15;
|
|
|
|
QLIST_INSERT_AFTER(prev, elem, next);
|
|
|
|
|
|
|
|
QLIST_REMOVE(prev, next);
|
|
|
|
g_free(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_save_qlist(void)
|
|
|
|
{
|
|
|
|
TestQListContainer *container = alloc_container();
|
|
|
|
|
|
|
|
save_vmstate(&vmstate_container, container);
|
|
|
|
compare_vmstate(qlist_dump, sizeof(qlist_dump));
|
|
|
|
free_container(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_load_qlist(void)
|
|
|
|
{
|
|
|
|
QEMUFile *fsave, *fload;
|
|
|
|
TestQListContainer *orig_container = alloc_container();
|
2022-03-15 15:41:56 +01:00
|
|
|
TestQListContainer *dest_container = g_new0(TestQListContainer, 1);
|
2020-01-13 14:48:23 +01:00
|
|
|
char eof;
|
|
|
|
|
|
|
|
QLIST_INIT(&dest_container->list);
|
|
|
|
|
|
|
|
fsave = open_test_file(true);
|
|
|
|
qemu_put_buffer(fsave, qlist_dump, sizeof(qlist_dump));
|
|
|
|
g_assert(!qemu_file_get_error(fsave));
|
|
|
|
qemu_fclose(fsave);
|
|
|
|
|
|
|
|
fload = open_test_file(false);
|
|
|
|
vmstate_load_state(fload, &vmstate_container, dest_container, 1);
|
|
|
|
eof = qemu_get_byte(fload);
|
|
|
|
g_assert(!qemu_file_get_error(fload));
|
|
|
|
g_assert_cmpint(eof, ==, QEMU_VM_EOF);
|
|
|
|
manipulate_container(orig_container);
|
|
|
|
manipulate_container(dest_container);
|
|
|
|
compare_containers(orig_container, dest_container);
|
|
|
|
free_container(orig_container);
|
|
|
|
free_container(dest_container);
|
2020-02-20 14:41:03 +01:00
|
|
|
qemu_fclose(fload);
|
2020-01-13 14:48:23 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 17:06:50 +01:00
|
|
|
typedef struct TmpTestStruct {
|
|
|
|
TestStruct *parent;
|
|
|
|
int64_t diff;
|
|
|
|
} TmpTestStruct;
|
|
|
|
|
2017-09-25 13:29:12 +02:00
|
|
|
static int tmp_child_pre_save(void *opaque)
|
2017-02-03 17:06:50 +01:00
|
|
|
{
|
|
|
|
struct TmpTestStruct *tts = opaque;
|
|
|
|
|
|
|
|
tts->diff = tts->parent->b - tts->parent->a;
|
2017-09-25 13:29:12 +02:00
|
|
|
|
|
|
|
return 0;
|
2017-02-03 17:06:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int tmp_child_post_load(void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
struct TmpTestStruct *tts = opaque;
|
|
|
|
|
|
|
|
tts->parent->b = tts->parent->a + tts->diff;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_tmp_back_to_parent = {
|
|
|
|
.name = "test/tmp_child_parent",
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-02-03 17:06:50 +01:00
|
|
|
VMSTATE_UINT64(f, TestStruct),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_tmp_child = {
|
|
|
|
.name = "test/tmp_child",
|
|
|
|
.pre_save = tmp_child_pre_save,
|
|
|
|
.post_load = tmp_child_post_load,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-02-03 17:06:50 +01:00
|
|
|
VMSTATE_INT64(diff, TmpTestStruct),
|
|
|
|
VMSTATE_STRUCT_POINTER(parent, TmpTestStruct,
|
|
|
|
vmstate_tmp_back_to_parent, TestStruct),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_with_tmp = {
|
|
|
|
.name = "test/with_tmp",
|
|
|
|
.version_id = 1,
|
2023-12-21 04:16:51 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2017-02-03 17:06:50 +01:00
|
|
|
VMSTATE_UINT32(a, TestStruct),
|
|
|
|
VMSTATE_UINT64(d, TestStruct),
|
|
|
|
VMSTATE_WITH_TMP(TestStruct, TmpTestStruct, vmstate_tmp_child),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void obj_tmp_copy(void *target, void *source)
|
|
|
|
{
|
|
|
|
memcpy(target, source, sizeof(TestStruct));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_tmp_struct(void)
|
|
|
|
{
|
|
|
|
TestStruct obj, obj_clone;
|
|
|
|
|
|
|
|
uint8_t const wire_with_tmp[] = {
|
|
|
|
/* u32 a */ 0x00, 0x00, 0x00, 0x02,
|
|
|
|
/* u64 d */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
|
|
|
/* diff */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
|
|
|
|
/* u64 f */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
|
|
|
|
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
|
|
|
|
};
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
|
|
|
obj.a = 2;
|
|
|
|
obj.b = 4;
|
|
|
|
obj.d = 1;
|
|
|
|
obj.f = 8;
|
|
|
|
save_vmstate(&vmstate_with_tmp, &obj);
|
|
|
|
|
|
|
|
compare_vmstate(wire_with_tmp, sizeof(wire_with_tmp));
|
|
|
|
|
|
|
|
memset(&obj, 0, sizeof(obj));
|
|
|
|
SUCCESS(load_vmstate(&vmstate_with_tmp, &obj, &obj_clone,
|
|
|
|
obj_tmp_copy, 1, wire_with_tmp,
|
|
|
|
sizeof(wire_with_tmp)));
|
|
|
|
g_assert_cmpint(obj.a, ==, 2); /* From top level vmsd */
|
|
|
|
g_assert_cmpint(obj.b, ==, 4); /* from the post_load */
|
|
|
|
g_assert_cmpint(obj.d, ==, 1); /* From top level vmsd */
|
|
|
|
g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:01:18 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-09-10 12:30:50 +02:00
|
|
|
g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
|
|
|
|
g_get_tmp_dir());
|
2013-11-28 15:01:18 +01:00
|
|
|
temp_fd = mkstemp(temp_file);
|
2021-05-25 15:44:58 +02:00
|
|
|
g_assert(temp_fd >= 0);
|
2013-11-28 15:01:18 +01:00
|
|
|
|
2016-04-27 12:05:08 +02:00
|
|
|
module_call_init(MODULE_INIT_QOM);
|
|
|
|
|
2019-12-11 15:23:28 +01:00
|
|
|
g_setenv("QTEST_SILENT_ERRORS", "1", 1);
|
2018-07-18 10:34:47 +02:00
|
|
|
|
2013-11-28 15:01:18 +01:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
2014-04-05 20:12:51 +02:00
|
|
|
g_test_add_func("/vmstate/simple/primitive", test_simple_primitive);
|
2018-11-14 14:21:30 +01:00
|
|
|
g_test_add_func("/vmstate/simple/array", test_simple_array);
|
2013-11-28 15:01:18 +01:00
|
|
|
g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
|
|
|
|
g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
|
|
|
|
g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/load/skip", test_load_skip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/save/noskip", test_save_noskip);
|
|
|
|
g_test_add_func("/vmstate/field_exists/save/skip", test_save_skip);
|
2016-10-21 16:37:39 +02:00
|
|
|
g_test_add_func("/vmstate/array/ptr/str/no0/save",
|
|
|
|
test_arr_ptr_str_no0_save);
|
|
|
|
g_test_add_func("/vmstate/array/ptr/str/no0/load",
|
|
|
|
test_arr_ptr_str_no0_load);
|
2017-02-22 17:01:18 +01:00
|
|
|
g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_save);
|
|
|
|
g_test_add_func("/vmstate/array/ptr/str/0/load",
|
|
|
|
test_arr_ptr_str_0_load);
|
2017-02-22 17:01:19 +01:00
|
|
|
g_test_add_func("/vmstate/array/ptr/prim/0/save",
|
|
|
|
test_arr_ptr_prim_0_save);
|
|
|
|
g_test_add_func("/vmstate/array/ptr/prim/0/load",
|
|
|
|
test_arr_ptr_prim_0_load);
|
2017-01-19 20:00:52 +01:00
|
|
|
g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q);
|
|
|
|
g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q);
|
2019-10-11 14:17:24 +02:00
|
|
|
g_test_add_func("/vmstate/gtree/save/savedomain", test_gtree_save_domain);
|
|
|
|
g_test_add_func("/vmstate/gtree/load/loaddomain", test_gtree_load_domain);
|
|
|
|
g_test_add_func("/vmstate/gtree/save/saveiommu", test_gtree_save_iommu);
|
|
|
|
g_test_add_func("/vmstate/gtree/load/loadiommu", test_gtree_load_iommu);
|
2020-01-13 14:48:23 +01:00
|
|
|
g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist);
|
|
|
|
g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist);
|
2017-02-03 17:06:50 +01:00
|
|
|
g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
|
2013-11-28 15:01:18 +01:00
|
|
|
g_test_run();
|
|
|
|
|
|
|
|
close(temp_fd);
|
|
|
|
unlink(temp_file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|