dscontrol.c 12 KB

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