1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: psloop - Main AML parse loop
6 * Copyright (C) 2000 - 2019, 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);
36 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
37 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
39 /*******************************************************************************
41 * FUNCTION: acpi_ps_get_arguments
43 * PARAMETERS: walk_state - Current state
44 * aml_op_start - Op start in AML
49 * DESCRIPTION: Get arguments for passed Op.
51 ******************************************************************************/
54 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
55 u8 * aml_op_start, union acpi_parse_object *op)
57 acpi_status status = AE_OK;
58 union acpi_parse_object *arg = NULL;
59 const struct acpi_opcode_info *op_info;
61 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
63 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
64 "Get arguments for opcode [%s]\n",
65 op->common.aml_op_name));
67 switch (op->common.aml_opcode) {
68 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
69 case AML_WORD_OP: /* AML_WORDDATA_ARG */
70 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
71 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
72 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
74 /* Fill in constant or string argument directly */
76 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
77 GET_CURRENT_ARG_TYPE(walk_state->
82 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
84 status = acpi_ps_get_next_namepath(walk_state,
85 &(walk_state->parser_state),
87 ACPI_POSSIBLE_METHOD_CALL);
88 if (ACPI_FAILURE(status)) {
89 return_ACPI_STATUS(status);
92 walk_state->arg_types = 0;
97 * Op is not a constant or string, append each argument to the Op
99 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
100 !walk_state->arg_count) {
101 walk_state->aml = walk_state->parser_state.aml;
103 switch (op->common.aml_opcode) {
107 case AML_VARIABLE_PACKAGE_OP:
114 ASL_CV_CAPTURE_COMMENTS(walk_state);
119 acpi_ps_get_next_arg(walk_state,
120 &(walk_state->parser_state),
122 (walk_state->arg_types), &arg);
123 if (ACPI_FAILURE(status)) {
124 return_ACPI_STATUS(status);
128 acpi_ps_append_arg(op, arg);
131 INCREMENT_ARG_LIST(walk_state->arg_types);
134 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
135 "Final argument count: %8.8X pass %u\n",
136 walk_state->arg_count,
137 walk_state->pass_number));
140 * This case handles the legacy option that groups all module-level
141 * code blocks together and defers execution until all of the tables
142 * are loaded. Execute all of these blocks at this time.
143 * Execute any module-level code that was detected during the table
146 * Note: this option is deprecated and will be eliminated in the
147 * future. Use of this option can cause problems with AML code that
148 * depends upon in-order immediate execution of module-level code.
150 if (!acpi_gbl_execute_tables_as_methods &&
151 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
152 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
154 * We want to skip If/Else/While constructs during Pass1 because we
155 * want to actually conditionally execute the code during Pass2.
157 * Except for disassembly, where we always want to walk the
158 * If/Else/While packages
160 switch (op->common.aml_opcode) {
165 * Currently supported module-level opcodes are:
166 * IF/ELSE/WHILE. These appear to be the most common,
167 * and easiest to support since they open an AML
170 if (walk_state->pass_number ==
171 ACPI_IMODE_LOAD_PASS1) {
172 acpi_ps_link_module_code(op->common.
184 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
185 "Pass1: Skipping an If/Else/While body\n"));
187 /* Skip body of if/else/while in pass 1 */
189 walk_state->parser_state.aml =
190 walk_state->parser_state.pkg_end;
191 walk_state->arg_count = 0;
196 * Check for an unsupported executable opcode at module
197 * level. We must be in PASS1, the parent must be a SCOPE,
198 * The opcode class must be EXECUTE, and the opcode must
199 * not be an argument to another opcode.
201 if ((walk_state->pass_number ==
202 ACPI_IMODE_LOAD_PASS1)
203 && (op->common.parent->common.aml_opcode ==
206 acpi_ps_get_opcode_info(op->common.
208 if ((op_info->class ==
209 AML_CLASS_EXECUTE) && (!arg)) {
210 ACPI_WARNING((AE_INFO,
211 "Unsupported module-level executable opcode "
212 "0x%.2X at table offset 0x%.4X",
222 acpi_table_header))));
229 /* Special processing for certain opcodes */
231 switch (op->common.aml_opcode) {
234 * Skip parsing of control method because we don't have enough
235 * info in the first pass to parse it correctly.
237 * Save the length and address of the body
239 op->named.data = walk_state->parser_state.aml;
240 op->named.length = (u32)
241 (walk_state->parser_state.pkg_end -
242 walk_state->parser_state.aml);
244 /* Skip body of method */
246 walk_state->parser_state.aml =
247 walk_state->parser_state.pkg_end;
248 walk_state->arg_count = 0;
253 case AML_VARIABLE_PACKAGE_OP:
255 if ((op->common.parent) &&
256 (op->common.parent->common.aml_opcode ==
258 && (walk_state->pass_number <=
259 ACPI_IMODE_LOAD_PASS2)) {
260 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
261 "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
262 walk_state->pass_number,
266 * Skip parsing of Buffers and Packages because we don't have
267 * enough info in the first pass to parse them correctly.
269 op->named.data = aml_op_start;
270 op->named.length = (u32)
271 (walk_state->parser_state.pkg_end -
276 walk_state->parser_state.aml =
277 walk_state->parser_state.pkg_end;
278 walk_state->arg_count = 0;
284 if (walk_state->control_state) {
285 walk_state->control_state->control.package_end =
286 walk_state->parser_state.pkg_end;
292 /* No action for all other opcodes */
300 return_ACPI_STATUS(AE_OK);
303 /*******************************************************************************
305 * FUNCTION: acpi_ps_link_module_code
307 * PARAMETERS: parent_op - Parent parser op
308 * aml_start - Pointer to the AML
309 * aml_length - Length of executable AML
310 * owner_id - owner_id of module level code
314 * DESCRIPTION: Wrap the module-level code with a method object and link the
315 * object to the global list. Note, the mutex field of the method
316 * object is used to link multiple module-level code objects.
318 * NOTE: In this legacy option, each block of detected executable AML
319 * code that is outside of any control method is wrapped with a temporary
320 * control method object and placed on a global list below.
322 * This function executes the module-level code for all tables only after
323 * all of the tables have been loaded. It is a legacy option and is
324 * not compatible with other ACPI implementations. See acpi_ns_load_table.
326 * This function will be removed when the legacy option is removed.
328 ******************************************************************************/
331 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
332 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
334 union acpi_operand_object *prev;
335 union acpi_operand_object *next;
336 union acpi_operand_object *method_obj;
337 struct acpi_namespace_node *parent_node;
339 ACPI_FUNCTION_TRACE(ps_link_module_code);
341 /* Get the tail of the list */
343 prev = next = acpi_gbl_module_code_list;
346 next = next->method.mutex;
350 * Insert the module level code into the list. Merge it if it is
351 * adjacent to the previous element.
354 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
356 /* Create, initialize, and link a new temporary method object */
358 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
363 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
364 "Create/Link new code block: %p\n",
367 if (parent_op->common.node) {
368 parent_node = parent_op->common.node;
370 parent_node = acpi_gbl_root_node;
373 method_obj->method.aml_start = aml_start;
374 method_obj->method.aml_length = aml_length;
375 method_obj->method.owner_id = owner_id;
376 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
379 * Save the parent node in next_object. This is cheating, but we
380 * don't want to expand the method object.
382 method_obj->method.next_object =
383 ACPI_CAST_PTR(union acpi_operand_object, parent_node);
386 acpi_gbl_module_code_list = method_obj;
388 prev->method.mutex = method_obj;
391 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
392 "Appending to existing code block: %p\n",
395 prev->method.aml_length += aml_length;
401 /*******************************************************************************
403 * FUNCTION: acpi_ps_parse_loop
405 * PARAMETERS: walk_state - Current state
409 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
412 ******************************************************************************/
414 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
416 acpi_status status = AE_OK;
417 union acpi_parse_object *op = NULL; /* current op */
418 struct acpi_parse_state *parser_state;
419 u8 *aml_op_start = NULL;
422 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
424 if (walk_state->descending_callback == NULL) {
425 return_ACPI_STATUS(AE_BAD_PARAMETER);
428 parser_state = &walk_state->parser_state;
429 walk_state->arg_types = 0;
431 #ifndef ACPI_CONSTANT_EVAL_ONLY
433 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
435 /* We are restarting a preempted control method */
437 if (acpi_ps_has_completed_scope(parser_state)) {
439 * We must check if a predicate to an IF or WHILE statement
442 if ((parser_state->scope->parse_scope.op) &&
443 ((parser_state->scope->parse_scope.op->common.
444 aml_opcode == AML_IF_OP)
445 || (parser_state->scope->parse_scope.op->common.
446 aml_opcode == AML_WHILE_OP))
447 && (walk_state->control_state)
448 && (walk_state->control_state->common.state ==
449 ACPI_CONTROL_PREDICATE_EXECUTING)) {
451 * A predicate was just completed, get the value of the
452 * predicate and branch based on that value
454 walk_state->op = NULL;
456 acpi_ds_get_predicate_value(walk_state,
459 if (ACPI_FAILURE(status)
460 && ((status & AE_CODE_MASK) !=
462 if (status == AE_AML_NO_RETURN_VALUE) {
463 ACPI_EXCEPTION((AE_INFO, status,
464 "Invoked method did not return a value"));
467 ACPI_EXCEPTION((AE_INFO, status,
468 "GetPredicate Failed"));
469 return_ACPI_STATUS(status);
473 acpi_ps_next_parse_state(walk_state, op,
477 acpi_ps_pop_scope(parser_state, &op,
478 &walk_state->arg_types,
479 &walk_state->arg_count);
480 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
481 "Popped scope, Op=%p\n", op));
482 } else if (walk_state->prev_op) {
484 /* We were in the middle of an op */
486 op = walk_state->prev_op;
487 walk_state->arg_types = walk_state->prev_arg_types;
492 /* Iterative parsing loop, while there is more AML to process: */
494 while ((parser_state->aml < parser_state->aml_end) || (op)) {
495 ASL_CV_CAPTURE_COMMENTS(walk_state);
497 aml_op_start = parser_state->aml;
500 acpi_ps_create_op(walk_state, aml_op_start, &op);
501 if (ACPI_FAILURE(status)) {
503 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
504 * executing it as a control method. However, if we encounter
505 * an error while loading the table, we need to keep trying to
506 * load the table rather than aborting the table load. Set the
507 * status to AE_OK to proceed with the table load.
510 parse_flags & ACPI_PARSE_MODULE_LEVEL)
511 && ((status == AE_ALREADY_EXISTS)
512 || (status == AE_NOT_FOUND))) {
515 if (status == AE_CTRL_PARSE_CONTINUE) {
519 if (status == AE_CTRL_PARSE_PENDING) {
523 if (status == AE_CTRL_TERMINATE) {
524 return_ACPI_STATUS(status);
528 acpi_ps_complete_op(walk_state, &op,
530 if (ACPI_FAILURE(status)) {
531 return_ACPI_STATUS(status);
533 if (acpi_ns_opens_scope
534 (acpi_ps_get_opcode_info
535 (walk_state->opcode)->object_type)) {
537 * If the scope/device op fails to parse, skip the body of
538 * the scope op because the parse failure indicates that
539 * the device may not exist.
541 ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode));
544 * Determine the opcode length before skipping the opcode.
545 * An opcode can be 1 byte or 2 bytes in length.
548 if ((walk_state->opcode & 0xFF00) ==
549 AML_EXTENDED_OPCODE) {
552 walk_state->parser_state.aml =
553 walk_state->aml + opcode_length;
555 walk_state->parser_state.aml =
556 acpi_ps_get_next_package_end
557 (&walk_state->parser_state);
559 walk_state->parser_state.aml;
565 acpi_ex_start_trace_opcode(op, walk_state);
569 * Start arg_count at zero because we don't know if there are
572 walk_state->arg_count = 0;
574 switch (op->common.aml_opcode) {
584 ASL_CV_CAPTURE_COMMENTS(walk_state);
588 /* Are there any arguments that must be processed? */
590 if (walk_state->arg_types) {
595 acpi_ps_get_arguments(walk_state, aml_op_start, op);
596 if (ACPI_FAILURE(status)) {
598 acpi_ps_complete_op(walk_state, &op,
600 if (ACPI_FAILURE(status)) {
601 return_ACPI_STATUS(status);
603 if ((walk_state->control_state) &&
604 ((walk_state->control_state->control.
606 || (walk_state->control_state->control.
607 opcode == AML_WHILE_OP))) {
609 * If the if/while op fails to parse, we will skip parsing
610 * the body of the op.
613 walk_state->control_state->control.
614 aml_predicate_start + 1;
616 acpi_ps_get_next_package_end
618 walk_state->aml = parser_state->aml;
621 "Skipping While/If block"));
622 if (*walk_state->aml == AML_ELSE_OP) {
624 "Skipping Else block"));
625 walk_state->parser_state.aml =
627 walk_state->parser_state.aml =
628 acpi_ps_get_next_package_end
633 ACPI_FREE(acpi_ut_pop_generic_state
634 (&walk_state->control_state));
641 /* Check for arguments that need to be processed */
643 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
644 "Parseloop: argument count: %8.8X\n",
645 walk_state->arg_count));
647 if (walk_state->arg_count) {
649 * There are arguments (complex ones), push Op and
650 * prepare for argument
652 status = acpi_ps_push_scope(parser_state, op,
653 walk_state->arg_types,
654 walk_state->arg_count);
655 if (ACPI_FAILURE(status)) {
657 acpi_ps_complete_op(walk_state, &op,
659 if (ACPI_FAILURE(status)) {
660 return_ACPI_STATUS(status);
671 * All arguments have been processed -- Op is complete,
674 walk_state->op_info =
675 acpi_ps_get_opcode_info(op->common.aml_opcode);
676 if (walk_state->op_info->flags & AML_NAMED) {
677 if (op->common.aml_opcode == AML_REGION_OP ||
678 op->common.aml_opcode == AML_DATA_REGION_OP) {
680 * Skip parsing of control method or opregion body,
681 * because we don't have enough info in the first pass
682 * to parse them correctly.
684 * Completed parsing an op_region declaration, we now
688 (u32) (parser_state->aml - op->named.data);
692 if (walk_state->op_info->flags & AML_CREATE) {
694 * Backup to beginning of create_XXXfield declaration (1 for
697 * body_length is unknown until we parse the body
700 (u32) (parser_state->aml - op->named.data);
703 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
705 * Backup to beginning of bank_field declaration
707 * body_length is unknown until we parse the body
710 (u32) (parser_state->aml - op->named.data);
713 /* This op complete, notify the dispatcher */
715 if (walk_state->ascending_callback != NULL) {
717 walk_state->opcode = op->common.aml_opcode;
719 status = walk_state->ascending_callback(walk_state);
721 acpi_ps_next_parse_state(walk_state, op, status);
722 if (status == AE_CTRL_PENDING) {
726 parse_flags & ACPI_PARSE_MODULE_LEVEL)
727 && (ACPI_AML_EXCEPTION(status)
728 || status == AE_ALREADY_EXISTS
729 || status == AE_NOT_FOUND)) {
731 * ACPI_PARSE_MODULE_LEVEL flag means that we
732 * are currently loading a table by executing
733 * it as a control method. However, if we
734 * encounter an error while loading the table,
735 * we need to keep trying to load the table
736 * rather than aborting the table load (setting
737 * the status to AE_OK continues the table
738 * load). If we get a failure at this point, it
739 * means that the dispatcher got an error while
740 * trying to execute the Op.
746 status = acpi_ps_complete_op(walk_state, &op, status);
747 if (ACPI_FAILURE(status)) {
748 return_ACPI_STATUS(status);
751 } /* while parser_state->Aml */
753 status = acpi_ps_complete_final_op(walk_state, op, status);
754 return_ACPI_STATUS(status);