1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2016 Flavius Anton
5 * Copyright (c) 2016 Mihai Tiganus
6 * Copyright (c) 2016-2019 Mihai Carabas
7 * Copyright (c) 2017-2019 Darius Mihai
8 * Copyright (c) 2017-2019 Elena Mihailescu
9 * Copyright (c) 2018-2019 Sergiu Weisz
10 * All rights reserved.
11 * The bhyve-snapshot feature was developed under sponsorships
12 * from Matthew Grooms.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/types.h>
40 #include <sys/systm.h>
41
42 #include <machine/vmm_snapshot.h>
43
44 void
vm_snapshot_buf_err(const char * bufname,const enum vm_snapshot_op op)45 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op)
46 {
47 const char *opstr;
48
49 if (op == VM_SNAPSHOT_SAVE)
50 opstr = "save";
51 else if (op == VM_SNAPSHOT_RESTORE)
52 opstr = "restore";
53 else
54 opstr = "unknown";
55
56 printf("%s: snapshot-%s failed for %s\r\n", __func__, opstr, bufname);
57 }
58
59 int
vm_snapshot_buf(volatile void * data,size_t data_size,struct vm_snapshot_meta * meta)60 vm_snapshot_buf(volatile void *data, size_t data_size,
61 struct vm_snapshot_meta *meta)
62 {
63 struct vm_snapshot_buffer *buffer;
64 int op;
65 void *nv_data;
66
67 nv_data = __DEVOLATILE(void *, data);
68 buffer = &meta->buffer;
69 op = meta->op;
70
71 if (buffer->buf_rem < data_size) {
72 printf("%s: buffer too small\r\n", __func__);
73 return (E2BIG);
74 }
75
76 if (op == VM_SNAPSHOT_SAVE)
77 copyout(nv_data, buffer->buf, data_size);
78 else if (op == VM_SNAPSHOT_RESTORE)
79 copyin(buffer->buf, nv_data, data_size);
80 else
81 return (EINVAL);
82
83 buffer->buf += data_size;
84 buffer->buf_rem -= data_size;
85
86 return (0);
87 }
88
89 size_t
vm_get_snapshot_size(struct vm_snapshot_meta * meta)90 vm_get_snapshot_size(struct vm_snapshot_meta *meta)
91 {
92 size_t length;
93 struct vm_snapshot_buffer *buffer;
94
95 buffer = &meta->buffer;
96
97 if (buffer->buf_size < buffer->buf_rem) {
98 printf("%s: Invalid buffer: size = %zu, rem = %zu\r\n",
99 __func__, buffer->buf_size, buffer->buf_rem);
100 length = 0;
101 } else {
102 length = buffer->buf_size - buffer->buf_rem;
103 }
104
105 return (length);
106 }
107
108 int
vm_snapshot_buf_cmp(volatile void * data,size_t data_size,struct vm_snapshot_meta * meta)109 vm_snapshot_buf_cmp(volatile void *data, size_t data_size,
110 struct vm_snapshot_meta *meta)
111 {
112 struct vm_snapshot_buffer *buffer;
113 int op;
114 int ret;
115 void *_data = *(void **)(void *)&data;
116
117 buffer = &meta->buffer;
118 op = meta->op;
119
120 if (buffer->buf_rem < data_size) {
121 printf("%s: buffer too small\r\n", __func__);
122 ret = E2BIG;
123 goto done;
124 }
125
126 if (op == VM_SNAPSHOT_SAVE) {
127 ret = 0;
128 copyout(_data, buffer->buf, data_size);
129 } else if (op == VM_SNAPSHOT_RESTORE) {
130 ret = memcmp(_data, buffer->buf, data_size);
131 } else {
132 ret = EINVAL;
133 goto done;
134 }
135
136 buffer->buf += data_size;
137 buffer->buf_rem -= data_size;
138
139 done:
140 return (ret);
141 }
142