1/******************************************************************************
2 *
3 * Module Name: dscontrol - Support for execution control opcodes -
4 *                          if/else/while/return
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2023, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions, and the following disclaimer,
17 *    without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 *    substantially similar to the "NO WARRANTY" disclaimer below
20 *    ("Disclaimer") and any redistribution must be conditioned upon
21 *    including a substantially similar Disclaimer requirement for further
22 *    binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 *    of any contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include "acpi.h"
46#include "accommon.h"
47#include "amlcode.h"
48#include "acdispat.h"
49#include "acinterp.h"
50#include "acdebug.h"
51
52#define _COMPONENT          ACPI_DISPATCHER
53        ACPI_MODULE_NAME    ("dscontrol")
54
55
56/*******************************************************************************
57 *
58 * FUNCTION:    AcpiDsExecBeginControlOp
59 *
60 * PARAMETERS:  WalkList        - The list that owns the walk stack
61 *              Op              - The control Op
62 *
63 * RETURN:      Status
64 *
65 * DESCRIPTION: Handles all control ops encountered during control method
66 *              execution.
67 *
68 ******************************************************************************/
69
70ACPI_STATUS
71AcpiDsExecBeginControlOp (
72    ACPI_WALK_STATE         *WalkState,
73    ACPI_PARSE_OBJECT       *Op)
74{
75    ACPI_STATUS             Status = AE_OK;
76    ACPI_GENERIC_STATE      *ControlState;
77
78
79    ACPI_FUNCTION_NAME (DsExecBeginControlOp);
80
81
82    ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",
83        Op, Op->Common.AmlOpcode, WalkState));
84
85    switch (Op->Common.AmlOpcode)
86    {
87    case AML_WHILE_OP:
88        /*
89         * If this is an additional iteration of a while loop, continue.
90         * There is no need to allocate a new control state.
91         */
92        if (WalkState->ControlState)
93        {
94            if (WalkState->ControlState->Control.AmlPredicateStart ==
95                (WalkState->ParserState.Aml - 1))
96            {
97                /* Reset the state to start-of-loop */
98
99                WalkState->ControlState->Common.State =
100                    ACPI_CONTROL_CONDITIONAL_EXECUTING;
101                break;
102            }
103        }
104
105        ACPI_FALLTHROUGH;
106
107    case AML_IF_OP:
108        /*
109         * IF/WHILE: Create a new control state to manage these
110         * constructs. We need to manage these as a stack, in order
111         * to handle nesting.
112         */
113        ControlState = AcpiUtCreateControlState ();
114        if (!ControlState)
115        {
116            Status = AE_NO_MEMORY;
117            break;
118        }
119        /*
120         * Save a pointer to the predicate for multiple executions
121         * of a loop
122         */
123        ControlState->Control.AmlPredicateStart =
124            WalkState->ParserState.Aml - 1;
125        ControlState->Control.PackageEnd =
126            WalkState->ParserState.PkgEnd;
127        ControlState->Control.Opcode =
128            Op->Common.AmlOpcode;
129        ControlState->Control.LoopTimeout = AcpiOsGetTimer () +
130           ((UINT64) AcpiGbl_MaxLoopIterations * ACPI_100NSEC_PER_SEC);
131
132        /* Push the control state on this walk's control stack */
133
134        AcpiUtPushGenericState (&WalkState->ControlState, ControlState);
135        break;
136
137    case AML_ELSE_OP:
138
139        /* Predicate is in the state object */
140        /* If predicate is true, the IF was executed, ignore ELSE part */
141
142        if (WalkState->LastPredicate)
143        {
144            Status = AE_CTRL_TRUE;
145        }
146
147        break;
148
149    case AML_RETURN_OP:
150
151        break;
152
153    default:
154
155        break;
156    }
157
158    return (Status);
159}
160
161
162/*******************************************************************************
163 *
164 * FUNCTION:    AcpiDsExecEndControlOp
165 *
166 * PARAMETERS:  WalkList        - The list that owns the walk stack
167 *              Op              - The control Op
168 *
169 * RETURN:      Status
170 *
171 * DESCRIPTION: Handles all control ops encountered during control method
172 *              execution.
173 *
174 ******************************************************************************/
175
176ACPI_STATUS
177AcpiDsExecEndControlOp (
178    ACPI_WALK_STATE         *WalkState,
179    ACPI_PARSE_OBJECT       *Op)
180{
181    ACPI_STATUS             Status = AE_OK;
182    ACPI_GENERIC_STATE      *ControlState;
183
184
185    ACPI_FUNCTION_NAME (DsExecEndControlOp);
186
187
188    switch (Op->Common.AmlOpcode)
189    {
190    case AML_IF_OP:
191
192        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", Op));
193
194        /*
195         * Save the result of the predicate in case there is an
196         * ELSE to come
197         */
198        WalkState->LastPredicate =
199            (BOOLEAN) WalkState->ControlState->Common.Value;
200
201        /*
202         * Pop the control state that was created at the start
203         * of the IF and free it
204         */
205        ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
206        AcpiUtDeleteGenericState (ControlState);
207        break;
208
209    case AML_ELSE_OP:
210
211        break;
212
213    case AML_WHILE_OP:
214
215        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", Op));
216
217        ControlState = WalkState->ControlState;
218        if (ControlState->Common.Value)
219        {
220            /* Predicate was true, the body of the loop was just executed */
221
222            /*
223             * This infinite loop detection mechanism allows the interpreter
224             * to escape possibly infinite loops. This can occur in poorly
225             * written AML when the hardware does not respond within a while
226             * loop and the loop does not implement a timeout.
227             */
228            if (ACPI_TIME_AFTER (AcpiOsGetTimer (),
229                    ControlState->Control.LoopTimeout))
230            {
231                Status = AE_AML_LOOP_TIMEOUT;
232                break;
233            }
234
235            /*
236             * Go back and evaluate the predicate and maybe execute the loop
237             * another time
238             */
239            Status = AE_CTRL_PENDING;
240            WalkState->AmlLastWhile =
241                ControlState->Control.AmlPredicateStart;
242            break;
243        }
244
245        /* Predicate was false, terminate this while loop */
246
247        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
248            "[WHILE_OP] termination! Op=%p\n",Op));
249
250        /* Pop this control state and free it */
251
252        ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
253        AcpiUtDeleteGenericState (ControlState);
254        break;
255
256    case AML_RETURN_OP:
257
258        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
259            "[RETURN_OP] Op=%p Arg=%p\n",Op, Op->Common.Value.Arg));
260
261        /*
262         * One optional operand -- the return value
263         * It can be either an immediate operand or a result that
264         * has been bubbled up the tree
265         */
266        if (Op->Common.Value.Arg)
267        {
268            /* Since we have a real Return(), delete any implicit return */
269
270            AcpiDsClearImplicitReturn (WalkState);
271
272            /* Return statement has an immediate operand */
273
274            Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);
275            if (ACPI_FAILURE (Status))
276            {
277                return (Status);
278            }
279
280            /*
281             * If value being returned is a Reference (such as
282             * an arg or local), resolve it now because it may
283             * cease to exist at the end of the method.
284             */
285            Status = AcpiExResolveToValue (
286                &WalkState->Operands [0], WalkState);
287            if (ACPI_FAILURE (Status))
288            {
289                return (Status);
290            }
291
292            /*
293             * Get the return value and save as the last result
294             * value. This is the only place where WalkState->ReturnDesc
295             * is set to anything other than zero!
296             */
297            WalkState->ReturnDesc = WalkState->Operands[0];
298        }
299        else if (WalkState->ResultCount)
300        {
301            /* Since we have a real Return(), delete any implicit return */
302
303            AcpiDsClearImplicitReturn (WalkState);
304
305            /*
306             * The return value has come from a previous calculation.
307             *
308             * If value being returned is a Reference (such as
309             * an arg or local), resolve it now because it may
310             * cease to exist at the end of the method.
311             *
312             * Allow references created by the Index operator to return
313             * unchanged.
314             */
315            if ((ACPI_GET_DESCRIPTOR_TYPE (WalkState->Results->Results.ObjDesc[0]) ==
316                    ACPI_DESC_TYPE_OPERAND) &&
317                ((WalkState->Results->Results.ObjDesc [0])->Common.Type ==
318                    ACPI_TYPE_LOCAL_REFERENCE) &&
319                ((WalkState->Results->Results.ObjDesc [0])->Reference.Class !=
320                    ACPI_REFCLASS_INDEX))
321            {
322                Status = AcpiExResolveToValue (
323                    &WalkState->Results->Results.ObjDesc [0], WalkState);
324                if (ACPI_FAILURE (Status))
325                {
326                    return (Status);
327                }
328            }
329
330            WalkState->ReturnDesc = WalkState->Results->Results.ObjDesc [0];
331        }
332        else
333        {
334            /* No return operand */
335
336            if (WalkState->NumOperands)
337            {
338                AcpiUtRemoveReference (WalkState->Operands [0]);
339            }
340
341            WalkState->Operands[0] = NULL;
342            WalkState->NumOperands = 0;
343            WalkState->ReturnDesc = NULL;
344        }
345
346
347        ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
348            "Completed RETURN_OP State=%p, RetVal=%p\n",
349            WalkState, WalkState->ReturnDesc));
350
351        /* End the control method execution right now */
352
353        Status = AE_CTRL_TERMINATE;
354        break;
355
356    case AML_NOOP_OP:
357
358        /* Just do nothing! */
359
360        break;
361
362    case AML_BREAKPOINT_OP:
363
364        AcpiDbSignalBreakPoint (WalkState);
365
366        /* Call to the OSL in case OS wants a piece of the action */
367
368        Status = AcpiOsSignal (ACPI_SIGNAL_BREAKPOINT,
369            __UNCONST("Executed AML Breakpoint opcode"));
370        break;
371
372    case AML_BREAK_OP:
373    case AML_CONTINUE_OP: /* ACPI 2.0 */
374
375        /* Pop and delete control states until we find a while */
376
377        while (WalkState->ControlState &&
378                (WalkState->ControlState->Control.Opcode != AML_WHILE_OP))
379        {
380            ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
381            AcpiUtDeleteGenericState (ControlState);
382        }
383
384        /* No while found? */
385
386        if (!WalkState->ControlState)
387        {
388            return (AE_AML_NO_WHILE);
389        }
390
391        /* Was: WalkState->AmlLastWhile = WalkState->ControlState->Control.AmlPredicateStart; */
392
393        WalkState->AmlLastWhile =
394            WalkState->ControlState->Control.PackageEnd;
395
396        /* Return status depending on opcode */
397
398        if (Op->Common.AmlOpcode == AML_BREAK_OP)
399        {
400            Status = AE_CTRL_BREAK;
401        }
402        else
403        {
404            Status = AE_CTRL_CONTINUE;
405        }
406        break;
407
408    default:
409
410        ACPI_ERROR ((AE_INFO, "Unknown control opcode=0x%X Op=%p",
411            Op->Common.AmlOpcode, Op));
412
413        Status = AE_AML_BAD_OPCODE;
414        break;
415    }
416
417    return (Status);
418}
419