1 /******************************************************************************
3 * Module Name: psloop - Main AML parse loop
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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_offset =
129 (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
130 walk_state->parser_state.
134 acpi_ps_get_next_arg(walk_state,
135 &(walk_state->parser_state),
137 (walk_state->arg_types), &arg);
138 if (ACPI_FAILURE(status)) {
139 return_ACPI_STATUS(status);
143 arg->common.aml_offset = walk_state->aml_offset;
144 acpi_ps_append_arg(op, arg);
147 INCREMENT_ARG_LIST(walk_state->arg_types);
151 * Handle executable code at "module-level". This refers to
152 * executable opcodes that appear outside of any control method.
154 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
155 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
157 * We want to skip If/Else/While constructs during Pass1 because we
158 * want to actually conditionally execute the code during Pass2.
160 * Except for disassembly, where we always want to walk the
161 * If/Else/While packages
163 switch (op->common.aml_opcode) {
168 * Currently supported module-level opcodes are:
169 * IF/ELSE/WHILE. These appear to be the most common,
170 * and easiest to support since they open an AML
173 if (walk_state->pass_number ==
174 ACPI_IMODE_LOAD_PASS1) {
175 acpi_ps_link_module_code(op->common.
187 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
188 "Pass1: Skipping an If/Else/While body\n"));
190 /* Skip body of if/else/while in pass 1 */
192 walk_state->parser_state.aml =
193 walk_state->parser_state.pkg_end;
194 walk_state->arg_count = 0;
199 * Check for an unsupported executable opcode at module
200 * level. We must be in PASS1, the parent must be a SCOPE,
201 * The opcode class must be EXECUTE, and the opcode must
202 * not be an argument to another opcode.
204 if ((walk_state->pass_number ==
205 ACPI_IMODE_LOAD_PASS1)
206 && (op->common.parent->common.aml_opcode ==
209 acpi_ps_get_opcode_info(op->common.
211 if ((op_info->class ==
212 AML_CLASS_EXECUTE) && (!arg)) {
213 ACPI_WARNING((AE_INFO,
214 "Unsupported module-level executable opcode "
215 "0x%.2X at table offset 0x%.4X",
225 acpi_table_header))));
232 /* Special processing for certain opcodes */
234 switch (op->common.aml_opcode) {
237 * Skip parsing of control method because we don't have enough
238 * info in the first pass to parse it correctly.
240 * Save the length and address of the body
242 op->named.data = walk_state->parser_state.aml;
243 op->named.length = (u32)
244 (walk_state->parser_state.pkg_end -
245 walk_state->parser_state.aml);
247 /* Skip body of method */
249 walk_state->parser_state.aml =
250 walk_state->parser_state.pkg_end;
251 walk_state->arg_count = 0;
256 case AML_VAR_PACKAGE_OP:
258 if ((op->common.parent) &&
259 (op->common.parent->common.aml_opcode ==
261 && (walk_state->pass_number <=
262 ACPI_IMODE_LOAD_PASS2)) {
264 * Skip parsing of Buffers and Packages because we don't have
265 * enough info in the first pass to parse them correctly.
267 op->named.data = aml_op_start;
268 op->named.length = (u32)
269 (walk_state->parser_state.pkg_end -
274 walk_state->parser_state.aml =
275 walk_state->parser_state.pkg_end;
276 walk_state->arg_count = 0;
282 if (walk_state->control_state) {
283 walk_state->control_state->control.package_end =
284 walk_state->parser_state.pkg_end;
290 /* No action for all other opcodes */
298 return_ACPI_STATUS(AE_OK);
301 /*******************************************************************************
303 * FUNCTION: acpi_ps_link_module_code
305 * PARAMETERS: parent_op - Parent parser op
306 * aml_start - Pointer to the AML
307 * aml_length - Length of executable AML
308 * owner_id - owner_id of module level code
312 * DESCRIPTION: Wrap the module-level code with a method object and link the
313 * object to the global list. Note, the mutex field of the method
314 * object is used to link multiple module-level code objects.
316 ******************************************************************************/
319 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
320 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
322 union acpi_operand_object *prev;
323 union acpi_operand_object *next;
324 union acpi_operand_object *method_obj;
325 struct acpi_namespace_node *parent_node;
327 /* Get the tail of the list */
329 prev = next = acpi_gbl_module_code_list;
332 next = next->method.mutex;
336 * Insert the module level code into the list. Merge it if it is
337 * adjacent to the previous element.
340 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
342 /* Create, initialize, and link a new temporary method object */
344 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
349 if (parent_op->common.node) {
350 parent_node = parent_op->common.node;
352 parent_node = acpi_gbl_root_node;
355 method_obj->method.aml_start = aml_start;
356 method_obj->method.aml_length = aml_length;
357 method_obj->method.owner_id = owner_id;
358 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
361 * Save the parent node in next_object. This is cheating, but we
362 * don't want to expand the method object.
364 method_obj->method.next_object =
365 ACPI_CAST_PTR(union acpi_operand_object, parent_node);
368 acpi_gbl_module_code_list = method_obj;
370 prev->method.mutex = method_obj;
373 prev->method.aml_length += aml_length;
377 /*******************************************************************************
379 * FUNCTION: acpi_ps_parse_loop
381 * PARAMETERS: walk_state - Current state
385 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
388 ******************************************************************************/
390 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
392 acpi_status status = AE_OK;
393 union acpi_parse_object *op = NULL; /* current op */
394 struct acpi_parse_state *parser_state;
395 u8 *aml_op_start = NULL;
397 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
399 if (walk_state->descending_callback == NULL) {
400 return_ACPI_STATUS(AE_BAD_PARAMETER);
403 parser_state = &walk_state->parser_state;
404 walk_state->arg_types = 0;
406 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
408 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
410 /* We are restarting a preempted control method */
412 if (acpi_ps_has_completed_scope(parser_state)) {
414 * We must check if a predicate to an IF or WHILE statement
417 if ((parser_state->scope->parse_scope.op) &&
418 ((parser_state->scope->parse_scope.op->common.
419 aml_opcode == AML_IF_OP)
420 || (parser_state->scope->parse_scope.op->common.
421 aml_opcode == AML_WHILE_OP))
422 && (walk_state->control_state)
423 && (walk_state->control_state->common.state ==
424 ACPI_CONTROL_PREDICATE_EXECUTING)) {
426 * A predicate was just completed, get the value of the
427 * predicate and branch based on that value
429 walk_state->op = NULL;
431 acpi_ds_get_predicate_value(walk_state,
434 if (ACPI_FAILURE(status)
435 && ((status & AE_CODE_MASK) !=
437 if (status == AE_AML_NO_RETURN_VALUE) {
438 ACPI_EXCEPTION((AE_INFO, status,
439 "Invoked method did not return a value"));
442 ACPI_EXCEPTION((AE_INFO, status,
443 "GetPredicate Failed"));
444 return_ACPI_STATUS(status);
448 acpi_ps_next_parse_state(walk_state, op,
452 acpi_ps_pop_scope(parser_state, &op,
453 &walk_state->arg_types,
454 &walk_state->arg_count);
455 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
456 "Popped scope, Op=%p\n", op));
457 } else if (walk_state->prev_op) {
459 /* We were in the middle of an op */
461 op = walk_state->prev_op;
462 walk_state->arg_types = walk_state->prev_arg_types;
467 /* Iterative parsing loop, while there is more AML to process: */
469 while ((parser_state->aml < parser_state->aml_end) || (op)) {
470 aml_op_start = parser_state->aml;
473 acpi_ps_create_op(walk_state, aml_op_start, &op);
474 if (ACPI_FAILURE(status)) {
475 if (status == AE_CTRL_PARSE_CONTINUE) {
479 if (status == AE_CTRL_PARSE_PENDING) {
483 if (status == AE_CTRL_TERMINATE) {
484 return_ACPI_STATUS(status);
488 acpi_ps_complete_op(walk_state, &op,
490 if (ACPI_FAILURE(status)) {
491 return_ACPI_STATUS(status);
497 op->common.aml_offset = walk_state->aml_offset;
499 if (walk_state->op_info) {
500 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
501 "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
502 (u32) op->common.aml_opcode,
503 walk_state->op_info->name, op,
505 op->common.aml_offset));
510 * Start arg_count at zero because we don't know if there are
513 walk_state->arg_count = 0;
515 /* Are there any arguments that must be processed? */
517 if (walk_state->arg_types) {
522 acpi_ps_get_arguments(walk_state, aml_op_start, op);
523 if (ACPI_FAILURE(status)) {
525 acpi_ps_complete_op(walk_state, &op,
527 if (ACPI_FAILURE(status)) {
528 return_ACPI_STATUS(status);
535 /* Check for arguments that need to be processed */
537 if (walk_state->arg_count) {
539 * There are arguments (complex ones), push Op and
540 * prepare for argument
542 status = acpi_ps_push_scope(parser_state, op,
543 walk_state->arg_types,
544 walk_state->arg_count);
545 if (ACPI_FAILURE(status)) {
547 acpi_ps_complete_op(walk_state, &op,
549 if (ACPI_FAILURE(status)) {
550 return_ACPI_STATUS(status);
561 * All arguments have been processed -- Op is complete,
564 walk_state->op_info =
565 acpi_ps_get_opcode_info(op->common.aml_opcode);
566 if (walk_state->op_info->flags & AML_NAMED) {
567 if (op->common.aml_opcode == AML_REGION_OP ||
568 op->common.aml_opcode == AML_DATA_REGION_OP) {
570 * Skip parsing of control method or opregion body,
571 * because we don't have enough info in the first pass
572 * to parse them correctly.
574 * Completed parsing an op_region declaration, we now
578 (u32) (parser_state->aml - op->named.data);
582 if (walk_state->op_info->flags & AML_CREATE) {
584 * Backup to beginning of create_XXXfield declaration (1 for
587 * body_length is unknown until we parse the body
590 (u32) (parser_state->aml - op->named.data);
593 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
595 * Backup to beginning of bank_field declaration
597 * body_length is unknown until we parse the body
600 (u32) (parser_state->aml - op->named.data);
603 /* This op complete, notify the dispatcher */
605 if (walk_state->ascending_callback != NULL) {
607 walk_state->opcode = op->common.aml_opcode;
609 status = walk_state->ascending_callback(walk_state);
611 acpi_ps_next_parse_state(walk_state, op, status);
612 if (status == AE_CTRL_PENDING) {
617 status = acpi_ps_complete_op(walk_state, &op, status);
618 if (ACPI_FAILURE(status)) {
619 return_ACPI_STATUS(status);
622 } /* while parser_state->Aml */
624 status = acpi_ps_complete_final_op(walk_state, op, status);
625 return_ACPI_STATUS(status);