Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: dscontrol - Support for execution control opcodes -
0005  *                          if/else/while/return
0006  *
0007  * Copyright (C) 2000 - 2022, Intel Corp.
0008  *
0009  *****************************************************************************/
0010 
0011 #include <acpi/acpi.h>
0012 #include "accommon.h"
0013 #include "amlcode.h"
0014 #include "acdispat.h"
0015 #include "acinterp.h"
0016 #include "acdebug.h"
0017 
0018 #define _COMPONENT          ACPI_DISPATCHER
0019 ACPI_MODULE_NAME("dscontrol")
0020 
0021 /*******************************************************************************
0022  *
0023  * FUNCTION:    acpi_ds_exec_begin_control_op
0024  *
0025  * PARAMETERS:  walk_list       - The list that owns the walk stack
0026  *              op              - The control Op
0027  *
0028  * RETURN:      Status
0029  *
0030  * DESCRIPTION: Handles all control ops encountered during control method
0031  *              execution.
0032  *
0033  ******************************************************************************/
0034 acpi_status
0035 acpi_ds_exec_begin_control_op(struct acpi_walk_state *walk_state,
0036                   union acpi_parse_object *op)
0037 {
0038     acpi_status status = AE_OK;
0039     union acpi_generic_state *control_state;
0040 
0041     ACPI_FUNCTION_NAME(ds_exec_begin_control_op);
0042 
0043     ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",
0044               op, op->common.aml_opcode, walk_state));
0045 
0046     switch (op->common.aml_opcode) {
0047     case AML_WHILE_OP:
0048         /*
0049          * If this is an additional iteration of a while loop, continue.
0050          * There is no need to allocate a new control state.
0051          */
0052         if (walk_state->control_state) {
0053             if (walk_state->control_state->control.
0054                 aml_predicate_start ==
0055                 (walk_state->parser_state.aml - 1)) {
0056 
0057                 /* Reset the state to start-of-loop */
0058 
0059                 walk_state->control_state->common.state =
0060                     ACPI_CONTROL_CONDITIONAL_EXECUTING;
0061                 break;
0062             }
0063         }
0064 
0065         ACPI_FALLTHROUGH;
0066 
0067     case AML_IF_OP:
0068         /*
0069          * IF/WHILE: Create a new control state to manage these
0070          * constructs. We need to manage these as a stack, in order
0071          * to handle nesting.
0072          */
0073         control_state = acpi_ut_create_control_state();
0074         if (!control_state) {
0075             status = AE_NO_MEMORY;
0076             break;
0077         }
0078         /*
0079          * Save a pointer to the predicate for multiple executions
0080          * of a loop
0081          */
0082         control_state->control.aml_predicate_start =
0083             walk_state->parser_state.aml - 1;
0084         control_state->control.package_end =
0085             walk_state->parser_state.pkg_end;
0086         control_state->control.opcode = op->common.aml_opcode;
0087         control_state->control.loop_timeout = acpi_os_get_timer() +
0088             ((u64)acpi_gbl_max_loop_iterations * ACPI_100NSEC_PER_SEC);
0089 
0090         /* Push the control state on this walk's control stack */
0091 
0092         acpi_ut_push_generic_state(&walk_state->control_state,
0093                        control_state);
0094         break;
0095 
0096     case AML_ELSE_OP:
0097 
0098         /* Predicate is in the state object */
0099         /* If predicate is true, the IF was executed, ignore ELSE part */
0100 
0101         if (walk_state->last_predicate) {
0102             status = AE_CTRL_TRUE;
0103         }
0104 
0105         break;
0106 
0107     case AML_RETURN_OP:
0108 
0109         break;
0110 
0111     default:
0112 
0113         break;
0114     }
0115 
0116     return (status);
0117 }
0118 
0119 /*******************************************************************************
0120  *
0121  * FUNCTION:    acpi_ds_exec_end_control_op
0122  *
0123  * PARAMETERS:  walk_list       - The list that owns the walk stack
0124  *              op              - The control Op
0125  *
0126  * RETURN:      Status
0127  *
0128  * DESCRIPTION: Handles all control ops encountered during control method
0129  *              execution.
0130  *
0131  ******************************************************************************/
0132 
0133 acpi_status
0134 acpi_ds_exec_end_control_op(struct acpi_walk_state *walk_state,
0135                 union acpi_parse_object *op)
0136 {
0137     acpi_status status = AE_OK;
0138     union acpi_generic_state *control_state;
0139 
0140     ACPI_FUNCTION_NAME(ds_exec_end_control_op);
0141 
0142     switch (op->common.aml_opcode) {
0143     case AML_IF_OP:
0144 
0145         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", op));
0146 
0147         /*
0148          * Save the result of the predicate in case there is an
0149          * ELSE to come
0150          */
0151         walk_state->last_predicate =
0152             (u8)walk_state->control_state->common.value;
0153 
0154         /*
0155          * Pop the control state that was created at the start
0156          * of the IF and free it
0157          */
0158         control_state =
0159             acpi_ut_pop_generic_state(&walk_state->control_state);
0160         acpi_ut_delete_generic_state(control_state);
0161         break;
0162 
0163     case AML_ELSE_OP:
0164 
0165         break;
0166 
0167     case AML_WHILE_OP:
0168 
0169         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", op));
0170 
0171         control_state = walk_state->control_state;
0172         if (control_state->common.value) {
0173 
0174             /* Predicate was true, the body of the loop was just executed */
0175 
0176             /*
0177              * This infinite loop detection mechanism allows the interpreter
0178              * to escape possibly infinite loops. This can occur in poorly
0179              * written AML when the hardware does not respond within a while
0180              * loop and the loop does not implement a timeout.
0181              */
0182             if (ACPI_TIME_AFTER(acpi_os_get_timer(),
0183                         control_state->control.
0184                         loop_timeout)) {
0185                 status = AE_AML_LOOP_TIMEOUT;
0186                 break;
0187             }
0188 
0189             /*
0190              * Go back and evaluate the predicate and maybe execute the loop
0191              * another time
0192              */
0193             status = AE_CTRL_PENDING;
0194             walk_state->aml_last_while =
0195                 control_state->control.aml_predicate_start;
0196             break;
0197         }
0198 
0199         /* Predicate was false, terminate this while loop */
0200 
0201         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
0202                   "[WHILE_OP] termination! Op=%p\n", op));
0203 
0204         /* Pop this control state and free it */
0205 
0206         control_state =
0207             acpi_ut_pop_generic_state(&walk_state->control_state);
0208         acpi_ut_delete_generic_state(control_state);
0209         break;
0210 
0211     case AML_RETURN_OP:
0212 
0213         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
0214                   "[RETURN_OP] Op=%p Arg=%p\n", op,
0215                   op->common.value.arg));
0216 
0217         /*
0218          * One optional operand -- the return value
0219          * It can be either an immediate operand or a result that
0220          * has been bubbled up the tree
0221          */
0222         if (op->common.value.arg) {
0223 
0224             /* Since we have a real Return(), delete any implicit return */
0225 
0226             acpi_ds_clear_implicit_return(walk_state);
0227 
0228             /* Return statement has an immediate operand */
0229 
0230             status =
0231                 acpi_ds_create_operands(walk_state,
0232                             op->common.value.arg);
0233             if (ACPI_FAILURE(status)) {
0234                 return (status);
0235             }
0236 
0237             /*
0238              * If value being returned is a Reference (such as
0239              * an arg or local), resolve it now because it may
0240              * cease to exist at the end of the method.
0241              */
0242             status =
0243                 acpi_ex_resolve_to_value(&walk_state->operands[0],
0244                              walk_state);
0245             if (ACPI_FAILURE(status)) {
0246                 return (status);
0247             }
0248 
0249             /*
0250              * Get the return value and save as the last result
0251              * value. This is the only place where walk_state->return_desc
0252              * is set to anything other than zero!
0253              */
0254             walk_state->return_desc = walk_state->operands[0];
0255         } else if (walk_state->result_count) {
0256 
0257             /* Since we have a real Return(), delete any implicit return */
0258 
0259             acpi_ds_clear_implicit_return(walk_state);
0260 
0261             /*
0262              * The return value has come from a previous calculation.
0263              *
0264              * If value being returned is a Reference (such as
0265              * an arg or local), resolve it now because it may
0266              * cease to exist at the end of the method.
0267              *
0268              * Allow references created by the Index operator to return
0269              * unchanged.
0270              */
0271             if ((ACPI_GET_DESCRIPTOR_TYPE
0272                  (walk_state->results->results.obj_desc[0]) ==
0273                  ACPI_DESC_TYPE_OPERAND)
0274                 && ((walk_state->results->results.obj_desc[0])->
0275                 common.type == ACPI_TYPE_LOCAL_REFERENCE)
0276                 && ((walk_state->results->results.obj_desc[0])->
0277                 reference.class != ACPI_REFCLASS_INDEX)) {
0278                 status =
0279                     acpi_ex_resolve_to_value(&walk_state->
0280                                  results->results.
0281                                  obj_desc[0],
0282                                  walk_state);
0283                 if (ACPI_FAILURE(status)) {
0284                     return (status);
0285                 }
0286             }
0287 
0288             walk_state->return_desc =
0289                 walk_state->results->results.obj_desc[0];
0290         } else {
0291             /* No return operand */
0292 
0293             if (walk_state->num_operands) {
0294                 acpi_ut_remove_reference(walk_state->
0295                              operands[0]);
0296             }
0297 
0298             walk_state->operands[0] = NULL;
0299             walk_state->num_operands = 0;
0300             walk_state->return_desc = NULL;
0301         }
0302 
0303         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
0304                   "Completed RETURN_OP State=%p, RetVal=%p\n",
0305                   walk_state, walk_state->return_desc));
0306 
0307         /* End the control method execution right now */
0308 
0309         status = AE_CTRL_TERMINATE;
0310         break;
0311 
0312     case AML_NOOP_OP:
0313 
0314         /* Just do nothing! */
0315 
0316         break;
0317 
0318     case AML_BREAKPOINT_OP:
0319 
0320         acpi_db_signal_break_point(walk_state);
0321 
0322         /* Call to the OSL in case OS wants a piece of the action */
0323 
0324         status = acpi_os_signal(ACPI_SIGNAL_BREAKPOINT,
0325                     "Executed AML Breakpoint opcode");
0326         break;
0327 
0328     case AML_BREAK_OP:
0329     case AML_CONTINUE_OP:   /* ACPI 2.0 */
0330 
0331         /* Pop and delete control states until we find a while */
0332 
0333         while (walk_state->control_state &&
0334                (walk_state->control_state->control.opcode !=
0335             AML_WHILE_OP)) {
0336             control_state =
0337                 acpi_ut_pop_generic_state(&walk_state->
0338                               control_state);
0339             acpi_ut_delete_generic_state(control_state);
0340         }
0341 
0342         /* No while found? */
0343 
0344         if (!walk_state->control_state) {
0345             return (AE_AML_NO_WHILE);
0346         }
0347 
0348         /* Was: walk_state->aml_last_while = walk_state->control_state->Control.aml_predicate_start; */
0349 
0350         walk_state->aml_last_while =
0351             walk_state->control_state->control.package_end;
0352 
0353         /* Return status depending on opcode */
0354 
0355         if (op->common.aml_opcode == AML_BREAK_OP) {
0356             status = AE_CTRL_BREAK;
0357         } else {
0358             status = AE_CTRL_CONTINUE;
0359         }
0360         break;
0361 
0362     default:
0363 
0364         ACPI_ERROR((AE_INFO, "Unknown control opcode=0x%X Op=%p",
0365                 op->common.aml_opcode, op));
0366 
0367         status = AE_AML_BAD_OPCODE;
0368         break;
0369     }
0370 
0371     return (status);
0372 }