aliasing-test.c 6.0 KB

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