11.1Schristos/******************************************************************************
21.1Schristos *
31.1Schristos * Module Name: osunixmap - Unix OSL for file mappings
41.1Schristos *
51.1Schristos *****************************************************************************/
61.1Schristos
71.1.1.14Schristos/******************************************************************************
81.1.1.14Schristos *
91.1.1.14Schristos * 1. Copyright Notice
101.1.1.14Schristos *
111.1.1.15Schristos * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.
121.1Schristos * All rights reserved.
131.1Schristos *
141.1.1.14Schristos * 2. License
151.1.1.14Schristos *
161.1.1.14Schristos * 2.1. This is your license from Intel Corp. under its intellectual property
171.1.1.14Schristos * rights. You may have additional license terms from the party that provided
181.1.1.14Schristos * you this software, covering your right to use that party's intellectual
191.1.1.14Schristos * property rights.
201.1.1.14Schristos *
211.1.1.14Schristos * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
221.1.1.14Schristos * copy of the source code appearing in this file ("Covered Code") an
231.1.1.14Schristos * irrevocable, perpetual, worldwide license under Intel's copyrights in the
241.1.1.14Schristos * base code distributed originally by Intel ("Original Intel Code") to copy,
251.1.1.14Schristos * make derivatives, distribute, use and display any portion of the Covered
261.1.1.14Schristos * Code in any form, with the right to sublicense such rights; and
271.1.1.14Schristos *
281.1.1.14Schristos * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
291.1.1.14Schristos * license (with the right to sublicense), under only those claims of Intel
301.1.1.14Schristos * patents that are infringed by the Original Intel Code, to make, use, sell,
311.1.1.14Schristos * offer to sell, and import the Covered Code and derivative works thereof
321.1.1.14Schristos * solely to the minimum extent necessary to exercise the above copyright
331.1.1.14Schristos * license, and in no event shall the patent license extend to any additions
341.1.1.14Schristos * to or modifications of the Original Intel Code. No other license or right
351.1.1.14Schristos * is granted directly or by implication, estoppel or otherwise;
361.1.1.14Schristos *
371.1.1.14Schristos * The above copyright and patent license is granted only if the following
381.1.1.14Schristos * conditions are met:
391.1.1.14Schristos *
401.1.1.14Schristos * 3. Conditions
411.1.1.14Schristos *
421.1.1.14Schristos * 3.1. Redistribution of Source with Rights to Further Distribute Source.
431.1.1.14Schristos * Redistribution of source code of any substantial portion of the Covered
441.1.1.14Schristos * Code or modification with rights to further distribute source must include
451.1.1.14Schristos * the above Copyright Notice, the above License, this list of Conditions,
461.1.1.14Schristos * and the following Disclaimer and Export Compliance provision. In addition,
471.1.1.14Schristos * Licensee must cause all Covered Code to which Licensee contributes to
481.1.1.14Schristos * contain a file documenting the changes Licensee made to create that Covered
491.1.1.14Schristos * Code and the date of any change. Licensee must include in that file the
501.1.1.14Schristos * documentation of any changes made by any predecessor Licensee. Licensee
511.1.1.14Schristos * must include a prominent statement that the modification is derived,
521.1.1.14Schristos * directly or indirectly, from Original Intel Code.
531.1.1.14Schristos *
541.1.1.14Schristos * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
551.1.1.14Schristos * Redistribution of source code of any substantial portion of the Covered
561.1.1.14Schristos * Code or modification without rights to further distribute source must
571.1.1.14Schristos * include the following Disclaimer and Export Compliance provision in the
581.1.1.14Schristos * documentation and/or other materials provided with distribution. In
591.1.1.14Schristos * addition, Licensee may not authorize further sublicense of source of any
601.1.1.14Schristos * portion of the Covered Code, and must include terms to the effect that the
611.1.1.14Schristos * license from Licensee to its licensee is limited to the intellectual
621.1.1.14Schristos * property embodied in the software Licensee provides to its licensee, and
631.1.1.14Schristos * not to intellectual property embodied in modifications its licensee may
641.1.1.14Schristos * make.
651.1.1.14Schristos *
661.1.1.14Schristos * 3.3. Redistribution of Executable. Redistribution in executable form of any
671.1.1.14Schristos * substantial portion of the Covered Code or modification must reproduce the
681.1.1.14Schristos * above Copyright Notice, and the following Disclaimer and Export Compliance
691.1.1.14Schristos * provision in the documentation and/or other materials provided with the
701.1.1.14Schristos * distribution.
711.1.1.14Schristos *
721.1.1.14Schristos * 3.4. Intel retains all right, title, and interest in and to the Original
731.1.1.14Schristos * Intel Code.
741.1.1.14Schristos *
751.1.1.14Schristos * 3.5. Neither the name Intel nor any other trademark owned or controlled by
761.1.1.14Schristos * Intel shall be used in advertising or otherwise to promote the sale, use or
771.1.1.14Schristos * other dealings in products derived from or relating to the Covered Code
781.1.1.14Schristos * without prior written authorization from Intel.
791.1.1.14Schristos *
801.1.1.14Schristos * 4. Disclaimer and Export Compliance
811.1.1.14Schristos *
821.1.1.14Schristos * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
831.1.1.14Schristos * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
841.1.1.14Schristos * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
851.1.1.14Schristos * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
861.1.1.14Schristos * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
871.1.1.14Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
881.1.1.14Schristos * PARTICULAR PURPOSE.
891.1.1.14Schristos *
901.1.1.14Schristos * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
911.1.1.14Schristos * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
921.1.1.14Schristos * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
931.1.1.14Schristos * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
941.1.1.14Schristos * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
951.1.1.14Schristos * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
961.1.1.14Schristos * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
971.1.1.14Schristos * LIMITED REMEDY.
981.1.1.14Schristos *
991.1.1.14Schristos * 4.3. Licensee shall not export, either directly or indirectly, any of this
1001.1.1.14Schristos * software or system incorporating such software without first obtaining any
1011.1.1.14Schristos * required license or other approval from the U. S. Department of Commerce or
1021.1.1.14Schristos * any other agency or department of the United States Government. In the
1031.1.1.14Schristos * event Licensee exports any such software from the United States or
1041.1.1.14Schristos * re-exports any such software from a foreign destination, Licensee shall
1051.1.1.14Schristos * ensure that the distribution and export/re-export of the software is in
1061.1.1.14Schristos * compliance with all laws, regulations, orders, or other restrictions of the
1071.1.1.14Schristos * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1081.1.1.14Schristos * any of its subsidiaries will export/re-export any technical data, process,
1091.1.1.14Schristos * software, or service, directly or indirectly, to any country for which the
1101.1.1.14Schristos * United States government or any agency thereof requires an export license,
1111.1.1.14Schristos * other governmental approval, or letter of assurance, without first obtaining
1121.1.1.14Schristos * such license, approval or letter.
1131.1.1.14Schristos *
1141.1.1.14Schristos *****************************************************************************
1151.1.1.14Schristos *
1161.1.1.14Schristos * Alternatively, you may choose to be licensed under the terms of the
1171.1.1.14Schristos * following license:
1181.1.1.14Schristos *
1191.1Schristos * Redistribution and use in source and binary forms, with or without
1201.1Schristos * modification, are permitted provided that the following conditions
1211.1Schristos * are met:
1221.1Schristos * 1. Redistributions of source code must retain the above copyright
1231.1Schristos *    notice, this list of conditions, and the following disclaimer,
1241.1Schristos *    without modification.
1251.1Schristos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1261.1Schristos *    substantially similar to the "NO WARRANTY" disclaimer below
1271.1Schristos *    ("Disclaimer") and any redistribution must be conditioned upon
1281.1Schristos *    including a substantially similar Disclaimer requirement for further
1291.1Schristos *    binary redistribution.
1301.1Schristos * 3. Neither the names of the above-listed copyright holders nor the names
1311.1Schristos *    of any contributors may be used to endorse or promote products derived
1321.1Schristos *    from this software without specific prior written permission.
1331.1Schristos *
1341.1Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1351.1Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1361.1.1.11Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1371.1Schristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1381.1.1.14Schristos * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1391.1.1.14Schristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1401.1.1.14Schristos * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1411.1.1.14Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1421.1.1.14Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1431.1.1.14Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1441.1.1.14Schristos * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1451.1.1.14Schristos *
1461.1.1.14Schristos * Alternatively, you may choose to be licensed under the terms of the
1471.1.1.14Schristos * GNU General Public License ("GPL") version 2 as published by the Free
1481.1.1.14Schristos * Software Foundation.
1491.1.1.14Schristos *
1501.1.1.14Schristos *****************************************************************************/
1511.1Schristos
1521.1Schristos#include "acpidump.h"
1531.1Schristos#include <unistd.h>
1541.1Schristos#include <sys/mman.h>
1551.1Schristos#ifdef _FreeBSD
1561.1Schristos#include <sys/param.h>
1571.1Schristos#endif
1581.1Schristos
1591.1Schristos#define _COMPONENT          ACPI_OS_SERVICES
1601.1Schristos        ACPI_MODULE_NAME    ("osunixmap")
1611.1Schristos
1621.1Schristos
1631.1Schristos#ifndef O_BINARY
1641.1Schristos#define O_BINARY 0
1651.1Schristos#endif
1661.1Schristos
1671.1.1.6Schristos#if defined(_DragonFly) || defined(_FreeBSD) || defined(_QNX)
1681.1Schristos#define MMAP_FLAGS          MAP_SHARED
1691.1Schristos#else
1701.1Schristos#define MMAP_FLAGS          MAP_PRIVATE
1711.1Schristos#endif
1721.1Schristos
1731.1Schristos#define SYSTEM_MEMORY       "/dev/mem"
1741.1Schristos
1751.1Schristos
1761.1Schristos/*******************************************************************************
1771.1Schristos *
1781.1Schristos * FUNCTION:    AcpiOsGetPageSize
1791.1Schristos *
1801.1Schristos * PARAMETERS:  None
1811.1Schristos *
1821.1Schristos * RETURN:      Page size of the platform.
1831.1Schristos *
1841.1Schristos * DESCRIPTION: Obtain page size of the platform.
1851.1Schristos *
1861.1Schristos ******************************************************************************/
1871.1Schristos
1881.1Schristosstatic ACPI_SIZE
1891.1SchristosAcpiOsGetPageSize (
1901.1Schristos    void)
1911.1Schristos{
1921.1Schristos
1931.1Schristos#ifdef PAGE_SIZE
1941.1Schristos    return PAGE_SIZE;
1951.1Schristos#else
1961.1Schristos    return sysconf (_SC_PAGESIZE);
1971.1Schristos#endif
1981.1Schristos}
1991.1Schristos
2001.1Schristos
2011.1Schristos/******************************************************************************
2021.1Schristos *
2031.1Schristos * FUNCTION:    AcpiOsMapMemory
2041.1Schristos *
2051.1Schristos * PARAMETERS:  Where               - Physical address of memory to be mapped
2061.1Schristos *              Length              - How much memory to map
2071.1Schristos *
2081.1Schristos * RETURN:      Pointer to mapped memory. Null on error.
2091.1Schristos *
2101.1Schristos * DESCRIPTION: Map physical memory into local address space.
2111.1Schristos *
2121.1Schristos *****************************************************************************/
2131.1Schristos
2141.1Schristosvoid *
2151.1SchristosAcpiOsMapMemory (
2161.1Schristos    ACPI_PHYSICAL_ADDRESS   Where,
2171.1Schristos    ACPI_SIZE               Length)
2181.1Schristos{
2191.1Schristos    UINT8                   *MappedMemory;
2201.1Schristos    ACPI_PHYSICAL_ADDRESS   Offset;
2211.1Schristos    ACPI_SIZE               PageSize;
2221.1Schristos    int                     fd;
2231.1Schristos
2241.1Schristos
2251.1Schristos    fd = open (SYSTEM_MEMORY, O_RDONLY | O_BINARY);
2261.1Schristos    if (fd < 0)
2271.1Schristos    {
2281.1Schristos        fprintf (stderr, "Cannot open %s\n", SYSTEM_MEMORY);
2291.1Schristos        return (NULL);
2301.1Schristos    }
2311.1Schristos
2321.1Schristos    /* Align the offset to use mmap */
2331.1Schristos
2341.1Schristos    PageSize = AcpiOsGetPageSize ();
2351.1Schristos    Offset = Where % PageSize;
2361.1Schristos
2371.1Schristos    /* Map the table header to get the length of the full table */
2381.1Schristos
2391.1Schristos    MappedMemory = mmap (NULL, (Length + Offset), PROT_READ, MMAP_FLAGS,
2401.1Schristos        fd, (Where - Offset));
2411.1Schristos    if (MappedMemory == MAP_FAILED)
2421.1Schristos    {
2431.1Schristos        fprintf (stderr, "Cannot map %s\n", SYSTEM_MEMORY);
2441.1Schristos        close (fd);
2451.1Schristos        return (NULL);
2461.1Schristos    }
2471.1Schristos
2481.1Schristos    close (fd);
2491.1Schristos    return (ACPI_CAST8 (MappedMemory + Offset));
2501.1Schristos}
2511.1Schristos
2521.1Schristos
2531.1Schristos/******************************************************************************
2541.1Schristos *
2551.1Schristos * FUNCTION:    AcpiOsUnmapMemory
2561.1Schristos *
2571.1Schristos * PARAMETERS:  Where               - Logical address of memory to be unmapped
2581.1Schristos *              Length              - How much memory to unmap
2591.1Schristos *
2601.1Schristos * RETURN:      None.
2611.1Schristos *
2621.1Schristos * DESCRIPTION: Delete a previously created mapping. Where and Length must
2631.1Schristos *              correspond to a previous mapping exactly.
2641.1Schristos *
2651.1Schristos *****************************************************************************/
2661.1Schristos
2671.1Schristosvoid
2681.1SchristosAcpiOsUnmapMemory (
2691.1Schristos    void                    *Where,
2701.1Schristos    ACPI_SIZE               Length)
2711.1Schristos{
2721.1Schristos    ACPI_PHYSICAL_ADDRESS   Offset;
2731.1Schristos    ACPI_SIZE               PageSize;
2741.1Schristos
2751.1Schristos
2761.1Schristos    PageSize = AcpiOsGetPageSize ();
2771.1.1.3Schristos    Offset = ACPI_TO_INTEGER (Where) % PageSize;
2781.1Schristos    munmap ((UINT8 *) Where - Offset, (Length + Offset));
2791.1Schristos}
280