1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999, Matthew Dillon. All Rights Reserved.
5 * Copyright (c) 2001, Thomas Moestl. All Rights Reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/stat.h>
33 #include <sys/blist.h>
34 #include <sys/queue.h>
35 #include <sys/sysctl.h>
36
37 #include <vm/swap_pager.h>
38 #include <vm/vm_param.h>
39
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <kvm.h>
44 #include <nlist.h>
45 #include <paths.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <limits.h>
51
52 #include "kvm_private.h"
53
54 static struct nlist kvm_swap_nl[] = {
55 { .n_name = "_swtailq" }, /* list of swap devices and sizes */
56 { .n_name = "_dmmax" }, /* maximum size of a swap block */
57 { .n_name = NULL }
58 };
59
60 #define NL_SWTAILQ 0
61 #define NL_DMMAX 1
62
63 static int kvm_swap_nl_cached = 0;
64 static int unswdev; /* number of found swap dev's */
65 static int dmmax;
66
67 static int kvm_getswapinfo_kvm(kvm_t *, struct kvm_swap *, int, int);
68 static int kvm_getswapinfo_sysctl(kvm_t *, struct kvm_swap *, int, int);
69 static int nlist_init(kvm_t *);
70 static int getsysctl(kvm_t *, const char *, void *, size_t);
71
72 #define KREAD(kd, addr, obj) \
73 (kvm_read(kd, addr, (char *)(obj), sizeof(*obj)) != sizeof(*obj))
74 #define KGET(idx, var) \
75 KGET2(kvm_swap_nl[(idx)].n_value, var, kvm_swap_nl[(idx)].n_name)
76 #define KGET2(addr, var, msg) \
77 if (KREAD(kd, (u_long)(addr), (var))) { \
78 _kvm_err(kd, kd->program, "cannot read %s", msg); \
79 return (-1); \
80 }
81
82 #define GETSWDEVNAME(dev, str, flags) \
83 if (dev == NODEV) { \
84 strlcpy(str, "[NFS swap]", sizeof(str)); \
85 } else { \
86 snprintf( \
87 str, sizeof(str),"%s%s", \
88 ((flags & SWIF_DEV_PREFIX) ? _PATH_DEV : ""), \
89 devname(dev, S_IFCHR) \
90 ); \
91 }
92
93 int
kvm_getswapinfo(kvm_t * kd,struct kvm_swap * swap_ary,int swap_max,int flags)94 kvm_getswapinfo(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max, int flags)
95 {
96
97 /*
98 * clear cache
99 */
100 if (kd == NULL) {
101 kvm_swap_nl_cached = 0;
102 return(0);
103 }
104
105 if (ISALIVE(kd)) {
106 return kvm_getswapinfo_sysctl(kd, swap_ary, swap_max, flags);
107 } else {
108 return kvm_getswapinfo_kvm(kd, swap_ary, swap_max, flags);
109 }
110 }
111
112 int
kvm_getswapinfo_kvm(kvm_t * kd,struct kvm_swap * swap_ary,int swap_max,int flags)113 kvm_getswapinfo_kvm(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
114 int flags)
115 {
116 int i, ttl;
117 TAILQ_HEAD(, swdevt) swtailq;
118 struct swdevt *sp, swinfo;
119 struct kvm_swap tot;
120
121 if (!kd->arch->ka_native(kd)) {
122 _kvm_err(kd, kd->program,
123 "cannot read swapinfo from non-native core");
124 return (-1);
125 }
126
127 if (!nlist_init(kd))
128 return (-1);
129
130 bzero(&tot, sizeof(tot));
131 KGET(NL_SWTAILQ, &swtailq);
132 sp = TAILQ_FIRST(&swtailq);
133 for (i = 0; sp != NULL; i++) {
134 KGET2(sp, &swinfo, "swinfo");
135 ttl = swinfo.sw_nblks - dmmax;
136 if (i < swap_max - 1) {
137 bzero(&swap_ary[i], sizeof(swap_ary[i]));
138 swap_ary[i].ksw_total = ttl;
139 swap_ary[i].ksw_used = swinfo.sw_used;
140 swap_ary[i].ksw_flags = swinfo.sw_flags;
141 GETSWDEVNAME(swinfo.sw_dev, swap_ary[i].ksw_devname,
142 flags);
143 }
144 tot.ksw_total += ttl;
145 tot.ksw_used += swinfo.sw_used;
146 sp = TAILQ_NEXT(&swinfo, sw_list);
147 }
148
149 if (i >= swap_max)
150 i = swap_max - 1;
151 if (i >= 0)
152 swap_ary[i] = tot;
153
154 return(i);
155 }
156
157 #define GETSYSCTL(kd, name, var) \
158 getsysctl(kd, name, &(var), sizeof(var))
159
160 /* The maximum MIB length for vm.swap_info and an additional device number */
161 #define SWI_MAXMIB 3
162
163 int
kvm_getswapinfo_sysctl(kvm_t * kd,struct kvm_swap * swap_ary,int swap_max,int flags)164 kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
165 int flags)
166 {
167 int ti, ttl;
168 size_t mibi, len;
169 int soid[SWI_MAXMIB];
170 struct xswdev xsd;
171 struct kvm_swap tot;
172
173 if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
174 return -1;
175
176 mibi = SWI_MAXMIB - 1;
177 if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
178 _kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
179 strerror(errno));
180 return -1;
181 }
182 bzero(&tot, sizeof(tot));
183 for (unswdev = 0;; unswdev++) {
184 soid[mibi] = unswdev;
185 len = sizeof(xsd);
186 if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
187 if (errno == ENOENT)
188 break;
189 _kvm_err(kd, kd->program, "cannot read sysctl: %s.",
190 strerror(errno));
191 return -1;
192 }
193 if (len != sizeof(xsd)) {
194 _kvm_err(kd, kd->program, "struct xswdev has unexpected "
195 "size; kernel and libkvm out of sync?");
196 return -1;
197 }
198 if (xsd.xsw_version != XSWDEV_VERSION) {
199 _kvm_err(kd, kd->program, "struct xswdev version "
200 "mismatch; kernel and libkvm out of sync?");
201 return -1;
202 }
203
204 ttl = xsd.xsw_nblks - dmmax;
205 if (unswdev < swap_max - 1) {
206 bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
207 swap_ary[unswdev].ksw_total = ttl;
208 swap_ary[unswdev].ksw_used = xsd.xsw_used;
209 swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
210 GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
211 flags);
212 }
213 tot.ksw_total += ttl;
214 tot.ksw_used += xsd.xsw_used;
215 }
216
217 ti = unswdev;
218 if (ti >= swap_max)
219 ti = swap_max - 1;
220 if (ti >= 0)
221 swap_ary[ti] = tot;
222
223 return(ti);
224 }
225
226 static int
nlist_init(kvm_t * kd)227 nlist_init(kvm_t *kd)
228 {
229
230 if (kvm_swap_nl_cached)
231 return (1);
232
233 if (kvm_nlist(kd, kvm_swap_nl) < 0)
234 return (0);
235
236 /* Required entries */
237 if (kvm_swap_nl[NL_SWTAILQ].n_value == 0) {
238 _kvm_err(kd, kd->program, "unable to find swtailq");
239 return (0);
240 }
241
242 if (kvm_swap_nl[NL_DMMAX].n_value == 0) {
243 _kvm_err(kd, kd->program, "unable to find dmmax");
244 return (0);
245 }
246
247 /* Get globals, type of swap */
248 KGET(NL_DMMAX, &dmmax);
249
250 kvm_swap_nl_cached = 1;
251 return (1);
252 }
253
254 static int
getsysctl(kvm_t * kd,const char * name,void * ptr,size_t len)255 getsysctl(kvm_t *kd, const char *name, void *ptr, size_t len)
256 {
257 size_t nlen = len;
258 if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
259 _kvm_err(kd, kd->program, "cannot read sysctl %s:%s", name,
260 strerror(errno));
261 return (0);
262 }
263 if (nlen != len) {
264 _kvm_err(kd, kd->program, "sysctl %s has unexpected size", name);
265 return (0);
266 }
267 return (1);
268 }
269