ReactOS  0.4.14-dev-342-gdc047f9
dscontrol.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Module Name: dscontrol - Support for execution control opcodes -
4  * if/else/while/return
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2019, 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 
72  ACPI_WALK_STATE *WalkState,
74 {
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 =
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  {
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  {
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 
178  ACPI_WALK_STATE *WalkState,
179  ACPI_PARSE_OBJECT *Op)
180 {
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  */
229  ControlState->Control.LoopTimeout))
230  {
232  break;
233  }
234 
235  /*
236  * Go back and evaluate the predicate and maybe execute the loop
237  * another time
238  */
240  WalkState->AmlLastWhile =
241  ControlState->Control.AmlPredicateStart;
242  break;
243  }
244 
245  /* Predicate was false, terminate this while loop */
246 
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 
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  */
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]) ==
317  ((WalkState->Results->Results.ObjDesc [0])->Common.Type ==
319  ((WalkState->Results->Results.ObjDesc [0])->Reference.Class !=
321  {
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 
348  "Completed RETURN_OP State=%p, RetVal=%p\n",
349  WalkState, WalkState->ReturnDesc));
350 
351  /* End the control method execution right now */
352 
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 
369  "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  {
401  }
402  else
403  {
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 
414  break;
415  }
416 
417  return (Status);
418 }
ACPI_STATUS AcpiDsCreateOperands(ACPI_WALK_STATE *WalkState, ACPI_PARSE_OBJECT *FirstArg)
Definition: dsutils.c:753
ACPI_PARSE_OBJ_COMMON Common
Definition: aclocal.h:1078
#define AML_ELSE_OP
Definition: amlcode.h:135
ACPI_STATE_COMMON UINT16 Opcode
Definition: aclocal.h:725
void AcpiUtDeleteGenericState(ACPI_GENERIC_STATE *State)
Definition: utstate.c:340
ACPI_GENERIC_STATE * Results
Definition: acstruct.h:122
UINT64 AcpiOsGetTimer(void)
Definition: osl.c:884
#define AE_AML_NO_WHILE
Definition: acexcep.h:205
#define AE_NO_MEMORY
Definition: acexcep.h:112
#define AML_IF_OP
Definition: amlcode.h:134
UINT8 NumOperands
Definition: acstruct.h:80
#define AML_WHILE_OP
Definition: amlcode.h:136
ACPI_STATUS AcpiOsSignal(UINT32 Function, void *Info)
Definition: osl.c:904
ACPI_STATE_COMMON union acpi_operand_object * ObjDesc[ACPI_RESULTS_FRAME_OBJ_NUM]
Definition: aclocal.h:779
#define ACPI_TYPE_LOCAL_REFERENCE
Definition: actypes.h:710
union acpi_operand_object * ReturnDesc
Definition: acstruct.h:123
void AcpiUtPushGenericState(ACPI_GENERIC_STATE **ListHead, ACPI_GENERIC_STATE *State)
Definition: utstate.c:65
UINT32 ACPI_STATUS
Definition: actypes.h:460
ACPI_GENERIC_STATE * ControlState
Definition: acstruct.h:110
#define AE_CTRL_PENDING
Definition: acexcep.h:225
ACPI_GENERIC_STATE * AcpiUtPopGenericState(ACPI_GENERIC_STATE **ListHead)
Definition: utstate.c:93
#define ACPI_FAILURE(a)
Definition: acexcep.h:95
#define AML_BREAK_OP
Definition: amlcode.h:139
#define AE_CTRL_TERMINATE
Definition: acexcep.h:226
UINT8 * PackageEnd
Definition: aclocal.h:728
UINT8 ResultCount
Definition: acstruct.h:90
UINT8 * AmlPredicateStart
Definition: aclocal.h:727
#define ACPI_MODULE_NAME(Name)
Definition: acoutput.h:216
#define ACPI_100NSEC_PER_SEC
Definition: actypes.h:475
ACPI_STATUS AcpiDsExecEndControlOp(ACPI_WALK_STATE *WalkState, ACPI_PARSE_OBJECT *Op)
Definition: dscontrol.c:177
#define AE_INFO
Definition: acoutput.h:230
smooth NULL
Definition: ftsmooth.c:416
#define AE_AML_BAD_OPCODE
Definition: acexcep.h:180
BOOLEAN LastPredicate
Definition: acstruct.h:83
#define AE_CTRL_CONTINUE
Definition: acexcep.h:233
#define AE_CTRL_TRUE
Definition: acexcep.h:227
UINT64 LoopTimeout
Definition: aclocal.h:729
#define ACPI_DB_DISPATCH
Definition: acoutput.h:163
#define ACPI_DESC_TYPE_OPERAND
Definition: acobject.h:573
ACPI_CONTROL_STATE Control
Definition: aclocal.h:823
#define ACPI_DEBUG_PRINT(pl)
Definition: acoutput.h:475
#define AML_RETURN_OP
Definition: amlcode.h:138
#define AML_NOOP_OP
Definition: amlcode.h:137
ACPI_GENERIC_STATE * AcpiUtCreateControlState(void)
Definition: utstate.c:300
#define ACPI_SIGNAL_BREAKPOINT
Definition: acpiosxf.h:74
ACPI_STATUS AcpiExResolveToValue(ACPI_OPERAND_OBJECT **StackPtr, ACPI_WALK_STATE *WalkState)
Definition: exresolv.c:79
ACPI_STATUS AcpiDsExecBeginControlOp(ACPI_WALK_STATE *WalkState, ACPI_PARSE_OBJECT *Op)
Definition: dscontrol.c:71
#define AML_CONTINUE_OP
Definition: amlcode.h:133
ACPI_COMMON_STATE Common
Definition: aclocal.h:822
Status
Definition: gdiplustypes.h:24
#define AE_AML_LOOP_TIMEOUT
Definition: acexcep.h:212
union acpi_operand_object * Operands[ACPI_OBJ_NUM_OPERANDS+1]
Definition: acstruct.h:105
#define AE_CTRL_BREAK
Definition: acexcep.h:232
ACPI_RESULT_VALUES Results
Definition: aclocal.h:829
void AcpiUtRemoveReference(ACPI_OPERAND_OBJECT *Object)
Definition: utdelete.c:784
#define ACPI_ERROR(plist)
Definition: acoutput.h:240
#define AML_BREAKPOINT_OP
Definition: amlcode.h:141
#define BOOLEAN
Definition: pedump.c:73
#define ACPI_FUNCTION_NAME(a)
Definition: acoutput.h:479
unsigned long long UINT64
#define ACPI_TIME_AFTER(a, b)
Definition: actypes.h:481
UINT8 * AmlLastWhile
Definition: acstruct.h:108
#define ACPI_CONTROL_CONDITIONAL_EXECUTING
Definition: aclocal.h:670
#define AE_OK
Definition: acexcep.h:97
void AcpiDsClearImplicitReturn(ACPI_WALK_STATE *WalkState)
Definition: dsutils.c:73
#define ACPI_GET_DESCRIPTOR_TYPE(d)
Definition: acmacros.h:414