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 * $FreeBSD$ 36 */ 37 38 #ifndef _BHYVE_SNAPSHOT_ 39 #define _BHYVE_SNAPSHOT_ 40 41 #include <machine/vmm_snapshot.h> 42 #include <libxo/xo.h> 43 #include <ucl.h> 44 45 #define BHYVE_RUN_DIR "/var/run/bhyve/" 46 #define MAX_SNAPSHOT_FILENAME PATH_MAX 47 48 struct vmctx; 49 50 struct restore_state { 51 int kdata_fd; 52 int vmmem_fd; 53 54 void *kdata_map; 55 size_t kdata_len; 56 57 size_t vmmem_len; 58 59 struct ucl_parser *meta_parser; 60 ucl_object_t *meta_root_obj; 61 }; 62 63 struct checkpoint_thread_info { 64 struct vmctx *ctx; 65 int socket_fd; 66 }; 67 68 typedef int (*vm_snapshot_dev_cb)(struct vm_snapshot_meta *); 69 typedef int (*vm_pause_dev_cb) (const char *); 70 typedef int (*vm_resume_dev_cb) (const char *); 71 72 struct vm_snapshot_dev_info { 73 const char *dev_name; /* device name */ 74 vm_snapshot_dev_cb snapshot_cb; /* callback for device snapshot */ 75 vm_pause_dev_cb pause_cb; /* callback for device pause */ 76 vm_resume_dev_cb resume_cb; /* callback for device resume */ 77 }; 78 79 struct vm_snapshot_kern_info { 80 const char *struct_name; /* kernel structure name*/ 81 enum snapshot_req req; /* request type */ 82 }; 83 84 void destroy_restore_state(struct restore_state *rstate); 85 86 const char *lookup_vmname(struct restore_state *rstate); 87 int lookup_memflags(struct restore_state *rstate); 88 size_t lookup_memsize(struct restore_state *rstate); 89 int lookup_guest_ncpus(struct restore_state *rstate); 90 91 void checkpoint_cpu_add(int vcpu); 92 void checkpoint_cpu_resume(int vcpu); 93 void checkpoint_cpu_suspend(int vcpu); 94 95 int restore_vm_mem(struct vmctx *ctx, struct restore_state *rstate); 96 int vm_restore_kern_structs(struct vmctx *ctx, struct restore_state *rstate); 97 98 int vm_restore_user_devs(struct restore_state *rstate); 99 int vm_pause_user_devs(void); 100 int vm_resume_user_devs(void); 101 102 int get_checkpoint_msg(int conn_fd, struct vmctx *ctx); 103 void *checkpoint_thread(void *param); 104 int init_checkpoint_thread(struct vmctx *ctx); 105 void init_snapshot(void); 106 107 int load_restore_file(const char *filename, struct restore_state *rstate); 108 109 int vm_snapshot_guest2host_addr(struct vmctx *ctx, void **addrp, size_t len, 110 bool restore_null, struct vm_snapshot_meta *meta); 111 112 /* 113 * Address variables are pointers to guest memory. 114 * 115 * When RNULL != 0, do not enforce invalid address checks; instead, make the 116 * pointer NULL at restore time. 117 */ 118 #define SNAPSHOT_GUEST2HOST_ADDR_OR_LEAVE(CTX, ADDR, LEN, RNULL, META, RES, LABEL) \ 119 do { \ 120 (RES) = vm_snapshot_guest2host_addr((CTX), (void **)&(ADDR), (LEN), \ 121 (RNULL), (META)); \ 122 if ((RES) != 0) { \ 123 if ((RES) == EFAULT) \ 124 fprintf(stderr, "%s: invalid address: %s\r\n", \ 125 __func__, #ADDR); \ 126 goto LABEL; \ 127 } \ 128 } while (0) 129 130 #endif 131