summaryrefslogtreecommitdiffstats
path: root/usr.sbin/memcontrol/memcontrol.c
blob: b6cb25f000e119066636bdef3abaf76318864d9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*-
 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/memrange.h>

#include <err.h>
#include <fcntl.h>
#include <inttypes.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct
{
    const char	*name;
    int		val;
    int		kind;
#define MDF_SETTABLE	(1<<0)
} attrnames[] = {
    {"uncacheable",	MDF_UNCACHEABLE,	MDF_SETTABLE},
    {"write-combine",	MDF_WRITECOMBINE,	MDF_SETTABLE},
    {"write-through",	MDF_WRITETHROUGH,	MDF_SETTABLE},
    {"write-back",	MDF_WRITEBACK,		MDF_SETTABLE},
    {"write-protect",	MDF_WRITEPROTECT,	MDF_SETTABLE},
    {"force",		MDF_FORCE,		MDF_SETTABLE},
    {"unknown",		MDF_UNKNOWN,		0},
    {"fixed-base",	MDF_FIXBASE,		0},
    {"fixed-length",	MDF_FIXLEN,		0},
    {"set-by-firmware",	MDF_FIRMWARE,		0},
    {"active",		MDF_ACTIVE,		MDF_SETTABLE},
    {"bogus",		MDF_BOGUS,		0},
    {NULL,		0,			0}
};

static void	listfunc(int memfd, int argc, char *argv[]);
static void	setfunc(int memfd, int argc, char *argv[]);
static void	clearfunc(int memfd, int argc, char *argv[]);
static void	helpfunc(int memfd, int argc, char *argv[]);
static void	help(const char *what);

struct 
{
    const char	*cmd;
    const char	*desc;
    void	(*func)(int memfd, int argc, char *argv[]);
} functions[] = {
    {"list",	
     "List current memory range attributes\n"
     "    list [-a]\n"
     "        -a    list all range slots, even those that are inactive",
     listfunc},
    {"set",	
     "Set memory range attributes\n"
     "    set -b <base> -l <length> -o <owner> <attribute>\n"
     "        <base>      memory range base address\n"
     "        <length>    length of memory range in bytes, power of 2\n"
     "        <owner>     text identifier for this setting (7 char max)\n"
     "        <attribute> attribute(s) to be applied to this range:\n"
     "                        uncacheable\n"
     "                        write-combine\n"
     "                        write-through\n"
     "                        write-back\n"
     "                        write-protect",
     setfunc},
    {"clear",	
     "Clear memory range attributes\n"
     "    clear -o <owner>\n"
     "        <owner>     all ranges with this owner will be cleared\n"
     "    clear -b <base> -l <length>\n"
     "        <base>      memory range base address\n"
     "        <length>    length of memory range in bytes, power of 2\n"
     "                    Base and length must exactly match an existing range",
     clearfunc},
    {NULL,	NULL,					helpfunc}
};

int
main(int argc, char *argv[])
{
    int		i, memfd;

    if (argc < 2) {
	help(NULL);
    } else {
	if ((memfd = open(_PATH_MEM, O_RDONLY)) == -1)
	    err(1, "can't open %s", _PATH_MEM);

	for (i = 0; functions[i].cmd != NULL; i++)
	    if (!strcmp(argv[1], functions[i].cmd))
		break;
	functions[i].func(memfd, argc - 1, argv + 1);
	close(memfd);
    }
    return(0);
}

static struct mem_range_desc *
mrgetall(int memfd, int *nmr)
{
    struct mem_range_desc	*mrd;
    struct mem_range_op		mro;

    mro.mo_arg[0] = 0;
    if (ioctl(memfd, MEMRANGE_GET, &mro))
	err(1, "can't size range descriptor array");
    
    *nmr = mro.mo_arg[0];
    mrd = malloc(*nmr * sizeof(struct mem_range_desc));
    if (mrd == NULL)
	errx(1, "can't allocate %zd bytes for %d range descriptors", 
	     *nmr * sizeof(struct mem_range_desc), *nmr);

    mro.mo_arg[0] = *nmr;
    mro.mo_desc = mrd;
    if (ioctl(memfd, MEMRANGE_GET, &mro))
	err(1, "can't fetch range descriptor array");

    return(mrd);
}


static void
listfunc(int memfd, int argc, char *argv[])
{
    struct mem_range_desc	*mrd;
    int				nd, i, j;
    int				ch;
    int				showall = 0;
    char			*owner;

    owner = NULL;
    while ((ch = getopt(argc, argv, "ao:")) != -1)
	switch(ch) {
	case 'a':
	    showall = 1;
	    break;
	case 'o':
	    owner = strdup(optarg);
	    break;
	case '?':
	default:
	    help("list");
	}

    mrd = mrgetall(memfd, &nd);
    
    for (i = 0; i < nd; i++) {
	if (!showall && !(mrd[i].mr_flags & MDF_ACTIVE))
	    continue;
	if (owner && strcmp(mrd[i].mr_owner, owner))
	    continue;
	printf("0x%" PRIx64 "/0x%" PRIx64 " %.8s ", mrd[i].mr_base, mrd[i].mr_len, 
	       mrd[i].mr_owner[0] ? mrd[i].mr_owner : "-");
	for (j = 0; attrnames[j].name != NULL; j++)
	    if (mrd[i].mr_flags & attrnames[j].val)
		printf("%s ", attrnames[j].name);
	printf("\n");
    }
    free(mrd);
    if (owner)
	free(owner);
}

static void
setfunc(int memfd, int argc, char *argv[])
{
    struct mem_range_desc	mrd;
    struct mem_range_op		mro;
    int				i;
    int				ch;
    char			*ep;

    mrd.mr_base = 0;
    mrd.mr_len = 0;
    mrd.mr_flags = 0;
    strcpy(mrd.mr_owner, "user");
    while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
	switch(ch) {
	case 'b':
	    mrd.mr_base = strtouq(optarg, &ep, 0);
	    if ((ep == optarg) || (*ep != 0))
		help("set");
	    break;
	case 'l':
	    mrd.mr_len = strtouq(optarg, &ep, 0);
	    if ((ep == optarg) || (*ep != 0))
		help("set");
	    break;
	case 'o':
	    if ((*optarg == 0) || (strlen(optarg) > 7))
		help("set");
	    strcpy(mrd.mr_owner, optarg);
	    break;
	    
	case '?':
	default:
	    help("set");
	}

    if (mrd.mr_len == 0)
	help("set");

    argc -= optind;
    argv += optind;
    
    while(argc--) {
	for (i = 0; attrnames[i].name != NULL; i++) {
	    if (!strcmp(attrnames[i].name, argv[0])) {
		if (!attrnames[i].kind & MDF_SETTABLE)
		    help("flags");
		mrd.mr_flags |= attrnames[i].val;
		break;
	    }
	}
	if (attrnames[i].name == NULL)
	    help("flags");
	argv++;
    }

    mro.mo_desc = &mrd;
    mro.mo_arg[0] = 0;
    if (ioctl(memfd, MEMRANGE_SET, &mro))
	err(1, "can't set range");
}

static void
clearfunc(int memfd, int argc, char *argv[])
{
    struct mem_range_desc	mrd, *mrdp;
    struct mem_range_op		mro;
    int				i, nd;
    int				ch;
    char			*ep, *owner;

    mrd.mr_base = 0;
    mrd.mr_len = 0;
    owner = NULL;
    while ((ch = getopt(argc, argv, "b:l:o:")) != -1)
	switch(ch) {
	case 'b':
	    mrd.mr_base = strtouq(optarg, &ep, 0);
	    if ((ep == optarg) || (*ep != 0))
		help("clear");
	    break;
	case 'l':
	    mrd.mr_len = strtouq(optarg, &ep, 0);
	    if ((ep == optarg) || (*ep != 0))
		help("clear");
	    break;
	case 'o':
	    if ((*optarg == 0) || (strlen(optarg) > 7))
		help("clear");
	    owner = strdup(optarg);
	    break;
	    
	case '?':
	default:
	    help("clear");
	}

    if (owner != NULL) {
	/* clear-by-owner */
	if ((mrd.mr_base != 0) || (mrd.mr_len != 0))
	    help("clear");

	mrdp = mrgetall(memfd, &nd);
	mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
	for (i = 0; i < nd; i++) {
	    if (!strcmp(owner, mrdp[i].mr_owner) && 
		(mrdp[i].mr_flags & MDF_ACTIVE) &&
		!(mrdp[i].mr_flags & MDF_FIXACTIVE)) {
		
		mro.mo_desc = mrdp + i;
		if (ioctl(memfd, MEMRANGE_SET, &mro))
		    warn("couldn't clear range owned by '%s'", owner);
	    }
	}
    } else if (mrd.mr_len != 0) {
	/* clear-by-base/len */
	mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
	mro.mo_desc = &mrd;
	if (ioctl(memfd, MEMRANGE_SET, &mro))
	    err(1, "couldn't clear range");
    } else {
	help("clear");
    }
}

static void
helpfunc(__unused int memfd, __unused int argc, char *argv[])
{
    help(argv[1]);
}

static void
help(const char *what)
{
    int		i;

    if (what != NULL) {
	/* find a function that matches */
	for (i = 0; functions[i].cmd != NULL; i++)
	    if (!strcmp(what, functions[i].cmd)) {
		fprintf(stderr, "%s\n", functions[i].desc);
		return;
	    }
	fprintf(stderr, "Unknown command '%s'\n", what);
    }
    
    /* print general help */
    fprintf(stderr, "Valid commands are :\n");
    for (i = 0; functions[i].cmd != NULL; i++)
	fprintf(stderr, "    %s\n", functions[i].cmd);
    fprintf(stderr, "Use help <command> for command-specific help\n");
}
OpenPOWER on IntegriCloud