Path: blob/main/sys/contrib/dev/acpica/components/executer/exoparg2.c
48521 views
/******************************************************************************1*2* Module Name: exoparg2 - AML execution - opcodes with 2 arguments3*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#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/acparser.h>154#include <contrib/dev/acpica/include/acinterp.h>155#include <contrib/dev/acpica/include/acevents.h>156#include <contrib/dev/acpica/include/amlcode.h>157158159#define _COMPONENT ACPI_EXECUTER160ACPI_MODULE_NAME ("exoparg2")161162163/*!164* Naming convention for AML interpreter execution routines.165*166* The routines that begin execution of AML opcodes are named with a common167* convention based upon the number of arguments, the number of target operands,168* and whether or not a value is returned:169*170* AcpiExOpcode_xA_yT_zR171*172* Where:173*174* xA - ARGUMENTS: The number of arguments (input operands) that are175* required for this opcode type (1 through 6 args).176* yT - TARGETS: The number of targets (output operands) that are required177* for this opcode type (0, 1, or 2 targets).178* zR - RETURN VALUE: Indicates whether this opcode type returns a value179* as the function return (0 or 1).180*181* The AcpiExOpcode* functions are called via the Dispatcher component with182* fully resolved operands.183!*/184185186/*******************************************************************************187*188* FUNCTION: AcpiExOpcode_2A_0T_0R189*190* PARAMETERS: WalkState - Current walk state191*192* RETURN: Status193*194* DESCRIPTION: Execute opcode with two arguments, no target, and no return195* value.196*197* ALLOCATION: Deletes both operands198*199******************************************************************************/200201ACPI_STATUS202AcpiExOpcode_2A_0T_0R (203ACPI_WALK_STATE *WalkState)204{205ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];206ACPI_NAMESPACE_NODE *Node;207UINT32 Value;208ACPI_STATUS Status = AE_OK;209210211ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_0T_0R,212AcpiPsGetOpcodeName (WalkState->Opcode));213214215/* Examine the opcode */216217switch (WalkState->Opcode)218{219case AML_NOTIFY_OP: /* Notify (NotifyObject, NotifyValue) */220221/* The first operand is a namespace node */222223Node = (ACPI_NAMESPACE_NODE *) Operand[0];224225/* Second value is the notify value */226227Value = (UINT32) Operand[1]->Integer.Value;228229/* Are notifies allowed on this object? */230231if (!AcpiEvIsNotifyObject (Node))232{233ACPI_ERROR ((AE_INFO,234"Unexpected notify object type [%s]",235AcpiUtGetTypeName (Node->Type)));236237Status = AE_AML_OPERAND_TYPE;238break;239}240241/*242* Dispatch the notify to the appropriate handler243* NOTE: the request is queued for execution after this method244* completes. The notify handlers are NOT invoked synchronously245* from this thread -- because handlers may in turn run other246* control methods.247*/248Status = AcpiEvQueueNotifyRequest (Node, Value);249break;250251default:252253ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",254WalkState->Opcode));255Status = AE_AML_BAD_OPCODE;256}257258return_ACPI_STATUS (Status);259}260261262/*******************************************************************************263*264* FUNCTION: AcpiExOpcode_2A_2T_1R265*266* PARAMETERS: WalkState - Current walk state267*268* RETURN: Status269*270* DESCRIPTION: Execute a dyadic operator (2 operands) with 2 output targets271* and one implicit return value.272*273******************************************************************************/274275ACPI_STATUS276AcpiExOpcode_2A_2T_1R (277ACPI_WALK_STATE *WalkState)278{279ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];280ACPI_OPERAND_OBJECT *ReturnDesc1 = NULL;281ACPI_OPERAND_OBJECT *ReturnDesc2 = NULL;282ACPI_STATUS Status;283284285ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_2T_1R,286AcpiPsGetOpcodeName (WalkState->Opcode));287288289/* Execute the opcode */290291switch (WalkState->Opcode)292{293case AML_DIVIDE_OP:294295/* Divide (Dividend, Divisor, RemainderResult QuotientResult) */296297ReturnDesc1 = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);298if (!ReturnDesc1)299{300Status = AE_NO_MEMORY;301goto Cleanup;302}303304ReturnDesc2 = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);305if (!ReturnDesc2)306{307Status = AE_NO_MEMORY;308goto Cleanup;309}310311/* Quotient to ReturnDesc1, remainder to ReturnDesc2 */312313Status = AcpiUtDivide (314Operand[0]->Integer.Value,315Operand[1]->Integer.Value,316&ReturnDesc1->Integer.Value,317&ReturnDesc2->Integer.Value);318if (ACPI_FAILURE (Status))319{320goto Cleanup;321}322break;323324default:325326ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",327WalkState->Opcode));328329Status = AE_AML_BAD_OPCODE;330goto Cleanup;331}332333/* Store the results to the target reference operands */334335Status = AcpiExStore (ReturnDesc2, Operand[2], WalkState);336if (ACPI_FAILURE (Status))337{338goto Cleanup;339}340341Status = AcpiExStore (ReturnDesc1, Operand[3], WalkState);342if (ACPI_FAILURE (Status))343{344goto Cleanup;345}346347Cleanup:348/*349* Since the remainder is not returned indirectly, remove a reference to350* it. Only the quotient is returned indirectly.351*/352AcpiUtRemoveReference (ReturnDesc2);353354if (ACPI_FAILURE (Status))355{356/* Delete the return object */357358AcpiUtRemoveReference (ReturnDesc1);359}360361/* Save return object (the remainder) on success */362363else364{365WalkState->ResultObj = ReturnDesc1;366}367368return_ACPI_STATUS (Status);369}370371372/*******************************************************************************373*374* FUNCTION: AcpiExOpcode_2A_1T_1R375*376* PARAMETERS: WalkState - Current walk state377*378* RETURN: Status379*380* DESCRIPTION: Execute opcode with two arguments, one target, and a return381* value.382*383******************************************************************************/384385ACPI_STATUS386AcpiExOpcode_2A_1T_1R (387ACPI_WALK_STATE *WalkState)388{389ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];390ACPI_OPERAND_OBJECT *ReturnDesc = NULL;391UINT64 Index;392ACPI_STATUS Status = AE_OK;393ACPI_SIZE Length = 0;394395396ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_1T_1R,397AcpiPsGetOpcodeName (WalkState->Opcode));398399400/* Execute the opcode */401402if (WalkState->OpInfo->Flags & AML_MATH)403{404/* All simple math opcodes (add, etc.) */405406ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);407if (!ReturnDesc)408{409Status = AE_NO_MEMORY;410goto Cleanup;411}412413ReturnDesc->Integer.Value = AcpiExDoMathOp (414WalkState->Opcode,415Operand[0]->Integer.Value,416Operand[1]->Integer.Value);417goto StoreResultToTarget;418}419420switch (WalkState->Opcode)421{422case AML_MOD_OP: /* Mod (Dividend, Divisor, RemainderResult (ACPI 2.0) */423424ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);425if (!ReturnDesc)426{427Status = AE_NO_MEMORY;428goto Cleanup;429}430431/* ReturnDesc will contain the remainder */432433Status = AcpiUtDivide (434Operand[0]->Integer.Value,435Operand[1]->Integer.Value,436NULL,437&ReturnDesc->Integer.Value);438break;439440case AML_CONCATENATE_OP: /* Concatenate (Data1, Data2, Result) */441442Status = AcpiExDoConcatenate (443Operand[0], Operand[1], &ReturnDesc, WalkState);444break;445446case AML_TO_STRING_OP: /* ToString (Buffer, Length, Result) (ACPI 2.0) */447/*448* Input object is guaranteed to be a buffer at this point (it may have449* been converted.) Copy the raw buffer data to a new object of450* type String.451*/452453/*454* Get the length of the new string. It is the smallest of:455* 1) Length of the input buffer456* 2) Max length as specified in the ToString operator457* 3) Length of input buffer up to a zero byte (null terminator)458*459* NOTE: A length of zero is ok, and will create a zero-length, null460* terminated string.461*/462while ((Length < Operand[0]->Buffer.Length) && /* Length of input buffer */463(Length < Operand[1]->Integer.Value) && /* Length operand */464(Operand[0]->Buffer.Pointer[Length])) /* Null terminator */465{466Length++;467}468469/* Allocate a new string object */470471ReturnDesc = AcpiUtCreateStringObject (Length);472if (!ReturnDesc)473{474Status = AE_NO_MEMORY;475goto Cleanup;476}477478/*479* Copy the raw buffer data with no transform.480* (NULL terminated already)481*/482memcpy (ReturnDesc->String.Pointer,483Operand[0]->Buffer.Pointer, Length);484break;485486case AML_CONCATENATE_TEMPLATE_OP:487488/* ConcatenateResTemplate (Buffer, Buffer, Result) (ACPI 2.0) */489490Status = AcpiExConcatTemplate (491Operand[0], Operand[1], &ReturnDesc, WalkState);492break;493494case AML_INDEX_OP: /* Index (Source Index Result) */495496/* Create the internal return object */497498ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);499if (!ReturnDesc)500{501Status = AE_NO_MEMORY;502goto Cleanup;503}504505/* Initialize the Index reference object */506507Index = Operand[1]->Integer.Value;508ReturnDesc->Reference.Value = (UINT32) Index;509ReturnDesc->Reference.Class = ACPI_REFCLASS_INDEX;510511/*512* At this point, the Source operand is a String, Buffer, or Package.513* Verify that the index is within range.514*/515switch ((Operand[0])->Common.Type)516{517case ACPI_TYPE_STRING:518519if (Index >= Operand[0]->String.Length)520{521Length = Operand[0]->String.Length;522Status = AE_AML_STRING_LIMIT;523}524525ReturnDesc->Reference.TargetType = ACPI_TYPE_BUFFER_FIELD;526ReturnDesc->Reference.IndexPointer =527&(Operand[0]->Buffer.Pointer [Index]);528break;529530case ACPI_TYPE_BUFFER:531532if (Index >= Operand[0]->Buffer.Length)533{534Length = Operand[0]->Buffer.Length;535Status = AE_AML_BUFFER_LIMIT;536}537538ReturnDesc->Reference.TargetType = ACPI_TYPE_BUFFER_FIELD;539ReturnDesc->Reference.IndexPointer =540&(Operand[0]->Buffer.Pointer [Index]);541break;542543case ACPI_TYPE_PACKAGE:544545if (Index >= Operand[0]->Package.Count)546{547Length = Operand[0]->Package.Count;548Status = AE_AML_PACKAGE_LIMIT;549}550551ReturnDesc->Reference.TargetType = ACPI_TYPE_PACKAGE;552ReturnDesc->Reference.Where =553&Operand[0]->Package.Elements [Index];554break;555556default:557558ACPI_ERROR ((AE_INFO,559"Invalid object type: %X", (Operand[0])->Common.Type));560Status = AE_AML_INTERNAL;561goto Cleanup;562}563564/* Failure means that the Index was beyond the end of the object */565566if (ACPI_FAILURE (Status))567{568ACPI_BIOS_EXCEPTION ((AE_INFO, Status,569"Index (0x%X%8.8X) is beyond end of object (length 0x%X)",570ACPI_FORMAT_UINT64 (Index), (UINT32) Length));571goto Cleanup;572}573574/*575* Save the target object and add a reference to it for the life576* of the index577*/578ReturnDesc->Reference.Object = Operand[0];579AcpiUtAddReference (Operand[0]);580581/* Store the reference to the Target */582583Status = AcpiExStore (ReturnDesc, Operand[2], WalkState);584585/* Return the reference */586587WalkState->ResultObj = ReturnDesc;588goto Cleanup;589590default:591592ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",593WalkState->Opcode));594Status = AE_AML_BAD_OPCODE;595break;596}597598599StoreResultToTarget:600601if (ACPI_SUCCESS (Status))602{603/*604* Store the result of the operation (which is now in ReturnDesc) into605* the Target descriptor.606*/607Status = AcpiExStore (ReturnDesc, Operand[2], WalkState);608if (ACPI_FAILURE (Status))609{610goto Cleanup;611}612613if (!WalkState->ResultObj)614{615WalkState->ResultObj = ReturnDesc;616}617}618619620Cleanup:621622/* Delete return object on error */623624if (ACPI_FAILURE (Status))625{626AcpiUtRemoveReference (ReturnDesc);627WalkState->ResultObj = NULL;628}629630return_ACPI_STATUS (Status);631}632633634/*******************************************************************************635*636* FUNCTION: AcpiExOpcode_2A_0T_1R637*638* PARAMETERS: WalkState - Current walk state639*640* RETURN: Status641*642* DESCRIPTION: Execute opcode with 2 arguments, no target, and a return value643*644******************************************************************************/645646ACPI_STATUS647AcpiExOpcode_2A_0T_1R (648ACPI_WALK_STATE *WalkState)649{650ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];651ACPI_OPERAND_OBJECT *ReturnDesc = NULL;652ACPI_STATUS Status = AE_OK;653BOOLEAN LogicalResult = FALSE;654655656ACPI_FUNCTION_TRACE_STR (ExOpcode_2A_0T_1R,657AcpiPsGetOpcodeName (WalkState->Opcode));658659660/* Create the internal return object */661662ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_INTEGER);663if (!ReturnDesc)664{665Status = AE_NO_MEMORY;666goto Cleanup;667}668669/* Execute the Opcode */670671if (WalkState->OpInfo->Flags & AML_LOGICAL_NUMERIC)672{673/* LogicalOp (Operand0, Operand1) */674675Status = AcpiExDoLogicalNumericOp (WalkState->Opcode,676Operand[0]->Integer.Value, Operand[1]->Integer.Value,677&LogicalResult);678goto StoreLogicalResult;679}680else if (WalkState->OpInfo->Flags & AML_LOGICAL)681{682/* LogicalOp (Operand0, Operand1) */683684Status = AcpiExDoLogicalOp (WalkState->Opcode, Operand[0],685Operand[1], &LogicalResult);686goto StoreLogicalResult;687}688689switch (WalkState->Opcode)690{691case AML_ACQUIRE_OP: /* Acquire (MutexObject, Timeout) */692693Status = AcpiExAcquireMutex (Operand[1], Operand[0], WalkState);694if (Status == AE_TIME)695{696LogicalResult = TRUE; /* TRUE = Acquire timed out */697Status = AE_OK;698}699break;700701702case AML_WAIT_OP: /* Wait (EventObject, Timeout) */703704Status = AcpiExSystemWaitEvent (Operand[1], Operand[0]);705if (Status == AE_TIME)706{707LogicalResult = TRUE; /* TRUE, Wait timed out */708Status = AE_OK;709}710break;711712default:713714ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",715WalkState->Opcode));716717Status = AE_AML_BAD_OPCODE;718goto Cleanup;719}720721722StoreLogicalResult:723/*724* Set return value to according to LogicalResult. logical TRUE (all ones)725* Default is FALSE (zero)726*/727if (LogicalResult)728{729ReturnDesc->Integer.Value = ACPI_UINT64_MAX;730}731732Cleanup:733734/* Delete return object on error */735736if (ACPI_FAILURE (Status))737{738AcpiUtRemoveReference (ReturnDesc);739}740741/* Save return object on success */742743else744{745WalkState->ResultObj = ReturnDesc;746}747748return_ACPI_STATUS (Status);749}750751752