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.
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>
59 #define _COMPONENT ACPI_PARSER
60 ACPI_MODULE_NAME ("psloop")
62 static u32 acpi_gbl_depth = 0;
65 /*******************************************************************************
67 * FUNCTION: acpi_ps_parse_loop
69 * PARAMETERS: walk_state - Current state
73 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
76 ******************************************************************************/
80 struct acpi_walk_state *walk_state)
82 acpi_status status = AE_OK;
84 union acpi_parse_object *op = NULL; /* current op */
85 union acpi_parse_object *arg = NULL;
86 union acpi_parse_object *pre_op = NULL;
87 struct acpi_parse_state *parser_state;
88 u8 *aml_op_start = NULL;
91 ACPI_FUNCTION_TRACE_PTR ("ps_parse_loop", walk_state);
93 if (walk_state->descending_callback == NULL) {
94 return_ACPI_STATUS (AE_BAD_PARAMETER);
97 parser_state = &walk_state->parser_state;
98 walk_state->arg_types = 0;
100 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
102 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
103 /* We are restarting a preempted control method */
105 if (acpi_ps_has_completed_scope (parser_state)) {
107 * We must check if a predicate to an IF or WHILE statement
110 if ((parser_state->scope->parse_scope.op) &&
111 ((parser_state->scope->parse_scope.op->common.aml_opcode == AML_IF_OP) ||
112 (parser_state->scope->parse_scope.op->common.aml_opcode == AML_WHILE_OP)) &&
113 (walk_state->control_state) &&
114 (walk_state->control_state->common.state ==
115 ACPI_CONTROL_PREDICATE_EXECUTING)) {
117 * A predicate was just completed, get the value of the
118 * predicate and branch based on that value
120 walk_state->op = NULL;
121 status = acpi_ds_get_predicate_value (walk_state, ACPI_TO_POINTER (TRUE));
122 if (ACPI_FAILURE (status) &&
123 ((status & AE_CODE_MASK) != AE_CODE_CONTROL)) {
124 if (status == AE_AML_NO_RETURN_VALUE) {
125 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
126 "Invoked method did not return a value, %s\n",
127 acpi_format_exception (status)));
130 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
131 "get_predicate Failed, %s\n",
132 acpi_format_exception (status)));
133 return_ACPI_STATUS (status);
136 status = acpi_ps_next_parse_state (walk_state, op, status);
139 acpi_ps_pop_scope (parser_state, &op,
140 &walk_state->arg_types, &walk_state->arg_count);
141 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "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 = (u32) ACPI_PTR_DIFF (parser_state->aml,
160 parser_state->aml_start);
161 walk_state->opcode = acpi_ps_peek_opcode (parser_state);
164 * First cut to determine what we have found:
165 * 1) A valid AML opcode
167 * 3) An unknown/invalid opcode
169 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
170 switch (walk_state->op_info->class) {
171 case AML_CLASS_ASCII:
172 case AML_CLASS_PREFIX:
174 * Starts with a valid prefix or ASCII char, this is a name
175 * string. Convert the bare name string to a namepath.
177 walk_state->opcode = AML_INT_NAMEPATH_OP;
178 walk_state->arg_types = ARGP_NAMESTRING;
181 case AML_CLASS_UNKNOWN:
183 /* The opcode is unrecognized. Just skip unknown opcodes */
185 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
186 "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
187 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 */
200 parser_state->aml += acpi_ps_get_opcode_size (walk_state->opcode);
201 walk_state->arg_types = walk_state->op_info->parse_args;
205 /* Create Op structure and append to parent's argument list */
207 if (walk_state->op_info->flags & AML_NAMED) {
208 /* Allocate a new pre_op if necessary */
211 pre_op = acpi_ps_alloc_op (walk_state->opcode);
213 status = AE_NO_MEMORY;
218 pre_op->common.value.arg = NULL;
219 pre_op->common.aml_opcode = walk_state->opcode;
222 * Get and append arguments until we find the node that contains
223 * the name (the type ARGP_NAME).
225 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
226 (GET_CURRENT_ARG_TYPE (walk_state->arg_types) != ARGP_NAME)) {
227 status = acpi_ps_get_next_arg (walk_state, parser_state,
228 GET_CURRENT_ARG_TYPE (walk_state->arg_types), &arg);
229 if (ACPI_FAILURE (status)) {
233 acpi_ps_append_arg (pre_op, arg);
234 INCREMENT_ARG_LIST (walk_state->arg_types);
238 * Make sure that we found a NAME and didn't run out of
241 if (!GET_CURRENT_ARG_TYPE (walk_state->arg_types)) {
242 status = AE_AML_NO_OPERAND;
246 /* We know that this arg is a name, move to next arg */
248 INCREMENT_ARG_LIST (walk_state->arg_types);
251 * Find the object. This will either insert the object into
252 * the namespace or simply look it up
254 walk_state->op = NULL;
256 status = walk_state->descending_callback (walk_state, &op);
257 if (ACPI_FAILURE (status)) {
258 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
259 "During name lookup/catalog, %s\n",
260 acpi_format_exception (status)));
268 status = acpi_ps_next_parse_state (walk_state, op, status);
269 if (status == AE_CTRL_PENDING) {
274 if (ACPI_FAILURE (status)) {
278 acpi_ps_append_arg (op, pre_op->common.value.arg);
281 if (op->common.aml_opcode == AML_REGION_OP) {
283 * Defer final parsing of an operation_region body,
284 * because we don't have enough info in the first pass
285 * to parse it correctly (i.e., there may be method
286 * calls within the term_arg elements of the body.)
288 * However, we must continue parsing because
289 * the opregion is not a standalone package --
290 * we don't know where the end is at this point.
292 * (Length is unknown until parse of the body complete)
294 op->named.data = aml_op_start;
295 op->named.length = 0;
299 /* Not a named opcode, just allocate Op and append to parent */
301 walk_state->op_info = acpi_ps_get_opcode_info (walk_state->opcode);
302 op = acpi_ps_alloc_op (walk_state->opcode);
304 status = AE_NO_MEMORY;
308 if (walk_state->op_info->flags & AML_CREATE) {
310 * Backup to beginning of create_xXXfield declaration
311 * body_length is unknown until we parse the body
313 op->named.data = aml_op_start;
314 op->named.length = 0;
317 acpi_ps_append_arg (acpi_ps_get_parent_scope (parser_state), op);
319 if ((walk_state->descending_callback != NULL)) {
321 * Find the object. This will either insert the object into
322 * the namespace or simply look it up
326 status = walk_state->descending_callback (walk_state, &op);
327 status = acpi_ps_next_parse_state (walk_state, op, status);
328 if (status == AE_CTRL_PENDING) {
333 if (ACPI_FAILURE (status)) {
339 op->common.aml_offset = walk_state->aml_offset;
341 if (walk_state->op_info) {
342 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
343 "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
344 (u32) op->common.aml_opcode, walk_state->op_info->name,
345 op, parser_state->aml, op->common.aml_offset));
351 * Start arg_count at zero because we don't know if there are
354 walk_state->arg_count = 0;
356 /* Are there any arguments that must be processed? */
358 if (walk_state->arg_types) {
361 switch (op->common.aml_opcode) {
362 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
363 case AML_WORD_OP: /* AML_WORDDATA_ARG */
364 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
365 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
366 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
368 /* Fill in constant or string argument directly */
370 acpi_ps_get_next_simple_arg (parser_state,
371 GET_CURRENT_ARG_TYPE (walk_state->arg_types), op);
374 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
376 status = acpi_ps_get_next_namepath (walk_state, parser_state, op, 1);
377 if (ACPI_FAILURE (status)) {
381 walk_state->arg_types = 0;
386 * Op is not a constant or string, append each argument
389 while (GET_CURRENT_ARG_TYPE (walk_state->arg_types) &&
390 !walk_state->arg_count) {
391 walk_state->aml_offset = (u32)
392 ACPI_PTR_DIFF (parser_state->aml, parser_state->aml_start);
394 status = acpi_ps_get_next_arg (walk_state, parser_state,
395 GET_CURRENT_ARG_TYPE (walk_state->arg_types),
397 if (ACPI_FAILURE (status)) {
402 arg->common.aml_offset = walk_state->aml_offset;
403 acpi_ps_append_arg (op, arg);
405 INCREMENT_ARG_LIST (walk_state->arg_types);
409 /* Special processing for certain opcodes */
411 /* TBD (remove): Temporary mechanism to disable this code if needed */
413 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
415 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS1) &&
416 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
418 * We want to skip If/Else/While constructs during Pass1
419 * because we want to actually conditionally execute the
422 * Except for disassembly, where we always want to
423 * walk the If/Else/While packages
425 switch (op->common.aml_opcode) {
430 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
431 "Pass1: Skipping an If/Else/While body\n"));
433 /* Skip body of if/else/while in pass 1 */
435 parser_state->aml = parser_state->pkg_end;
436 walk_state->arg_count = 0;
444 switch (op->common.aml_opcode) {
448 * Skip parsing of control method
449 * because we don't have enough info in the first pass
450 * to parse it correctly.
452 * Save the length and address of the body
454 op->named.data = parser_state->aml;
455 op->named.length = (u32) (parser_state->pkg_end -
458 /* Skip body of method */
460 parser_state->aml = parser_state->pkg_end;
461 walk_state->arg_count = 0;
466 case AML_VAR_PACKAGE_OP:
468 if ((op->common.parent) &&
469 (op->common.parent->common.aml_opcode == AML_NAME_OP) &&
470 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2)) {
472 * Skip parsing of Buffers and Packages
473 * because we don't have enough info in the first pass
474 * to parse them correctly.
476 op->named.data = aml_op_start;
477 op->named.length = (u32) (parser_state->pkg_end -
482 parser_state->aml = parser_state->pkg_end;
483 walk_state->arg_count = 0;
489 if (walk_state->control_state) {
490 walk_state->control_state->control.package_end =
491 parser_state->pkg_end;
497 /* No action for all other opcodes */
504 /* Check for arguments that need to be processed */
506 if (walk_state->arg_count) {
508 * There are arguments (complex ones), push Op and
509 * prepare for argument
511 status = acpi_ps_push_scope (parser_state, op,
512 walk_state->arg_types, walk_state->arg_count);
513 if (ACPI_FAILURE (status)) {
521 * All arguments have been processed -- Op is complete,
524 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
525 if (walk_state->op_info->flags & AML_NAMED) {
526 if (acpi_gbl_depth) {
530 if (op->common.aml_opcode == AML_REGION_OP) {
532 * Skip parsing of control method or opregion body,
533 * because we don't have enough info in the first pass
534 * to parse them correctly.
536 * Completed parsing an op_region declaration, we now
539 op->named.length = (u32) (parser_state->aml - op->named.data);
543 if (walk_state->op_info->flags & AML_CREATE) {
545 * Backup to beginning of create_xXXfield declaration (1 for
548 * body_length is unknown until we parse the body
550 op->named.length = (u32) (parser_state->aml - op->named.data);
553 /* This op complete, notify the dispatcher */
555 if (walk_state->ascending_callback != NULL) {
557 walk_state->opcode = op->common.aml_opcode;
559 status = walk_state->ascending_callback (walk_state);
560 status = acpi_ps_next_parse_state (walk_state, op, status);
561 if (status == AE_CTRL_PENDING) {
570 * Finished one argument of the containing scope
572 parser_state->scope->parse_scope.arg_count--;
574 /* Finished with pre_op */
577 acpi_ps_free_op (pre_op);
581 /* Close this Op (will result in parse subtree deletion) */
583 status2 = acpi_ps_complete_this_op (walk_state, op);
584 if (ACPI_FAILURE (status2)) {
585 return_ACPI_STATUS (status2);
594 case AE_CTRL_TRANSFER:
596 /* We are about to transfer to a called method. */
598 walk_state->prev_op = op;
599 walk_state->prev_arg_types = walk_state->arg_types;
600 return_ACPI_STATUS (status);
605 acpi_ps_pop_scope (parser_state, &op,
606 &walk_state->arg_types, &walk_state->arg_count);
610 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
611 walk_state->opcode = op->common.aml_opcode;
613 status = walk_state->ascending_callback (walk_state);
614 status = acpi_ps_next_parse_state (walk_state, op, status);
616 status2 = acpi_ps_complete_this_op (walk_state, op);
617 if (ACPI_FAILURE (status2)) {
618 return_ACPI_STATUS (status2);
627 case AE_CTRL_CONTINUE:
629 /* Pop off scopes until we find the While */
631 while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
632 acpi_ps_pop_scope (parser_state, &op,
633 &walk_state->arg_types, &walk_state->arg_count);
636 /* Close this iteration of the While loop */
639 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
640 walk_state->opcode = op->common.aml_opcode;
642 status = walk_state->ascending_callback (walk_state);
643 status = acpi_ps_next_parse_state (walk_state, op, status);
645 status2 = acpi_ps_complete_this_op (walk_state, op);
646 if (ACPI_FAILURE (status2)) {
647 return_ACPI_STATUS (status2);
655 case AE_CTRL_TERMINATE:
662 status2 = acpi_ps_complete_this_op (walk_state, op);
663 if (ACPI_FAILURE (status2)) {
664 return_ACPI_STATUS (status2);
667 acpi_ps_pop_scope (parser_state, &op,
668 &walk_state->arg_types, &walk_state->arg_count);
672 return_ACPI_STATUS (status);
675 default: /* All other non-AE_OK status */
679 status2 = acpi_ps_complete_this_op (walk_state, op);
680 if (ACPI_FAILURE (status2)) {
681 return_ACPI_STATUS (status2);
684 acpi_ps_pop_scope (parser_state, &op,
685 &walk_state->arg_types, &walk_state->arg_count);
691 * TBD: Cleanup parse ops on error
695 acpi_ps_pop_scope (parser_state, &op,
696 &walk_state->arg_types, &walk_state->arg_count);
699 walk_state->prev_op = op;
700 walk_state->prev_arg_types = walk_state->arg_types;
701 return_ACPI_STATUS (status);
704 /* This scope complete? */
706 if (acpi_ps_has_completed_scope (parser_state)) {
707 acpi_ps_pop_scope (parser_state, &op,
708 &walk_state->arg_types, &walk_state->arg_count);
709 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", op));
715 } /* while parser_state->Aml */
719 * Complete the last Op (if not completed), and clear the scope stack.
720 * It is easily possible to end an AML "package" with an unbounded number
721 * of open scopes (such as when several ASL blocks are closed with
722 * sequential closing braces). We want to terminate each one cleanly.
724 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", op));
727 if (walk_state->ascending_callback != NULL) {
729 walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
730 walk_state->opcode = op->common.aml_opcode;
732 status = walk_state->ascending_callback (walk_state);
733 status = acpi_ps_next_parse_state (walk_state, op, status);
734 if (status == AE_CTRL_PENDING) {
739 if (status == AE_CTRL_TERMINATE) {
745 status2 = acpi_ps_complete_this_op (walk_state, op);
746 if (ACPI_FAILURE (status2)) {
747 return_ACPI_STATUS (status2);
751 acpi_ps_pop_scope (parser_state, &op,
752 &walk_state->arg_types, &walk_state->arg_count);
756 return_ACPI_STATUS (status);
759 else if (ACPI_FAILURE (status)) {
760 /* First error is most important */
762 (void) acpi_ps_complete_this_op (walk_state, op);
763 return_ACPI_STATUS (status);
767 status2 = acpi_ps_complete_this_op (walk_state, op);
768 if (ACPI_FAILURE (status2)) {
769 return_ACPI_STATUS (status2);
773 acpi_ps_pop_scope (parser_state, &op, &walk_state->arg_types,
774 &walk_state->arg_count);
778 return_ACPI_STATUS (status);