aliasing-test.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. close(fd);
  115. perror("write");
  116. return -1;
  117. }
  118. do {
  119. rc = read(fd, buf, sizeof(buf));
  120. if (rc > 0)
  121. size += rc;
  122. } while (rc > 0);
  123. close(fd);
  124. return size;
  125. }
  126. static int scan_rom(char *path, char *file)
  127. {
  128. struct dirent **namelist;
  129. char *name, *path2;
  130. int i, n, r, rc = 0, result = 0;
  131. struct stat buf;
  132. n = scandir(path, &namelist, 0, alphasort);
  133. if (n < 0) {
  134. perror("scandir");
  135. return -1;
  136. }
  137. for (i = 0; i < n; i++) {
  138. name = namelist[i]->d_name;
  139. if (fnmatch(".", name, 0) == 0)
  140. goto skip;
  141. if (fnmatch("..", name, 0) == 0)
  142. goto skip;
  143. path2 = malloc(strlen(path) + strlen(name) + 3);
  144. strcpy(path2, path);
  145. strcat(path2, "/");
  146. strcat(path2, name);
  147. if (fnmatch(file, name, 0) == 0) {
  148. rc = read_rom(path2);
  149. /*
  150. * It's OK if the ROM is unreadable. Maybe there
  151. * is no ROM, or some other error occurred. The
  152. * important thing is that no MCA happened.
  153. */
  154. if (rc > 0)
  155. fprintf(stderr, "PASS: %s read %d bytes\n", path2, rc);
  156. else {
  157. fprintf(stderr, "PASS: %s not readable\n", path2);
  158. return rc;
  159. }
  160. } else {
  161. r = lstat(path2, &buf);
  162. if (r == 0 && S_ISDIR(buf.st_mode)) {
  163. rc = scan_rom(path2, file);
  164. if (rc < 0)
  165. return rc;
  166. }
  167. }
  168. result |= rc;
  169. free(path2);
  170. skip:
  171. free(namelist[i]);
  172. }
  173. free(namelist);
  174. return result;
  175. }
  176. int main(void)
  177. {
  178. int rc;
  179. if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)
  180. fprintf(stderr, "PASS: /dev/mem 0x0-0xa0000 is readable\n");
  181. else
  182. fprintf(stderr, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");
  183. /*
  184. * It's not safe to blindly read the VGA frame buffer. If you know
  185. * how to poke the card the right way, it should respond, but it's
  186. * not safe in general. Many machines, e.g., Intel chipsets, cover
  187. * up a non-responding card by just returning -1, but others will
  188. * report the failure as a machine check.
  189. */
  190. if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)
  191. fprintf(stderr, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");
  192. else
  193. fprintf(stderr, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");
  194. if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)
  195. fprintf(stderr, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");
  196. else
  197. fprintf(stderr, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");
  198. /*
  199. * Often you can map all the individual pieces above (0-0xA0000,
  200. * 0xA0000-0xC0000, and 0xC0000-0x100000), but can't map the whole
  201. * thing at once. This is because the individual pieces use different
  202. * attributes, and there's no single attribute supported over the
  203. * whole region.
  204. */
  205. rc = map_mem("/dev/mem", 0, 1024*1024, 0);
  206. if (rc == 0)
  207. fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 is mappable\n");
  208. else if (rc > 0)
  209. fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 not mappable\n");
  210. else
  211. fprintf(stderr, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");
  212. scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);
  213. scan_tree("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);
  214. scan_tree("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);
  215. scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 1024*1024, 0);
  216. scan_rom("/sys/devices", "rom");
  217. scan_tree("/proc/bus/pci", "??.?", 0, 0xA0000, 1);
  218. scan_tree("/proc/bus/pci", "??.?", 0xA0000, 0x20000, 0);
  219. scan_tree("/proc/bus/pci", "??.?", 0xC0000, 0x40000, 1);
  220. scan_tree("/proc/bus/pci", "??.?", 0, 1024*1024, 0);
  221. return rc;
  222. }