1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * All rights reserved.
7 *
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #ifndef _SYS_KERNELDUMP_H
39 #define _SYS_KERNELDUMP_H
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43
44 #include <machine/endian.h>
45
46 #if BYTE_ORDER == LITTLE_ENDIAN
47 #define dtoh32(x) __bswap32(x)
48 #define dtoh64(x) __bswap64(x)
49 #define htod32(x) __bswap32(x)
50 #define htod64(x) __bswap64(x)
51 #elif BYTE_ORDER == BIG_ENDIAN
52 #define dtoh32(x) (x)
53 #define dtoh64(x) (x)
54 #define htod32(x) (x)
55 #define htod64(x) (x)
56 #endif
57
58 #define KERNELDUMP_COMP_NONE 0
59 #define KERNELDUMP_COMP_GZIP 1
60 #define KERNELDUMP_COMP_ZSTD 2
61
62 #define KERNELDUMP_ENC_NONE 0
63 #define KERNELDUMP_ENC_AES_256_CBC 1
64 #define KERNELDUMP_ENC_CHACHA20 2
65
66 #define KERNELDUMP_BUFFER_SIZE 4096
67 #define KERNELDUMP_IV_MAX_SIZE 32
68 #define KERNELDUMP_KEY_MAX_SIZE 64
69 #define KERNELDUMP_ENCKEY_MAX_SIZE (16384 / 8)
70
71 /*
72 * All uintX_t fields are in dump byte order, which is the same as
73 * network byte order. Use the macros defined above to read or
74 * write the fields.
75 */
76 struct kerneldumpheader {
77 char magic[20];
78 #define KERNELDUMPMAGIC "FreeBSD Kernel Dump"
79 #define TEXTDUMPMAGIC "FreeBSD Text Dump"
80 #define KERNELDUMPMAGIC_CLEARED "Cleared Kernel Dump"
81 char architecture[12];
82 uint32_t version;
83 #define KERNELDUMPVERSION 4
84 #define KERNELDUMP_TEXT_VERSION 4
85 uint32_t architectureversion;
86 #define KERNELDUMP_AARCH64_VERSION 1
87 #define KERNELDUMP_AMD64_VERSION 2
88 #define KERNELDUMP_ARM_VERSION 1
89 #define KERNELDUMP_I386_VERSION 2
90 #define KERNELDUMP_MIPS_VERSION 1
91 #define KERNELDUMP_POWERPC_VERSION 1
92 #define KERNELDUMP_RISCV_VERSION 1
93 #define KERNELDUMP_SPARC64_VERSION 1
94 uint64_t dumplength; /* excl headers */
95 uint64_t dumptime;
96 uint32_t dumpkeysize;
97 uint32_t blocksize;
98 char hostname[64];
99 char versionstring[192];
100 char panicstring[175];
101 uint8_t compression;
102 uint64_t dumpextent;
103 char unused[4];
104 uint32_t parity;
105 };
106
107 struct kerneldumpkey {
108 uint8_t kdk_encryption;
109 uint8_t kdk_iv[KERNELDUMP_IV_MAX_SIZE];
110 uint32_t kdk_encryptedkeysize;
111 uint8_t kdk_encryptedkey[];
112 } __packed;
113
114 /*
115 * Parity calculation is endian insensitive.
116 */
117 static __inline u_int32_t
kerneldump_parity(struct kerneldumpheader * kdhp)118 kerneldump_parity(struct kerneldumpheader *kdhp)
119 {
120 uint32_t *up, parity;
121 u_int i;
122
123 up = (uint32_t *)kdhp;
124 parity = 0;
125 for (i = 0; i < sizeof *kdhp; i += sizeof *up)
126 parity ^= *up++;
127 return (parity);
128 }
129
130 #ifdef _KERNEL
131 struct dump_pa {
132 vm_paddr_t pa_start;
133 vm_paddr_t pa_size;
134 };
135
136 struct minidumpstate {
137 struct msgbuf *msgbufp;
138 struct bitset *dump_bitset;
139 };
140
141 int minidumpsys(struct dumperinfo *, bool);
142 int dumpsys_generic(struct dumperinfo *);
143
144 void dumpsys_map_chunk(vm_paddr_t, size_t, void **);
145 typedef int dumpsys_callback_t(struct dump_pa *, int, void *);
146 int dumpsys_foreach_chunk(dumpsys_callback_t, void *);
147 int dumpsys_cb_dumpdata(struct dump_pa *, int, void *);
148 int dumpsys_buf_seek(struct dumperinfo *, size_t);
149 int dumpsys_buf_write(struct dumperinfo *, char *, size_t);
150 int dumpsys_buf_flush(struct dumperinfo *);
151
152 void dumpsys_gen_pa_init(void);
153 struct dump_pa *dumpsys_gen_pa_next(struct dump_pa *);
154 void dumpsys_gen_wbinv_all(void);
155 void dumpsys_gen_unmap_chunk(vm_paddr_t, size_t, void *);
156 int dumpsys_gen_write_aux_headers(struct dumperinfo *);
157
158 void dumpsys_pb_init(uint64_t);
159 void dumpsys_pb_progress(size_t);
160
161 extern int do_minidump;
162
163 int livedump_start(int, int, uint8_t);
164 int livedump_start_vnode(struct vnode *, int, uint8_t);
165
166 /* Live minidump events */
167 typedef void (*livedump_start_fn)(void *arg, int *errorp);
168 typedef void (*livedump_dump_fn)(void *arg, void *virtual, off_t offset,
169 size_t len, int *errorp);
170 typedef void (*livedump_finish_fn)(void *arg);
171 EVENTHANDLER_DECLARE(livedumper_start, livedump_start_fn);
172 EVENTHANDLER_DECLARE(livedumper_dump, livedump_dump_fn);
173 EVENTHANDLER_DECLARE(livedumper_finish, livedump_finish_fn);
174
175 #endif
176
177 #endif /* _SYS_KERNELDUMP_H */
178