xref: /titanic_44/usr/src/uts/intel/io/acpica/dispatcher/dscontrol.c (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
1 /******************************************************************************
2  *
3  * Module Name: dscontrol - Support for execution control opcodes -
4  *                          if/else/while/return
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2016, 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 MERCHANTIBILITY 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 
70 ACPI_STATUS
71 AcpiDsExecBeginControlOp (
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         /*lint -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 
130 
131         /* Push the control state on this walk's control stack */
132 
133         AcpiUtPushGenericState (&WalkState->ControlState, ControlState);
134         break;
135 
136     case AML_ELSE_OP:
137 
138         /* Predicate is in the state object */
139         /* If predicate is true, the IF was executed, ignore ELSE part */
140 
141         if (WalkState->LastPredicate)
142         {
143             Status = AE_CTRL_TRUE;
144         }
145 
146         break;
147 
148     case AML_RETURN_OP:
149 
150         break;
151 
152     default:
153 
154         break;
155     }
156 
157     return (Status);
158 }
159 
160 
161 /*******************************************************************************
162  *
163  * FUNCTION:    AcpiDsExecEndControlOp
164  *
165  * PARAMETERS:  WalkList        - The list that owns the walk stack
166  *              Op              - The control Op
167  *
168  * RETURN:      Status
169  *
170  * DESCRIPTION: Handles all control ops encountered during control method
171  *              execution.
172  *
173  ******************************************************************************/
174 
175 ACPI_STATUS
176 AcpiDsExecEndControlOp (
177     ACPI_WALK_STATE         *WalkState,
178     ACPI_PARSE_OBJECT       *Op)
179 {
180     ACPI_STATUS             Status = AE_OK;
181     ACPI_GENERIC_STATE      *ControlState;
182 
183 
184     ACPI_FUNCTION_NAME (DsExecEndControlOp);
185 
186 
187     switch (Op->Common.AmlOpcode)
188     {
189     case AML_IF_OP:
190 
191         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", Op));
192 
193         /*
194          * Save the result of the predicate in case there is an
195          * ELSE to come
196          */
197         WalkState->LastPredicate =
198             (BOOLEAN) WalkState->ControlState->Common.Value;
199 
200         /*
201          * Pop the control state that was created at the start
202          * of the IF and free it
203          */
204         ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
205         AcpiUtDeleteGenericState (ControlState);
206         break;
207 
208     case AML_ELSE_OP:
209 
210         break;
211 
212     case AML_WHILE_OP:
213 
214         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", Op));
215 
216         ControlState = WalkState->ControlState;
217         if (ControlState->Common.Value)
218         {
219             /* Predicate was true, the body of the loop was just executed */
220 
221             /*
222              * This loop counter mechanism allows the interpreter to escape
223              * possibly infinite loops. This can occur in poorly written AML
224              * when the hardware does not respond within a while loop and the
225              * loop does not implement a timeout.
226              */
227             ControlState->Control.LoopCount++;
228             if (ControlState->Control.LoopCount > AcpiGbl_MaxLoopIterations)
229             {
230                 Status = AE_AML_INFINITE_LOOP;
231                 break;
232             }
233 
234             /*
235              * Go back and evaluate the predicate and maybe execute the loop
236              * another time
237              */
238             Status = AE_CTRL_PENDING;
239             WalkState->AmlLastWhile =
240                 ControlState->Control.AmlPredicateStart;
241             break;
242         }
243 
244         /* Predicate was false, terminate this while loop */
245 
246         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
247             "[WHILE_OP] termination! Op=%p\n",Op));
248 
249         /* Pop this control state and free it */
250 
251         ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
252         AcpiUtDeleteGenericState (ControlState);
253         break;
254 
255     case AML_RETURN_OP:
256 
257         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
258             "[RETURN_OP] Op=%p Arg=%p\n",Op, Op->Common.Value.Arg));
259 
260         /*
261          * One optional operand -- the return value
262          * It can be either an immediate operand or a result that
263          * has been bubbled up the tree
264          */
265         if (Op->Common.Value.Arg)
266         {
267             /* Since we have a real Return(), delete any implicit return */
268 
269             AcpiDsClearImplicitReturn (WalkState);
270 
271             /* Return statement has an immediate operand */
272 
273             Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);
274             if (ACPI_FAILURE (Status))
275             {
276                 return (Status);
277             }
278 
279             /*
280              * If value being returned is a Reference (such as
281              * an arg or local), resolve it now because it may
282              * cease to exist at the end of the method.
283              */
284             Status = AcpiExResolveToValue (
285                 &WalkState->Operands [0], WalkState);
286             if (ACPI_FAILURE (Status))
287             {
288                 return (Status);
289             }
290 
291             /*
292              * Get the return value and save as the last result
293              * value. This is the only place where WalkState->ReturnDesc
294              * is set to anything other than zero!
295              */
296             WalkState->ReturnDesc = WalkState->Operands[0];
297         }
298         else if (WalkState->ResultCount)
299         {
300             /* Since we have a real Return(), delete any implicit return */
301 
302             AcpiDsClearImplicitReturn (WalkState);
303 
304             /*
305              * The return value has come from a previous calculation.
306              *
307              * If value being returned is a Reference (such as
308              * an arg or local), resolve it now because it may
309              * cease to exist at the end of the method.
310              *
311              * Allow references created by the Index operator to return
312              * unchanged.
313              */
314             if ((ACPI_GET_DESCRIPTOR_TYPE (WalkState->Results->Results.ObjDesc[0]) ==
315                     ACPI_DESC_TYPE_OPERAND) &&
316                 ((WalkState->Results->Results.ObjDesc [0])->Common.Type ==
317                     ACPI_TYPE_LOCAL_REFERENCE) &&
318                 ((WalkState->Results->Results.ObjDesc [0])->Reference.Class !=
319                     ACPI_REFCLASS_INDEX))
320             {
321                 Status = AcpiExResolveToValue (
322                     &WalkState->Results->Results.ObjDesc [0], WalkState);
323                 if (ACPI_FAILURE (Status))
324                 {
325                     return (Status);
326                 }
327             }
328 
329             WalkState->ReturnDesc = WalkState->Results->Results.ObjDesc [0];
330         }
331         else
332         {
333             /* No return operand */
334 
335             if (WalkState->NumOperands)
336             {
337                 AcpiUtRemoveReference (WalkState->Operands [0]);
338             }
339 
340             WalkState->Operands[0] = NULL;
341             WalkState->NumOperands = 0;
342             WalkState->ReturnDesc = NULL;
343         }
344 
345 
346         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
347             "Completed RETURN_OP State=%p, RetVal=%p\n",
348             WalkState, WalkState->ReturnDesc));
349 
350         /* End the control method execution right now */
351 
352         Status = AE_CTRL_TERMINATE;
353         break;
354 
355     case AML_NOOP_OP:
356 
357         /* Just do nothing! */
358 
359         break;
360 
361     case AML_BREAK_POINT_OP:
362 
363 #ifdef ACPI_DEBUGGER
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             "Executed AML Breakpoint opcode");
370 #endif
371         break;
372 
373     case AML_BREAK_OP:
374     case AML_CONTINUE_OP: /* ACPI 2.0 */
375 
376         /* Pop and delete control states until we find a while */
377 
378         while (WalkState->ControlState &&
379                 (WalkState->ControlState->Control.Opcode != AML_WHILE_OP))
380         {
381             ControlState = AcpiUtPopGenericState (&WalkState->ControlState);
382             AcpiUtDeleteGenericState (ControlState);
383         }
384 
385         /* No while found? */
386 
387         if (!WalkState->ControlState)
388         {
389             return (AE_AML_NO_WHILE);
390         }
391 
392         /* Was: WalkState->AmlLastWhile = WalkState->ControlState->Control.AmlPredicateStart; */
393 
394         WalkState->AmlLastWhile =
395             WalkState->ControlState->Control.PackageEnd;
396 
397         /* Return status depending on opcode */
398 
399         if (Op->Common.AmlOpcode == AML_BREAK_OP)
400         {
401             Status = AE_CTRL_BREAK;
402         }
403         else
404         {
405             Status = AE_CTRL_CONTINUE;
406         }
407         break;
408 
409     default:
410 
411         ACPI_ERROR ((AE_INFO, "Unknown control opcode=0x%X Op=%p",
412             Op->Common.AmlOpcode, Op));
413 
414         Status = AE_AML_BAD_OPCODE;
415         break;
416     }
417 
418     return (Status);
419 }
420