dscontrol.c 9.3 KB

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