dscontrol.c 11 KB

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