aliasing-test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Exercise /dev/mem mmap cases that have been troublesome in the past
  4. *
  5. * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
  6. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <dirent.h>
  12. #include <fcntl.h>
  13. #include <fnmatch.h>
  14. #include <string.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/mman.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <linux/pci.h>
  20. int sum;
  21. static int map_mem(char *path, off_t offset, size_t length, int touch)
  22. {
  23. int fd, rc;
  24. void *addr;
  25. int *c;
  26. fd = open(path, O_RDWR);
  27. if (fd == -1) {
  28. perror(path);
  29. return -1;
  30. }
  31. if (fnmatch("/proc/bus/pci/*", path, 0) == 0) {
  32. rc = ioctl(fd, PCIIOC_MMAP_IS_MEM);
  33. if (rc == -1)
  34. perror("PCIIOC_MMAP_IS_MEM ioctl");
  35. }
  36. addr = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
  37. if (addr == MAP_FAILED)
  38. return 1;
  39. if (touch) {
  40. c = (int *) addr;
  41. while (c < (int *) (addr + length))
  42. sum += *c++;
  43. }
  44. rc = munmap(addr, length);
  45. if (rc == -1) {
  46. perror("munmap");
  47. return -1;
  48. }
  49. close(fd);
  50. return 0;
  51. }
  52. static int scan_tree(char *path, char *file, off_t offset, size_t length, int touch)
  53. {
  54. struct dirent **namelist;
  55. char *name, *path2;
  56. int i, n, r, rc = 0, result = 0;
  57. struct stat buf;
  58. n = scandir(path, &namelist, 0, alphasort);
  59. if (n < 0) {
  60. perror("scandir");
  61. return -1;
  62. }
  63. for (i = 0; i < n; i++) {
  64. name = namelist[i]->d_name;
  65. if (fnmatch(".", name, 0) == 0)
  66. goto skip;
  67. if (fnmatch("..", name, 0) == 0)
  68. goto skip;
  69. path2 = malloc(strlen(path) + strlen(name) + 3);
  70. strcpy(path2, path);
  71. strcat(path2, "/");
  72. strcat(path2, name);
  73. if (fnmatch(file, name, 0) == 0) {
  74. rc = map_mem(path2, offset, length, touch);
  75. if (rc == 0)
  76. fprintf(stderr, "PASS: %s 0x%lx-0x%lx is %s\n", path2, offset, offset + length, touch ? "readable" : "mappable");
  77. else if (rc > 0)
  78. fprintf(stderr, "PASS: %s 0x%lx-0x%lx not mappable\n", path2, offset, offset + length);
  79. else {
  80. fprintf(stderr, "FAIL: %s 0x%lx-0x%lx not accessible\n", path2, offset, offset + length);
  81. return rc;
  82. }
  83. } else {
  84. r = lstat(path2, &buf);
  85. if (r == 0 && S_ISDIR(buf.st_mode)) {
  86. rc = scan_tree(path2, file, offset, length, touch);
  87. if (rc < 0)
  88. return rc;
  89. }
  90. }
  91. result |= rc;
  92. free(path2);
  93. skip:
  94. free(namelist[i]);
  95. }
  96. free(namelist);
  97. return result;
  98. }
  99. char buf[1024];
  100. static int read_rom(char *path)
  101. {
  102. int fd, rc;
  103. size_t size = 0;
  104. fd = open(path, O_RDWR);
  105. if (fd == -1) {
  106. perror(path);
  107. return -1;
  108. }
  109. rc = write(fd, "1", 2);
  110. if (rc <= 0) {
  111. close(fd);
  112. perror("write");
  113. return -1;
  114. }
  115. do {
  116. rc = read(fd, buf, sizeof(buf));
  117. if (rc > 0)
  118. size += rc;
  119. } while (rc > 0);
  120. close(fd);
  121. return size;
  122. }
  123. static int scan_rom(char *path, char *file)
  124. {
  125. struct dirent **namelist;
  126. char *name, *path2;
  127. int i, n, r, rc = 0, result = 0;
  128. struct stat buf;
  129. n = scandir(path, &namelist, 0, alphasort);
  130. if (n < 0) {
  131. perror("scandir");
  132. return -1;
  133. }
  134. for (i = 0; i < n; i++) {
  135. name = namelist[i]->d_name;
  136. if (fnmatch(".", name, 0) == 0)
  137. goto skip;
  138. if (fnmatch("..", name, 0) == 0)
  139. goto skip;
  140. path2 = malloc(strlen(path) + strlen(name) + 3);
  141. strcpy(path2, path);
  142. strcat(path2, "/");
  143. strcat(path2, name);
  144. if (fnmatch(file, name, 0) == 0) {
  145. rc = read_rom(path2);
  146. /*
  147. * It's OK if the ROM is unreadable. Maybe there
  148. * is no ROM, or some other error occurred. The
  149. * important thing is that no MCA happened.
  150. */
  151. if (rc > 0)
  152. fprintf(stderr, "PASS: %s read %d bytes\n", path2, rc);
  153. else {
  154. fprintf(stderr, "PASS: %s not readable\n", path2);
  155. return rc;
  156. }
  157. } else {
  158. r = lstat(path2, &buf);
  159. if (r == 0 && S_ISDIR(buf.st_mode)) {
  160. rc = scan_rom(path2, file);
  161. if (rc < 0)
  162. return rc;
  163. }
  164. }
  165. result |= rc;
  166. free(path2);
  167. skip:
  168. free(namelist[i]);
  169. }
  170. free(namelist);
  171. return result;
  172. }
  173. int main(void)
  174. {
  175. int rc;
  176. if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)
  177. fprintf(stderr, "PASS: /dev/mem 0x0-0xa0000 is readable\n");
  178. else
  179. fprintf(stderr, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");
  180. /*
  181. * It's not safe to blindly read the VGA frame buffer. If you know
  182. * how to poke the card the right way, it should respond, but it's
  183. * not safe in general. Many machines, e.g., Intel chipsets, cover
  184. * up a non-responding card by just returning -1, but others will
  185. * report the failure as a machine check.
  186. */
  187. if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)
  188. fprintf(stderr, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");
  189. else
  190. fprintf(stderr, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");
  191. if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)
  192. fprintf(stderr, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");
  193. else
  194. fprintf(stderr, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");
  195. /*
  196. * Often you can map all the individual pieces above (0-0xA0000,
  197. * 0xA0000-0xC0000, and 0xC0000-0x100000), but can't map the whole
  198. * thing at once. This is because the individual pieces use different
  199. * attributes, and there's no single attribute supported over the
  200. * whole region.
  201. */
  202. rc = map_mem("/dev/mem", 0, 1024*1024, 0);
  203. if (rc == 0)
  204. fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 is mappable\n");
  205. else if (rc > 0)
  206. fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 not mappable\n");
  207. else
  208. fprintf(stderr, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");
  209. scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);
  210. scan_tree("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);
  211. scan_tree("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);
  212. scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 1024*1024, 0);
  213. scan_rom("/sys/devices", "rom");
  214. scan_tree("/proc/bus/pci", "??.?", 0, 0xA0000, 1);
  215. scan_tree("/proc/bus/pci", "??.?", 0xA0000, 0x20000, 0);
  216. scan_tree("/proc/bus/pci", "??.?", 0xC0000, 0x40000, 1);
  217. scan_tree("/proc/bus/pci", "??.?", 0, 1024*1024, 0);
  218. return rc;
  219. }