1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: psloop - Main AML parse loop
6 * Copyright (C) 2000 - 2023, Intel Corp.
8 *****************************************************************************/
11 * Parse the AML and build an operation tree as most interpreters, (such as
12 * Perl) do. Parsing is done by hand rather than with a YACC generated parser
13 * to tightly constrain stack and dynamic memory usage. Parsing is kept
14 * flexible and the code fairly compact by parsing based on a list of AML
15 * opcode templates in aml_op_info[].
18 #include <acpi/acpi.h>
24 #include "acconvert.h"
27 #define _COMPONENT ACPI_PARSER
28 ACPI_MODULE_NAME("psloop")
30 /* Local prototypes */
32 acpi_ps_get_arguments(struct acpi_walk_state
*walk_state
,
33 u8
* aml_op_start
, union acpi_parse_object
*op
);
35 /*******************************************************************************
37 * FUNCTION: acpi_ps_get_arguments
39 * PARAMETERS: walk_state - Current state
40 * aml_op_start - Op start in AML
45 * DESCRIPTION: Get arguments for passed Op.
47 ******************************************************************************/
50 acpi_ps_get_arguments(struct acpi_walk_state
*walk_state
,
51 u8
* aml_op_start
, union acpi_parse_object
*op
)
53 acpi_status status
= AE_OK
;
54 union acpi_parse_object
*arg
= NULL
;
56 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments
, walk_state
);
58 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
59 "Get arguments for opcode [%s]\n",
60 op
->common
.aml_op_name
));
62 switch (op
->common
.aml_opcode
) {
63 case AML_BYTE_OP
: /* AML_BYTEDATA_ARG */
64 case AML_WORD_OP
: /* AML_WORDDATA_ARG */
65 case AML_DWORD_OP
: /* AML_DWORDATA_ARG */
66 case AML_QWORD_OP
: /* AML_QWORDATA_ARG */
67 case AML_STRING_OP
: /* AML_ASCIICHARLIST_ARG */
69 /* Fill in constant or string argument directly */
71 acpi_ps_get_next_simple_arg(&(walk_state
->parser_state
),
72 GET_CURRENT_ARG_TYPE(walk_state
->
77 case AML_INT_NAMEPATH_OP
: /* AML_NAMESTRING_ARG */
79 status
= acpi_ps_get_next_namepath(walk_state
,
80 &(walk_state
->parser_state
),
82 ACPI_POSSIBLE_METHOD_CALL
);
83 if (ACPI_FAILURE(status
)) {
84 return_ACPI_STATUS(status
);
87 walk_state
->arg_types
= 0;
92 * Op is not a constant or string, append each argument to the Op
94 while (GET_CURRENT_ARG_TYPE(walk_state
->arg_types
) &&
95 !walk_state
->arg_count
) {
96 walk_state
->aml
= walk_state
->parser_state
.aml
;
98 switch (op
->common
.aml_opcode
) {
102 case AML_VARIABLE_PACKAGE_OP
:
109 ASL_CV_CAPTURE_COMMENTS(walk_state
);
114 acpi_ps_get_next_arg(walk_state
,
115 &(walk_state
->parser_state
),
117 (walk_state
->arg_types
), &arg
);
118 if (ACPI_FAILURE(status
)) {
119 return_ACPI_STATUS(status
);
123 acpi_ps_append_arg(op
, arg
);
126 INCREMENT_ARG_LIST(walk_state
->arg_types
);
129 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
130 "Final argument count: %8.8X pass %u\n",
131 walk_state
->arg_count
,
132 walk_state
->pass_number
));
134 /* Special processing for certain opcodes */
136 switch (op
->common
.aml_opcode
) {
139 * Skip parsing of control method because we don't have enough
140 * info in the first pass to parse it correctly.
142 * Save the length and address of the body
144 op
->named
.data
= walk_state
->parser_state
.aml
;
145 op
->named
.length
= (u32
)
146 (walk_state
->parser_state
.pkg_end
-
147 walk_state
->parser_state
.aml
);
149 /* Skip body of method */
151 walk_state
->parser_state
.aml
=
152 walk_state
->parser_state
.pkg_end
;
153 walk_state
->arg_count
= 0;
158 case AML_VARIABLE_PACKAGE_OP
:
160 if ((op
->common
.parent
) &&
161 (op
->common
.parent
->common
.aml_opcode
==
163 && (walk_state
->pass_number
<=
164 ACPI_IMODE_LOAD_PASS2
)) {
165 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
166 "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
167 walk_state
->pass_number
,
171 * Skip parsing of Buffers and Packages because we don't have
172 * enough info in the first pass to parse them correctly.
174 op
->named
.data
= aml_op_start
;
175 op
->named
.length
= (u32
)
176 (walk_state
->parser_state
.pkg_end
-
181 walk_state
->parser_state
.aml
=
182 walk_state
->parser_state
.pkg_end
;
183 walk_state
->arg_count
= 0;
189 if (walk_state
->control_state
) {
190 walk_state
->control_state
->control
.package_end
=
191 walk_state
->parser_state
.pkg_end
;
197 /* No action for all other opcodes */
205 return_ACPI_STATUS(AE_OK
);
208 /*******************************************************************************
210 * FUNCTION: acpi_ps_parse_loop
212 * PARAMETERS: walk_state - Current state
216 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
219 ******************************************************************************/
221 acpi_status
acpi_ps_parse_loop(struct acpi_walk_state
*walk_state
)
223 acpi_status status
= AE_OK
;
224 union acpi_parse_object
*op
= NULL
; /* current op */
225 struct acpi_parse_state
*parser_state
;
226 u8
*aml_op_start
= NULL
;
229 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop
, walk_state
);
231 if (walk_state
->descending_callback
== NULL
) {
232 return_ACPI_STATUS(AE_BAD_PARAMETER
);
235 parser_state
= &walk_state
->parser_state
;
236 walk_state
->arg_types
= 0;
238 #ifndef ACPI_CONSTANT_EVAL_ONLY
240 if (walk_state
->walk_type
& ACPI_WALK_METHOD_RESTART
) {
242 /* We are restarting a preempted control method */
244 if (acpi_ps_has_completed_scope(parser_state
)) {
246 * We must check if a predicate to an IF or WHILE statement
249 if ((parser_state
->scope
->parse_scope
.op
) &&
250 ((parser_state
->scope
->parse_scope
.op
->common
.
251 aml_opcode
== AML_IF_OP
)
252 || (parser_state
->scope
->parse_scope
.op
->common
.
253 aml_opcode
== AML_WHILE_OP
))
254 && (walk_state
->control_state
)
255 && (walk_state
->control_state
->common
.state
==
256 ACPI_CONTROL_PREDICATE_EXECUTING
)) {
258 * A predicate was just completed, get the value of the
259 * predicate and branch based on that value
261 walk_state
->op
= NULL
;
263 acpi_ds_get_predicate_value(walk_state
,
266 if (ACPI_FAILURE(status
)
267 && !ACPI_CNTL_EXCEPTION(status
)) {
268 if (status
== AE_AML_NO_RETURN_VALUE
) {
269 ACPI_EXCEPTION((AE_INFO
, status
,
270 "Invoked method did not return a value"));
273 ACPI_EXCEPTION((AE_INFO
, status
,
274 "GetPredicate Failed"));
275 return_ACPI_STATUS(status
);
279 acpi_ps_next_parse_state(walk_state
, op
,
283 acpi_ps_pop_scope(parser_state
, &op
,
284 &walk_state
->arg_types
,
285 &walk_state
->arg_count
);
286 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
287 "Popped scope, Op=%p\n", op
));
288 } else if (walk_state
->prev_op
) {
290 /* We were in the middle of an op */
292 op
= walk_state
->prev_op
;
293 walk_state
->arg_types
= walk_state
->prev_arg_types
;
298 /* Iterative parsing loop, while there is more AML to process: */
300 while ((parser_state
->aml
< parser_state
->aml_end
) || (op
)) {
301 ASL_CV_CAPTURE_COMMENTS(walk_state
);
303 aml_op_start
= parser_state
->aml
;
306 acpi_ps_create_op(walk_state
, aml_op_start
, &op
);
307 if (ACPI_FAILURE(status
)) {
309 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
310 * executing it as a control method. However, if we encounter
311 * an error while loading the table, we need to keep trying to
312 * load the table rather than aborting the table load. Set the
313 * status to AE_OK to proceed with the table load.
316 parse_flags
& ACPI_PARSE_MODULE_LEVEL
)
317 && ((status
== AE_ALREADY_EXISTS
)
318 || (status
== AE_NOT_FOUND
))) {
321 if (status
== AE_CTRL_PARSE_CONTINUE
) {
325 if (status
== AE_CTRL_PARSE_PENDING
) {
329 if (status
== AE_CTRL_TERMINATE
) {
330 return_ACPI_STATUS(status
);
334 acpi_ps_complete_op(walk_state
, &op
,
336 if (ACPI_FAILURE(status
)) {
337 return_ACPI_STATUS(status
);
339 if (acpi_ns_opens_scope
340 (acpi_ps_get_opcode_info
341 (walk_state
->opcode
)->object_type
)) {
343 * If the scope/device op fails to parse, skip the body of
344 * the scope op because the parse failure indicates that
345 * the device may not exist.
347 ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state
->opcode
), walk_state
->opcode
));
350 * Determine the opcode length before skipping the opcode.
351 * An opcode can be 1 byte or 2 bytes in length.
354 if ((walk_state
->opcode
& 0xFF00) ==
355 AML_EXTENDED_OPCODE
) {
358 walk_state
->parser_state
.aml
=
359 walk_state
->aml
+ opcode_length
;
361 walk_state
->parser_state
.aml
=
362 acpi_ps_get_next_package_end
363 (&walk_state
->parser_state
);
365 walk_state
->parser_state
.aml
;
371 acpi_ex_start_trace_opcode(op
, walk_state
);
375 * Start arg_count at zero because we don't know if there are
378 walk_state
->arg_count
= 0;
380 switch (op
->common
.aml_opcode
) {
390 ASL_CV_CAPTURE_COMMENTS(walk_state
);
394 /* Are there any arguments that must be processed? */
396 if (walk_state
->arg_types
) {
401 acpi_ps_get_arguments(walk_state
, aml_op_start
, op
);
402 if (ACPI_FAILURE(status
)) {
404 acpi_ps_complete_op(walk_state
, &op
,
406 if (ACPI_FAILURE(status
)) {
407 return_ACPI_STATUS(status
);
409 if ((walk_state
->control_state
) &&
410 ((walk_state
->control_state
->control
.
412 || (walk_state
->control_state
->control
.
413 opcode
== AML_WHILE_OP
))) {
415 * If the if/while op fails to parse, we will skip parsing
416 * the body of the op.
419 walk_state
->control_state
->control
.
420 aml_predicate_start
+ 1;
422 acpi_ps_get_next_package_end
424 walk_state
->aml
= parser_state
->aml
;
427 "Skipping While/If block"));
428 if (*walk_state
->aml
== AML_ELSE_OP
) {
430 "Skipping Else block"));
431 walk_state
->parser_state
.aml
=
433 walk_state
->parser_state
.aml
=
434 acpi_ps_get_next_package_end
439 ACPI_FREE(acpi_ut_pop_generic_state
440 (&walk_state
->control_state
));
447 /* Check for arguments that need to be processed */
449 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
450 "Parseloop: argument count: %8.8X\n",
451 walk_state
->arg_count
));
453 if (walk_state
->arg_count
) {
455 * There are arguments (complex ones), push Op and
456 * prepare for argument
458 status
= acpi_ps_push_scope(parser_state
, op
,
459 walk_state
->arg_types
,
460 walk_state
->arg_count
);
461 if (ACPI_FAILURE(status
)) {
463 acpi_ps_complete_op(walk_state
, &op
,
465 if (ACPI_FAILURE(status
)) {
466 return_ACPI_STATUS(status
);
477 * All arguments have been processed -- Op is complete,
480 walk_state
->op_info
=
481 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
482 if (walk_state
->op_info
->flags
& AML_NAMED
) {
483 if (op
->common
.aml_opcode
== AML_REGION_OP
||
484 op
->common
.aml_opcode
== AML_DATA_REGION_OP
) {
486 * Skip parsing of control method or opregion body,
487 * because we don't have enough info in the first pass
488 * to parse them correctly.
490 * Completed parsing an op_region declaration, we now
494 (u32
) (parser_state
->aml
- op
->named
.data
);
498 if (walk_state
->op_info
->flags
& AML_CREATE
) {
500 * Backup to beginning of create_XXXfield declaration (1 for
503 * body_length is unknown until we parse the body
506 (u32
) (parser_state
->aml
- op
->named
.data
);
509 if (op
->common
.aml_opcode
== AML_BANK_FIELD_OP
) {
511 * Backup to beginning of bank_field declaration
513 * body_length is unknown until we parse the body
516 (u32
) (parser_state
->aml
- op
->named
.data
);
519 /* This op complete, notify the dispatcher */
521 if (walk_state
->ascending_callback
!= NULL
) {
523 walk_state
->opcode
= op
->common
.aml_opcode
;
525 status
= walk_state
->ascending_callback(walk_state
);
527 acpi_ps_next_parse_state(walk_state
, op
, status
);
528 if (status
== AE_CTRL_PENDING
) {
532 parse_flags
& ACPI_PARSE_MODULE_LEVEL
)
533 && (ACPI_AML_EXCEPTION(status
)
534 || status
== AE_ALREADY_EXISTS
535 || status
== AE_NOT_FOUND
)) {
537 * ACPI_PARSE_MODULE_LEVEL flag means that we
538 * are currently loading a table by executing
539 * it as a control method. However, if we
540 * encounter an error while loading the table,
541 * we need to keep trying to load the table
542 * rather than aborting the table load (setting
543 * the status to AE_OK continues the table
544 * load). If we get a failure at this point, it
545 * means that the dispatcher got an error while
546 * trying to execute the Op.
552 status
= acpi_ps_complete_op(walk_state
, &op
, status
);
553 if (ACPI_FAILURE(status
)) {
554 return_ACPI_STATUS(status
);
557 } /* while parser_state->Aml */
559 status
= acpi_ps_complete_final_op(walk_state
, op
, status
);
560 return_ACPI_STATUS(status
);