// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.01/******************************************************************************2*3* Module Name: dscontrol - Support for execution control opcodes -4* if/else/while/return5*6* Copyright (C) 2000 - 2025, Intel Corp.7*8*****************************************************************************/910#include <acpi/acpi.h>11#include "accommon.h"12#include "amlcode.h"13#include "acdispat.h"14#include "acinterp.h"15#include "acdebug.h"1617#define _COMPONENT ACPI_DISPATCHER18ACPI_MODULE_NAME("dscontrol")1920/*******************************************************************************21*22* FUNCTION: acpi_ds_exec_begin_control_op23*24* PARAMETERS: walk_list - The list that owns the walk stack25* op - The control Op26*27* RETURN: Status28*29* DESCRIPTION: Handles all control ops encountered during control method30* execution.31*32******************************************************************************/33acpi_status34acpi_ds_exec_begin_control_op(struct acpi_walk_state *walk_state,35union acpi_parse_object *op)36{37acpi_status status = AE_OK;38union acpi_generic_state *control_state;3940ACPI_FUNCTION_NAME(ds_exec_begin_control_op);4142ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",43op, op->common.aml_opcode, walk_state));4445switch (op->common.aml_opcode) {46case AML_WHILE_OP:47/*48* If this is an additional iteration of a while loop, continue.49* There is no need to allocate a new control state.50*/51if (walk_state->control_state) {52if (walk_state->control_state->control.53aml_predicate_start ==54(walk_state->parser_state.aml - 1)) {5556/* Reset the state to start-of-loop */5758walk_state->control_state->common.state =59ACPI_CONTROL_CONDITIONAL_EXECUTING;60break;61}62}6364ACPI_FALLTHROUGH;6566case AML_IF_OP:67/*68* IF/WHILE: Create a new control state to manage these69* constructs. We need to manage these as a stack, in order70* to handle nesting.71*/72control_state = acpi_ut_create_control_state();73if (!control_state) {74status = AE_NO_MEMORY;75break;76}77/*78* Save a pointer to the predicate for multiple executions79* of a loop80*/81control_state->control.aml_predicate_start =82walk_state->parser_state.aml - 1;83control_state->control.package_end =84walk_state->parser_state.pkg_end;85control_state->control.opcode = op->common.aml_opcode;86control_state->control.loop_timeout = acpi_os_get_timer() +87((u64)acpi_gbl_max_loop_iterations * ACPI_100NSEC_PER_SEC);8889/* Push the control state on this walk's control stack */9091acpi_ut_push_generic_state(&walk_state->control_state,92control_state);93break;9495case AML_ELSE_OP:9697/* Predicate is in the state object */98/* If predicate is true, the IF was executed, ignore ELSE part */99100if (walk_state->last_predicate) {101status = AE_CTRL_TRUE;102}103104break;105106case AML_RETURN_OP:107108break;109110default:111112break;113}114115return (status);116}117118/*******************************************************************************119*120* FUNCTION: acpi_ds_exec_end_control_op121*122* PARAMETERS: walk_list - The list that owns the walk stack123* op - The control Op124*125* RETURN: Status126*127* DESCRIPTION: Handles all control ops encountered during control method128* execution.129*130******************************************************************************/131132acpi_status133acpi_ds_exec_end_control_op(struct acpi_walk_state *walk_state,134union acpi_parse_object *op)135{136acpi_status status = AE_OK;137union acpi_generic_state *control_state;138139ACPI_FUNCTION_NAME(ds_exec_end_control_op);140141switch (op->common.aml_opcode) {142case AML_IF_OP:143144ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", op));145146/*147* Save the result of the predicate in case there is an148* ELSE to come149*/150walk_state->last_predicate =151(u8)walk_state->control_state->common.value;152153/*154* Pop the control state that was created at the start155* of the IF and free it156*/157control_state =158acpi_ut_pop_generic_state(&walk_state->control_state);159acpi_ut_delete_generic_state(control_state);160break;161162case AML_ELSE_OP:163164break;165166case AML_WHILE_OP:167168ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", op));169170control_state = walk_state->control_state;171if (control_state->common.value) {172173/* Predicate was true, the body of the loop was just executed */174175/*176* This infinite loop detection mechanism allows the interpreter177* to escape possibly infinite loops. This can occur in poorly178* written AML when the hardware does not respond within a while179* loop and the loop does not implement a timeout.180*/181if (ACPI_TIME_AFTER(acpi_os_get_timer(),182control_state->control.183loop_timeout)) {184status = AE_AML_LOOP_TIMEOUT;185break;186}187188/*189* Go back and evaluate the predicate and maybe execute the loop190* another time191*/192status = AE_CTRL_PENDING;193walk_state->aml_last_while =194control_state->control.aml_predicate_start;195break;196}197198/* Predicate was false, terminate this while loop */199200ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,201"[WHILE_OP] termination! Op=%p\n", op));202203/* Pop this control state and free it */204205control_state =206acpi_ut_pop_generic_state(&walk_state->control_state);207acpi_ut_delete_generic_state(control_state);208break;209210case AML_RETURN_OP:211212ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,213"[RETURN_OP] Op=%p Arg=%p\n", op,214op->common.value.arg));215216/*217* One optional operand -- the return value218* It can be either an immediate operand or a result that219* has been bubbled up the tree220*/221if (op->common.value.arg) {222223/* Since we have a real Return(), delete any implicit return */224225acpi_ds_clear_implicit_return(walk_state);226227/* Return statement has an immediate operand */228229status =230acpi_ds_create_operands(walk_state,231op->common.value.arg);232if (ACPI_FAILURE(status)) {233return (status);234}235236/*237* If value being returned is a Reference (such as238* an arg or local), resolve it now because it may239* cease to exist at the end of the method.240*/241status =242acpi_ex_resolve_to_value(&walk_state->operands[0],243walk_state);244if (ACPI_FAILURE(status)) {245return (status);246}247248/*249* Get the return value and save as the last result250* value. This is the only place where walk_state->return_desc251* is set to anything other than zero!252*/253walk_state->return_desc = walk_state->operands[0];254} else if (walk_state->result_count) {255256/* Since we have a real Return(), delete any implicit return */257258acpi_ds_clear_implicit_return(walk_state);259260/*261* The return value has come from a previous calculation.262*263* If value being returned is a Reference (such as264* an arg or local), resolve it now because it may265* cease to exist at the end of the method.266*267* Allow references created by the Index operator to return268* unchanged.269*/270if ((ACPI_GET_DESCRIPTOR_TYPE271(walk_state->results->results.obj_desc[0]) ==272ACPI_DESC_TYPE_OPERAND)273&& ((walk_state->results->results.obj_desc[0])->274common.type == ACPI_TYPE_LOCAL_REFERENCE)275&& ((walk_state->results->results.obj_desc[0])->276reference.class != ACPI_REFCLASS_INDEX)) {277status =278acpi_ex_resolve_to_value(&walk_state->279results->results.280obj_desc[0],281walk_state);282if (ACPI_FAILURE(status)) {283return (status);284}285}286287walk_state->return_desc =288walk_state->results->results.obj_desc[0];289} else {290/* No return operand */291292if (walk_state->num_operands) {293acpi_ut_remove_reference(walk_state->294operands[0]);295}296297walk_state->operands[0] = NULL;298walk_state->num_operands = 0;299walk_state->return_desc = NULL;300}301302ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,303"Completed RETURN_OP State=%p, RetVal=%p\n",304walk_state, walk_state->return_desc));305306/* End the control method execution right now */307308status = AE_CTRL_TERMINATE;309break;310311case AML_NOOP_OP:312313/* Just do nothing! */314315break;316317case AML_BREAKPOINT_OP:318319acpi_db_signal_break_point(walk_state);320321/* Call to the OSL in case OS wants a piece of the action */322323status = acpi_os_signal(ACPI_SIGNAL_BREAKPOINT,324"Executed AML Breakpoint opcode");325break;326327case AML_BREAK_OP:328case AML_CONTINUE_OP: /* ACPI 2.0 */329330/* Pop and delete control states until we find a while */331332while (walk_state->control_state &&333(walk_state->control_state->control.opcode !=334AML_WHILE_OP)) {335control_state =336acpi_ut_pop_generic_state(&walk_state->337control_state);338acpi_ut_delete_generic_state(control_state);339}340341/* No while found? */342343if (!walk_state->control_state) {344return (AE_AML_NO_WHILE);345}346347/* Was: walk_state->aml_last_while = walk_state->control_state->Control.aml_predicate_start; */348349walk_state->aml_last_while =350walk_state->control_state->control.package_end;351352/* Return status depending on opcode */353354if (op->common.aml_opcode == AML_BREAK_OP) {355status = AE_CTRL_BREAK;356} else {357status = AE_CTRL_CONTINUE;358}359break;360361default:362363ACPI_ERROR((AE_INFO, "Unknown control opcode=0x%X Op=%p",364op->common.aml_opcode, op));365366status = AE_AML_BAD_OPCODE;367break;368}369370return (status);371}372373374