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