1 // SPDX-License-Identifier: GPL-2.0-only
3 * Exercise /dev/mem mmap cases that have been troublesome in the past
5 * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
6 * Bjorn Helgaas <bjorn.helgaas@hp.com>
11 #include <sys/types.h>
16 #include <sys/ioctl.h>
20 #include <linux/pci.h>
24 static int map_mem(char *path, off_t offset, size_t length, int touch)
30 fd = open(path, O_RDWR);
36 if (fnmatch("/proc/bus/pci/*", path, 0) == 0) {
37 rc = ioctl(fd, PCIIOC_MMAP_IS_MEM);
39 perror("PCIIOC_MMAP_IS_MEM ioctl");
42 addr = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
43 if (addr == MAP_FAILED)
48 while (c < (int *) (addr + length))
52 rc = munmap(addr, length);
62 static int scan_tree(char *path, char *file, off_t offset, size_t length, int touch)
64 struct dirent **namelist;
66 int i, n, r, rc = 0, result = 0;
69 n = scandir(path, &namelist, 0, alphasort);
75 for (i = 0; i < n; i++) {
76 name = namelist[i]->d_name;
78 if (fnmatch(".", name, 0) == 0)
80 if (fnmatch("..", name, 0) == 0)
83 path2 = malloc(strlen(path) + strlen(name) + 3);
88 if (fnmatch(file, name, 0) == 0) {
89 rc = map_mem(path2, offset, length, touch);
91 fprintf(stderr, "PASS: %s 0x%lx-0x%lx is %s\n", path2, offset, offset + length, touch ? "readable" : "mappable");
93 fprintf(stderr, "PASS: %s 0x%lx-0x%lx not mappable\n", path2, offset, offset + length);
95 fprintf(stderr, "FAIL: %s 0x%lx-0x%lx not accessible\n", path2, offset, offset + length);
99 r = lstat(path2, &buf);
100 if (r == 0 && S_ISDIR(buf.st_mode)) {
101 rc = scan_tree(path2, file, offset, length, touch);
119 static int read_rom(char *path)
124 fd = open(path, O_RDWR);
130 rc = write(fd, "1", 2);
138 rc = read(fd, buf, sizeof(buf));
147 static int scan_rom(char *path, char *file)
149 struct dirent **namelist;
151 int i, n, r, rc = 0, result = 0;
154 n = scandir(path, &namelist, 0, alphasort);
160 for (i = 0; i < n; i++) {
161 name = namelist[i]->d_name;
163 if (fnmatch(".", name, 0) == 0)
165 if (fnmatch("..", name, 0) == 0)
168 path2 = malloc(strlen(path) + strlen(name) + 3);
173 if (fnmatch(file, name, 0) == 0) {
174 rc = read_rom(path2);
177 * It's OK if the ROM is unreadable. Maybe there
178 * is no ROM, or some other error occurred. The
179 * important thing is that no MCA happened.
182 fprintf(stderr, "PASS: %s read %d bytes\n", path2, rc);
184 fprintf(stderr, "PASS: %s not readable\n", path2);
188 r = lstat(path2, &buf);
189 if (r == 0 && S_ISDIR(buf.st_mode)) {
190 rc = scan_rom(path2, file);
210 if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)
211 fprintf(stderr, "PASS: /dev/mem 0x0-0xa0000 is readable\n");
213 fprintf(stderr, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");
216 * It's not safe to blindly read the VGA frame buffer. If you know
217 * how to poke the card the right way, it should respond, but it's
218 * not safe in general. Many machines, e.g., Intel chipsets, cover
219 * up a non-responding card by just returning -1, but others will
220 * report the failure as a machine check.
222 if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)
223 fprintf(stderr, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");
225 fprintf(stderr, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");
227 if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)
228 fprintf(stderr, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");
230 fprintf(stderr, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");
233 * Often you can map all the individual pieces above (0-0xA0000,
234 * 0xA0000-0xC0000, and 0xC0000-0x100000), but can't map the whole
235 * thing at once. This is because the individual pieces use different
236 * attributes, and there's no single attribute supported over the
239 rc = map_mem("/dev/mem", 0, 1024*1024, 0);
241 fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 is mappable\n");
243 fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 not mappable\n");
245 fprintf(stderr, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");
247 scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);
248 scan_tree("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);
249 scan_tree("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);
250 scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 1024*1024, 0);
252 scan_rom("/sys/devices", "rom");
254 scan_tree("/proc/bus/pci", "??.?", 0, 0xA0000, 1);
255 scan_tree("/proc/bus/pci", "??.?", 0xA0000, 0x20000, 0);
256 scan_tree("/proc/bus/pci", "??.?", 0xC0000, 0x40000, 1);
257 scan_tree("/proc/bus/pci", "??.?", 0, 1024*1024, 0);