2 * Exercise /dev/mem mmap cases that have been troublesome in the past
4 * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
14 #include <sys/types.h>
19 #include <sys/ioctl.h>
23 #include <linux/pci.h>
27 static int map_mem(char *path
, off_t offset
, size_t length
, int touch
)
33 fd
= open(path
, O_RDWR
);
39 if (fnmatch("/proc/bus/pci/*", path
, 0) == 0) {
40 rc
= ioctl(fd
, PCIIOC_MMAP_IS_MEM
);
42 perror("PCIIOC_MMAP_IS_MEM ioctl");
45 addr
= mmap(NULL
, length
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, offset
);
46 if (addr
== MAP_FAILED
)
51 while (c
< (int *) (addr
+ length
))
55 rc
= munmap(addr
, length
);
65 static int scan_tree(char *path
, char *file
, off_t offset
, size_t length
, int touch
)
67 struct dirent
**namelist
;
69 int i
, n
, r
, rc
= 0, result
= 0;
72 n
= scandir(path
, &namelist
, 0, alphasort
);
78 for (i
= 0; i
< n
; i
++) {
79 name
= namelist
[i
]->d_name
;
81 if (fnmatch(".", name
, 0) == 0)
83 if (fnmatch("..", name
, 0) == 0)
86 path2
= malloc(strlen(path
) + strlen(name
) + 3);
91 if (fnmatch(file
, name
, 0) == 0) {
92 rc
= map_mem(path2
, offset
, length
, touch
);
94 fprintf(stderr
, "PASS: %s 0x%lx-0x%lx is %s\n", path2
, offset
, offset
+ length
, touch
? "readable" : "mappable");
96 fprintf(stderr
, "PASS: %s 0x%lx-0x%lx not mappable\n", path2
, offset
, offset
+ length
);
98 fprintf(stderr
, "FAIL: %s 0x%lx-0x%lx not accessible\n", path2
, offset
, offset
+ length
);
102 r
= lstat(path2
, &buf
);
103 if (r
== 0 && S_ISDIR(buf
.st_mode
)) {
104 rc
= scan_tree(path2
, file
, offset
, length
, touch
);
122 static int read_rom(char *path
)
127 fd
= open(path
, O_RDWR
);
133 rc
= write(fd
, "1", 2);
141 rc
= read(fd
, buf
, sizeof(buf
));
150 static int scan_rom(char *path
, char *file
)
152 struct dirent
**namelist
;
154 int i
, n
, r
, rc
= 0, result
= 0;
157 n
= scandir(path
, &namelist
, 0, alphasort
);
163 for (i
= 0; i
< n
; i
++) {
164 name
= namelist
[i
]->d_name
;
166 if (fnmatch(".", name
, 0) == 0)
168 if (fnmatch("..", name
, 0) == 0)
171 path2
= malloc(strlen(path
) + strlen(name
) + 3);
176 if (fnmatch(file
, name
, 0) == 0) {
177 rc
= read_rom(path2
);
180 * It's OK if the ROM is unreadable. Maybe there
181 * is no ROM, or some other error occurred. The
182 * important thing is that no MCA happened.
185 fprintf(stderr
, "PASS: %s read %d bytes\n", path2
, rc
);
187 fprintf(stderr
, "PASS: %s not readable\n", path2
);
191 r
= lstat(path2
, &buf
);
192 if (r
== 0 && S_ISDIR(buf
.st_mode
)) {
193 rc
= scan_rom(path2
, file
);
213 if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)
214 fprintf(stderr
, "PASS: /dev/mem 0x0-0xa0000 is readable\n");
216 fprintf(stderr
, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");
219 * It's not safe to blindly read the VGA frame buffer. If you know
220 * how to poke the card the right way, it should respond, but it's
221 * not safe in general. Many machines, e.g., Intel chipsets, cover
222 * up a non-responding card by just returning -1, but others will
223 * report the failure as a machine check.
225 if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)
226 fprintf(stderr
, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");
228 fprintf(stderr
, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");
230 if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)
231 fprintf(stderr
, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");
233 fprintf(stderr
, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");
236 * Often you can map all the individual pieces above (0-0xA0000,
237 * 0xA0000-0xC0000, and 0xC0000-0x100000), but can't map the whole
238 * thing at once. This is because the individual pieces use different
239 * attributes, and there's no single attribute supported over the
242 rc
= map_mem("/dev/mem", 0, 1024*1024, 0);
244 fprintf(stderr
, "PASS: /dev/mem 0x0-0x100000 is mappable\n");
246 fprintf(stderr
, "PASS: /dev/mem 0x0-0x100000 not mappable\n");
248 fprintf(stderr
, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");
250 scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);
251 scan_tree("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);
252 scan_tree("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);
253 scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 1024*1024, 0);
255 scan_rom("/sys/devices", "rom");
257 scan_tree("/proc/bus/pci", "??.?", 0, 0xA0000, 1);
258 scan_tree("/proc/bus/pci", "??.?", 0xA0000, 0x20000, 0);
259 scan_tree("/proc/bus/pci", "??.?", 0xC0000, 0x40000, 1);
260 scan_tree("/proc/bus/pci", "??.?", 0, 1024*1024, 0);