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