dscontrol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /******************************************************************************
  2. *
  3. * Module Name: dscontrol - Support for execution control opcodes -
  4. * if/else/while/return
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2016, 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/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: acpi_ds_exec_begin_control_op
  54. *
  55. * PARAMETERS: walk_list - 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. acpi_ds_exec_begin_control_op(struct acpi_walk_state *walk_state,
  66. union acpi_parse_object *op)
  67. {
  68. acpi_status status = AE_OK;
  69. union acpi_generic_state *control_state;
  70. ACPI_FUNCTION_NAME(ds_exec_begin_control_op);
  71. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n",
  72. op, op->common.aml_opcode, walk_state));
  73. switch (op->common.aml_opcode) {
  74. case AML_WHILE_OP:
  75. /*
  76. * If this is an additional iteration of a while loop, continue.
  77. * There is no need to allocate a new control state.
  78. */
  79. if (walk_state->control_state) {
  80. if (walk_state->control_state->control.
  81. aml_predicate_start ==
  82. (walk_state->parser_state.aml - 1)) {
  83. /* Reset the state to start-of-loop */
  84. walk_state->control_state->common.state =
  85. ACPI_CONTROL_CONDITIONAL_EXECUTING;
  86. break;
  87. }
  88. }
  89. /*lint -fallthrough */
  90. case AML_IF_OP:
  91. /*
  92. * IF/WHILE: Create a new control state to manage these
  93. * constructs. We need to manage these as a stack, in order
  94. * to handle nesting.
  95. */
  96. control_state = acpi_ut_create_control_state();
  97. if (!control_state) {
  98. status = AE_NO_MEMORY;
  99. break;
  100. }
  101. /*
  102. * Save a pointer to the predicate for multiple executions
  103. * of a loop
  104. */
  105. control_state->control.aml_predicate_start =
  106. walk_state->parser_state.aml - 1;
  107. control_state->control.package_end =
  108. walk_state->parser_state.pkg_end;
  109. control_state->control.opcode = op->common.aml_opcode;
  110. /* Push the control state on this walk's control stack */
  111. acpi_ut_push_generic_state(&walk_state->control_state,
  112. control_state);
  113. break;
  114. case AML_ELSE_OP:
  115. /* Predicate is in the state object */
  116. /* If predicate is true, the IF was executed, ignore ELSE part */
  117. if (walk_state->last_predicate) {
  118. status = AE_CTRL_TRUE;
  119. }
  120. break;
  121. case AML_RETURN_OP:
  122. break;
  123. default:
  124. break;
  125. }
  126. return (status);
  127. }
  128. /*******************************************************************************
  129. *
  130. * FUNCTION: acpi_ds_exec_end_control_op
  131. *
  132. * PARAMETERS: walk_list - The list that owns the walk stack
  133. * op - The control Op
  134. *
  135. * RETURN: Status
  136. *
  137. * DESCRIPTION: Handles all control ops encountered during control method
  138. * execution.
  139. *
  140. ******************************************************************************/
  141. acpi_status
  142. acpi_ds_exec_end_control_op(struct acpi_walk_state *walk_state,
  143. union acpi_parse_object *op)
  144. {
  145. acpi_status status = AE_OK;
  146. union acpi_generic_state *control_state;
  147. ACPI_FUNCTION_NAME(ds_exec_end_control_op);
  148. switch (op->common.aml_opcode) {
  149. case AML_IF_OP:
  150. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", op));
  151. /*
  152. * Save the result of the predicate in case there is an
  153. * ELSE to come
  154. */
  155. walk_state->last_predicate =
  156. (u8)walk_state->control_state->common.value;
  157. /*
  158. * Pop the control state that was created at the start
  159. * of the IF and free it
  160. */
  161. control_state =
  162. acpi_ut_pop_generic_state(&walk_state->control_state);
  163. acpi_ut_delete_generic_state(control_state);
  164. break;
  165. case AML_ELSE_OP:
  166. break;
  167. case AML_WHILE_OP:
  168. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", op));
  169. control_state = walk_state->control_state;
  170. if (control_state->common.value) {
  171. /* Predicate was true, the body of the loop was just executed */
  172. /*
  173. * This loop counter mechanism allows the interpreter to escape
  174. * possibly infinite loops. This can occur in poorly written AML
  175. * when the hardware does not respond within a while loop and the
  176. * loop does not implement a timeout.
  177. */
  178. control_state->control.loop_count++;
  179. if (control_state->control.loop_count >
  180. acpi_gbl_max_loop_iterations) {
  181. status = AE_AML_INFINITE_LOOP;
  182. break;
  183. }
  184. /*
  185. * Go back and evaluate the predicate and maybe execute the loop
  186. * another time
  187. */
  188. status = AE_CTRL_PENDING;
  189. walk_state->aml_last_while =
  190. control_state->control.aml_predicate_start;
  191. break;
  192. }
  193. /* Predicate was false, terminate this while loop */
  194. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  195. "[WHILE_OP] termination! Op=%p\n", op));
  196. /* Pop this control state and free it */
  197. control_state =
  198. acpi_ut_pop_generic_state(&walk_state->control_state);
  199. acpi_ut_delete_generic_state(control_state);
  200. break;
  201. case AML_RETURN_OP:
  202. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  203. "[RETURN_OP] Op=%p Arg=%p\n", op,
  204. op->common.value.arg));
  205. /*
  206. * One optional operand -- the return value
  207. * It can be either an immediate operand or a result that
  208. * has been bubbled up the tree
  209. */
  210. if (op->common.value.arg) {
  211. /* Since we have a real Return(), delete any implicit return */
  212. acpi_ds_clear_implicit_return(walk_state);
  213. /* Return statement has an immediate operand */
  214. status =
  215. acpi_ds_create_operands(walk_state,
  216. op->common.value.arg);
  217. if (ACPI_FAILURE(status)) {
  218. return (status);
  219. }
  220. /*
  221. * If value being returned is a Reference (such as
  222. * an arg or local), resolve it now because it may
  223. * cease to exist at the end of the method.
  224. */
  225. status =
  226. acpi_ex_resolve_to_value(&walk_state->operands[0],
  227. walk_state);
  228. if (ACPI_FAILURE(status)) {
  229. return (status);
  230. }
  231. /*
  232. * Get the return value and save as the last result
  233. * value. This is the only place where walk_state->return_desc
  234. * is set to anything other than zero!
  235. */
  236. walk_state->return_desc = walk_state->operands[0];
  237. } else if (walk_state->result_count) {
  238. /* Since we have a real Return(), delete any implicit return */
  239. acpi_ds_clear_implicit_return(walk_state);
  240. /*
  241. * The return value has come from a previous calculation.
  242. *
  243. * If value being returned is a Reference (such as
  244. * an arg or local), resolve it now because it may
  245. * cease to exist at the end of the method.
  246. *
  247. * Allow references created by the Index operator to return
  248. * unchanged.
  249. */
  250. if ((ACPI_GET_DESCRIPTOR_TYPE
  251. (walk_state->results->results.obj_desc[0]) ==
  252. ACPI_DESC_TYPE_OPERAND)
  253. && ((walk_state->results->results.obj_desc[0])->
  254. common.type == ACPI_TYPE_LOCAL_REFERENCE)
  255. && ((walk_state->results->results.obj_desc[0])->
  256. reference.class != ACPI_REFCLASS_INDEX)) {
  257. status =
  258. acpi_ex_resolve_to_value(&walk_state->
  259. results->results.
  260. obj_desc[0],
  261. walk_state);
  262. if (ACPI_FAILURE(status)) {
  263. return (status);
  264. }
  265. }
  266. walk_state->return_desc =
  267. walk_state->results->results.obj_desc[0];
  268. } else {
  269. /* No return operand */
  270. if (walk_state->num_operands) {
  271. acpi_ut_remove_reference(walk_state->
  272. operands[0]);
  273. }
  274. walk_state->operands[0] = NULL;
  275. walk_state->num_operands = 0;
  276. walk_state->return_desc = NULL;
  277. }
  278. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  279. "Completed RETURN_OP State=%p, RetVal=%p\n",
  280. walk_state, walk_state->return_desc));
  281. /* End the control method execution right now */
  282. status = AE_CTRL_TERMINATE;
  283. break;
  284. case AML_NOOP_OP:
  285. /* Just do nothing! */
  286. break;
  287. case AML_BREAK_POINT_OP:
  288. acpi_db_signal_break_point(walk_state);
  289. /* Call to the OSL in case OS wants a piece of the action */
  290. status = acpi_os_signal(ACPI_SIGNAL_BREAKPOINT,
  291. "Executed AML Breakpoint opcode");
  292. break;
  293. case AML_BREAK_OP:
  294. case AML_CONTINUE_OP: /* ACPI 2.0 */
  295. /* Pop and delete control states until we find a while */
  296. while (walk_state->control_state &&
  297. (walk_state->control_state->control.opcode !=
  298. AML_WHILE_OP)) {
  299. control_state =
  300. acpi_ut_pop_generic_state(&walk_state->
  301. control_state);
  302. acpi_ut_delete_generic_state(control_state);
  303. }
  304. /* No while found? */
  305. if (!walk_state->control_state) {
  306. return (AE_AML_NO_WHILE);
  307. }
  308. /* Was: walk_state->aml_last_while = walk_state->control_state->Control.aml_predicate_start; */
  309. walk_state->aml_last_while =
  310. walk_state->control_state->control.package_end;
  311. /* Return status depending on opcode */
  312. if (op->common.aml_opcode == AML_BREAK_OP) {
  313. status = AE_CTRL_BREAK;
  314. } else {
  315. status = AE_CTRL_CONTINUE;
  316. }
  317. break;
  318. default:
  319. ACPI_ERROR((AE_INFO, "Unknown control opcode=0x%X Op=%p",
  320. op->common.aml_opcode, op));
  321. status = AE_AML_BAD_OPCODE;
  322. break;
  323. }
  324. return (status);
  325. }