1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 * Parse the AML and build an operation tree as most interpreters,
47 * like Perl, do. Parsing is done by hand rather than with a YACC
48 * generated parser to tightly constrain stack and dynamic memory
49 * usage. At the same time, parsing is kept flexible and the code
50 * fairly compact by parsing based on a list of AML opcode
51 * templates in aml_op_info[]
54 #include <acpi/acpi.h>
55 #include <acpi/acparser.h>
56 #include <acpi/acdispat.h>
57 #include <acpi/amlcode.h>
58 #include <acpi/acnamesp.h>
59 #include <acpi/acinterp.h>
61 #define _COMPONENT ACPI_PARSER
62 ACPI_MODULE_NAME ("psparse")
65 static u32 acpi_gbl_depth = 0;
67 /* Local prototypes */
70 acpi_ps_complete_this_op (
71 struct acpi_walk_state *walk_state,
72 union acpi_parse_object *op);
75 acpi_ps_next_parse_state (
76 struct acpi_walk_state *walk_state,
77 union acpi_parse_object *op,
78 acpi_status callback_status);
82 struct acpi_walk_state *walk_state);
85 /*******************************************************************************
87 * FUNCTION: acpi_ps_get_opcode_size
89 * PARAMETERS: Opcode - An AML opcode
91 * RETURN: Size of the opcode, in bytes (1 or 2)
93 * DESCRIPTION: Get the size of the current opcode.
95 ******************************************************************************/
98 acpi_ps_get_opcode_size (
102 /* Extended (2-byte) opcode if > 255 */
104 if (opcode > 0x00FF) {
108 /* Otherwise, just a single byte opcode */
114 /*******************************************************************************
116 * FUNCTION: acpi_ps_peek_opcode
118 * PARAMETERS: parser_state - A parser state object
120 * RETURN: Next AML opcode
122 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
124 ******************************************************************************/
127 acpi_ps_peek_opcode (
128 struct acpi_parse_state *parser_state)
134 aml = parser_state->aml;
135 opcode = (u16) ACPI_GET8 (aml);
137 if (opcode == AML_EXTOP) {
138 /* Extended opcode */
141 opcode = (u16) ((opcode << 8) | ACPI_GET8 (aml));
148 /*******************************************************************************
150 * FUNCTION: acpi_ps_complete_this_op
152 * PARAMETERS: walk_state - Current State
153 * Op - Op to complete
157 * DESCRIPTION: Perform any cleanup at the completion of an Op.
159 ******************************************************************************/
162 acpi_ps_complete_this_op (
163 struct acpi_walk_state *walk_state,
164 union acpi_parse_object *op)
166 union acpi_parse_object *prev;
167 union acpi_parse_object *next;
168 const struct acpi_opcode_info *parent_info;
169 union acpi_parse_object *replacement_op = NULL;
172 ACPI_FUNCTION_TRACE_PTR ("ps_complete_this_op", op);
175 /* Check for null Op, can happen if AML code is corrupt */
181 /* Delete this op and the subtree below it if asked to */
183 if (((walk_state->parse_flags & ACPI_PARSE_TREE_MASK) != ACPI_PARSE_DELETE_TREE) ||
184 (walk_state->op_info->class == AML_CLASS_ARGUMENT)) {
188 /* Make sure that we only delete this subtree */
190 if (op->common.parent) {
192 * Check if we need to replace the operator and its subtree
193 * with a return value op (placeholder op)
195 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
197 switch (parent_info->class) {
198 case AML_CLASS_CONTROL:
201 case AML_CLASS_CREATE:
204 * These opcodes contain term_arg operands. The current
205 * op must be replaced by a placeholder return op
207 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
208 if (!replacement_op) {
213 case AML_CLASS_NAMED_OBJECT:
216 * These opcodes contain term_arg operands. The current
217 * op must be replaced by a placeholder return op
219 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
220 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
221 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
222 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
223 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
224 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
225 if (!replacement_op) {
230 if ((op->common.parent->common.aml_opcode == AML_NAME_OP) &&
231 (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
232 if ((op->common.aml_opcode == AML_BUFFER_OP) ||
233 (op->common.aml_opcode == AML_PACKAGE_OP) ||
234 (op->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
235 replacement_op = acpi_ps_alloc_op (op->common.aml_opcode);
236 if (!replacement_op) {
240 replacement_op->named.data = op->named.data;
241 replacement_op->named.length = op->named.length;
247 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
248 if (!replacement_op) {
253 /* We must unlink this op from the parent tree */
255 prev = op->common.parent->common.value.arg;
257 /* This op is the first in the list */
259 if (replacement_op) {
260 replacement_op->common.parent = op->common.parent;
261 replacement_op->common.value.arg = NULL;
262 replacement_op->common.node = op->common.node;
263 op->common.parent->common.value.arg = replacement_op;
264 replacement_op->common.next = op->common.next;
267 op->common.parent->common.value.arg = op->common.next;
271 /* Search the parent list */
274 /* Traverse all siblings in the parent's argument list */
276 next = prev->common.next;
278 if (replacement_op) {
279 replacement_op->common.parent = op->common.parent;
280 replacement_op->common.value.arg = NULL;
281 replacement_op->common.node = op->common.node;
282 prev->common.next = replacement_op;
283 replacement_op->common.next = op->common.next;
287 prev->common.next = op->common.next;
298 /* Now we can actually delete the subtree rooted at Op */
300 acpi_ps_delete_parse_tree (op);
305 /*******************************************************************************
307 * FUNCTION: acpi_ps_next_parse_state
309 * PARAMETERS: walk_state - Current state
310 * Op - Current parse op
311 * callback_status - Status from previous operation
315 * DESCRIPTION: Update the parser state based upon the return exception from
316 * the parser callback.
318 ******************************************************************************/
321 acpi_ps_next_parse_state (
322 struct acpi_walk_state *walk_state,
323 union acpi_parse_object *op,
324 acpi_status callback_status)
326 struct acpi_parse_state *parser_state = &walk_state->parser_state;
327 acpi_status status = AE_CTRL_PENDING;
330 ACPI_FUNCTION_TRACE_PTR ("ps_next_parse_state", op);
333 switch (callback_status) {
334 case AE_CTRL_TERMINATE:
337 * A control method was terminated via a RETURN statement.
338 * The walk of this method is complete.
340 parser_state->aml = parser_state->aml_end;
341 status = AE_CTRL_TERMINATE;
347 parser_state->aml = walk_state->aml_last_while;
348 walk_state->control_state->common.value = FALSE;
349 status = AE_CTRL_BREAK;
352 case AE_CTRL_CONTINUE:
355 parser_state->aml = walk_state->aml_last_while;
356 status = AE_CTRL_CONTINUE;
359 case AE_CTRL_PENDING:
361 parser_state->aml = walk_state->aml_last_while;
367 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
375 * Predicate of an IF was true, and we are at the matching ELSE.
376 * Just close out this package
378 parser_state->aml = acpi_ps_get_next_package_end (parser_state);
385 * Either an IF/WHILE Predicate was false or we encountered a BREAK
386 * opcode. In both cases, we do not execute the rest of the
387 * package; We simply close out the parent (finishing the walk of
388 * this branch of the tree) and continue execution at the parent
391 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
393 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
395 walk_state->control_state->common.value = FALSE;
396 status = AE_CTRL_END;
400 case AE_CTRL_TRANSFER:
402 /* A method call (invocation) -- transfer control */
404 status = AE_CTRL_TRANSFER;
405 walk_state->prev_op = op;
406 walk_state->method_call_op = op;
407 walk_state->method_call_node = (op->common.value.arg)->common.node;
409 /* Will return value (if any) be used by the caller? */
411 walk_state->return_used = acpi_ds_is_result_used (op, walk_state);
417 status = callback_status;
418 if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
424 return_ACPI_STATUS (status);
428 /*******************************************************************************
430 * FUNCTION: acpi_ps_parse_loop
432 * PARAMETERS: walk_state - Current state
436 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
439 ******************************************************************************/
443 struct acpi_walk_state *walk_state)
445 acpi_status status = AE_OK;
446 union acpi_parse_object *op = NULL; /* current op */
447 union acpi_parse_object *arg = NULL;
448 union acpi_parse_object *pre_op = NULL;
449 struct acpi_parse_state *parser_state;
450 u8 *aml_op_start = NULL;
453 ACPI_FUNCTION_TRACE_PTR ("ps_parse_loop", walk_state);
455 if (walk_state->descending_callback == NULL) {
456 return_ACPI_STATUS (AE_BAD_PARAMETER);
459 parser_state = &walk_state->parser_state;
460 walk_state->arg_types = 0;
462 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
464 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
465 /* We are restarting a preempted control method */
467 if (acpi_ps_has_completed_scope (parser_state)) {
469 * We must check if a predicate to an IF or WHILE statement
472 if ((parser_state->scope->parse_scope.op) &&
473 ((parser_state->scope->parse_scope.op->common.aml_opcode == AML_IF_OP) ||
474 (parser_state->scope->parse_scope.op->common.aml_opcode == AML_WHILE_OP)) &&
475 (walk_state->control_state) &&
476 (walk_state->control_state->common.state ==
477 ACPI_CONTROL_PREDICATE_EXECUTING)) {
479 * A predicate was just completed, get the value of the
480 * predicate and branch based on that value
482 walk_state->op = NULL;
483 status = acpi_ds_get_predicate_value (walk_state, ACPI_TO_POINTER (TRUE));
484 if (ACPI_FAILURE (status) &&
485 ((status & AE_CODE_MASK) != AE_CODE_CONTROL)) {
486 if (status == AE_AML_NO_RETURN_VALUE) {
487 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
488 "Invoked method did not return a value, %s\n",
489 acpi_format_exception (status)));
492 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
493 "get_predicate Failed, %s\n",
494 acpi_format_exception (status)));
495 return_ACPI_STATUS (status);
498 status = acpi_ps_next_parse_state (walk_state, op, status);
501 acpi_ps_pop_scope (parser_state, &op,
502 &walk_state->arg_types, &walk_state->arg_count);
503 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
505 else if (walk_state->prev_op) {
506 /* We were in the middle of an op */
508 op = walk_state->prev_op;
509 walk_state->arg_types = walk_state->prev_arg_types;
514 /* Iterative parsing loop, while there is more AML to process: */
516 while ((parser_state->aml < parser_state->aml_end) || (op)) {
517 aml_op_start = parser_state->aml;
519 /* Get the next opcode from the AML stream */
521 walk_state->aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
522 parser_state->aml_start);
523 walk_state->opcode = acpi_ps_peek_opcode (parser_state);
526 * First cut to determine what we have found:
527 * 1) A valid AML opcode
529 * 3) An unknown/invalid opcode
531 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
532 switch (walk_state->op_info->class) {
533 case AML_CLASS_ASCII:
534 case AML_CLASS_PREFIX:
536 * Starts with a valid prefix or ASCII char, this is a name
537 * string. Convert the bare name string to a namepath.
539 walk_state->opcode = AML_INT_NAMEPATH_OP;
540 walk_state->arg_types = ARGP_NAMESTRING;
543 case AML_CLASS_UNKNOWN:
545 /* The opcode is unrecognized. Just skip unknown opcodes */
547 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
548 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
549 walk_state->opcode, parser_state->aml, walk_state->aml_offset));
551 ACPI_DUMP_BUFFER (parser_state->aml, 128);
553 /* Assume one-byte bad opcode */
560 /* Found opcode info, this is a normal opcode */
562 parser_state->aml += acpi_ps_get_opcode_size (walk_state->opcode);
563 walk_state->arg_types = walk_state->op_info->parse_args;
567 /* Create Op structure and append to parent's argument list */
569 if (walk_state->op_info->flags & AML_NAMED) {
570 /* Allocate a new pre_op if necessary */
573 pre_op = acpi_ps_alloc_op (walk_state->opcode);
575 status = AE_NO_MEMORY;
580 pre_op->common.value.arg = NULL;
581 pre_op->common.aml_opcode = walk_state->opcode;
584 * Get and append arguments until we find the node that contains
585 * the name (the type ARGP_NAME).
587 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
588 (GET_CURRENT_ARG_TYPE (walk_state->arg_types) != ARGP_NAME)) {
589 status = acpi_ps_get_next_arg (walk_state, parser_state,
590 GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
591 if (ACPI_FAILURE (status)) {
595 acpi_ps_append_arg (pre_op, arg);
596 INCREMENT_ARG_LIST (walk_state->arg_types);
600 * Make sure that we found a NAME and didn't run out of
603 if (!GET_CURRENT_ARG_TYPE (walk_state->arg_types)) {
604 status = AE_AML_NO_OPERAND;
608 /* We know that this arg is a name, move to next arg */
610 INCREMENT_ARG_LIST (walk_state->arg_types);
613 * Find the object. This will either insert the object into
614 * the namespace or simply look it up
616 walk_state->op = NULL;
618 status = walk_state->descending_callback (walk_state, &op);
619 if (ACPI_FAILURE (status)) {
620 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
621 "During name lookup/catalog, %s\n",
622 acpi_format_exception (status)));
630 status = acpi_ps_next_parse_state (walk_state, op, status);
631 if (status == AE_CTRL_PENDING) {
636 if (ACPI_FAILURE (status)) {
640 acpi_ps_append_arg (op, pre_op->common.value.arg);
643 if (op->common.aml_opcode == AML_REGION_OP) {
645 * Defer final parsing of an operation_region body,
646 * because we don't have enough info in the first pass
647 * to parse it correctly (i.e., there may be method
648 * calls within the term_arg elements of the body.)
650 * However, we must continue parsing because
651 * the opregion is not a standalone package --
652 * we don't know where the end is at this point.
654 * (Length is unknown until parse of the body complete)
656 op->named.data = aml_op_start;
657 op->named.length = 0;
661 /* Not a named opcode, just allocate Op and append to parent */
663 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
664 op = acpi_ps_alloc_op (walk_state->opcode);
666 status = AE_NO_MEMORY;
670 if (walk_state->op_info->flags & AML_CREATE) {
672 * Backup to beginning of create_xXXfield declaration
673 * body_length is unknown until we parse the body
675 op->named.data = aml_op_start;
676 op->named.length = 0;
679 acpi_ps_append_arg (acpi_ps_get_parent_scope (parser_state), op);
681 if ((walk_state->descending_callback != NULL)) {
683 * Find the object. This will either insert the object into
684 * the namespace or simply look it up
688 status = walk_state->descending_callback (walk_state, &op);
689 status = acpi_ps_next_parse_state (walk_state, op, status);
690 if (status == AE_CTRL_PENDING) {
695 if (ACPI_FAILURE (status)) {
701 op->common.aml_offset = walk_state->aml_offset;
703 if (walk_state->op_info) {
704 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
705 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
706 (u32) op->common.aml_opcode, walk_state->op_info->name,
707 op, parser_state->aml, op->common.aml_offset));
713 * Start arg_count at zero because we don't know if there are
716 walk_state->arg_count = 0;
718 /* Are there any arguments that must be processed? */
720 if (walk_state->arg_types) {
723 switch (op->common.aml_opcode) {
724 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
725 case AML_WORD_OP: /* AML_WORDDATA_ARG */
726 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
727 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
728 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
730 /* Fill in constant or string argument directly */
732 acpi_ps_get_next_simple_arg (parser_state,
733 GET_CURRENT_ARG_TYPE (walk_state->arg_types), op);
736 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
738 status = acpi_ps_get_next_namepath (walk_state, parser_state, op, 1);
739 if (ACPI_FAILURE (status)) {
743 walk_state->arg_types = 0;
749 * Op is not a constant or string, append each argument
752 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
753 !walk_state->arg_count) {
754 walk_state->aml_offset = (u32)
755 ACPI_PTR_DIFF (parser_state->aml, parser_state->aml_start);
757 status = acpi_ps_get_next_arg (walk_state, parser_state,
758 GET_CURRENT_ARG_TYPE (walk_state->arg_types),
760 if (ACPI_FAILURE (status)) {
765 arg->common.aml_offset = walk_state->aml_offset;
766 acpi_ps_append_arg (op, arg);
768 INCREMENT_ARG_LIST (walk_state->arg_types);
771 /* Special processing for certain opcodes */
773 switch (op->common.aml_opcode) {
777 * Skip parsing of control method
778 * because we don't have enough info in the first pass
779 * to parse it correctly.
781 * Save the length and address of the body
783 op->named.data = parser_state->aml;
784 op->named.length = (u32) (parser_state->pkg_end -
787 /* Skip body of method */
789 parser_state->aml = parser_state->pkg_end;
790 walk_state->arg_count = 0;
795 case AML_VAR_PACKAGE_OP:
797 if ((op->common.parent) &&
798 (op->common.parent->common.aml_opcode == AML_NAME_OP) &&
799 (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
801 * Skip parsing of Buffers and Packages
802 * because we don't have enough info in the first pass
803 * to parse them correctly.
805 op->named.data = aml_op_start;
806 op->named.length = (u32) (parser_state->pkg_end -
811 parser_state->aml = parser_state->pkg_end;
812 walk_state->arg_count = 0;
818 if (walk_state->control_state) {
819 walk_state->control_state->control.package_end =
820 parser_state->pkg_end;
826 /* No action for all other opcodes */
833 /* Check for arguments that need to be processed */
835 if (walk_state->arg_count) {
837 * There are arguments (complex ones), push Op and
838 * prepare for argument
840 status = acpi_ps_push_scope (parser_state, op,
841 walk_state->arg_types, walk_state->arg_count);
842 if (ACPI_FAILURE (status)) {
850 * All arguments have been processed -- Op is complete,
853 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
854 if (walk_state->op_info->flags & AML_NAMED) {
855 if (acpi_gbl_depth) {
859 if (op->common.aml_opcode == AML_REGION_OP) {
861 * Skip parsing of control method or opregion body,
862 * because we don't have enough info in the first pass
863 * to parse them correctly.
865 * Completed parsing an op_region declaration, we now
868 op->named.length = (u32) (parser_state->aml - op->named.data);
872 if (walk_state->op_info->flags & AML_CREATE) {
874 * Backup to beginning of create_xXXfield declaration (1 for
877 * body_length is unknown until we parse the body
879 op->named.length = (u32) (parser_state->aml - op->named.data);
882 /* This op complete, notify the dispatcher */
884 if (walk_state->ascending_callback != NULL) {
886 walk_state->opcode = op->common.aml_opcode;
888 status = walk_state->ascending_callback (walk_state);
889 status = acpi_ps_next_parse_state (walk_state, op, status);
890 if (status == AE_CTRL_PENDING) {
899 * Finished one argument of the containing scope
901 parser_state->scope->parse_scope.arg_count--;
903 /* Close this Op (will result in parse subtree deletion) */
905 acpi_ps_complete_this_op (walk_state, op);
908 acpi_ps_free_op (pre_op);
917 case AE_CTRL_TRANSFER:
919 /* We are about to transfer to a called method. */
921 walk_state->prev_op = op;
922 walk_state->prev_arg_types = walk_state->arg_types;
923 return_ACPI_STATUS (status);
928 acpi_ps_pop_scope (parser_state, &op,
929 &walk_state->arg_types, &walk_state->arg_count);
933 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
934 walk_state->opcode = op->common.aml_opcode;
936 status = walk_state->ascending_callback (walk_state);
937 status = acpi_ps_next_parse_state (walk_state, op, status);
939 acpi_ps_complete_this_op (walk_state, op);
947 case AE_CTRL_CONTINUE:
949 /* Pop off scopes until we find the While */
951 while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
952 acpi_ps_pop_scope (parser_state, &op,
953 &walk_state->arg_types, &walk_state->arg_count);
956 /* Close this iteration of the While loop */
959 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
960 walk_state->opcode = op->common.aml_opcode;
962 status = walk_state->ascending_callback (walk_state);
963 status = acpi_ps_next_parse_state (walk_state, op, status);
965 acpi_ps_complete_this_op (walk_state, op);
972 case AE_CTRL_TERMINATE:
979 acpi_ps_complete_this_op (walk_state, op);
981 acpi_ps_pop_scope (parser_state, &op,
982 &walk_state->arg_types, &walk_state->arg_count);
986 return_ACPI_STATUS (status);
989 default: /* All other non-AE_OK status */
993 acpi_ps_complete_this_op (walk_state, op);
995 acpi_ps_pop_scope (parser_state, &op,
996 &walk_state->arg_types, &walk_state->arg_count);
1002 * TBD: Cleanup parse ops on error
1006 acpi_ps_pop_scope (parser_state, &op,
1007 &walk_state->arg_types, &walk_state->arg_count);
1010 walk_state->prev_op = op;
1011 walk_state->prev_arg_types = walk_state->arg_types;
1012 return_ACPI_STATUS (status);
1015 /* This scope complete? */
1017 if (acpi_ps_has_completed_scope (parser_state)) {
1018 acpi_ps_pop_scope (parser_state, &op,
1019 &walk_state->arg_types, &walk_state->arg_count);
1020 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
1026 } /* while parser_state->Aml */
1030 * Complete the last Op (if not completed), and clear the scope stack.
1031 * It is easily possible to end an AML "package" with an unbounded number
1032 * of open scopes (such as when several ASL blocks are closed with
1033 * sequential closing braces). We want to terminate each one cleanly.
1035 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", op));
1038 if (walk_state->ascending_callback != NULL) {
1039 walk_state->op = op;
1040 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
1041 walk_state->opcode = op->common.aml_opcode;
1043 status = walk_state->ascending_callback (walk_state);
1044 status = acpi_ps_next_parse_state (walk_state, op, status);
1045 if (status == AE_CTRL_PENDING) {
1050 if (status == AE_CTRL_TERMINATE) {
1056 acpi_ps_complete_this_op (walk_state, op);
1059 acpi_ps_pop_scope (parser_state, &op,
1060 &walk_state->arg_types, &walk_state->arg_count);
1064 return_ACPI_STATUS (status);
1067 else if (ACPI_FAILURE (status)) {
1068 acpi_ps_complete_this_op (walk_state, op);
1069 return_ACPI_STATUS (status);
1073 acpi_ps_complete_this_op (walk_state, op);
1076 acpi_ps_pop_scope (parser_state, &op, &walk_state->arg_types,
1077 &walk_state->arg_count);
1081 return_ACPI_STATUS (status);
1085 /*******************************************************************************
1087 * FUNCTION: acpi_ps_parse_aml
1089 * PARAMETERS: walk_state - Current state
1094 * DESCRIPTION: Parse raw AML and return a tree of ops
1096 ******************************************************************************/
1100 struct acpi_walk_state *walk_state)
1103 acpi_status terminate_status;
1104 struct acpi_thread_state *thread;
1105 struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
1106 struct acpi_walk_state *previous_walk_state;
1109 ACPI_FUNCTION_TRACE ("ps_parse_aml");
1111 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1112 "Entered with walk_state=%p Aml=%p size=%X\n",
1113 walk_state, walk_state->parser_state.aml,
1114 walk_state->parser_state.aml_size));
1117 /* Create and initialize a new thread state */
1119 thread = acpi_ut_create_thread_state ();
1121 return_ACPI_STATUS (AE_NO_MEMORY);
1124 walk_state->thread = thread;
1125 acpi_ds_push_walk_state (walk_state, thread);
1128 * This global allows the AML debugger to get a handle to the currently
1129 * executing control method.
1131 acpi_gbl_current_walk_list = thread;
1134 * Execute the walk loop as long as there is a valid Walk State. This
1135 * handles nested control method invocations without recursion.
1137 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", walk_state));
1140 while (walk_state) {
1141 if (ACPI_SUCCESS (status)) {
1143 * The parse_loop executes AML until the method terminates
1144 * or calls another method.
1146 status = acpi_ps_parse_loop (walk_state);
1149 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1150 "Completed one call to walk loop, %s State=%p\n",
1151 acpi_format_exception (status), walk_state));
1153 if (status == AE_CTRL_TRANSFER) {
1155 * A method call was detected.
1156 * Transfer control to the called control method
1158 status = acpi_ds_call_control_method (thread, walk_state, NULL);
1161 * If the transfer to the new method method call worked, a new walk
1162 * state was created -- get it
1164 walk_state = acpi_ds_get_current_walk_state (thread);
1167 else if (status == AE_CTRL_TERMINATE) {
1170 else if ((status != AE_OK) && (walk_state->method_desc)) {
1171 ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1172 walk_state->method_node, NULL, status);
1174 /* Check for possible multi-thread reentrancy problem */
1176 if ((status == AE_ALREADY_EXISTS) &&
1177 (!walk_state->method_desc->method.semaphore)) {
1179 * This method is marked not_serialized, but it tried to create
1180 * a named object, causing the second thread entrance to fail.
1181 * We will workaround this by marking the method permanently
1184 walk_state->method_desc->method.method_flags |= AML_METHOD_SERIALIZED;
1185 walk_state->method_desc->method.concurrency = 1;
1189 if (walk_state->method_desc) {
1190 /* Decrement the thread count on the method parse tree */
1192 if (walk_state->method_desc->method.thread_count) {
1193 walk_state->method_desc->method.thread_count--;
1197 /* We are done with this walk, move on to the parent if any */
1199 walk_state = acpi_ds_pop_walk_state (thread);
1201 /* Reset the current scope to the beginning of scope stack */
1203 acpi_ds_scope_stack_clear (walk_state);
1206 * If we just returned from the execution of a control method,
1207 * there's lots of cleanup to do
1209 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) {
1210 terminate_status = acpi_ds_terminate_control_method (walk_state);
1211 if (ACPI_FAILURE (terminate_status)) {
1212 ACPI_REPORT_ERROR ((
1213 "Could not terminate control method properly\n"));
1215 /* Ignore error and continue */
1219 /* Delete this walk state and all linked control states */
1221 acpi_ps_cleanup_scope (&walk_state->parser_state);
1223 previous_walk_state = walk_state;
1225 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1226 "return_value=%p, implicit_value=%p State=%p\n",
1227 walk_state->return_desc, walk_state->implicit_return_obj, walk_state));
1229 /* Check if we have restarted a preempted walk */
1231 walk_state = acpi_ds_get_current_walk_state (thread);
1233 if (ACPI_SUCCESS (status)) {
1235 * There is another walk state, restart it.
1236 * If the method return value is not used by the parent,
1237 * The object is deleted
1239 if (!previous_walk_state->return_desc) {
1240 status = acpi_ds_restart_control_method (walk_state,
1241 previous_walk_state->implicit_return_obj);
1245 * We have a valid return value, delete any implicit
1248 acpi_ds_clear_implicit_return (previous_walk_state);
1250 status = acpi_ds_restart_control_method (walk_state,
1251 previous_walk_state->return_desc);
1253 if (ACPI_SUCCESS (status)) {
1254 walk_state->walk_type |= ACPI_WALK_METHOD_RESTART;
1258 /* On error, delete any return object */
1260 acpi_ut_remove_reference (previous_walk_state->return_desc);
1265 * Just completed a 1st-level method, save the final internal return
1268 else if (previous_walk_state->caller_return_desc) {
1269 if (previous_walk_state->implicit_return_obj) {
1270 *(previous_walk_state->caller_return_desc) =
1271 previous_walk_state->implicit_return_obj;
1274 /* NULL if no return value */
1276 *(previous_walk_state->caller_return_desc) =
1277 previous_walk_state->return_desc;
1281 if (previous_walk_state->return_desc) {
1282 /* Caller doesn't want it, must delete it */
1284 acpi_ut_remove_reference (previous_walk_state->return_desc);
1286 if (previous_walk_state->implicit_return_obj) {
1287 /* Caller doesn't want it, must delete it */
1289 acpi_ut_remove_reference (previous_walk_state->implicit_return_obj);
1293 acpi_ds_delete_walk_state (previous_walk_state);
1298 acpi_ex_release_all_mutexes (thread);
1299 acpi_ut_delete_generic_state (ACPI_CAST_PTR (union acpi_generic_state, thread));
1300 acpi_gbl_current_walk_list = prev_walk_list;
1301 return_ACPI_STATUS (status);