Path: blob/main/sys/contrib/dev/acpica/compiler/aslexternal.c
48373 views
/******************************************************************************1*2* Module Name: aslexternal - ASL External opcode compiler support3*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/compiler/aslcompiler.h>152#include "aslcompiler.y.h"153#include <contrib/dev/acpica/include/acparser.h>154#include <contrib/dev/acpica/include/amlcode.h>155#include <contrib/dev/acpica/include/acnamesp.h>156157158#define _COMPONENT ACPI_COMPILER159ACPI_MODULE_NAME ("aslexternal")160161162/* Local prototypes */163164static void165ExInsertArgCount (166ACPI_PARSE_OBJECT *Op);167168static void169ExMoveExternals (170ACPI_PARSE_OBJECT *DefinitionBlockOp);171172173/*******************************************************************************174*175* FUNCTION: ExDoExternal176*177* PARAMETERS: Op - Current Parse node178*179* RETURN: None180*181* DESCRIPTION: Add an External() definition to the global list. This list182* is used to generate External opcodes.183*184******************************************************************************/185186void187ExDoExternal (188ACPI_PARSE_OBJECT *Op)189{190ACPI_PARSE_OBJECT *ListOp;191ACPI_PARSE_OBJECT *Prev;192ACPI_PARSE_OBJECT *Next;193ACPI_PARSE_OBJECT *ArgCountOp;194ACPI_PARSE_OBJECT *TypeOp;195ACPI_PARSE_OBJECT *ExternTypeOp = Op->Asl.Child->Asl.Next;196UINT32 ExternType;197UINT8 ParamCount = ASL_EXTERNAL_METHOD_UNKNOWN_PARAMS;198UINT32 ParamTypes[ACPI_METHOD_NUM_ARGS];199200201ExternType = AnMapObjTypeToBtype (ExternTypeOp);202if (ExternType != ACPI_BTYPE_METHOD)203{204/*205* If this is not a method, it has zero parameters this local variable206* is used only for methods207*/208ParamCount = 0;209}210211/*212* The parser allows optional parameter return types regardless of the213* type. Check object type keyword emit error if optional parameter/return214* types exist.215*216* Check the parameter return type217*/218TypeOp = ExternTypeOp->Asl.Next;219if (TypeOp->Asl.Child)220{221/* Ignore the return type for now. */222223(void) MtProcessTypeOp (TypeOp->Asl.Child);224if (ExternType != ACPI_BTYPE_METHOD)225{226sprintf (AslGbl_MsgBuffer, "Found type [%s]", AcpiUtGetTypeName(ExternType));227AslError (ASL_ERROR, ASL_MSG_EXTERN_INVALID_RET_TYPE, TypeOp,228AslGbl_MsgBuffer);229}230}231232/* Check the parameter types */233234TypeOp = TypeOp->Asl.Next;235if (TypeOp->Asl.Child)236{237ParamCount = MtProcessParameterTypeList (TypeOp->Asl.Child, ParamTypes);238if (ExternType != ACPI_BTYPE_METHOD)239{240sprintf (AslGbl_MsgBuffer, "Found type [%s]", AcpiUtGetTypeName(ExternType));241AslError (ASL_ERROR, ASL_MSG_EXTERN_INVALID_PARAM_TYPE, TypeOp,242AslGbl_MsgBuffer);243}244}245246ArgCountOp = Op->Asl.Child->Asl.Next->Asl.Next;247ArgCountOp->Asl.AmlOpcode = AML_RAW_DATA_BYTE;248ArgCountOp->Asl.ParseOpcode = PARSEOP_BYTECONST;249ArgCountOp->Asl.Value.Integer = ParamCount;250UtSetParseOpName (ArgCountOp);251252/* Create new list node of arbitrary type */253254ListOp = TrAllocateOp (PARSEOP_DEFAULT_ARG);255256/* Store External node as child */257258ListOp->Asl.Child = Op;259ListOp->Asl.Next = NULL;260261if (AslGbl_ExternalsListHead)262{263/* Link new External to end of list */264265Prev = AslGbl_ExternalsListHead;266Next = Prev;267while (Next)268{269Prev = Next;270Next = Next->Asl.Next;271}272273Prev->Asl.Next = ListOp;274}275else276{277AslGbl_ExternalsListHead = ListOp;278}279}280281282/*******************************************************************************283*284* FUNCTION: ExInsertArgCount285*286* PARAMETERS: Op - Op for a method invocation287*288* RETURN: None289*290* DESCRIPTION: Obtain the number of arguments for a control method -- from291* the actual invocation.292*293******************************************************************************/294295static void296ExInsertArgCount (297ACPI_PARSE_OBJECT *Op)298{299ACPI_PARSE_OBJECT *Next;300ACPI_PARSE_OBJECT *NameOp;301ACPI_PARSE_OBJECT *Child;302ACPI_PARSE_OBJECT *ArgCountOp;303char * ExternalName;304char * CallName;305UINT16 ArgCount = 0;306ACPI_STATUS Status;307308309CallName = AcpiNsGetNormalizedPathname (Op->Asl.Node, TRUE);310311Next = AslGbl_ExternalsListHead;312while (Next)313{314ArgCount = 0;315316/* Skip if External node already handled */317318if (Next->Asl.Child->Asl.CompileFlags & OP_VISITED)319{320Next = Next->Asl.Next;321continue;322}323324NameOp = Next->Asl.Child->Asl.Child;325ExternalName = AcpiNsGetNormalizedPathname (NameOp->Asl.Node, TRUE);326327if (strcmp (CallName, ExternalName))328{329ACPI_FREE (ExternalName);330Next = Next->Asl.Next;331continue;332}333334Next->Asl.Child->Asl.CompileFlags |= OP_VISITED;335336/*337* Since we will reposition Externals to the Root, set Namepath338* to the fully qualified name and recalculate the aml length339*/340Status = UtInternalizeName (ExternalName,341&NameOp->Asl.Value.String);342343ACPI_FREE (ExternalName);344if (ACPI_FAILURE (Status))345{346AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,347NULL, "- Could not Internalize External");348break;349}350351NameOp->Asl.AmlLength = strlen (NameOp->Asl.Value.String);352353/* Get argument count */354355Child = Op->Asl.Child;356while (Child)357{358ArgCount++;359Child = Child->Asl.Next;360}361362/* Setup ArgCount operand */363364ArgCountOp = Next->Asl.Child->Asl.Child->Asl.Next->Asl.Next;365ArgCountOp->Asl.Value.Integer = ArgCount;366break;367}368369ACPI_FREE (CallName);370}371372373/*******************************************************************************374*375* FUNCTION: ExAmlExternalWalkBegin376*377* PARAMETERS: ASL_WALK_CALLBACK378*379* RETURN: None380*381* DESCRIPTION: Parse tree walk to create external opcode list for methods.382*383******************************************************************************/384385ACPI_STATUS386ExAmlExternalWalkBegin (387ACPI_PARSE_OBJECT *Op,388UINT32 Level,389void *Context)390{391392/* External list head saved in the definition block op */393394if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)395{396AslGbl_ExternalsListHead = Op->Asl.Value.Arg;397}398399if (!AslGbl_ExternalsListHead)400{401return (AE_OK);402}403404if (Op->Asl.ParseOpcode != PARSEOP_METHODCALL)405{406return (AE_OK);407}408409/*410* The NameOp child under an ExternalOp gets turned into PARSE_METHODCALL411* by XfNamespaceLocateBegin(). Ignore these.412*/413if (Op->Asl.Parent &&414Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_EXTERNAL)415{416return (AE_OK);417}418419ExInsertArgCount (Op);420return (AE_OK);421}422423424/*******************************************************************************425*426* FUNCTION: ExAmlExternalWalkEnd427*428* PARAMETERS: ASL_WALK_CALLBACK429*430* RETURN: None431*432* DESCRIPTION: Parse tree walk to create external opcode list for methods.433* Here, we just want to catch the case where a definition block434* has been completed. Then we move all of the externals into435* a single block in the parse tree and thus the AML code.436*437******************************************************************************/438439ACPI_STATUS440ExAmlExternalWalkEnd (441ACPI_PARSE_OBJECT *Op,442UINT32 Level,443void *Context)444{445446if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)447{448/*449* Process any existing external list. (Support for450* multiple definition blocks in a single file/compile)451*/452ExMoveExternals (Op);453AslGbl_ExternalsListHead = NULL;454}455456return (AE_OK);457}458459460/*******************************************************************************461*462* FUNCTION: ExMoveExternals463*464* PARAMETERS: DefinitionBlockOp - Op for current definition block465*466* RETURN: None467*468* DESCRIPTION: Move all externals present in the source file into a single469* block of AML code, surrounded by an "If (0)" to prevent470* AML interpreters from attempting to execute the External471* opcodes.472*473******************************************************************************/474475static void476ExMoveExternals (477ACPI_PARSE_OBJECT *DefinitionBlockOp)478{479ACPI_PARSE_OBJECT *ParentOp;480ACPI_PARSE_OBJECT *ExternalOp;481ACPI_PARSE_OBJECT *PredicateOp;482ACPI_PARSE_OBJECT *NextOp;483ACPI_PARSE_OBJECT *Prev;484ACPI_PARSE_OBJECT *Next;485char *ExternalName;486ACPI_OBJECT_TYPE ObjType;487ACPI_STATUS Status;488UINT32 i;489490491if (!AslGbl_ExternalsListHead)492{493return;494}495496/* Remove the External nodes from the tree */497498NextOp = AslGbl_ExternalsListHead;499while (NextOp)500{501/*502* The External is stored in child pointer of each node in the503* list504*/505ExternalOp = NextOp->Asl.Child;506507/* Get/set the fully qualified name */508509ExternalName = AcpiNsGetNormalizedPathname (ExternalOp->Asl.Node, TRUE);510ExternalOp->Asl.ExternalName = ExternalName;511ExternalOp->Asl.Namepath = ExternalName;512513/* Set line numbers (for listings, etc.) */514515ExternalOp->Asl.LineNumber = 0;516ExternalOp->Asl.LogicalLineNumber = 0;517518Next = ExternalOp->Asl.Child;519Next->Asl.LineNumber = 0;520Next->Asl.LogicalLineNumber = 0;521522if (Next->Asl.ParseOpcode == PARSEOP_NAMESEG)523{524Next->Asl.ParseOpcode = PARSEOP_NAMESTRING;525}526527Next->Asl.ExternalName = ExternalName;528Status = UtInternalizeName (ExternalName, &Next->Asl.Value.String);529if (ACPI_FAILURE (Status))530{531AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,532Next, "Could not internalize namestring");533return;534}535536Next->Asl.AmlLength = strlen (Next->Asl.Value.String);537538Next = Next->Asl.Next;539Next->Asl.LineNumber = 0;540Next->Asl.LogicalLineNumber = 0;541542Next = Next->Asl.Next;543Next->Asl.LineNumber = 0;544Next->Asl.LogicalLineNumber = 0;545546Next = Next->Asl.Next;547Next->Asl.LineNumber = 0;548Next->Asl.LogicalLineNumber = 0;549550ParentOp = ExternalOp->Asl.Parent;551Prev = Next = ParentOp->Asl.Child;552553/* Now find the External node's position in parse tree */554555while (Next != ExternalOp)556{557Prev = Next;558Next = Next->Asl.Next;559}560561/* Remove the External from the parse tree */562563if (Prev == ExternalOp)564{565/* External was the first child node */566567ParentOp->Asl.Child = ExternalOp->Asl.Next;568}569570Prev->Asl.Next = ExternalOp->Asl.Next;571ExternalOp->Asl.Next = NULL;572ExternalOp->Asl.Parent = AslGbl_ExternalsListHead;573574/* Point the External to the next in the list */575576if (NextOp->Asl.Next)577{578ExternalOp->Asl.Next = NextOp->Asl.Next->Asl.Child;579}580581NextOp = NextOp->Asl.Next;582}583584/*585* Loop again to remove MethodObj Externals for which586* a MethodCall was not found (dead external reference)587*/588Prev = AslGbl_ExternalsListHead->Asl.Child;589Next = Prev;590while (Next)591{592ObjType = (ACPI_OBJECT_TYPE)593Next->Asl.Child->Asl.Next->Asl.Value.Integer;594595if (ObjType == ACPI_TYPE_METHOD &&596!(Next->Asl.CompileFlags & OP_VISITED))597{598if (Next == Prev)599{600AslGbl_ExternalsListHead->Asl.Child = Next->Asl.Next;601Next->Asl.Next = NULL;602Prev = AslGbl_ExternalsListHead->Asl.Child;603Next = Prev;604continue;605}606else607{608Prev->Asl.Next = Next->Asl.Next;609Next->Asl.Next = NULL;610Next = Prev->Asl.Next;611continue;612}613}614615Prev = Next;616Next = Next->Asl.Next;617}618619/* If list is now empty, don't bother to make If (0) block */620621if (!AslGbl_ExternalsListHead->Asl.Child)622{623return;624}625626/* Convert Gbl_ExternalsListHead parent to If(). */627628AslGbl_ExternalsListHead->Asl.ParseOpcode = PARSEOP_IF;629AslGbl_ExternalsListHead->Asl.AmlOpcode = AML_IF_OP;630AslGbl_ExternalsListHead->Asl.CompileFlags = OP_AML_PACKAGE;631UtSetParseOpName (AslGbl_ExternalsListHead);632633/* Create a Zero op for the If predicate */634635PredicateOp = TrAllocateOp (PARSEOP_ZERO);636PredicateOp->Asl.AmlOpcode = AML_ZERO_OP;637638PredicateOp->Asl.Parent = AslGbl_ExternalsListHead;639PredicateOp->Asl.Child = NULL;640PredicateOp->Asl.Next = AslGbl_ExternalsListHead->Asl.Child;641AslGbl_ExternalsListHead->Asl.Child = PredicateOp;642643/* Set line numbers (for listings, etc.) */644645AslGbl_ExternalsListHead->Asl.LineNumber = 0;646AslGbl_ExternalsListHead->Asl.LogicalLineNumber = 0;647648PredicateOp->Asl.LineNumber = 0;649PredicateOp->Asl.LogicalLineNumber = 0;650651/* Insert block back in the list */652653Prev = DefinitionBlockOp->Asl.Child;654Next = Prev;655656/* Find last default arg */657658for (i = 0; i < 6; i++)659{660Prev = Next;661Next = Prev->Asl.Next;662}663664if (Next)665{666/* Definition Block is not empty */667668AslGbl_ExternalsListHead->Asl.Next = Next;669}670else671{672/* Definition Block is empty. */673674AslGbl_ExternalsListHead->Asl.Next = NULL;675}676677Prev->Asl.Next = AslGbl_ExternalsListHead;678AslGbl_ExternalsListHead->Asl.Parent = Prev->Asl.Parent;679}680681682