xref: /freebsd/sys/amd64/include/vmm_snapshot.h (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
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  * $FreeBSD$
36  */
37 
38 #ifndef _VMM_SNAPSHOT_
39 #define _VMM_SNAPSHOT_
40 
41 #include <sys/errno.h>
42 #include <sys/types.h>
43 #ifndef _KERNEL
44 #include <stdbool.h>
45 #endif
46 
47 struct vmctx;
48 
49 enum snapshot_req {
50 	STRUCT_VMX,
51 	STRUCT_VIOAPIC,
52 	STRUCT_VM,
53 	STRUCT_VLAPIC,
54 	VM_MEM,
55 	STRUCT_VHPET,
56 	STRUCT_VMCX,
57 	STRUCT_VATPIC,
58 	STRUCT_VATPIT,
59 	STRUCT_VPMTMR,
60 	STRUCT_VRTC,
61 };
62 
63 struct vm_snapshot_buffer {
64 	/*
65 	 * R/O for device-specific functions;
66 	 * written by generic snapshot functions.
67 	 */
68 	uint8_t *const buf_start;
69 	const size_t buf_size;
70 
71 	/*
72 	 * R/W for device-specific functions used to keep track of buffer
73 	 * current position and remaining size.
74 	 */
75 	uint8_t *buf;
76 	size_t buf_rem;
77 
78 	/*
79 	 * Length of the snapshot is either determined as (buf_size - buf_rem)
80 	 * or (buf - buf_start) -- the second variation returns a signed value
81 	 * so it may not be appropriate.
82 	 *
83 	 * Use vm_get_snapshot_size(meta).
84 	 */
85 };
86 
87 enum vm_snapshot_op {
88 	VM_SNAPSHOT_SAVE,
89 	VM_SNAPSHOT_RESTORE,
90 };
91 
92 struct vm_snapshot_meta {
93 	struct vmctx *ctx;
94 	void *dev_data;
95 	const char *dev_name;      /* identify userspace devices */
96 	enum snapshot_req dev_req; /* identify kernel structs */
97 
98 	struct vm_snapshot_buffer buffer;
99 
100 	enum vm_snapshot_op op;
101 };
102 
103 
104 void vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op);
105 int vm_snapshot_buf(volatile void *data, size_t data_size,
106 		    struct vm_snapshot_meta *meta);
107 size_t vm_get_snapshot_size(struct vm_snapshot_meta *meta);
108 int vm_snapshot_guest2host_addr(void **addrp, size_t len, bool restore_null,
109 				struct vm_snapshot_meta *meta);
110 int vm_snapshot_buf_cmp(volatile void *data, size_t data_size,
111 			      struct vm_snapshot_meta *meta);
112 
113 #define	SNAPSHOT_BUF_OR_LEAVE(DATA, LEN, META, RES, LABEL)			\
114 do {										\
115 	(RES) = vm_snapshot_buf((DATA), (LEN), (META));				\
116 	if ((RES) != 0) {							\
117 		vm_snapshot_buf_err(#DATA, (META)->op);				\
118 		goto LABEL;							\
119 	}									\
120 } while (0)
121 
122 #define	SNAPSHOT_VAR_OR_LEAVE(DATA, META, RES, LABEL)				\
123 	SNAPSHOT_BUF_OR_LEAVE(&(DATA), sizeof(DATA), (META), (RES), LABEL)
124 
125 /*
126  * Address variables are pointers to guest memory.
127  *
128  * When RNULL != 0, do not enforce invalid address checks; instead, make the
129  * pointer NULL at restore time.
130  */
131 #define	SNAPSHOT_GUEST2HOST_ADDR_OR_LEAVE(ADDR, LEN, RNULL, META, RES, LABEL)	\
132 do {										\
133 	(RES) = vm_snapshot_guest2host_addr((void **)&(ADDR), (LEN), (RNULL),	\
134 			(META));					\
135 	if ((RES) != 0) {							\
136 		if ((RES) == EFAULT)						\
137 			fprintf(stderr, "%s: invalid address: %s\r\n",		\
138 				__func__, #ADDR);				\
139 		goto LABEL;							\
140 	}									\
141 } while (0)
142 
143 /* compare the value in the meta buffer with the data */
144 #define	SNAPSHOT_BUF_CMP_OR_LEAVE(DATA, LEN, META, RES, LABEL)			\
145 do {										\
146 	(RES) = vm_snapshot_buf_cmp((DATA), (LEN), (META));			\
147 	if ((RES) != 0) {							\
148 		vm_snapshot_buf_err(#DATA, (META)->op);				\
149 		goto LABEL;							\
150 	}									\
151 } while (0)
152 
153 #define	SNAPSHOT_VAR_CMP_OR_LEAVE(DATA, META, RES, LABEL)			\
154 	SNAPSHOT_BUF_CMP_OR_LEAVE(&(DATA), sizeof(DATA), (META), (RES), LABEL)
155 
156 #endif
157