Path: blob/main/sys/contrib/dev/acpica/common/dmswitch.c
48375 views
/******************************************************************************1*2* Module Name: adwalk - Disassembler routines for switch statements3*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/amlcode.h>155#include <contrib/dev/acpica/include/acdisasm.h>156#include <contrib/dev/acpica/include/acdispat.h>157#include <contrib/dev/acpica/include/acnamesp.h>158#include <contrib/dev/acpica/include/acapps.h>159160161#define _COMPONENT ACPI_CA_DISASSEMBLER162ACPI_MODULE_NAME ("dmswitch")163164static BOOLEAN165AcpiDmIsSwitchBlock (166ACPI_PARSE_OBJECT *Op,167char **Temp);168169static BOOLEAN170AcpiDmIsCaseBlock (171ACPI_PARSE_OBJECT *Op);172173174/*******************************************************************************175*176* FUNCTION: AcpiDmProcessSwitch177*178* PARAMETERS: Op - Object to be examined179*180* RETURN: ACPI_STATUS181*182* DESCRIPTION: Walk function to create a list of all temporary (_T_) objects.183* If a While loop is found that can be converted to a Switch, do184* the conversion, remove the temporary name from the list, and185* mark the parse op with an IGNORE flag.186*187******************************************************************************/188189ACPI_STATUS190AcpiDmProcessSwitch (191ACPI_PARSE_OBJECT *Op)192{193char *Temp = NULL;194ACPI_PARSE_OBJECT_LIST *NewTemp;195ACPI_PARSE_OBJECT_LIST *Current;196ACPI_PARSE_OBJECT_LIST *Previous;197BOOLEAN FoundTemp = FALSE;198199200switch (Op->Common.AmlOpcode)201{202case AML_NAME_OP:203204Temp = (char *) (&Op->Named.Name);205206if (!strncmp(Temp, "_T_", 3))207{208/* Allocate and init a new Temp List node */209210NewTemp = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PARSE_OBJECT_LIST));211if (!NewTemp)212{213return (AE_NO_MEMORY);214}215216if (AcpiGbl_TempListHead)217{218Current = AcpiGbl_TempListHead;219AcpiGbl_TempListHead = NewTemp;220AcpiGbl_TempListHead->Op = Op;221AcpiGbl_TempListHead->Next = Current;222}223else224{225AcpiGbl_TempListHead = NewTemp;226AcpiGbl_TempListHead->Op = Op;227AcpiGbl_TempListHead->Next = NULL;228}229}230break;231232case AML_WHILE_OP:233234if (!AcpiDmIsSwitchBlock (Op, &Temp))235{236break;237}238239/* Found a Switch */240241Op->Common.DisasmOpcode = ACPI_DASM_SWITCH;242243Previous = Current = AcpiGbl_TempListHead;244while (Current)245{246/* Note, if we get here Temp is not NULL */247248if (!strncmp(Temp, (char *) (&Current->Op->Named.Name), 4))249{250/* Match found. Ignore disassembly */251252Current->Op->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;253254/* Remove from list */255256if (Current == AcpiGbl_TempListHead)257{258AcpiGbl_TempListHead = Current->Next;259}260else261{262Previous->Next = Current->Next;263}264265Current->Op = NULL;266Current->Next = NULL;267ACPI_FREE (Current);268FoundTemp = TRUE;269break;270}271272Previous = Current;273Current = Current->Next;274}275276if (!FoundTemp)277{278fprintf (stderr,279"Warning: Declaration for temp name %.4s not found\n", Temp);280}281break;282283default:284break;285}286287return (AE_OK);288}289290291/*******************************************************************************292*293* FUNCTION: AcpiDmClearTempList294*295* PARAMETERS: None296*297* RETURN: None298*299* DESCRIPTION: Removes any remaining temporary objects from global list and300* frees301*302******************************************************************************/303304void305AcpiDmClearTempList (306void)307{308ACPI_PARSE_OBJECT_LIST *Current;309310311while (AcpiGbl_TempListHead)312{313Current = AcpiGbl_TempListHead;314AcpiGbl_TempListHead = AcpiGbl_TempListHead->Next;315Current->Op = NULL;316Current->Next = NULL;317ACPI_FREE (Current);318}319}320321322/*******************************************************************************323*324* FUNCTION: AcpiDmIsSwitchBlock325*326* PARAMETERS: Op - While Object327* Temp - Where the compiler temp name is returned328* (_T_x)329*330* RETURN: TRUE if While block can be converted to a Switch/Case block331*332* DESCRIPTION: Determines if While block is a Switch/Case statement. Modifies333* parse tree to allow for Switch/Case disassembly during walk.334*335* EXAMPLE: Example of parse tree to be converted336*337* While338* One339* Store340* ByteConst341* -NamePath-342* If343* LEqual344* -NamePath-345* Zero346* Return347* One348* Else349* Return350* WordConst351* Break352*353******************************************************************************/354355BOOLEAN356AcpiDmIsSwitchBlock (357ACPI_PARSE_OBJECT *Op,358char **Temp)359{360ACPI_PARSE_OBJECT *OneOp;361ACPI_PARSE_OBJECT *StoreOp;362ACPI_PARSE_OBJECT *NamePathOp;363ACPI_PARSE_OBJECT *PredicateOp;364ACPI_PARSE_OBJECT *CurrentOp;365ACPI_PARSE_OBJECT *TempOp;366367368/* Check for One Op Predicate */369370OneOp = AcpiPsGetArg (Op, 0);371if (!OneOp || (OneOp->Common.AmlOpcode != AML_ONE_OP))372{373return (FALSE);374}375376/* Check for Store Op */377378StoreOp = OneOp->Common.Next;379if (!StoreOp || (StoreOp->Common.AmlOpcode != AML_STORE_OP))380{381return (FALSE);382}383384/* Check for Name Op with _T_ string */385386NamePathOp = AcpiPsGetArg (StoreOp, 1);387if (!NamePathOp ||388(NamePathOp->Common.AmlOpcode != AML_INT_NAMEPATH_OP))389{390return (FALSE);391}392393if (strncmp ((char *) (NamePathOp->Common.Value.Name), "_T_", 3))394{395return (FALSE);396}397398*Temp = (char *) (NamePathOp->Common.Value.Name);399400/* This is a Switch/Case control block */401402/* Ignore the One Op Predicate */403404OneOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;405406/* Ignore the Store Op, but not the children */407408StoreOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;409410/*411* First arg of Store Op is the Switch condition.412* Mark it as a Switch predicate and as a parameter list for paren413* closing and correct indentation.414*/415PredicateOp = AcpiPsGetArg (StoreOp, 0);416PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;417PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;418419/* Ignore the Name Op */420421NamePathOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;422423/* Remaining opcodes are the Case statements (If/ElseIf's) */424425CurrentOp = StoreOp->Common.Next;426while (AcpiDmIsCaseBlock (CurrentOp))427{428/* Block is a Case structure */429430if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)431{432/* ElseIf */433434CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;435CurrentOp = AcpiPsGetArg (CurrentOp, 0);436}437438/* If */439440CurrentOp->Common.DisasmOpcode = ACPI_DASM_CASE;441442/*443* Mark the parse tree for Case disassembly. There are two444* types of Case statements. The first type of statement begins with445* an LEqual. The second starts with an LNot and uses a Match statement446* on a Package of constants.447*/448TempOp = AcpiPsGetArg (CurrentOp, 0);449switch (TempOp->Common.AmlOpcode)450{451case (AML_LOGICAL_EQUAL_OP):452453/* Ignore just the LEqual Op */454455TempOp->Common.DisasmOpcode = ACPI_DASM_IGNORE_SINGLE;456457/* Ignore the NamePath Op */458459TempOp = AcpiPsGetArg (TempOp, 0);460TempOp->Common.DisasmFlags = ACPI_PARSEOP_IGNORE;461462/*463* Second arg of LEqual will be the Case predicate.464* Mark it as a predicate and also as a parameter list for paren465* closing and correct indentation.466*/467PredicateOp = TempOp->Common.Next;468PredicateOp->Common.DisasmOpcode = ACPI_DASM_SWITCH_PREDICATE;469PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;470break;471472case (AML_LOGICAL_NOT_OP):473474/*475* The Package will be the predicate of the Case statement.476* It's under:477* LNOT478* LEQUAL479* MATCH480* PACKAGE481*/482483/* Get the LEqual Op from LNot */484485TempOp = AcpiPsGetArg (TempOp, 0);486487/* Get the Match Op from LEqual */488489TempOp = AcpiPsGetArg (TempOp, 0);490491/* Get the Package Op from Match */492493PredicateOp = AcpiPsGetArg (TempOp, 0);494495/* Mark as parameter list for paren closing */496497PredicateOp->Common.DisasmFlags |= ACPI_PARSEOP_PARAMETER_LIST;498499/*500* The Package list would be too deeply indented if we501* chose to simply ignore the all the parent opcodes, so502* we rearrange the parse tree instead.503*/504505/*506* Save the second arg of the If/Else Op which is the507* block code of code for this Case statement.508*/509TempOp = AcpiPsGetArg (CurrentOp, 1);510511/*512* Move the Package Op to the child (predicate) of the513* Case statement.514*/515CurrentOp->Common.Value.Arg = PredicateOp;516PredicateOp->Common.Parent = CurrentOp;517518/* Add the block code */519520PredicateOp->Common.Next = TempOp;521break;522523default:524525/* Should never get here */526break;527}528529/* Advance to next Case block */530531CurrentOp = CurrentOp->Common.Next;532}533534/* If CurrentOp is now an Else, then this is a Default block */535536if (CurrentOp && CurrentOp->Common.AmlOpcode == AML_ELSE_OP)537{538CurrentOp->Common.DisasmOpcode = ACPI_DASM_DEFAULT;539}540541/*542* From the first If advance to the Break op. It's possible to543* have an Else (Default) op here when there is only one Case544* statement, so check for it.545*/546CurrentOp = StoreOp->Common.Next->Common.Next;547if (!CurrentOp)548{549return (FALSE);550}551if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)552{553CurrentOp = CurrentOp->Common.Next;554if (!CurrentOp)555{556return (FALSE);557}558}559560/* Ignore the Break Op */561562CurrentOp->Common.DisasmFlags |= ACPI_PARSEOP_IGNORE;563return (TRUE);564}565566567/*******************************************************************************568*569* FUNCTION: AcpiDmIsCaseBlock570*571* PARAMETERS: Op - Object to test572*573* RETURN: TRUE if Object is beginning of a Case block.574*575* DESCRIPTION: Determines if an Object is the beginning of a Case block for a576* Switch/Case statement. Parse tree must be one of the following577* forms:578*579* Else (Optional)580* If581* LEqual582* -NamePath- _T_x583*584* Else (Optional)585* If586* LNot587* LEqual588* Match589* Package590* ByteConst591* -NamePath- _T_x592*593******************************************************************************/594595static BOOLEAN596AcpiDmIsCaseBlock (597ACPI_PARSE_OBJECT *Op)598{599ACPI_PARSE_OBJECT *CurrentOp;600601602if (!Op)603{604return (FALSE);605}606607/* Look for an If or ElseIf */608609CurrentOp = Op;610if (CurrentOp->Common.AmlOpcode == AML_ELSE_OP)611{612CurrentOp = AcpiPsGetArg (CurrentOp, 0);613if (!CurrentOp)614{615return (FALSE);616}617}618619if (!CurrentOp || CurrentOp->Common.AmlOpcode != AML_IF_OP)620{621return (FALSE);622}623624/* Child must be LEqual or LNot */625626CurrentOp = AcpiPsGetArg (CurrentOp, 0);627if (!CurrentOp)628{629return (FALSE);630}631632switch (CurrentOp->Common.AmlOpcode)633{634case (AML_LOGICAL_EQUAL_OP):635636/* Next child must be NamePath with string _T_ */637638CurrentOp = AcpiPsGetArg (CurrentOp, 0);639if (!CurrentOp || !CurrentOp->Common.Value.Name ||640strncmp(CurrentOp->Common.Value.Name, "_T_", 3))641{642return (FALSE);643}644break;645646case (AML_LOGICAL_NOT_OP):647648/* Child of LNot must be LEqual op */649650CurrentOp = AcpiPsGetArg (CurrentOp, 0);651if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_LOGICAL_EQUAL_OP))652{653return (FALSE);654}655656/* Child of LNot must be Match op */657658CurrentOp = AcpiPsGetArg (CurrentOp, 0);659if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_MATCH_OP))660{661return (FALSE);662}663664/* First child of Match must be Package op */665666CurrentOp = AcpiPsGetArg (CurrentOp, 0);667if (!CurrentOp || (CurrentOp->Common.AmlOpcode != AML_PACKAGE_OP))668{669return (FALSE);670}671672/* Third child of Match must be NamePath with string _T_ */673674CurrentOp = AcpiPsGetArg (CurrentOp->Common.Parent, 2);675if (!CurrentOp || !CurrentOp->Common.Value.Name ||676strncmp(CurrentOp->Common.Value.Name, "_T_", 3))677{678return (FALSE);679}680break;681682default:683684return (FALSE);685}686687return (TRUE);688}689690691