1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
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.
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_DEBUG_PRINT((ACPI_DB_ERROR
,
127 "Invoked method did not return a value, %s\n",
128 acpi_format_exception
132 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
133 "get_predicate Failed, %s\n",
134 acpi_format_exception
136 return_ACPI_STATUS(status
);
140 acpi_ps_next_parse_state(walk_state
, op
,
144 acpi_ps_pop_scope(parser_state
, &op
,
145 &walk_state
->arg_types
,
146 &walk_state
->arg_count
);
147 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
148 "Popped scope, Op=%p\n", op
));
149 } else if (walk_state
->prev_op
) {
150 /* We were in the middle of an op */
152 op
= walk_state
->prev_op
;
153 walk_state
->arg_types
= walk_state
->prev_arg_types
;
158 /* Iterative parsing loop, while there is more AML to process: */
160 while ((parser_state
->aml
< parser_state
->aml_end
) || (op
)) {
161 aml_op_start
= parser_state
->aml
;
163 /* Get the next opcode from the AML stream */
165 walk_state
->aml_offset
=
166 (u32
) ACPI_PTR_DIFF(parser_state
->aml
,
167 parser_state
->aml_start
);
168 walk_state
->opcode
= acpi_ps_peek_opcode(parser_state
);
171 * First cut to determine what we have found:
172 * 1) A valid AML opcode
174 * 3) An unknown/invalid opcode
176 walk_state
->op_info
=
177 acpi_ps_get_opcode_info(walk_state
->opcode
);
178 switch (walk_state
->op_info
->class) {
179 case AML_CLASS_ASCII
:
180 case AML_CLASS_PREFIX
:
182 * Starts with a valid prefix or ASCII char, this is a name
183 * string. Convert the bare name string to a namepath.
185 walk_state
->opcode
= AML_INT_NAMEPATH_OP
;
186 walk_state
->arg_types
= ARGP_NAMESTRING
;
189 case AML_CLASS_UNKNOWN
:
191 /* The opcode is unrecognized. Just skip unknown opcodes */
193 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
194 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
197 walk_state
->aml_offset
));
199 ACPI_DUMP_BUFFER(parser_state
->aml
, 128);
201 /* Assume one-byte bad opcode */
208 /* Found opcode info, this is a normal opcode */
211 acpi_ps_get_opcode_size(walk_state
->opcode
);
212 walk_state
->arg_types
=
213 walk_state
->op_info
->parse_args
;
217 /* Create Op structure and append to parent's argument list */
219 if (walk_state
->op_info
->flags
& AML_NAMED
) {
220 /* Allocate a new pre_op if necessary */
224 acpi_ps_alloc_op(walk_state
->
227 status
= AE_NO_MEMORY
;
232 pre_op
->common
.value
.arg
= NULL
;
233 pre_op
->common
.aml_opcode
= walk_state
->opcode
;
236 * Get and append arguments until we find the node that contains
237 * the name (the type ARGP_NAME).
239 while (GET_CURRENT_ARG_TYPE
240 (walk_state
->arg_types
)
242 (GET_CURRENT_ARG_TYPE
243 (walk_state
->arg_types
) != ARGP_NAME
)) {
245 acpi_ps_get_next_arg(walk_state
,
251 if (ACPI_FAILURE(status
)) {
255 acpi_ps_append_arg(pre_op
, arg
);
256 INCREMENT_ARG_LIST(walk_state
->
261 * Make sure that we found a NAME and didn't run out of
264 if (!GET_CURRENT_ARG_TYPE
265 (walk_state
->arg_types
)) {
266 status
= AE_AML_NO_OPERAND
;
270 /* We know that this arg is a name, move to next arg */
272 INCREMENT_ARG_LIST(walk_state
->arg_types
);
275 * Find the object. This will either insert the object into
276 * the namespace or simply look it up
278 walk_state
->op
= NULL
;
281 walk_state
->descending_callback(walk_state
,
283 if (ACPI_FAILURE(status
)) {
284 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
285 "During name lookup/catalog, %s\n",
286 acpi_format_exception
296 acpi_ps_next_parse_state(walk_state
, op
,
298 if (status
== AE_CTRL_PENDING
) {
303 if (ACPI_FAILURE(status
)) {
307 acpi_ps_append_arg(op
,
308 pre_op
->common
.value
.arg
);
311 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
313 * Defer final parsing of an operation_region body,
314 * because we don't have enough info in the first pass
315 * to parse it correctly (i.e., there may be method
316 * calls within the term_arg elements of the body.)
318 * However, we must continue parsing because
319 * the opregion is not a standalone package --
320 * we don't know where the end is at this point.
322 * (Length is unknown until parse of the body complete)
324 op
->named
.data
= aml_op_start
;
325 op
->named
.length
= 0;
328 /* Not a named opcode, just allocate Op and append to parent */
330 walk_state
->op_info
=
331 acpi_ps_get_opcode_info(walk_state
->opcode
);
332 op
= acpi_ps_alloc_op(walk_state
->opcode
);
334 status
= AE_NO_MEMORY
;
338 if (walk_state
->op_info
->flags
& AML_CREATE
) {
340 * Backup to beginning of create_xXXfield declaration
341 * body_length is unknown until we parse the body
343 op
->named
.data
= aml_op_start
;
344 op
->named
.length
= 0;
347 acpi_ps_append_arg(acpi_ps_get_parent_scope
350 if ((walk_state
->descending_callback
!= NULL
)) {
352 * Find the object. This will either insert the object into
353 * the namespace or simply look it up
359 descending_callback(walk_state
,
362 acpi_ps_next_parse_state(walk_state
,
365 if (status
== AE_CTRL_PENDING
) {
370 if (ACPI_FAILURE(status
)) {
376 op
->common
.aml_offset
= walk_state
->aml_offset
;
378 if (walk_state
->op_info
) {
379 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
380 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
381 (u32
) op
->common
.aml_opcode
,
382 walk_state
->op_info
->name
, op
,
384 op
->common
.aml_offset
));
389 * Start arg_count at zero because we don't know if there are
392 walk_state
->arg_count
= 0;
394 /* Are there any arguments that must be processed? */
396 if (walk_state
->arg_types
) {
399 switch (op
->common
.aml_opcode
) {
400 case AML_BYTE_OP
: /* AML_BYTEDATA_ARG */
401 case AML_WORD_OP
: /* AML_WORDDATA_ARG */
402 case AML_DWORD_OP
: /* AML_DWORDATA_ARG */
403 case AML_QWORD_OP
: /* AML_QWORDATA_ARG */
404 case AML_STRING_OP
: /* AML_ASCIICHARLIST_ARG */
406 /* Fill in constant or string argument directly */
408 acpi_ps_get_next_simple_arg(parser_state
,
414 case AML_INT_NAMEPATH_OP
: /* AML_NAMESTRING_ARG */
417 acpi_ps_get_next_namepath(walk_state
,
420 if (ACPI_FAILURE(status
)) {
424 walk_state
->arg_types
= 0;
429 * Op is not a constant or string, append each argument
432 while (GET_CURRENT_ARG_TYPE
433 (walk_state
->arg_types
)
434 && !walk_state
->arg_count
) {
435 walk_state
->aml_offset
= (u32
)
436 ACPI_PTR_DIFF(parser_state
->aml
,
441 acpi_ps_get_next_arg(walk_state
,
447 if (ACPI_FAILURE(status
)) {
452 arg
->common
.aml_offset
=
453 walk_state
->aml_offset
;
454 acpi_ps_append_arg(op
, arg
);
456 INCREMENT_ARG_LIST(walk_state
->
460 /* Special processing for certain opcodes */
462 /* TBD (remove): Temporary mechanism to disable this code if needed */
464 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
466 if ((walk_state
->pass_number
<=
467 ACPI_IMODE_LOAD_PASS1
)
470 parse_flags
& ACPI_PARSE_DISASSEMBLE
) ==
473 * We want to skip If/Else/While constructs during Pass1
474 * because we want to actually conditionally execute the
477 * Except for disassembly, where we always want to
478 * walk the If/Else/While packages
480 switch (op
->common
.aml_opcode
) {
485 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
486 "Pass1: Skipping an If/Else/While body\n"));
488 /* Skip body of if/else/while in pass 1 */
491 parser_state
->pkg_end
;
492 walk_state
->arg_count
= 0;
500 switch (op
->common
.aml_opcode
) {
504 * Skip parsing of control method
505 * because we don't have enough info in the first pass
506 * to parse it correctly.
508 * Save the length and address of the body
510 op
->named
.data
= parser_state
->aml
;
512 (u32
) (parser_state
->pkg_end
-
515 /* Skip body of method */
518 parser_state
->pkg_end
;
519 walk_state
->arg_count
= 0;
524 case AML_VAR_PACKAGE_OP
:
526 if ((op
->common
.parent
) &&
527 (op
->common
.parent
->common
.
528 aml_opcode
== AML_NAME_OP
)
529 && (walk_state
->pass_number
<=
530 ACPI_IMODE_LOAD_PASS2
)) {
532 * Skip parsing of Buffers and Packages
533 * because we don't have enough info in the first pass
534 * to parse them correctly.
536 op
->named
.data
= aml_op_start
;
538 (u32
) (parser_state
->
545 parser_state
->pkg_end
;
546 walk_state
->arg_count
= 0;
552 if (walk_state
->control_state
) {
553 walk_state
->control_state
->
554 control
.package_end
=
555 parser_state
->pkg_end
;
561 /* No action for all other opcodes */
568 /* Check for arguments that need to be processed */
570 if (walk_state
->arg_count
) {
572 * There are arguments (complex ones), push Op and
573 * prepare for argument
575 status
= acpi_ps_push_scope(parser_state
, op
,
576 walk_state
->arg_types
,
577 walk_state
->arg_count
);
578 if (ACPI_FAILURE(status
)) {
586 * All arguments have been processed -- Op is complete,
589 walk_state
->op_info
=
590 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
591 if (walk_state
->op_info
->flags
& AML_NAMED
) {
592 if (acpi_gbl_depth
) {
596 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
598 * Skip parsing of control method or opregion body,
599 * because we don't have enough info in the first pass
600 * to parse them correctly.
602 * Completed parsing an op_region declaration, we now
606 (u32
) (parser_state
->aml
- op
->named
.data
);
610 if (walk_state
->op_info
->flags
& AML_CREATE
) {
612 * Backup to beginning of create_xXXfield declaration (1 for
615 * body_length is unknown until we parse the body
618 (u32
) (parser_state
->aml
- op
->named
.data
);
621 /* This op complete, notify the dispatcher */
623 if (walk_state
->ascending_callback
!= NULL
) {
625 walk_state
->opcode
= op
->common
.aml_opcode
;
627 status
= walk_state
->ascending_callback(walk_state
);
629 acpi_ps_next_parse_state(walk_state
, op
, status
);
630 if (status
== AE_CTRL_PENDING
) {
638 * Finished one argument of the containing scope
640 parser_state
->scope
->parse_scope
.arg_count
--;
642 /* Finished with pre_op */
645 acpi_ps_free_op(pre_op
);
649 /* Close this Op (will result in parse subtree deletion) */
651 status2
= acpi_ps_complete_this_op(walk_state
, op
);
652 if (ACPI_FAILURE(status2
)) {
653 return_ACPI_STATUS(status2
);
661 case AE_CTRL_TRANSFER
:
663 /* We are about to transfer to a called method. */
665 walk_state
->prev_op
= op
;
666 walk_state
->prev_arg_types
= walk_state
->arg_types
;
667 return_ACPI_STATUS(status
);
671 acpi_ps_pop_scope(parser_state
, &op
,
672 &walk_state
->arg_types
,
673 &walk_state
->arg_count
);
677 walk_state
->op_info
=
678 acpi_ps_get_opcode_info(op
->common
.
680 walk_state
->opcode
= op
->common
.aml_opcode
;
683 walk_state
->ascending_callback(walk_state
);
685 acpi_ps_next_parse_state(walk_state
, op
,
689 acpi_ps_complete_this_op(walk_state
, op
);
690 if (ACPI_FAILURE(status2
)) {
691 return_ACPI_STATUS(status2
);
699 case AE_CTRL_CONTINUE
:
701 /* Pop off scopes until we find the While */
703 while (!op
|| (op
->common
.aml_opcode
!= AML_WHILE_OP
)) {
704 acpi_ps_pop_scope(parser_state
, &op
,
705 &walk_state
->arg_types
,
706 &walk_state
->arg_count
);
709 /* Close this iteration of the While loop */
712 walk_state
->op_info
=
713 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
714 walk_state
->opcode
= op
->common
.aml_opcode
;
716 status
= walk_state
->ascending_callback(walk_state
);
718 acpi_ps_next_parse_state(walk_state
, op
, status
);
720 status2
= acpi_ps_complete_this_op(walk_state
, op
);
721 if (ACPI_FAILURE(status2
)) {
722 return_ACPI_STATUS(status2
);
729 case AE_CTRL_TERMINATE
:
737 acpi_ps_complete_this_op(walk_state
,
739 if (ACPI_FAILURE(status2
)) {
740 return_ACPI_STATUS(status2
);
743 acpi_ps_pop_scope(parser_state
, &op
,
744 &walk_state
->arg_types
,
745 &walk_state
->arg_count
);
749 return_ACPI_STATUS(status
);
751 default: /* All other non-AE_OK status */
756 acpi_ps_complete_this_op(walk_state
,
758 if (ACPI_FAILURE(status2
)) {
759 return_ACPI_STATUS(status2
);
762 acpi_ps_pop_scope(parser_state
, &op
,
763 &walk_state
->arg_types
,
764 &walk_state
->arg_count
);
769 * TBD: Cleanup parse ops on error
773 acpi_ps_pop_scope(parser_state
, &op
,
774 &walk_state
->arg_types
,
775 &walk_state
->arg_count
);
778 walk_state
->prev_op
= op
;
779 walk_state
->prev_arg_types
= walk_state
->arg_types
;
780 return_ACPI_STATUS(status
);
783 /* This scope complete? */
785 if (acpi_ps_has_completed_scope(parser_state
)) {
786 acpi_ps_pop_scope(parser_state
, &op
,
787 &walk_state
->arg_types
,
788 &walk_state
->arg_count
);
789 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
790 "Popped scope, Op=%p\n", op
));
795 } /* while parser_state->Aml */
798 * Complete the last Op (if not completed), and clear the scope stack.
799 * It is easily possible to end an AML "package" with an unbounded number
800 * of open scopes (such as when several ASL blocks are closed with
801 * sequential closing braces). We want to terminate each one cleanly.
803 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "AML package complete at Op %p\n",
807 if (walk_state
->ascending_callback
!= NULL
) {
809 walk_state
->op_info
=
810 acpi_ps_get_opcode_info(op
->common
.
812 walk_state
->opcode
= op
->common
.aml_opcode
;
815 walk_state
->ascending_callback(walk_state
);
817 acpi_ps_next_parse_state(walk_state
, op
,
819 if (status
== AE_CTRL_PENDING
) {
824 if (status
== AE_CTRL_TERMINATE
) {
831 acpi_ps_complete_this_op
840 acpi_ps_pop_scope(parser_state
,
849 return_ACPI_STATUS(status
);
852 else if (ACPI_FAILURE(status
)) {
853 /* First error is most important */
856 acpi_ps_complete_this_op(walk_state
,
858 return_ACPI_STATUS(status
);
862 status2
= acpi_ps_complete_this_op(walk_state
, op
);
863 if (ACPI_FAILURE(status2
)) {
864 return_ACPI_STATUS(status2
);
868 acpi_ps_pop_scope(parser_state
, &op
, &walk_state
->arg_types
,
869 &walk_state
->arg_count
);
873 return_ACPI_STATUS(status
);