1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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)
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 aml_offset = (u32)ACPI_PTR_DIFF(walk_state->aml,
103 parser_state.aml_start);
106 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
109 sizeof(struct acpi_table_header))));
111 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
114 #ifdef ACPI_ASL_COMPILER
116 * This is executed for the disassembler only. Output goes
117 * to the disassembled ASL output file.
120 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
123 sizeof(struct acpi_table_header)));
125 /* Dump the context surrounding the invalid opcode */
127 acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
128 aml - 16), 48, DB_BYTE_DISPLAY,
130 sizeof(struct acpi_table_header) -
132 acpi_os_printf(" */\n");
136 /* Increment past one-byte or two-byte opcode */
138 walk_state->parser_state.aml++;
139 if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
140 walk_state->parser_state.aml++;
143 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
147 /* Found opcode info, this is a normal opcode */
149 walk_state->parser_state.aml +=
150 acpi_ps_get_opcode_size(walk_state->opcode);
151 walk_state->arg_types = walk_state->op_info->parse_args;
155 return_ACPI_STATUS(AE_OK);
158 /*******************************************************************************
160 * FUNCTION: acpi_ps_build_named_op
162 * PARAMETERS: walk_state - Current state
163 * aml_op_start - Begin of named Op in AML
164 * unnamed_op - Early Op (not a named Op)
169 * DESCRIPTION: Parse a named Op
171 ******************************************************************************/
174 acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
176 union acpi_parse_object *unnamed_op,
177 union acpi_parse_object **op)
179 acpi_status status = AE_OK;
180 union acpi_parse_object *arg = NULL;
182 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
184 unnamed_op->common.value.arg = NULL;
185 unnamed_op->common.arg_list_length = 0;
186 unnamed_op->common.aml_opcode = walk_state->opcode;
189 * Get and append arguments until we find the node that contains
190 * the name (the type ARGP_NAME).
192 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
193 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
194 ASL_CV_CAPTURE_COMMENTS(walk_state);
196 acpi_ps_get_next_arg(walk_state,
197 &(walk_state->parser_state),
198 GET_CURRENT_ARG_TYPE(walk_state->
200 if (ACPI_FAILURE(status)) {
201 return_ACPI_STATUS(status);
204 acpi_ps_append_arg(unnamed_op, arg);
205 INCREMENT_ARG_LIST(walk_state->arg_types);
208 /* are there any inline comments associated with the name_seg?? If so, save this. */
210 ASL_CV_CAPTURE_COMMENTS(walk_state);
212 #ifdef ACPI_ASL_COMPILER
213 if (acpi_gbl_current_inline_comment != NULL) {
214 unnamed_op->common.name_comment =
215 acpi_gbl_current_inline_comment;
216 acpi_gbl_current_inline_comment = NULL;
221 * Make sure that we found a NAME and didn't run out of arguments
223 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
224 return_ACPI_STATUS(AE_AML_NO_OPERAND);
227 /* We know that this arg is a name, move to next arg */
229 INCREMENT_ARG_LIST(walk_state->arg_types);
232 * Find the object. This will either insert the object into
233 * the namespace or simply look it up
235 walk_state->op = NULL;
237 status = walk_state->descending_callback(walk_state, op);
238 if (ACPI_FAILURE(status)) {
239 if (status != AE_CTRL_TERMINATE) {
240 ACPI_EXCEPTION((AE_INFO, status,
241 "During name lookup/catalog"));
243 return_ACPI_STATUS(status);
247 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
250 status = acpi_ps_next_parse_state(walk_state, *op, status);
251 if (ACPI_FAILURE(status)) {
252 if (status == AE_CTRL_PENDING) {
253 status = AE_CTRL_PARSE_PENDING;
255 return_ACPI_STATUS(status);
258 acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
260 #ifdef ACPI_ASL_COMPILER
262 /* save any comments that might be associated with unnamed_op. */
264 (*op)->common.inline_comment = unnamed_op->common.inline_comment;
265 (*op)->common.end_node_comment = unnamed_op->common.end_node_comment;
266 (*op)->common.close_brace_comment =
267 unnamed_op->common.close_brace_comment;
268 (*op)->common.name_comment = unnamed_op->common.name_comment;
269 (*op)->common.comment_list = unnamed_op->common.comment_list;
270 (*op)->common.end_blk_comment = unnamed_op->common.end_blk_comment;
271 (*op)->common.cv_filename = unnamed_op->common.cv_filename;
272 (*op)->common.cv_parent_filename =
273 unnamed_op->common.cv_parent_filename;
274 (*op)->named.aml = unnamed_op->common.aml;
276 unnamed_op->common.inline_comment = NULL;
277 unnamed_op->common.end_node_comment = NULL;
278 unnamed_op->common.close_brace_comment = NULL;
279 unnamed_op->common.name_comment = NULL;
280 unnamed_op->common.comment_list = NULL;
281 unnamed_op->common.end_blk_comment = NULL;
284 if ((*op)->common.aml_opcode == AML_REGION_OP ||
285 (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
287 * Defer final parsing of an operation_region body, because we don't
288 * have enough info in the first pass to parse it correctly (i.e.,
289 * there may be method calls within the term_arg elements of the body.)
291 * However, we must continue parsing because the opregion is not a
292 * standalone package -- we don't know where the end is at this point.
294 * (Length is unknown until parse of the body complete)
296 (*op)->named.data = aml_op_start;
297 (*op)->named.length = 0;
300 return_ACPI_STATUS(AE_OK);
303 /*******************************************************************************
305 * FUNCTION: acpi_ps_create_op
307 * PARAMETERS: walk_state - Current state
308 * aml_op_start - Op start in AML
309 * new_op - Returned Op
313 * DESCRIPTION: Get Op from AML
315 ******************************************************************************/
318 acpi_ps_create_op(struct acpi_walk_state *walk_state,
319 u8 *aml_op_start, union acpi_parse_object **new_op)
321 acpi_status status = AE_OK;
322 union acpi_parse_object *op;
323 union acpi_parse_object *named_op = NULL;
324 union acpi_parse_object *parent_scope;
326 const struct acpi_opcode_info *op_info;
328 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
330 status = acpi_ps_get_aml_opcode(walk_state);
331 if (status == AE_CTRL_PARSE_CONTINUE) {
332 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
335 /* Create Op structure and append to parent's argument list */
337 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
338 op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start);
340 return_ACPI_STATUS(AE_NO_MEMORY);
343 if (walk_state->op_info->flags & AML_NAMED) {
345 acpi_ps_build_named_op(walk_state, aml_op_start, op,
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
353 return_ACPI_STATUS(AE_OK);
356 /* Not a named opcode, just allocate Op and append to parent */
358 if (walk_state->op_info->flags & AML_CREATE) {
360 * Backup to beginning of create_XXXfield declaration
361 * body_length is unknown until we parse the body
363 op->named.data = aml_op_start;
364 op->named.length = 0;
367 if (walk_state->opcode == AML_BANK_FIELD_OP) {
369 * Backup to beginning of bank_field declaration
370 * body_length is unknown until we parse the body
372 op->named.data = aml_op_start;
373 op->named.length = 0;
376 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
377 acpi_ps_append_arg(parent_scope, op);
381 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
382 if (op_info->flags & AML_HAS_TARGET) {
384 acpi_ps_get_argument_count(op_info->type);
385 if (parent_scope->common.arg_list_length >
387 op->common.flags |= ACPI_PARSEOP_TARGET;
392 * Special case for both Increment() and Decrement(), where
393 * the lone argument is both a source and a target.
395 else if ((parent_scope->common.aml_opcode == AML_INCREMENT_OP)
396 || (parent_scope->common.aml_opcode ==
398 op->common.flags |= ACPI_PARSEOP_TARGET;
402 if (walk_state->descending_callback != NULL) {
404 * Find the object. This will either insert the object into
405 * the namespace or simply look it up
407 walk_state->op = *new_op = op;
409 status = walk_state->descending_callback(walk_state, &op);
410 status = acpi_ps_next_parse_state(walk_state, op, status);
411 if (status == AE_CTRL_PENDING) {
412 status = AE_CTRL_PARSE_PENDING;
416 return_ACPI_STATUS(status);
419 /*******************************************************************************
421 * FUNCTION: acpi_ps_complete_op
423 * PARAMETERS: walk_state - Current state
425 * status - Parse status before complete Op
429 * DESCRIPTION: Complete Op
431 ******************************************************************************/
434 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
435 union acpi_parse_object **op, acpi_status status)
439 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
442 * Finished one argument of the containing scope
444 walk_state->parser_state.scope->parse_scope.arg_count--;
446 /* Close this Op (will result in parse subtree deletion) */
448 status2 = acpi_ps_complete_this_op(walk_state, *op);
449 if (ACPI_FAILURE(status2)) {
450 return_ACPI_STATUS(status2);
460 case AE_CTRL_TRANSFER:
462 /* We are about to transfer to a called method */
464 walk_state->prev_op = NULL;
465 walk_state->prev_arg_types = walk_state->arg_types;
466 return_ACPI_STATUS(status);
470 acpi_ps_pop_scope(&(walk_state->parser_state), op,
471 &walk_state->arg_types,
472 &walk_state->arg_count);
475 walk_state->op = *op;
476 walk_state->op_info =
477 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
478 walk_state->opcode = (*op)->common.aml_opcode;
480 status = walk_state->ascending_callback(walk_state);
482 acpi_ps_next_parse_state(walk_state, *op, status);
484 status2 = acpi_ps_complete_this_op(walk_state, *op);
485 if (ACPI_FAILURE(status2)) {
486 return_ACPI_STATUS(status2);
494 case AE_CTRL_CONTINUE:
496 /* Pop off scopes until we find the While */
498 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
499 acpi_ps_pop_scope(&(walk_state->parser_state), op,
500 &walk_state->arg_types,
501 &walk_state->arg_count);
504 /* Close this iteration of the While loop */
506 walk_state->op = *op;
507 walk_state->op_info =
508 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
509 walk_state->opcode = (*op)->common.aml_opcode;
511 status = walk_state->ascending_callback(walk_state);
512 status = acpi_ps_next_parse_state(walk_state, *op, status);
514 status2 = acpi_ps_complete_this_op(walk_state, *op);
515 if (ACPI_FAILURE(status2)) {
516 return_ACPI_STATUS(status2);
522 case AE_CTRL_TERMINATE:
528 acpi_ps_complete_this_op(walk_state, *op);
529 if (ACPI_FAILURE(status2)) {
530 return_ACPI_STATUS(status2);
533 acpi_ut_delete_generic_state
534 (acpi_ut_pop_generic_state
535 (&walk_state->control_state));
538 acpi_ps_pop_scope(&(walk_state->parser_state), op,
539 &walk_state->arg_types,
540 &walk_state->arg_count);
544 return_ACPI_STATUS(AE_OK);
546 default: /* All other non-AE_OK status */
551 acpi_ps_complete_this_op(walk_state, *op);
552 if (ACPI_FAILURE(status2)) {
553 return_ACPI_STATUS(status2);
557 acpi_ps_pop_scope(&(walk_state->parser_state), op,
558 &walk_state->arg_types,
559 &walk_state->arg_count);
565 * TBD: Cleanup parse ops on error
568 acpi_ps_pop_scope(parser_state, op,
569 &walk_state->arg_types,
570 &walk_state->arg_count);
573 walk_state->prev_op = NULL;
574 walk_state->prev_arg_types = walk_state->arg_types;
575 return_ACPI_STATUS(status);
578 /* This scope complete? */
580 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
581 acpi_ps_pop_scope(&(walk_state->parser_state), op,
582 &walk_state->arg_types,
583 &walk_state->arg_count);
584 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
589 return_ACPI_STATUS(AE_OK);
592 /*******************************************************************************
594 * FUNCTION: acpi_ps_complete_final_op
596 * PARAMETERS: walk_state - Current state
598 * status - Current parse status before complete last
603 * DESCRIPTION: Complete last Op.
605 ******************************************************************************/
608 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
609 union acpi_parse_object *op, acpi_status status)
613 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
616 * Complete the last Op (if not completed), and clear the scope stack.
617 * It is easily possible to end an AML "package" with an unbounded number
618 * of open scopes (such as when several ASL blocks are closed with
619 * sequential closing braces). We want to terminate each one cleanly.
621 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
625 if (walk_state->ascending_callback != NULL) {
627 walk_state->op_info =
628 acpi_ps_get_opcode_info(op->common.
630 walk_state->opcode = op->common.aml_opcode;
633 walk_state->ascending_callback(walk_state);
635 acpi_ps_next_parse_state(walk_state, op,
637 if (status == AE_CTRL_PENDING) {
639 acpi_ps_complete_op(walk_state, &op,
641 if (ACPI_FAILURE(status)) {
642 return_ACPI_STATUS(status);
646 if (status == AE_CTRL_TERMINATE) {
653 acpi_ps_complete_this_op
673 return_ACPI_STATUS(status);
676 else if (ACPI_FAILURE(status)) {
678 /* First error is most important */
681 acpi_ps_complete_this_op(walk_state,
683 return_ACPI_STATUS(status);
687 status2 = acpi_ps_complete_this_op(walk_state, op);
688 if (ACPI_FAILURE(status2)) {
689 return_ACPI_STATUS(status2);
693 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
694 &walk_state->arg_types,
695 &walk_state->arg_count);
699 return_ACPI_STATUS(status);