1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: psloop - Main AML parse loop
6 * Copyright (C) 2000 - 2018, 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"
26 #define _COMPONENT ACPI_PARSER
27 ACPI_MODULE_NAME("psloop")
29 /* Local prototypes */
31 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
32 u8 * aml_op_start, union acpi_parse_object *op);
35 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
36 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
38 /*******************************************************************************
40 * FUNCTION: acpi_ps_get_arguments
42 * PARAMETERS: walk_state - Current state
43 * aml_op_start - Op start in AML
48 * DESCRIPTION: Get arguments for passed Op.
50 ******************************************************************************/
53 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
54 u8 * aml_op_start, union acpi_parse_object *op)
56 acpi_status status = AE_OK;
57 union acpi_parse_object *arg = NULL;
58 const struct acpi_opcode_info *op_info;
60 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
62 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
63 "Get arguments for opcode [%s]\n",
64 op->common.aml_op_name));
66 switch (op->common.aml_opcode) {
67 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
68 case AML_WORD_OP: /* AML_WORDDATA_ARG */
69 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
70 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
71 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
73 /* Fill in constant or string argument directly */
75 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
76 GET_CURRENT_ARG_TYPE(walk_state->
81 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
83 status = acpi_ps_get_next_namepath(walk_state,
84 &(walk_state->parser_state),
86 ACPI_POSSIBLE_METHOD_CALL);
87 if (ACPI_FAILURE(status)) {
88 return_ACPI_STATUS(status);
91 walk_state->arg_types = 0;
96 * Op is not a constant or string, append each argument to the Op
98 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
99 !walk_state->arg_count) {
100 walk_state->aml = walk_state->parser_state.aml;
102 switch (op->common.aml_opcode) {
106 case AML_VARIABLE_PACKAGE_OP:
113 ASL_CV_CAPTURE_COMMENTS(walk_state);
118 acpi_ps_get_next_arg(walk_state,
119 &(walk_state->parser_state),
121 (walk_state->arg_types), &arg);
122 if (ACPI_FAILURE(status)) {
123 return_ACPI_STATUS(status);
127 acpi_ps_append_arg(op, arg);
130 INCREMENT_ARG_LIST(walk_state->arg_types);
133 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
134 "Final argument count: %8.8X pass %u\n",
135 walk_state->arg_count,
136 walk_state->pass_number));
139 * This case handles the legacy option that groups all module-level
140 * code blocks together and defers execution until all of the tables
141 * are loaded. Execute all of these blocks at this time.
142 * Execute any module-level code that was detected during the table
145 * Note: this option is deprecated and will be eliminated in the
146 * future. Use of this option can cause problems with AML code that
147 * depends upon in-order immediate execution of module-level code.
149 if (acpi_gbl_group_module_level_code &&
150 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
151 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
153 * We want to skip If/Else/While constructs during Pass1 because we
154 * want to actually conditionally execute the code during Pass2.
156 * Except for disassembly, where we always want to walk the
157 * If/Else/While packages
159 switch (op->common.aml_opcode) {
164 * Currently supported module-level opcodes are:
165 * IF/ELSE/WHILE. These appear to be the most common,
166 * and easiest to support since they open an AML
169 if (walk_state->pass_number ==
170 ACPI_IMODE_LOAD_PASS1) {
171 acpi_ps_link_module_code(op->common.
183 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
184 "Pass1: Skipping an If/Else/While body\n"));
186 /* Skip body of if/else/while in pass 1 */
188 walk_state->parser_state.aml =
189 walk_state->parser_state.pkg_end;
190 walk_state->arg_count = 0;
195 * Check for an unsupported executable opcode at module
196 * level. We must be in PASS1, the parent must be a SCOPE,
197 * The opcode class must be EXECUTE, and the opcode must
198 * not be an argument to another opcode.
200 if ((walk_state->pass_number ==
201 ACPI_IMODE_LOAD_PASS1)
202 && (op->common.parent->common.aml_opcode ==
205 acpi_ps_get_opcode_info(op->common.
207 if ((op_info->class ==
208 AML_CLASS_EXECUTE) && (!arg)) {
209 ACPI_WARNING((AE_INFO,
210 "Unsupported module-level executable opcode "
211 "0x%.2X at table offset 0x%.4X",
221 acpi_table_header))));
228 /* Special processing for certain opcodes */
230 switch (op->common.aml_opcode) {
233 * Skip parsing of control method because we don't have enough
234 * info in the first pass to parse it correctly.
236 * Save the length and address of the body
238 op->named.data = walk_state->parser_state.aml;
239 op->named.length = (u32)
240 (walk_state->parser_state.pkg_end -
241 walk_state->parser_state.aml);
243 /* Skip body of method */
245 walk_state->parser_state.aml =
246 walk_state->parser_state.pkg_end;
247 walk_state->arg_count = 0;
252 case AML_VARIABLE_PACKAGE_OP:
254 if ((op->common.parent) &&
255 (op->common.parent->common.aml_opcode ==
257 && (walk_state->pass_number <=
258 ACPI_IMODE_LOAD_PASS2)) {
259 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
260 "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
261 walk_state->pass_number,
265 * Skip parsing of Buffers and Packages because we don't have
266 * enough info in the first pass to parse them correctly.
268 op->named.data = aml_op_start;
269 op->named.length = (u32)
270 (walk_state->parser_state.pkg_end -
275 walk_state->parser_state.aml =
276 walk_state->parser_state.pkg_end;
277 walk_state->arg_count = 0;
283 if (walk_state->control_state) {
284 walk_state->control_state->control.package_end =
285 walk_state->parser_state.pkg_end;
291 /* No action for all other opcodes */
299 return_ACPI_STATUS(AE_OK);
302 /*******************************************************************************
304 * FUNCTION: acpi_ps_link_module_code
306 * PARAMETERS: parent_op - Parent parser op
307 * aml_start - Pointer to the AML
308 * aml_length - Length of executable AML
309 * owner_id - owner_id of module level code
313 * DESCRIPTION: Wrap the module-level code with a method object and link the
314 * object to the global list. Note, the mutex field of the method
315 * object is used to link multiple module-level code objects.
317 * NOTE: In this legacy option, each block of detected executable AML
318 * code that is outside of any control method is wrapped with a temporary
319 * control method object and placed on a global list below.
321 * This function executes the module-level code for all tables only after
322 * all of the tables have been loaded. It is a legacy option and is
323 * not compatible with other ACPI implementations. See acpi_ns_load_table.
325 * This function will be removed when the legacy option is removed.
327 ******************************************************************************/
330 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
331 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
333 union acpi_operand_object *prev;
334 union acpi_operand_object *next;
335 union acpi_operand_object *method_obj;
336 struct acpi_namespace_node *parent_node;
338 ACPI_FUNCTION_TRACE(ps_link_module_code);
340 /* Get the tail of the list */
342 prev = next = acpi_gbl_module_code_list;
345 next = next->method.mutex;
349 * Insert the module level code into the list. Merge it if it is
350 * adjacent to the previous element.
353 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
355 /* Create, initialize, and link a new temporary method object */
357 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
362 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
363 "Create/Link new code block: %p\n",
366 if (parent_op->common.node) {
367 parent_node = parent_op->common.node;
369 parent_node = acpi_gbl_root_node;
372 method_obj->method.aml_start = aml_start;
373 method_obj->method.aml_length = aml_length;
374 method_obj->method.owner_id = owner_id;
375 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
378 * Save the parent node in next_object. This is cheating, but we
379 * don't want to expand the method object.
381 method_obj->method.next_object =
382 ACPI_CAST_PTR(union acpi_operand_object, parent_node);
385 acpi_gbl_module_code_list = method_obj;
387 prev->method.mutex = method_obj;
390 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
391 "Appending to existing code block: %p\n",
394 prev->method.aml_length += aml_length;
400 /*******************************************************************************
402 * FUNCTION: acpi_ps_parse_loop
404 * PARAMETERS: walk_state - Current state
408 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
411 ******************************************************************************/
413 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
415 acpi_status status = AE_OK;
416 union acpi_parse_object *op = NULL; /* current op */
417 struct acpi_parse_state *parser_state;
418 u8 *aml_op_start = NULL;
420 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
422 if (walk_state->descending_callback == NULL) {
423 return_ACPI_STATUS(AE_BAD_PARAMETER);
426 parser_state = &walk_state->parser_state;
427 walk_state->arg_types = 0;
429 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
431 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
433 /* We are restarting a preempted control method */
435 if (acpi_ps_has_completed_scope(parser_state)) {
437 * We must check if a predicate to an IF or WHILE statement
440 if ((parser_state->scope->parse_scope.op) &&
441 ((parser_state->scope->parse_scope.op->common.
442 aml_opcode == AML_IF_OP)
443 || (parser_state->scope->parse_scope.op->common.
444 aml_opcode == AML_WHILE_OP))
445 && (walk_state->control_state)
446 && (walk_state->control_state->common.state ==
447 ACPI_CONTROL_PREDICATE_EXECUTING)) {
449 * A predicate was just completed, get the value of the
450 * predicate and branch based on that value
452 walk_state->op = NULL;
454 acpi_ds_get_predicate_value(walk_state,
457 if (ACPI_FAILURE(status)
458 && ((status & AE_CODE_MASK) !=
460 if (status == AE_AML_NO_RETURN_VALUE) {
461 ACPI_EXCEPTION((AE_INFO, status,
462 "Invoked method did not return a value"));
465 ACPI_EXCEPTION((AE_INFO, status,
466 "GetPredicate Failed"));
467 return_ACPI_STATUS(status);
471 acpi_ps_next_parse_state(walk_state, op,
475 acpi_ps_pop_scope(parser_state, &op,
476 &walk_state->arg_types,
477 &walk_state->arg_count);
478 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
479 "Popped scope, Op=%p\n", op));
480 } else if (walk_state->prev_op) {
482 /* We were in the middle of an op */
484 op = walk_state->prev_op;
485 walk_state->arg_types = walk_state->prev_arg_types;
490 /* Iterative parsing loop, while there is more AML to process: */
492 while ((parser_state->aml < parser_state->aml_end) || (op)) {
493 ASL_CV_CAPTURE_COMMENTS(walk_state);
495 aml_op_start = parser_state->aml;
498 acpi_ps_create_op(walk_state, aml_op_start, &op);
499 if (ACPI_FAILURE(status)) {
500 if (status == AE_CTRL_PARSE_CONTINUE) {
504 if (status == AE_CTRL_PARSE_PENDING) {
508 if (status == AE_CTRL_TERMINATE) {
509 return_ACPI_STATUS(status);
513 acpi_ps_complete_op(walk_state, &op,
515 if (ACPI_FAILURE(status)) {
516 return_ACPI_STATUS(status);
518 if (walk_state->opcode == AML_SCOPE_OP) {
520 * If the scope op fails to parse, skip the body of the
521 * scope op because the parse failure indicates that the
522 * device may not exist.
524 walk_state->parser_state.aml =
526 walk_state->parser_state.aml =
527 acpi_ps_get_next_package_end
528 (&walk_state->parser_state);
530 walk_state->parser_state.aml;
532 "Skipping Scope block"));
538 acpi_ex_start_trace_opcode(op, walk_state);
542 * Start arg_count at zero because we don't know if there are
545 walk_state->arg_count = 0;
547 switch (op->common.aml_opcode) {
557 ASL_CV_CAPTURE_COMMENTS(walk_state);
561 /* Are there any arguments that must be processed? */
563 if (walk_state->arg_types) {
568 acpi_ps_get_arguments(walk_state, aml_op_start, op);
569 if (ACPI_FAILURE(status)) {
571 acpi_ps_complete_op(walk_state, &op,
573 if (ACPI_FAILURE(status)) {
574 return_ACPI_STATUS(status);
576 if ((walk_state->control_state) &&
577 ((walk_state->control_state->control.
579 || (walk_state->control_state->control.
580 opcode == AML_WHILE_OP))) {
582 * If the if/while op fails to parse, we will skip parsing
583 * the body of the op.
586 walk_state->control_state->control.
587 aml_predicate_start + 1;
589 acpi_ps_get_next_package_end
591 walk_state->aml = parser_state->aml;
594 "Skipping While/If block"));
595 if (*walk_state->aml == AML_ELSE_OP) {
597 "Skipping Else block"));
598 walk_state->parser_state.aml =
600 walk_state->parser_state.aml =
601 acpi_ps_get_next_package_end
606 ACPI_FREE(acpi_ut_pop_generic_state
607 (&walk_state->control_state));
614 /* Check for arguments that need to be processed */
616 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
617 "Parseloop: argument count: %8.8X\n",
618 walk_state->arg_count));
620 if (walk_state->arg_count) {
622 * There are arguments (complex ones), push Op and
623 * prepare for argument
625 status = acpi_ps_push_scope(parser_state, op,
626 walk_state->arg_types,
627 walk_state->arg_count);
628 if (ACPI_FAILURE(status)) {
630 acpi_ps_complete_op(walk_state, &op,
632 if (ACPI_FAILURE(status)) {
633 return_ACPI_STATUS(status);
644 * All arguments have been processed -- Op is complete,
647 walk_state->op_info =
648 acpi_ps_get_opcode_info(op->common.aml_opcode);
649 if (walk_state->op_info->flags & AML_NAMED) {
650 if (op->common.aml_opcode == AML_REGION_OP ||
651 op->common.aml_opcode == AML_DATA_REGION_OP) {
653 * Skip parsing of control method or opregion body,
654 * because we don't have enough info in the first pass
655 * to parse them correctly.
657 * Completed parsing an op_region declaration, we now
661 (u32) (parser_state->aml - op->named.data);
665 if (walk_state->op_info->flags & AML_CREATE) {
667 * Backup to beginning of create_XXXfield declaration (1 for
670 * body_length is unknown until we parse the body
673 (u32) (parser_state->aml - op->named.data);
676 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
678 * Backup to beginning of bank_field declaration
680 * body_length is unknown until we parse the body
683 (u32) (parser_state->aml - op->named.data);
686 /* This op complete, notify the dispatcher */
688 if (walk_state->ascending_callback != NULL) {
690 walk_state->opcode = op->common.aml_opcode;
692 status = walk_state->ascending_callback(walk_state);
694 acpi_ps_next_parse_state(walk_state, op, status);
695 if (status == AE_CTRL_PENDING) {
700 status = acpi_ps_complete_op(walk_state, &op, status);
701 if (ACPI_FAILURE(status)) {
702 return_ACPI_STATUS(status);
705 } /* while parser_state->Aml */
707 status = acpi_ps_complete_final_op(walk_state, op, status);
708 return_ACPI_STATUS(status);