aliasing-test.c 5.9 KB

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