xref: /freebsd/usr.sbin/bhyve/snapshot.c (revision b1c5f60ce87cc2f179dfb81de507d9b7bf59564c)
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 #ifndef WITHOUT_CAPSICUM
41 #include <sys/capsicum.h>
42 #endif
43 #include <sys/mman.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/un.h>
48 
49 #include <machine/atomic.h>
50 #include <machine/segments.h>
51 
52 #ifndef WITHOUT_CAPSICUM
53 #include <capsicum_helpers.h>
54 #endif
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <libgen.h>
62 #include <signal.h>
63 #include <unistd.h>
64 #include <assert.h>
65 #include <errno.h>
66 #include <pthread.h>
67 #include <pthread_np.h>
68 #include <sysexits.h>
69 #include <stdbool.h>
70 #include <sys/ioctl.h>
71 
72 #include <machine/vmm.h>
73 #ifndef WITHOUT_CAPSICUM
74 #include <machine/vmm_dev.h>
75 #endif
76 #include <machine/vmm_snapshot.h>
77 #include <vmmapi.h>
78 
79 #include "bhyverun.h"
80 #include "acpi.h"
81 #include "atkbdc.h"
82 #include "debug.h"
83 #include "inout.h"
84 #include "fwctl.h"
85 #include "ioapic.h"
86 #include "mem.h"
87 #include "mevent.h"
88 #include "mptbl.h"
89 #include "pci_emul.h"
90 #include "pci_irq.h"
91 #include "pci_lpc.h"
92 #include "smbiostbl.h"
93 #include "snapshot.h"
94 #include "xmsr.h"
95 #include "spinup_ap.h"
96 #include "rtc.h"
97 
98 #include <libxo/xo.h>
99 #include <ucl.h>
100 
101 struct spinner_info {
102 	const size_t *crtval;
103 	const size_t maxval;
104 	const size_t total;
105 };
106 
107 extern int guest_ncpus;
108 
109 static struct winsize winsize;
110 static sig_t old_winch_handler;
111 
112 #define	KB		(1024UL)
113 #define	MB		(1024UL * KB)
114 #define	GB		(1024UL * MB)
115 
116 #define	SNAPSHOT_CHUNK	(4 * MB)
117 #define	PROG_BUF_SZ	(8192)
118 
119 #define	SNAPSHOT_BUFFER_SIZE (20 * MB)
120 
121 #define	JSON_STRUCT_ARR_KEY		"structs"
122 #define	JSON_DEV_ARR_KEY		"devices"
123 #define	JSON_BASIC_METADATA_KEY 	"basic metadata"
124 #define	JSON_SNAPSHOT_REQ_KEY		"snapshot_req"
125 #define	JSON_SIZE_KEY			"size"
126 #define	JSON_FILE_OFFSET_KEY		"file_offset"
127 
128 #define	JSON_NCPUS_KEY			"ncpus"
129 #define	JSON_VMNAME_KEY 		"vmname"
130 #define	JSON_MEMSIZE_KEY		"memsize"
131 #define	JSON_MEMFLAGS_KEY		"memflags"
132 
133 #define min(a,b)		\
134 ({				\
135  __typeof__ (a) _a = (a);	\
136  __typeof__ (b) _b = (b); 	\
137  _a < _b ? _a : _b;       	\
138  })
139 
140 const struct vm_snapshot_dev_info snapshot_devs[] = {
141 	{ "atkbdc",	atkbdc_snapshot,	NULL,		NULL		},
142 	{ "virtio-net",	pci_snapshot,		pci_pause,	pci_resume	},
143 	{ "virtio-blk",	pci_snapshot,		pci_pause,	pci_resume	},
144 	{ "virtio-rnd",	pci_snapshot,		NULL,		NULL		},
145 	{ "lpc",	pci_snapshot,		NULL,		NULL		},
146 	{ "fbuf",	pci_snapshot,		NULL,		NULL		},
147 	{ "xhci",	pci_snapshot,		NULL,		NULL		},
148 	{ "e1000",	pci_snapshot,		NULL,		NULL		},
149 	{ "ahci",	pci_snapshot,		pci_pause,	pci_resume	},
150 	{ "ahci-hd",	pci_snapshot,		pci_pause,	pci_resume	},
151 	{ "ahci-cd",	pci_snapshot,		pci_pause,	pci_resume	},
152 };
153 
154 const struct vm_snapshot_kern_info snapshot_kern_structs[] = {
155 	{ "vhpet",	STRUCT_VHPET	},
156 	{ "vm",		STRUCT_VM	},
157 	{ "vmx",	STRUCT_VMX	},
158 	{ "vioapic",	STRUCT_VIOAPIC	},
159 	{ "vlapic",	STRUCT_VLAPIC	},
160 	{ "vmcx",	STRUCT_VMCX	},
161 	{ "vatpit",	STRUCT_VATPIT	},
162 	{ "vatpic",	STRUCT_VATPIC	},
163 	{ "vpmtmr",	STRUCT_VPMTMR	},
164 	{ "vrtc",	STRUCT_VRTC	},
165 };
166 
167 static cpuset_t vcpus_active, vcpus_suspended;
168 static pthread_mutex_t vcpu_lock;
169 static pthread_cond_t vcpus_idle, vcpus_can_run;
170 static bool checkpoint_active;
171 
172 /*
173  * TODO: Harden this function and all of its callers since 'base_str' is a user
174  * provided string.
175  */
176 static char *
177 strcat_extension(const char *base_str, const char *ext)
178 {
179 	char *res;
180 	size_t base_len, ext_len;
181 
182 	base_len = strnlen(base_str, NAME_MAX);
183 	ext_len = strnlen(ext, NAME_MAX);
184 
185 	if (base_len + ext_len > NAME_MAX) {
186 		fprintf(stderr, "Filename exceeds maximum length.\n");
187 		return (NULL);
188 	}
189 
190 	res = malloc(base_len + ext_len + 1);
191 	if (res == NULL) {
192 		perror("Failed to allocate memory.");
193 		return (NULL);
194 	}
195 
196 	memcpy(res, base_str, base_len);
197 	memcpy(res + base_len, ext, ext_len);
198 	res[base_len + ext_len] = 0;
199 
200 	return (res);
201 }
202 
203 void
204 destroy_restore_state(struct restore_state *rstate)
205 {
206 	if (rstate == NULL) {
207 		fprintf(stderr, "Attempting to destroy NULL restore struct.\n");
208 		return;
209 	}
210 
211 	if (rstate->kdata_map != MAP_FAILED)
212 		munmap(rstate->kdata_map, rstate->kdata_len);
213 
214 	if (rstate->kdata_fd > 0)
215 		close(rstate->kdata_fd);
216 	if (rstate->vmmem_fd > 0)
217 		close(rstate->vmmem_fd);
218 
219 	if (rstate->meta_root_obj != NULL)
220 		ucl_object_unref(rstate->meta_root_obj);
221 	if (rstate->meta_parser != NULL)
222 		ucl_parser_free(rstate->meta_parser);
223 }
224 
225 static int
226 load_vmmem_file(const char *filename, struct restore_state *rstate)
227 {
228 	struct stat sb;
229 	int err;
230 
231 	rstate->vmmem_fd = open(filename, O_RDONLY);
232 	if (rstate->vmmem_fd < 0) {
233 		perror("Failed to open restore file");
234 		return (-1);
235 	}
236 
237 	err = fstat(rstate->vmmem_fd, &sb);
238 	if (err < 0) {
239 		perror("Failed to stat restore file");
240 		goto err_load_vmmem;
241 	}
242 
243 	if (sb.st_size == 0) {
244 		fprintf(stderr, "Restore file is empty.\n");
245 		goto err_load_vmmem;
246 	}
247 
248 	rstate->vmmem_len = sb.st_size;
249 
250 	return (0);
251 
252 err_load_vmmem:
253 	if (rstate->vmmem_fd > 0)
254 		close(rstate->vmmem_fd);
255 	return (-1);
256 }
257 
258 static int
259 load_kdata_file(const char *filename, struct restore_state *rstate)
260 {
261 	struct stat sb;
262 	int err;
263 
264 	rstate->kdata_fd = open(filename, O_RDONLY);
265 	if (rstate->kdata_fd < 0) {
266 		perror("Failed to open kernel data file");
267 		return (-1);
268 	}
269 
270 	err = fstat(rstate->kdata_fd, &sb);
271 	if (err < 0) {
272 		perror("Failed to stat kernel data file");
273 		goto err_load_kdata;
274 	}
275 
276 	if (sb.st_size == 0) {
277 		fprintf(stderr, "Kernel data file is empty.\n");
278 		goto err_load_kdata;
279 	}
280 
281 	rstate->kdata_len = sb.st_size;
282 	rstate->kdata_map = mmap(NULL, rstate->kdata_len, PROT_READ,
283 				 MAP_SHARED, rstate->kdata_fd, 0);
284 	if (rstate->kdata_map == MAP_FAILED) {
285 		perror("Failed to map restore file");
286 		goto err_load_kdata;
287 	}
288 
289 	return (0);
290 
291 err_load_kdata:
292 	if (rstate->kdata_fd > 0)
293 		close(rstate->kdata_fd);
294 	return (-1);
295 }
296 
297 static int
298 load_metadata_file(const char *filename, struct restore_state *rstate)
299 {
300 	const ucl_object_t *obj;
301 	struct ucl_parser *parser;
302 	int err;
303 
304 	parser = ucl_parser_new(UCL_PARSER_DEFAULT);
305 	if (parser == NULL) {
306 		fprintf(stderr, "Failed to initialize UCL parser.\n");
307 		goto err_load_metadata;
308 	}
309 
310 	err = ucl_parser_add_file(parser, filename);
311 	if (err == 0) {
312 		fprintf(stderr, "Failed to parse metadata file: '%s'\n",
313 			filename);
314 		err = -1;
315 		goto err_load_metadata;
316 	}
317 
318 	obj = ucl_parser_get_object(parser);
319 	if (obj == NULL) {
320 		fprintf(stderr, "Failed to parse object.\n");
321 		err = -1;
322 		goto err_load_metadata;
323 	}
324 
325 	rstate->meta_parser = parser;
326 	rstate->meta_root_obj = (ucl_object_t *)obj;
327 
328 	return (0);
329 
330 err_load_metadata:
331 	if (parser != NULL)
332 		ucl_parser_free(parser);
333 	return (err);
334 }
335 
336 int
337 load_restore_file(const char *filename, struct restore_state *rstate)
338 {
339 	int err = 0;
340 	char *kdata_filename = NULL, *meta_filename = NULL;
341 
342 	assert(filename != NULL);
343 	assert(rstate != NULL);
344 
345 	memset(rstate, 0, sizeof(*rstate));
346 	rstate->kdata_map = MAP_FAILED;
347 
348 	err = load_vmmem_file(filename, rstate);
349 	if (err != 0) {
350 		fprintf(stderr, "Failed to load guest RAM file.\n");
351 		goto err_restore;
352 	}
353 
354 	kdata_filename = strcat_extension(filename, ".kern");
355 	if (kdata_filename == NULL) {
356 		fprintf(stderr, "Failed to construct kernel data filename.\n");
357 		goto err_restore;
358 	}
359 
360 	err = load_kdata_file(kdata_filename, rstate);
361 	if (err != 0) {
362 		fprintf(stderr, "Failed to load guest kernel data file.\n");
363 		goto err_restore;
364 	}
365 
366 	meta_filename = strcat_extension(filename, ".meta");
367 	if (meta_filename == NULL) {
368 		fprintf(stderr, "Failed to construct kernel metadata filename.\n");
369 		goto err_restore;
370 	}
371 
372 	err = load_metadata_file(meta_filename, rstate);
373 	if (err != 0) {
374 		fprintf(stderr, "Failed to load guest metadata file.\n");
375 		goto err_restore;
376 	}
377 
378 	return (0);
379 
380 err_restore:
381 	destroy_restore_state(rstate);
382 	if (kdata_filename != NULL)
383 		free(kdata_filename);
384 	if (meta_filename != NULL)
385 		free(meta_filename);
386 	return (-1);
387 }
388 
389 #define JSON_GET_INT_OR_RETURN(key, obj, result_ptr, ret)			\
390 do {										\
391 	const ucl_object_t *obj__;						\
392 	obj__ = ucl_object_lookup(obj, key);					\
393 	if (obj__ == NULL) {							\
394 		fprintf(stderr, "Missing key: '%s'", key);			\
395 		return (ret);							\
396 	}									\
397 	if (!ucl_object_toint_safe(obj__, result_ptr)) {			\
398 		fprintf(stderr, "Cannot convert '%s' value to int.", key);	\
399 		return (ret);							\
400 	}									\
401 } while(0)
402 
403 #define JSON_GET_STRING_OR_RETURN(key, obj, result_ptr, ret)			\
404 do {										\
405 	const ucl_object_t *obj__;						\
406 	obj__ = ucl_object_lookup(obj, key);					\
407 	if (obj__ == NULL) {							\
408 		fprintf(stderr, "Missing key: '%s'", key);			\
409 		return (ret);							\
410 	}									\
411 	if (!ucl_object_tostring_safe(obj__, result_ptr)) {			\
412 		fprintf(stderr, "Cannot convert '%s' value to string.", key);	\
413 		return (ret);							\
414 	}									\
415 } while(0)
416 
417 static void *
418 lookup_struct(enum snapshot_req struct_id, struct restore_state *rstate,
419 	      size_t *struct_size)
420 {
421 	const ucl_object_t *structs = NULL, *obj = NULL;
422 	ucl_object_iter_t it = NULL;
423 	int64_t snapshot_req, size, file_offset;
424 
425 	structs = ucl_object_lookup(rstate->meta_root_obj, JSON_STRUCT_ARR_KEY);
426 	if (structs == NULL) {
427 		fprintf(stderr, "Failed to find '%s' object.\n",
428 			JSON_STRUCT_ARR_KEY);
429 		return (NULL);
430 	}
431 
432 	if (ucl_object_type((ucl_object_t *)structs) != UCL_ARRAY) {
433 		fprintf(stderr, "Object '%s' is not an array.\n",
434 		JSON_STRUCT_ARR_KEY);
435 		return (NULL);
436 	}
437 
438 	while ((obj = ucl_object_iterate(structs, &it, true)) != NULL) {
439 		snapshot_req = -1;
440 		JSON_GET_INT_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj,
441 				       &snapshot_req, NULL);
442 		assert(snapshot_req >= 0);
443 		if ((enum snapshot_req) snapshot_req == struct_id) {
444 			JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj,
445 					       &size, NULL);
446 			assert(size >= 0);
447 
448 			JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj,
449 					       &file_offset, NULL);
450 			assert(file_offset >= 0);
451 			assert(file_offset + size <= rstate->kdata_len);
452 
453 			*struct_size = (size_t)size;
454 			return (rstate->kdata_map + file_offset);
455 		}
456 	}
457 
458 	return (NULL);
459 }
460 
461 static void *
462 lookup_check_dev(const char *dev_name, struct restore_state *rstate,
463 		 const ucl_object_t *obj, size_t *data_size)
464 {
465 	const char *snapshot_req;
466 	int64_t size, file_offset;
467 
468 	snapshot_req = NULL;
469 	JSON_GET_STRING_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj,
470 				  &snapshot_req, NULL);
471 	assert(snapshot_req != NULL);
472 	if (!strcmp(snapshot_req, dev_name)) {
473 		JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj,
474 				       &size, NULL);
475 		assert(size >= 0);
476 
477 		JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj,
478 				       &file_offset, NULL);
479 		assert(file_offset >= 0);
480 		assert(file_offset + size <= rstate->kdata_len);
481 
482 		*data_size = (size_t)size;
483 		return (rstate->kdata_map + file_offset);
484 	}
485 
486 	return (NULL);
487 }
488 
489 static void*
490 lookup_dev(const char *dev_name, struct restore_state *rstate,
491 	   size_t *data_size)
492 {
493 	const ucl_object_t *devs = NULL, *obj = NULL;
494 	ucl_object_iter_t it = NULL;
495 	void *ret;
496 
497 	devs = ucl_object_lookup(rstate->meta_root_obj, JSON_DEV_ARR_KEY);
498 	if (devs == NULL) {
499 		fprintf(stderr, "Failed to find '%s' object.\n",
500 			JSON_DEV_ARR_KEY);
501 		return (NULL);
502 	}
503 
504 	if (ucl_object_type((ucl_object_t *)devs) != UCL_ARRAY) {
505 		fprintf(stderr, "Object '%s' is not an array.\n",
506 			JSON_DEV_ARR_KEY);
507 		return (NULL);
508 	}
509 
510 	while ((obj = ucl_object_iterate(devs, &it, true)) != NULL) {
511 		ret = lookup_check_dev(dev_name, rstate, obj, data_size);
512 		if (ret != NULL)
513 			return (ret);
514 	}
515 
516 	return (NULL);
517 }
518 
519 static const ucl_object_t *
520 lookup_basic_metadata_object(struct restore_state *rstate)
521 {
522 	const ucl_object_t *basic_meta_obj = NULL;
523 
524 	basic_meta_obj = ucl_object_lookup(rstate->meta_root_obj,
525 					   JSON_BASIC_METADATA_KEY);
526 	if (basic_meta_obj == NULL) {
527 		fprintf(stderr, "Failed to find '%s' object.\n",
528 			JSON_BASIC_METADATA_KEY);
529 		return (NULL);
530 	}
531 
532 	if (ucl_object_type((ucl_object_t *)basic_meta_obj) != UCL_OBJECT) {
533 		fprintf(stderr, "Object '%s' is not a JSON object.\n",
534 		JSON_BASIC_METADATA_KEY);
535 		return (NULL);
536 	}
537 
538 	return (basic_meta_obj);
539 }
540 
541 const char *
542 lookup_vmname(struct restore_state *rstate)
543 {
544 	const char *vmname;
545 	const ucl_object_t *obj;
546 
547 	obj = lookup_basic_metadata_object(rstate);
548 	if (obj == NULL)
549 		return (NULL);
550 
551 	JSON_GET_STRING_OR_RETURN(JSON_VMNAME_KEY, obj, &vmname, NULL);
552 	return (vmname);
553 }
554 
555 int
556 lookup_memflags(struct restore_state *rstate)
557 {
558 	int64_t memflags;
559 	const ucl_object_t *obj;
560 
561 	obj = lookup_basic_metadata_object(rstate);
562 	if (obj == NULL)
563 		return (0);
564 
565 	JSON_GET_INT_OR_RETURN(JSON_MEMFLAGS_KEY, obj, &memflags, 0);
566 
567 	return ((int)memflags);
568 }
569 
570 size_t
571 lookup_memsize(struct restore_state *rstate)
572 {
573 	int64_t memsize;
574 	const ucl_object_t *obj;
575 
576 	obj = lookup_basic_metadata_object(rstate);
577 	if (obj == NULL)
578 		return (0);
579 
580 	JSON_GET_INT_OR_RETURN(JSON_MEMSIZE_KEY, obj, &memsize, 0);
581 	if (memsize < 0)
582 		memsize = 0;
583 
584 	return ((size_t)memsize);
585 }
586 
587 
588 int
589 lookup_guest_ncpus(struct restore_state *rstate)
590 {
591 	int64_t ncpus;
592 	const ucl_object_t *obj;
593 
594 	obj = lookup_basic_metadata_object(rstate);
595 	if (obj == NULL)
596 		return (0);
597 
598 	JSON_GET_INT_OR_RETURN(JSON_NCPUS_KEY, obj, &ncpus, 0);
599 	return ((int)ncpus);
600 }
601 
602 static void
603 winch_handler(int signal)
604 {
605 #ifdef TIOCGWINSZ
606 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
607 #endif /* TIOCGWINSZ */
608 }
609 
610 static int
611 print_progress(size_t crtval, const size_t maxval)
612 {
613 	size_t rc;
614 	double crtval_gb, maxval_gb;
615 	size_t i, win_width, prog_start, prog_done, prog_end;
616 	int mval_len;
617 
618 	static char prog_buf[PROG_BUF_SZ];
619 	static const size_t len = sizeof(prog_buf);
620 
621 	static size_t div;
622 	static char *div_str;
623 
624 	static char wip_bar[] = { '/', '-', '\\', '|' };
625 	static int wip_idx = 0;
626 
627 	if (maxval == 0) {
628 		printf("[0B / 0B]\r\n");
629 		return (0);
630 	}
631 
632 	if (crtval > maxval)
633 		crtval = maxval;
634 
635 	if (maxval > 10 * GB) {
636 		div = GB;
637 		div_str = "GiB";
638 	} else if (maxval > 10 * MB) {
639 		div = MB;
640 		div_str = "MiB";
641 	} else {
642 		div = KB;
643 		div_str = "KiB";
644 	}
645 
646 	crtval_gb = (double) crtval / div;
647 	maxval_gb = (double) maxval / div;
648 
649 	rc = snprintf(prog_buf, len, "%.03lf", maxval_gb);
650 	if (rc == len) {
651 		fprintf(stderr, "Maxval too big\n");
652 		return (-1);
653 	}
654 	mval_len = rc;
655 
656 	rc = snprintf(prog_buf, len, "\r[%*.03lf%s / %.03lf%s] |",
657 		mval_len, crtval_gb, div_str, maxval_gb, div_str);
658 
659 	if (rc == len) {
660 		fprintf(stderr, "Buffer too small to print progress\n");
661 		return (-1);
662 	}
663 
664 	win_width = min(winsize.ws_col, len);
665 	prog_start = rc;
666 
667 	if (prog_start < (win_width - 2)) {
668 		prog_end = win_width - prog_start - 2;
669 		prog_done = prog_end * (crtval_gb / maxval_gb);
670 
671 		for (i = prog_start; i < prog_start + prog_done; i++)
672 			prog_buf[i] = '#';
673 
674 		if (crtval != maxval) {
675 			prog_buf[i] = wip_bar[wip_idx];
676 			wip_idx = (wip_idx + 1) % sizeof(wip_bar);
677 			i++;
678 		} else {
679 			prog_buf[i++] = '#';
680 		}
681 
682 		for (; i < win_width - 2; i++)
683 			prog_buf[i] = '_';
684 
685 		prog_buf[win_width - 2] = '|';
686 	}
687 
688 	prog_buf[win_width - 1] = '\0';
689 	write(STDOUT_FILENO, prog_buf, win_width);
690 
691 	return (0);
692 }
693 
694 static void *
695 snapshot_spinner_cb(void *arg)
696 {
697 	int rc;
698 	size_t crtval, maxval, total;
699 	struct spinner_info *si;
700 	struct timespec ts;
701 
702 	si = arg;
703 	if (si == NULL)
704 		pthread_exit(NULL);
705 
706 	ts.tv_sec = 0;
707 	ts.tv_nsec = 50 * 1000 * 1000; /* 50 ms sleep time */
708 
709 	do {
710 		crtval = *si->crtval;
711 		maxval = si->maxval;
712 		total = si->total;
713 
714 		rc = print_progress(crtval, total);
715 		if (rc < 0) {
716 			fprintf(stderr, "Failed to parse progress\n");
717 			break;
718 		}
719 
720 		nanosleep(&ts, NULL);
721 	} while (crtval < maxval);
722 
723 	pthread_exit(NULL);
724 	return NULL;
725 }
726 
727 static int
728 vm_snapshot_mem_part(const int snapfd, const size_t foff, void *src,
729 		     const size_t len, const size_t totalmem, const bool op_wr)
730 {
731 	int rc;
732 	size_t part_done, todo, rem;
733 	ssize_t done;
734 	bool show_progress;
735 	pthread_t spinner_th;
736 	struct spinner_info *si;
737 
738 	if (lseek(snapfd, foff, SEEK_SET) < 0) {
739 		perror("Failed to change file offset");
740 		return (-1);
741 	}
742 
743 	show_progress = false;
744 	if (isatty(STDIN_FILENO) && (winsize.ws_col != 0))
745 		show_progress = true;
746 
747 	part_done = foff;
748 	rem = len;
749 
750 	if (show_progress) {
751 		si = &(struct spinner_info) {
752 			.crtval = &part_done,
753 			.maxval = foff + len,
754 			.total = totalmem
755 		};
756 
757 		rc = pthread_create(&spinner_th, 0, snapshot_spinner_cb, si);
758 		if (rc) {
759 			perror("Unable to create spinner thread");
760 			show_progress = false;
761 		}
762 	}
763 
764 	while (rem > 0) {
765 		if (show_progress)
766 			todo = min(SNAPSHOT_CHUNK, rem);
767 		else
768 			todo = rem;
769 
770 		if (op_wr)
771 			done = write(snapfd, src, todo);
772 		else
773 			done = read(snapfd, src, todo);
774 		if (done < 0) {
775 			perror("Failed to write in file");
776 			return (-1);
777 		}
778 
779 		src += done;
780 		part_done += done;
781 		rem -= done;
782 	}
783 
784 	if (show_progress) {
785 		rc = pthread_join(spinner_th, NULL);
786 		if (rc)
787 			perror("Unable to end spinner thread");
788 	}
789 
790 	return (0);
791 }
792 
793 static size_t
794 vm_snapshot_mem(struct vmctx *ctx, int snapfd, size_t memsz, const bool op_wr)
795 {
796 	int ret;
797 	size_t lowmem, highmem, totalmem;
798 	char *baseaddr;
799 
800 	ret = vm_get_guestmem_from_ctx(ctx, &baseaddr, &lowmem, &highmem);
801 	if (ret) {
802 		fprintf(stderr, "%s: unable to retrieve guest memory size\r\n",
803 			__func__);
804 		return (0);
805 	}
806 	totalmem = lowmem + highmem;
807 
808 	if ((op_wr == false) && (totalmem != memsz)) {
809 		fprintf(stderr, "%s: mem size mismatch: %ld vs %ld\r\n",
810 			__func__, totalmem, memsz);
811 		return (0);
812 	}
813 
814 	winsize.ws_col = 80;
815 #ifdef TIOCGWINSZ
816 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
817 #endif /* TIOCGWINSZ */
818 	old_winch_handler = signal(SIGWINCH, winch_handler);
819 
820 	ret = vm_snapshot_mem_part(snapfd, 0, baseaddr, lowmem,
821 		totalmem, op_wr);
822 	if (ret) {
823 		fprintf(stderr, "%s: Could not %s lowmem\r\n",
824 			__func__, op_wr ? "write" : "read");
825 		totalmem = 0;
826 		goto done;
827 	}
828 
829 	if (highmem == 0)
830 		goto done;
831 
832 	ret = vm_snapshot_mem_part(snapfd, lowmem, baseaddr + 4*GB,
833 		highmem, totalmem, op_wr);
834 	if (ret) {
835 		fprintf(stderr, "%s: Could not %s highmem\r\n",
836 		        __func__, op_wr ? "write" : "read");
837 		totalmem = 0;
838 		goto done;
839 	}
840 
841 done:
842 	printf("\r\n");
843 	signal(SIGWINCH, old_winch_handler);
844 
845 	return (totalmem);
846 }
847 
848 int
849 restore_vm_mem(struct vmctx *ctx, struct restore_state *rstate)
850 {
851 	size_t restored;
852 
853 	restored = vm_snapshot_mem(ctx, rstate->vmmem_fd, rstate->vmmem_len,
854 				   false);
855 
856 	if (restored != rstate->vmmem_len)
857 		return (-1);
858 
859 	return (0);
860 }
861 
862 static int
863 vm_restore_kern_struct(struct vmctx *ctx, struct restore_state *rstate,
864 		       const struct vm_snapshot_kern_info *info)
865 {
866 	void *struct_ptr;
867 	size_t struct_size;
868 	int ret;
869 	struct vm_snapshot_meta *meta;
870 
871 	struct_ptr = lookup_struct(info->req, rstate, &struct_size);
872 	if (struct_ptr == NULL) {
873 		fprintf(stderr, "%s: Failed to lookup struct %s\r\n",
874 			__func__, info->struct_name);
875 		ret = -1;
876 		goto done;
877 	}
878 
879 	if (struct_size == 0) {
880 		fprintf(stderr, "%s: Kernel struct size was 0 for: %s\r\n",
881 			__func__, info->struct_name);
882 		ret = -1;
883 		goto done;
884 	}
885 
886 	meta = &(struct vm_snapshot_meta) {
887 		.ctx = ctx,
888 		.dev_name = info->struct_name,
889 		.dev_req  = info->req,
890 
891 		.buffer.buf_start = struct_ptr,
892 		.buffer.buf_size = struct_size,
893 
894 		.buffer.buf = struct_ptr,
895 		.buffer.buf_rem = struct_size,
896 
897 		.op = VM_SNAPSHOT_RESTORE,
898 	};
899 
900 	ret = vm_snapshot_req(meta);
901 	if (ret != 0) {
902 		fprintf(stderr, "%s: Failed to restore struct: %s\r\n",
903 			__func__, info->struct_name);
904 		goto done;
905 	}
906 
907 done:
908 	return (ret);
909 }
910 
911 int
912 vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate)
913 {
914 	int ret;
915 	int i;
916 
917 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
918 		ret = vm_restore_kern_struct(ctx, rstate,
919 					     &snapshot_kern_structs[i]);
920 		if (ret != 0)
921 			return (ret);
922 	}
923 
924 	return (0);
925 }
926 
927 int
928 vm_restore_user_dev(struct vmctx *ctx, struct restore_state *rstate,
929 		    const struct vm_snapshot_dev_info *info)
930 {
931 	void *dev_ptr;
932 	size_t dev_size;
933 	int ret;
934 	struct vm_snapshot_meta *meta;
935 
936 	dev_ptr = lookup_dev(info->dev_name, rstate, &dev_size);
937 	if (dev_ptr == NULL) {
938 		fprintf(stderr, "Failed to lookup dev: %s\r\n", info->dev_name);
939 		fprintf(stderr, "Continuing the restore/migration process\r\n");
940 		return (0);
941 	}
942 
943 	if (dev_size == 0) {
944 		fprintf(stderr, "%s: Device size is 0. "
945 			"Assuming %s is not used\r\n",
946 			__func__, info->dev_name);
947 		return (0);
948 	}
949 
950 	meta = &(struct vm_snapshot_meta) {
951 		.ctx = ctx,
952 		.dev_name = info->dev_name,
953 
954 		.buffer.buf_start = dev_ptr,
955 		.buffer.buf_size = dev_size,
956 
957 		.buffer.buf = dev_ptr,
958 		.buffer.buf_rem = dev_size,
959 
960 		.op = VM_SNAPSHOT_RESTORE,
961 	};
962 
963 	ret = (*info->snapshot_cb)(meta);
964 	if (ret != 0) {
965 		fprintf(stderr, "Failed to restore dev: %s\r\n",
966 			info->dev_name);
967 		return (-1);
968 	}
969 
970 	return (0);
971 }
972 
973 
974 int
975 vm_restore_user_devs(struct vmctx *ctx, struct restore_state *rstate)
976 {
977 	int ret;
978 	int i;
979 
980 	for (i = 0; i < nitems(snapshot_devs); i++) {
981 		ret = vm_restore_user_dev(ctx, rstate, &snapshot_devs[i]);
982 		if (ret != 0)
983 			return (ret);
984 	}
985 
986 	return 0;
987 }
988 
989 int
990 vm_pause_user_devs(struct vmctx *ctx)
991 {
992 	const struct vm_snapshot_dev_info *info;
993 	int ret;
994 	int i;
995 
996 	for (i = 0; i < nitems(snapshot_devs); i++) {
997 		info = &snapshot_devs[i];
998 		if (info->pause_cb == NULL)
999 			continue;
1000 
1001 		ret = info->pause_cb(ctx, info->dev_name);
1002 		if (ret != 0)
1003 			return (ret);
1004 	}
1005 
1006 	return (0);
1007 }
1008 
1009 int
1010 vm_resume_user_devs(struct vmctx *ctx)
1011 {
1012 	const struct vm_snapshot_dev_info *info;
1013 	int ret;
1014 	int i;
1015 
1016 	for (i = 0; i < nitems(snapshot_devs); i++) {
1017 		info = &snapshot_devs[i];
1018 		if (info->resume_cb == NULL)
1019 			continue;
1020 
1021 		ret = info->resume_cb(ctx, info->dev_name);
1022 		if (ret != 0)
1023 			return (ret);
1024 	}
1025 
1026 	return (0);
1027 }
1028 
1029 static int
1030 vm_snapshot_kern_struct(int data_fd, xo_handle_t *xop, const char *array_key,
1031 			struct vm_snapshot_meta *meta, off_t *offset)
1032 {
1033 	int ret;
1034 	size_t data_size;
1035 	ssize_t write_cnt;
1036 
1037 	ret = vm_snapshot_req(meta);
1038 	if (ret != 0) {
1039 		fprintf(stderr, "%s: Failed to snapshot struct %s\r\n",
1040 			__func__, meta->dev_name);
1041 		ret = -1;
1042 		goto done;
1043 	}
1044 
1045 	data_size = vm_get_snapshot_size(meta);
1046 
1047 	write_cnt = write(data_fd, meta->buffer.buf_start, data_size);
1048 	if (write_cnt != data_size) {
1049 		perror("Failed to write all snapshotted data.");
1050 		ret = -1;
1051 		goto done;
1052 	}
1053 
1054 	/* Write metadata. */
1055 	xo_open_instance_h(xop, array_key);
1056 	xo_emit_h(xop, "{:debug_name/%s}\n", meta->dev_name);
1057 	xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%d}\n",
1058 		  meta->dev_req);
1059 	xo_emit_h(xop, "{:" JSON_SIZE_KEY "/%lu}\n", data_size);
1060 	xo_emit_h(xop, "{:" JSON_FILE_OFFSET_KEY "/%lu}\n", *offset);
1061 	xo_close_instance_h(xop, JSON_STRUCT_ARR_KEY);
1062 
1063 	*offset += data_size;
1064 
1065 done:
1066 	return (ret);
1067 }
1068 
1069 static int
1070 vm_snapshot_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
1071 {
1072 	int ret, i, error;
1073 	size_t offset, buf_size;
1074 	char *buffer;
1075 	struct vm_snapshot_meta *meta;
1076 
1077 	error = 0;
1078 	offset = 0;
1079 	buf_size = SNAPSHOT_BUFFER_SIZE;
1080 
1081 	buffer = malloc(SNAPSHOT_BUFFER_SIZE * sizeof(char));
1082 	if (buffer == NULL) {
1083 		error = ENOMEM;
1084 		perror("Failed to allocate memory for snapshot buffer");
1085 		goto err_vm_snapshot_kern_data;
1086 	}
1087 
1088 	meta = &(struct vm_snapshot_meta) {
1089 		.ctx = ctx,
1090 
1091 		.buffer.buf_start = buffer,
1092 		.buffer.buf_size = buf_size,
1093 
1094 		.op = VM_SNAPSHOT_SAVE,
1095 	};
1096 
1097 	xo_open_list_h(xop, JSON_STRUCT_ARR_KEY);
1098 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
1099 		meta->dev_name = snapshot_kern_structs[i].struct_name;
1100 		meta->dev_req  = snapshot_kern_structs[i].req;
1101 
1102 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1103 		meta->buffer.buf = meta->buffer.buf_start;
1104 		meta->buffer.buf_rem = meta->buffer.buf_size;
1105 
1106 		ret = vm_snapshot_kern_struct(data_fd, xop, JSON_DEV_ARR_KEY,
1107 					      meta, &offset);
1108 		if (ret != 0) {
1109 			error = -1;
1110 			goto err_vm_snapshot_kern_data;
1111 		}
1112 	}
1113 	xo_close_list_h(xop, JSON_STRUCT_ARR_KEY);
1114 
1115 err_vm_snapshot_kern_data:
1116 	if (buffer != NULL)
1117 		free(buffer);
1118 	return (error);
1119 }
1120 
1121 static int
1122 vm_snapshot_basic_metadata(struct vmctx *ctx, xo_handle_t *xop, size_t memsz)
1123 {
1124 
1125 	xo_open_container_h(xop, JSON_BASIC_METADATA_KEY);
1126 	xo_emit_h(xop, "{:" JSON_NCPUS_KEY "/%ld}\n", guest_ncpus);
1127 	xo_emit_h(xop, "{:" JSON_VMNAME_KEY "/%s}\n", vm_get_name(ctx));
1128 	xo_emit_h(xop, "{:" JSON_MEMSIZE_KEY "/%lu}\n", memsz);
1129 	xo_emit_h(xop, "{:" JSON_MEMFLAGS_KEY "/%d}\n", vm_get_memflags(ctx));
1130 	xo_close_container_h(xop, JSON_BASIC_METADATA_KEY);
1131 
1132 	return (0);
1133 }
1134 
1135 static int
1136 vm_snapshot_dev_write_data(int data_fd, xo_handle_t *xop, const char *array_key,
1137 			   struct vm_snapshot_meta *meta, off_t *offset)
1138 {
1139 	int ret;
1140 	size_t data_size;
1141 
1142 	data_size = vm_get_snapshot_size(meta);
1143 
1144 	ret = write(data_fd, meta->buffer.buf_start, data_size);
1145 	if (ret != data_size) {
1146 		perror("Failed to write all snapshotted data.");
1147 		return (-1);
1148 	}
1149 
1150 	/* Write metadata. */
1151 	xo_open_instance_h(xop, array_key);
1152 	xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%s}\n", meta->dev_name);
1153 	xo_emit_h(xop, "{:" JSON_SIZE_KEY "/%lu}\n", data_size);
1154 	xo_emit_h(xop, "{:" JSON_FILE_OFFSET_KEY "/%lu}\n", *offset);
1155 	xo_close_instance_h(xop, array_key);
1156 
1157 	*offset += data_size;
1158 
1159 	return (0);
1160 }
1161 
1162 static int
1163 vm_snapshot_user_dev(const struct vm_snapshot_dev_info *info,
1164 		     int data_fd, xo_handle_t *xop,
1165 		     struct vm_snapshot_meta *meta, off_t *offset)
1166 {
1167 	int ret;
1168 
1169 	ret = (*info->snapshot_cb)(meta);
1170 	if (ret != 0) {
1171 		fprintf(stderr, "Failed to snapshot %s; ret=%d\r\n",
1172 			meta->dev_name, ret);
1173 		return (ret);
1174 	}
1175 
1176 	ret = vm_snapshot_dev_write_data(data_fd, xop, JSON_DEV_ARR_KEY, meta,
1177 					 offset);
1178 	if (ret != 0)
1179 		return (ret);
1180 
1181 	return (0);
1182 }
1183 
1184 static int
1185 vm_snapshot_user_devs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
1186 {
1187 	int ret, i;
1188 	off_t offset;
1189 	void *buffer;
1190 	size_t buf_size;
1191 	struct vm_snapshot_meta *meta;
1192 
1193 	buf_size = SNAPSHOT_BUFFER_SIZE;
1194 
1195 	offset = lseek(data_fd, 0, SEEK_CUR);
1196 	if (offset < 0) {
1197 		perror("Failed to get data file current offset.");
1198 		return (-1);
1199 	}
1200 
1201 	buffer = malloc(buf_size);
1202 	if (buffer == NULL) {
1203 		perror("Failed to allocate memory for snapshot buffer");
1204 		ret = ENOSPC;
1205 		goto snapshot_err;
1206 	}
1207 
1208 	meta = &(struct vm_snapshot_meta) {
1209 		.ctx = ctx,
1210 
1211 		.buffer.buf_start = buffer,
1212 		.buffer.buf_size = buf_size,
1213 
1214 		.op = VM_SNAPSHOT_SAVE,
1215 	};
1216 
1217 	xo_open_list_h(xop, JSON_DEV_ARR_KEY);
1218 
1219 	/* Restore other devices that support this feature */
1220 	for (i = 0; i < nitems(snapshot_devs); i++) {
1221 		meta->dev_name = snapshot_devs[i].dev_name;
1222 
1223 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1224 		meta->buffer.buf = meta->buffer.buf_start;
1225 		meta->buffer.buf_rem = meta->buffer.buf_size;
1226 
1227 		ret = vm_snapshot_user_dev(&snapshot_devs[i], data_fd, xop,
1228 					   meta, &offset);
1229 		if (ret != 0)
1230 			goto snapshot_err;
1231 	}
1232 
1233 	xo_close_list_h(xop, JSON_DEV_ARR_KEY);
1234 
1235 snapshot_err:
1236 	if (buffer != NULL)
1237 		free(buffer);
1238 	return (ret);
1239 }
1240 
1241 void
1242 checkpoint_cpu_add(int vcpu)
1243 {
1244 
1245 	pthread_mutex_lock(&vcpu_lock);
1246 	CPU_SET(vcpu, &vcpus_active);
1247 
1248 	if (checkpoint_active) {
1249 		CPU_SET(vcpu, &vcpus_suspended);
1250 		while (checkpoint_active)
1251 			pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1252 		CPU_CLR(vcpu, &vcpus_suspended);
1253 	}
1254 	pthread_mutex_unlock(&vcpu_lock);
1255 }
1256 
1257 /*
1258  * When a vCPU is suspended for any reason, it calls
1259  * checkpoint_cpu_suspend().  This records that the vCPU is idle.
1260  * Before returning from suspension, checkpoint_cpu_resume() is
1261  * called.  In suspend we note that the vCPU is idle.  In resume we
1262  * pause the vCPU thread until the checkpoint is complete.  The reason
1263  * for the two-step process is that vCPUs might already be stopped in
1264  * the debug server when a checkpoint is requested.  This approach
1265  * allows us to account for and handle those vCPUs.
1266  */
1267 void
1268 checkpoint_cpu_suspend(int vcpu)
1269 {
1270 
1271 	pthread_mutex_lock(&vcpu_lock);
1272 	CPU_SET(vcpu, &vcpus_suspended);
1273 	if (checkpoint_active && CPU_CMP(&vcpus_active, &vcpus_suspended) == 0)
1274 		pthread_cond_signal(&vcpus_idle);
1275 	pthread_mutex_unlock(&vcpu_lock);
1276 }
1277 
1278 void
1279 checkpoint_cpu_resume(int vcpu)
1280 {
1281 
1282 	pthread_mutex_lock(&vcpu_lock);
1283 	while (checkpoint_active)
1284 		pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1285 	CPU_CLR(vcpu, &vcpus_suspended);
1286 	pthread_mutex_unlock(&vcpu_lock);
1287 }
1288 
1289 static void
1290 vm_vcpu_pause(struct vmctx *ctx)
1291 {
1292 
1293 	pthread_mutex_lock(&vcpu_lock);
1294 	checkpoint_active = true;
1295 	vm_suspend_cpu(ctx, -1);
1296 	while (CPU_CMP(&vcpus_active, &vcpus_suspended) != 0)
1297 		pthread_cond_wait(&vcpus_idle, &vcpu_lock);
1298 	pthread_mutex_unlock(&vcpu_lock);
1299 }
1300 
1301 static void
1302 vm_vcpu_resume(struct vmctx *ctx)
1303 {
1304 
1305 	pthread_mutex_lock(&vcpu_lock);
1306 	checkpoint_active = false;
1307 	pthread_mutex_unlock(&vcpu_lock);
1308 	vm_resume_cpu(ctx, -1);
1309 	pthread_cond_broadcast(&vcpus_can_run);
1310 }
1311 
1312 static int
1313 vm_checkpoint(struct vmctx *ctx, const char *checkpoint_file, bool stop_vm)
1314 {
1315 	int fd_checkpoint = 0, kdata_fd = 0;
1316 	int ret = 0;
1317 	int error = 0;
1318 	size_t memsz;
1319 	xo_handle_t *xop = NULL;
1320 	char *meta_filename = NULL;
1321 	char *kdata_filename = NULL;
1322 	FILE *meta_file = NULL;
1323 
1324 	kdata_filename = strcat_extension(checkpoint_file, ".kern");
1325 	if (kdata_filename == NULL) {
1326 		fprintf(stderr, "Failed to construct kernel data filename.\n");
1327 		return (-1);
1328 	}
1329 
1330 	kdata_fd = open(kdata_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700);
1331 	if (kdata_fd < 0) {
1332 		perror("Failed to open kernel data snapshot file.");
1333 		error = -1;
1334 		goto done;
1335 	}
1336 
1337 	fd_checkpoint = open(checkpoint_file, O_RDWR | O_CREAT | O_TRUNC, 0700);
1338 
1339 	if (fd_checkpoint < 0) {
1340 		perror("Failed to create checkpoint file");
1341 		error = -1;
1342 		goto done;
1343 	}
1344 
1345 	meta_filename = strcat_extension(checkpoint_file, ".meta");
1346 	if (meta_filename == NULL) {
1347 		fprintf(stderr, "Failed to construct vm metadata filename.\n");
1348 		goto done;
1349 	}
1350 
1351 	meta_file = fopen(meta_filename, "w");
1352 	if (meta_file == NULL) {
1353 		perror("Failed to open vm metadata snapshot file.");
1354 		goto done;
1355 	}
1356 
1357 	xop = xo_create_to_file(meta_file, XO_STYLE_JSON, XOF_PRETTY);
1358 	if (xop == NULL) {
1359 		perror("Failed to get libxo handle on metadata file.");
1360 		goto done;
1361 	}
1362 
1363 	vm_vcpu_pause(ctx);
1364 
1365 	ret = vm_pause_user_devs(ctx);
1366 	if (ret != 0) {
1367 		fprintf(stderr, "Could not pause devices\r\n");
1368 		error = ret;
1369 		goto done;
1370 	}
1371 
1372 	memsz = vm_snapshot_mem(ctx, fd_checkpoint, 0, true);
1373 	if (memsz == 0) {
1374 		perror("Could not write guest memory to file");
1375 		error = -1;
1376 		goto done;
1377 	}
1378 
1379 	ret = vm_snapshot_basic_metadata(ctx, xop, memsz);
1380 	if (ret != 0) {
1381 		fprintf(stderr, "Failed to snapshot vm basic metadata.\n");
1382 		error = -1;
1383 		goto done;
1384 	}
1385 
1386 
1387 	ret = vm_snapshot_kern_structs(ctx, kdata_fd, xop);
1388 	if (ret != 0) {
1389 		fprintf(stderr, "Failed to snapshot vm kernel data.\n");
1390 		error = -1;
1391 		goto done;
1392 	}
1393 
1394 	ret = vm_snapshot_user_devs(ctx, kdata_fd, xop);
1395 	if (ret != 0) {
1396 		fprintf(stderr, "Failed to snapshot device state.\n");
1397 		error = -1;
1398 		goto done;
1399 	}
1400 
1401 	xo_finish_h(xop);
1402 
1403 	if (stop_vm) {
1404 		vm_destroy(ctx);
1405 		exit(0);
1406 	}
1407 
1408 done:
1409 	ret = vm_resume_user_devs(ctx);
1410 	if (ret != 0)
1411 		fprintf(stderr, "Could not resume devices\r\n");
1412 	vm_vcpu_resume(ctx);
1413 	if (fd_checkpoint > 0)
1414 		close(fd_checkpoint);
1415 	if (meta_filename != NULL)
1416 		free(meta_filename);
1417 	if (kdata_filename != NULL)
1418 		free(kdata_filename);
1419 	if (xop != NULL)
1420 		xo_destroy(xop);
1421 	if (meta_file != NULL)
1422 		fclose(meta_file);
1423 	if (kdata_fd > 0)
1424 		close(kdata_fd);
1425 	return (error);
1426 }
1427 
1428 static int
1429 handle_message(struct vmctx *ctx, nvlist_t *nvl)
1430 {
1431 	int err;
1432 	const char *cmd;
1433 
1434 	if (!nvlist_exists_string(nvl, "cmd"))
1435 		return (-1);
1436 
1437 	cmd = nvlist_get_string(nvl, "cmd");
1438 	if (strcmp(cmd, "checkpoint") == 0) {
1439 		if (!nvlist_exists_string(nvl, "filename") ||
1440 		    !nvlist_exists_bool(nvl, "suspend"))
1441 			err = -1;
1442 		else
1443 			err = vm_checkpoint(ctx, nvlist_get_string(nvl, "filename"),
1444 			    nvlist_get_bool(nvl, "suspend"));
1445 	} else {
1446 		EPRINTLN("Unrecognized checkpoint operation\n");
1447 		err = -1;
1448 	}
1449 
1450 	if (err != 0)
1451 		EPRINTLN("Unable to perform the requested operation\n");
1452 
1453 	nvlist_destroy(nvl);
1454 	return (err);
1455 }
1456 
1457 /*
1458  * Listen for commands from bhyvectl
1459  */
1460 void *
1461 checkpoint_thread(void *param)
1462 {
1463 	struct checkpoint_thread_info *thread_info;
1464 	nvlist_t *nvl;
1465 
1466 	pthread_set_name_np(pthread_self(), "checkpoint thread");
1467 	thread_info = (struct checkpoint_thread_info *)param;
1468 
1469 	for (;;) {
1470 		nvl = nvlist_recv(thread_info->socket_fd, 0);
1471 		if (nvl != NULL)
1472 			handle_message(thread_info->ctx, nvl);
1473 		else
1474 			EPRINTLN("nvlist_recv() failed: %s", strerror(errno));
1475 	}
1476 
1477 	return (NULL);
1478 }
1479 
1480 void
1481 init_snapshot(void)
1482 {
1483 	int err;
1484 
1485 	err = pthread_mutex_init(&vcpu_lock, NULL);
1486 	if (err != 0)
1487 		errc(1, err, "checkpoint mutex init");
1488 	err = pthread_cond_init(&vcpus_idle, NULL);
1489 	if (err != 0)
1490 		errc(1, err, "checkpoint cv init (vcpus_idle)");
1491 	err = pthread_cond_init(&vcpus_can_run, NULL);
1492 	if (err != 0)
1493 		errc(1, err, "checkpoint cv init (vcpus_can_run)");
1494 }
1495 
1496 /*
1497  * Create the listening socket for IPC with bhyvectl
1498  */
1499 int
1500 init_checkpoint_thread(struct vmctx *ctx)
1501 {
1502 	struct checkpoint_thread_info *checkpoint_info = NULL;
1503 	struct sockaddr_un addr;
1504 	int socket_fd;
1505 	pthread_t checkpoint_pthread;
1506 	int err;
1507 
1508 	memset(&addr, 0, sizeof(addr));
1509 
1510 	socket_fd = socket(PF_UNIX, SOCK_DGRAM, 0);
1511 	if (socket_fd < 0) {
1512 		EPRINTLN("Socket creation failed: %s", strerror(errno));
1513 		err = -1;
1514 		goto fail;
1515 	}
1516 
1517 	addr.sun_family = AF_UNIX;
1518 
1519 	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
1520 		 BHYVE_RUN_DIR, vm_get_name(ctx));
1521 	addr.sun_len = SUN_LEN(&addr);
1522 	unlink(addr.sun_path);
1523 
1524 	if (bind(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
1525 		EPRINTLN("Failed to bind socket \"%s\": %s\n",
1526 		    addr.sun_path, strerror(errno));
1527 		err = -1;
1528 		goto fail;
1529 	}
1530 
1531 	checkpoint_info = calloc(1, sizeof(*checkpoint_info));
1532 	checkpoint_info->ctx = ctx;
1533 	checkpoint_info->socket_fd = socket_fd;
1534 
1535 	err = pthread_create(&checkpoint_pthread, NULL, checkpoint_thread,
1536 		checkpoint_info);
1537 	if (err != 0)
1538 		goto fail;
1539 
1540 	return (0);
1541 fail:
1542 	free(checkpoint_info);
1543 	if (socket_fd > 0)
1544 		close(socket_fd);
1545 	unlink(addr.sun_path);
1546 
1547 	return (err);
1548 }
1549 
1550 void
1551 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op)
1552 {
1553 	const char *__op;
1554 
1555 	if (op == VM_SNAPSHOT_SAVE)
1556 		__op = "save";
1557 	else if (op == VM_SNAPSHOT_RESTORE)
1558 		__op = "restore";
1559 	else
1560 		__op = "unknown";
1561 
1562 	fprintf(stderr, "%s: snapshot-%s failed for %s\r\n",
1563 		__func__, __op, bufname);
1564 }
1565 
1566 int
1567 vm_snapshot_buf(volatile void *data, size_t data_size,
1568 		struct vm_snapshot_meta *meta)
1569 {
1570 	struct vm_snapshot_buffer *buffer;
1571 	int op;
1572 
1573 	buffer = &meta->buffer;
1574 	op = meta->op;
1575 
1576 	if (buffer->buf_rem < data_size) {
1577 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1578 		return (E2BIG);
1579 	}
1580 
1581 	if (op == VM_SNAPSHOT_SAVE)
1582 		memcpy(buffer->buf, (uint8_t *) data, data_size);
1583 	else if (op == VM_SNAPSHOT_RESTORE)
1584 		memcpy((uint8_t *) data, buffer->buf, data_size);
1585 	else
1586 		return (EINVAL);
1587 
1588 	buffer->buf += data_size;
1589 	buffer->buf_rem -= data_size;
1590 
1591 	return (0);
1592 }
1593 
1594 size_t
1595 vm_get_snapshot_size(struct vm_snapshot_meta *meta)
1596 {
1597 	size_t length;
1598 	struct vm_snapshot_buffer *buffer;
1599 
1600 	buffer = &meta->buffer;
1601 
1602 	if (buffer->buf_size < buffer->buf_rem) {
1603 		fprintf(stderr, "%s: Invalid buffer: size = %zu, rem = %zu\r\n",
1604 			__func__, buffer->buf_size, buffer->buf_rem);
1605 		length = 0;
1606 	} else {
1607 		length = buffer->buf_size - buffer->buf_rem;
1608 	}
1609 
1610 	return (length);
1611 }
1612 
1613 int
1614 vm_snapshot_guest2host_addr(void **addrp, size_t len, bool restore_null,
1615 			    struct vm_snapshot_meta *meta)
1616 {
1617 	int ret;
1618 	vm_paddr_t gaddr;
1619 
1620 	if (meta->op == VM_SNAPSHOT_SAVE) {
1621 		gaddr = paddr_host2guest(meta->ctx, *addrp);
1622 		if (gaddr == (vm_paddr_t) -1) {
1623 			if (!restore_null ||
1624 			    (restore_null && (*addrp != NULL))) {
1625 				ret = EFAULT;
1626 				goto done;
1627 			}
1628 		}
1629 
1630 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1631 	} else if (meta->op == VM_SNAPSHOT_RESTORE) {
1632 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1633 		if (gaddr == (vm_paddr_t) -1) {
1634 			if (!restore_null) {
1635 				ret = EFAULT;
1636 				goto done;
1637 			}
1638 		}
1639 
1640 		*addrp = paddr_guest2host(meta->ctx, gaddr, len);
1641 	} else {
1642 		ret = EINVAL;
1643 	}
1644 
1645 done:
1646 	return (ret);
1647 }
1648 
1649 int
1650 vm_snapshot_buf_cmp(volatile void *data, size_t data_size,
1651 		    struct vm_snapshot_meta *meta)
1652 {
1653 	struct vm_snapshot_buffer *buffer;
1654 	int op;
1655 	int ret;
1656 
1657 	buffer = &meta->buffer;
1658 	op = meta->op;
1659 
1660 	if (buffer->buf_rem < data_size) {
1661 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1662 		ret = E2BIG;
1663 		goto done;
1664 	}
1665 
1666 	if (op == VM_SNAPSHOT_SAVE) {
1667 		ret = 0;
1668 		memcpy(buffer->buf, (uint8_t *) data, data_size);
1669 	} else if (op == VM_SNAPSHOT_RESTORE) {
1670 		ret = memcmp((uint8_t *) data, buffer->buf, data_size);
1671 	} else {
1672 		ret = EINVAL;
1673 		goto done;
1674 	}
1675 
1676 	buffer->buf += data_size;
1677 	buffer->buf_rem -= data_size;
1678 
1679 done:
1680 	return (ret);
1681 }
1682