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 */
178 return_ACPI_STATUS (AE_OK); /* OK for now */
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)) {
185 return_ACPI_STATUS (AE_OK);
188 /* Make sure that we only delete this subtree */
190 if (op->common.parent) {
191 prev = op->common.parent->common.value.arg;
193 /* Nothing more to do */
199 * Check if we need to replace the operator and its subtree
200 * with a return value op (placeholder op)
202 parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
204 switch (parent_info->class) {
205 case AML_CLASS_CONTROL:
208 case AML_CLASS_CREATE:
211 * These opcodes contain term_arg operands. The current
212 * op must be replaced by a placeholder return op
214 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
215 if (!replacement_op) {
220 case AML_CLASS_NAMED_OBJECT:
223 * These opcodes contain term_arg operands. The current
224 * op must be replaced by a placeholder return op
226 if ((op->common.parent->common.aml_opcode == AML_REGION_OP) ||
227 (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
228 (op->common.parent->common.aml_opcode == AML_BUFFER_OP) ||
229 (op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
230 (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
231 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
232 if (!replacement_op) {
236 else if ((op->common.parent->common.aml_opcode == AML_NAME_OP) &&
237 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2)) {
238 if ((op->common.aml_opcode == AML_BUFFER_OP) ||
239 (op->common.aml_opcode == AML_PACKAGE_OP) ||
240 (op->common.aml_opcode == AML_VAR_PACKAGE_OP)) {
241 replacement_op = acpi_ps_alloc_op (op->common.aml_opcode);
242 if (!replacement_op) {
246 replacement_op->named.data = op->named.data;
247 replacement_op->named.length = op->named.length;
254 replacement_op = acpi_ps_alloc_op (AML_INT_RETURN_VALUE_OP);
255 if (!replacement_op) {
260 /* We must unlink this op from the parent tree */
263 /* This op is the first in the list */
265 if (replacement_op) {
266 replacement_op->common.parent = op->common.parent;
267 replacement_op->common.value.arg = NULL;
268 replacement_op->common.node = op->common.node;
269 op->common.parent->common.value.arg = replacement_op;
270 replacement_op->common.next = op->common.next;
273 op->common.parent->common.value.arg = op->common.next;
277 /* Search the parent list */
280 /* Traverse all siblings in the parent's argument list */
282 next = prev->common.next;
284 if (replacement_op) {
285 replacement_op->common.parent = op->common.parent;
286 replacement_op->common.value.arg = NULL;
287 replacement_op->common.node = op->common.node;
288 prev->common.next = replacement_op;
289 replacement_op->common.next = op->common.next;
293 prev->common.next = op->common.next;
304 /* Now we can actually delete the subtree rooted at Op */
306 acpi_ps_delete_parse_tree (op);
307 return_ACPI_STATUS (AE_OK);
312 /* Always delete the subtree, even on error */
314 acpi_ps_delete_parse_tree (op);
315 return_ACPI_STATUS (AE_NO_MEMORY);
319 /*******************************************************************************
321 * FUNCTION: acpi_ps_next_parse_state
323 * PARAMETERS: walk_state - Current state
324 * Op - Current parse op
325 * callback_status - Status from previous operation
329 * DESCRIPTION: Update the parser state based upon the return exception from
330 * the parser callback.
332 ******************************************************************************/
335 acpi_ps_next_parse_state (
336 struct acpi_walk_state *walk_state,
337 union acpi_parse_object *op,
338 acpi_status callback_status)
340 struct acpi_parse_state *parser_state = &walk_state->parser_state;
341 acpi_status status = AE_CTRL_PENDING;
344 ACPI_FUNCTION_TRACE_PTR ("ps_next_parse_state", op);
347 switch (callback_status) {
348 case AE_CTRL_TERMINATE:
351 * A control method was terminated via a RETURN statement.
352 * The walk of this method is complete.
354 parser_state->aml = parser_state->aml_end;
355 status = AE_CTRL_TERMINATE;
361 parser_state->aml = walk_state->aml_last_while;
362 walk_state->control_state->common.value = FALSE;
363 status = AE_CTRL_BREAK;
366 case AE_CTRL_CONTINUE:
369 parser_state->aml = walk_state->aml_last_while;
370 status = AE_CTRL_CONTINUE;
373 case AE_CTRL_PENDING:
375 parser_state->aml = walk_state->aml_last_while;
381 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
389 * Predicate of an IF was true, and we are at the matching ELSE.
390 * Just close out this package
392 parser_state->aml = acpi_ps_get_next_package_end (parser_state);
399 * Either an IF/WHILE Predicate was false or we encountered a BREAK
400 * opcode. In both cases, we do not execute the rest of the
401 * package; We simply close out the parent (finishing the walk of
402 * this branch of the tree) and continue execution at the parent
405 parser_state->aml = parser_state->scope->parse_scope.pkg_end;
407 /* In the case of a BREAK, just force a predicate (if any) to FALSE */
409 walk_state->control_state->common.value = FALSE;
410 status = AE_CTRL_END;
414 case AE_CTRL_TRANSFER:
416 /* A method call (invocation) -- transfer control */
418 status = AE_CTRL_TRANSFER;
419 walk_state->prev_op = op;
420 walk_state->method_call_op = op;
421 walk_state->method_call_node = (op->common.value.arg)->common.node;
423 /* Will return value (if any) be used by the caller? */
425 walk_state->return_used = acpi_ds_is_result_used (op, walk_state);
431 status = callback_status;
432 if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
438 return_ACPI_STATUS (status);
442 /*******************************************************************************
444 * FUNCTION: acpi_ps_parse_loop
446 * PARAMETERS: walk_state - Current state
450 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
453 ******************************************************************************/
457 struct acpi_walk_state *walk_state)
459 acpi_status status = AE_OK;
461 union acpi_parse_object *op = NULL; /* current op */
462 union acpi_parse_object *arg = NULL;
463 union acpi_parse_object *pre_op = NULL;
464 struct acpi_parse_state *parser_state;
465 u8 *aml_op_start = NULL;
468 ACPI_FUNCTION_TRACE_PTR ("ps_parse_loop", walk_state);
470 if (walk_state->descending_callback == NULL) {
471 return_ACPI_STATUS (AE_BAD_PARAMETER);
474 parser_state = &walk_state->parser_state;
475 walk_state->arg_types = 0;
477 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
479 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
480 /* We are restarting a preempted control method */
482 if (acpi_ps_has_completed_scope (parser_state)) {
484 * We must check if a predicate to an IF or WHILE statement
487 if ((parser_state->scope->parse_scope.op) &&
488 ((parser_state->scope->parse_scope.op->common.aml_opcode == AML_IF_OP) ||
489 (parser_state->scope->parse_scope.op->common.aml_opcode == AML_WHILE_OP)) &&
490 (walk_state->control_state) &&
491 (walk_state->control_state->common.state ==
492 ACPI_CONTROL_PREDICATE_EXECUTING)) {
494 * A predicate was just completed, get the value of the
495 * predicate and branch based on that value
497 walk_state->op = NULL;
498 status = acpi_ds_get_predicate_value (walk_state, ACPI_TO_POINTER (TRUE));
499 if (ACPI_FAILURE (status) &&
500 ((status & AE_CODE_MASK) != AE_CODE_CONTROL)) {
501 if (status == AE_AML_NO_RETURN_VALUE) {
502 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
503 "Invoked method did not return a value, %s\n",
504 acpi_format_exception (status)));
507 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
508 "get_predicate Failed, %s\n",
509 acpi_format_exception (status)));
510 return_ACPI_STATUS (status);
513 status = acpi_ps_next_parse_state (walk_state, op, status);
516 acpi_ps_pop_scope (parser_state, &op,
517 &walk_state->arg_types, &walk_state->arg_count);
518 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
520 else if (walk_state->prev_op) {
521 /* We were in the middle of an op */
523 op = walk_state->prev_op;
524 walk_state->arg_types = walk_state->prev_arg_types;
529 /* Iterative parsing loop, while there is more AML to process: */
531 while ((parser_state->aml < parser_state->aml_end) || (op)) {
532 aml_op_start = parser_state->aml;
534 /* Get the next opcode from the AML stream */
536 walk_state->aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
537 parser_state->aml_start);
538 walk_state->opcode = acpi_ps_peek_opcode (parser_state);
541 * First cut to determine what we have found:
542 * 1) A valid AML opcode
544 * 3) An unknown/invalid opcode
546 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
547 switch (walk_state->op_info->class) {
548 case AML_CLASS_ASCII:
549 case AML_CLASS_PREFIX:
551 * Starts with a valid prefix or ASCII char, this is a name
552 * string. Convert the bare name string to a namepath.
554 walk_state->opcode = AML_INT_NAMEPATH_OP;
555 walk_state->arg_types = ARGP_NAMESTRING;
558 case AML_CLASS_UNKNOWN:
560 /* The opcode is unrecognized. Just skip unknown opcodes */
562 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
563 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
564 walk_state->opcode, parser_state->aml, walk_state->aml_offset));
566 ACPI_DUMP_BUFFER (parser_state->aml, 128);
568 /* Assume one-byte bad opcode */
575 /* Found opcode info, this is a normal opcode */
577 parser_state->aml += acpi_ps_get_opcode_size (walk_state->opcode);
578 walk_state->arg_types = walk_state->op_info->parse_args;
582 /* Create Op structure and append to parent's argument list */
584 if (walk_state->op_info->flags & AML_NAMED) {
585 /* Allocate a new pre_op if necessary */
588 pre_op = acpi_ps_alloc_op (walk_state->opcode);
590 status = AE_NO_MEMORY;
595 pre_op->common.value.arg = NULL;
596 pre_op->common.aml_opcode = walk_state->opcode;
599 * Get and append arguments until we find the node that contains
600 * the name (the type ARGP_NAME).
602 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
603 (GET_CURRENT_ARG_TYPE (walk_state->arg_types) != ARGP_NAME)) {
604 status = acpi_ps_get_next_arg (walk_state, parser_state,
605 GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
606 if (ACPI_FAILURE (status)) {
610 acpi_ps_append_arg (pre_op, arg);
611 INCREMENT_ARG_LIST (walk_state->arg_types);
615 * Make sure that we found a NAME and didn't run out of
618 if (!GET_CURRENT_ARG_TYPE (walk_state->arg_types)) {
619 status = AE_AML_NO_OPERAND;
623 /* We know that this arg is a name, move to next arg */
625 INCREMENT_ARG_LIST (walk_state->arg_types);
628 * Find the object. This will either insert the object into
629 * the namespace or simply look it up
631 walk_state->op = NULL;
633 status = walk_state->descending_callback (walk_state, &op);
634 if (ACPI_FAILURE (status)) {
635 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
636 "During name lookup/catalog, %s\n",
637 acpi_format_exception (status)));
645 status = acpi_ps_next_parse_state (walk_state, op, status);
646 if (status == AE_CTRL_PENDING) {
651 if (ACPI_FAILURE (status)) {
655 acpi_ps_append_arg (op, pre_op->common.value.arg);
658 if (op->common.aml_opcode == AML_REGION_OP) {
660 * Defer final parsing of an operation_region body,
661 * because we don't have enough info in the first pass
662 * to parse it correctly (i.e., there may be method
663 * calls within the term_arg elements of the body.)
665 * However, we must continue parsing because
666 * the opregion is not a standalone package --
667 * we don't know where the end is at this point.
669 * (Length is unknown until parse of the body complete)
671 op->named.data = aml_op_start;
672 op->named.length = 0;
676 /* Not a named opcode, just allocate Op and append to parent */
678 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
679 op = acpi_ps_alloc_op (walk_state->opcode);
681 status = AE_NO_MEMORY;
685 if (walk_state->op_info->flags & AML_CREATE) {
687 * Backup to beginning of create_xXXfield declaration
688 * body_length is unknown until we parse the body
690 op->named.data = aml_op_start;
691 op->named.length = 0;
694 acpi_ps_append_arg (acpi_ps_get_parent_scope (parser_state), op);
696 if ((walk_state->descending_callback != NULL)) {
698 * Find the object. This will either insert the object into
699 * the namespace or simply look it up
703 status = walk_state->descending_callback (walk_state, &op);
704 status = acpi_ps_next_parse_state (walk_state, op, status);
705 if (status == AE_CTRL_PENDING) {
710 if (ACPI_FAILURE (status)) {
716 op->common.aml_offset = walk_state->aml_offset;
718 if (walk_state->op_info) {
719 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
720 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
721 (u32) op->common.aml_opcode, walk_state->op_info->name,
722 op, parser_state->aml, op->common.aml_offset));
728 * Start arg_count at zero because we don't know if there are
731 walk_state->arg_count = 0;
733 /* Are there any arguments that must be processed? */
735 if (walk_state->arg_types) {
738 switch (op->common.aml_opcode) {
739 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
740 case AML_WORD_OP: /* AML_WORDDATA_ARG */
741 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
742 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
743 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
745 /* Fill in constant or string argument directly */
747 acpi_ps_get_next_simple_arg (parser_state,
748 GET_CURRENT_ARG_TYPE (walk_state->arg_types), op);
751 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
753 status = acpi_ps_get_next_namepath (walk_state, parser_state, op, 1);
754 if (ACPI_FAILURE (status)) {
758 walk_state->arg_types = 0;
763 * Op is not a constant or string, append each argument
766 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
767 !walk_state->arg_count) {
768 walk_state->aml_offset = (u32)
769 ACPI_PTR_DIFF (parser_state->aml, parser_state->aml_start);
771 status = acpi_ps_get_next_arg (walk_state, parser_state,
772 GET_CURRENT_ARG_TYPE (walk_state->arg_types),
774 if (ACPI_FAILURE (status)) {
779 arg->common.aml_offset = walk_state->aml_offset;
780 acpi_ps_append_arg (op, arg);
782 INCREMENT_ARG_LIST (walk_state->arg_types);
785 /* Special processing for certain opcodes */
787 if (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS1) {
788 switch (op->common.aml_opcode) {
793 /* Skip body of if/else/while in pass 1 */
795 parser_state->aml = parser_state->pkg_end;
796 walk_state->arg_count = 0;
804 switch (op->common.aml_opcode) {
808 * Skip parsing of control method
809 * because we don't have enough info in the first pass
810 * to parse it correctly.
812 * Save the length and address of the body
814 op->named.data = parser_state->aml;
815 op->named.length = (u32) (parser_state->pkg_end -
818 /* Skip body of method */
820 parser_state->aml = parser_state->pkg_end;
821 walk_state->arg_count = 0;
826 case AML_VAR_PACKAGE_OP:
828 if ((op->common.parent) &&
829 (op->common.parent->common.aml_opcode == AML_NAME_OP) &&
830 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2)) {
832 * Skip parsing of Buffers and Packages
833 * because we don't have enough info in the first pass
834 * to parse them correctly.
836 op->named.data = aml_op_start;
837 op->named.length = (u32) (parser_state->pkg_end -
842 parser_state->aml = parser_state->pkg_end;
843 walk_state->arg_count = 0;
849 if (walk_state->control_state) {
850 walk_state->control_state->control.package_end =
851 parser_state->pkg_end;
857 /* No action for all other opcodes */
864 /* Check for arguments that need to be processed */
866 if (walk_state->arg_count) {
868 * There are arguments (complex ones), push Op and
869 * prepare for argument
871 status = acpi_ps_push_scope (parser_state, op,
872 walk_state->arg_types, walk_state->arg_count);
873 if (ACPI_FAILURE (status)) {
881 * All arguments have been processed -- Op is complete,
884 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
885 if (walk_state->op_info->flags & AML_NAMED) {
886 if (acpi_gbl_depth) {
890 if (op->common.aml_opcode == AML_REGION_OP) {
892 * Skip parsing of control method or opregion body,
893 * because we don't have enough info in the first pass
894 * to parse them correctly.
896 * Completed parsing an op_region declaration, we now
899 op->named.length = (u32) (parser_state->aml - op->named.data);
903 if (walk_state->op_info->flags & AML_CREATE) {
905 * Backup to beginning of create_xXXfield declaration (1 for
908 * body_length is unknown until we parse the body
910 op->named.length = (u32) (parser_state->aml - op->named.data);
913 /* This op complete, notify the dispatcher */
915 if (walk_state->ascending_callback != NULL) {
917 walk_state->opcode = op->common.aml_opcode;
919 status = walk_state->ascending_callback (walk_state);
920 status = acpi_ps_next_parse_state (walk_state, op, status);
921 if (status == AE_CTRL_PENDING) {
930 * Finished one argument of the containing scope
932 parser_state->scope->parse_scope.arg_count--;
934 /* Finished with pre_op */
937 acpi_ps_free_op (pre_op);
941 /* Close this Op (will result in parse subtree deletion) */
943 status2 = acpi_ps_complete_this_op (walk_state, op);
944 if (ACPI_FAILURE (status2)) {
945 return_ACPI_STATUS (status2);
954 case AE_CTRL_TRANSFER:
956 /* We are about to transfer to a called method. */
958 walk_state->prev_op = op;
959 walk_state->prev_arg_types = walk_state->arg_types;
960 return_ACPI_STATUS (status);
965 acpi_ps_pop_scope (parser_state, &op,
966 &walk_state->arg_types, &walk_state->arg_count);
970 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
971 walk_state->opcode = op->common.aml_opcode;
973 status = walk_state->ascending_callback (walk_state);
974 status = acpi_ps_next_parse_state (walk_state, op, status);
976 status2 = acpi_ps_complete_this_op (walk_state, op);
977 if (ACPI_FAILURE (status2)) {
978 return_ACPI_STATUS (status2);
987 case AE_CTRL_CONTINUE:
989 /* Pop off scopes until we find the While */
991 while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
992 acpi_ps_pop_scope (parser_state, &op,
993 &walk_state->arg_types, &walk_state->arg_count);
996 /* Close this iteration of the While loop */
999 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
1000 walk_state->opcode = op->common.aml_opcode;
1002 status = walk_state->ascending_callback (walk_state);
1003 status = acpi_ps_next_parse_state (walk_state, op, status);
1005 status2 = acpi_ps_complete_this_op (walk_state, op);
1006 if (ACPI_FAILURE (status2)) {
1007 return_ACPI_STATUS (status2);
1015 case AE_CTRL_TERMINATE:
1022 status2 = acpi_ps_complete_this_op (walk_state, op);
1023 if (ACPI_FAILURE (status2)) {
1024 return_ACPI_STATUS (status2);
1027 acpi_ps_pop_scope (parser_state, &op,
1028 &walk_state->arg_types, &walk_state->arg_count);
1032 return_ACPI_STATUS (status);
1035 default: /* All other non-AE_OK status */
1039 status2 = acpi_ps_complete_this_op (walk_state, op);
1040 if (ACPI_FAILURE (status2)) {
1041 return_ACPI_STATUS (status2);
1044 acpi_ps_pop_scope (parser_state, &op,
1045 &walk_state->arg_types, &walk_state->arg_count);
1051 * TBD: Cleanup parse ops on error
1055 acpi_ps_pop_scope (parser_state, &op,
1056 &walk_state->arg_types, &walk_state->arg_count);
1059 walk_state->prev_op = op;
1060 walk_state->prev_arg_types = walk_state->arg_types;
1061 return_ACPI_STATUS (status);
1064 /* This scope complete? */
1066 if (acpi_ps_has_completed_scope (parser_state)) {
1067 acpi_ps_pop_scope (parser_state, &op,
1068 &walk_state->arg_types, &walk_state->arg_count);
1069 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
1075 } /* while parser_state->Aml */
1079 * Complete the last Op (if not completed), and clear the scope stack.
1080 * It is easily possible to end an AML "package" with an unbounded number
1081 * of open scopes (such as when several ASL blocks are closed with
1082 * sequential closing braces). We want to terminate each one cleanly.
1084 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", op));
1087 if (walk_state->ascending_callback != NULL) {
1088 walk_state->op = op;
1089 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
1090 walk_state->opcode = op->common.aml_opcode;
1092 status = walk_state->ascending_callback (walk_state);
1093 status = acpi_ps_next_parse_state (walk_state, op, status);
1094 if (status == AE_CTRL_PENDING) {
1099 if (status == AE_CTRL_TERMINATE) {
1105 status2 = acpi_ps_complete_this_op (walk_state, op);
1106 if (ACPI_FAILURE (status2)) {
1107 return_ACPI_STATUS (status2);
1111 acpi_ps_pop_scope (parser_state, &op,
1112 &walk_state->arg_types, &walk_state->arg_count);
1116 return_ACPI_STATUS (status);
1119 else if (ACPI_FAILURE (status)) {
1120 /* First error is most important */
1122 (void) acpi_ps_complete_this_op (walk_state, op);
1123 return_ACPI_STATUS (status);
1127 status2 = acpi_ps_complete_this_op (walk_state, op);
1128 if (ACPI_FAILURE (status2)) {
1129 return_ACPI_STATUS (status2);
1133 acpi_ps_pop_scope (parser_state, &op, &walk_state->arg_types,
1134 &walk_state->arg_count);
1138 return_ACPI_STATUS (status);
1142 /*******************************************************************************
1144 * FUNCTION: acpi_ps_parse_aml
1146 * PARAMETERS: walk_state - Current state
1151 * DESCRIPTION: Parse raw AML and return a tree of ops
1153 ******************************************************************************/
1157 struct acpi_walk_state *walk_state)
1160 acpi_status terminate_status;
1161 struct acpi_thread_state *thread;
1162 struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
1163 struct acpi_walk_state *previous_walk_state;
1166 ACPI_FUNCTION_TRACE ("ps_parse_aml");
1168 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1169 "Entered with walk_state=%p Aml=%p size=%X\n",
1170 walk_state, walk_state->parser_state.aml,
1171 walk_state->parser_state.aml_size));
1174 /* Create and initialize a new thread state */
1176 thread = acpi_ut_create_thread_state ();
1178 return_ACPI_STATUS (AE_NO_MEMORY);
1181 walk_state->thread = thread;
1182 acpi_ds_push_walk_state (walk_state, thread);
1185 * This global allows the AML debugger to get a handle to the currently
1186 * executing control method.
1188 acpi_gbl_current_walk_list = thread;
1191 * Execute the walk loop as long as there is a valid Walk State. This
1192 * handles nested control method invocations without recursion.
1194 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "State=%p\n", walk_state));
1197 while (walk_state) {
1198 if (ACPI_SUCCESS (status)) {
1200 * The parse_loop executes AML until the method terminates
1201 * or calls another method.
1203 status = acpi_ps_parse_loop (walk_state);
1206 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1207 "Completed one call to walk loop, %s State=%p\n",
1208 acpi_format_exception (status), walk_state));
1210 if (status == AE_CTRL_TRANSFER) {
1212 * A method call was detected.
1213 * Transfer control to the called control method
1215 status = acpi_ds_call_control_method (thread, walk_state, NULL);
1218 * If the transfer to the new method method call worked, a new walk
1219 * state was created -- get it
1221 walk_state = acpi_ds_get_current_walk_state (thread);
1224 else if (status == AE_CTRL_TERMINATE) {
1227 else if ((status != AE_OK) && (walk_state->method_desc)) {
1228 ACPI_REPORT_METHOD_ERROR ("Method execution failed",
1229 walk_state->method_node, NULL, status);
1231 /* Check for possible multi-thread reentrancy problem */
1233 if ((status == AE_ALREADY_EXISTS) &&
1234 (!walk_state->method_desc->method.semaphore)) {
1236 * This method is marked not_serialized, but it tried to create
1237 * a named object, causing the second thread entrance to fail.
1238 * We will workaround this by marking the method permanently
1241 walk_state->method_desc->method.method_flags |= AML_METHOD_SERIALIZED;
1242 walk_state->method_desc->method.concurrency = 1;
1246 if (walk_state->method_desc) {
1247 /* Decrement the thread count on the method parse tree */
1249 if (walk_state->method_desc->method.thread_count) {
1250 walk_state->method_desc->method.thread_count--;
1254 /* We are done with this walk, move on to the parent if any */
1256 walk_state = acpi_ds_pop_walk_state (thread);
1258 /* Reset the current scope to the beginning of scope stack */
1260 acpi_ds_scope_stack_clear (walk_state);
1263 * If we just returned from the execution of a control method,
1264 * there's lots of cleanup to do
1266 if ((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) {
1267 terminate_status = acpi_ds_terminate_control_method (walk_state);
1268 if (ACPI_FAILURE (terminate_status)) {
1269 ACPI_REPORT_ERROR ((
1270 "Could not terminate control method properly\n"));
1272 /* Ignore error and continue */
1276 /* Delete this walk state and all linked control states */
1278 acpi_ps_cleanup_scope (&walk_state->parser_state);
1280 previous_walk_state = walk_state;
1282 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
1283 "return_value=%p, implicit_value=%p State=%p\n",
1284 walk_state->return_desc, walk_state->implicit_return_obj, walk_state));
1286 /* Check if we have restarted a preempted walk */
1288 walk_state = acpi_ds_get_current_walk_state (thread);
1290 if (ACPI_SUCCESS (status)) {
1292 * There is another walk state, restart it.
1293 * If the method return value is not used by the parent,
1294 * The object is deleted
1296 if (!previous_walk_state->return_desc) {
1297 status = acpi_ds_restart_control_method (walk_state,
1298 previous_walk_state->implicit_return_obj);
1302 * We have a valid return value, delete any implicit
1305 acpi_ds_clear_implicit_return (previous_walk_state);
1307 status = acpi_ds_restart_control_method (walk_state,
1308 previous_walk_state->return_desc);
1310 if (ACPI_SUCCESS (status)) {
1311 walk_state->walk_type |= ACPI_WALK_METHOD_RESTART;
1315 /* On error, delete any return object */
1317 acpi_ut_remove_reference (previous_walk_state->return_desc);
1322 * Just completed a 1st-level method, save the final internal return
1325 else if (previous_walk_state->caller_return_desc) {
1326 if (previous_walk_state->implicit_return_obj) {
1327 *(previous_walk_state->caller_return_desc) =
1328 previous_walk_state->implicit_return_obj;
1331 /* NULL if no return value */
1333 *(previous_walk_state->caller_return_desc) =
1334 previous_walk_state->return_desc;
1338 if (previous_walk_state->return_desc) {
1339 /* Caller doesn't want it, must delete it */
1341 acpi_ut_remove_reference (previous_walk_state->return_desc);
1343 if (previous_walk_state->implicit_return_obj) {
1344 /* Caller doesn't want it, must delete it */
1346 acpi_ut_remove_reference (previous_walk_state->implicit_return_obj);
1350 acpi_ds_delete_walk_state (previous_walk_state);
1355 acpi_ex_release_all_mutexes (thread);
1356 acpi_ut_delete_generic_state (ACPI_CAST_PTR (union acpi_generic_state, thread));
1357 acpi_gbl_current_walk_list = prev_walk_list;
1358 return_ACPI_STATUS (status);