xref: /openbsd-src/usr.sbin/memconfig/memconfig.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /* $OpenBSD: memconfig.c,v 1.15 2011/09/20 08:28:51 deraadt Exp $ */
2 
3 /*-
4  * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5  * 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 THE 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 THE 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  * $FreeBSD: /home/ncvs/src/usr.sbin/memcontrol/memcontrol.c,v 1.8 2002/09/15 15:07:55 dwmalone Exp $
29  */
30 
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/memrange.h>
34 
35 #include <err.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 struct {
43 	const char	*name;
44 	int		val;
45 	int		kind;
46 #define MDF_SETTABLE	(1<<0)
47 } attrnames[] = {
48 	{"uncacheable",		MDF_UNCACHEABLE,	MDF_SETTABLE},
49 	{"write-combine",	MDF_WRITECOMBINE,	MDF_SETTABLE},
50 	{"write-through",	MDF_WRITETHROUGH,	MDF_SETTABLE},
51 	{"write-back",		MDF_WRITEBACK,		MDF_SETTABLE},
52 	{"write-protect",	MDF_WRITEPROTECT,	MDF_SETTABLE},
53 	{"force",		MDF_FORCE,		MDF_SETTABLE},
54 	{"unknown",		MDF_UNKNOWN,		0},
55 	{"fixed-base",		MDF_FIXBASE,		0},
56 	{"fixed-length",	MDF_FIXLEN,		0},
57 	{"set-by-firmware",	MDF_FIRMWARE,		0},
58 	{"active",		MDF_ACTIVE,		MDF_SETTABLE},
59 	{"fix-active",		MDF_FIXACTIVE,		0},
60 	{"bogus",		MDF_BOGUS,		0},
61 	{NULL,			0,			0}
62 };
63 
64 static void	listfunc(int, int, char *[]);
65 static void	setfunc(int, int, char *[]);
66 static void	clearfunc(int, int, char *[]);
67 static void	helpfunc(int, int, char *[]);
68 static void	help(const char *);
69 static struct mem_range_desc *mrgetall(int, int *);
70 
71 struct
72 {
73 	const char	*cmd;
74 	const char	*desc;
75 	void	(*func)(int, int, char *[]);
76 } functions[] = {
77 	{"list",
78 	 "List current memory range attributes\n"
79 	 "    list [-a]\n"
80 	 "        -a    list all range slots, even those that are inactive",
81 	 listfunc},
82 	{"set",
83 	 "Set memory range attributes\n"
84 	 "    set -b <base> -l <length> -o <owner> <attribute>\n"
85 	 "        <base>      memory range base address\n"
86 	 "        <length>    length of memory range in bytes, power of 2\n"
87 	 "        <owner>     text identifier for this setting (7 char max)\n"
88 	 "        <attribute> attribute(s) to be applied to this range:\n"
89 	 "                        uncacheable\n"
90 	 "                        write-combine\n"
91 	 "                        write-through\n"
92 	 "                        write-back\n"
93 	 "                        write-protect",
94 	 setfunc},
95 	{"clear",
96 	 "Clear memory range attributes\n"
97 	 "    clear -o <owner>\n"
98 	 "        <owner>     all ranges with this owner will be cleared\n"
99 	 "    clear -b <base> -l <length>\n"
100 	 "        <base>      memory range base address\n"
101 	 "        <length>    length of memory range in bytes, power of 2\n"
102 	 "                    Base and length must exactly match an existing range",
103 	 clearfunc},
104 	{NULL,	NULL,					helpfunc}
105 };
106 
107 int
108 main(int argc, char *argv[])
109 {
110 	int	 i, memfd = -1;
111 
112 	if (argc < 2) {
113 		help(NULL);
114 	} else {
115 		for (i = 0; functions[i].cmd != NULL; i++)
116 			if (!strcmp(argv[1], functions[i].cmd))
117 				break;
118 
119 		if ((functions[i].func != helpfunc) &&
120 		    (memfd = open("/dev/mem", O_RDONLY)) == -1)
121 			err(1, "can't open /dev/mem");
122 		functions[i].func(memfd, argc - 1, argv + 1);
123 		close(memfd);
124 	}
125 	return(0);
126 }
127 
128 static struct mem_range_desc *
129 mrgetall(int memfd, int *nmr)
130 {
131 	struct mem_range_desc	*mrd;
132 	struct mem_range_op	mro;
133 
134 	mro.mo_arg[0] = 0;
135 	if (ioctl(memfd, MEMRANGE_GET, &mro))
136 		err(1, "can't size range descriptor array");
137 
138 	*nmr = mro.mo_arg[0];
139 	mrd = calloc(*nmr, sizeof(struct mem_range_desc));
140 	if (mrd == NULL)
141 		errx(1, "can't allocate %zu bytes for %d range descriptors",
142 		     *nmr * sizeof(struct mem_range_desc), *nmr);
143 
144 	mro.mo_arg[0] = *nmr;
145 	mro.mo_desc = mrd;
146 	if (ioctl(memfd, MEMRANGE_GET, &mro))
147 		err(1, "can't fetch range descriptor array");
148 
149 	return(mrd);
150 }
151 
152 
153 static void
154 listfunc(int memfd, int argc, char *argv[])
155 {
156 	int	nd, i, j, k, ch, showall = 0;
157 	struct mem_range_desc	*mrd;
158 	char	*owner;
159 
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 			if (!(owner = strdup(optarg)))
168 				errx(1, "out of memory");
169 			break;
170 		default:
171 			help("list");
172 		}
173 
174 	mrd = mrgetall(memfd, &nd);
175 
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("%qx/%qx %.8s ", mrd[i].mr_base, mrd[i].mr_len,
182 		       mrd[i].mr_owner[0] ? mrd[i].mr_owner : "-");
183 		for (j = 0; j < 32; j++) {
184 			if ( ((1<<j) & mrd[i].mr_flags) == 0)
185 				continue;
186 			for (k = 0; attrnames[k].name != NULL; k++)
187 				if (((1<<j) & mrd[i].mr_flags) & attrnames[k].val) {
188 					printf("%s ", attrnames[k].name);
189 					break;
190 				}
191 			if (attrnames[k].name == NULL)
192 				printf("0x%x", (1<<j) & mrd[i].mr_flags);
193 		}
194 		printf("\n");
195 	}
196 	free(mrd);
197 	if (owner)
198 		free(owner);
199 }
200 
201 static void
202 setfunc(int memfd, int argc, char *argv[])
203 {
204 	struct mem_range_desc	 mrd;
205 	struct mem_range_op	 mro;
206 	int	 i, ch;
207 	char	*ep;
208 
209 	mrd.mr_base = 0;
210 	mrd.mr_len = 0;
211 	mrd.mr_flags = 0;
212 	strlcpy(mrd.mr_owner, "user", sizeof mrd.mr_owner);
213 
214 	while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
215 		switch(ch) {
216 		case 'b':
217 			mrd.mr_base = strtouq(optarg, &ep, 0);
218 			if ((ep == optarg) || (*ep != 0))
219 				help("set");
220 			break;
221 		case 'l':
222 			mrd.mr_len = strtouq(optarg, &ep, 0);
223 			if ((ep == optarg) || (*ep != 0))
224 				help("set");
225 			break;
226 		case 'o':
227 			if (*optarg == 0 ||
228 			    strlen(optarg) > sizeof(mrd.mr_owner)-1)
229 				help("set");
230 			strlcpy(mrd.mr_owner, optarg, sizeof mrd.mr_owner);
231 			break;
232 		default:
233 			help("set");
234 		}
235 
236 	if (mrd.mr_len == 0)
237 		help("set");
238 
239 	argc -= optind;
240 	argv += optind;
241 
242 	while(argc--) {
243 		for (i = 0; attrnames[i].name != NULL; i++) {
244 			if (!strcmp(attrnames[i].name, argv[0])) {
245 				if (!attrnames[i].kind & MDF_SETTABLE)
246 					help("flags");
247 				mrd.mr_flags |= attrnames[i].val;
248 				break;
249 			}
250 		}
251 		if (attrnames[i].name == NULL)
252 			help("flags");
253 		argv++;
254 	}
255 
256 	mro.mo_desc = &mrd;
257 	mro.mo_arg[0] = 0;
258 	if (ioctl(memfd, MEMRANGE_SET, &mro))
259 		err(1, "can't set range");
260 }
261 
262 static void
263 clearfunc(int memfd, int argc, char *argv[])
264 {
265 	struct mem_range_desc	 mrd, *mrdp;
266 	struct mem_range_op      mro;
267 	int	i, nd, ch, got_base = 0;
268 	char	*ep, *owner;
269 
270 	mrd.mr_base = 0;
271 	mrd.mr_len = 0;
272 	owner = NULL;
273 	while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
274 		switch(ch) {
275 		case 'b':
276 			mrd.mr_base = strtouq(optarg, &ep, 0);
277 			if ((ep == optarg) || (*ep != 0))
278 				help("clear");
279 			else
280 				got_base = 1;
281 			break;
282 		case 'l':
283 			mrd.mr_len = strtouq(optarg, &ep, 0);
284 			if ((ep == optarg) || (*ep != 0))
285 				help("clear");
286 			break;
287 		case 'o':
288 			if ((*optarg == 0) || (strlen(optarg) > 7))
289 				help("clear");
290 			if (!(owner = strdup(optarg)))
291 				errx(1, "out of memory");
292 			break;
293 
294 		default:
295 			help("clear");
296 		}
297 
298 	if (owner != NULL) {
299 		/* clear-by-owner */
300 		if (got_base || mrd.mr_len != 0)
301 			help("clear");
302 
303 		mrdp = mrgetall(memfd, &nd);
304 		mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
305 		for (i = 0; i < nd; i++) {
306 			if (!strcmp(owner, mrdp[i].mr_owner) &&
307 			    (mrdp[i].mr_flags & MDF_ACTIVE) &&
308 			    !(mrdp[i].mr_flags & MDF_FIXACTIVE)) {
309 
310 				mro.mo_desc = mrdp + i;
311 				if (ioctl(memfd, MEMRANGE_SET, &mro))
312 					warn("couldn't clear range owned by '%s'",
313 					    owner);
314 			}
315 		}
316 	} else if (got_base && mrd.mr_len != 0) {
317 		/* clear-by-base/len */
318 		mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
319 		mro.mo_desc = &mrd;
320 		if (ioctl(memfd, MEMRANGE_SET, &mro))
321 			err(1, "couldn't clear range");
322 	} else {
323 		help("clear");
324 	}
325 }
326 
327 static void
328 helpfunc(int memfd, int argc, char *argv[])
329 {
330 	help(argv[1]);
331 }
332 
333 static void
334 help(const char *what)
335 {
336 	int	 i;
337 
338 	if (what != NULL) {
339 		/* find a function that matches */
340 		for (i = 0; functions[i].cmd != NULL; i++)
341 			if (!strcmp(what, functions[i].cmd)) {
342 				fprintf(stderr, "%s\n", functions[i].desc);
343 				return;
344 			}
345 		fprintf(stderr, "Unknown command '%s'\n", what);
346 	}
347 
348 	/* print general help */
349 	fprintf(stderr, "Valid commands are :\n");
350 	for (i = 0; functions[i].cmd != NULL; i++)
351 		fprintf(stderr, "    %s\n", functions[i].cmd);
352 	fprintf(stderr, "Use help <command> for command-specific help\n");
353 }
354