Path: blob/main/sys/contrib/dev/acpica/components/dispatcher/dscontrol.c
48521 views
/******************************************************************************1*2* Module Name: dscontrol - Support for execution control opcodes -3* if/else/while/return4*5*****************************************************************************/67/******************************************************************************8*9* 1. Copyright Notice10*11* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.12* All rights reserved.13*14* 2. License15*16* 2.1. This is your license from Intel Corp. under its intellectual property17* rights. You may have additional license terms from the party that provided18* you this software, covering your right to use that party's intellectual19* property rights.20*21* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a22* copy of the source code appearing in this file ("Covered Code") an23* irrevocable, perpetual, worldwide license under Intel's copyrights in the24* base code distributed originally by Intel ("Original Intel Code") to copy,25* make derivatives, distribute, use and display any portion of the Covered26* Code in any form, with the right to sublicense such rights; and27*28* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent29* license (with the right to sublicense), under only those claims of Intel30* patents that are infringed by the Original Intel Code, to make, use, sell,31* offer to sell, and import the Covered Code and derivative works thereof32* solely to the minimum extent necessary to exercise the above copyright33* license, and in no event shall the patent license extend to any additions34* to or modifications of the Original Intel Code. No other license or right35* is granted directly or by implication, estoppel or otherwise;36*37* The above copyright and patent license is granted only if the following38* conditions are met:39*40* 3. Conditions41*42* 3.1. Redistribution of Source with Rights to Further Distribute Source.43* Redistribution of source code of any substantial portion of the Covered44* Code or modification with rights to further distribute source must include45* the above Copyright Notice, the above License, this list of Conditions,46* and the following Disclaimer and Export Compliance provision. In addition,47* Licensee must cause all Covered Code to which Licensee contributes to48* contain a file documenting the changes Licensee made to create that Covered49* Code and the date of any change. Licensee must include in that file the50* documentation of any changes made by any predecessor Licensee. Licensee51* must include a prominent statement that the modification is derived,52* directly or indirectly, from Original Intel Code.53*54* 3.2. Redistribution of Source with no Rights to Further Distribute Source.55* Redistribution of source code of any substantial portion of the Covered56* Code or modification without rights to further distribute source must57* include the following Disclaimer and Export Compliance provision in the58* documentation and/or other materials provided with distribution. In59* addition, Licensee may not authorize further sublicense of source of any60* portion of the Covered Code, and must include terms to the effect that the61* license from Licensee to its licensee is limited to the intellectual62* property embodied in the software Licensee provides to its licensee, and63* not to intellectual property embodied in modifications its licensee may64* make.65*66* 3.3. Redistribution of Executable. Redistribution in executable form of any67* substantial portion of the Covered Code or modification must reproduce the68* above Copyright Notice, and the following Disclaimer and Export Compliance69* provision in the documentation and/or other materials provided with the70* distribution.71*72* 3.4. Intel retains all right, title, and interest in and to the Original73* Intel Code.74*75* 3.5. Neither the name Intel nor any other trademark owned or controlled by76* Intel shall be used in advertising or otherwise to promote the sale, use or77* other dealings in products derived from or relating to the Covered Code78* without prior written authorization from Intel.79*80* 4. Disclaimer and Export Compliance81*82* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED83* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE84* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,85* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY86* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY87* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A88* PARTICULAR PURPOSE.89*90* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES91* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR92* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,93* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY94* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL95* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS96* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY97* LIMITED REMEDY.98*99* 4.3. Licensee shall not export, either directly or indirectly, any of this100* software or system incorporating such software without first obtaining any101* required license or other approval from the U. S. Department of Commerce or102* any other agency or department of the United States Government. In the103* event Licensee exports any such software from the United States or104* re-exports any such software from a foreign destination, Licensee shall105* ensure that the distribution and export/re-export of the software is in106* compliance with all laws, regulations, orders, or other restrictions of the107* U.S. Export Administration Regulations. Licensee agrees that neither it nor108* any of its subsidiaries will export/re-export any technical data, process,109* software, or service, directly or indirectly, to any country for which the110* United States government or any agency thereof requires an export license,111* other governmental approval, or letter of assurance, without first obtaining112* such license, approval or letter.113*114*****************************************************************************115*116* Alternatively, you may choose to be licensed under the terms of the117* following license:118*119* Redistribution and use in source and binary forms, with or without120* modification, are permitted provided that the following conditions121* are met:122* 1. Redistributions of source code must retain the above copyright123* notice, this list of conditions, and the following disclaimer,124* without modification.125* 2. Redistributions in binary form must reproduce at minimum a disclaimer126* substantially similar to the "NO WARRANTY" disclaimer below127* ("Disclaimer") and any redistribution must be conditioned upon128* including a substantially similar Disclaimer requirement for further129* binary redistribution.130* 3. Neither the names of the above-listed copyright holders nor the names131* of any contributors may be used to endorse or promote products derived132* from this software without specific prior written permission.133*134* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS135* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT136* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR137* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT138* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,139* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT140* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,141* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY142* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT143* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE144* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.145*146* Alternatively, you may choose to be licensed under the terms of the147* GNU General Public License ("GPL") version 2 as published by the Free148* Software Foundation.149*150*****************************************************************************/151152#include <contrib/dev/acpica/include/acpi.h>153#include <contrib/dev/acpica/include/accommon.h>154#include <contrib/dev/acpica/include/amlcode.h>155#include <contrib/dev/acpica/include/acdispat.h>156#include <contrib/dev/acpica/include/acinterp.h>157#include <contrib/dev/acpica/include/acdebug.h>158159#define _COMPONENT ACPI_DISPATCHER160ACPI_MODULE_NAME ("dscontrol")161162163/*******************************************************************************164*165* FUNCTION: AcpiDsExecBeginControlOp166*167* PARAMETERS: WalkList - The list that owns the walk stack168* Op - The control Op169*170* RETURN: Status171*172* DESCRIPTION: Handles all control ops encountered during control method173* execution.174*175******************************************************************************/176177ACPI_STATUS178AcpiDsExecBeginControlOp (179ACPI_WALK_STATE *WalkState,180ACPI_PARSE_OBJECT *Op)181{182ACPI_STATUS Status = AE_OK;183ACPI_GENERIC_STATE *ControlState;184185186ACPI_FUNCTION_NAME (DsExecBeginControlOp);187188189ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",190Op, Op->Common.AmlOpcode, WalkState));191192switch (Op->Common.AmlOpcode)193{194case AML_WHILE_OP:195/*196* If this is an additional iteration of a while loop, continue.197* There is no need to allocate a new control state.198*/199if (WalkState->ControlState)200{201if (WalkState->ControlState->Control.AmlPredicateStart ==202(WalkState->ParserState.Aml - 1))203{204/* Reset the state to start-of-loop */205206WalkState->ControlState->Common.State =207ACPI_CONTROL_CONDITIONAL_EXECUTING;208break;209}210}211212ACPI_FALLTHROUGH;213214case AML_IF_OP:215/*216* IF/WHILE: Create a new control state to manage these217* constructs. We need to manage these as a stack, in order218* to handle nesting.219*/220ControlState = AcpiUtCreateControlState ();221if (!ControlState)222{223Status = AE_NO_MEMORY;224break;225}226/*227* Save a pointer to the predicate for multiple executions228* of a loop229*/230ControlState->Control.AmlPredicateStart =231WalkState->ParserState.Aml - 1;232ControlState->Control.PackageEnd =233WalkState->ParserState.PkgEnd;234ControlState->Control.Opcode =235Op->Common.AmlOpcode;236ControlState->Control.LoopTimeout = AcpiOsGetTimer () +237((UINT64) AcpiGbl_MaxLoopIterations * ACPI_100NSEC_PER_SEC);238239/* Push the control state on this walk's control stack */240241AcpiUtPushGenericState (&WalkState->ControlState, ControlState);242break;243244case AML_ELSE_OP:245246/* Predicate is in the state object */247/* If predicate is true, the IF was executed, ignore ELSE part */248249if (WalkState->LastPredicate)250{251Status = AE_CTRL_TRUE;252}253254break;255256case AML_RETURN_OP:257258break;259260default:261262break;263}264265return (Status);266}267268269/*******************************************************************************270*271* FUNCTION: AcpiDsExecEndControlOp272*273* PARAMETERS: WalkList - The list that owns the walk stack274* Op - The control Op275*276* RETURN: Status277*278* DESCRIPTION: Handles all control ops encountered during control method279* execution.280*281******************************************************************************/282283ACPI_STATUS284AcpiDsExecEndControlOp (285ACPI_WALK_STATE *WalkState,286ACPI_PARSE_OBJECT *Op)287{288ACPI_STATUS Status = AE_OK;289ACPI_GENERIC_STATE *ControlState;290291292ACPI_FUNCTION_NAME (DsExecEndControlOp);293294295switch (Op->Common.AmlOpcode)296{297case AML_IF_OP:298299ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", Op));300301/*302* Save the result of the predicate in case there is an303* ELSE to come304*/305WalkState->LastPredicate =306(BOOLEAN) WalkState->ControlState->Common.Value;307308/*309* Pop the control state that was created at the start310* of the IF and free it311*/312ControlState = AcpiUtPopGenericState (&WalkState->ControlState);313AcpiUtDeleteGenericState (ControlState);314break;315316case AML_ELSE_OP:317318break;319320case AML_WHILE_OP:321322ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", Op));323324ControlState = WalkState->ControlState;325if (ControlState->Common.Value)326{327/* Predicate was true, the body of the loop was just executed */328329/*330* This infinite loop detection mechanism allows the interpreter331* to escape possibly infinite loops. This can occur in poorly332* written AML when the hardware does not respond within a while333* loop and the loop does not implement a timeout.334*/335if (ACPI_TIME_AFTER (AcpiOsGetTimer (),336ControlState->Control.LoopTimeout))337{338Status = AE_AML_LOOP_TIMEOUT;339break;340}341342/*343* Go back and evaluate the predicate and maybe execute the loop344* another time345*/346Status = AE_CTRL_PENDING;347WalkState->AmlLastWhile =348ControlState->Control.AmlPredicateStart;349break;350}351352/* Predicate was false, terminate this while loop */353354ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,355"[WHILE_OP] termination! Op=%p\n",Op));356357/* Pop this control state and free it */358359ControlState = AcpiUtPopGenericState (&WalkState->ControlState);360AcpiUtDeleteGenericState (ControlState);361break;362363case AML_RETURN_OP:364365ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,366"[RETURN_OP] Op=%p Arg=%p\n",Op, Op->Common.Value.Arg));367368/*369* One optional operand -- the return value370* It can be either an immediate operand or a result that371* has been bubbled up the tree372*/373if (Op->Common.Value.Arg)374{375/* Since we have a real Return(), delete any implicit return */376377AcpiDsClearImplicitReturn (WalkState);378379/* Return statement has an immediate operand */380381Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);382if (ACPI_FAILURE (Status))383{384return (Status);385}386387/*388* If value being returned is a Reference (such as389* an arg or local), resolve it now because it may390* cease to exist at the end of the method.391*/392Status = AcpiExResolveToValue (393&WalkState->Operands [0], WalkState);394if (ACPI_FAILURE (Status))395{396return (Status);397}398399/*400* Get the return value and save as the last result401* value. This is the only place where WalkState->ReturnDesc402* is set to anything other than zero!403*/404WalkState->ReturnDesc = WalkState->Operands[0];405}406else if (WalkState->ResultCount)407{408/* Since we have a real Return(), delete any implicit return */409410AcpiDsClearImplicitReturn (WalkState);411412/*413* The return value has come from a previous calculation.414*415* If value being returned is a Reference (such as416* an arg or local), resolve it now because it may417* cease to exist at the end of the method.418*419* Allow references created by the Index operator to return420* unchanged.421*/422if ((ACPI_GET_DESCRIPTOR_TYPE (WalkState->Results->Results.ObjDesc[0]) ==423ACPI_DESC_TYPE_OPERAND) &&424((WalkState->Results->Results.ObjDesc [0])->Common.Type ==425ACPI_TYPE_LOCAL_REFERENCE) &&426((WalkState->Results->Results.ObjDesc [0])->Reference.Class !=427ACPI_REFCLASS_INDEX))428{429Status = AcpiExResolveToValue (430&WalkState->Results->Results.ObjDesc [0], WalkState);431if (ACPI_FAILURE (Status))432{433return (Status);434}435}436437WalkState->ReturnDesc = WalkState->Results->Results.ObjDesc [0];438}439else440{441/* No return operand */442443if (WalkState->NumOperands)444{445AcpiUtRemoveReference (WalkState->Operands [0]);446}447448WalkState->Operands[0] = NULL;449WalkState->NumOperands = 0;450WalkState->ReturnDesc = NULL;451}452453454ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,455"Completed RETURN_OP State=%p, RetVal=%p\n",456WalkState, WalkState->ReturnDesc));457458/* End the control method execution right now */459460Status = AE_CTRL_TERMINATE;461break;462463case AML_NOOP_OP:464465/* Just do nothing! */466467break;468469case AML_BREAKPOINT_OP:470471AcpiDbSignalBreakPoint (WalkState);472473/* Call to the OSL in case OS wants a piece of the action */474475Status = AcpiOsSignal (ACPI_SIGNAL_BREAKPOINT,476"Executed AML Breakpoint opcode");477break;478479case AML_BREAK_OP:480case AML_CONTINUE_OP: /* ACPI 2.0 */481482/* Pop and delete control states until we find a while */483484while (WalkState->ControlState &&485(WalkState->ControlState->Control.Opcode != AML_WHILE_OP))486{487ControlState = AcpiUtPopGenericState (&WalkState->ControlState);488AcpiUtDeleteGenericState (ControlState);489}490491/* No while found? */492493if (!WalkState->ControlState)494{495return (AE_AML_NO_WHILE);496}497498/* Was: WalkState->AmlLastWhile = WalkState->ControlState->Control.AmlPredicateStart; */499500WalkState->AmlLastWhile =501WalkState->ControlState->Control.PackageEnd;502503/* Return status depending on opcode */504505if (Op->Common.AmlOpcode == AML_BREAK_OP)506{507Status = AE_CTRL_BREAK;508}509else510{511Status = AE_CTRL_CONTINUE;512}513break;514515default:516517ACPI_ERROR ((AE_INFO, "Unknown control opcode=0x%X Op=%p",518Op->Common.AmlOpcode, Op));519520Status = AE_AML_BAD_OPCODE;521break;522}523524return (Status);525}526527528