1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2006, 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.
45 * Parse the AML and build an operation tree as most interpreters,
46 * like Perl, do. Parsing is done by hand rather than with a YACC
47 * generated parser to tightly constrain stack and dynamic memory
48 * usage. At the same time, parsing is kept flexible and the code
49 * fairly compact by parsing based on a list of AML opcode
50 * templates in aml_op_info[]
53 #include <acpi/acpi.h>
54 #include <acpi/acparser.h>
55 #include <acpi/acdispat.h>
56 #include <acpi/amlcode.h>
58 #define _COMPONENT ACPI_PARSER
59 ACPI_MODULE_NAME("psloop")
61 static u32 acpi_gbl_depth
= 0;
63 /*******************************************************************************
65 * FUNCTION: acpi_ps_parse_loop
67 * PARAMETERS: walk_state - Current state
71 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
74 ******************************************************************************/
76 acpi_status
acpi_ps_parse_loop(struct acpi_walk_state
*walk_state
)
78 acpi_status status
= AE_OK
;
80 union acpi_parse_object
*op
= NULL
; /* current op */
81 union acpi_parse_object
*arg
= NULL
;
82 union acpi_parse_object
*pre_op
= NULL
;
83 struct acpi_parse_state
*parser_state
;
84 u8
*aml_op_start
= NULL
;
86 ACPI_FUNCTION_TRACE_PTR("ps_parse_loop", walk_state
);
88 if (walk_state
->descending_callback
== NULL
) {
89 return_ACPI_STATUS(AE_BAD_PARAMETER
);
92 parser_state
= &walk_state
->parser_state
;
93 walk_state
->arg_types
= 0;
95 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
97 if (walk_state
->walk_type
& ACPI_WALK_METHOD_RESTART
) {
98 /* We are restarting a preempted control method */
100 if (acpi_ps_has_completed_scope(parser_state
)) {
102 * We must check if a predicate to an IF or WHILE statement
105 if ((parser_state
->scope
->parse_scope
.op
) &&
106 ((parser_state
->scope
->parse_scope
.op
->common
.
107 aml_opcode
== AML_IF_OP
)
108 || (parser_state
->scope
->parse_scope
.op
->common
.
109 aml_opcode
== AML_WHILE_OP
))
110 && (walk_state
->control_state
)
111 && (walk_state
->control_state
->common
.state
==
112 ACPI_CONTROL_PREDICATE_EXECUTING
)) {
114 * A predicate was just completed, get the value of the
115 * predicate and branch based on that value
117 walk_state
->op
= NULL
;
119 acpi_ds_get_predicate_value(walk_state
,
122 if (ACPI_FAILURE(status
)
123 && ((status
& AE_CODE_MASK
) !=
125 if (status
== AE_AML_NO_RETURN_VALUE
) {
126 ACPI_EXCEPTION((AE_INFO
, status
,
127 "Invoked method did not return a value"));
130 ACPI_EXCEPTION((AE_INFO
, status
,
131 "get_predicate Failed"));
132 return_ACPI_STATUS(status
);
136 acpi_ps_next_parse_state(walk_state
, op
,
140 acpi_ps_pop_scope(parser_state
, &op
,
141 &walk_state
->arg_types
,
142 &walk_state
->arg_count
);
143 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
144 "Popped scope, Op=%p\n", op
));
145 } else if (walk_state
->prev_op
) {
146 /* We were in the middle of an op */
148 op
= walk_state
->prev_op
;
149 walk_state
->arg_types
= walk_state
->prev_arg_types
;
154 /* Iterative parsing loop, while there is more AML to process: */
156 while ((parser_state
->aml
< parser_state
->aml_end
) || (op
)) {
157 aml_op_start
= parser_state
->aml
;
159 /* Get the next opcode from the AML stream */
161 walk_state
->aml_offset
=
162 (u32
) ACPI_PTR_DIFF(parser_state
->aml
,
163 parser_state
->aml_start
);
164 walk_state
->opcode
= acpi_ps_peek_opcode(parser_state
);
167 * First cut to determine what we have found:
168 * 1) A valid AML opcode
170 * 3) An unknown/invalid opcode
172 walk_state
->op_info
=
173 acpi_ps_get_opcode_info(walk_state
->opcode
);
174 switch (walk_state
->op_info
->class) {
175 case AML_CLASS_ASCII
:
176 case AML_CLASS_PREFIX
:
178 * Starts with a valid prefix or ASCII char, this is a name
179 * string. Convert the bare name string to a namepath.
181 walk_state
->opcode
= AML_INT_NAMEPATH_OP
;
182 walk_state
->arg_types
= ARGP_NAMESTRING
;
185 case AML_CLASS_UNKNOWN
:
187 /* The opcode is unrecognized. Just skip unknown opcodes */
190 "Found unknown opcode %X at AML address %p offset %X, ignoring",
193 walk_state
->aml_offset
));
195 ACPI_DUMP_BUFFER(parser_state
->aml
, 128);
197 /* Assume one-byte bad opcode */
204 /* Found opcode info, this is a normal opcode */
207 acpi_ps_get_opcode_size(walk_state
->opcode
);
208 walk_state
->arg_types
=
209 walk_state
->op_info
->parse_args
;
213 /* Create Op structure and append to parent's argument list */
215 if (walk_state
->op_info
->flags
& AML_NAMED
) {
216 /* Allocate a new pre_op if necessary */
220 acpi_ps_alloc_op(walk_state
->
223 status
= AE_NO_MEMORY
;
228 pre_op
->common
.value
.arg
= NULL
;
229 pre_op
->common
.aml_opcode
= walk_state
->opcode
;
232 * Get and append arguments until we find the node that contains
233 * the name (the type ARGP_NAME).
235 while (GET_CURRENT_ARG_TYPE
236 (walk_state
->arg_types
)
238 (GET_CURRENT_ARG_TYPE
239 (walk_state
->arg_types
) != ARGP_NAME
)) {
241 acpi_ps_get_next_arg(walk_state
,
247 if (ACPI_FAILURE(status
)) {
251 acpi_ps_append_arg(pre_op
, arg
);
252 INCREMENT_ARG_LIST(walk_state
->
257 * Make sure that we found a NAME and didn't run out of
260 if (!GET_CURRENT_ARG_TYPE
261 (walk_state
->arg_types
)) {
262 status
= AE_AML_NO_OPERAND
;
266 /* We know that this arg is a name, move to next arg */
268 INCREMENT_ARG_LIST(walk_state
->arg_types
);
271 * Find the object. This will either insert the object into
272 * the namespace or simply look it up
274 walk_state
->op
= NULL
;
277 walk_state
->descending_callback(walk_state
,
279 if (ACPI_FAILURE(status
)) {
280 ACPI_EXCEPTION((AE_INFO
, status
,
281 "During name lookup/catalog"));
290 acpi_ps_next_parse_state(walk_state
, op
,
292 if (status
== AE_CTRL_PENDING
) {
297 if (ACPI_FAILURE(status
)) {
301 acpi_ps_append_arg(op
,
302 pre_op
->common
.value
.arg
);
305 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
307 * Defer final parsing of an operation_region body,
308 * because we don't have enough info in the first pass
309 * to parse it correctly (i.e., there may be method
310 * calls within the term_arg elements of the body.)
312 * However, we must continue parsing because
313 * the opregion is not a standalone package --
314 * we don't know where the end is at this point.
316 * (Length is unknown until parse of the body complete)
318 op
->named
.data
= aml_op_start
;
319 op
->named
.length
= 0;
322 /* Not a named opcode, just allocate Op and append to parent */
324 walk_state
->op_info
=
325 acpi_ps_get_opcode_info(walk_state
->opcode
);
326 op
= acpi_ps_alloc_op(walk_state
->opcode
);
328 status
= AE_NO_MEMORY
;
332 if (walk_state
->op_info
->flags
& AML_CREATE
) {
334 * Backup to beginning of create_xXXfield declaration
335 * body_length is unknown until we parse the body
337 op
->named
.data
= aml_op_start
;
338 op
->named
.length
= 0;
341 acpi_ps_append_arg(acpi_ps_get_parent_scope
344 if ((walk_state
->descending_callback
!= NULL
)) {
346 * Find the object. This will either insert the object into
347 * the namespace or simply look it up
353 descending_callback(walk_state
,
356 acpi_ps_next_parse_state(walk_state
,
359 if (status
== AE_CTRL_PENDING
) {
364 if (ACPI_FAILURE(status
)) {
370 op
->common
.aml_offset
= walk_state
->aml_offset
;
372 if (walk_state
->op_info
) {
373 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
374 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
375 (u32
) op
->common
.aml_opcode
,
376 walk_state
->op_info
->name
, op
,
378 op
->common
.aml_offset
));
383 * Start arg_count at zero because we don't know if there are
386 walk_state
->arg_count
= 0;
388 /* Are there any arguments that must be processed? */
390 if (walk_state
->arg_types
) {
393 switch (op
->common
.aml_opcode
) {
394 case AML_BYTE_OP
: /* AML_BYTEDATA_ARG */
395 case AML_WORD_OP
: /* AML_WORDDATA_ARG */
396 case AML_DWORD_OP
: /* AML_DWORDATA_ARG */
397 case AML_QWORD_OP
: /* AML_QWORDATA_ARG */
398 case AML_STRING_OP
: /* AML_ASCIICHARLIST_ARG */
400 /* Fill in constant or string argument directly */
402 acpi_ps_get_next_simple_arg(parser_state
,
408 case AML_INT_NAMEPATH_OP
: /* AML_NAMESTRING_ARG */
411 acpi_ps_get_next_namepath(walk_state
,
414 if (ACPI_FAILURE(status
)) {
418 walk_state
->arg_types
= 0;
423 * Op is not a constant or string, append each argument
426 while (GET_CURRENT_ARG_TYPE
427 (walk_state
->arg_types
)
428 && !walk_state
->arg_count
) {
429 walk_state
->aml_offset
= (u32
)
430 ACPI_PTR_DIFF(parser_state
->aml
,
435 acpi_ps_get_next_arg(walk_state
,
441 if (ACPI_FAILURE(status
)) {
446 arg
->common
.aml_offset
=
447 walk_state
->aml_offset
;
448 acpi_ps_append_arg(op
, arg
);
450 INCREMENT_ARG_LIST(walk_state
->
454 /* Special processing for certain opcodes */
456 /* TBD (remove): Temporary mechanism to disable this code if needed */
458 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
460 if ((walk_state
->pass_number
<=
461 ACPI_IMODE_LOAD_PASS1
)
464 parse_flags
& ACPI_PARSE_DISASSEMBLE
) ==
467 * We want to skip If/Else/While constructs during Pass1
468 * because we want to actually conditionally execute the
471 * Except for disassembly, where we always want to
472 * walk the If/Else/While packages
474 switch (op
->common
.aml_opcode
) {
479 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
480 "Pass1: Skipping an If/Else/While body\n"));
482 /* Skip body of if/else/while in pass 1 */
485 parser_state
->pkg_end
;
486 walk_state
->arg_count
= 0;
494 switch (op
->common
.aml_opcode
) {
498 * Skip parsing of control method
499 * because we don't have enough info in the first pass
500 * to parse it correctly.
502 * Save the length and address of the body
504 op
->named
.data
= parser_state
->aml
;
506 (u32
) (parser_state
->pkg_end
-
509 /* Skip body of method */
512 parser_state
->pkg_end
;
513 walk_state
->arg_count
= 0;
518 case AML_VAR_PACKAGE_OP
:
520 if ((op
->common
.parent
) &&
521 (op
->common
.parent
->common
.
522 aml_opcode
== AML_NAME_OP
)
523 && (walk_state
->pass_number
<=
524 ACPI_IMODE_LOAD_PASS2
)) {
526 * Skip parsing of Buffers and Packages
527 * because we don't have enough info in the first pass
528 * to parse them correctly.
530 op
->named
.data
= aml_op_start
;
532 (u32
) (parser_state
->
539 parser_state
->pkg_end
;
540 walk_state
->arg_count
= 0;
546 if (walk_state
->control_state
) {
547 walk_state
->control_state
->
548 control
.package_end
=
549 parser_state
->pkg_end
;
555 /* No action for all other opcodes */
562 /* Check for arguments that need to be processed */
564 if (walk_state
->arg_count
) {
566 * There are arguments (complex ones), push Op and
567 * prepare for argument
569 status
= acpi_ps_push_scope(parser_state
, op
,
570 walk_state
->arg_types
,
571 walk_state
->arg_count
);
572 if (ACPI_FAILURE(status
)) {
580 * All arguments have been processed -- Op is complete,
583 walk_state
->op_info
=
584 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
585 if (walk_state
->op_info
->flags
& AML_NAMED
) {
586 if (acpi_gbl_depth
) {
590 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
592 * Skip parsing of control method or opregion body,
593 * because we don't have enough info in the first pass
594 * to parse them correctly.
596 * Completed parsing an op_region declaration, we now
600 (u32
) (parser_state
->aml
- op
->named
.data
);
604 if (walk_state
->op_info
->flags
& AML_CREATE
) {
606 * Backup to beginning of create_xXXfield declaration (1 for
609 * body_length is unknown until we parse the body
612 (u32
) (parser_state
->aml
- op
->named
.data
);
615 /* This op complete, notify the dispatcher */
617 if (walk_state
->ascending_callback
!= NULL
) {
619 walk_state
->opcode
= op
->common
.aml_opcode
;
621 status
= walk_state
->ascending_callback(walk_state
);
623 acpi_ps_next_parse_state(walk_state
, op
, status
);
624 if (status
== AE_CTRL_PENDING
) {
632 * Finished one argument of the containing scope
634 parser_state
->scope
->parse_scope
.arg_count
--;
636 /* Finished with pre_op */
639 acpi_ps_free_op(pre_op
);
643 /* Close this Op (will result in parse subtree deletion) */
645 status2
= acpi_ps_complete_this_op(walk_state
, op
);
646 if (ACPI_FAILURE(status2
)) {
647 return_ACPI_STATUS(status2
);
655 case AE_CTRL_TRANSFER
:
657 /* We are about to transfer to a called method. */
659 walk_state
->prev_op
= op
;
660 walk_state
->prev_arg_types
= walk_state
->arg_types
;
661 return_ACPI_STATUS(status
);
665 acpi_ps_pop_scope(parser_state
, &op
,
666 &walk_state
->arg_types
,
667 &walk_state
->arg_count
);
671 walk_state
->op_info
=
672 acpi_ps_get_opcode_info(op
->common
.
674 walk_state
->opcode
= op
->common
.aml_opcode
;
677 walk_state
->ascending_callback(walk_state
);
679 acpi_ps_next_parse_state(walk_state
, op
,
683 acpi_ps_complete_this_op(walk_state
, op
);
684 if (ACPI_FAILURE(status2
)) {
685 return_ACPI_STATUS(status2
);
693 case AE_CTRL_CONTINUE
:
695 /* Pop off scopes until we find the While */
697 while (!op
|| (op
->common
.aml_opcode
!= AML_WHILE_OP
)) {
698 acpi_ps_pop_scope(parser_state
, &op
,
699 &walk_state
->arg_types
,
700 &walk_state
->arg_count
);
702 if (op
->common
.aml_opcode
!= AML_WHILE_OP
) {
704 acpi_ds_result_stack_pop
706 if (ACPI_FAILURE(status2
)) {
707 return_ACPI_STATUS(status2
);
712 /* Close this iteration of the While loop */
715 walk_state
->op_info
=
716 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
717 walk_state
->opcode
= op
->common
.aml_opcode
;
719 status
= walk_state
->ascending_callback(walk_state
);
721 acpi_ps_next_parse_state(walk_state
, op
, status
);
723 status2
= acpi_ps_complete_this_op(walk_state
, op
);
724 if (ACPI_FAILURE(status2
)) {
725 return_ACPI_STATUS(status2
);
732 case AE_CTRL_TERMINATE
:
740 acpi_ps_complete_this_op(walk_state
,
742 if (ACPI_FAILURE(status2
)) {
743 return_ACPI_STATUS(status2
);
746 acpi_ps_pop_scope(parser_state
, &op
,
747 &walk_state
->arg_types
,
748 &walk_state
->arg_count
);
752 return_ACPI_STATUS(status
);
754 default: /* All other non-AE_OK status */
759 acpi_ps_complete_this_op(walk_state
,
761 if (ACPI_FAILURE(status2
)) {
762 return_ACPI_STATUS(status2
);
765 acpi_ps_pop_scope(parser_state
, &op
,
766 &walk_state
->arg_types
,
767 &walk_state
->arg_count
);
772 * TBD: Cleanup parse ops on error
776 acpi_ps_pop_scope(parser_state
, &op
,
777 &walk_state
->arg_types
,
778 &walk_state
->arg_count
);
781 walk_state
->prev_op
= op
;
782 walk_state
->prev_arg_types
= walk_state
->arg_types
;
783 return_ACPI_STATUS(status
);
786 /* This scope complete? */
788 if (acpi_ps_has_completed_scope(parser_state
)) {
789 acpi_ps_pop_scope(parser_state
, &op
,
790 &walk_state
->arg_types
,
791 &walk_state
->arg_count
);
792 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
793 "Popped scope, Op=%p\n", op
));
798 } /* while parser_state->Aml */
801 * Complete the last Op (if not completed), and clear the scope stack.
802 * It is easily possible to end an AML "package" with an unbounded number
803 * of open scopes (such as when several ASL blocks are closed with
804 * sequential closing braces). We want to terminate each one cleanly.
806 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "AML package complete at Op %p\n",
810 if (walk_state
->ascending_callback
!= NULL
) {
812 walk_state
->op_info
=
813 acpi_ps_get_opcode_info(op
->common
.
815 walk_state
->opcode
= op
->common
.aml_opcode
;
818 walk_state
->ascending_callback(walk_state
);
820 acpi_ps_next_parse_state(walk_state
, op
,
822 if (status
== AE_CTRL_PENDING
) {
827 if (status
== AE_CTRL_TERMINATE
) {
834 acpi_ps_complete_this_op
843 acpi_ps_pop_scope(parser_state
,
852 return_ACPI_STATUS(status
);
855 else if (ACPI_FAILURE(status
)) {
856 /* First error is most important */
859 acpi_ps_complete_this_op(walk_state
,
861 return_ACPI_STATUS(status
);
865 status2
= acpi_ps_complete_this_op(walk_state
, op
);
866 if (ACPI_FAILURE(status2
)) {
867 return_ACPI_STATUS(status2
);
871 acpi_ps_pop_scope(parser_state
, &op
, &walk_state
->arg_types
,
872 &walk_state
->arg_count
);
876 return_ACPI_STATUS(status
);