xref: /freebsd/usr.sbin/bhyve/snapshot.c (revision 9d54812421274e490dc5f0fe4722ab8d35d9b258)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 Flavius Anton
5  * Copyright (c) 2016 Mihai Tiganus
6  * Copyright (c) 2016-2019 Mihai Carabas
7  * Copyright (c) 2017-2019 Darius Mihai
8  * Copyright (c) 2017-2019 Elena Mihailescu
9  * Copyright (c) 2018-2019 Sergiu Weisz
10  * All rights reserved.
11  * The bhyve-snapshot feature was developed under sponsorships
12  * from Matthew Grooms.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/types.h>
40 #ifndef WITHOUT_CAPSICUM
41 #include <sys/capsicum.h>
42 #endif
43 #include <sys/mman.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/un.h>
48 
49 #include <machine/atomic.h>
50 #include <machine/segments.h>
51 
52 #ifndef WITHOUT_CAPSICUM
53 #include <capsicum_helpers.h>
54 #endif
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <libgen.h>
62 #include <signal.h>
63 #include <unistd.h>
64 #include <assert.h>
65 #include <errno.h>
66 #include <pthread.h>
67 #include <pthread_np.h>
68 #include <sysexits.h>
69 #include <stdbool.h>
70 #include <sys/ioctl.h>
71 
72 #include <machine/vmm.h>
73 #ifndef WITHOUT_CAPSICUM
74 #include <machine/vmm_dev.h>
75 #endif
76 #include <machine/vmm_snapshot.h>
77 #include <vmmapi.h>
78 
79 #include "bhyverun.h"
80 #include "acpi.h"
81 #include "atkbdc.h"
82 #include "debug.h"
83 #include "inout.h"
84 #include "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 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 const struct vm_snapshot_kern_info snapshot_kern_structs[] = {
156 	{ "vhpet",	STRUCT_VHPET	},
157 	{ "vm",		STRUCT_VM	},
158 	{ "vmx",	STRUCT_VMX	},
159 	{ "vioapic",	STRUCT_VIOAPIC	},
160 	{ "vlapic",	STRUCT_VLAPIC	},
161 	{ "vmcx",	STRUCT_VMCX	},
162 	{ "vatpit",	STRUCT_VATPIT	},
163 	{ "vatpic",	STRUCT_VATPIC	},
164 	{ "vpmtmr",	STRUCT_VPMTMR	},
165 	{ "vrtc",	STRUCT_VRTC	},
166 };
167 
168 static cpuset_t vcpus_active, vcpus_suspended;
169 static pthread_mutex_t vcpu_lock;
170 static pthread_cond_t vcpus_idle, vcpus_can_run;
171 static bool checkpoint_active;
172 
173 /*
174  * TODO: Harden this function and all of its callers since 'base_str' is a user
175  * provided string.
176  */
177 static char *
178 strcat_extension(const char *base_str, const char *ext)
179 {
180 	char *res;
181 	size_t base_len, ext_len;
182 
183 	base_len = strnlen(base_str, NAME_MAX);
184 	ext_len = strnlen(ext, NAME_MAX);
185 
186 	if (base_len + ext_len > NAME_MAX) {
187 		fprintf(stderr, "Filename exceeds maximum length.\n");
188 		return (NULL);
189 	}
190 
191 	res = malloc(base_len + ext_len + 1);
192 	if (res == NULL) {
193 		perror("Failed to allocate memory.");
194 		return (NULL);
195 	}
196 
197 	memcpy(res, base_str, base_len);
198 	memcpy(res + base_len, ext, ext_len);
199 	res[base_len + ext_len] = 0;
200 
201 	return (res);
202 }
203 
204 void
205 destroy_restore_state(struct restore_state *rstate)
206 {
207 	if (rstate == NULL) {
208 		fprintf(stderr, "Attempting to destroy NULL restore struct.\n");
209 		return;
210 	}
211 
212 	if (rstate->kdata_map != MAP_FAILED)
213 		munmap(rstate->kdata_map, rstate->kdata_len);
214 
215 	if (rstate->kdata_fd > 0)
216 		close(rstate->kdata_fd);
217 	if (rstate->vmmem_fd > 0)
218 		close(rstate->vmmem_fd);
219 
220 	if (rstate->meta_root_obj != NULL)
221 		ucl_object_unref(rstate->meta_root_obj);
222 	if (rstate->meta_parser != NULL)
223 		ucl_parser_free(rstate->meta_parser);
224 }
225 
226 static int
227 load_vmmem_file(const char *filename, struct restore_state *rstate)
228 {
229 	struct stat sb;
230 	int err;
231 
232 	rstate->vmmem_fd = open(filename, O_RDONLY);
233 	if (rstate->vmmem_fd < 0) {
234 		perror("Failed to open restore file");
235 		return (-1);
236 	}
237 
238 	err = fstat(rstate->vmmem_fd, &sb);
239 	if (err < 0) {
240 		perror("Failed to stat restore file");
241 		goto err_load_vmmem;
242 	}
243 
244 	if (sb.st_size == 0) {
245 		fprintf(stderr, "Restore file is empty.\n");
246 		goto err_load_vmmem;
247 	}
248 
249 	rstate->vmmem_len = sb.st_size;
250 
251 	return (0);
252 
253 err_load_vmmem:
254 	if (rstate->vmmem_fd > 0)
255 		close(rstate->vmmem_fd);
256 	return (-1);
257 }
258 
259 static int
260 load_kdata_file(const char *filename, struct restore_state *rstate)
261 {
262 	struct stat sb;
263 	int err;
264 
265 	rstate->kdata_fd = open(filename, O_RDONLY);
266 	if (rstate->kdata_fd < 0) {
267 		perror("Failed to open kernel data file");
268 		return (-1);
269 	}
270 
271 	err = fstat(rstate->kdata_fd, &sb);
272 	if (err < 0) {
273 		perror("Failed to stat kernel data file");
274 		goto err_load_kdata;
275 	}
276 
277 	if (sb.st_size == 0) {
278 		fprintf(stderr, "Kernel data file is empty.\n");
279 		goto err_load_kdata;
280 	}
281 
282 	rstate->kdata_len = sb.st_size;
283 	rstate->kdata_map = mmap(NULL, rstate->kdata_len, PROT_READ,
284 				 MAP_SHARED, rstate->kdata_fd, 0);
285 	if (rstate->kdata_map == MAP_FAILED) {
286 		perror("Failed to map restore file");
287 		goto err_load_kdata;
288 	}
289 
290 	return (0);
291 
292 err_load_kdata:
293 	if (rstate->kdata_fd > 0)
294 		close(rstate->kdata_fd);
295 	return (-1);
296 }
297 
298 static int
299 load_metadata_file(const char *filename, struct restore_state *rstate)
300 {
301 	const ucl_object_t *obj;
302 	struct ucl_parser *parser;
303 	int err;
304 
305 	parser = ucl_parser_new(UCL_PARSER_DEFAULT);
306 	if (parser == NULL) {
307 		fprintf(stderr, "Failed to initialize UCL parser.\n");
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((ucl_object_t *)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(file_offset + size <= rstate->kdata_len);
453 
454 			*struct_size = (size_t)size;
455 			return (rstate->kdata_map + file_offset);
456 		}
457 	}
458 
459 	return (NULL);
460 }
461 
462 static void *
463 lookup_check_dev(const char *dev_name, struct restore_state *rstate,
464 		 const ucl_object_t *obj, size_t *data_size)
465 {
466 	const char *snapshot_req;
467 	int64_t size, file_offset;
468 
469 	snapshot_req = NULL;
470 	JSON_GET_STRING_OR_RETURN(JSON_SNAPSHOT_REQ_KEY, obj,
471 				  &snapshot_req, NULL);
472 	assert(snapshot_req != NULL);
473 	if (!strcmp(snapshot_req, dev_name)) {
474 		JSON_GET_INT_OR_RETURN(JSON_SIZE_KEY, obj,
475 				       &size, NULL);
476 		assert(size >= 0);
477 
478 		JSON_GET_INT_OR_RETURN(JSON_FILE_OFFSET_KEY, obj,
479 				       &file_offset, NULL);
480 		assert(file_offset >= 0);
481 		assert(file_offset + size <= rstate->kdata_len);
482 
483 		*data_size = (size_t)size;
484 		return (rstate->kdata_map + file_offset);
485 	}
486 
487 	return (NULL);
488 }
489 
490 static void*
491 lookup_dev(const char *dev_name, struct restore_state *rstate,
492 	   size_t *data_size)
493 {
494 	const ucl_object_t *devs = NULL, *obj = NULL;
495 	ucl_object_iter_t it = NULL;
496 	void *ret;
497 
498 	devs = ucl_object_lookup(rstate->meta_root_obj, JSON_DEV_ARR_KEY);
499 	if (devs == NULL) {
500 		fprintf(stderr, "Failed to find '%s' object.\n",
501 			JSON_DEV_ARR_KEY);
502 		return (NULL);
503 	}
504 
505 	if (ucl_object_type((ucl_object_t *)devs) != UCL_ARRAY) {
506 		fprintf(stderr, "Object '%s' is not an array.\n",
507 			JSON_DEV_ARR_KEY);
508 		return (NULL);
509 	}
510 
511 	while ((obj = ucl_object_iterate(devs, &it, true)) != NULL) {
512 		ret = lookup_check_dev(dev_name, rstate, obj, data_size);
513 		if (ret != NULL)
514 			return (ret);
515 	}
516 
517 	return (NULL);
518 }
519 
520 static const ucl_object_t *
521 lookup_basic_metadata_object(struct restore_state *rstate)
522 {
523 	const ucl_object_t *basic_meta_obj = NULL;
524 
525 	basic_meta_obj = ucl_object_lookup(rstate->meta_root_obj,
526 					   JSON_BASIC_METADATA_KEY);
527 	if (basic_meta_obj == NULL) {
528 		fprintf(stderr, "Failed to find '%s' object.\n",
529 			JSON_BASIC_METADATA_KEY);
530 		return (NULL);
531 	}
532 
533 	if (ucl_object_type((ucl_object_t *)basic_meta_obj) != UCL_OBJECT) {
534 		fprintf(stderr, "Object '%s' is not a JSON object.\n",
535 		JSON_BASIC_METADATA_KEY);
536 		return (NULL);
537 	}
538 
539 	return (basic_meta_obj);
540 }
541 
542 const char *
543 lookup_vmname(struct restore_state *rstate)
544 {
545 	const char *vmname;
546 	const ucl_object_t *obj;
547 
548 	obj = lookup_basic_metadata_object(rstate);
549 	if (obj == NULL)
550 		return (NULL);
551 
552 	JSON_GET_STRING_OR_RETURN(JSON_VMNAME_KEY, obj, &vmname, NULL);
553 	return (vmname);
554 }
555 
556 int
557 lookup_memflags(struct restore_state *rstate)
558 {
559 	int64_t memflags;
560 	const ucl_object_t *obj;
561 
562 	obj = lookup_basic_metadata_object(rstate);
563 	if (obj == NULL)
564 		return (0);
565 
566 	JSON_GET_INT_OR_RETURN(JSON_MEMFLAGS_KEY, obj, &memflags, 0);
567 
568 	return ((int)memflags);
569 }
570 
571 size_t
572 lookup_memsize(struct restore_state *rstate)
573 {
574 	int64_t memsize;
575 	const ucl_object_t *obj;
576 
577 	obj = lookup_basic_metadata_object(rstate);
578 	if (obj == NULL)
579 		return (0);
580 
581 	JSON_GET_INT_OR_RETURN(JSON_MEMSIZE_KEY, obj, &memsize, 0);
582 	if (memsize < 0)
583 		memsize = 0;
584 
585 	return ((size_t)memsize);
586 }
587 
588 
589 int
590 lookup_guest_ncpus(struct restore_state *rstate)
591 {
592 	int64_t ncpus;
593 	const ucl_object_t *obj;
594 
595 	obj = lookup_basic_metadata_object(rstate);
596 	if (obj == NULL)
597 		return (0);
598 
599 	JSON_GET_INT_OR_RETURN(JSON_NCPUS_KEY, obj, &ncpus, 0);
600 	return ((int)ncpus);
601 }
602 
603 static void
604 winch_handler(int signal)
605 {
606 #ifdef TIOCGWINSZ
607 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
608 #endif /* TIOCGWINSZ */
609 }
610 
611 static int
612 print_progress(size_t crtval, const size_t maxval)
613 {
614 	size_t rc;
615 	double crtval_gb, maxval_gb;
616 	size_t i, win_width, prog_start, prog_done, prog_end;
617 	int mval_len;
618 
619 	static char prog_buf[PROG_BUF_SZ];
620 	static const size_t len = sizeof(prog_buf);
621 
622 	static size_t div;
623 	static char *div_str;
624 
625 	static char wip_bar[] = { '/', '-', '\\', '|' };
626 	static int wip_idx = 0;
627 
628 	if (maxval == 0) {
629 		printf("[0B / 0B]\r\n");
630 		return (0);
631 	}
632 
633 	if (crtval > maxval)
634 		crtval = maxval;
635 
636 	if (maxval > 10 * GB) {
637 		div = GB;
638 		div_str = "GiB";
639 	} else if (maxval > 10 * MB) {
640 		div = MB;
641 		div_str = "MiB";
642 	} else {
643 		div = KB;
644 		div_str = "KiB";
645 	}
646 
647 	crtval_gb = (double) crtval / div;
648 	maxval_gb = (double) maxval / div;
649 
650 	rc = snprintf(prog_buf, len, "%.03lf", maxval_gb);
651 	if (rc == len) {
652 		fprintf(stderr, "Maxval too big\n");
653 		return (-1);
654 	}
655 	mval_len = rc;
656 
657 	rc = snprintf(prog_buf, len, "\r[%*.03lf%s / %.03lf%s] |",
658 		mval_len, crtval_gb, div_str, maxval_gb, div_str);
659 
660 	if (rc == len) {
661 		fprintf(stderr, "Buffer too small to print progress\n");
662 		return (-1);
663 	}
664 
665 	win_width = min(winsize.ws_col, len);
666 	prog_start = rc;
667 
668 	if (prog_start < (win_width - 2)) {
669 		prog_end = win_width - prog_start - 2;
670 		prog_done = prog_end * (crtval_gb / maxval_gb);
671 
672 		for (i = prog_start; i < prog_start + prog_done; i++)
673 			prog_buf[i] = '#';
674 
675 		if (crtval != maxval) {
676 			prog_buf[i] = wip_bar[wip_idx];
677 			wip_idx = (wip_idx + 1) % sizeof(wip_bar);
678 			i++;
679 		} else {
680 			prog_buf[i++] = '#';
681 		}
682 
683 		for (; i < win_width - 2; i++)
684 			prog_buf[i] = '_';
685 
686 		prog_buf[win_width - 2] = '|';
687 	}
688 
689 	prog_buf[win_width - 1] = '\0';
690 	write(STDOUT_FILENO, prog_buf, win_width);
691 
692 	return (0);
693 }
694 
695 static void *
696 snapshot_spinner_cb(void *arg)
697 {
698 	int rc;
699 	size_t crtval, maxval, total;
700 	struct spinner_info *si;
701 	struct timespec ts;
702 
703 	si = arg;
704 	if (si == NULL)
705 		pthread_exit(NULL);
706 
707 	ts.tv_sec = 0;
708 	ts.tv_nsec = 50 * 1000 * 1000; /* 50 ms sleep time */
709 
710 	do {
711 		crtval = *si->crtval;
712 		maxval = si->maxval;
713 		total = si->total;
714 
715 		rc = print_progress(crtval, total);
716 		if (rc < 0) {
717 			fprintf(stderr, "Failed to parse progress\n");
718 			break;
719 		}
720 
721 		nanosleep(&ts, NULL);
722 	} while (crtval < maxval);
723 
724 	pthread_exit(NULL);
725 	return NULL;
726 }
727 
728 static int
729 vm_snapshot_mem_part(const int snapfd, const size_t foff, void *src,
730 		     const size_t len, const size_t totalmem, const bool op_wr)
731 {
732 	int rc;
733 	size_t part_done, todo, rem;
734 	ssize_t done;
735 	bool show_progress;
736 	pthread_t spinner_th;
737 	struct spinner_info *si;
738 
739 	if (lseek(snapfd, foff, SEEK_SET) < 0) {
740 		perror("Failed to change file offset");
741 		return (-1);
742 	}
743 
744 	show_progress = false;
745 	if (isatty(STDIN_FILENO) && (winsize.ws_col != 0))
746 		show_progress = true;
747 
748 	part_done = foff;
749 	rem = len;
750 
751 	if (show_progress) {
752 		si = &(struct spinner_info) {
753 			.crtval = &part_done,
754 			.maxval = foff + len,
755 			.total = totalmem
756 		};
757 
758 		rc = pthread_create(&spinner_th, 0, snapshot_spinner_cb, si);
759 		if (rc) {
760 			perror("Unable to create spinner thread");
761 			show_progress = false;
762 		}
763 	}
764 
765 	while (rem > 0) {
766 		if (show_progress)
767 			todo = min(SNAPSHOT_CHUNK, rem);
768 		else
769 			todo = rem;
770 
771 		if (op_wr)
772 			done = write(snapfd, src, todo);
773 		else
774 			done = read(snapfd, src, todo);
775 		if (done < 0) {
776 			perror("Failed to write in file");
777 			return (-1);
778 		}
779 
780 		src += done;
781 		part_done += done;
782 		rem -= done;
783 	}
784 
785 	if (show_progress) {
786 		rc = pthread_join(spinner_th, NULL);
787 		if (rc)
788 			perror("Unable to end spinner thread");
789 	}
790 
791 	return (0);
792 }
793 
794 static size_t
795 vm_snapshot_mem(struct vmctx *ctx, int snapfd, size_t memsz, const bool op_wr)
796 {
797 	int ret;
798 	size_t lowmem, highmem, totalmem;
799 	char *baseaddr;
800 
801 	ret = vm_get_guestmem_from_ctx(ctx, &baseaddr, &lowmem, &highmem);
802 	if (ret) {
803 		fprintf(stderr, "%s: unable to retrieve guest memory size\r\n",
804 			__func__);
805 		return (0);
806 	}
807 	totalmem = lowmem + highmem;
808 
809 	if ((op_wr == false) && (totalmem != memsz)) {
810 		fprintf(stderr, "%s: mem size mismatch: %ld vs %ld\r\n",
811 			__func__, totalmem, memsz);
812 		return (0);
813 	}
814 
815 	winsize.ws_col = 80;
816 #ifdef TIOCGWINSZ
817 	ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
818 #endif /* TIOCGWINSZ */
819 	old_winch_handler = signal(SIGWINCH, winch_handler);
820 
821 	ret = vm_snapshot_mem_part(snapfd, 0, baseaddr, lowmem,
822 		totalmem, op_wr);
823 	if (ret) {
824 		fprintf(stderr, "%s: Could not %s lowmem\r\n",
825 			__func__, op_wr ? "write" : "read");
826 		totalmem = 0;
827 		goto done;
828 	}
829 
830 	if (highmem == 0)
831 		goto done;
832 
833 	ret = vm_snapshot_mem_part(snapfd, lowmem, baseaddr + 4*GB,
834 		highmem, totalmem, op_wr);
835 	if (ret) {
836 		fprintf(stderr, "%s: Could not %s highmem\r\n",
837 		        __func__, op_wr ? "write" : "read");
838 		totalmem = 0;
839 		goto done;
840 	}
841 
842 done:
843 	printf("\r\n");
844 	signal(SIGWINCH, old_winch_handler);
845 
846 	return (totalmem);
847 }
848 
849 int
850 restore_vm_mem(struct vmctx *ctx, struct restore_state *rstate)
851 {
852 	size_t restored;
853 
854 	restored = vm_snapshot_mem(ctx, rstate->vmmem_fd, rstate->vmmem_len,
855 				   false);
856 
857 	if (restored != rstate->vmmem_len)
858 		return (-1);
859 
860 	return (0);
861 }
862 
863 static int
864 vm_restore_kern_struct(struct vmctx *ctx, struct restore_state *rstate,
865 		       const struct vm_snapshot_kern_info *info)
866 {
867 	void *struct_ptr;
868 	size_t struct_size;
869 	int ret;
870 	struct vm_snapshot_meta *meta;
871 
872 	struct_ptr = lookup_struct(info->req, rstate, &struct_size);
873 	if (struct_ptr == NULL) {
874 		fprintf(stderr, "%s: Failed to lookup struct %s\r\n",
875 			__func__, info->struct_name);
876 		ret = -1;
877 		goto done;
878 	}
879 
880 	if (struct_size == 0) {
881 		fprintf(stderr, "%s: Kernel struct size was 0 for: %s\r\n",
882 			__func__, info->struct_name);
883 		ret = -1;
884 		goto done;
885 	}
886 
887 	meta = &(struct vm_snapshot_meta) {
888 		.ctx = ctx,
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(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 	int ret;
916 	int i;
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 int
929 vm_restore_user_dev(struct vmctx *ctx, 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 		.ctx = ctx,
953 		.dev_name = info->dev_name,
954 
955 		.buffer.buf_start = dev_ptr,
956 		.buffer.buf_size = dev_size,
957 
958 		.buffer.buf = dev_ptr,
959 		.buffer.buf_rem = dev_size,
960 
961 		.op = VM_SNAPSHOT_RESTORE,
962 	};
963 
964 	ret = (*info->snapshot_cb)(meta);
965 	if (ret != 0) {
966 		fprintf(stderr, "Failed to restore dev: %s\r\n",
967 			info->dev_name);
968 		return (-1);
969 	}
970 
971 	return (0);
972 }
973 
974 
975 int
976 vm_restore_user_devs(struct vmctx *ctx, struct restore_state *rstate)
977 {
978 	int ret;
979 	int i;
980 
981 	for (i = 0; i < nitems(snapshot_devs); i++) {
982 		ret = vm_restore_user_dev(ctx, rstate, &snapshot_devs[i]);
983 		if (ret != 0)
984 			return (ret);
985 	}
986 
987 	return 0;
988 }
989 
990 int
991 vm_pause_user_devs(struct vmctx *ctx)
992 {
993 	const struct vm_snapshot_dev_info *info;
994 	int ret;
995 	int i;
996 
997 	for (i = 0; i < nitems(snapshot_devs); i++) {
998 		info = &snapshot_devs[i];
999 		if (info->pause_cb == NULL)
1000 			continue;
1001 
1002 		ret = info->pause_cb(ctx, info->dev_name);
1003 		if (ret != 0)
1004 			return (ret);
1005 	}
1006 
1007 	return (0);
1008 }
1009 
1010 int
1011 vm_resume_user_devs(struct vmctx *ctx)
1012 {
1013 	const struct vm_snapshot_dev_info *info;
1014 	int ret;
1015 	int i;
1016 
1017 	for (i = 0; i < nitems(snapshot_devs); i++) {
1018 		info = &snapshot_devs[i];
1019 		if (info->resume_cb == NULL)
1020 			continue;
1021 
1022 		ret = info->resume_cb(ctx, info->dev_name);
1023 		if (ret != 0)
1024 			return (ret);
1025 	}
1026 
1027 	return (0);
1028 }
1029 
1030 static int
1031 vm_snapshot_kern_struct(int data_fd, xo_handle_t *xop, const char *array_key,
1032 			struct vm_snapshot_meta *meta, off_t *offset)
1033 {
1034 	int ret;
1035 	size_t data_size;
1036 	ssize_t write_cnt;
1037 
1038 	ret = vm_snapshot_req(meta);
1039 	if (ret != 0) {
1040 		fprintf(stderr, "%s: Failed to snapshot struct %s\r\n",
1041 			__func__, meta->dev_name);
1042 		ret = -1;
1043 		goto done;
1044 	}
1045 
1046 	data_size = vm_get_snapshot_size(meta);
1047 
1048 	write_cnt = write(data_fd, meta->buffer.buf_start, data_size);
1049 	if (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, i, error;
1074 	size_t offset, buf_size;
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 		.ctx = ctx,
1091 
1092 		.buffer.buf_start = buffer,
1093 		.buffer.buf_size = buf_size,
1094 
1095 		.op = VM_SNAPSHOT_SAVE,
1096 	};
1097 
1098 	xo_open_list_h(xop, JSON_STRUCT_ARR_KEY);
1099 	for (i = 0; i < nitems(snapshot_kern_structs); i++) {
1100 		meta->dev_name = snapshot_kern_structs[i].struct_name;
1101 		meta->dev_req  = snapshot_kern_structs[i].req;
1102 
1103 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1104 		meta->buffer.buf = meta->buffer.buf_start;
1105 		meta->buffer.buf_rem = meta->buffer.buf_size;
1106 
1107 		ret = vm_snapshot_kern_struct(data_fd, xop, JSON_DEV_ARR_KEY,
1108 					      meta, &offset);
1109 		if (ret != 0) {
1110 			error = -1;
1111 			goto err_vm_snapshot_kern_data;
1112 		}
1113 	}
1114 	xo_close_list_h(xop, JSON_STRUCT_ARR_KEY);
1115 
1116 err_vm_snapshot_kern_data:
1117 	if (buffer != NULL)
1118 		free(buffer);
1119 	return (error);
1120 }
1121 
1122 static int
1123 vm_snapshot_basic_metadata(struct vmctx *ctx, xo_handle_t *xop, size_t memsz)
1124 {
1125 
1126 	xo_open_container_h(xop, JSON_BASIC_METADATA_KEY);
1127 	xo_emit_h(xop, "{:" JSON_NCPUS_KEY "/%ld}\n", guest_ncpus);
1128 	xo_emit_h(xop, "{:" JSON_VMNAME_KEY "/%s}\n", vm_get_name(ctx));
1129 	xo_emit_h(xop, "{:" JSON_MEMSIZE_KEY "/%lu}\n", memsz);
1130 	xo_emit_h(xop, "{:" JSON_MEMFLAGS_KEY "/%d}\n", vm_get_memflags(ctx));
1131 	xo_close_container_h(xop, JSON_BASIC_METADATA_KEY);
1132 
1133 	return (0);
1134 }
1135 
1136 static int
1137 vm_snapshot_dev_write_data(int data_fd, xo_handle_t *xop, const char *array_key,
1138 			   struct vm_snapshot_meta *meta, off_t *offset)
1139 {
1140 	int ret;
1141 	size_t data_size;
1142 
1143 	data_size = vm_get_snapshot_size(meta);
1144 
1145 	ret = write(data_fd, meta->buffer.buf_start, data_size);
1146 	if (ret != data_size) {
1147 		perror("Failed to write all snapshotted data.");
1148 		return (-1);
1149 	}
1150 
1151 	/* Write metadata. */
1152 	xo_open_instance_h(xop, array_key);
1153 	xo_emit_h(xop, "{:" JSON_SNAPSHOT_REQ_KEY "/%s}\n", meta->dev_name);
1154 	xo_emit_h(xop, "{:" JSON_SIZE_KEY "/%lu}\n", data_size);
1155 	xo_emit_h(xop, "{:" JSON_FILE_OFFSET_KEY "/%lu}\n", *offset);
1156 	xo_close_instance_h(xop, array_key);
1157 
1158 	*offset += data_size;
1159 
1160 	return (0);
1161 }
1162 
1163 static int
1164 vm_snapshot_user_dev(const struct vm_snapshot_dev_info *info,
1165 		     int data_fd, xo_handle_t *xop,
1166 		     struct vm_snapshot_meta *meta, off_t *offset)
1167 {
1168 	int ret;
1169 
1170 	ret = (*info->snapshot_cb)(meta);
1171 	if (ret != 0) {
1172 		fprintf(stderr, "Failed to snapshot %s; ret=%d\r\n",
1173 			meta->dev_name, ret);
1174 		return (ret);
1175 	}
1176 
1177 	ret = vm_snapshot_dev_write_data(data_fd, xop, JSON_DEV_ARR_KEY, meta,
1178 					 offset);
1179 	if (ret != 0)
1180 		return (ret);
1181 
1182 	return (0);
1183 }
1184 
1185 static int
1186 vm_snapshot_user_devs(struct vmctx *ctx, int data_fd, xo_handle_t *xop)
1187 {
1188 	int ret, i;
1189 	off_t offset;
1190 	void *buffer;
1191 	size_t buf_size;
1192 	struct vm_snapshot_meta *meta;
1193 
1194 	buf_size = SNAPSHOT_BUFFER_SIZE;
1195 
1196 	offset = lseek(data_fd, 0, SEEK_CUR);
1197 	if (offset < 0) {
1198 		perror("Failed to get data file current offset.");
1199 		return (-1);
1200 	}
1201 
1202 	buffer = malloc(buf_size);
1203 	if (buffer == NULL) {
1204 		perror("Failed to allocate memory for snapshot buffer");
1205 		ret = ENOSPC;
1206 		goto snapshot_err;
1207 	}
1208 
1209 	meta = &(struct vm_snapshot_meta) {
1210 		.ctx = ctx,
1211 
1212 		.buffer.buf_start = buffer,
1213 		.buffer.buf_size = buf_size,
1214 
1215 		.op = VM_SNAPSHOT_SAVE,
1216 	};
1217 
1218 	xo_open_list_h(xop, JSON_DEV_ARR_KEY);
1219 
1220 	/* Restore other devices that support this feature */
1221 	for (i = 0; i < nitems(snapshot_devs); i++) {
1222 		meta->dev_name = snapshot_devs[i].dev_name;
1223 
1224 		memset(meta->buffer.buf_start, 0, meta->buffer.buf_size);
1225 		meta->buffer.buf = meta->buffer.buf_start;
1226 		meta->buffer.buf_rem = meta->buffer.buf_size;
1227 
1228 		ret = vm_snapshot_user_dev(&snapshot_devs[i], data_fd, xop,
1229 					   meta, &offset);
1230 		if (ret != 0)
1231 			goto snapshot_err;
1232 	}
1233 
1234 	xo_close_list_h(xop, JSON_DEV_ARR_KEY);
1235 
1236 snapshot_err:
1237 	if (buffer != NULL)
1238 		free(buffer);
1239 	return (ret);
1240 }
1241 
1242 void
1243 checkpoint_cpu_add(int vcpu)
1244 {
1245 
1246 	pthread_mutex_lock(&vcpu_lock);
1247 	CPU_SET(vcpu, &vcpus_active);
1248 
1249 	if (checkpoint_active) {
1250 		CPU_SET(vcpu, &vcpus_suspended);
1251 		while (checkpoint_active)
1252 			pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1253 		CPU_CLR(vcpu, &vcpus_suspended);
1254 	}
1255 	pthread_mutex_unlock(&vcpu_lock);
1256 }
1257 
1258 /*
1259  * When a vCPU is suspended for any reason, it calls
1260  * checkpoint_cpu_suspend().  This records that the vCPU is idle.
1261  * Before returning from suspension, checkpoint_cpu_resume() is
1262  * called.  In suspend we note that the vCPU is idle.  In resume we
1263  * pause the vCPU thread until the checkpoint is complete.  The reason
1264  * for the two-step process is that vCPUs might already be stopped in
1265  * the debug server when a checkpoint is requested.  This approach
1266  * allows us to account for and handle those vCPUs.
1267  */
1268 void
1269 checkpoint_cpu_suspend(int vcpu)
1270 {
1271 
1272 	pthread_mutex_lock(&vcpu_lock);
1273 	CPU_SET(vcpu, &vcpus_suspended);
1274 	if (checkpoint_active && CPU_CMP(&vcpus_active, &vcpus_suspended) == 0)
1275 		pthread_cond_signal(&vcpus_idle);
1276 	pthread_mutex_unlock(&vcpu_lock);
1277 }
1278 
1279 void
1280 checkpoint_cpu_resume(int vcpu)
1281 {
1282 
1283 	pthread_mutex_lock(&vcpu_lock);
1284 	while (checkpoint_active)
1285 		pthread_cond_wait(&vcpus_can_run, &vcpu_lock);
1286 	CPU_CLR(vcpu, &vcpus_suspended);
1287 	pthread_mutex_unlock(&vcpu_lock);
1288 }
1289 
1290 static void
1291 vm_vcpu_pause(struct vmctx *ctx)
1292 {
1293 
1294 	pthread_mutex_lock(&vcpu_lock);
1295 	checkpoint_active = true;
1296 	vm_suspend_cpu(ctx, -1);
1297 	while (CPU_CMP(&vcpus_active, &vcpus_suspended) != 0)
1298 		pthread_cond_wait(&vcpus_idle, &vcpu_lock);
1299 	pthread_mutex_unlock(&vcpu_lock);
1300 }
1301 
1302 static void
1303 vm_vcpu_resume(struct vmctx *ctx)
1304 {
1305 
1306 	pthread_mutex_lock(&vcpu_lock);
1307 	checkpoint_active = false;
1308 	pthread_mutex_unlock(&vcpu_lock);
1309 	vm_resume_cpu(ctx, -1);
1310 	pthread_cond_broadcast(&vcpus_can_run);
1311 }
1312 
1313 static int
1314 vm_checkpoint(struct vmctx *ctx, const char *checkpoint_file, bool stop_vm)
1315 {
1316 	int fd_checkpoint = 0, kdata_fd = 0;
1317 	int ret = 0;
1318 	int error = 0;
1319 	size_t memsz;
1320 	xo_handle_t *xop = NULL;
1321 	char *meta_filename = NULL;
1322 	char *kdata_filename = NULL;
1323 	FILE *meta_file = NULL;
1324 
1325 	kdata_filename = strcat_extension(checkpoint_file, ".kern");
1326 	if (kdata_filename == NULL) {
1327 		fprintf(stderr, "Failed to construct kernel data filename.\n");
1328 		return (-1);
1329 	}
1330 
1331 	kdata_fd = open(kdata_filename, O_WRONLY | O_CREAT | O_TRUNC, 0700);
1332 	if (kdata_fd < 0) {
1333 		perror("Failed to open kernel data snapshot file.");
1334 		error = -1;
1335 		goto done;
1336 	}
1337 
1338 	fd_checkpoint = open(checkpoint_file, O_RDWR | O_CREAT | O_TRUNC, 0700);
1339 
1340 	if (fd_checkpoint < 0) {
1341 		perror("Failed to create checkpoint file");
1342 		error = -1;
1343 		goto done;
1344 	}
1345 
1346 	meta_filename = strcat_extension(checkpoint_file, ".meta");
1347 	if (meta_filename == NULL) {
1348 		fprintf(stderr, "Failed to construct vm metadata filename.\n");
1349 		goto done;
1350 	}
1351 
1352 	meta_file = fopen(meta_filename, "w");
1353 	if (meta_file == NULL) {
1354 		perror("Failed to open vm metadata snapshot file.");
1355 		goto done;
1356 	}
1357 
1358 	xop = xo_create_to_file(meta_file, XO_STYLE_JSON, XOF_PRETTY);
1359 	if (xop == NULL) {
1360 		perror("Failed to get libxo handle on metadata file.");
1361 		goto done;
1362 	}
1363 
1364 	vm_vcpu_pause(ctx);
1365 
1366 	ret = vm_pause_user_devs(ctx);
1367 	if (ret != 0) {
1368 		fprintf(stderr, "Could not pause devices\r\n");
1369 		error = ret;
1370 		goto done;
1371 	}
1372 
1373 	memsz = vm_snapshot_mem(ctx, fd_checkpoint, 0, true);
1374 	if (memsz == 0) {
1375 		perror("Could not write guest memory to file");
1376 		error = -1;
1377 		goto done;
1378 	}
1379 
1380 	ret = vm_snapshot_basic_metadata(ctx, xop, memsz);
1381 	if (ret != 0) {
1382 		fprintf(stderr, "Failed to snapshot vm basic metadata.\n");
1383 		error = -1;
1384 		goto done;
1385 	}
1386 
1387 
1388 	ret = vm_snapshot_kern_structs(ctx, kdata_fd, xop);
1389 	if (ret != 0) {
1390 		fprintf(stderr, "Failed to snapshot vm kernel data.\n");
1391 		error = -1;
1392 		goto done;
1393 	}
1394 
1395 	ret = vm_snapshot_user_devs(ctx, kdata_fd, xop);
1396 	if (ret != 0) {
1397 		fprintf(stderr, "Failed to snapshot device state.\n");
1398 		error = -1;
1399 		goto done;
1400 	}
1401 
1402 	xo_finish_h(xop);
1403 
1404 	if (stop_vm) {
1405 		vm_destroy(ctx);
1406 		exit(0);
1407 	}
1408 
1409 done:
1410 	ret = vm_resume_user_devs(ctx);
1411 	if (ret != 0)
1412 		fprintf(stderr, "Could not resume devices\r\n");
1413 	vm_vcpu_resume(ctx);
1414 	if (fd_checkpoint > 0)
1415 		close(fd_checkpoint);
1416 	if (meta_filename != NULL)
1417 		free(meta_filename);
1418 	if (kdata_filename != NULL)
1419 		free(kdata_filename);
1420 	if (xop != NULL)
1421 		xo_destroy(xop);
1422 	if (meta_file != NULL)
1423 		fclose(meta_file);
1424 	if (kdata_fd > 0)
1425 		close(kdata_fd);
1426 	return (error);
1427 }
1428 
1429 static int
1430 handle_message(struct vmctx *ctx, nvlist_t *nvl)
1431 {
1432 	const char *cmd;
1433 	struct ipc_command **ipc_cmd;
1434 
1435 	if (!nvlist_exists_string(nvl, "cmd"))
1436 		return (EINVAL);
1437 
1438 	cmd = nvlist_get_string(nvl, "cmd");
1439 	IPC_COMMAND_FOREACH(ipc_cmd, ipc_cmd_set) {
1440 		if (strcmp(cmd, (*ipc_cmd)->name) == 0)
1441 			return ((*ipc_cmd)->handler(ctx, nvl));
1442 	}
1443 
1444 	return (EOPNOTSUPP);
1445 }
1446 
1447 /*
1448  * Listen for commands from bhyvectl
1449  */
1450 void *
1451 checkpoint_thread(void *param)
1452 {
1453 	int fd;
1454 	struct checkpoint_thread_info *thread_info;
1455 	nvlist_t *nvl;
1456 
1457 	pthread_set_name_np(pthread_self(), "checkpoint thread");
1458 	thread_info = (struct checkpoint_thread_info *)param;
1459 
1460 	while ((fd = accept(thread_info->socket_fd, NULL, NULL)) != -1) {
1461 		nvl = nvlist_recv(fd, 0);
1462 		if (nvl != NULL)
1463 			handle_message(thread_info->ctx, nvl);
1464 		else
1465 			EPRINTLN("nvlist_recv() failed: %s", strerror(errno));
1466 
1467 		close(fd);
1468 		nvlist_destroy(nvl);
1469 	}
1470 
1471 	return (NULL);
1472 }
1473 
1474 static int
1475 vm_do_checkpoint(struct vmctx *ctx, const nvlist_t *nvl)
1476 {
1477 	int error;
1478 
1479 	if (!nvlist_exists_string(nvl, "filename") ||
1480 	    !nvlist_exists_bool(nvl, "suspend"))
1481 		error = EINVAL;
1482 	else
1483 		error = vm_checkpoint(ctx, nvlist_get_string(nvl, "filename"),
1484 		    nvlist_get_bool(nvl, "suspend"));
1485 
1486 	return (error);
1487 }
1488 IPC_COMMAND(ipc_cmd_set, checkpoint, vm_do_checkpoint);
1489 
1490 void
1491 init_snapshot(void)
1492 {
1493 	int err;
1494 
1495 	err = pthread_mutex_init(&vcpu_lock, NULL);
1496 	if (err != 0)
1497 		errc(1, err, "checkpoint mutex init");
1498 	err = pthread_cond_init(&vcpus_idle, NULL);
1499 	if (err != 0)
1500 		errc(1, err, "checkpoint cv init (vcpus_idle)");
1501 	err = pthread_cond_init(&vcpus_can_run, NULL);
1502 	if (err != 0)
1503 		errc(1, err, "checkpoint cv init (vcpus_can_run)");
1504 }
1505 
1506 /*
1507  * Create the listening socket for IPC with bhyvectl
1508  */
1509 int
1510 init_checkpoint_thread(struct vmctx *ctx)
1511 {
1512 	struct checkpoint_thread_info *checkpoint_info = NULL;
1513 	struct sockaddr_un addr;
1514 	int socket_fd;
1515 	pthread_t checkpoint_pthread;
1516 	int err;
1517 
1518 	memset(&addr, 0, sizeof(addr));
1519 
1520 	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
1521 	if (socket_fd < 0) {
1522 		EPRINTLN("Socket creation failed: %s", strerror(errno));
1523 		err = -1;
1524 		goto fail;
1525 	}
1526 
1527 	addr.sun_family = AF_UNIX;
1528 
1529 	snprintf(addr.sun_path, sizeof(addr.sun_path), "%s%s",
1530 		 BHYVE_RUN_DIR, vm_get_name(ctx));
1531 	addr.sun_len = SUN_LEN(&addr);
1532 	unlink(addr.sun_path);
1533 
1534 	if (bind(socket_fd, (struct sockaddr *)&addr, addr.sun_len) != 0) {
1535 		EPRINTLN("Failed to bind socket \"%s\": %s\n",
1536 		    addr.sun_path, strerror(errno));
1537 		err = -1;
1538 		goto fail;
1539 	}
1540 
1541 	if (listen(socket_fd, 10) < 0) {
1542 		EPRINTLN("ipc socket listen: %s\n", strerror(errno));
1543 		err = errno;
1544 		goto fail;
1545 	}
1546 
1547 	checkpoint_info = calloc(1, sizeof(*checkpoint_info));
1548 	checkpoint_info->ctx = ctx;
1549 	checkpoint_info->socket_fd = socket_fd;
1550 
1551 	err = pthread_create(&checkpoint_pthread, NULL, checkpoint_thread,
1552 		checkpoint_info);
1553 	if (err != 0)
1554 		goto fail;
1555 
1556 	return (0);
1557 fail:
1558 	free(checkpoint_info);
1559 	if (socket_fd > 0)
1560 		close(socket_fd);
1561 	unlink(addr.sun_path);
1562 
1563 	return (err);
1564 }
1565 
1566 void
1567 vm_snapshot_buf_err(const char *bufname, const enum vm_snapshot_op op)
1568 {
1569 	const char *__op;
1570 
1571 	if (op == VM_SNAPSHOT_SAVE)
1572 		__op = "save";
1573 	else if (op == VM_SNAPSHOT_RESTORE)
1574 		__op = "restore";
1575 	else
1576 		__op = "unknown";
1577 
1578 	fprintf(stderr, "%s: snapshot-%s failed for %s\r\n",
1579 		__func__, __op, bufname);
1580 }
1581 
1582 int
1583 vm_snapshot_buf(volatile void *data, size_t data_size,
1584 		struct vm_snapshot_meta *meta)
1585 {
1586 	struct vm_snapshot_buffer *buffer;
1587 	int op;
1588 
1589 	buffer = &meta->buffer;
1590 	op = meta->op;
1591 
1592 	if (buffer->buf_rem < data_size) {
1593 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1594 		return (E2BIG);
1595 	}
1596 
1597 	if (op == VM_SNAPSHOT_SAVE)
1598 		memcpy(buffer->buf, (uint8_t *) data, data_size);
1599 	else if (op == VM_SNAPSHOT_RESTORE)
1600 		memcpy((uint8_t *) data, buffer->buf, data_size);
1601 	else
1602 		return (EINVAL);
1603 
1604 	buffer->buf += data_size;
1605 	buffer->buf_rem -= data_size;
1606 
1607 	return (0);
1608 }
1609 
1610 size_t
1611 vm_get_snapshot_size(struct vm_snapshot_meta *meta)
1612 {
1613 	size_t length;
1614 	struct vm_snapshot_buffer *buffer;
1615 
1616 	buffer = &meta->buffer;
1617 
1618 	if (buffer->buf_size < buffer->buf_rem) {
1619 		fprintf(stderr, "%s: Invalid buffer: size = %zu, rem = %zu\r\n",
1620 			__func__, buffer->buf_size, buffer->buf_rem);
1621 		length = 0;
1622 	} else {
1623 		length = buffer->buf_size - buffer->buf_rem;
1624 	}
1625 
1626 	return (length);
1627 }
1628 
1629 int
1630 vm_snapshot_guest2host_addr(void **addrp, size_t len, bool restore_null,
1631 			    struct vm_snapshot_meta *meta)
1632 {
1633 	int ret;
1634 	vm_paddr_t gaddr;
1635 
1636 	if (meta->op == VM_SNAPSHOT_SAVE) {
1637 		gaddr = paddr_host2guest(meta->ctx, *addrp);
1638 		if (gaddr == (vm_paddr_t) -1) {
1639 			if (!restore_null ||
1640 			    (restore_null && (*addrp != NULL))) {
1641 				ret = EFAULT;
1642 				goto done;
1643 			}
1644 		}
1645 
1646 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1647 	} else if (meta->op == VM_SNAPSHOT_RESTORE) {
1648 		SNAPSHOT_VAR_OR_LEAVE(gaddr, meta, ret, done);
1649 		if (gaddr == (vm_paddr_t) -1) {
1650 			if (!restore_null) {
1651 				ret = EFAULT;
1652 				goto done;
1653 			}
1654 		}
1655 
1656 		*addrp = paddr_guest2host(meta->ctx, gaddr, len);
1657 	} else {
1658 		ret = EINVAL;
1659 	}
1660 
1661 done:
1662 	return (ret);
1663 }
1664 
1665 int
1666 vm_snapshot_buf_cmp(volatile void *data, size_t data_size,
1667 		    struct vm_snapshot_meta *meta)
1668 {
1669 	struct vm_snapshot_buffer *buffer;
1670 	int op;
1671 	int ret;
1672 
1673 	buffer = &meta->buffer;
1674 	op = meta->op;
1675 
1676 	if (buffer->buf_rem < data_size) {
1677 		fprintf(stderr, "%s: buffer too small\r\n", __func__);
1678 		ret = E2BIG;
1679 		goto done;
1680 	}
1681 
1682 	if (op == VM_SNAPSHOT_SAVE) {
1683 		ret = 0;
1684 		memcpy(buffer->buf, (uint8_t *) data, data_size);
1685 	} else if (op == VM_SNAPSHOT_RESTORE) {
1686 		ret = memcmp((uint8_t *) data, buffer->buf, data_size);
1687 	} else {
1688 		ret = EINVAL;
1689 		goto done;
1690 	}
1691 
1692 	buffer->buf += data_size;
1693 	buffer->buf_rem -= data_size;
1694 
1695 done:
1696 	return (ret);
1697 }
1698