dscontrol.c 11 KB

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