summaryrefslogtreecommitdiffstats
path: root/qemu/tests/qemu-iotests/142
blob: 29c0606bd7a63f156be0e3c1c5682cc50a9994f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
#
# Test for configuring cache modes of arbitrary nodes (requires O_DIRECT)
#
# Copyright (C) 2015 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=kwolf@redhat.com

seq=`basename $0`
echo "QA output created by $seq"

here=`pwd`
status=1	# failure is the default!

_cleanup()
{
    _cleanup_test_img
    rm -f $TEST_IMG.snap
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ./common.rc
. ./common.filter

_supported_fmt qcow2
_supported_proto file
_supported_os Linux

# We test all cache modes anyway, but O_DIRECT needs to be supported
_default_cache_mode none
_supported_cache_modes none directsync

function do_run_qemu()
{
    echo Testing: "$@"
    (
        if ! test -t 0; then
            while read cmd; do
                echo $cmd
            done
        fi
        echo quit
    ) | $QEMU -nographic -monitor stdio -nodefaults "$@"
    echo
}

function run_qemu()
{
    do_run_qemu "$@" 2>&1 | _filter_testdir | _filter_qemu
}

size=128M

TEST_IMG="$TEST_IMG.base" _make_test_img $size
TEST_IMG="$TEST_IMG.snap" _make_test_img $size
_make_test_img -b "$TEST_IMG.base" $size

echo
echo === Simple test for all cache modes ===
echo

run_qemu -drive file="$TEST_IMG",cache=none
run_qemu -drive file="$TEST_IMG",cache=directsync
run_qemu -drive file="$TEST_IMG",cache=writeback
run_qemu -drive file="$TEST_IMG",cache=writethrough
run_qemu -drive file="$TEST_IMG",cache=unsafe
run_qemu -drive file="$TEST_IMG",cache=invalid_value

echo
echo === Check inheritance of cache modes ===
echo

files="if=none,file=$TEST_IMG,backing.file.filename=$TEST_IMG.base"
ids="node-name=image,backing.node-name=backing,backing.file.node-name=backing-file,file.node-name=file"

function check_cache_all()
{
    # cache.direct is supposed to be inherited by both bs->file and
    # bs->backing

    echo -e "cache.direct=on on none0"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't"
    echo -e "\ncache.direct=on on file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't"
    echo -e "\ncache.direct=on on backing"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't"
    echo -e "\ncache.direct=on on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.direct=on | grep -e "Cache" -e "[Cc]annot|[Cc]ould not|[Cc]an't"

    # cache.writeback is supposed to be inherited by bs->backing; bs->file
    # always gets cache.writeback=on

    echo -e "\n\ncache.writeback=off on none0"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.writeback=off | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.writeback=off on file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.writeback=off | grep -e "doesn't" -e "does not"
    echo -e "\ncache.writeback=off on backing"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.writeback=off | grep -e "doesn't" -e "does not"
    echo -e "\ncache.writeback=off on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.writeback=off | grep -e "doesn't" -e "does not"

    # cache.no-flush is supposed to be inherited by both bs->file and bs->backing

    echo -e "\n\ncache.no-flush=on on none0"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",file.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on backing"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$files","$ids",backing.file.cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
}

echo
echo "--- Configure cache modes on the command line ---"
echo

# First check the inherited cache mode after opening the image.

hmp_cmds="info block none0
info block image
info block file
info block backing
info block backing-file"

check_cache_all

echo
echo "--- Cache modes after reopen (live snapshot) ---"
echo

# Then trigger a reopen and check that the cache modes are still the same.

hmp_cmds="snapshot_blkdev -n none0 $TEST_IMG.snap $IMGFMT
info block
info block image
info block file
info block backing
info block backing-file"

check_cache_all

echo
echo "--- Change cache modes with reopen (qemu-io command, flags) ---"
echo

# This one actually changes the cache mode with the reopen. For this test, the
# new cache mode is specified in the flags, not as an option.

hmp_cmds='qemu-io none0 "reopen -c none"
info block none0
info block image
info block file
info block backing
info block backing-file'

check_cache_all

echo
echo "--- Change cache modes with reopen (qemu-io command, options) ---"
echo

# This one actually changes the cache mode with the reopen. For this test, the
# new cache mode is specified as an option, not in the flags.

hmp_cmds='qemu-io none0 "reopen -o cache.direct=on"
info block none0
info block image
info block file
info block backing
info block backing-file'

check_cache_all

echo
echo "--- Change cache modes after snapshot ---"
echo

# This checks that the original image doesn't inherit from the snapshot

hmp_cmds="snapshot_blkdev -n none0 $TEST_IMG.snap $IMGFMT
qemu-io none0 \"reopen -c none\"
info block none0
info block image
info block file
info block backing
info block backing-file"

check_cache_all

echo
echo "--- Change cache mode in parent, child has explicit option in JSON ---"
echo

# This checks that children with options explicitly set by the json:
# pseudo-protocol don't inherit these options from their parents.
#
# Yes, blkdebug::json:... is criminal, but I can't see another way to have a
# BDS initialised with the json: pseudo-protocol, but still have it inherit
# options from its parent node.

hmp_cmds="qemu-io none0 \"reopen -o cache.direct=on,cache.no-flush=on\"
info block none0
info block image
info block blkdebug
info block file"

echo "$hmp_cmds" | run_qemu -drive if=none,file="blkdebug::json:{\"filename\":\"$TEST_IMG\",,\"cache\":{\"direct\":false}}",node-name=image,file.node-name=blkdebug,file.image.node-name=file | grep "Cache"

echo
echo "=== Check that referenced BDSes don't inherit ==="
echo

drv_bkfile="if=none,driver=file,filename=$TEST_IMG.base,node-name=backing-file"
drv_bk="if=none,file=json:{'driver':'$IMGFMT',,'file':'backing-file',,'node-name':'backing'}"
drv_file="if=none,driver=file,filename=$TEST_IMG,node-name=file"
drv_img="if=none,id=blk,file=json:{'driver':'$IMGFMT',,'file':'file',,'backing':'backing',,'node-name':'image'}"

function check_cache_all_separate()
{
    # Check cache.direct

    echo -e "cache.direct=on on blk"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.direct=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.direct=on on file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.direct=on -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.direct=on on backing"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.direct=on -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.direct=on on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.direct=on -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"

    # Check cache.writeback

    echo -e "\n\ncache.writeback=off on blk"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.writeback=off | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.writeback=off on file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.writeback=off -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.writeback=off on backing"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.writeback=off -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.writeback=off on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.writeback=off -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"

    # Check cache.no-flush

    echo -e "\n\ncache.no-flush=on on blk"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img",cache.no-flush=on | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk" -drive "$drv_file",cache.no-flush=on -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on backing"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile" -drive "$drv_bk",cache.no-flush=on -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
    echo -e "\ncache.no-flush=on on backing-file"
    echo "$hmp_cmds" | run_qemu -drive "$drv_bkfile",cache.no-flush=on -drive "$drv_bk" -drive "$drv_file" -drive "$drv_img" | grep -e "Cache" -e "[Cc]annot\|[Cc]ould not\|[Cc]an't"
}

echo
echo "--- Configure cache modes on the command line ---"
echo

# First check the inherited cache mode after opening the image.

hmp_cmds="info block image
info block file
info block backing
info block backing-file"

check_cache_all_separate

echo
echo "--- Cache modes after reopen (live snapshot) ---"
echo

# Then trigger a reopen and check that the cache modes are still the same.

hmp_cmds="snapshot_blkdev -n blk $TEST_IMG.snap $IMGFMT
info block blk
info block image
info block file
info block backing
info block backing-file"

check_cache_all_separate

echo
echo "--- Change cache modes with reopen (qemu-io command, flags) ---"
echo

# This one actually changes the cache mode with the reopen. For this test, the
# new cache mode is specified as flags, not as option.

hmp_cmds='qemu-io blk "reopen -c none"
info block image
info block file
info block backing
info block backing-file'

check_cache_all_separate


echo
echo "=== Reopening children instead of the root ==="
echo

files="if=none,file=$TEST_IMG,backing.file.filename=$TEST_IMG.base"
ids="node-name=image,backing.node-name=backing,backing.file.node-name=backing-file,file.node-name=file"

echo
echo "--- Basic reopen ---"
echo

hmp_cmds='qemu-io none0 "reopen -o backing.cache.direct=on"
info block none0
info block image
info block file
info block backing
info block backing-file'

check_cache_all

echo
echo "--- Change cache mode after reopening child ---"
echo

# This checks that children with options explicitly set with reopen don't
# inherit these options from their parents any more

# TODO Implement node-name support for 'qemu-io' HMP command for -c
# Can use only -o to access child node options for now

hmp_cmds="qemu-io none0 \"reopen -o file.cache.direct=off,file.cache.no-flush=off\"
qemu-io none0 \"reopen -o backing.file.cache.direct=off,backing.file.cache.no-flush=on\"
qemu-io none0 \"reopen -c none\"
info block image
info block file
info block backing
info block backing-file"

echo "$hmp_cmds" | run_qemu -drive "$files","$ids" | grep "Cache"

# success, all done
echo "*** done"
rm -f $seq.full
status=0
n class="mi">0; i < n_elems; i++) { void *addr = base_addr + size * i; if (field->flags & VMS_ARRAY_OF_POINTER) { addr = *(void **)addr; } if (field->flags & VMS_STRUCT) { ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id); } else { ret = field->info->get(f, addr, size); } if (ret >= 0) { ret = qemu_file_get_error(f); } if (ret < 0) { qemu_file_set_error(f, ret); trace_vmstate_load_field_error(field->name, ret); return ret; } } } else if (field->flags & VMS_MUST_EXIST) { error_report("Input validation failed: %s/%s", vmsd->name, field->name); return -1; } field++; } ret = vmstate_subsection_load(f, vmsd, opaque); if (ret != 0) { return ret; } if (vmsd->post_load) { ret = vmsd->post_load(opaque, version_id); } trace_vmstate_load_state_end(vmsd->name, "end", ret); return ret; } static int vmfield_name_num(VMStateField *start, VMStateField *search) { VMStateField *field; int found = 0; for (field = start; field->name; field++) { if (!strcmp(field->name, search->name)) { if (field == search) { return found; } found++; } } return -1; } static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search) { VMStateField *field; int found = 0; for (field = start; field->name; field++) { if (!strcmp(field->name, search->name)) { found++; /* name found more than once, so it's not unique */ if (found > 1) { return false; } } } return true; } static const char *vmfield_get_type_name(VMStateField *field) { const char *type = "unknown"; if (field->flags & VMS_STRUCT) { type = "struct"; } else if (field->info->name) { type = field->info->name; } return type; } static bool vmsd_can_compress(VMStateField *field) { if (field->field_exists) { /* Dynamically existing fields mess up compression */ return false; } if (field->flags & VMS_STRUCT) { VMStateField *sfield = field->vmsd->fields; while (sfield->name) { if (!vmsd_can_compress(sfield)) { /* Child elements can't compress, so can't we */ return false; } sfield++; } if (field->vmsd->subsections) { /* Subsections may come and go, better don't compress */ return false; } } return true; } static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc, VMStateField *field, int i, int max) { char *name, *old_name; bool is_array = max > 1; bool can_compress = vmsd_can_compress(field); if (!vmdesc) { return; } name = g_strdup(field->name); /* Field name is not unique, need to make it unique */ if (!vmfield_name_is_unique(vmsd->fields, field)) { int num = vmfield_name_num(vmsd->fields, field); old_name = name; name = g_strdup_printf("%s[%d]", name, num); g_free(old_name); } json_start_object(vmdesc, NULL); json_prop_str(vmdesc, "name", name); if (is_array) { if (can_compress) { json_prop_int(vmdesc, "array_len", max); } else { json_prop_int(vmdesc, "index", i); } } json_prop_str(vmdesc, "type", vmfield_get_type_name(field)); if (field->flags & VMS_STRUCT) { json_start_object(vmdesc, "struct"); } g_free(name); } static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc, VMStateField *field, size_t size, int i) { if (!vmdesc) { return; } if (field->flags & VMS_STRUCT) { /* We printed a struct in between, close its child object */ json_end_object(vmdesc); } json_prop_int(vmdesc, "size", size); json_end_object(vmdesc); } bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque) { if (vmsd->needed && !vmsd->needed(opaque)) { /* optional section not needed */ return false; } return true; } void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, void *opaque, QJSON *vmdesc) { VMStateField *field = vmsd->fields; if (vmsd->pre_save) { vmsd->pre_save(opaque); } if (vmdesc) { json_prop_str(vmdesc, "vmsd_name", vmsd->name); json_prop_int(vmdesc, "version", vmsd->version_id); json_start_array(vmdesc, "fields"); } while (field->name) { if (!field->field_exists || field->field_exists(opaque, vmsd->version_id)) { void *base_addr = vmstate_base_addr(opaque, field, false); int i, n_elems = vmstate_n_elems(opaque, field); int size = vmstate_size(opaque, field); int64_t old_offset, written_bytes; QJSON *vmdesc_loop = vmdesc; for (i = 0; i < n_elems; i++) { void *addr = base_addr + size * i; vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems); old_offset = qemu_ftell_fast(f); if (field->flags & VMS_ARRAY_OF_POINTER) { addr = *(void **)addr; } if (field->flags & VMS_STRUCT) { vmstate_save_state(f, field->vmsd, addr, vmdesc_loop); } else { field->info->put(f, addr, size); } written_bytes = qemu_ftell_fast(f) - old_offset; vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i); /* Compressed arrays only care about the first element */ if (vmdesc_loop && vmsd_can_compress(field)) { vmdesc_loop = NULL; } } } else { if (field->flags & VMS_MUST_EXIST) { error_report("Output state validation failed: %s/%s", vmsd->name, field->name); assert(!(field->flags & VMS_MUST_EXIST)); } } field++; } if (vmdesc) { json_end_array(vmdesc); } vmstate_subsection_save(f, vmsd, opaque, vmdesc); } static const VMStateDescription * vmstate_get_subsection(const VMStateDescription **sub, char *idstr) { while (sub && *sub && (*sub)->needed) { if (strcmp(idstr, (*sub)->name) == 0) { return *sub; } sub++; } return NULL; } static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, void *opaque) { trace_vmstate_subsection_load(vmsd->name); while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) { char idstr[256], *idstr_ret; int ret; uint8_t version_id, len, size; const VMStateDescription *sub_vmsd; len = qemu_peek_byte(f, 1); if (len < strlen(vmsd->name) + 1) { /* subsection name has be be "section_name/a" */ trace_vmstate_subsection_load_bad(vmsd->name, "(short)"); return 0; } size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2); if (size != len) { trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)"); return 0; } memcpy(idstr, idstr_ret, size); idstr[size] = 0; if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) { trace_vmstate_subsection_load_bad(vmsd->name, idstr); /* it don't have a valid subsection name */ return 0; } sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr); if (sub_vmsd == NULL) { trace_vmstate_subsection_load_bad(vmsd->name, "(lookup)"); return -ENOENT; } qemu_file_skip(f, 1); /* subsection */ qemu_file_skip(f, 1); /* len */ qemu_file_skip(f, len); /* idstr */ version_id = qemu_get_be32(f); ret = vmstate_load_state(f, sub_vmsd, opaque, version_id); if (ret) { trace_vmstate_subsection_load_bad(vmsd->name, "(child)"); return ret; } } trace_vmstate_subsection_load_good(vmsd->name); return 0; } static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, void *opaque, QJSON *vmdesc) { const VMStateDescription **sub = vmsd->subsections; bool subsection_found = false; while (sub && *sub && (*sub)->needed) { if ((*sub)->needed(opaque)) { const VMStateDescription *vmsd = *sub; uint8_t len; if (vmdesc) { /* Only create subsection array when we have any */ if (!subsection_found) { json_start_array(vmdesc, "subsections"); subsection_found = true; } json_start_object(vmdesc, NULL); } qemu_put_byte(f, QEMU_VM_SUBSECTION); len = strlen(vmsd->name); qemu_put_byte(f, len); qemu_put_buffer(f, (uint8_t *)vmsd->name, len); qemu_put_be32(f, vmsd->version_id); vmstate_save_state(f, vmsd, opaque, vmdesc); if (vmdesc) { json_end_object(vmdesc); } } sub++; } if (vmdesc && subsection_found) { json_end_array(vmdesc); } } /* bool */ static int get_bool(QEMUFile *f, void *pv, size_t size) { bool *v = pv; *v = qemu_get_byte(f); return 0; } static void put_bool(QEMUFile *f, void *pv, size_t size) { bool *v = pv; qemu_put_byte(f, *v); } const VMStateInfo vmstate_info_bool = { .name = "bool", .get = get_bool, .put = put_bool, }; /* 8 bit int */ static int get_int8(QEMUFile *f, void *pv, size_t size) { int8_t *v = pv; qemu_get_s8s(f, v); return 0; } static void put_int8(QEMUFile *f, void *pv, size_t size) { int8_t *v = pv; qemu_put_s8s(f, v); } const VMStateInfo vmstate_info_int8 = { .name = "int8", .get = get_int8, .put = put_int8, }; /* 16 bit int */ static int get_int16(QEMUFile *f, void *pv, size_t size) { int16_t *v = pv; qemu_get_sbe16s(f, v); return 0; } static void put_int16(QEMUFile *f, void *pv, size_t size) { int16_t *v = pv; qemu_put_sbe16s(f, v); } const VMStateInfo vmstate_info_int16 = { .name = "int16", .get = get_int16, .put = put_int16, }; /* 32 bit int */ static int get_int32(QEMUFile *f, void *pv, size_t size) { int32_t *v = pv; qemu_get_sbe32s(f, v); return 0; } static void put_int32(QEMUFile *f, void *pv, size_t size) { int32_t *v = pv; qemu_put_sbe32s(f, v); } const VMStateInfo vmstate_info_int32 = { .name = "int32", .get = get_int32, .put = put_int32, }; /* 32 bit int. See that the received value is the same than the one in the field */ static int get_int32_equal(QEMUFile *f, void *pv, size_t size) { int32_t *v = pv; int32_t v2; qemu_get_sbe32s(f, &v2); if (*v == v2) { return 0; } return -EINVAL; } const VMStateInfo vmstate_info_int32_equal = { .name = "int32 equal", .get = get_int32_equal, .put = put_int32, }; /* 32 bit int. Check that the received value is non-negative * and less than or equal to the one in the field. */ static int get_int32_le(QEMUFile *f, void *pv, size_t size) { int32_t *cur = pv; int32_t loaded; qemu_get_sbe32s(f, &loaded); if (loaded >= 0 && loaded <= *cur) { *cur = loaded; return 0; } return -EINVAL; } const VMStateInfo vmstate_info_int32_le = { .name = "int32 le", .get = get_int32_le, .put = put_int32, }; /* 64 bit int */ static int get_int64(QEMUFile *f, void *pv, size_t size) { int64_t *v = pv; qemu_get_sbe64s(f, v); return 0; } static void put_int64(QEMUFile *f, void *pv, size_t size) { int64_t *v = pv; qemu_put_sbe64s(f, v); } const VMStateInfo vmstate_info_int64 = { .name = "int64", .get = get_int64, .put = put_int64, }; /* 8 bit unsigned int */ static int get_uint8(QEMUFile *f, void *pv, size_t size) { uint8_t *v = pv; qemu_get_8s(f, v); return 0; } static void put_uint8(QEMUFile *f, void *pv, size_t size) { uint8_t *v = pv; qemu_put_8s(f, v); } const VMStateInfo vmstate_info_uint8 = { .name = "uint8", .get = get_uint8, .put = put_uint8, }; /* 16 bit unsigned int */ static int get_uint16(QEMUFile *f, void *pv, size_t size) { uint16_t *v = pv; qemu_get_be16s(f, v); return 0; } static void put_uint16(QEMUFile *f, void *pv, size_t size) { uint16_t *v = pv; qemu_put_be16s(f, v); } const VMStateInfo vmstate_info_uint16 = { .name = "uint16", .get = get_uint16, .put = put_uint16, }; /* 32 bit unsigned int */ static int get_uint32(QEMUFile *f, void *pv, size_t size) { uint32_t *v = pv; qemu_get_be32s(f, v); return 0; } static void put_uint32(QEMUFile *f, void *pv, size_t size) { uint32_t *v = pv; qemu_put_be32s(f, v); } const VMStateInfo vmstate_info_uint32 = { .name = "uint32", .get = get_uint32, .put = put_uint32, }; /* 32 bit uint. See that the received value is the same than the one in the field */ static int get_uint32_equal(QEMUFile *f, void *pv, size_t size) { uint32_t *v = pv; uint32_t v2; qemu_get_be32s(f, &v2); if (*v == v2) { return 0; } return -EINVAL; } const VMStateInfo vmstate_info_uint32_equal = { .name = "uint32 equal", .get = get_uint32_equal, .put = put_uint32, }; /* 64 bit unsigned int */ static int get_uint64(QEMUFile *f, void *pv, size_t size) { uint64_t *v = pv; qemu_get_be64s(f, v); return 0; } static void put_uint64(QEMUFile *f, void *pv, size_t size) { uint64_t *v = pv; qemu_put_be64s(f, v); } const VMStateInfo vmstate_info_uint64 = { .name = "uint64", .get = get_uint64, .put = put_uint64, }; /* 64 bit unsigned int. See that the received value is the same than the one in the field */ static int get_uint64_equal(QEMUFile *f, void *pv, size_t size) { uint64_t *v = pv; uint64_t v2; qemu_get_be64s(f, &v2); if (*v == v2) { return 0; } return -EINVAL; } const VMStateInfo vmstate_info_uint64_equal = { .name = "int64 equal", .get = get_uint64_equal, .put = put_uint64, }; /* 8 bit int. See that the received value is the same than the one in the field */ static int get_uint8_equal(QEMUFile *f, void *pv, size_t size) { uint8_t *v = pv; uint8_t v2; qemu_get_8s(f, &v2); if (*v == v2) { return 0; } return -EINVAL; } const VMStateInfo vmstate_info_uint8_equal = { .name = "uint8 equal", .get = get_uint8_equal, .put = put_uint8, }; /* 16 bit unsigned int int. See that the received value is the same than the one in the field */ static int get_uint16_equal(QEMUFile *f, void *pv, size_t size) { uint16_t *v = pv; uint16_t v2; qemu_get_be16s(f, &v2); if (*v == v2) { return 0; } return -EINVAL; } const VMStateInfo vmstate_info_uint16_equal = { .name = "uint16 equal", .get = get_uint16_equal, .put = put_uint16, }; /* floating point */ static int get_float64(QEMUFile *f, void *pv, size_t size) { float64 *v = pv; *v = make_float64(qemu_get_be64(f)); return 0; } static void put_float64(QEMUFile *f, void *pv, size_t size) { uint64_t *v = pv; qemu_put_be64(f, float64_val(*v)); } const VMStateInfo vmstate_info_float64 = { .name = "float64", .get = get_float64, .put = put_float64, }; /* CPU_DoubleU type */ static int get_cpudouble(QEMUFile *f, void *pv, size_t size) { CPU_DoubleU *v = pv; qemu_get_be32s(f, &v->l.upper); qemu_get_be32s(f, &v->l.lower); return 0; } static void put_cpudouble(QEMUFile *f, void *pv, size_t size) { CPU_DoubleU *v = pv; qemu_put_be32s(f, &v->l.upper); qemu_put_be32s(f, &v->l.lower); } const VMStateInfo vmstate_info_cpudouble = { .name = "CPU_Double_U", .get = get_cpudouble, .put = put_cpudouble, }; /* uint8_t buffers */ static int get_buffer(QEMUFile *f, void *pv, size_t size) { uint8_t *v = pv; qemu_get_buffer(f, v, size); return 0; } static void put_buffer(QEMUFile *f, void *pv, size_t size) { uint8_t *v = pv; qemu_put_buffer(f, v, size); } const VMStateInfo vmstate_info_buffer = { .name = "buffer", .get = get_buffer, .put = put_buffer, }; /* unused buffers: space that was used for some fields that are not useful anymore */ static int get_unused_buffer(QEMUFile *f, void *pv, size_t size) { uint8_t buf[1024]; int block_len; while (size > 0) { block_len = MIN(sizeof(buf), size); size -= block_len; qemu_get_buffer(f, buf, block_len); } return 0; } static void put_unused_buffer(QEMUFile *f, void *pv, size_t size) { static const uint8_t buf[1024]; int block_len; while (size > 0) { block_len = MIN(sizeof(buf), size); size -= block_len; qemu_put_buffer(f, buf, block_len); } } const VMStateInfo vmstate_info_unused_buffer = { .name = "unused_buffer", .get = get_unused_buffer, .put = put_unused_buffer, }; /* bitmaps (as defined by bitmap.h). Note that size here is the size * of the bitmap in bits. The on-the-wire format of a bitmap is 64 * bit words with the bits in big endian order. The in-memory format * is an array of 'unsigned long', which may be either 32 or 64 bits. */ /* This is the number of 64 bit words sent over the wire */ #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64) static int get_bitmap(QEMUFile *f, void *pv, size_t size) { unsigned long *bmp = pv; int i, idx = 0; for (i = 0; i < BITS_TO_U64S(size); i++) { uint64_t w = qemu_get_be64(f); bmp[idx++] = w; if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { bmp[idx++] = w >> 32; } } return 0; } static void put_bitmap(QEMUFile *f, void *pv, size_t size) { unsigned long *bmp = pv; int i, idx = 0; for (i = 0; i < BITS_TO_U64S(size); i++) { uint64_t w = bmp[idx++]; if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) { w |= ((uint64_t)bmp[idx++]) << 32; } qemu_put_be64(f, w); } } const VMStateInfo vmstate_info_bitmap = { .name = "bitmap", .get = get_bitmap, .put = put_bitmap, };