1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
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, (such as
46 * Perl) do. Parsing is done by hand rather than with a YACC generated parser
47 * to tightly constrain stack and dynamic memory usage. Parsing is kept
48 * flexible and the code fairly compact by parsing based on a list of AML
49 * opcode templates in aml_op_info[].
52 #include <acpi/acpi.h>
58 #define _COMPONENT ACPI_PARSER
59 ACPI_MODULE_NAME("psloop")
61 /* Local prototypes */
63 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
64 u8 * aml_op_start, union acpi_parse_object *op);
67 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
68 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
70 /*******************************************************************************
72 * FUNCTION: acpi_ps_get_arguments
74 * PARAMETERS: walk_state - Current state
75 * aml_op_start - Op start in AML
80 * DESCRIPTION: Get arguments for passed Op.
82 ******************************************************************************/
85 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
86 u8 * aml_op_start, union acpi_parse_object *op)
88 acpi_status status = AE_OK;
89 union acpi_parse_object *arg = NULL;
90 const struct acpi_opcode_info *op_info;
92 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
94 switch (op->common.aml_opcode) {
95 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
96 case AML_WORD_OP: /* AML_WORDDATA_ARG */
97 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
98 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
99 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
101 /* Fill in constant or string argument directly */
103 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
104 GET_CURRENT_ARG_TYPE(walk_state->
109 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
112 acpi_ps_get_next_namepath(walk_state,
113 &(walk_state->parser_state), op,
115 if (ACPI_FAILURE(status)) {
116 return_ACPI_STATUS(status);
119 walk_state->arg_types = 0;
124 * Op is not a constant or string, append each argument to the Op
126 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
127 && !walk_state->arg_count) {
128 walk_state->aml = walk_state->parser_state.aml;
131 acpi_ps_get_next_arg(walk_state,
132 &(walk_state->parser_state),
134 (walk_state->arg_types), &arg);
135 if (ACPI_FAILURE(status)) {
136 return_ACPI_STATUS(status);
140 acpi_ps_append_arg(op, arg);
143 INCREMENT_ARG_LIST(walk_state->arg_types);
147 * Handle executable code at "module-level". This refers to
148 * executable opcodes that appear outside of any control method.
150 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
151 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
153 * We want to skip If/Else/While constructs during Pass1 because we
154 * want to actually conditionally execute the code during Pass2.
156 * Except for disassembly, where we always want to walk the
157 * If/Else/While packages
159 switch (op->common.aml_opcode) {
164 * Currently supported module-level opcodes are:
165 * IF/ELSE/WHILE. These appear to be the most common,
166 * and easiest to support since they open an AML
169 if (walk_state->pass_number ==
170 ACPI_IMODE_LOAD_PASS1) {
171 acpi_ps_link_module_code(op->common.
183 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
184 "Pass1: Skipping an If/Else/While body\n"));
186 /* Skip body of if/else/while in pass 1 */
188 walk_state->parser_state.aml =
189 walk_state->parser_state.pkg_end;
190 walk_state->arg_count = 0;
195 * Check for an unsupported executable opcode at module
196 * level. We must be in PASS1, the parent must be a SCOPE,
197 * The opcode class must be EXECUTE, and the opcode must
198 * not be an argument to another opcode.
200 if ((walk_state->pass_number ==
201 ACPI_IMODE_LOAD_PASS1)
202 && (op->common.parent->common.aml_opcode ==
205 acpi_ps_get_opcode_info(op->common.
207 if ((op_info->class ==
208 AML_CLASS_EXECUTE) && (!arg)) {
209 ACPI_WARNING((AE_INFO,
210 "Unsupported module-level executable opcode "
211 "0x%.2X at table offset 0x%.4X",
221 acpi_table_header))));
228 /* Special processing for certain opcodes */
230 switch (op->common.aml_opcode) {
233 * Skip parsing of control method because we don't have enough
234 * info in the first pass to parse it correctly.
236 * Save the length and address of the body
238 op->named.data = walk_state->parser_state.aml;
239 op->named.length = (u32)
240 (walk_state->parser_state.pkg_end -
241 walk_state->parser_state.aml);
243 /* Skip body of method */
245 walk_state->parser_state.aml =
246 walk_state->parser_state.pkg_end;
247 walk_state->arg_count = 0;
252 case AML_VAR_PACKAGE_OP:
254 if ((op->common.parent) &&
255 (op->common.parent->common.aml_opcode ==
257 && (walk_state->pass_number <=
258 ACPI_IMODE_LOAD_PASS2)) {
260 * Skip parsing of Buffers and Packages because we don't have
261 * enough info in the first pass to parse them correctly.
263 op->named.data = aml_op_start;
264 op->named.length = (u32)
265 (walk_state->parser_state.pkg_end -
270 walk_state->parser_state.aml =
271 walk_state->parser_state.pkg_end;
272 walk_state->arg_count = 0;
278 if (walk_state->control_state) {
279 walk_state->control_state->control.package_end =
280 walk_state->parser_state.pkg_end;
286 /* No action for all other opcodes */
294 return_ACPI_STATUS(AE_OK);
297 /*******************************************************************************
299 * FUNCTION: acpi_ps_link_module_code
301 * PARAMETERS: parent_op - Parent parser op
302 * aml_start - Pointer to the AML
303 * aml_length - Length of executable AML
304 * owner_id - owner_id of module level code
308 * DESCRIPTION: Wrap the module-level code with a method object and link the
309 * object to the global list. Note, the mutex field of the method
310 * object is used to link multiple module-level code objects.
312 ******************************************************************************/
315 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
316 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
318 union acpi_operand_object *prev;
319 union acpi_operand_object *next;
320 union acpi_operand_object *method_obj;
321 struct acpi_namespace_node *parent_node;
323 /* Get the tail of the list */
325 prev = next = acpi_gbl_module_code_list;
328 next = next->method.mutex;
332 * Insert the module level code into the list. Merge it if it is
333 * adjacent to the previous element.
336 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
338 /* Create, initialize, and link a new temporary method object */
340 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
345 if (parent_op->common.node) {
346 parent_node = parent_op->common.node;
348 parent_node = acpi_gbl_root_node;
351 method_obj->method.aml_start = aml_start;
352 method_obj->method.aml_length = aml_length;
353 method_obj->method.owner_id = owner_id;
354 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
357 * Save the parent node in next_object. This is cheating, but we
358 * don't want to expand the method object.
360 method_obj->method.next_object =
361 ACPI_CAST_PTR(union acpi_operand_object, parent_node);
364 acpi_gbl_module_code_list = method_obj;
366 prev->method.mutex = method_obj;
369 prev->method.aml_length += aml_length;
373 /*******************************************************************************
375 * FUNCTION: acpi_ps_parse_loop
377 * PARAMETERS: walk_state - Current state
381 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
384 ******************************************************************************/
386 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
388 acpi_status status = AE_OK;
389 union acpi_parse_object *op = NULL; /* current op */
390 struct acpi_parse_state *parser_state;
391 u8 *aml_op_start = NULL;
393 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
395 if (walk_state->descending_callback == NULL) {
396 return_ACPI_STATUS(AE_BAD_PARAMETER);
399 parser_state = &walk_state->parser_state;
400 walk_state->arg_types = 0;
402 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
404 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
406 /* We are restarting a preempted control method */
408 if (acpi_ps_has_completed_scope(parser_state)) {
410 * We must check if a predicate to an IF or WHILE statement
413 if ((parser_state->scope->parse_scope.op) &&
414 ((parser_state->scope->parse_scope.op->common.
415 aml_opcode == AML_IF_OP)
416 || (parser_state->scope->parse_scope.op->common.
417 aml_opcode == AML_WHILE_OP))
418 && (walk_state->control_state)
419 && (walk_state->control_state->common.state ==
420 ACPI_CONTROL_PREDICATE_EXECUTING)) {
422 * A predicate was just completed, get the value of the
423 * predicate and branch based on that value
425 walk_state->op = NULL;
427 acpi_ds_get_predicate_value(walk_state,
430 if (ACPI_FAILURE(status)
431 && ((status & AE_CODE_MASK) !=
433 if (status == AE_AML_NO_RETURN_VALUE) {
434 ACPI_EXCEPTION((AE_INFO, status,
435 "Invoked method did not return a value"));
438 ACPI_EXCEPTION((AE_INFO, status,
439 "GetPredicate Failed"));
440 return_ACPI_STATUS(status);
444 acpi_ps_next_parse_state(walk_state, op,
448 acpi_ps_pop_scope(parser_state, &op,
449 &walk_state->arg_types,
450 &walk_state->arg_count);
451 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
452 "Popped scope, Op=%p\n", op));
453 } else if (walk_state->prev_op) {
455 /* We were in the middle of an op */
457 op = walk_state->prev_op;
458 walk_state->arg_types = walk_state->prev_arg_types;
463 /* Iterative parsing loop, while there is more AML to process: */
465 while ((parser_state->aml < parser_state->aml_end) || (op)) {
466 aml_op_start = parser_state->aml;
469 acpi_ps_create_op(walk_state, aml_op_start, &op);
470 if (ACPI_FAILURE(status)) {
471 if (status == AE_CTRL_PARSE_CONTINUE) {
475 if (status == AE_CTRL_PARSE_PENDING) {
479 if (status == AE_CTRL_TERMINATE) {
480 return_ACPI_STATUS(status);
484 acpi_ps_complete_op(walk_state, &op,
486 if (ACPI_FAILURE(status)) {
487 return_ACPI_STATUS(status);
493 if (walk_state->op_info) {
494 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
495 "Opcode %4.4X [%s] Op %p Aml %p\n",
496 (u32)op->common.aml_opcode,
497 walk_state->op_info->name, op,
501 if (walk_state->op_info) {
502 ACPI_DEBUG_PRINT((ACPI_DB_TRACE_POINT,
503 "Begin opcode: %s[0x%p] Class=0x%02x, Type=0x%02x, Flags=0x%04x.\n",
504 op->common.aml_op_name,
506 walk_state->op_info->class,
507 walk_state->op_info->type,
508 walk_state->op_info->flags));
510 ACPI_DEBUG_PRINT((ACPI_DB_TRACE_POINT,
511 "Begin opcode: %s[0x%p].\n",
512 op->common.aml_op_name,
518 * Start arg_count at zero because we don't know if there are
521 walk_state->arg_count = 0;
523 /* Are there any arguments that must be processed? */
525 if (walk_state->arg_types) {
530 acpi_ps_get_arguments(walk_state, aml_op_start, op);
531 if (ACPI_FAILURE(status)) {
533 acpi_ps_complete_op(walk_state, &op,
535 if (ACPI_FAILURE(status)) {
536 return_ACPI_STATUS(status);
543 /* Check for arguments that need to be processed */
545 if (walk_state->arg_count) {
547 * There are arguments (complex ones), push Op and
548 * prepare for argument
550 status = acpi_ps_push_scope(parser_state, op,
551 walk_state->arg_types,
552 walk_state->arg_count);
553 if (ACPI_FAILURE(status)) {
555 acpi_ps_complete_op(walk_state, &op,
557 if (ACPI_FAILURE(status)) {
558 return_ACPI_STATUS(status);
569 * All arguments have been processed -- Op is complete,
572 walk_state->op_info =
573 acpi_ps_get_opcode_info(op->common.aml_opcode);
574 if (walk_state->op_info->flags & AML_NAMED) {
575 if (op->common.aml_opcode == AML_REGION_OP ||
576 op->common.aml_opcode == AML_DATA_REGION_OP) {
578 * Skip parsing of control method or opregion body,
579 * because we don't have enough info in the first pass
580 * to parse them correctly.
582 * Completed parsing an op_region declaration, we now
586 (u32) (parser_state->aml - op->named.data);
590 if (walk_state->op_info->flags & AML_CREATE) {
592 * Backup to beginning of create_XXXfield declaration (1 for
595 * body_length is unknown until we parse the body
598 (u32) (parser_state->aml - op->named.data);
601 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
603 * Backup to beginning of bank_field declaration
605 * body_length is unknown until we parse the body
608 (u32) (parser_state->aml - op->named.data);
611 /* This op complete, notify the dispatcher */
613 if (walk_state->ascending_callback != NULL) {
615 walk_state->opcode = op->common.aml_opcode;
617 status = walk_state->ascending_callback(walk_state);
619 acpi_ps_next_parse_state(walk_state, op, status);
620 if (status == AE_CTRL_PENDING) {
625 status = acpi_ps_complete_op(walk_state, &op, status);
626 if (ACPI_FAILURE(status)) {
627 return_ACPI_STATUS(status);
630 } /* while parser_state->Aml */
632 status = acpi_ps_complete_final_op(walk_state, op, status);
633 return_ACPI_STATUS(status);