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