xref: /openbsd-src/usr.sbin/memconfig/memconfig.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /* $OpenBSD: memconfig.c,v 1.12 2007/09/02 15:19:39 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 	{"bogus",		MDF_BOGUS,		0},
60 	{NULL,			0,			0}
61 };
62 
63 static void	listfunc(int, int, char *[]);
64 static void	setfunc(int, int, char *[]);
65 static void	clearfunc(int, int, char *[]);
66 static void	helpfunc(int, int, char *[]);
67 static void	help(const char *);
68 static struct mem_range_desc *mrgetall(int, int *);
69 
70 struct
71 {
72 	const char	*cmd;
73 	const char	*desc;
74 	void	(*func)(int, int, char *[]);
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}
104 };
105 
106 int
107 main(int argc, char *argv[])
108 {
109 	int	 i, memfd;
110 
111 	if (argc < 2) {
112 		help(NULL);
113 	} else {
114 		if ((memfd = open("/dev/mem", O_RDONLY)) == -1)
115 			err(1, "can't open /dev/mem");
116 
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);
122 	}
123 	return(0);
124 }
125 
126 static struct mem_range_desc *
127 mrgetall(int memfd, int *nmr)
128 {
129 	struct mem_range_desc	*mrd;
130 	struct mem_range_op	mro;
131 
132 	mro.mo_arg[0] = 0;
133 	if (ioctl(memfd, MEMRANGE_GET, &mro))
134 		err(1, "can't size range descriptor array");
135 
136 	*nmr = mro.mo_arg[0];
137 	mrd = calloc(*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);
141 
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");
146 
147 	return(mrd);
148 }
149 
150 
151 static void
152 listfunc(int memfd, int argc, char *argv[])
153 {
154 	int	nd, i, j, ch, showall = 0;
155 	struct mem_range_desc	*mrd;
156 	char	*owner;
157 
158 	owner = NULL;
159 	while ((ch = getopt(argc, argv, "ao:")) != -1)
160 		switch(ch) {
161 		case 'a':
162 			showall = 1;
163 			break;
164 		case 'o':
165 			if (!(owner = strdup(optarg)))
166 				errx(1, "out of memory");
167 			break;
168 		default:
169 			help("list");
170 		}
171 
172 	mrd = mrgetall(memfd, &nd);
173 
174 	for (i = 0; i < nd; i++) {
175 		if (!showall && !(mrd[i].mr_flags & MDF_ACTIVE))
176 			continue;
177 		if (owner && strcmp(mrd[i].mr_owner, owner))
178 			continue;
179 		printf("%qx/%qx %.8s ", mrd[i].mr_base, mrd[i].mr_len,
180 		       mrd[i].mr_owner[0] ? mrd[i].mr_owner : "-");
181 		for (j = 0; attrnames[j].name != NULL; j++)
182 			if (mrd[i].mr_flags & attrnames[j].val)
183 				printf("%s ", attrnames[j].name);
184 		printf("\n");
185 	}
186 	free(mrd);
187 	if (owner)
188 		free(owner);
189 }
190 
191 static void
192 setfunc(int memfd, int argc, char *argv[])
193 {
194 	struct mem_range_desc	 mrd;
195 	struct mem_range_op	 mro;
196 	int	 i, ch;
197 	char	*ep;
198 
199 	mrd.mr_base = 0;
200 	mrd.mr_len = 0;
201 	mrd.mr_flags = 0;
202 	strlcpy(mrd.mr_owner, "user", sizeof mrd.mr_owner);
203 
204 	while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
205 		switch(ch) {
206 		case 'b':
207 			mrd.mr_base = strtouq(optarg, &ep, 0);
208 			if ((ep == optarg) || (*ep != 0))
209 				help("set");
210 			break;
211 		case 'l':
212 			mrd.mr_len = strtouq(optarg, &ep, 0);
213 			if ((ep == optarg) || (*ep != 0))
214 				help("set");
215 			break;
216 		case 'o':
217 			if (*optarg == 0 ||
218 			    strlen(optarg) > sizeof(mrd.mr_owner)-1)
219 				help("set");
220 			strlcpy(mrd.mr_owner, optarg, sizeof mrd.mr_owner);
221 			break;
222 		default:
223 			help("set");
224 		}
225 
226 	if (mrd.mr_len == 0)
227 		help("set");
228 
229 	argc -= optind;
230 	argv += optind;
231 
232 	while(argc--) {
233 		for (i = 0; attrnames[i].name != NULL; i++) {
234 			if (!strcmp(attrnames[i].name, argv[0])) {
235 				if (!attrnames[i].kind & MDF_SETTABLE)
236 					help("flags");
237 				mrd.mr_flags |= attrnames[i].val;
238 				break;
239 			}
240 		}
241 		if (attrnames[i].name == NULL)
242 			help("flags");
243 		argv++;
244 	}
245 
246 	mro.mo_desc = &mrd;
247 	mro.mo_arg[0] = 0;
248 	if (ioctl(memfd, MEMRANGE_SET, &mro))
249 		err(1, "can't set range");
250 }
251 
252 static void
253 clearfunc(int memfd, int argc, char *argv[])
254 {
255 	struct mem_range_desc	 mrd, *mrdp;
256 	struct mem_range_op      mro;
257 	int	i, nd, ch;
258 	char	*ep, *owner;
259 
260 	mrd.mr_base = 0;
261 	mrd.mr_len = 0;
262 	owner = NULL;
263 	while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
264 		switch(ch) {
265 		case 'b':
266 			mrd.mr_base = strtouq(optarg, &ep, 0);
267 			if ((ep == optarg) || (*ep != 0))
268 				help("clear");
269 			break;
270 		case 'l':
271 			mrd.mr_len = strtouq(optarg, &ep, 0);
272 			if ((ep == optarg) || (*ep != 0))
273 				help("clear");
274 			break;
275 		case 'o':
276 			if ((*optarg == 0) || (strlen(optarg) > 7))
277 				help("clear");
278 			if (!(owner = strdup(optarg)))
279 				errx(1, "out of memory");
280 			break;
281 
282 		default:
283 			help("clear");
284 		}
285 
286 	if (owner != NULL) {
287 		/* clear-by-owner */
288 		if ((mrd.mr_base != 0) || (mrd.mr_len != 0))
289 			help("clear");
290 
291 		mrdp = mrgetall(memfd, &nd);
292 		mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
293 		for (i = 0; i < nd; i++) {
294 			if (!strcmp(owner, mrdp[i].mr_owner) &&
295 			    (mrdp[i].mr_flags & MDF_ACTIVE) &&
296 			    !(mrdp[i].mr_flags & MDF_FIXACTIVE)) {
297 
298 				mro.mo_desc = mrdp + i;
299 				if (ioctl(memfd, MEMRANGE_SET, &mro))
300 					warn("couldn't clear range owned by '%s'",
301 					    owner);
302 			}
303 		}
304 	} else if ((mrd.mr_base != 0) && (mrd.mr_len != 0)) {
305 		/* clear-by-base/len */
306 		mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
307 		mro.mo_desc = &mrd;
308 		if (ioctl(memfd, MEMRANGE_SET, &mro))
309 			err(1, "couldn't clear range");
310 	} else {
311 		help("clear");
312 	}
313 }
314 
315 static void
316 helpfunc(int memfd, int argc, char *argv[])
317 {
318 	help(argv[1]);
319 }
320 
321 static void
322 help(const char *what)
323 {
324 	int	 i;
325 
326 	if (what != NULL) {
327 		/* find a function that matches */
328 		for (i = 0; functions[i].cmd != NULL; i++)
329 			if (!strcmp(what, functions[i].cmd)) {
330 				fprintf(stderr, "%s\n", functions[i].desc);
331 				return;
332 			}
333 		fprintf(stderr, "Unknown command '%s'\n", what);
334 	}
335 
336 	/* print general help */
337 	fprintf(stderr, "Valid commands are :\n");
338 	for (i = 0; functions[i].cmd != NULL; i++)
339 		fprintf(stderr, "    %s\n", functions[i].cmd);
340 	fprintf(stderr, "Use help <command> for command-specific help\n");
341 }
342