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