1 /******************************************************************************
3 * Module Name: psparse - Parser top level AML parse routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2004, 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;
68 /*******************************************************************************
70 * FUNCTION: acpi_ps_get_opcode_size
72 * PARAMETERS: Opcode - An AML opcode
74 * RETURN: Size of the opcode, in bytes (1 or 2)
76 * DESCRIPTION: Get the size of the current opcode.
78 ******************************************************************************/
81 acpi_ps_get_opcode_size (
85 /* Extended (2-byte) opcode if > 255 */
87 if (opcode > 0x00FF) {
91 /* Otherwise, just a single byte opcode */
97 /*******************************************************************************
99 * FUNCTION: acpi_ps_peek_opcode
101 * PARAMETERS: parser_state - A parser state object
105 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
107 ******************************************************************************/
110 acpi_ps_peek_opcode (
111 struct acpi_parse_state *parser_state)
117 aml = parser_state->aml;
118 opcode = (u16) ACPI_GET8 (aml);
121 if (opcode == AML_EXTOP) {
122 /* Extended opcode */
125 opcode = (u16) ((opcode << 8) | ACPI_GET8 (aml));
132 /*******************************************************************************
134 * FUNCTION: acpi_ps_complete_this_op
136 * PARAMETERS: walk_state - Current State
137 * Op - Op to complete
141 * DESCRIPTION: Perform any cleanup at the completion of an Op.
143 ******************************************************************************/
146 acpi_ps_complete_this_op (
147 struct acpi_walk_state *walk_state,
148 union acpi_parse_object *op)
150 union acpi_parse_object *prev;
151 union acpi_parse_object *next;
152 const struct acpi_opcode_info *parent_info;
153 union acpi_parse_object *replacement_op = NULL;
156 ACPI_FUNCTION_TRACE_PTR ("ps_complete_this_op", op);
159 /* Check for null Op, can happen if AML code is corrupt */
165 /* Delete this op and the subtree below it if asked to */
167 if (((walk_state->parse_flags & ACPI_PARSE_TREE_MASK) == ACPI_PARSE_DELETE_TREE) &&
168 (walk_state->op_info->class != AML_CLASS_ARGUMENT)) {
169 /* Make sure that we only delete this subtree */
171 if (op->common.parent) {
173 * Check if we need to replace the operator and its subtree
174 * with a return value op (placeholder op)
176 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
178 switch (parent_info->class) {
179 case AML_CLASS_CONTROL:
182 case AML_CLASS_CREATE:
185 * These opcodes contain term_arg operands. The current
186 * op must be replaced by a placeholder return op
188 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
189 if (!replacement_op) {
194 case AML_CLASS_NAMED_OBJECT:
197 * These opcodes contain term_arg operands. The current
198 * op must be replaced by a placeholder return op
200 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
201 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
202 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
203 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
204 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
205 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
206 if (!replacement_op) {
211 if ((op->common.parent->common.aml_opcode == AML_NAME_OP) &&
212 (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
213 if ((op->common.aml_opcode == AML_BUFFER_OP) ||
214 (op->common.aml_opcode == AML_PACKAGE_OP) ||
215 (op->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
216 replacement_op = acpi_ps_alloc_op (op->common.aml_opcode);
217 if (!replacement_op) {
221 replacement_op->named.data = op->named.data;
222 replacement_op->named.length = op->named.length;
228 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
229 if (!replacement_op) {
234 /* We must unlink this op from the parent tree */
236 prev = op->common.parent->common.value.arg;
238 /* This op is the first in the list */
240 if (replacement_op) {
241 replacement_op->common.parent = op->common.parent;
242 replacement_op->common.value.arg = NULL;
243 replacement_op->common.node = op->common.node;
244 op->common.parent->common.value.arg = replacement_op;
245 replacement_op->common.next = op->common.next;
248 op->common.parent->common.value.arg = op->common.next;
252 /* Search the parent list */
255 /* Traverse all siblings in the parent's argument list */
257 next = prev->common.next;
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 prev->common.next = replacement_op;
264 replacement_op->common.next = op->common.next;
268 prev->common.next = op->common.next;
277 /* Now we can actually delete the subtree rooted at op */
279 acpi_ps_delete_parse_tree (op);
288 /*******************************************************************************
290 * FUNCTION: acpi_ps_next_parse_state
292 * PARAMETERS: parser_state - Current parser state object
296 * DESCRIPTION: Update the parser state based upon the return exception from
297 * the parser callback.
299 ******************************************************************************/
302 acpi_ps_next_parse_state (
303 struct acpi_walk_state *walk_state,
304 union acpi_parse_object *op,
305 acpi_status callback_status)
307 struct acpi_parse_state *parser_state = &walk_state->parser_state;
308 acpi_status status = AE_CTRL_PENDING;
311 ACPI_FUNCTION_TRACE_PTR ("ps_next_parse_state", op);
314 switch (callback_status) {
315 case AE_CTRL_TERMINATE:
318 * A control method was terminated via a RETURN statement.
319 * The walk of this method is complete.
321 parser_state->aml = parser_state->aml_end;
322 status = AE_CTRL_TERMINATE;
328 parser_state->aml = walk_state->aml_last_while;
329 walk_state->control_state->common.value = FALSE;
330 status = AE_CTRL_BREAK;
333 case AE_CTRL_CONTINUE:
336 parser_state->aml = walk_state->aml_last_while;
337 status = AE_CTRL_CONTINUE;
340 case AE_CTRL_PENDING:
342 parser_state->aml = walk_state->aml_last_while;
348 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
356 * Predicate of an IF was true, and we are at the matching ELSE.
357 * Just close out this package
359 parser_state->aml = acpi_ps_get_next_package_end (parser_state);
366 * Either an IF/WHILE Predicate was false or we encountered a BREAK
367 * opcode. In both cases, we do not execute the rest of the
368 * package; We simply close out the parent (finishing the walk of
369 * this branch of the tree) and continue execution at the parent
372 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
374 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
376 walk_state->control_state->common.value = FALSE;
377 status = AE_CTRL_END;
381 case AE_CTRL_TRANSFER:
384 * A method call (invocation) -- transfer control
386 status = AE_CTRL_TRANSFER;
387 walk_state->prev_op = op;
388 walk_state->method_call_op = op;
389 walk_state->method_call_node = (op->common.value.arg)->common.node;
391 /* Will return value (if any) be used by the caller? */
393 walk_state->return_used = acpi_ds_is_result_used (op, walk_state);
398 status = callback_status;
399 if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
405 return_ACPI_STATUS (status);
409 /*******************************************************************************
411 * FUNCTION: acpi_ps_parse_loop
413 * PARAMETERS: parser_state - Current parser state object
417 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
420 ******************************************************************************/
424 struct acpi_walk_state *walk_state)
426 acpi_status status = AE_OK;
427 union acpi_parse_object *op = NULL; /* current op */
428 union acpi_parse_object *arg = NULL;
429 union acpi_parse_object *pre_op = NULL;
430 struct acpi_parse_state *parser_state;
431 u8 *aml_op_start = NULL;
434 ACPI_FUNCTION_TRACE_PTR ("ps_parse_loop", walk_state);
436 if (walk_state->descending_callback == NULL) {
437 return_ACPI_STATUS (AE_BAD_PARAMETER);
440 parser_state = &walk_state->parser_state;
441 walk_state->arg_types = 0;
443 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
444 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
445 /* We are restarting a preempted control method */
447 if (acpi_ps_has_completed_scope (parser_state)) {
449 * We must check if a predicate to an IF or WHILE statement
452 if ((parser_state->scope->parse_scope.op) &&
453 ((parser_state->scope->parse_scope.op->common.aml_opcode == AML_IF_OP) ||
454 (parser_state->scope->parse_scope.op->common.aml_opcode == AML_WHILE_OP)) &&
455 (walk_state->control_state) &&
456 (walk_state->control_state->common.state ==
457 ACPI_CONTROL_PREDICATE_EXECUTING)) {
459 * A predicate was just completed, get the value of the
460 * predicate and branch based on that value
462 walk_state->op = NULL;
463 status = acpi_ds_get_predicate_value (walk_state, ACPI_TO_POINTER (TRUE));
464 if (ACPI_FAILURE (status) &&
465 ((status & AE_CODE_MASK) != AE_CODE_CONTROL)) {
466 if (status == AE_AML_NO_RETURN_VALUE) {
467 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
468 "Invoked method did not return a value, %s\n",
469 acpi_format_exception (status)));
472 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "get_predicate Failed, %s\n",
473 acpi_format_exception (status)));
474 return_ACPI_STATUS (status);
477 status = acpi_ps_next_parse_state (walk_state, op, status);
480 acpi_ps_pop_scope (parser_state, &op,
481 &walk_state->arg_types, &walk_state->arg_count);
482 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
484 else if (walk_state->prev_op) {
485 /* We were in the middle of an op */
487 op = walk_state->prev_op;
488 walk_state->arg_types = walk_state->prev_arg_types;
494 * Iterative parsing loop, while there is more aml to process:
496 while ((parser_state->aml < parser_state->aml_end) || (op)) {
497 aml_op_start = parser_state->aml;
499 /* Get the next opcode from the AML stream */
501 walk_state->aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
502 parser_state->aml_start);
503 walk_state->opcode = acpi_ps_peek_opcode (parser_state);
506 * First cut to determine what we have found:
507 * 1) A valid AML opcode
509 * 3) An unknown/invalid opcode
511 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
512 switch (walk_state->op_info->class) {
513 case AML_CLASS_ASCII:
514 case AML_CLASS_PREFIX:
516 * Starts with a valid prefix or ASCII char, this is a name
517 * string. Convert the bare name string to a namepath.
519 walk_state->opcode = AML_INT_NAMEPATH_OP;
520 walk_state->arg_types = ARGP_NAMESTRING;
523 case AML_CLASS_UNKNOWN:
525 /* The opcode is unrecognized. Just skip unknown opcodes */
527 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
528 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
529 walk_state->opcode, parser_state->aml, walk_state->aml_offset));
531 ACPI_DUMP_BUFFER (parser_state->aml, 128);
533 /* Assume one-byte bad opcode */
540 /* Found opcode info, this is a normal opcode */
542 parser_state->aml += acpi_ps_get_opcode_size (walk_state->opcode);
543 walk_state->arg_types = walk_state->op_info->parse_args;
547 /* Create Op structure and append to parent's argument list */
549 if (walk_state->op_info->flags & AML_NAMED) {
550 /* Allocate a new pre_op if necessary */
553 pre_op = acpi_ps_alloc_op (walk_state->opcode);
555 return_ACPI_STATUS (AE_NO_MEMORY);
559 pre_op->common.value.arg = NULL;
560 pre_op->common.aml_opcode = walk_state->opcode;
563 * Get and append arguments until we find the node that contains
564 * the name (the type ARGP_NAME).
566 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
567 (GET_CURRENT_ARG_TYPE (walk_state->arg_types) != ARGP_NAME)) {
568 status = acpi_ps_get_next_arg (walk_state, parser_state,
569 GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
570 if (ACPI_FAILURE (status)) {
574 acpi_ps_append_arg (pre_op, arg);
575 INCREMENT_ARG_LIST (walk_state->arg_types);
578 /* Make sure that we found a NAME and didn't run out of arguments */
580 if (!GET_CURRENT_ARG_TYPE (walk_state->arg_types)) {
581 return_ACPI_STATUS (AE_AML_NO_OPERAND);
584 /* We know that this arg is a name, move to next arg */
586 INCREMENT_ARG_LIST (walk_state->arg_types);
589 * Find the object. This will either insert the object into
590 * the namespace or simply look it up
592 walk_state->op = NULL;
594 status = walk_state->descending_callback (walk_state, &op);
595 if (ACPI_FAILURE (status)) {
596 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "During name lookup/catalog, %s\n",
597 acpi_format_exception (status)));
605 status = acpi_ps_next_parse_state (walk_state, op, status);
606 if (status == AE_CTRL_PENDING) {
611 if (ACPI_FAILURE (status)) {
615 acpi_ps_append_arg (op, pre_op->common.value.arg);
618 if (op->common.aml_opcode == AML_REGION_OP) {
620 * Defer final parsing of an operation_region body,
621 * because we don't have enough info in the first pass
622 * to parse it correctly (i.e., there may be method
623 * calls within the term_arg elements of the body.)
625 * However, we must continue parsing because
626 * the opregion is not a standalone package --
627 * we don't know where the end is at this point.
629 * (Length is unknown until parse of the body complete)
631 op->named.data = aml_op_start;
632 op->named.length = 0;
636 /* Not a named opcode, just allocate Op and append to parent */
638 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
639 op = acpi_ps_alloc_op (walk_state->opcode);
641 return_ACPI_STATUS (AE_NO_MEMORY);
644 if (walk_state->op_info->flags & AML_CREATE) {
646 * Backup to beginning of create_xXXfield declaration
647 * body_length is unknown until we parse the body
649 op->named.data = aml_op_start;
650 op->named.length = 0;
653 acpi_ps_append_arg (acpi_ps_get_parent_scope (parser_state), op);
655 if ((walk_state->descending_callback != NULL)) {
657 * Find the object. This will either insert the object into
658 * the namespace or simply look it up
662 status = walk_state->descending_callback (walk_state, &op);
663 status = acpi_ps_next_parse_state (walk_state, op, status);
664 if (status == AE_CTRL_PENDING) {
669 if (ACPI_FAILURE (status)) {
675 op->common.aml_offset = walk_state->aml_offset;
677 if (walk_state->op_info) {
678 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
679 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
680 (u32) op->common.aml_opcode, walk_state->op_info->name,
681 op, parser_state->aml, op->common.aml_offset));
686 /* Start arg_count at zero because we don't know if there are any args yet */
688 walk_state->arg_count = 0;
690 if (walk_state->arg_types) /* Are there any arguments that must be processed? */ {
693 switch (op->common.aml_opcode) {
694 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
695 case AML_WORD_OP: /* AML_WORDDATA_ARG */
696 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
697 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
698 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
700 /* Fill in constant or string argument directly */
702 acpi_ps_get_next_simple_arg (parser_state,
703 GET_CURRENT_ARG_TYPE (walk_state->arg_types), op);
706 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
708 status = acpi_ps_get_next_namepath (walk_state, parser_state, op, 1);
709 if (ACPI_FAILURE (status)) {
713 walk_state->arg_types = 0;
718 /* Op is not a constant or string, append each argument to the Op */
720 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
721 !walk_state->arg_count) {
722 walk_state->aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
723 parser_state->aml_start);
724 status = acpi_ps_get_next_arg (walk_state, parser_state,
725 GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
726 if (ACPI_FAILURE (status)) {
731 arg->common.aml_offset = walk_state->aml_offset;
732 acpi_ps_append_arg (op, arg);
734 INCREMENT_ARG_LIST (walk_state->arg_types);
737 /* Special processing for certain opcodes */
739 switch (op->common.aml_opcode) {
743 * Skip parsing of control method
744 * because we don't have enough info in the first pass
745 * to parse it correctly.
747 * Save the length and address of the body
749 op->named.data = parser_state->aml;
750 op->named.length = (u32) (parser_state->pkg_end - parser_state->aml);
752 /* Skip body of method */
754 parser_state->aml = parser_state->pkg_end;
755 walk_state->arg_count = 0;
760 case AML_VAR_PACKAGE_OP:
762 if ((op->common.parent) &&
763 (op->common.parent->common.aml_opcode == AML_NAME_OP) &&
764 (walk_state->descending_callback != acpi_ds_exec_begin_op)) {
766 * Skip parsing of Buffers and Packages
767 * because we don't have enough info in the first pass
768 * to parse them correctly.
770 op->named.data = aml_op_start;
771 op->named.length = (u32) (parser_state->pkg_end - aml_op_start);
775 parser_state->aml = parser_state->pkg_end;
776 walk_state->arg_count = 0;
782 if (walk_state->control_state) {
783 walk_state->control_state->control.package_end = parser_state->pkg_end;
789 /* No action for all other opcodes */
796 /* Check for arguments that need to be processed */
798 if (walk_state->arg_count) {
799 /* There are arguments (complex ones), push Op and prepare for argument */
801 status = acpi_ps_push_scope (parser_state, op,
802 walk_state->arg_types, walk_state->arg_count);
803 if (ACPI_FAILURE (status)) {
804 return_ACPI_STATUS (status);
810 /* All arguments have been processed -- Op is complete, prepare for next */
812 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
813 if (walk_state->op_info->flags & AML_NAMED) {
814 if (acpi_gbl_depth) {
818 if (op->common.aml_opcode == AML_REGION_OP) {
820 * Skip parsing of control method or opregion body,
821 * because we don't have enough info in the first pass
822 * to parse them correctly.
824 * Completed parsing an op_region declaration, we now
827 op->named.length = (u32) (parser_state->aml - op->named.data);
831 if (walk_state->op_info->flags & AML_CREATE) {
833 * Backup to beginning of create_xXXfield declaration (1 for
836 * body_length is unknown until we parse the body
838 op->named.length = (u32) (parser_state->aml - op->named.data);
841 /* This op complete, notify the dispatcher */
843 if (walk_state->ascending_callback != NULL) {
845 walk_state->opcode = op->common.aml_opcode;
847 status = walk_state->ascending_callback (walk_state);
848 status = acpi_ps_next_parse_state (walk_state, op, status);
849 if (status == AE_CTRL_PENDING) {
858 * Finished one argument of the containing scope
860 parser_state->scope->parse_scope.arg_count--;
862 /* Close this Op (will result in parse subtree deletion) */
864 acpi_ps_complete_this_op (walk_state, op);
867 acpi_ps_free_op (pre_op);
876 case AE_CTRL_TRANSFER:
879 * We are about to transfer to a called method.
881 walk_state->prev_op = op;
882 walk_state->prev_arg_types = walk_state->arg_types;
883 return_ACPI_STATUS (status);
888 acpi_ps_pop_scope (parser_state, &op,
889 &walk_state->arg_types, &walk_state->arg_count);
893 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
894 walk_state->opcode = op->common.aml_opcode;
896 status = walk_state->ascending_callback (walk_state);
897 status = acpi_ps_next_parse_state (walk_state, op, status);
899 acpi_ps_complete_this_op (walk_state, op);
907 case AE_CTRL_CONTINUE:
909 /* Pop off scopes until we find the While */
911 while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
912 acpi_ps_pop_scope (parser_state, &op,
913 &walk_state->arg_types, &walk_state->arg_count);
916 /* Close this iteration of the While loop */
919 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
920 walk_state->opcode = op->common.aml_opcode;
922 status = walk_state->ascending_callback (walk_state);
923 status = acpi_ps_next_parse_state (walk_state, op, status);
925 acpi_ps_complete_this_op (walk_state, op);
932 case AE_CTRL_TERMINATE:
939 acpi_ps_complete_this_op (walk_state, op);
941 acpi_ps_pop_scope (parser_state, &op,
942 &walk_state->arg_types, &walk_state->arg_count);
946 return_ACPI_STATUS (status);
949 default: /* All other non-AE_OK status */
953 acpi_ps_complete_this_op (walk_state, op);
955 acpi_ps_pop_scope (parser_state, &op,
956 &walk_state->arg_types, &walk_state->arg_count);
962 * TBD: Cleanup parse ops on error
966 acpi_ps_pop_scope (parser_state, &op,
967 &walk_state->arg_types, &walk_state->arg_count);
970 walk_state->prev_op = op;
971 walk_state->prev_arg_types = walk_state->arg_types;
972 return_ACPI_STATUS (status);
975 /* This scope complete? */
977 if (acpi_ps_has_completed_scope (parser_state)) {
978 acpi_ps_pop_scope (parser_state, &op,
979 &walk_state->arg_types, &walk_state->arg_count);
980 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
986 } /* while parser_state->Aml */
990 * Complete the last Op (if not completed), and clear the scope stack.
991 * It is easily possible to end an AML "package" with an unbounded number
992 * of open scopes (such as when several ASL blocks are closed with
993 * sequential closing braces). We want to terminate each one cleanly.
995 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", op));
998 if (walk_state->ascending_callback != NULL) {
1000 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
1001 walk_state->opcode = op->common.aml_opcode;
1003 status = walk_state->ascending_callback (walk_state);
1004 status = acpi_ps_next_parse_state (walk_state, op, status);
1005 if (status == AE_CTRL_PENDING) {
1010 if (status == AE_CTRL_TERMINATE) {
1016 acpi_ps_complete_this_op (walk_state, op);
1019 acpi_ps_pop_scope (parser_state, &op,
1020 &walk_state->arg_types, &walk_state->arg_count);
1024 return_ACPI_STATUS (status);
1027 else if (ACPI_FAILURE (status)) {
1028 acpi_ps_complete_this_op (walk_state, op);
1029 return_ACPI_STATUS (status);
1033 acpi_ps_complete_this_op (walk_state, op);
1036 acpi_ps_pop_scope (parser_state, &op, &walk_state->arg_types,
1037 &walk_state->arg_count);
1041 return_ACPI_STATUS (status);
1045 /*******************************************************************************
1047 * FUNCTION: acpi_ps_parse_aml
1049 * PARAMETERS: start_scope - The starting point of the parse. Becomes the
1050 * root of the parsed op tree.
1051 * Aml - Pointer to the raw AML code to parse
1052 * aml_size - Length of the AML to parse
1057 * DESCRIPTION: Parse raw AML and return a tree of ops
1059 ******************************************************************************/
1063 struct acpi_walk_state *walk_state)
1066 acpi_status terminate_status;
1067 struct acpi_thread_state *thread;
1068 struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
1069 struct acpi_walk_state *previous_walk_state;
1072 ACPI_FUNCTION_TRACE ("ps_parse_aml");
1074 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Entered with walk_state=%p Aml=%p size=%X\n",
1075 walk_state, walk_state->parser_state.aml, walk_state->parser_state.aml_size));
1078 /* Create and initialize a new thread state */
1080 thread = acpi_ut_create_thread_state ();
1082 return_ACPI_STATUS (AE_NO_MEMORY);
1085 walk_state->thread = thread;
1086 acpi_ds_push_walk_state (walk_state, thread);
1089 * This global allows the AML debugger to get a handle to the currently
1090 * executing control method.
1092 acpi_gbl_current_walk_list = thread;
1095 * Execute the walk loop as long as there is a valid Walk State. This
1096 * handles nested control method invocations without recursion.
1098 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", walk_state));
1101 while (walk_state) {
1102 if (ACPI_SUCCESS (status)) {
1104 * The parse_loop executes AML until the method terminates
1105 * or calls another method.
1107 status = acpi_ps_parse_loop (walk_state);
1110 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1111 "Completed one call to walk loop, %s State=%p\n",
1112 acpi_format_exception (status), walk_state));
1114 if (status == AE_CTRL_TRANSFER) {
1116 * A method call was detected.
1117 * Transfer control to the called control method
1119 status = acpi_ds_call_control_method (thread, walk_state, NULL);
1122 * If the transfer to the new method method call worked, a new walk
1123 * state was created -- get it
1125 walk_state = acpi_ds_get_current_walk_state (thread);
1128 else if (status == AE_CTRL_TERMINATE) {
1131 else if (status != AE_OK) {
1132 ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1133 walk_state->method_node, NULL, status);
1135 /* Check for possible multi-thread reentrancy problem */
1137 if ((status == AE_ALREADY_EXISTS) &&
1138 (!walk_state->method_desc->method.semaphore)) {
1140 * This method is marked not_serialized, but it tried to create a named
1141 * object, causing the second thread entrance to fail. We will workaround
1142 * this by marking the method permanently as Serialized.
1144 walk_state->method_desc->method.method_flags |= AML_METHOD_SERIALIZED;
1145 walk_state->method_desc->method.concurrency = 1;
1149 if (walk_state->method_desc) {
1150 /* Decrement the thread count on the method parse tree */
1152 if (walk_state->method_desc->method.thread_count) {
1153 walk_state->method_desc->method.thread_count--;
1157 /* We are done with this walk, move on to the parent if any */
1159 walk_state = acpi_ds_pop_walk_state (thread);
1161 /* Reset the current scope to the beginning of scope stack */
1163 acpi_ds_scope_stack_clear (walk_state);
1166 * If we just returned from the execution of a control method,
1167 * there's lots of cleanup to do
1169 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) {
1170 terminate_status = acpi_ds_terminate_control_method (walk_state);
1171 if (ACPI_FAILURE (terminate_status)) {
1172 ACPI_REPORT_ERROR ((
1173 "Could not terminate control method properly\n"));
1175 /* Ignore error and continue */
1179 /* Delete this walk state and all linked control states */
1181 acpi_ps_cleanup_scope (&walk_state->parser_state);
1183 previous_walk_state = walk_state;
1185 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "return_value=%p, State=%p\n",
1186 walk_state->return_desc, walk_state));
1188 /* Check if we have restarted a preempted walk */
1190 walk_state = acpi_ds_get_current_walk_state (thread);
1192 if (ACPI_SUCCESS (status)) {
1194 * There is another walk state, restart it.
1195 * If the method return value is not used by the parent,
1196 * The object is deleted
1198 status = acpi_ds_restart_control_method (walk_state,
1199 previous_walk_state->return_desc);
1200 if (ACPI_SUCCESS (status)) {
1201 walk_state->walk_type |= ACPI_WALK_METHOD_RESTART;
1205 /* On error, delete any return object */
1207 acpi_ut_remove_reference (previous_walk_state->return_desc);
1212 * Just completed a 1st-level method, save the final internal return
1215 else if (previous_walk_state->caller_return_desc) {
1216 *(previous_walk_state->caller_return_desc) = previous_walk_state->return_desc; /* NULL if no return value */
1218 else if (previous_walk_state->return_desc) {
1219 /* Caller doesn't want it, must delete it */
1221 acpi_ut_remove_reference (previous_walk_state->return_desc);
1224 acpi_ds_delete_walk_state (previous_walk_state);
1229 acpi_ex_release_all_mutexes (thread);
1230 acpi_ut_delete_generic_state (ACPI_CAST_PTR (union acpi_generic_state, thread));
1231 acpi_gbl_current_walk_list = prev_walk_list;
1232 return_ACPI_STATUS (status);