1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2018, 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.
44 #include <acpi/acpi.h>
48 #include "acconvert.h"
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME("psobject")
53 /* Local prototypes */
54 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
56 /*******************************************************************************
58 * FUNCTION: acpi_ps_get_aml_opcode
60 * PARAMETERS: walk_state - Current state
64 * DESCRIPTION: Extract the next AML opcode from the input stream.
66 ******************************************************************************/
68 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
70 ACPI_ERROR_ONLY(u32 aml_offset);
72 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
74 walk_state->aml = walk_state->parser_state.aml;
75 walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
78 * First cut to determine what we have found:
79 * 1) A valid AML opcode
81 * 3) An unknown/invalid opcode
83 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
85 switch (walk_state->op_info->class) {
87 case AML_CLASS_PREFIX:
89 * Starts with a valid prefix or ASCII char, this is a name
90 * string. Convert the bare name string to a namepath.
92 walk_state->opcode = AML_INT_NAMEPATH_OP;
93 walk_state->arg_types = ARGP_NAMESTRING;
96 case AML_CLASS_UNKNOWN:
98 /* The opcode is unrecognized. Complain and skip unknown opcodes */
100 if (walk_state->pass_number == 2) {
101 ACPI_ERROR_ONLY(aml_offset =
102 (u32)ACPI_PTR_DIFF(walk_state->aml,
108 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
111 sizeof(struct acpi_table_header))));
113 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
116 #ifdef ACPI_ASL_COMPILER
118 * This is executed for the disassembler only. Output goes
119 * to the disassembled ASL output file.
122 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
125 sizeof(struct acpi_table_header)));
128 "Aborting disassembly, AML byte code is corrupt"));
130 /* Dump the context surrounding the invalid opcode */
132 acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
133 aml - 16), 48, DB_BYTE_DISPLAY,
135 sizeof(struct acpi_table_header) -
137 acpi_os_printf(" */\n");
140 * Just abort the disassembly, cannot continue because the
141 * parser is essentially lost. The disassembler can then
142 * randomly fail because an ill-constructed parse tree
145 return_ACPI_STATUS(AE_AML_BAD_OPCODE);
149 /* Increment past one-byte or two-byte opcode */
151 walk_state->parser_state.aml++;
152 if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
153 walk_state->parser_state.aml++;
156 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
160 /* Found opcode info, this is a normal opcode */
162 walk_state->parser_state.aml +=
163 acpi_ps_get_opcode_size(walk_state->opcode);
164 walk_state->arg_types = walk_state->op_info->parse_args;
168 return_ACPI_STATUS(AE_OK);
171 /*******************************************************************************
173 * FUNCTION: acpi_ps_build_named_op
175 * PARAMETERS: walk_state - Current state
176 * aml_op_start - Begin of named Op in AML
177 * unnamed_op - Early Op (not a named Op)
182 * DESCRIPTION: Parse a named Op
184 ******************************************************************************/
187 acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
189 union acpi_parse_object *unnamed_op,
190 union acpi_parse_object **op)
192 acpi_status status = AE_OK;
193 union acpi_parse_object *arg = NULL;
195 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
197 unnamed_op->common.value.arg = NULL;
198 unnamed_op->common.arg_list_length = 0;
199 unnamed_op->common.aml_opcode = walk_state->opcode;
202 * Get and append arguments until we find the node that contains
203 * the name (the type ARGP_NAME).
205 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
206 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
207 ASL_CV_CAPTURE_COMMENTS(walk_state);
209 acpi_ps_get_next_arg(walk_state,
210 &(walk_state->parser_state),
211 GET_CURRENT_ARG_TYPE(walk_state->
213 if (ACPI_FAILURE(status)) {
214 return_ACPI_STATUS(status);
217 acpi_ps_append_arg(unnamed_op, arg);
218 INCREMENT_ARG_LIST(walk_state->arg_types);
221 /* are there any inline comments associated with the name_seg?? If so, save this. */
223 ASL_CV_CAPTURE_COMMENTS(walk_state);
225 #ifdef ACPI_ASL_COMPILER
226 if (acpi_gbl_current_inline_comment != NULL) {
227 unnamed_op->common.name_comment =
228 acpi_gbl_current_inline_comment;
229 acpi_gbl_current_inline_comment = NULL;
234 * Make sure that we found a NAME and didn't run out of arguments
236 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
237 return_ACPI_STATUS(AE_AML_NO_OPERAND);
240 /* We know that this arg is a name, move to next arg */
242 INCREMENT_ARG_LIST(walk_state->arg_types);
245 * Find the object. This will either insert the object into
246 * the namespace or simply look it up
248 walk_state->op = NULL;
250 status = walk_state->descending_callback(walk_state, op);
251 if (ACPI_FAILURE(status)) {
252 if (status != AE_CTRL_TERMINATE) {
253 ACPI_EXCEPTION((AE_INFO, status,
254 "During name lookup/catalog"));
256 return_ACPI_STATUS(status);
260 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
263 status = acpi_ps_next_parse_state(walk_state, *op, status);
264 if (ACPI_FAILURE(status)) {
265 if (status == AE_CTRL_PENDING) {
266 status = AE_CTRL_PARSE_PENDING;
268 return_ACPI_STATUS(status);
271 acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
273 #ifdef ACPI_ASL_COMPILER
275 /* save any comments that might be associated with unnamed_op. */
277 (*op)->common.inline_comment = unnamed_op->common.inline_comment;
278 (*op)->common.end_node_comment = unnamed_op->common.end_node_comment;
279 (*op)->common.close_brace_comment =
280 unnamed_op->common.close_brace_comment;
281 (*op)->common.name_comment = unnamed_op->common.name_comment;
282 (*op)->common.comment_list = unnamed_op->common.comment_list;
283 (*op)->common.end_blk_comment = unnamed_op->common.end_blk_comment;
284 (*op)->common.cv_filename = unnamed_op->common.cv_filename;
285 (*op)->common.cv_parent_filename =
286 unnamed_op->common.cv_parent_filename;
287 (*op)->named.aml = unnamed_op->common.aml;
289 unnamed_op->common.inline_comment = NULL;
290 unnamed_op->common.end_node_comment = NULL;
291 unnamed_op->common.close_brace_comment = NULL;
292 unnamed_op->common.name_comment = NULL;
293 unnamed_op->common.comment_list = NULL;
294 unnamed_op->common.end_blk_comment = NULL;
297 if ((*op)->common.aml_opcode == AML_REGION_OP ||
298 (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
300 * Defer final parsing of an operation_region body, because we don't
301 * have enough info in the first pass to parse it correctly (i.e.,
302 * there may be method calls within the term_arg elements of the body.)
304 * However, we must continue parsing because the opregion is not a
305 * standalone package -- we don't know where the end is at this point.
307 * (Length is unknown until parse of the body complete)
309 (*op)->named.data = aml_op_start;
310 (*op)->named.length = 0;
313 return_ACPI_STATUS(AE_OK);
316 /*******************************************************************************
318 * FUNCTION: acpi_ps_create_op
320 * PARAMETERS: walk_state - Current state
321 * aml_op_start - Op start in AML
322 * new_op - Returned Op
326 * DESCRIPTION: Get Op from AML
328 ******************************************************************************/
331 acpi_ps_create_op(struct acpi_walk_state *walk_state,
332 u8 *aml_op_start, union acpi_parse_object **new_op)
334 acpi_status status = AE_OK;
335 union acpi_parse_object *op;
336 union acpi_parse_object *named_op = NULL;
337 union acpi_parse_object *parent_scope;
339 const struct acpi_opcode_info *op_info;
341 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
343 status = acpi_ps_get_aml_opcode(walk_state);
344 if (status == AE_CTRL_PARSE_CONTINUE) {
345 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
347 if (ACPI_FAILURE(status)) {
348 return_ACPI_STATUS(status);
351 /* Create Op structure and append to parent's argument list */
353 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
354 op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start);
356 return_ACPI_STATUS(AE_NO_MEMORY);
359 if (walk_state->op_info->flags & AML_NAMED) {
361 acpi_ps_build_named_op(walk_state, aml_op_start, op,
365 #ifdef ACPI_ASL_COMPILER
366 if (acpi_gbl_disasm_flag
367 && walk_state->opcode == AML_EXTERNAL_OP
368 && status == AE_NOT_FOUND) {
370 * If parsing of AML_EXTERNAL_OP's name path fails, then skip
371 * past this opcode and keep parsing. This is a much better
372 * alternative than to abort the entire disassembler. At this
373 * point, the parser_state is at the end of the namepath of the
374 * external declaration opcode. Setting walk_state->Aml to
375 * walk_state->parser_state.Aml + 2 moves increments the
376 * walk_state->Aml past the object type and the paramcount of the
379 walk_state->aml = walk_state->parser_state.aml + 2;
380 walk_state->parser_state.aml = walk_state->aml;
381 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
384 if (ACPI_FAILURE(status)) {
385 return_ACPI_STATUS(status);
389 return_ACPI_STATUS(AE_OK);
392 /* Not a named opcode, just allocate Op and append to parent */
394 if (walk_state->op_info->flags & AML_CREATE) {
396 * Backup to beginning of create_XXXfield declaration
397 * body_length is unknown until we parse the body
399 op->named.data = aml_op_start;
400 op->named.length = 0;
403 if (walk_state->opcode == AML_BANK_FIELD_OP) {
405 * Backup to beginning of bank_field declaration
406 * body_length is unknown until we parse the body
408 op->named.data = aml_op_start;
409 op->named.length = 0;
412 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
413 acpi_ps_append_arg(parent_scope, op);
417 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
418 if (op_info->flags & AML_HAS_TARGET) {
420 acpi_ps_get_argument_count(op_info->type);
421 if (parent_scope->common.arg_list_length >
423 op->common.flags |= ACPI_PARSEOP_TARGET;
428 * Special case for both Increment() and Decrement(), where
429 * the lone argument is both a source and a target.
431 else if ((parent_scope->common.aml_opcode == AML_INCREMENT_OP)
432 || (parent_scope->common.aml_opcode ==
434 op->common.flags |= ACPI_PARSEOP_TARGET;
438 if (walk_state->descending_callback != NULL) {
440 * Find the object. This will either insert the object into
441 * the namespace or simply look it up
443 walk_state->op = *new_op = op;
445 status = walk_state->descending_callback(walk_state, &op);
446 status = acpi_ps_next_parse_state(walk_state, op, status);
447 if (status == AE_CTRL_PENDING) {
448 status = AE_CTRL_PARSE_PENDING;
452 return_ACPI_STATUS(status);
455 /*******************************************************************************
457 * FUNCTION: acpi_ps_complete_op
459 * PARAMETERS: walk_state - Current state
461 * status - Parse status before complete Op
465 * DESCRIPTION: Complete Op
467 ******************************************************************************/
470 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
471 union acpi_parse_object **op, acpi_status status)
475 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
478 * Finished one argument of the containing scope
480 walk_state->parser_state.scope->parse_scope.arg_count--;
482 /* Close this Op (will result in parse subtree deletion) */
484 status2 = acpi_ps_complete_this_op(walk_state, *op);
485 if (ACPI_FAILURE(status2)) {
486 return_ACPI_STATUS(status2);
496 case AE_CTRL_TRANSFER:
498 /* We are about to transfer to a called method */
500 walk_state->prev_op = NULL;
501 walk_state->prev_arg_types = walk_state->arg_types;
502 return_ACPI_STATUS(status);
506 acpi_ps_pop_scope(&(walk_state->parser_state), op,
507 &walk_state->arg_types,
508 &walk_state->arg_count);
511 walk_state->op = *op;
512 walk_state->op_info =
513 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
514 walk_state->opcode = (*op)->common.aml_opcode;
516 status = walk_state->ascending_callback(walk_state);
518 acpi_ps_next_parse_state(walk_state, *op, status);
520 status2 = acpi_ps_complete_this_op(walk_state, *op);
521 if (ACPI_FAILURE(status2)) {
522 return_ACPI_STATUS(status2);
530 case AE_CTRL_CONTINUE:
532 /* Pop off scopes until we find the While */
534 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
535 acpi_ps_pop_scope(&(walk_state->parser_state), op,
536 &walk_state->arg_types,
537 &walk_state->arg_count);
540 /* Close this iteration of the While loop */
542 walk_state->op = *op;
543 walk_state->op_info =
544 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
545 walk_state->opcode = (*op)->common.aml_opcode;
547 status = walk_state->ascending_callback(walk_state);
548 status = acpi_ps_next_parse_state(walk_state, *op, status);
550 status2 = acpi_ps_complete_this_op(walk_state, *op);
551 if (ACPI_FAILURE(status2)) {
552 return_ACPI_STATUS(status2);
558 case AE_CTRL_TERMINATE:
564 acpi_ps_complete_this_op(walk_state, *op);
565 if (ACPI_FAILURE(status2)) {
566 return_ACPI_STATUS(status2);
569 acpi_ut_delete_generic_state
570 (acpi_ut_pop_generic_state
571 (&walk_state->control_state));
574 acpi_ps_pop_scope(&(walk_state->parser_state), op,
575 &walk_state->arg_types,
576 &walk_state->arg_count);
580 return_ACPI_STATUS(AE_OK);
582 default: /* All other non-AE_OK status */
587 acpi_ps_complete_this_op(walk_state, *op);
588 if (ACPI_FAILURE(status2)) {
589 return_ACPI_STATUS(status2);
593 acpi_ps_pop_scope(&(walk_state->parser_state), op,
594 &walk_state->arg_types,
595 &walk_state->arg_count);
601 * TBD: Cleanup parse ops on error
604 acpi_ps_pop_scope(parser_state, op,
605 &walk_state->arg_types,
606 &walk_state->arg_count);
609 walk_state->prev_op = NULL;
610 walk_state->prev_arg_types = walk_state->arg_types;
611 return_ACPI_STATUS(status);
614 /* This scope complete? */
616 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
617 acpi_ps_pop_scope(&(walk_state->parser_state), op,
618 &walk_state->arg_types,
619 &walk_state->arg_count);
620 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
625 return_ACPI_STATUS(AE_OK);
628 /*******************************************************************************
630 * FUNCTION: acpi_ps_complete_final_op
632 * PARAMETERS: walk_state - Current state
634 * status - Current parse status before complete last
639 * DESCRIPTION: Complete last Op.
641 ******************************************************************************/
644 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
645 union acpi_parse_object *op, acpi_status status)
649 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
652 * Complete the last Op (if not completed), and clear the scope stack.
653 * It is easily possible to end an AML "package" with an unbounded number
654 * of open scopes (such as when several ASL blocks are closed with
655 * sequential closing braces). We want to terminate each one cleanly.
657 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
661 if (walk_state->ascending_callback != NULL) {
663 walk_state->op_info =
664 acpi_ps_get_opcode_info(op->common.
666 walk_state->opcode = op->common.aml_opcode;
669 walk_state->ascending_callback(walk_state);
671 acpi_ps_next_parse_state(walk_state, op,
673 if (status == AE_CTRL_PENDING) {
675 acpi_ps_complete_op(walk_state, &op,
677 if (ACPI_FAILURE(status)) {
678 return_ACPI_STATUS(status);
682 if (status == AE_CTRL_TERMINATE) {
689 acpi_ps_complete_this_op
709 return_ACPI_STATUS(status);
712 else if (ACPI_FAILURE(status)) {
714 /* First error is most important */
717 acpi_ps_complete_this_op(walk_state,
719 return_ACPI_STATUS(status);
723 status2 = acpi_ps_complete_this_op(walk_state, op);
724 if (ACPI_FAILURE(status2)) {
725 return_ACPI_STATUS(status2);
729 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
730 &walk_state->arg_types,
731 &walk_state->arg_count);
735 return_ACPI_STATUS(status);