xref: /freebsd/usr.sbin/bhyve/snapshot.c (revision d91f8db5f1822c43cd256f19aae1d059e4b25a26)
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 #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 "ipc.h"
85 #include "fwctl.h"
86 #include "ioapic.h"
87 #include "mem.h"
88 #include "mevent.h"
89 #include "mptbl.h"
90 #include "pci_emul.h"
91 #include "pci_irq.h"
92 #include "pci_lpc.h"
93 #include "smbiostbl.h"
94 #include "snapshot.h"
95 #include "xmsr.h"
96 #include "spinup_ap.h"
97 #include "rtc.h"
98 
99 #include <libxo/xo.h>
100 #include <ucl.h>
101 
102 struct spinner_info {
103 	const size_t *crtval;
104 	const size_t maxval;
105 	const size_t total;
106 };
107 
108 extern int guest_ncpus;
109 
110 static struct winsize winsize;
111 static sig_t old_winch_handler;
112 
113 #define	KB		(1024UL)
114 #define	MB		(1024UL * KB)
115 #define	GB		(1024UL * MB)
116 
117 #define	SNAPSHOT_CHUNK	(4 * MB)
118 #define	PROG_BUF_SZ	(8192)
119 
120 #define	SNAPSHOT_BUFFER_SIZE (20 * MB)
121 
122 #define	JSON_STRUCT_ARR_KEY		"structs"
123 #define	JSON_DEV_ARR_KEY		"devices"
124 #define	JSON_BASIC_METADATA_KEY 	"basic metadata"
125 #define	JSON_SNAPSHOT_REQ_KEY		"snapshot_req"
126 #define	JSON_SIZE_KEY			"size"
127 #define	JSON_FILE_OFFSET_KEY		"file_offset"
128 
129 #define	JSON_NCPUS_KEY			"ncpus"
130 #define	JSON_VMNAME_KEY 		"vmname"
131 #define	JSON_MEMSIZE_KEY		"memsize"
132 #define	JSON_MEMFLAGS_KEY		"memflags"
133 
134 #define min(a,b)		\
135 ({				\
136  __typeof__ (a) _a = (a);	\
137  __typeof__ (b) _b = (b); 	\
138  _a < _b ? _a : _b;       	\
139  })
140 
141 static const struct vm_snapshot_dev_info snapshot_devs[] = {
142 	{ "atkbdc",	atkbdc_snapshot,	NULL,		NULL		},
143 	{ "virtio-net",	pci_snapshot,		pci_pause,	pci_resume	},
144 	{ "virtio-blk",	pci_snapshot,		pci_pause,	pci_resume	},
145 	{ "virtio-rnd",	pci_snapshot,		NULL,		NULL		},
146 	{ "lpc",	pci_snapshot,		NULL,		NULL		},
147 	{ "fbuf",	pci_snapshot,		NULL,		NULL		},
148 	{ "xhci",	pci_snapshot,		NULL,		NULL		},
149 	{ "e1000",	pci_snapshot,		NULL,		NULL		},
150 	{ "ahci",	pci_snapshot,		pci_pause,	pci_resume	},
151 	{ "ahci-hd",	pci_snapshot,		pci_pause,	pci_resume	},
152 	{ "ahci-cd",	pci_snapshot,		pci_pause,	pci_resume	},
153 };
154 
155 static const struct vm_snapshot_kern_info snapshot_kern_structs[] = {
156 	{ "vhpet",	STRUCT_VHPET	},
157 	{ "vm",		STRUCT_VM	},
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 	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 		err = -1;
308 		goto err_load_metadata;
309 	}
310 
311 	err = ucl_parser_add_file(parser, filename);
312 	if (err == 0) {
313 		fprintf(stderr, "Failed to parse metadata file: '%s'\n",
314 			filename);
315 		err = -1;
316 		goto err_load_metadata;
317 	}
318 
319 	obj = ucl_parser_get_object(parser);
320 	if (obj == NULL) {
321 		fprintf(stderr, "Failed to parse object.\n");
322 		err = -1;
323 		goto err_load_metadata;
324 	}
325 
326 	rstate->meta_parser = parser;
327 	rstate->meta_root_obj = (ucl_object_t *)obj;
328 
329 	return (0);
330 
331 err_load_metadata:
332 	if (parser != NULL)
333 		ucl_parser_free(parser);
334 	return (err);
335 }
336 
337 int
338 load_restore_file(const char *filename, struct restore_state *rstate)
339 {
340 	int err = 0;
341 	char *kdata_filename = NULL, *meta_filename = NULL;
342 
343 	assert(filename != NULL);
344 	assert(rstate != NULL);
345 
346 	memset(rstate, 0, sizeof(*rstate));
347 	rstate->kdata_map = MAP_FAILED;
348 
349 	err = load_vmmem_file(filename, rstate);
350 	if (err != 0) {
351 		fprintf(stderr, "Failed to load guest RAM file.\n");
352 		goto err_restore;
353 	}
354 
355 	kdata_filename = strcat_extension(filename, ".kern");
356 	if (kdata_filename == NULL) {
357 		fprintf(stderr, "Failed to construct kernel data filename.\n");
358 		goto err_restore;
359 	}
360 
361 	err = load_kdata_file(kdata_filename, rstate);
362 	if (err != 0) {
363 		fprintf(stderr, "Failed to load guest kernel data file.\n");
364 		goto err_restore;
365 	}
366 
367 	meta_filename = strcat_extension(filename, ".meta");
368 	if (meta_filename == NULL) {
369 		fprintf(stderr, "Failed to construct kernel metadata filename.\n");
370 		goto err_restore;
371 	}
372 
373 	err = load_metadata_file(meta_filename, rstate);
374 	if (err != 0) {
375 		fprintf(stderr, "Failed to load guest metadata file.\n");
376 		goto err_restore;
377 	}
378 
379 	return (0);
380 
381 err_restore:
382 	destroy_restore_state(rstate);
383 	if (kdata_filename != NULL)
384 		free(kdata_filename);
385 	if (meta_filename != NULL)
386 		free(meta_filename);
387 	return (-1);
388 }
389 
390 #define JSON_GET_INT_OR_RETURN(key, obj, result_ptr, ret)			\
391 do {										\
392 	const ucl_object_t *obj__;						\
393 	obj__ = ucl_object_lookup(obj, key);					\
394 	if (obj__ == NULL) {							\
395 		fprintf(stderr, "Missing key: '%s'", key);			\
396 		return (ret);							\
397 	}									\
398 	if (!ucl_object_toint_safe(obj__, result_ptr)) {			\
399 		fprintf(stderr, "Cannot convert '%s' value to int.", key);	\
400 		return (ret);							\
401 	}									\
402 } while(0)
403 
404 #define JSON_GET_STRING_OR_RETURN(key, obj, result_ptr, ret)			\
405 do {										\
406 	const ucl_object_t *obj__;						\
407 	obj__ = ucl_object_lookup(obj, key);					\
408 	if (obj__ == NULL) {							\
409 		fprintf(stderr, "Missing key: '%s'", key);			\
410 		return (ret);							\
411 	}									\
412 	if (!ucl_object_tostring_safe(obj__, result_ptr)) {			\
413 		fprintf(stderr, "Cannot convert '%s' value to string.", key);	\
414 		return (ret);							\
415 	}									\
416 } while(0)
417 
418 static void *
419 lookup_struct(enum snapshot_req struct_id, struct restore_state *rstate,
420 	      size_t *struct_size)
421 {
422 	const ucl_object_t *structs = NULL, *obj = NULL;
423 	ucl_object_iter_t it = NULL;
424 	int64_t snapshot_req, size, file_offset;
425 
426 	structs = ucl_object_lookup(rstate->meta_root_obj, JSON_STRUCT_ARR_KEY);
427 	if (structs == NULL) {
428 		fprintf(stderr, "Failed to find '%s' object.\n",
429 			JSON_STRUCT_ARR_KEY);
430 		return (NULL);
431 	}
432 
433 	if (ucl_object_type(structs) != UCL_ARRAY) {
434 		fprintf(stderr, "Object '%s' is not an array.\n",
435 		JSON_STRUCT_ARR_KEY);
436 		return (NULL);
437 	}
438 
439 	while ((obj = ucl_object_iterate(structs, &it, true)) != NULL) {
440 		snapshot_req = -1;
441 		JSON_GET_INT_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj,
442 				       &snapshot_req, NULL);
443 		assert(snapshot_req >= 0);
444 		if ((enum snapshot_req) snapshot_req == struct_id) {
445 			JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj,
446 					       &size, NULL);
447 			assert(size >= 0);
448 
449 			JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj,
450 					       &file_offset, NULL);
451 			assert(file_offset >= 0);
452 			assert((uint64_t)file_offset + size <=
453 			    rstate->kdata_len);
454 
455 			*struct_size = (size_t)size;
456 			return ((uint8_t *)rstate->kdata_map + file_offset);
457 		}
458 	}
459 
460 	return (NULL);
461 }
462 
463 static void *
464 lookup_check_dev(const char *dev_name, struct restore_state *rstate,
465 		 const ucl_object_t *obj, size_t *data_size)
466 {
467 	const char *snapshot_req;
468 	int64_t size, file_offset;
469 
470 	snapshot_req = NULL;
471 	JSON_GET_STRING_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj,
472 				  &snapshot_req, NULL);
473 	assert(snapshot_req != NULL);
474 	if (!strcmp(snapshot_req, dev_name)) {
475 		JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj,
476 				       &size, NULL);
477 		assert(size >= 0);
478 
479 		JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj,
480 				       &file_offset, NULL);
481 		assert(file_offset >= 0);
482 		assert((uint64_t)file_offset + size <= rstate->kdata_len);
483 
484 		*data_size = (size_t)size;
485 		return ((uint8_t *)rstate->kdata_map + file_offset);
486 	}
487 
488 	return (NULL);
489 }
490 
491 static void*
492 lookup_dev(const char *dev_name, struct restore_state *rstate,
493 	   size_t *data_size)
494 {
495 	const ucl_object_t *devs = NULL, *obj = NULL;
496 	ucl_object_iter_t it = NULL;
497 	void *ret;
498 
499 	devs = ucl_object_lookup(rstate->meta_root_obj, JSON_DEV_ARR_KEY);
500 	if (devs == NULL) {
501 		fprintf(stderr, "Failed to find '%s' object.\n",
502 			JSON_DEV_ARR_KEY);
503 		return (NULL);
504 	}
505 
506 	if (ucl_object_type(devs) != UCL_ARRAY) {
507 		fprintf(stderr, "Object '%s' is not an array.\n",
508 			JSON_DEV_ARR_KEY);
509 		return (NULL);
510 	}
511 
512 	while ((obj = ucl_object_iterate(devs, &it, true)) != NULL) {
513 		ret = lookup_check_dev(dev_name, rstate, obj, data_size);
514 		if (ret != NULL)
515 			return (ret);
516 	}
517 
518 	return (NULL);
519 }
520 
521 static const ucl_object_t *
522 lookup_basic_metadata_object(struct restore_state *rstate)
523 {
524 	const ucl_object_t *basic_meta_obj = NULL;
525 
526 	basic_meta_obj = ucl_object_lookup(rstate->meta_root_obj,
527 					   JSON_BASIC_METADATA_KEY);
528 	if (basic_meta_obj == NULL) {
529 		fprintf(stderr, "Failed to find '%s' object.\n",
530 			JSON_BASIC_METADATA_KEY);
531 		return (NULL);
532 	}
533 
534 	if (ucl_object_type(basic_meta_obj) != UCL_OBJECT) {
535 		fprintf(stderr, "Object '%s' is not a JSON object.\n",
536 		JSON_BASIC_METADATA_KEY);
537 		return (NULL);
538 	}
539 
540 	return (basic_meta_obj);
541 }
542 
543 const char *
544 lookup_vmname(struct restore_state *rstate)
545 {
546 	const char *vmname;
547 	const ucl_object_t *obj;
548 
549 	obj = lookup_basic_metadata_object(rstate);
550 	if (obj == NULL)
551 		return (NULL);
552 
553 	JSON_GET_STRING_OR_RETURN(JSON_VMNAME_KEY, obj, &vmname, NULL);
554 	return (vmname);
555 }
556 
557 int
558 lookup_memflags(struct restore_state *rstate)
559 {
560 	int64_t memflags;
561 	const ucl_object_t *obj;
562 
563 	obj = lookup_basic_metadata_object(rstate);
564 	if (obj == NULL)
565 		return (0);
566 
567 	JSON_GET_INT_OR_RETURN(JSON_MEMFLAGS_KEY, obj, &memflags, 0);
568 
569 	return ((int)memflags);
570 }
571 
572 size_t
573 lookup_memsize(struct restore_state *rstate)
574 {
575 	int64_t memsize;
576 	const ucl_object_t *obj;
577 
578 	obj = lookup_basic_metadata_object(rstate);
579 	if (obj == NULL)
580 		return (0);
581 
582 	JSON_GET_INT_OR_RETURN(JSON_MEMSIZE_KEY, obj, &memsize, 0);
583 	if (memsize < 0)
584 		memsize = 0;
585 
586 	return ((size_t)memsize);
587 }
588 
589 
590 int
591 lookup_guest_ncpus(struct restore_state *rstate)
592 {
593 	int64_t ncpus;
594 	const ucl_object_t *obj;
595 
596 	obj = lookup_basic_metadata_object(rstate);
597 	if (obj == NULL)
598 		return (0);
599 
600 	JSON_GET_INT_OR_RETURN(JSON_NCPUS_KEY, obj, &ncpus, 0);
601 	return ((int)ncpus);
602 }
603 
604 static void
605 winch_handler(int signal __unused)
606 {
607 #ifdef TIOCGWINSZ
608 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
609 #endif /* TIOCGWINSZ */
610 }
611 
612 static int
613 print_progress(size_t crtval, const size_t maxval)
614 {
615 	size_t rc;
616 	double crtval_gb, maxval_gb;
617 	size_t i, win_width, prog_start, prog_done, prog_end;
618 	int mval_len;
619 
620 	static char prog_buf[PROG_BUF_SZ];
621 	static const size_t len = sizeof(prog_buf);
622 
623 	static size_t div;
624 	static const char *div_str;
625 
626 	static char wip_bar[] = { '/', '-', '\\', '|' };
627 	static int wip_idx = 0;
628 
629 	if (maxval == 0) {
630 		printf("[0B / 0B]\r\n");
631 		return (0);
632 	}
633 
634 	if (crtval > maxval)
635 		crtval = maxval;
636 
637 	if (maxval > 10 * GB) {
638 		div = GB;
639 		div_str = "GiB";
640 	} else if (maxval > 10 * MB) {
641 		div = MB;
642 		div_str = "MiB";
643 	} else {
644 		div = KB;
645 		div_str = "KiB";
646 	}
647 
648 	crtval_gb = (double) crtval / div;
649 	maxval_gb = (double) maxval / div;
650 
651 	rc = snprintf(prog_buf, len, "%.03lf", maxval_gb);
652 	if (rc == len) {
653 		fprintf(stderr, "Maxval too big\n");
654 		return (-1);
655 	}
656 	mval_len = rc;
657 
658 	rc = snprintf(prog_buf, len, "\r[%*.03lf%s / %.03lf%s] |",
659 		mval_len, crtval_gb, div_str, maxval_gb, div_str);
660 
661 	if (rc == len) {
662 		fprintf(stderr, "Buffer too small to print progress\n");
663 		return (-1);
664 	}
665 
666 	win_width = min(winsize.ws_col, len);
667 	prog_start = rc;
668 
669 	if (prog_start < (win_width - 2)) {
670 		prog_end = win_width - prog_start - 2;
671 		prog_done = prog_end * (crtval_gb / maxval_gb);
672 
673 		for (i = prog_start; i < prog_start + prog_done; i++)
674 			prog_buf[i] = '#';
675 
676 		if (crtval != maxval) {
677 			prog_buf[i] = wip_bar[wip_idx];
678 			wip_idx = (wip_idx + 1) % sizeof(wip_bar);
679 			i++;
680 		} else {
681 			prog_buf[i++] = '#';
682 		}
683 
684 		for (; i < win_width - 2; i++)
685 			prog_buf[i] = '_';
686 
687 		prog_buf[win_width - 2] = '|';
688 	}
689 
690 	prog_buf[win_width - 1] = '\0';
691 	write(STDOUT_FILENO, prog_buf, win_width);
692 
693 	return (0);
694 }
695 
696 static void *
697 snapshot_spinner_cb(void *arg)
698 {
699 	int rc;
700 	size_t crtval, maxval, total;
701 	struct spinner_info *si;
702 	struct timespec ts;
703 
704 	si = arg;
705 	if (si == NULL)
706 		pthread_exit(NULL);
707 
708 	ts.tv_sec = 0;
709 	ts.tv_nsec = 50 * 1000 * 1000; /* 50 ms sleep time */
710 
711 	do {
712 		crtval = *si->crtval;
713 		maxval = si->maxval;
714 		total = si->total;
715 
716 		rc = print_progress(crtval, total);
717 		if (rc < 0) {
718 			fprintf(stderr, "Failed to parse progress\n");
719 			break;
720 		}
721 
722 		nanosleep(&ts, NULL);
723 	} while (crtval < maxval);
724 
725 	pthread_exit(NULL);
726 	return NULL;
727 }
728 
729 static int
730 vm_snapshot_mem_part(const int snapfd, const size_t foff, void *src,
731 		     const size_t len, const size_t totalmem, const bool op_wr)
732 {
733 	int rc;
734 	size_t part_done, todo, rem;
735 	ssize_t done;
736 	bool show_progress;
737 	pthread_t spinner_th;
738 	struct spinner_info *si;
739 
740 	if (lseek(snapfd, foff, SEEK_SET) < 0) {
741 		perror("Failed to change file offset");
742 		return (-1);
743 	}
744 
745 	show_progress = false;
746 	if (isatty(STDIN_FILENO) && (winsize.ws_col != 0))
747 		show_progress = true;
748 
749 	part_done = foff;
750 	rem = len;
751 
752 	if (show_progress) {
753 		si = &(struct spinner_info) {
754 			.crtval = &part_done,
755 			.maxval = foff + len,
756 			.total = totalmem
757 		};
758 
759 		rc = pthread_create(&spinner_th, 0, snapshot_spinner_cb, si);
760 		if (rc) {
761 			perror("Unable to create spinner thread");
762 			show_progress = false;
763 		}
764 	}
765 
766 	while (rem > 0) {
767 		if (show_progress)
768 			todo = min(SNAPSHOT_CHUNK, rem);
769 		else
770 			todo = rem;
771 
772 		if (op_wr)
773 			done = write(snapfd, src, todo);
774 		else
775 			done = read(snapfd, src, todo);
776 		if (done < 0) {
777 			perror("Failed to write in file");
778 			return (-1);
779 		}
780 
781 		src = (uint8_t *)src + done;
782 		part_done += done;
783 		rem -= done;
784 	}
785 
786 	if (show_progress) {
787 		rc = pthread_join(spinner_th, NULL);
788 		if (rc)
789 			perror("Unable to end spinner thread");
790 	}
791 
792 	return (0);
793 }
794 
795 static size_t
796 vm_snapshot_mem(struct vmctx *ctx, int snapfd, size_t memsz, const bool op_wr)
797 {
798 	int ret;
799 	size_t lowmem, highmem, totalmem;
800 	char *baseaddr;
801 
802 	ret = vm_get_guestmem_from_ctx(ctx, &baseaddr, &lowmem, &highmem);
803 	if (ret) {
804 		fprintf(stderr, "%s: unable to retrieve guest memory size\r\n",
805 			__func__);
806 		return (0);
807 	}
808 	totalmem = lowmem + highmem;
809 
810 	if ((op_wr == false) && (totalmem != memsz)) {
811 		fprintf(stderr, "%s: mem size mismatch: %ld vs %ld\r\n",
812 			__func__, totalmem, memsz);
813 		return (0);
814 	}
815 
816 	winsize.ws_col = 80;
817 #ifdef TIOCGWINSZ
818 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
819 #endif /* TIOCGWINSZ */
820 	old_winch_handler = signal(SIGWINCH, winch_handler);
821 
822 	ret = vm_snapshot_mem_part(snapfd, 0, baseaddr, lowmem,
823 		totalmem, op_wr);
824 	if (ret) {
825 		fprintf(stderr, "%s: Could not %s lowmem\r\n",
826 			__func__, op_wr ? "write" : "read");
827 		totalmem = 0;
828 		goto done;
829 	}
830 
831 	if (highmem == 0)
832 		goto done;
833 
834 	ret = vm_snapshot_mem_part(snapfd, lowmem, baseaddr + 4*GB,
835 		highmem, totalmem, op_wr);
836 	if (ret) {
837 		fprintf(stderr, "%s: Could not %s highmem\r\n",
838 		        __func__, op_wr ? "write" : "read");
839 		totalmem = 0;
840 		goto done;
841 	}
842 
843 done:
844 	printf("\r\n");
845 	signal(SIGWINCH, old_winch_handler);
846 
847 	return (totalmem);
848 }
849 
850 int
851 restore_vm_mem(struct vmctx *ctx, struct restore_state *rstate)
852 {
853 	size_t restored;
854 
855 	restored = vm_snapshot_mem(ctx, rstate->vmmem_fd, rstate->vmmem_len,
856 				   false);
857 
858 	if (restored != rstate->vmmem_len)
859 		return (-1);
860 
861 	return (0);
862 }
863 
864 static int
865 vm_restore_kern_struct(struct vmctx *ctx, struct restore_state *rstate,
866 		       const struct vm_snapshot_kern_info *info)
867 {
868 	void *struct_ptr;
869 	size_t struct_size;
870 	int ret;
871 	struct vm_snapshot_meta *meta;
872 
873 	struct_ptr = lookup_struct(info->req, rstate, &struct_size);
874 	if (struct_ptr == NULL) {
875 		fprintf(stderr, "%s: Failed to lookup struct %s\r\n",
876 			__func__, info->struct_name);
877 		ret = -1;
878 		goto done;
879 	}
880 
881 	if (struct_size == 0) {
882 		fprintf(stderr, "%s: Kernel struct size was 0 for: %s\r\n",
883 			__func__, info->struct_name);
884 		ret = -1;
885 		goto done;
886 	}
887 
888 	meta = &(struct vm_snapshot_meta) {
889 		.dev_name = info->struct_name,
890 		.dev_req  = info->req,
891 
892 		.buffer.buf_start = struct_ptr,
893 		.buffer.buf_size = struct_size,
894 
895 		.buffer.buf = struct_ptr,
896 		.buffer.buf_rem = struct_size,
897 
898 		.op = VM_SNAPSHOT_RESTORE,
899 	};
900 
901 	ret = vm_snapshot_req(ctx, meta);
902 	if (ret != 0) {
903 		fprintf(stderr, "%s: Failed to restore struct: %s\r\n",
904 			__func__, info->struct_name);
905 		goto done;
906 	}
907 
908 done:
909 	return (ret);
910 }
911 
912 int
913 vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate)
914 {
915 	size_t i;
916 	int ret;
917 
918 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
919 		ret = vm_restore_kern_struct(ctx, rstate,
920 					     &snapshot_kern_structs[i]);
921 		if (ret != 0)
922 			return (ret);
923 	}
924 
925 	return (0);
926 }
927 
928 static int
929 vm_restore_user_dev(struct restore_state *rstate,
930 		    const struct vm_snapshot_dev_info *info)
931 {
932 	void *dev_ptr;
933 	size_t dev_size;
934 	int ret;
935 	struct vm_snapshot_meta *meta;
936 
937 	dev_ptr = lookup_dev(info->dev_name, rstate, &dev_size);
938 	if (dev_ptr == NULL) {
939 		fprintf(stderr, "Failed to lookup dev: %s\r\n", info->dev_name);
940 		fprintf(stderr, "Continuing the restore/migration process\r\n");
941 		return (0);
942 	}
943 
944 	if (dev_size == 0) {
945 		fprintf(stderr, "%s: Device size is 0. "
946 			"Assuming %s is not used\r\n",
947 			__func__, info->dev_name);
948 		return (0);
949 	}
950 
951 	meta = &(struct vm_snapshot_meta) {
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 restore_state *rstate)
976 {
977 	size_t i;
978 	int ret;
979 
980 	for (i = 0; i < nitems(snapshot_devs); i++) {
981 		ret = vm_restore_user_dev(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(void)
991 {
992 	const struct vm_snapshot_dev_info *info;
993 	size_t i;
994 	int ret;
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(info->dev_name);
1002 		if (ret != 0)
1003 			return (ret);
1004 	}
1005 
1006 	return (0);
1007 }
1008 
1009 int
1010 vm_resume_user_devs(void)
1011 {
1012 	const struct vm_snapshot_dev_info *info;
1013 	size_t i;
1014 	int ret;
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(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(struct vmctx *ctx, int data_fd, xo_handle_t *xop,
1031     const char *array_key, 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(ctx, 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 	/* XXX-MJ no handling for short writes. */
1048 	write_cnt = write(data_fd, meta->buffer.buf_start, data_size);
1049 	if (write_cnt < 0 || (size_t)write_cnt != data_size) {
1050 		perror("Failed to write all snapshotted data.");
1051 		ret = -1;
1052 		goto done;
1053 	}
1054 
1055 	/* Write metadata. */
1056 	xo_open_instance_h(xop, array_key);
1057 	xo_emit_h(xop, "{:debug_name/%s}\n", meta->dev_name);
1058 	xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%d}\n",
1059 		  meta->dev_req);
1060 	xo_emit_h(xop, "{:" JSON_SIZE_KEY "/%lu}\n", data_size);
1061 	xo_emit_h(xop, "{:" JSON_FILE_OFFSET_KEY "/%lu}\n", *offset);
1062 	xo_close_instance_h(xop, JSON_STRUCT_ARR_KEY);
1063 
1064 	*offset += data_size;
1065 
1066 done:
1067 	return (ret);
1068 }
1069 
1070 static int
1071 vm_snapshot_kern_structs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
1072 {
1073 	int ret, error;
1074 	size_t buf_size, i, offset;
1075 	char *buffer;
1076 	struct vm_snapshot_meta *meta;
1077 
1078 	error = 0;
1079 	offset = 0;
1080 	buf_size = SNAPSHOT_BUFFER_SIZE;
1081 
1082 	buffer = malloc(SNAPSHOT_BUFFER_SIZE * sizeof(char));
1083 	if (buffer == NULL) {
1084 		error = ENOMEM;
1085 		perror("Failed to allocate memory for snapshot buffer");
1086 		goto err_vm_snapshot_kern_data;
1087 	}
1088 
1089 	meta = &(struct vm_snapshot_meta) {
1090 		.buffer.buf_start = buffer,
1091 		.buffer.buf_size = buf_size,
1092 
1093 		.op = VM_SNAPSHOT_SAVE,
1094 	};
1095 
1096 	xo_open_list_h(xop, JSON_STRUCT_ARR_KEY);
1097 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
1098 		meta->dev_name = snapshot_kern_structs[i].struct_name;
1099 		meta->dev_req  = snapshot_kern_structs[i].req;
1100 
1101 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1102 		meta->buffer.buf = meta->buffer.buf_start;
1103 		meta->buffer.buf_rem = meta->buffer.buf_size;
1104 
1105 		ret = vm_snapshot_kern_struct(ctx, data_fd, xop,
1106 		    JSON_DEV_ARR_KEY, meta, &offset);
1107 		if (ret != 0) {
1108 			error = -1;
1109 			goto err_vm_snapshot_kern_data;
1110 		}
1111 	}
1112 	xo_close_list_h(xop, JSON_STRUCT_ARR_KEY);
1113 
1114 err_vm_snapshot_kern_data:
1115 	if (buffer != NULL)
1116 		free(buffer);
1117 	return (error);
1118 }
1119 
1120 static int
1121 vm_snapshot_basic_metadata(struct vmctx *ctx, xo_handle_t *xop, size_t memsz)
1122 {
1123 
1124 	xo_open_container_h(xop, JSON_BASIC_METADATA_KEY);
1125 	xo_emit_h(xop, "{:" JSON_NCPUS_KEY "/%ld}\n", guest_ncpus);
1126 	xo_emit_h(xop, "{:" JSON_VMNAME_KEY "/%s}\n", vm_get_name(ctx));
1127 	xo_emit_h(xop, "{:" JSON_MEMSIZE_KEY "/%lu}\n", memsz);
1128 	xo_emit_h(xop, "{:" JSON_MEMFLAGS_KEY "/%d}\n", vm_get_memflags(ctx));
1129 	xo_close_container_h(xop, JSON_BASIC_METADATA_KEY);
1130 
1131 	return (0);
1132 }
1133 
1134 static int
1135 vm_snapshot_dev_write_data(int data_fd, xo_handle_t *xop, const char *array_key,
1136 			   struct vm_snapshot_meta *meta, off_t *offset)
1137 {
1138 	ssize_t ret;
1139 	size_t data_size;
1140 
1141 	data_size = vm_get_snapshot_size(meta);
1142 
1143 	/* XXX-MJ no handling for short writes. */
1144 	ret = write(data_fd, meta->buffer.buf_start, data_size);
1145 	if (ret < 0 || (size_t)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(int data_fd, xo_handle_t *xop)
1186 {
1187 	int ret;
1188 	off_t offset;
1189 	void *buffer;
1190 	size_t buf_size, i;
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 		.buffer.buf_start = buffer,
1210 		.buffer.buf_size = buf_size,
1211 
1212 		.op = VM_SNAPSHOT_SAVE,
1213 	};
1214 
1215 	xo_open_list_h(xop, JSON_DEV_ARR_KEY);
1216 
1217 	/* Restore other devices that support this feature */
1218 	for (i = 0; i < nitems(snapshot_devs); i++) {
1219 		meta->dev_name = snapshot_devs[i].dev_name;
1220 
1221 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1222 		meta->buffer.buf = meta->buffer.buf_start;
1223 		meta->buffer.buf_rem = meta->buffer.buf_size;
1224 
1225 		ret = vm_snapshot_user_dev(&snapshot_devs[i], data_fd, xop,
1226 					   meta, &offset);
1227 		if (ret != 0)
1228 			goto snapshot_err;
1229 	}
1230 
1231 	xo_close_list_h(xop, JSON_DEV_ARR_KEY);
1232 
1233 snapshot_err:
1234 	if (buffer != NULL)
1235 		free(buffer);
1236 	return (ret);
1237 }
1238 
1239 void
1240 checkpoint_cpu_add(int vcpu)
1241 {
1242 
1243 	pthread_mutex_lock(&vcpu_lock);
1244 	CPU_SET(vcpu, &vcpus_active);
1245 
1246 	if (checkpoint_active) {
1247 		CPU_SET(vcpu, &vcpus_suspended);
1248 		while (checkpoint_active)
1249 			pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1250 		CPU_CLR(vcpu, &vcpus_suspended);
1251 	}
1252 	pthread_mutex_unlock(&vcpu_lock);
1253 }
1254 
1255 /*
1256  * When a vCPU is suspended for any reason, it calls
1257  * checkpoint_cpu_suspend().  This records that the vCPU is idle.
1258  * Before returning from suspension, checkpoint_cpu_resume() is
1259  * called.  In suspend we note that the vCPU is idle.  In resume we
1260  * pause the vCPU thread until the checkpoint is complete.  The reason
1261  * for the two-step process is that vCPUs might already be stopped in
1262  * the debug server when a checkpoint is requested.  This approach
1263  * allows us to account for and handle those vCPUs.
1264  */
1265 void
1266 checkpoint_cpu_suspend(int vcpu)
1267 {
1268 
1269 	pthread_mutex_lock(&vcpu_lock);
1270 	CPU_SET(vcpu, &vcpus_suspended);
1271 	if (checkpoint_active && CPU_CMP(&vcpus_active, &vcpus_suspended) == 0)
1272 		pthread_cond_signal(&vcpus_idle);
1273 	pthread_mutex_unlock(&vcpu_lock);
1274 }
1275 
1276 void
1277 checkpoint_cpu_resume(int vcpu)
1278 {
1279 
1280 	pthread_mutex_lock(&vcpu_lock);
1281 	while (checkpoint_active)
1282 		pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1283 	CPU_CLR(vcpu, &vcpus_suspended);
1284 	pthread_mutex_unlock(&vcpu_lock);
1285 }
1286 
1287 static void
1288 vm_vcpu_pause(struct vmctx *ctx)
1289 {
1290 
1291 	pthread_mutex_lock(&vcpu_lock);
1292 	checkpoint_active = true;
1293 	vm_suspend_all_cpus(ctx);
1294 	while (CPU_CMP(&vcpus_active, &vcpus_suspended) != 0)
1295 		pthread_cond_wait(&vcpus_idle, &vcpu_lock);
1296 	pthread_mutex_unlock(&vcpu_lock);
1297 }
1298 
1299 static void
1300 vm_vcpu_resume(struct vmctx *ctx)
1301 {
1302 
1303 	pthread_mutex_lock(&vcpu_lock);
1304 	checkpoint_active = false;
1305 	pthread_mutex_unlock(&vcpu_lock);
1306 	vm_resume_all_cpus(ctx);
1307 	pthread_cond_broadcast(&vcpus_can_run);
1308 }
1309 
1310 static int
1311 vm_checkpoint(struct vmctx *ctx, int fddir, const char *checkpoint_file,
1312     bool stop_vm)
1313 {
1314 	int fd_checkpoint = 0, kdata_fd = 0, fd_meta;
1315 	int ret = 0;
1316 	int error = 0;
1317 	size_t memsz;
1318 	xo_handle_t *xop = NULL;
1319 	char *meta_filename = NULL;
1320 	char *kdata_filename = NULL;
1321 	FILE *meta_file = NULL;
1322 
1323 	kdata_filename = strcat_extension(checkpoint_file, ".kern");
1324 	if (kdata_filename == NULL) {
1325 		fprintf(stderr, "Failed to construct kernel data filename.\n");
1326 		return (-1);
1327 	}
1328 
1329 	kdata_fd = openat(fddir, kdata_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700);
1330 	if (kdata_fd < 0) {
1331 		perror("Failed to open kernel data snapshot file.");
1332 		error = -1;
1333 		goto done;
1334 	}
1335 
1336 	fd_checkpoint = openat(fddir, checkpoint_file, O_RDWR | O_CREAT | O_TRUNC, 0700);
1337 
1338 	if (fd_checkpoint < 0) {
1339 		perror("Failed to create checkpoint file");
1340 		error = -1;
1341 		goto done;
1342 	}
1343 
1344 	meta_filename = strcat_extension(checkpoint_file, ".meta");
1345 	if (meta_filename == NULL) {
1346 		fprintf(stderr, "Failed to construct vm metadata filename.\n");
1347 		goto done;
1348 	}
1349 
1350 	fd_meta = openat(fddir, meta_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700);
1351 	if (fd_meta != -1)
1352 		meta_file = fdopen(fd_meta, "w");
1353 	if (meta_file == NULL) {
1354 		perror("Failed to open vm metadata snapshot file.");
1355 		close(fd_meta);
1356 		goto done;
1357 	}
1358 
1359 	xop = xo_create_to_file(meta_file, XO_STYLE_JSON, XOF_PRETTY);
1360 	if (xop == NULL) {
1361 		perror("Failed to get libxo handle on metadata file.");
1362 		goto done;
1363 	}
1364 
1365 	vm_vcpu_pause(ctx);
1366 
1367 	ret = vm_pause_user_devs();
1368 	if (ret != 0) {
1369 		fprintf(stderr, "Could not pause devices\r\n");
1370 		error = ret;
1371 		goto done;
1372 	}
1373 
1374 	memsz = vm_snapshot_mem(ctx, fd_checkpoint, 0, true);
1375 	if (memsz == 0) {
1376 		perror("Could not write guest memory to file");
1377 		error = -1;
1378 		goto done;
1379 	}
1380 
1381 	ret = vm_snapshot_basic_metadata(ctx, xop, memsz);
1382 	if (ret != 0) {
1383 		fprintf(stderr, "Failed to snapshot vm basic metadata.\n");
1384 		error = -1;
1385 		goto done;
1386 	}
1387 
1388 
1389 	ret = vm_snapshot_kern_structs(ctx, kdata_fd, xop);
1390 	if (ret != 0) {
1391 		fprintf(stderr, "Failed to snapshot vm kernel data.\n");
1392 		error = -1;
1393 		goto done;
1394 	}
1395 
1396 	ret = vm_snapshot_user_devs(kdata_fd, xop);
1397 	if (ret != 0) {
1398 		fprintf(stderr, "Failed to snapshot device state.\n");
1399 		error = -1;
1400 		goto done;
1401 	}
1402 
1403 	xo_finish_h(xop);
1404 
1405 	if (stop_vm) {
1406 		vm_destroy(ctx);
1407 		exit(0);
1408 	}
1409 
1410 done:
1411 	ret = vm_resume_user_devs();
1412 	if (ret != 0)
1413 		fprintf(stderr, "Could not resume devices\r\n");
1414 	vm_vcpu_resume(ctx);
1415 	if (fd_checkpoint > 0)
1416 		close(fd_checkpoint);
1417 	if (meta_filename != NULL)
1418 		free(meta_filename);
1419 	if (kdata_filename != NULL)
1420 		free(kdata_filename);
1421 	if (xop != NULL)
1422 		xo_destroy(xop);
1423 	if (meta_file != NULL)
1424 		fclose(meta_file);
1425 	if (kdata_fd > 0)
1426 		close(kdata_fd);
1427 	return (error);
1428 }
1429 
1430 static int
1431 handle_message(struct vmctx *ctx, nvlist_t *nvl)
1432 {
1433 	const char *cmd;
1434 	struct ipc_command **ipc_cmd;
1435 
1436 	if (!nvlist_exists_string(nvl, "cmd"))
1437 		return (EINVAL);
1438 
1439 	cmd = nvlist_get_string(nvl, "cmd");
1440 	IPC_COMMAND_FOREACH(ipc_cmd, ipc_cmd_set) {
1441 		if (strcmp(cmd, (*ipc_cmd)->name) == 0)
1442 			return ((*ipc_cmd)->handler(ctx, nvl));
1443 	}
1444 
1445 	return (EOPNOTSUPP);
1446 }
1447 
1448 /*
1449  * Listen for commands from bhyvectl
1450  */
1451 void *
1452 checkpoint_thread(void *param)
1453 {
1454 	int fd;
1455 	struct checkpoint_thread_info *thread_info;
1456 	nvlist_t *nvl;
1457 
1458 	pthread_set_name_np(pthread_self(), "checkpoint thread");
1459 	thread_info = (struct checkpoint_thread_info *)param;
1460 
1461 	while ((fd = accept(thread_info->socket_fd, NULL, NULL)) != -1) {
1462 		nvl = nvlist_recv(fd, 0);
1463 		if (nvl != NULL)
1464 			handle_message(thread_info->ctx, nvl);
1465 		else
1466 			EPRINTLN("nvlist_recv() failed: %s", strerror(errno));
1467 
1468 		close(fd);
1469 		nvlist_destroy(nvl);
1470 	}
1471 
1472 	return (NULL);
1473 }
1474 
1475 static int
1476 vm_do_checkpoint(struct vmctx *ctx, const nvlist_t *nvl)
1477 {
1478 	int error;
1479 
1480 	if (!nvlist_exists_string(nvl, "filename") ||
1481 	    !nvlist_exists_bool(nvl, "suspend") ||
1482 	    !nvlist_exists_descriptor(nvl, "fddir"))
1483 		error = EINVAL;
1484 	else
1485 		error = vm_checkpoint(ctx,
1486 		    nvlist_get_descriptor(nvl, "fddir"),
1487 		    nvlist_get_string(nvl, "filename"),
1488 		    nvlist_get_bool(nvl, "suspend"));
1489 
1490 	return (error);
1491 }
1492 IPC_COMMAND(ipc_cmd_set, checkpoint, vm_do_checkpoint);
1493 
1494 void
1495 init_snapshot(void)
1496 {
1497 	int err;
1498 
1499 	err = pthread_mutex_init(&vcpu_lock, NULL);
1500 	if (err != 0)
1501 		errc(1, err, "checkpoint mutex init");
1502 	err = pthread_cond_init(&vcpus_idle, NULL);
1503 	if (err != 0)
1504 		errc(1, err, "checkpoint cv init (vcpus_idle)");
1505 	err = pthread_cond_init(&vcpus_can_run, NULL);
1506 	if (err != 0)
1507 		errc(1, err, "checkpoint cv init (vcpus_can_run)");
1508 }
1509 
1510 /*
1511  * Create the listening socket for IPC with bhyvectl
1512  */
1513 int
1514 init_checkpoint_thread(struct vmctx *ctx)
1515 {
1516 	struct checkpoint_thread_info *checkpoint_info = NULL;
1517 	struct sockaddr_un addr;
1518 	int socket_fd;
1519 	pthread_t checkpoint_pthread;
1520 	int err;
1521 #ifndef WITHOUT_CAPSICUM
1522 	cap_rights_t rights;
1523 #endif
1524 
1525 	memset(&addr, 0, sizeof(addr));
1526 
1527 	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
1528 	if (socket_fd < 0) {
1529 		EPRINTLN("Socket creation failed: %s", strerror(errno));
1530 		err = -1;
1531 		goto fail;
1532 	}
1533 
1534 	addr.sun_family = AF_UNIX;
1535 
1536 	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
1537 		 BHYVE_RUN_DIR, vm_get_name(ctx));
1538 	addr.sun_len = SUN_LEN(&addr);
1539 	unlink(addr.sun_path);
1540 
1541 	if (bind(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
1542 		EPRINTLN("Failed to bind socket \"%s\": %s\n",
1543 		    addr.sun_path, strerror(errno));
1544 		err = -1;
1545 		goto fail;
1546 	}
1547 
1548 	if (listen(socket_fd, 10) < 0) {
1549 		EPRINTLN("ipc socket listen: %s\n", strerror(errno));
1550 		err = errno;
1551 		goto fail;
1552 	}
1553 
1554 #ifndef WITHOUT_CAPSICUM
1555 	cap_rights_init(&rights, CAP_ACCEPT, CAP_READ, CAP_RECV, CAP_WRITE,
1556 	    CAP_SEND, CAP_GETSOCKOPT);
1557 
1558 	if (caph_rights_limit(socket_fd, &rights) == -1)
1559 		errx(EX_OSERR, "Unable to apply rights for sandbox");
1560 #endif
1561 	checkpoint_info = calloc(1, sizeof(*checkpoint_info));
1562 	checkpoint_info->ctx = ctx;
1563 	checkpoint_info->socket_fd = socket_fd;
1564 
1565 	err = pthread_create(&checkpoint_pthread, NULL, checkpoint_thread,
1566 		checkpoint_info);
1567 	if (err != 0)
1568 		goto fail;
1569 
1570 	return (0);
1571 fail:
1572 	free(checkpoint_info);
1573 	if (socket_fd > 0)
1574 		close(socket_fd);
1575 	unlink(addr.sun_path);
1576 
1577 	return (err);
1578 }
1579 
1580 void
1581 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op)
1582 {
1583 	const char *__op;
1584 
1585 	if (op == VM_SNAPSHOT_SAVE)
1586 		__op = "save";
1587 	else if (op == VM_SNAPSHOT_RESTORE)
1588 		__op = "restore";
1589 	else
1590 		__op = "unknown";
1591 
1592 	fprintf(stderr, "%s: snapshot-%s failed for %s\r\n",
1593 		__func__, __op, bufname);
1594 }
1595 
1596 int
1597 vm_snapshot_buf(void *data, size_t data_size, struct vm_snapshot_meta *meta)
1598 {
1599 	struct vm_snapshot_buffer *buffer;
1600 	int op;
1601 
1602 	buffer = &meta->buffer;
1603 	op = meta->op;
1604 
1605 	if (buffer->buf_rem < data_size) {
1606 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1607 		return (E2BIG);
1608 	}
1609 
1610 	if (op == VM_SNAPSHOT_SAVE)
1611 		memcpy(buffer->buf, data, data_size);
1612 	else if (op == VM_SNAPSHOT_RESTORE)
1613 		memcpy(data, buffer->buf, data_size);
1614 	else
1615 		return (EINVAL);
1616 
1617 	buffer->buf += data_size;
1618 	buffer->buf_rem -= data_size;
1619 
1620 	return (0);
1621 }
1622 
1623 size_t
1624 vm_get_snapshot_size(struct vm_snapshot_meta *meta)
1625 {
1626 	size_t length;
1627 	struct vm_snapshot_buffer *buffer;
1628 
1629 	buffer = &meta->buffer;
1630 
1631 	if (buffer->buf_size < buffer->buf_rem) {
1632 		fprintf(stderr, "%s: Invalid buffer: size = %zu, rem = %zu\r\n",
1633 			__func__, buffer->buf_size, buffer->buf_rem);
1634 		length = 0;
1635 	} else {
1636 		length = buffer->buf_size - buffer->buf_rem;
1637 	}
1638 
1639 	return (length);
1640 }
1641 
1642 int
1643 vm_snapshot_guest2host_addr(struct vmctx *ctx, void **addrp, size_t len,
1644     bool restore_null, struct vm_snapshot_meta *meta)
1645 {
1646 	int ret;
1647 	vm_paddr_t gaddr;
1648 
1649 	if (meta->op == VM_SNAPSHOT_SAVE) {
1650 		gaddr = paddr_host2guest(ctx, *addrp);
1651 		if (gaddr == (vm_paddr_t) -1) {
1652 			if (!restore_null ||
1653 			    (restore_null && (*addrp != NULL))) {
1654 				ret = EFAULT;
1655 				goto done;
1656 			}
1657 		}
1658 
1659 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1660 	} else if (meta->op == VM_SNAPSHOT_RESTORE) {
1661 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1662 		if (gaddr == (vm_paddr_t) -1) {
1663 			if (!restore_null) {
1664 				ret = EFAULT;
1665 				goto done;
1666 			}
1667 		}
1668 
1669 		*addrp = paddr_guest2host(ctx, gaddr, len);
1670 	} else {
1671 		ret = EINVAL;
1672 	}
1673 
1674 done:
1675 	return (ret);
1676 }
1677 
1678 int
1679 vm_snapshot_buf_cmp(void *data, size_t data_size, struct vm_snapshot_meta *meta)
1680 {
1681 	struct vm_snapshot_buffer *buffer;
1682 	int op;
1683 	int ret;
1684 
1685 	buffer = &meta->buffer;
1686 	op = meta->op;
1687 
1688 	if (buffer->buf_rem < data_size) {
1689 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1690 		ret = E2BIG;
1691 		goto done;
1692 	}
1693 
1694 	if (op == VM_SNAPSHOT_SAVE) {
1695 		ret = 0;
1696 		memcpy(buffer->buf, data, data_size);
1697 	} else if (op == VM_SNAPSHOT_RESTORE) {
1698 		ret = memcmp(data, buffer->buf, data_size);
1699 	} else {
1700 		ret = EINVAL;
1701 		goto done;
1702 	}
1703 
1704 	buffer->buf += data_size;
1705 	buffer->buf_rem -= data_size;
1706 
1707 done:
1708 	return (ret);
1709 }
1710