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