2 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
26 * $FreeBSD: src/usr.sbin/memcontrol/memcontrol.c,v 1.3.4.5 2002/09/16 21:58:41 dwmalone Exp $
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/memrange.h>
46 #define MDF_SETTABLE (1<<0)
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},
63 static void listfunc(int memfd
, int argc
, char *argv
[]);
64 static void setfunc(int memfd
, int argc
, char *argv
[]);
65 static void clearfunc(int memfd
, int argc
, char *argv
[]);
66 static void helpfunc(int memfd
, int argc
, char *argv
[]);
67 static void help(const char *what
);
73 void (*func
)(int memfd
, int argc
, char *argv
[]);
76 "List current memory range attributes\n"
78 " -a list all range slots, even those that are inactive",
81 "Set memory range attributes\n"
82 " set -b <base> -l <length> -o <owner> <attribute>\n"
83 " <base> memory range base address\n"
84 " <length> length of memory range in bytes, power of 2\n"
85 " <owner> text identifier for this setting (7 char max)\n"
86 " <attribute> attribute(s) to be applied to this range:\n"
94 "Clear memory range attributes\n"
96 " <owner> all ranges with this owner will be cleared\n"
97 " clear -b <base> -l <length>\n"
98 " <base> memory range base address\n"
99 " <length> length of memory range in bytes, power of 2\n"
100 " Base and length must exactly match an existing range",
102 {NULL
, NULL
, helpfunc
}
106 main(int argc
, char *argv
[])
113 if ((memfd
= open(_PATH_MEM
, O_RDONLY
)) == -1)
114 err(1, "can't open %s", _PATH_MEM
);
116 for (i
= 0; functions
[i
].cmd
!= NULL
; i
++)
117 if (!strcmp(argv
[1], functions
[i
].cmd
))
119 functions
[i
].func(memfd
, argc
- 1, argv
+ 1);
125 static struct mem_range_desc
*
126 mrgetall(int memfd
, int *nmr
)
128 struct mem_range_desc
*mrd
;
129 struct mem_range_op mro
;
132 if (ioctl(memfd
, MEMRANGE_GET
, &mro
))
133 err(1, "can't size range descriptor array");
135 *nmr
= mro
.mo_arg
[0];
136 mrd
= malloc(*nmr
* sizeof(struct mem_range_desc
));
138 errx(1, "can't allocate %zu bytes for %d range descriptors",
139 *nmr
* sizeof(struct mem_range_desc
), *nmr
);
141 mro
.mo_arg
[0] = *nmr
;
143 if (ioctl(memfd
, MEMRANGE_GET
, &mro
))
144 err(1, "can't fetch range descriptor array");
151 listfunc(int memfd
, int argc
, char *argv
[])
153 struct mem_range_desc
*mrd
;
160 while ((ch
= getopt(argc
, argv
, "ao:")) != -1)
166 owner
= strdup(optarg
);
173 mrd
= mrgetall(memfd
, &nd
);
175 for (i
= 0; i
< nd
; i
++) {
176 if (!showall
&& !(mrd
[i
].mr_flags
& MDF_ACTIVE
))
178 if (owner
&& strcmp(mrd
[i
].mr_owner
, owner
))
180 printf("%jx/%jx %.8s ",
181 (uintmax_t)mrd
[i
].mr_base
, (uintmax_t)mrd
[i
].mr_len
,
182 mrd
[i
].mr_owner
[0] ? mrd
[i
].mr_owner
: "-");
183 for (j
= 0; attrnames
[j
].name
!= NULL
; j
++)
184 if (mrd
[i
].mr_flags
& attrnames
[j
].val
)
185 printf("%s ", attrnames
[j
].name
);
194 setfunc(int memfd
, int argc
, char *argv
[])
196 struct mem_range_desc mrd
;
197 struct mem_range_op mro
;
205 strcpy(mrd
.mr_owner
, "user");
206 while ((ch
= getopt(argc
, argv
, "b:l:o:")) != -1)
209 mrd
.mr_base
= strtouq(optarg
, &ep
, 0);
210 if ((ep
== optarg
) || (*ep
!= 0))
214 mrd
.mr_len
= strtouq(optarg
, &ep
, 0);
215 if ((ep
== optarg
) || (*ep
!= 0))
219 if ((*optarg
== 0) || (strlen(optarg
) > 7))
221 strcpy(mrd
.mr_owner
, optarg
);
236 for (i
= 0; attrnames
[i
].name
!= NULL
; i
++) {
237 if (!strcmp(attrnames
[i
].name
, argv
[0])) {
238 if (!(attrnames
[i
].kind
& MDF_SETTABLE
))
240 mrd
.mr_flags
|= attrnames
[i
].val
;
244 if (attrnames
[i
].name
== NULL
)
251 if (ioctl(memfd
, MEMRANGE_SET
, &mro
))
252 err(1, "can't set range");
256 clearfunc(int memfd
, int argc
, char *argv
[])
258 struct mem_range_desc mrd
, *mrdp
;
259 struct mem_range_op mro
;
267 while ((ch
= getopt(argc
, argv
, "b:l:o:")) != -1)
270 mrd
.mr_base
= strtouq(optarg
, &ep
, 0);
271 if ((ep
== optarg
) || (*ep
!= 0))
275 mrd
.mr_len
= strtouq(optarg
, &ep
, 0);
276 if ((ep
== optarg
) || (*ep
!= 0))
280 if ((*optarg
== 0) || (strlen(optarg
) > 7))
282 owner
= strdup(optarg
);
292 if ((mrd
.mr_base
!= 0) || (mrd
.mr_len
!= 0))
295 mrdp
= mrgetall(memfd
, &nd
);
296 mro
.mo_arg
[0] = MEMRANGE_SET_REMOVE
;
297 for (i
= 0; i
< nd
; i
++) {
298 if (!strcmp(owner
, mrdp
[i
].mr_owner
) &&
299 (mrdp
[i
].mr_flags
& MDF_ACTIVE
) &&
300 !(mrdp
[i
].mr_flags
& MDF_FIXACTIVE
)) {
302 mro
.mo_desc
= mrdp
+ i
;
303 if (ioctl(memfd
, MEMRANGE_SET
, &mro
))
304 warn("couldn't clear range owned by '%s'", owner
);
307 } else if (mrd
.mr_len
!= 0) {
308 /* clear-by-base/len */
309 mro
.mo_arg
[0] = MEMRANGE_SET_REMOVE
;
311 if (ioctl(memfd
, MEMRANGE_SET
, &mro
))
312 err(1, "couldn't clear range");
319 helpfunc(int memfd __unused
, int argc __unused
, char *argv
[])
325 help(const char *what
)
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
);
336 fprintf(stderr
, "Unknown command '%s'\n", what
);
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");