Path: blob/main/sys/contrib/dev/acpica/components/executer/exutils.c
48521 views
/******************************************************************************1*2* Module Name: exutils - interpreter/scanner utilities3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151/*152* DEFINE_AML_GLOBALS is tested in amlcode.h153* to determine whether certain global names should be "defined" or only154* "declared" in the current compilation. This enhances maintainability155* by enabling a single header file to embody all knowledge of the names156* in question.157*158* Exactly one module of any executable should #define DEFINE_GLOBALS159* before #including the header files which use this convention. The160* names in question will be defined and initialized in that module,161* and declared as extern in all other modules which #include those162* header files.163*/164165#define DEFINE_AML_GLOBALS166167#include <contrib/dev/acpica/include/acpi.h>168#include <contrib/dev/acpica/include/accommon.h>169#include <contrib/dev/acpica/include/acinterp.h>170#include <contrib/dev/acpica/include/amlcode.h>171172#define _COMPONENT ACPI_EXECUTER173ACPI_MODULE_NAME ("exutils")174175/* Local prototypes */176177static UINT32178AcpiExDigitsNeeded (179UINT64 Value,180UINT32 Base);181182183/*******************************************************************************184*185* FUNCTION: AcpiExEnterInterpreter186*187* PARAMETERS: None188*189* RETURN: None190*191* DESCRIPTION: Enter the interpreter execution region. Failure to enter192* the interpreter region is a fatal system error. Used in193* conjunction with ExitInterpreter.194*195******************************************************************************/196197void198AcpiExEnterInterpreter (199void)200{201ACPI_STATUS Status;202203204ACPI_FUNCTION_TRACE (ExEnterInterpreter);205206207Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);208if (ACPI_FAILURE (Status))209{210ACPI_ERROR ((AE_INFO, "Could not acquire AML Interpreter mutex"));211}212Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);213if (ACPI_FAILURE (Status))214{215ACPI_ERROR ((AE_INFO, "Could not acquire AML Namespace mutex"));216}217218return_VOID;219}220221222/*******************************************************************************223*224* FUNCTION: AcpiExExitInterpreter225*226* PARAMETERS: None227*228* RETURN: None229*230* DESCRIPTION: Exit the interpreter execution region. This is the top level231* routine used to exit the interpreter when all processing has232* been completed, or when the method blocks.233*234* Cases where the interpreter is unlocked internally:235* 1) Method will be blocked on a Sleep() AML opcode236* 2) Method will be blocked on an Acquire() AML opcode237* 3) Method will be blocked on a Wait() AML opcode238* 4) Method will be blocked to acquire the global lock239* 5) Method will be blocked waiting to execute a serialized control240* method that is currently executing241* 6) About to invoke a user-installed opregion handler242*243******************************************************************************/244245void246AcpiExExitInterpreter (247void)248{249ACPI_STATUS Status;250251252ACPI_FUNCTION_TRACE (ExExitInterpreter);253254255Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);256if (ACPI_FAILURE (Status))257{258ACPI_ERROR ((AE_INFO, "Could not release AML Namespace mutex"));259}260Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);261if (ACPI_FAILURE (Status))262{263ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex"));264}265266return_VOID;267}268269270/*******************************************************************************271*272* FUNCTION: AcpiExTruncateFor32bitTable273*274* PARAMETERS: ObjDesc - Object to be truncated275*276* RETURN: TRUE if a truncation was performed, FALSE otherwise.277*278* DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is279* 32-bit, as determined by the revision of the DSDT.280*281******************************************************************************/282283BOOLEAN284AcpiExTruncateFor32bitTable (285ACPI_OPERAND_OBJECT *ObjDesc)286{287288ACPI_FUNCTION_ENTRY ();289290291/*292* Object must be a valid number and we must be executing293* a control method. Object could be NS node for AML_INT_NAMEPATH_OP.294*/295if ((!ObjDesc) ||296(ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) ||297(ObjDesc->Common.Type != ACPI_TYPE_INTEGER))298{299return (FALSE);300}301302if ((AcpiGbl_IntegerByteWidth == 4) &&303(ObjDesc->Integer.Value > (UINT64) ACPI_UINT32_MAX))304{305/*306* We are executing in a 32-bit ACPI table. Truncate307* the value to 32 bits by zeroing out the upper 32-bit field308*/309ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX;310return (TRUE);311}312313return (FALSE);314}315316317/*******************************************************************************318*319* FUNCTION: AcpiExAcquireGlobalLock320*321* PARAMETERS: FieldFlags - Flags with Lock rule:322* AlwaysLock or NeverLock323*324* RETURN: None325*326* DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field327* flags specify that it is to be obtained before field access.328*329******************************************************************************/330331void332AcpiExAcquireGlobalLock (333UINT32 FieldFlags)334{335ACPI_STATUS Status;336337338ACPI_FUNCTION_TRACE (ExAcquireGlobalLock);339340341/* Only use the lock if the AlwaysLock bit is set */342343if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))344{345return_VOID;346}347348/* Attempt to get the global lock, wait forever */349350Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER,351AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());352353if (ACPI_FAILURE (Status))354{355ACPI_EXCEPTION ((AE_INFO, Status,356"Could not acquire Global Lock"));357}358359return_VOID;360}361362363/*******************************************************************************364*365* FUNCTION: AcpiExReleaseGlobalLock366*367* PARAMETERS: FieldFlags - Flags with Lock rule:368* AlwaysLock or NeverLock369*370* RETURN: None371*372* DESCRIPTION: Release the ACPI hardware Global Lock373*374******************************************************************************/375376void377AcpiExReleaseGlobalLock (378UINT32 FieldFlags)379{380ACPI_STATUS Status;381382383ACPI_FUNCTION_TRACE (ExReleaseGlobalLock);384385386/* Only use the lock if the AlwaysLock bit is set */387388if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))389{390return_VOID;391}392393/* Release the global lock */394395Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);396if (ACPI_FAILURE (Status))397{398/* Report the error, but there isn't much else we can do */399400ACPI_EXCEPTION ((AE_INFO, Status,401"Could not release Global Lock"));402}403404return_VOID;405}406407408/*******************************************************************************409*410* FUNCTION: AcpiExDigitsNeeded411*412* PARAMETERS: Value - Value to be represented413* Base - Base of representation414*415* RETURN: The number of digits.416*417* DESCRIPTION: Calculate the number of digits needed to represent the Value418* in the given Base (Radix)419*420******************************************************************************/421422static UINT32423AcpiExDigitsNeeded (424UINT64 Value,425UINT32 Base)426{427UINT32 NumDigits;428UINT64 CurrentValue;429430431ACPI_FUNCTION_TRACE (ExDigitsNeeded);432433434/* UINT64 is unsigned, so we don't worry about a '-' prefix */435436if (Value == 0)437{438return_UINT32 (1);439}440441CurrentValue = Value;442NumDigits = 0;443444/* Count the digits in the requested base */445446while (CurrentValue)447{448(void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);449NumDigits++;450}451452return_UINT32 (NumDigits);453}454455456/*******************************************************************************457*458* FUNCTION: AcpiExEisaIdToString459*460* PARAMETERS: OutString - Where to put the converted string (8 bytes)461* CompressedId - EISAID to be converted462*463* RETURN: None464*465* DESCRIPTION: Convert a numeric EISAID to string representation. Return466* buffer must be large enough to hold the string. The string467* returned is always exactly of length ACPI_EISAID_STRING_SIZE468* (includes null terminator). The EISAID is always 32 bits.469*470******************************************************************************/471472void473AcpiExEisaIdToString (474char *OutString,475UINT64 CompressedId)476{477UINT32 SwappedId;478479480ACPI_FUNCTION_ENTRY ();481482483/* The EISAID should be a 32-bit integer */484485if (CompressedId > ACPI_UINT32_MAX)486{487ACPI_WARNING ((AE_INFO,488"Expected EISAID is larger than 32 bits: "489"0x%8.8X%8.8X, truncating",490ACPI_FORMAT_UINT64 (CompressedId)));491}492493/* Swap ID to big-endian to get contiguous bits */494495SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId);496497/* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */498499OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F));500OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F));501OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F));502OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12);503OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8);504OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4);505OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0);506OutString[7] = 0;507}508509510/*******************************************************************************511*512* FUNCTION: AcpiExIntegerToString513*514* PARAMETERS: OutString - Where to put the converted string. At least515* 21 bytes are needed to hold the largest516* possible 64-bit integer.517* Value - Value to be converted518*519* RETURN: Converted string in OutString520*521* DESCRIPTION: Convert a 64-bit integer to decimal string representation.522* Assumes string buffer is large enough to hold the string. The523* largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).524*525******************************************************************************/526527void528AcpiExIntegerToString (529char *OutString,530UINT64 Value)531{532UINT32 Count;533UINT32 DigitsNeeded;534UINT32 Remainder;535536537ACPI_FUNCTION_ENTRY ();538539540DigitsNeeded = AcpiExDigitsNeeded (Value, 10);541OutString[DigitsNeeded] = 0;542543for (Count = DigitsNeeded; Count > 0; Count--)544{545(void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);546OutString[Count-1] = (char) ('0' + Remainder);\547}548}549550551/*******************************************************************************552*553* FUNCTION: AcpiExPciClsToString554*555* PARAMETERS: OutString - Where to put the converted string (7 bytes)556* ClassCode - PCI class code to be converted (3 bytes)557*558* RETURN: Converted string in OutString559*560* DESCRIPTION: Convert 3-bytes PCI class code to string representation.561* Return buffer must be large enough to hold the string. The562* string returned is always exactly of length563* ACPI_PCICLS_STRING_SIZE (includes null terminator).564*565******************************************************************************/566567void568AcpiExPciClsToString (569char *OutString,570UINT8 ClassCode[3])571{572573ACPI_FUNCTION_ENTRY ();574575576/* All 3 bytes are hexadecimal */577578OutString[0] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 4);579OutString[1] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[0], 0);580OutString[2] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 4);581OutString[3] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[1], 0);582OutString[4] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 4);583OutString[5] = AcpiUtHexToAsciiChar ((UINT64) ClassCode[2], 0);584OutString[6] = 0;585}586587588/*******************************************************************************589*590* FUNCTION: AcpiIsValidSpaceId591*592* PARAMETERS: SpaceId - ID to be validated593*594* RETURN: TRUE if SpaceId is a valid/supported ID.595*596* DESCRIPTION: Validate an operation region SpaceID.597*598******************************************************************************/599600BOOLEAN601AcpiIsValidSpaceId (602UINT8 SpaceId)603{604605if ((SpaceId >= ACPI_NUM_PREDEFINED_REGIONS) &&606(SpaceId < ACPI_USER_REGION_BEGIN) &&607(SpaceId != ACPI_ADR_SPACE_DATA_TABLE) &&608(SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE))609{610return (FALSE);611}612613return (TRUE);614}615616617