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
) {
99 /* We are restarting a preempted control method */
101 if (acpi_ps_has_completed_scope(parser_state
)) {
103 * We must check if a predicate to an IF or WHILE statement
106 if ((parser_state
->scope
->parse_scope
.op
) &&
107 ((parser_state
->scope
->parse_scope
.op
->common
.
108 aml_opcode
== AML_IF_OP
)
109 || (parser_state
->scope
->parse_scope
.op
->common
.
110 aml_opcode
== AML_WHILE_OP
))
111 && (walk_state
->control_state
)
112 && (walk_state
->control_state
->common
.state
==
113 ACPI_CONTROL_PREDICATE_EXECUTING
)) {
115 * A predicate was just completed, get the value of the
116 * predicate and branch based on that value
118 walk_state
->op
= NULL
;
120 acpi_ds_get_predicate_value(walk_state
,
123 if (ACPI_FAILURE(status
)
124 && ((status
& AE_CODE_MASK
) !=
126 if (status
== AE_AML_NO_RETURN_VALUE
) {
127 ACPI_EXCEPTION((AE_INFO
, status
,
128 "Invoked method did not return a value"));
131 ACPI_EXCEPTION((AE_INFO
, status
,
132 "get_predicate Failed"));
133 return_ACPI_STATUS(status
);
137 acpi_ps_next_parse_state(walk_state
, op
,
141 acpi_ps_pop_scope(parser_state
, &op
,
142 &walk_state
->arg_types
,
143 &walk_state
->arg_count
);
144 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
145 "Popped scope, Op=%p\n", op
));
146 } else if (walk_state
->prev_op
) {
148 /* We were in the middle of an op */
150 op
= walk_state
->prev_op
;
151 walk_state
->arg_types
= walk_state
->prev_arg_types
;
156 /* Iterative parsing loop, while there is more AML to process: */
158 while ((parser_state
->aml
< parser_state
->aml_end
) || (op
)) {
159 aml_op_start
= parser_state
->aml
;
162 /* Get the next opcode from the AML stream */
164 walk_state
->aml_offset
=
165 (u32
) ACPI_PTR_DIFF(parser_state
->aml
,
166 parser_state
->aml_start
);
167 walk_state
->opcode
= acpi_ps_peek_opcode(parser_state
);
170 * First cut to determine what we have found:
171 * 1) A valid AML opcode
173 * 3) An unknown/invalid opcode
175 walk_state
->op_info
=
176 acpi_ps_get_opcode_info(walk_state
->opcode
);
177 switch (walk_state
->op_info
->class) {
178 case AML_CLASS_ASCII
:
179 case AML_CLASS_PREFIX
:
181 * Starts with a valid prefix or ASCII char, this is a name
182 * string. Convert the bare name string to a namepath.
184 walk_state
->opcode
= AML_INT_NAMEPATH_OP
;
185 walk_state
->arg_types
= ARGP_NAMESTRING
;
188 case AML_CLASS_UNKNOWN
:
190 /* The opcode is unrecognized. Just skip unknown opcodes */
193 "Found unknown opcode %X at AML address %p offset %X, ignoring",
196 walk_state
->aml_offset
));
198 ACPI_DUMP_BUFFER(parser_state
->aml
, 128);
200 /* Assume one-byte bad opcode */
207 /* Found opcode info, this is a normal opcode */
210 acpi_ps_get_opcode_size(walk_state
->opcode
);
211 walk_state
->arg_types
=
212 walk_state
->op_info
->parse_args
;
216 /* Create Op structure and append to parent's argument list */
218 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_EXCEPTION((AE_INFO
, status
,
285 "During name lookup/catalog"));
294 acpi_ps_next_parse_state(walk_state
, op
,
296 if (status
== AE_CTRL_PENDING
) {
301 if (ACPI_FAILURE(status
)) {
305 acpi_ps_append_arg(op
,
306 pre_op
->common
.value
.arg
);
309 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
311 * Defer final parsing of an operation_region body,
312 * because we don't have enough info in the first pass
313 * to parse it correctly (i.e., there may be method
314 * calls within the term_arg elements of the body.)
316 * However, we must continue parsing because
317 * the opregion is not a standalone package --
318 * we don't know where the end is at this point.
320 * (Length is unknown until parse of the body complete)
322 op
->named
.data
= aml_op_start
;
323 op
->named
.length
= 0;
326 /* Not a named opcode, just allocate Op and append to parent */
328 walk_state
->op_info
=
329 acpi_ps_get_opcode_info(walk_state
->opcode
);
330 op
= acpi_ps_alloc_op(walk_state
->opcode
);
332 status
= AE_NO_MEMORY
;
336 if (walk_state
->op_info
->flags
& AML_CREATE
) {
338 * Backup to beginning of create_xXXfield declaration
339 * body_length is unknown until we parse the body
341 op
->named
.data
= aml_op_start
;
342 op
->named
.length
= 0;
345 acpi_ps_append_arg(acpi_ps_get_parent_scope
348 if ((walk_state
->descending_callback
!= NULL
)) {
350 * Find the object. This will either insert the object into
351 * the namespace or simply look it up
357 descending_callback(walk_state
,
360 acpi_ps_next_parse_state(walk_state
,
363 if (status
== AE_CTRL_PENDING
) {
368 if (ACPI_FAILURE(status
)) {
374 op
->common
.aml_offset
= walk_state
->aml_offset
;
376 if (walk_state
->op_info
) {
377 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
378 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
379 (u32
) op
->common
.aml_opcode
,
380 walk_state
->op_info
->name
, op
,
382 op
->common
.aml_offset
));
387 * Start arg_count at zero because we don't know if there are
390 walk_state
->arg_count
= 0;
392 /* Are there any arguments that must be processed? */
394 if (walk_state
->arg_types
) {
398 switch (op
->common
.aml_opcode
) {
399 case AML_BYTE_OP
: /* AML_BYTEDATA_ARG */
400 case AML_WORD_OP
: /* AML_WORDDATA_ARG */
401 case AML_DWORD_OP
: /* AML_DWORDATA_ARG */
402 case AML_QWORD_OP
: /* AML_QWORDATA_ARG */
403 case AML_STRING_OP
: /* AML_ASCIICHARLIST_ARG */
405 /* Fill in constant or string argument directly */
407 acpi_ps_get_next_simple_arg(parser_state
,
413 case AML_INT_NAMEPATH_OP
: /* AML_NAMESTRING_ARG */
416 acpi_ps_get_next_namepath(walk_state
,
419 if (ACPI_FAILURE(status
)) {
423 walk_state
->arg_types
= 0;
428 * Op is not a constant or string, append each argument
431 while (GET_CURRENT_ARG_TYPE
432 (walk_state
->arg_types
)
433 && !walk_state
->arg_count
) {
434 walk_state
->aml_offset
= (u32
)
435 ACPI_PTR_DIFF(parser_state
->aml
,
440 acpi_ps_get_next_arg(walk_state
,
446 if (ACPI_FAILURE(status
)) {
451 arg
->common
.aml_offset
=
452 walk_state
->aml_offset
;
453 acpi_ps_append_arg(op
, arg
);
455 INCREMENT_ARG_LIST(walk_state
->
459 /* Special processing for certain opcodes */
461 /* TBD (remove): Temporary mechanism to disable this code if needed */
463 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
465 if ((walk_state
->pass_number
<=
466 ACPI_IMODE_LOAD_PASS1
)
469 parse_flags
& ACPI_PARSE_DISASSEMBLE
) ==
472 * We want to skip If/Else/While constructs during Pass1
473 * because we want to actually conditionally execute the
476 * Except for disassembly, where we always want to
477 * walk the If/Else/While packages
479 switch (op
->common
.aml_opcode
) {
484 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
485 "Pass1: Skipping an If/Else/While body\n"));
487 /* Skip body of if/else/while in pass 1 */
490 parser_state
->pkg_end
;
491 walk_state
->arg_count
= 0;
499 switch (op
->common
.aml_opcode
) {
503 * Skip parsing of control method
504 * because we don't have enough info in the first pass
505 * to parse it correctly.
507 * Save the length and address of the body
509 op
->named
.data
= parser_state
->aml
;
511 (u32
) (parser_state
->pkg_end
-
514 /* Skip body of method */
517 parser_state
->pkg_end
;
518 walk_state
->arg_count
= 0;
523 case AML_VAR_PACKAGE_OP
:
525 if ((op
->common
.parent
) &&
526 (op
->common
.parent
->common
.
527 aml_opcode
== AML_NAME_OP
)
528 && (walk_state
->pass_number
<=
529 ACPI_IMODE_LOAD_PASS2
)) {
531 * Skip parsing of Buffers and Packages
532 * because we don't have enough info in the first pass
533 * to parse them correctly.
535 op
->named
.data
= aml_op_start
;
537 (u32
) (parser_state
->
544 parser_state
->pkg_end
;
545 walk_state
->arg_count
= 0;
551 if (walk_state
->control_state
) {
552 walk_state
->control_state
->
553 control
.package_end
=
554 parser_state
->pkg_end
;
560 /* No action for all other opcodes */
567 /* Check for arguments that need to be processed */
569 if (walk_state
->arg_count
) {
571 * There are arguments (complex ones), push Op and
572 * prepare for argument
574 status
= acpi_ps_push_scope(parser_state
, op
,
575 walk_state
->arg_types
,
576 walk_state
->arg_count
);
577 if (ACPI_FAILURE(status
)) {
585 * All arguments have been processed -- Op is complete,
588 walk_state
->op_info
=
589 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
590 if (walk_state
->op_info
->flags
& AML_NAMED
) {
591 if (acpi_gbl_depth
) {
595 if (op
->common
.aml_opcode
== AML_REGION_OP
) {
597 * Skip parsing of control method or opregion body,
598 * because we don't have enough info in the first pass
599 * to parse them correctly.
601 * Completed parsing an op_region declaration, we now
605 (u32
) (parser_state
->aml
- op
->named
.data
);
609 if (walk_state
->op_info
->flags
& AML_CREATE
) {
611 * Backup to beginning of create_xXXfield declaration (1 for
614 * body_length is unknown until we parse the body
617 (u32
) (parser_state
->aml
- op
->named
.data
);
620 /* This op complete, notify the dispatcher */
622 if (walk_state
->ascending_callback
!= NULL
) {
624 walk_state
->opcode
= op
->common
.aml_opcode
;
626 status
= walk_state
->ascending_callback(walk_state
);
628 acpi_ps_next_parse_state(walk_state
, op
, status
);
629 if (status
== AE_CTRL_PENDING
) {
637 * Finished one argument of the containing scope
639 parser_state
->scope
->parse_scope
.arg_count
--;
641 /* Finished with pre_op */
644 acpi_ps_free_op(pre_op
);
648 /* Close this Op (will result in parse subtree deletion) */
650 status2
= acpi_ps_complete_this_op(walk_state
, op
);
651 if (ACPI_FAILURE(status2
)) {
652 return_ACPI_STATUS(status2
);
660 case AE_CTRL_TRANSFER
:
662 /* We are about to transfer to a called method. */
664 walk_state
->prev_op
= op
;
665 walk_state
->prev_arg_types
= walk_state
->arg_types
;
666 return_ACPI_STATUS(status
);
670 acpi_ps_pop_scope(parser_state
, &op
,
671 &walk_state
->arg_types
,
672 &walk_state
->arg_count
);
676 walk_state
->op_info
=
677 acpi_ps_get_opcode_info(op
->common
.
679 walk_state
->opcode
= op
->common
.aml_opcode
;
682 walk_state
->ascending_callback(walk_state
);
684 acpi_ps_next_parse_state(walk_state
, op
,
688 acpi_ps_complete_this_op(walk_state
, op
);
689 if (ACPI_FAILURE(status2
)) {
690 return_ACPI_STATUS(status2
);
698 case AE_CTRL_CONTINUE
:
700 /* Pop off scopes until we find the While */
702 while (!op
|| (op
->common
.aml_opcode
!= AML_WHILE_OP
)) {
703 acpi_ps_pop_scope(parser_state
, &op
,
704 &walk_state
->arg_types
,
705 &walk_state
->arg_count
);
707 if (op
->common
.aml_opcode
!= AML_WHILE_OP
) {
709 acpi_ds_result_stack_pop
711 if (ACPI_FAILURE(status2
)) {
712 return_ACPI_STATUS(status2
);
717 /* Close this iteration of the While loop */
720 walk_state
->op_info
=
721 acpi_ps_get_opcode_info(op
->common
.aml_opcode
);
722 walk_state
->opcode
= op
->common
.aml_opcode
;
724 status
= walk_state
->ascending_callback(walk_state
);
726 acpi_ps_next_parse_state(walk_state
, op
, status
);
728 status2
= acpi_ps_complete_this_op(walk_state
, op
);
729 if (ACPI_FAILURE(status2
)) {
730 return_ACPI_STATUS(status2
);
737 case AE_CTRL_TERMINATE
:
745 acpi_ps_complete_this_op(walk_state
,
747 if (ACPI_FAILURE(status2
)) {
748 return_ACPI_STATUS(status2
);
752 acpi_ds_result_stack_pop
754 if (ACPI_FAILURE(status2
)) {
755 return_ACPI_STATUS(status2
);
758 acpi_ut_delete_generic_state
759 (acpi_ut_pop_generic_state
760 (&walk_state
->control_state
));
763 acpi_ps_pop_scope(parser_state
, &op
,
764 &walk_state
->arg_types
,
765 &walk_state
->arg_count
);
769 return_ACPI_STATUS(status
);
771 default: /* All other non-AE_OK status */
776 acpi_ps_complete_this_op(walk_state
,
778 if (ACPI_FAILURE(status2
)) {
779 return_ACPI_STATUS(status2
);
783 acpi_ps_pop_scope(parser_state
, &op
,
784 &walk_state
->arg_types
,
785 &walk_state
->arg_count
);
790 * TBD: Cleanup parse ops on error
794 acpi_ps_pop_scope(parser_state
, &op
,
795 &walk_state
->arg_types
,
796 &walk_state
->arg_count
);
799 walk_state
->prev_op
= op
;
800 walk_state
->prev_arg_types
= walk_state
->arg_types
;
801 return_ACPI_STATUS(status
);
804 /* This scope complete? */
806 if (acpi_ps_has_completed_scope(parser_state
)) {
807 acpi_ps_pop_scope(parser_state
, &op
,
808 &walk_state
->arg_types
,
809 &walk_state
->arg_count
);
810 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
,
811 "Popped scope, Op=%p\n", op
));
816 } /* while parser_state->Aml */
819 * Complete the last Op (if not completed), and clear the scope stack.
820 * It is easily possible to end an AML "package" with an unbounded number
821 * of open scopes (such as when several ASL blocks are closed with
822 * sequential closing braces). We want to terminate each one cleanly.
824 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "AML package complete at Op %p\n",
828 if (walk_state
->ascending_callback
!= NULL
) {
830 walk_state
->op_info
=
831 acpi_ps_get_opcode_info(op
->common
.
833 walk_state
->opcode
= op
->common
.aml_opcode
;
836 walk_state
->ascending_callback(walk_state
);
838 acpi_ps_next_parse_state(walk_state
, op
,
840 if (status
== AE_CTRL_PENDING
) {
845 if (status
== AE_CTRL_TERMINATE
) {
852 acpi_ps_complete_this_op
861 acpi_ps_pop_scope(parser_state
,
870 return_ACPI_STATUS(status
);
873 else if (ACPI_FAILURE(status
)) {
875 /* First error is most important */
878 acpi_ps_complete_this_op(walk_state
,
880 return_ACPI_STATUS(status
);
884 status2
= acpi_ps_complete_this_op(walk_state
, op
);
885 if (ACPI_FAILURE(status2
)) {
886 return_ACPI_STATUS(status2
);
890 acpi_ps_pop_scope(parser_state
, &op
, &walk_state
->arg_types
,
891 &walk_state
->arg_count
);
895 return_ACPI_STATUS(status
);