osunixmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /******************************************************************************
  2. *
  3. * Module Name: osunixmap - Unix OSL for file mappings
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include "acpidump.h"
  43. #include <unistd.h>
  44. #include <sys/mman.h>
  45. #ifdef _free_BSD
  46. #include <sys/param.h>
  47. #endif
  48. #define _COMPONENT ACPI_OS_SERVICES
  49. ACPI_MODULE_NAME("osunixmap")
  50. #ifndef O_BINARY
  51. #define O_BINARY 0
  52. #endif
  53. #if defined(_dragon_fly) || defined(_free_BSD) || defined(_QNX)
  54. #define MMAP_FLAGS MAP_SHARED
  55. #else
  56. #define MMAP_FLAGS MAP_PRIVATE
  57. #endif
  58. #define SYSTEM_MEMORY "/dev/mem"
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_os_get_page_size
  62. *
  63. * PARAMETERS: None
  64. *
  65. * RETURN: Page size of the platform.
  66. *
  67. * DESCRIPTION: Obtain page size of the platform.
  68. *
  69. ******************************************************************************/
  70. static acpi_size acpi_os_get_page_size(void)
  71. {
  72. #ifdef PAGE_SIZE
  73. return PAGE_SIZE;
  74. #else
  75. return sysconf(_SC_PAGESIZE);
  76. #endif
  77. }
  78. /******************************************************************************
  79. *
  80. * FUNCTION: acpi_os_map_memory
  81. *
  82. * PARAMETERS: where - Physical address of memory to be mapped
  83. * length - How much memory to map
  84. *
  85. * RETURN: Pointer to mapped memory. Null on error.
  86. *
  87. * DESCRIPTION: Map physical memory into local address space.
  88. *
  89. *****************************************************************************/
  90. void *acpi_os_map_memory(acpi_physical_address where, acpi_size length)
  91. {
  92. u8 *mapped_memory;
  93. acpi_physical_address offset;
  94. acpi_size page_size;
  95. int fd;
  96. fd = open(SYSTEM_MEMORY, O_RDONLY | O_BINARY);
  97. if (fd < 0) {
  98. fprintf(stderr, "Cannot open %s\n", SYSTEM_MEMORY);
  99. return (NULL);
  100. }
  101. /* Align the offset to use mmap */
  102. page_size = acpi_os_get_page_size();
  103. offset = where % page_size;
  104. /* Map the table header to get the length of the full table */
  105. mapped_memory = mmap(NULL, (length + offset), PROT_READ, MMAP_FLAGS,
  106. fd, (where - offset));
  107. if (mapped_memory == MAP_FAILED) {
  108. fprintf(stderr, "Cannot map %s\n", SYSTEM_MEMORY);
  109. close(fd);
  110. return (NULL);
  111. }
  112. close(fd);
  113. return (ACPI_CAST8(mapped_memory + offset));
  114. }
  115. /******************************************************************************
  116. *
  117. * FUNCTION: acpi_os_unmap_memory
  118. *
  119. * PARAMETERS: where - Logical address of memory to be unmapped
  120. * length - How much memory to unmap
  121. *
  122. * RETURN: None.
  123. *
  124. * DESCRIPTION: Delete a previously created mapping. Where and Length must
  125. * correspond to a previous mapping exactly.
  126. *
  127. *****************************************************************************/
  128. void acpi_os_unmap_memory(void *where, acpi_size length)
  129. {
  130. acpi_physical_address offset;
  131. acpi_size page_size;
  132. page_size = acpi_os_get_page_size();
  133. offset = ACPI_TO_INTEGER(where) % page_size;
  134. munmap((u8 *)where - offset, (length + offset));
  135. }