1 /*- 2 * Copyright (c) 2021-2022 Juniper Networks 3 * 4 * This software was developed by Mitchell Horne <mhorne@FreeBSD.org> 5 * under sponsorship from Juniper Networks and Klara Systems. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/conf.h> 32 #include <sys/caprights.h> 33 #include <sys/disk.h> 34 #include <sys/eventhandler.h> 35 #include <sys/fcntl.h> 36 #include <sys/file.h> 37 #include <sys/kerneldump.h> 38 #include <sys/limits.h> 39 #include <sys/malloc.h> 40 #include <sys/namei.h> 41 #include <sys/priv.h> 42 #include <sys/proc.h> 43 #include <sys/stat.h> 44 #include <sys/sysctl.h> 45 #include <sys/vnode.h> 46 47 #include <machine/vmparam.h> 48 49 static dumper_start_t vnode_dumper_start; 50 static dumper_t vnode_dump; 51 static dumper_hdr_t vnode_write_headers; 52 53 static struct sx livedump_sx; 54 SX_SYSINIT(livedump, &livedump_sx, "Livedump sx"); 55 56 /* 57 * Invoke a live minidump on the system. 58 */ 59 int 60 livedump_start(int fd, int flags, uint8_t compression) 61 { 62 #if MINIDUMP_PAGE_TRACKING == 1 63 struct dumperinfo di, *livedi; 64 struct diocskerneldump_arg kda; 65 struct vnode *vp; 66 struct file *fp; 67 void *rl_cookie; 68 int error; 69 70 error = priv_check(curthread, PRIV_KMEM_READ); 71 if (error != 0) 72 return (error); 73 74 if (flags != 0) 75 return (EINVAL); 76 77 error = getvnode(curthread, fd, &cap_write_rights, &fp); 78 if (error != 0) 79 return (error); 80 vp = fp->f_vnode; 81 82 if ((fp->f_flag & FWRITE) == 0) { 83 error = EBADF; 84 goto drop; 85 } 86 87 /* Set up a new dumper. */ 88 bzero(&di, sizeof(di)); 89 di.dumper_start = vnode_dumper_start; 90 di.dumper = vnode_dump; 91 di.dumper_hdr = vnode_write_headers; 92 di.blocksize = PAGE_SIZE; /* Arbitrary. */ 93 di.maxiosize = MAXDUMPPGS * PAGE_SIZE; 94 95 bzero(&kda, sizeof(kda)); 96 kda.kda_compression = compression; 97 error = dumper_create(&di, "livedump", &kda, &livedi); 98 if (error != 0) 99 goto drop; 100 101 /* Only allow one livedump to proceed at a time. */ 102 if (sx_try_xlock(&livedump_sx) == 0) { 103 dumper_destroy(livedi); 104 error = EBUSY; 105 goto drop; 106 } 107 108 /* To be used by the callback functions. */ 109 livedi->priv = vp; 110 111 /* Lock the entire file range and vnode. */ 112 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 113 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 114 115 EVENTHANDLER_INVOKE(livedumper_start, &error); 116 if (error != 0) 117 goto out; 118 119 dump_savectx(); 120 error = minidumpsys(livedi, true); 121 122 EVENTHANDLER_INVOKE(livedumper_finish); 123 out: 124 VOP_UNLOCK(vp); 125 vn_rangelock_unlock(vp, rl_cookie); 126 sx_xunlock(&livedump_sx); 127 dumper_destroy(livedi); 128 drop: 129 fdrop(fp, curthread); 130 return (error); 131 #else 132 return (EOPNOTSUPP); 133 #endif /* MINIDUMP_PAGE_TRACKING == 1 */ 134 } 135 136 int 137 vnode_dumper_start(struct dumperinfo *di, void *key, uint32_t keysize) 138 { 139 140 /* Always begin with an offset of zero. */ 141 di->dumpoff = 0; 142 143 KASSERT(keysize == 0, ("encryption not supported for livedumps")); 144 return (0); 145 } 146 147 /* 148 * Callback from dumpsys() to dump a chunk of memory. 149 * 150 * Parameters: 151 * arg Opaque private pointer to vnode 152 * virtual Virtual address (where to read the data from) 153 * physical Physical memory address (unused) 154 * offset Offset from start of core file 155 * length Data length 156 * 157 * Return value: 158 * 0 on success 159 * errno on error 160 */ 161 int 162 vnode_dump(void *arg, void *virtual, vm_offset_t physical __unused, 163 off_t offset, size_t length) 164 { 165 struct vnode *vp; 166 int error = 0; 167 168 vp = arg; 169 MPASS(vp != NULL); 170 ASSERT_VOP_LOCKED(vp, __func__); 171 172 EVENTHANDLER_INVOKE(livedumper_dump, virtual, offset, length, &error); 173 if (error != 0) 174 return (error); 175 176 /* Done? */ 177 if (virtual == NULL) 178 return (0); 179 180 error = vn_rdwr(UIO_WRITE, vp, virtual, length, offset, UIO_SYSSPACE, 181 IO_NODELOCKED, curthread->td_ucred, NOCRED, NULL, curthread); 182 if (error != 0) 183 uprintf("%s: error writing livedump block at offset %jx: %d\n", 184 __func__, (uintmax_t)offset, error); 185 return (error); 186 } 187 188 /* 189 * Callback from dumpsys() to write out the dump header, placed at the end. 190 */ 191 int 192 vnode_write_headers(struct dumperinfo *di, struct kerneldumpheader *kdh) 193 { 194 struct vnode *vp; 195 int error; 196 off_t offset; 197 198 vp = di->priv; 199 MPASS(vp != NULL); 200 ASSERT_VOP_LOCKED(vp, __func__); 201 202 /* Compensate for compression/encryption adjustment of dumpoff. */ 203 offset = roundup2(di->dumpoff, di->blocksize); 204 205 /* Write the kernel dump header to the end of the file. */ 206 error = vn_rdwr(UIO_WRITE, vp, kdh, sizeof(*kdh), offset, 207 UIO_SYSSPACE, IO_NODELOCKED, curthread->td_ucred, NOCRED, NULL, 208 curthread); 209 if (error != 0) 210 uprintf("%s: error writing livedump header: %d\n", __func__, 211 error); 212 return (error); 213 } 214