nlookup.9 - document nlookup_init_root
[dragonfly.git] / usr.sbin / memcontrol / memcontrol.c
blobd240b40ff75d3fb20264dd3c38c4401231ae4482
1 /*-
2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/memcontrol/memcontrol.c,v 1.3.4.5 2002/09/16 21:58:41 dwmalone Exp $
27 * $DragonFly: src/usr.sbin/memcontrol/memcontrol.c,v 1.3 2005/03/18 01:57:58 cpressey Exp $
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/memrange.h>
34 #include <err.h>
35 #include <fcntl.h>
36 #include <paths.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 struct
44 const char *name;
45 int val;
46 int kind;
47 #define MDF_SETTABLE (1<<0)
48 } attrnames[] = {
49 {"uncacheable", MDF_UNCACHEABLE, MDF_SETTABLE},
50 {"write-combine", MDF_WRITECOMBINE, MDF_SETTABLE},
51 {"write-through", MDF_WRITETHROUGH, MDF_SETTABLE},
52 {"write-back", MDF_WRITEBACK, MDF_SETTABLE},
53 {"write-protect", MDF_WRITEPROTECT, MDF_SETTABLE},
54 {"force", MDF_FORCE, MDF_SETTABLE},
55 {"unknown", MDF_UNKNOWN, 0},
56 {"fixed-base", MDF_FIXBASE, 0},
57 {"fixed-length", MDF_FIXLEN, 0},
58 {"set-by-firmware", MDF_FIRMWARE, 0},
59 {"active", MDF_ACTIVE, MDF_SETTABLE},
60 {"bogus", MDF_BOGUS, 0},
61 {NULL, 0, 0}
64 static void listfunc(int memfd, int argc, char *argv[]);
65 static void setfunc(int memfd, int argc, char *argv[]);
66 static void clearfunc(int memfd, int argc, char *argv[]);
67 static void helpfunc(int memfd, int argc, char *argv[]);
68 static void help(const char *what);
70 struct
72 const char *cmd;
73 const char *desc;
74 void (*func)(int memfd, int argc, char *argv[]);
75 } functions[] = {
76 {"list",
77 "List current memory range attributes\n"
78 " list [-a]\n"
79 " -a list all range slots, even those that are inactive",
80 listfunc},
81 {"set",
82 "Set memory range attributes\n"
83 " set -b <base> -l <length> -o <owner> <attribute>\n"
84 " <base> memory range base address\n"
85 " <length> length of memory range in bytes, power of 2\n"
86 " <owner> text identifier for this setting (7 char max)\n"
87 " <attribute> attribute(s) to be applied to this range:\n"
88 " uncacheable\n"
89 " write-combine\n"
90 " write-through\n"
91 " write-back\n"
92 " write-protect",
93 setfunc},
94 {"clear",
95 "Clear memory range attributes\n"
96 " clear -o <owner>\n"
97 " <owner> all ranges with this owner will be cleared\n"
98 " clear -b <base> -l <length>\n"
99 " <base> memory range base address\n"
100 " <length> length of memory range in bytes, power of 2\n"
101 " Base and length must exactly match an existing range",
102 clearfunc},
103 {NULL, NULL, helpfunc}
107 main(int argc, char *argv[])
109 int i, memfd;
111 if (argc < 2) {
112 help(NULL);
113 } else {
114 if ((memfd = open(_PATH_MEM, O_RDONLY)) == -1)
115 err(1, "can't open %s", _PATH_MEM);
117 for (i = 0; functions[i].cmd != NULL; i++)
118 if (!strcmp(argv[1], functions[i].cmd))
119 break;
120 functions[i].func(memfd, argc - 1, argv + 1);
121 close(memfd);
123 return(0);
126 static struct mem_range_desc *
127 mrgetall(int memfd, int *nmr)
129 struct mem_range_desc *mrd;
130 struct mem_range_op mro;
132 mro.mo_arg[0] = 0;
133 if (ioctl(memfd, MEMRANGE_GET, &mro))
134 err(1, "can't size range descriptor array");
136 *nmr = mro.mo_arg[0];
137 mrd = malloc(*nmr * sizeof(struct mem_range_desc));
138 if (mrd == NULL)
139 errx(1, "can't allocate %zu bytes for %d range descriptors",
140 *nmr * sizeof(struct mem_range_desc), *nmr);
142 mro.mo_arg[0] = *nmr;
143 mro.mo_desc = mrd;
144 if (ioctl(memfd, MEMRANGE_GET, &mro))
145 err(1, "can't fetch range descriptor array");
147 return(mrd);
151 static void
152 listfunc(int memfd, int argc, char *argv[])
154 struct mem_range_desc *mrd;
155 int nd, i, j;
156 int ch;
157 int showall = 0;
158 char *owner;
160 owner = NULL;
161 while ((ch = getopt(argc, argv, "ao:")) != -1)
162 switch(ch) {
163 case 'a':
164 showall = 1;
165 break;
166 case 'o':
167 owner = strdup(optarg);
168 break;
169 case '?':
170 default:
171 help("list");
174 mrd = mrgetall(memfd, &nd);
176 for (i = 0; i < nd; i++) {
177 if (!showall && !(mrd[i].mr_flags & MDF_ACTIVE))
178 continue;
179 if (owner && strcmp(mrd[i].mr_owner, owner))
180 continue;
181 printf("%jx/%jx %.8s ",
182 (uintmax_t)mrd[i].mr_base, (uintmax_t)mrd[i].mr_len,
183 mrd[i].mr_owner[0] ? mrd[i].mr_owner : "-");
184 for (j = 0; attrnames[j].name != NULL; j++)
185 if (mrd[i].mr_flags & attrnames[j].val)
186 printf("%s ", attrnames[j].name);
187 printf("\n");
189 free(mrd);
190 if (owner)
191 free(owner);
194 static void
195 setfunc(int memfd, int argc, char *argv[])
197 struct mem_range_desc mrd;
198 struct mem_range_op mro;
199 int i;
200 int ch;
201 char *ep;
203 mrd.mr_base = 0;
204 mrd.mr_len = 0;
205 mrd.mr_flags = 0;
206 strcpy(mrd.mr_owner, "user");
207 while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
208 switch(ch) {
209 case 'b':
210 mrd.mr_base = strtouq(optarg, &ep, 0);
211 if ((ep == optarg) || (*ep != 0))
212 help("set");
213 break;
214 case 'l':
215 mrd.mr_len = strtouq(optarg, &ep, 0);
216 if ((ep == optarg) || (*ep != 0))
217 help("set");
218 break;
219 case 'o':
220 if ((*optarg == 0) || (strlen(optarg) > 7))
221 help("set");
222 strcpy(mrd.mr_owner, optarg);
223 break;
225 case '?':
226 default:
227 help("set");
230 if (mrd.mr_len == 0)
231 help("set");
233 argc -= optind;
234 argv += optind;
236 while(argc--) {
237 for (i = 0; attrnames[i].name != NULL; i++) {
238 if (!strcmp(attrnames[i].name, argv[0])) {
239 if (!attrnames[i].kind & MDF_SETTABLE)
240 help("flags");
241 mrd.mr_flags |= attrnames[i].val;
242 break;
245 if (attrnames[i].name == NULL)
246 help("flags");
247 argv++;
250 mro.mo_desc = &mrd;
251 mro.mo_arg[0] = 0;
252 if (ioctl(memfd, MEMRANGE_SET, &mro))
253 err(1, "can't set range");
256 static void
257 clearfunc(int memfd, int argc, char *argv[])
259 struct mem_range_desc mrd, *mrdp;
260 struct mem_range_op mro;
261 int i, nd;
262 int ch;
263 char *ep, *owner;
265 mrd.mr_base = 0;
266 mrd.mr_len = 0;
267 owner = NULL;
268 while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
269 switch(ch) {
270 case 'b':
271 mrd.mr_base = strtouq(optarg, &ep, 0);
272 if ((ep == optarg) || (*ep != 0))
273 help("clear");
274 break;
275 case 'l':
276 mrd.mr_len = strtouq(optarg, &ep, 0);
277 if ((ep == optarg) || (*ep != 0))
278 help("clear");
279 break;
280 case 'o':
281 if ((*optarg == 0) || (strlen(optarg) > 7))
282 help("clear");
283 owner = strdup(optarg);
284 break;
286 case '?':
287 default:
288 help("clear");
291 if (owner != NULL) {
292 /* clear-by-owner */
293 if ((mrd.mr_base != 0) || (mrd.mr_len != 0))
294 help("clear");
296 mrdp = mrgetall(memfd, &nd);
297 mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
298 for (i = 0; i < nd; i++) {
299 if (!strcmp(owner, mrdp[i].mr_owner) &&
300 (mrdp[i].mr_flags & MDF_ACTIVE) &&
301 !(mrdp[i].mr_flags & MDF_FIXACTIVE)) {
303 mro.mo_desc = mrdp + i;
304 if (ioctl(memfd, MEMRANGE_SET, &mro))
305 warn("couldn't clear range owned by '%s'", owner);
308 } else if (mrd.mr_len != 0) {
309 /* clear-by-base/len */
310 mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
311 mro.mo_desc = &mrd;
312 if (ioctl(memfd, MEMRANGE_SET, &mro))
313 err(1, "couldn't clear range");
314 } else {
315 help("clear");
319 static void
320 helpfunc(int memfd __unused, int argc __unused, char *argv[])
322 help(argv[1]);
325 static void
326 help(const char *what)
328 int i;
330 if (what != NULL) {
331 /* find a function that matches */
332 for (i = 0; functions[i].cmd != NULL; i++)
333 if (!strcmp(what, functions[i].cmd)) {
334 fprintf(stderr, "%s\n", functions[i].desc);
335 return;
337 fprintf(stderr, "Unknown command '%s'\n", what);
340 /* print general help */
341 fprintf(stderr, "Valid commands are :\n");
342 for (i = 0; functions[i].cmd != NULL; i++)
343 fprintf(stderr, " %s\n", functions[i].cmd);
344 fprintf(stderr, "Use help <command> for command-specific help\n");