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_REPORT_ERROR(("Invoked method did not return a value, %s\n", acpi_format_exception(status)));
129 ACPI_REPORT_ERROR(("get_predicate Failed, %s\n", acpi_format_exception(status)));
130 return_ACPI_STATUS(status);
134 acpi_ps_next_parse_state(walk_state, op,
138 acpi_ps_pop_scope(parser_state, &op,
139 &walk_state->arg_types,
140 &walk_state->arg_count);
141 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
142 "Popped scope, Op=%p\n", op));
143 } else if (walk_state->prev_op) {
144 /* We were in the middle of an op */
146 op = walk_state->prev_op;
147 walk_state->arg_types = walk_state->prev_arg_types;
152 /* Iterative parsing loop, while there is more AML to process: */
154 while ((parser_state->aml < parser_state->aml_end) || (op)) {
155 aml_op_start = parser_state->aml;
157 /* Get the next opcode from the AML stream */
159 walk_state->aml_offset =
160 (u32) ACPI_PTR_DIFF(parser_state->aml,
161 parser_state->aml_start);
162 walk_state->opcode = acpi_ps_peek_opcode(parser_state);
165 * First cut to determine what we have found:
166 * 1) A valid AML opcode
168 * 3) An unknown/invalid opcode
170 walk_state->op_info =
171 acpi_ps_get_opcode_info(walk_state->opcode);
172 switch (walk_state->op_info->class) {
173 case AML_CLASS_ASCII:
174 case AML_CLASS_PREFIX:
176 * Starts with a valid prefix or ASCII char, this is a name
177 * string. Convert the bare name string to a namepath.
179 walk_state->opcode = AML_INT_NAMEPATH_OP;
180 walk_state->arg_types = ARGP_NAMESTRING;
183 case AML_CLASS_UNKNOWN:
185 /* The opcode is unrecognized. Just skip unknown opcodes */
187 ACPI_REPORT_ERROR(("Found unknown opcode %X at AML address %p offset %X, ignoring\n", walk_state->opcode, parser_state->aml, walk_state->aml_offset));
189 ACPI_DUMP_BUFFER(parser_state->aml, 128);
191 /* Assume one-byte bad opcode */
198 /* Found opcode info, this is a normal opcode */
201 acpi_ps_get_opcode_size(walk_state->opcode);
202 walk_state->arg_types =
203 walk_state->op_info->parse_args;
207 /* Create Op structure and append to parent's argument list */
209 if (walk_state->op_info->flags & AML_NAMED) {
210 /* Allocate a new pre_op if necessary */
214 acpi_ps_alloc_op(walk_state->
217 status = AE_NO_MEMORY;
222 pre_op->common.value.arg = NULL;
223 pre_op->common.aml_opcode = walk_state->opcode;
226 * Get and append arguments until we find the node that contains
227 * the name (the type ARGP_NAME).
229 while (GET_CURRENT_ARG_TYPE
230 (walk_state->arg_types)
232 (GET_CURRENT_ARG_TYPE
233 (walk_state->arg_types) != ARGP_NAME)) {
235 acpi_ps_get_next_arg(walk_state,
241 if (ACPI_FAILURE(status)) {
245 acpi_ps_append_arg(pre_op, arg);
246 INCREMENT_ARG_LIST(walk_state->
251 * Make sure that we found a NAME and didn't run out of
254 if (!GET_CURRENT_ARG_TYPE
255 (walk_state->arg_types)) {
256 status = AE_AML_NO_OPERAND;
260 /* We know that this arg is a name, move to next arg */
262 INCREMENT_ARG_LIST(walk_state->arg_types);
265 * Find the object. This will either insert the object into
266 * the namespace or simply look it up
268 walk_state->op = NULL;
271 walk_state->descending_callback(walk_state,
273 if (ACPI_FAILURE(status)) {
274 ACPI_REPORT_ERROR(("During name lookup/catalog, %s\n", acpi_format_exception(status)));
283 acpi_ps_next_parse_state(walk_state, op,
285 if (status == AE_CTRL_PENDING) {
290 if (ACPI_FAILURE(status)) {
294 acpi_ps_append_arg(op,
295 pre_op->common.value.arg);
298 if (op->common.aml_opcode == AML_REGION_OP) {
300 * Defer final parsing of an operation_region body,
301 * because we don't have enough info in the first pass
302 * to parse it correctly (i.e., there may be method
303 * calls within the term_arg elements of the body.)
305 * However, we must continue parsing because
306 * the opregion is not a standalone package --
307 * we don't know where the end is at this point.
309 * (Length is unknown until parse of the body complete)
311 op->named.data = aml_op_start;
312 op->named.length = 0;
315 /* Not a named opcode, just allocate Op and append to parent */
317 walk_state->op_info =
318 acpi_ps_get_opcode_info(walk_state->opcode);
319 op = acpi_ps_alloc_op(walk_state->opcode);
321 status = AE_NO_MEMORY;
325 if (walk_state->op_info->flags & AML_CREATE) {
327 * Backup to beginning of create_xXXfield declaration
328 * body_length is unknown until we parse the body
330 op->named.data = aml_op_start;
331 op->named.length = 0;
334 acpi_ps_append_arg(acpi_ps_get_parent_scope
337 if ((walk_state->descending_callback != NULL)) {
339 * Find the object. This will either insert the object into
340 * the namespace or simply look it up
346 descending_callback(walk_state,
349 acpi_ps_next_parse_state(walk_state,
352 if (status == AE_CTRL_PENDING) {
357 if (ACPI_FAILURE(status)) {
363 op->common.aml_offset = walk_state->aml_offset;
365 if (walk_state->op_info) {
366 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
367 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
368 (u32) op->common.aml_opcode,
369 walk_state->op_info->name, op,
371 op->common.aml_offset));
376 * Start arg_count at zero because we don't know if there are
379 walk_state->arg_count = 0;
381 /* Are there any arguments that must be processed? */
383 if (walk_state->arg_types) {
386 switch (op->common.aml_opcode) {
387 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
388 case AML_WORD_OP: /* AML_WORDDATA_ARG */
389 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
390 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
391 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
393 /* Fill in constant or string argument directly */
395 acpi_ps_get_next_simple_arg(parser_state,
401 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
404 acpi_ps_get_next_namepath(walk_state,
407 if (ACPI_FAILURE(status)) {
411 walk_state->arg_types = 0;
416 * Op is not a constant or string, append each argument
419 while (GET_CURRENT_ARG_TYPE
420 (walk_state->arg_types)
421 && !walk_state->arg_count) {
422 walk_state->aml_offset = (u32)
423 ACPI_PTR_DIFF(parser_state->aml,
428 acpi_ps_get_next_arg(walk_state,
434 if (ACPI_FAILURE(status)) {
439 arg->common.aml_offset =
440 walk_state->aml_offset;
441 acpi_ps_append_arg(op, arg);
443 INCREMENT_ARG_LIST(walk_state->
447 /* Special processing for certain opcodes */
449 /* TBD (remove): Temporary mechanism to disable this code if needed */
451 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
453 if ((walk_state->pass_number <=
454 ACPI_IMODE_LOAD_PASS1)
457 parse_flags & ACPI_PARSE_DISASSEMBLE) ==
460 * We want to skip If/Else/While constructs during Pass1
461 * because we want to actually conditionally execute the
464 * Except for disassembly, where we always want to
465 * walk the If/Else/While packages
467 switch (op->common.aml_opcode) {
472 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
473 "Pass1: Skipping an If/Else/While body\n"));
475 /* Skip body of if/else/while in pass 1 */
478 parser_state->pkg_end;
479 walk_state->arg_count = 0;
487 switch (op->common.aml_opcode) {
491 * Skip parsing of control method
492 * because we don't have enough info in the first pass
493 * to parse it correctly.
495 * Save the length and address of the body
497 op->named.data = parser_state->aml;
499 (u32) (parser_state->pkg_end -
502 /* Skip body of method */
505 parser_state->pkg_end;
506 walk_state->arg_count = 0;
511 case AML_VAR_PACKAGE_OP:
513 if ((op->common.parent) &&
514 (op->common.parent->common.
515 aml_opcode == AML_NAME_OP)
516 && (walk_state->pass_number <=
517 ACPI_IMODE_LOAD_PASS2)) {
519 * Skip parsing of Buffers and Packages
520 * because we don't have enough info in the first pass
521 * to parse them correctly.
523 op->named.data = aml_op_start;
525 (u32) (parser_state->
532 parser_state->pkg_end;
533 walk_state->arg_count = 0;
539 if (walk_state->control_state) {
540 walk_state->control_state->
541 control.package_end =
542 parser_state->pkg_end;
548 /* No action for all other opcodes */
555 /* Check for arguments that need to be processed */
557 if (walk_state->arg_count) {
559 * There are arguments (complex ones), push Op and
560 * prepare for argument
562 status = acpi_ps_push_scope(parser_state, op,
563 walk_state->arg_types,
564 walk_state->arg_count);
565 if (ACPI_FAILURE(status)) {
573 * All arguments have been processed -- Op is complete,
576 walk_state->op_info =
577 acpi_ps_get_opcode_info(op->common.aml_opcode);
578 if (walk_state->op_info->flags & AML_NAMED) {
579 if (acpi_gbl_depth) {
583 if (op->common.aml_opcode == AML_REGION_OP) {
585 * Skip parsing of control method or opregion body,
586 * because we don't have enough info in the first pass
587 * to parse them correctly.
589 * Completed parsing an op_region declaration, we now
593 (u32) (parser_state->aml - op->named.data);
597 if (walk_state->op_info->flags & AML_CREATE) {
599 * Backup to beginning of create_xXXfield declaration (1 for
602 * body_length is unknown until we parse the body
605 (u32) (parser_state->aml - op->named.data);
608 /* This op complete, notify the dispatcher */
610 if (walk_state->ascending_callback != NULL) {
612 walk_state->opcode = op->common.aml_opcode;
614 status = walk_state->ascending_callback(walk_state);
616 acpi_ps_next_parse_state(walk_state, op, status);
617 if (status == AE_CTRL_PENDING) {
625 * Finished one argument of the containing scope
627 parser_state->scope->parse_scope.arg_count--;
629 /* Finished with pre_op */
632 acpi_ps_free_op(pre_op);
636 /* Close this Op (will result in parse subtree deletion) */
638 status2 = acpi_ps_complete_this_op(walk_state, op);
639 if (ACPI_FAILURE(status2)) {
640 return_ACPI_STATUS(status2);
648 case AE_CTRL_TRANSFER:
650 /* We are about to transfer to a called method. */
652 walk_state->prev_op = op;
653 walk_state->prev_arg_types = walk_state->arg_types;
654 return_ACPI_STATUS(status);
658 acpi_ps_pop_scope(parser_state, &op,
659 &walk_state->arg_types,
660 &walk_state->arg_count);
664 walk_state->op_info =
665 acpi_ps_get_opcode_info(op->common.
667 walk_state->opcode = op->common.aml_opcode;
670 walk_state->ascending_callback(walk_state);
672 acpi_ps_next_parse_state(walk_state, op,
676 acpi_ps_complete_this_op(walk_state, op);
677 if (ACPI_FAILURE(status2)) {
678 return_ACPI_STATUS(status2);
686 case AE_CTRL_CONTINUE:
688 /* Pop off scopes until we find the While */
690 while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
691 acpi_ps_pop_scope(parser_state, &op,
692 &walk_state->arg_types,
693 &walk_state->arg_count);
695 if (op->common.aml_opcode != AML_WHILE_OP) {
697 acpi_ds_result_stack_pop
699 if (ACPI_FAILURE(status2)) {
700 return_ACPI_STATUS(status2);
705 /* Close this iteration of the While loop */
708 walk_state->op_info =
709 acpi_ps_get_opcode_info(op->common.aml_opcode);
710 walk_state->opcode = op->common.aml_opcode;
712 status = walk_state->ascending_callback(walk_state);
714 acpi_ps_next_parse_state(walk_state, op, status);
716 status2 = acpi_ps_complete_this_op(walk_state, op);
717 if (ACPI_FAILURE(status2)) {
718 return_ACPI_STATUS(status2);
725 case AE_CTRL_TERMINATE:
733 acpi_ps_complete_this_op(walk_state,
735 if (ACPI_FAILURE(status2)) {
736 return_ACPI_STATUS(status2);
739 acpi_ps_pop_scope(parser_state, &op,
740 &walk_state->arg_types,
741 &walk_state->arg_count);
745 return_ACPI_STATUS(status);
747 default: /* All other non-AE_OK status */
752 acpi_ps_complete_this_op(walk_state,
754 if (ACPI_FAILURE(status2)) {
755 return_ACPI_STATUS(status2);
758 acpi_ps_pop_scope(parser_state, &op,
759 &walk_state->arg_types,
760 &walk_state->arg_count);
765 * TBD: Cleanup parse ops on error
769 acpi_ps_pop_scope(parser_state, &op,
770 &walk_state->arg_types,
771 &walk_state->arg_count);
774 walk_state->prev_op = op;
775 walk_state->prev_arg_types = walk_state->arg_types;
776 return_ACPI_STATUS(status);
779 /* This scope complete? */
781 if (acpi_ps_has_completed_scope(parser_state)) {
782 acpi_ps_pop_scope(parser_state, &op,
783 &walk_state->arg_types,
784 &walk_state->arg_count);
785 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
786 "Popped scope, Op=%p\n", op));
791 } /* while parser_state->Aml */
794 * Complete the last Op (if not completed), and clear the scope stack.
795 * It is easily possible to end an AML "package" with an unbounded number
796 * of open scopes (such as when several ASL blocks are closed with
797 * sequential closing braces). We want to terminate each one cleanly.
799 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
803 if (walk_state->ascending_callback != NULL) {
805 walk_state->op_info =
806 acpi_ps_get_opcode_info(op->common.
808 walk_state->opcode = op->common.aml_opcode;
811 walk_state->ascending_callback(walk_state);
813 acpi_ps_next_parse_state(walk_state, op,
815 if (status == AE_CTRL_PENDING) {
820 if (status == AE_CTRL_TERMINATE) {
827 acpi_ps_complete_this_op
836 acpi_ps_pop_scope(parser_state,
845 return_ACPI_STATUS(status);
848 else if (ACPI_FAILURE(status)) {
849 /* First error is most important */
852 acpi_ps_complete_this_op(walk_state,
854 return_ACPI_STATUS(status);
858 status2 = acpi_ps_complete_this_op(walk_state, op);
859 if (ACPI_FAILURE(status2)) {
860 return_ACPI_STATUS(status2);
864 acpi_ps_pop_scope(parser_state, &op, &walk_state->arg_types,
865 &walk_state->arg_count);
869 return_ACPI_STATUS(status);