mmap-test.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD - The Framework for UserSpace Devices - Example program
  32. *
  33. * This example creates a a mmap-able buffer.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <ctype.h>
  40. #include <unistd.h>
  41. #include <sys/mman.h>
  42. #include "fusd.h"
  43. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  44. #define BUFFER_SIZE 4096
  45. static char *mmap_buffer;
  46. static void hexdump(void *ptr_r, int size)
  47. {
  48. int J, I;
  49. unsigned char *ptr = ptr_r;
  50. unsigned long Addr = 0;
  51. if (ptr == NULL) {
  52. puts("NULL POINTER");
  53. puts("-------------------------------------------------------------------------------------");
  54. return;
  55. }
  56. while (Addr <= size) {
  57. for (J = 0; J < 2; J++) {
  58. printf("%08p: ", Addr + ptr);
  59. for (I = 0; I < 16; I++, Addr++) { if (Addr <= size) { printf("%02lX ", (unsigned char) ptr[Addr]); } else { printf(" "); } }
  60. printf(" | "); Addr -= 16;
  61. for (I = 0; I < 16; I++, Addr++) { if (Addr <= size) { putchar(isprint(ptr[Addr]) ? ptr[Addr] : '.'); } else { putchar(' '); } }
  62. puts("");
  63. }
  64. }
  65. puts("-------------------------------------------------------------------------------------");
  66. }
  67. ssize_t mmaptest_read(struct fusd_file_info *file, char *user_buffer,
  68. size_t user_length, loff_t *offset)
  69. {
  70. int len;
  71. if (*offset > BUFFER_SIZE) {
  72. return 0;
  73. }
  74. len = MIN(user_length + (*offset), BUFFER_SIZE);
  75. memcpy(user_buffer, mmap_buffer + (*offset), len);
  76. *offset += len;
  77. return len;
  78. }
  79. int tester_mmap(struct fusd_file_info *file, int offset, size_t length, int prot, int flags,
  80. void **addr, size_t *out_length)
  81. {
  82. printf("Got a mmap request from PID:%d [offset=%d, size=%d, prot=%X, flags=%X, addr=%p]\n",
  83. file->pid, offset, length, prot, flags, *addr);
  84. if (length <= BUFFER_SIZE) {
  85. *addr = mmap_buffer;
  86. *out_length = BUFFER_SIZE;
  87. return 0;
  88. }
  89. return -1;
  90. }
  91. int do_open(struct fusd_file_info *file)
  92. {
  93. /* opens and closes always succeed */
  94. return 0;
  95. }
  96. int do_close(struct fusd_file_info *file)
  97. {
  98. /* Show content of the buffer */
  99. hexdump(mmap_buffer, 512);
  100. return 0;
  101. }
  102. struct fusd_file_operations drums_fops = {
  103. open: do_open,
  104. read: mmaptest_read,
  105. mmap: tester_mmap,
  106. close: do_close
  107. };
  108. int main(int argc, char *argv[])
  109. {
  110. int i;
  111. mmap_buffer = (char *)mmap(NULL, BUFFER_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  112. if (fusd_register("mmap-tester", "mmaptest", "mmap-tester", 0666, NULL, &drums_fops) < 0) {
  113. fprintf(stderr, "mmap-tester register failed: %m\n");
  114. return -1;
  115. }
  116. memset(mmap_buffer, 0xAA, BUFFER_SIZE);
  117. fprintf(stderr, "calling fusd_run...\n");
  118. fusd_run();
  119. return 0;
  120. }