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
)
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
)));
126 "Aborting disassembly, AML byte code is corrupt"));
128 /* Dump the context surrounding the invalid opcode */
130 acpi_ut_dump_buffer(((u8
*)walk_state
->parser_state
.
131 aml
- 16), 48, DB_BYTE_DISPLAY
,
133 sizeof(struct acpi_table_header
) -
135 acpi_os_printf(" */\n");
138 * Just abort the disassembly, cannot continue because the
139 * parser is essentially lost. The disassembler can then
140 * randomly fail because an ill-constructed parse tree
143 return_ACPI_STATUS(AE_AML_BAD_OPCODE
);
147 /* Increment past one-byte or two-byte opcode */
149 walk_state
->parser_state
.aml
++;
150 if (walk_state
->opcode
> 0xFF) { /* Can only happen if first byte is 0x5B */
151 walk_state
->parser_state
.aml
++;
154 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
158 /* Found opcode info, this is a normal opcode */
160 walk_state
->parser_state
.aml
+=
161 acpi_ps_get_opcode_size(walk_state
->opcode
);
162 walk_state
->arg_types
= walk_state
->op_info
->parse_args
;
166 return_ACPI_STATUS(AE_OK
);
169 /*******************************************************************************
171 * FUNCTION: acpi_ps_build_named_op
173 * PARAMETERS: walk_state - Current state
174 * aml_op_start - Begin of named Op in AML
175 * unnamed_op - Early Op (not a named Op)
180 * DESCRIPTION: Parse a named Op
182 ******************************************************************************/
185 acpi_ps_build_named_op(struct acpi_walk_state
*walk_state
,
187 union acpi_parse_object
*unnamed_op
,
188 union acpi_parse_object
**op
)
190 acpi_status status
= AE_OK
;
191 union acpi_parse_object
*arg
= NULL
;
193 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op
, walk_state
);
195 unnamed_op
->common
.value
.arg
= NULL
;
196 unnamed_op
->common
.arg_list_length
= 0;
197 unnamed_op
->common
.aml_opcode
= walk_state
->opcode
;
200 * Get and append arguments until we find the node that contains
201 * the name (the type ARGP_NAME).
203 while (GET_CURRENT_ARG_TYPE(walk_state
->arg_types
) &&
204 (GET_CURRENT_ARG_TYPE(walk_state
->arg_types
) != ARGP_NAME
)) {
205 ASL_CV_CAPTURE_COMMENTS(walk_state
);
207 acpi_ps_get_next_arg(walk_state
,
208 &(walk_state
->parser_state
),
209 GET_CURRENT_ARG_TYPE(walk_state
->
211 if (ACPI_FAILURE(status
)) {
212 return_ACPI_STATUS(status
);
215 acpi_ps_append_arg(unnamed_op
, arg
);
216 INCREMENT_ARG_LIST(walk_state
->arg_types
);
219 /* are there any inline comments associated with the name_seg?? If so, save this. */
221 ASL_CV_CAPTURE_COMMENTS(walk_state
);
223 #ifdef ACPI_ASL_COMPILER
224 if (acpi_gbl_current_inline_comment
!= NULL
) {
225 unnamed_op
->common
.name_comment
=
226 acpi_gbl_current_inline_comment
;
227 acpi_gbl_current_inline_comment
= NULL
;
232 * Make sure that we found a NAME and didn't run out of arguments
234 if (!GET_CURRENT_ARG_TYPE(walk_state
->arg_types
)) {
235 return_ACPI_STATUS(AE_AML_NO_OPERAND
);
238 /* We know that this arg is a name, move to next arg */
240 INCREMENT_ARG_LIST(walk_state
->arg_types
);
243 * Find the object. This will either insert the object into
244 * the namespace or simply look it up
246 walk_state
->op
= NULL
;
248 status
= walk_state
->descending_callback(walk_state
, op
);
249 if (ACPI_FAILURE(status
)) {
250 if (status
!= AE_CTRL_TERMINATE
) {
251 ACPI_EXCEPTION((AE_INFO
, status
,
252 "During name lookup/catalog"));
254 return_ACPI_STATUS(status
);
258 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
261 status
= acpi_ps_next_parse_state(walk_state
, *op
, status
);
262 if (ACPI_FAILURE(status
)) {
263 if (status
== AE_CTRL_PENDING
) {
264 status
= AE_CTRL_PARSE_PENDING
;
266 return_ACPI_STATUS(status
);
269 acpi_ps_append_arg(*op
, unnamed_op
->common
.value
.arg
);
271 #ifdef ACPI_ASL_COMPILER
273 /* save any comments that might be associated with unnamed_op. */
275 (*op
)->common
.inline_comment
= unnamed_op
->common
.inline_comment
;
276 (*op
)->common
.end_node_comment
= unnamed_op
->common
.end_node_comment
;
277 (*op
)->common
.close_brace_comment
=
278 unnamed_op
->common
.close_brace_comment
;
279 (*op
)->common
.name_comment
= unnamed_op
->common
.name_comment
;
280 (*op
)->common
.comment_list
= unnamed_op
->common
.comment_list
;
281 (*op
)->common
.end_blk_comment
= unnamed_op
->common
.end_blk_comment
;
282 (*op
)->common
.cv_filename
= unnamed_op
->common
.cv_filename
;
283 (*op
)->common
.cv_parent_filename
=
284 unnamed_op
->common
.cv_parent_filename
;
285 (*op
)->named
.aml
= unnamed_op
->common
.aml
;
287 unnamed_op
->common
.inline_comment
= NULL
;
288 unnamed_op
->common
.end_node_comment
= NULL
;
289 unnamed_op
->common
.close_brace_comment
= NULL
;
290 unnamed_op
->common
.name_comment
= NULL
;
291 unnamed_op
->common
.comment_list
= NULL
;
292 unnamed_op
->common
.end_blk_comment
= NULL
;
295 if ((*op
)->common
.aml_opcode
== AML_REGION_OP
||
296 (*op
)->common
.aml_opcode
== AML_DATA_REGION_OP
) {
298 * Defer final parsing of an operation_region body, because we don't
299 * have enough info in the first pass to parse it correctly (i.e.,
300 * there may be method calls within the term_arg elements of the body.)
302 * However, we must continue parsing because the opregion is not a
303 * standalone package -- we don't know where the end is at this point.
305 * (Length is unknown until parse of the body complete)
307 (*op
)->named
.data
= aml_op_start
;
308 (*op
)->named
.length
= 0;
311 return_ACPI_STATUS(AE_OK
);
314 /*******************************************************************************
316 * FUNCTION: acpi_ps_create_op
318 * PARAMETERS: walk_state - Current state
319 * aml_op_start - Op start in AML
320 * new_op - Returned Op
324 * DESCRIPTION: Get Op from AML
326 ******************************************************************************/
329 acpi_ps_create_op(struct acpi_walk_state
*walk_state
,
330 u8
*aml_op_start
, union acpi_parse_object
**new_op
)
332 acpi_status status
= AE_OK
;
333 union acpi_parse_object
*op
;
334 union acpi_parse_object
*named_op
= NULL
;
335 union acpi_parse_object
*parent_scope
;
337 const struct acpi_opcode_info
*op_info
;
339 ACPI_FUNCTION_TRACE_PTR(ps_create_op
, walk_state
);
341 status
= acpi_ps_get_aml_opcode(walk_state
);
342 if (status
== AE_CTRL_PARSE_CONTINUE
) {
343 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
345 if (ACPI_FAILURE(status
)) {
346 return_ACPI_STATUS(status
);
349 /* Create Op structure and append to parent's argument list */
351 walk_state
->op_info
= acpi_ps_get_opcode_info(walk_state
->opcode
);
352 op
= acpi_ps_alloc_op(walk_state
->opcode
, aml_op_start
);
354 return_ACPI_STATUS(AE_NO_MEMORY
);
357 if (walk_state
->op_info
->flags
& AML_NAMED
) {
359 acpi_ps_build_named_op(walk_state
, aml_op_start
, op
,
363 #ifdef ACPI_ASL_COMPILER
364 if (acpi_gbl_disasm_flag
365 && walk_state
->opcode
== AML_EXTERNAL_OP
366 && status
== AE_NOT_FOUND
) {
368 * If parsing of AML_EXTERNAL_OP's name path fails, then skip
369 * past this opcode and keep parsing. This is a much better
370 * alternative than to abort the entire disassembler. At this
371 * point, the parser_state is at the end of the namepath of the
372 * external declaration opcode. Setting walk_state->Aml to
373 * walk_state->parser_state.Aml + 2 moves increments the
374 * walk_state->Aml past the object type and the paramcount of the
377 walk_state
->aml
= walk_state
->parser_state
.aml
+ 2;
378 walk_state
->parser_state
.aml
= walk_state
->aml
;
379 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
382 if (ACPI_FAILURE(status
)) {
383 return_ACPI_STATUS(status
);
387 return_ACPI_STATUS(AE_OK
);
390 /* Not a named opcode, just allocate Op and append to parent */
392 if (walk_state
->op_info
->flags
& AML_CREATE
) {
394 * Backup to beginning of create_XXXfield declaration
395 * body_length is unknown until we parse the body
397 op
->named
.data
= aml_op_start
;
398 op
->named
.length
= 0;
401 if (walk_state
->opcode
== AML_BANK_FIELD_OP
) {
403 * Backup to beginning of bank_field declaration
404 * body_length is unknown until we parse the body
406 op
->named
.data
= aml_op_start
;
407 op
->named
.length
= 0;
410 parent_scope
= acpi_ps_get_parent_scope(&(walk_state
->parser_state
));
411 acpi_ps_append_arg(parent_scope
, op
);
415 acpi_ps_get_opcode_info(parent_scope
->common
.aml_opcode
);
416 if (op_info
->flags
& AML_HAS_TARGET
) {
418 acpi_ps_get_argument_count(op_info
->type
);
419 if (parent_scope
->common
.arg_list_length
>
421 op
->common
.flags
|= ACPI_PARSEOP_TARGET
;
426 * Special case for both Increment() and Decrement(), where
427 * the lone argument is both a source and a target.
429 else if ((parent_scope
->common
.aml_opcode
== AML_INCREMENT_OP
)
430 || (parent_scope
->common
.aml_opcode
==
432 op
->common
.flags
|= ACPI_PARSEOP_TARGET
;
436 if (walk_state
->descending_callback
!= NULL
) {
438 * Find the object. This will either insert the object into
439 * the namespace or simply look it up
441 walk_state
->op
= *new_op
= op
;
443 status
= walk_state
->descending_callback(walk_state
, &op
);
444 status
= acpi_ps_next_parse_state(walk_state
, op
, status
);
445 if (status
== AE_CTRL_PENDING
) {
446 status
= AE_CTRL_PARSE_PENDING
;
450 return_ACPI_STATUS(status
);
453 /*******************************************************************************
455 * FUNCTION: acpi_ps_complete_op
457 * PARAMETERS: walk_state - Current state
459 * status - Parse status before complete Op
463 * DESCRIPTION: Complete Op
465 ******************************************************************************/
468 acpi_ps_complete_op(struct acpi_walk_state
*walk_state
,
469 union acpi_parse_object
**op
, acpi_status status
)
473 ACPI_FUNCTION_TRACE_PTR(ps_complete_op
, walk_state
);
476 * Finished one argument of the containing scope
478 walk_state
->parser_state
.scope
->parse_scope
.arg_count
--;
480 /* Close this Op (will result in parse subtree deletion) */
482 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
483 if (ACPI_FAILURE(status2
)) {
484 return_ACPI_STATUS(status2
);
494 case AE_CTRL_TRANSFER
:
496 /* We are about to transfer to a called method */
498 walk_state
->prev_op
= NULL
;
499 walk_state
->prev_arg_types
= walk_state
->arg_types
;
500 return_ACPI_STATUS(status
);
504 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
505 &walk_state
->arg_types
,
506 &walk_state
->arg_count
);
509 walk_state
->op
= *op
;
510 walk_state
->op_info
=
511 acpi_ps_get_opcode_info((*op
)->common
.aml_opcode
);
512 walk_state
->opcode
= (*op
)->common
.aml_opcode
;
514 status
= walk_state
->ascending_callback(walk_state
);
516 acpi_ps_next_parse_state(walk_state
, *op
, status
);
518 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
519 if (ACPI_FAILURE(status2
)) {
520 return_ACPI_STATUS(status2
);
528 case AE_CTRL_CONTINUE
:
530 /* Pop off scopes until we find the While */
532 while (!(*op
) || ((*op
)->common
.aml_opcode
!= AML_WHILE_OP
)) {
533 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
534 &walk_state
->arg_types
,
535 &walk_state
->arg_count
);
538 /* Close this iteration of the While loop */
540 walk_state
->op
= *op
;
541 walk_state
->op_info
=
542 acpi_ps_get_opcode_info((*op
)->common
.aml_opcode
);
543 walk_state
->opcode
= (*op
)->common
.aml_opcode
;
545 status
= walk_state
->ascending_callback(walk_state
);
546 status
= acpi_ps_next_parse_state(walk_state
, *op
, status
);
548 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
549 if (ACPI_FAILURE(status2
)) {
550 return_ACPI_STATUS(status2
);
556 case AE_CTRL_TERMINATE
:
562 acpi_ps_complete_this_op(walk_state
, *op
);
563 if (ACPI_FAILURE(status2
)) {
564 return_ACPI_STATUS(status2
);
567 acpi_ut_delete_generic_state
568 (acpi_ut_pop_generic_state
569 (&walk_state
->control_state
));
572 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
573 &walk_state
->arg_types
,
574 &walk_state
->arg_count
);
578 return_ACPI_STATUS(AE_OK
);
580 default: /* All other non-AE_OK status */
585 acpi_ps_complete_this_op(walk_state
, *op
);
586 if (ACPI_FAILURE(status2
)) {
587 return_ACPI_STATUS(status2
);
591 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
592 &walk_state
->arg_types
,
593 &walk_state
->arg_count
);
599 * TBD: Cleanup parse ops on error
602 acpi_ps_pop_scope(parser_state
, op
,
603 &walk_state
->arg_types
,
604 &walk_state
->arg_count
);
607 walk_state
->prev_op
= NULL
;
608 walk_state
->prev_arg_types
= walk_state
->arg_types
;
609 return_ACPI_STATUS(status
);
612 /* This scope complete? */
614 if (acpi_ps_has_completed_scope(&(walk_state
->parser_state
))) {
615 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
616 &walk_state
->arg_types
,
617 &walk_state
->arg_count
);
618 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "Popped scope, Op=%p\n", *op
));
623 return_ACPI_STATUS(AE_OK
);
626 /*******************************************************************************
628 * FUNCTION: acpi_ps_complete_final_op
630 * PARAMETERS: walk_state - Current state
632 * status - Current parse status before complete last
637 * DESCRIPTION: Complete last Op.
639 ******************************************************************************/
642 acpi_ps_complete_final_op(struct acpi_walk_state
*walk_state
,
643 union acpi_parse_object
*op
, acpi_status status
)
647 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op
, walk_state
);
650 * Complete the last Op (if not completed), and clear the scope stack.
651 * It is easily possible to end an AML "package" with an unbounded number
652 * of open scopes (such as when several ASL blocks are closed with
653 * sequential closing braces). We want to terminate each one cleanly.
655 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "AML package complete at Op %p\n",
659 if (walk_state
->ascending_callback
!= NULL
) {
661 walk_state
->op_info
=
662 acpi_ps_get_opcode_info(op
->common
.
664 walk_state
->opcode
= op
->common
.aml_opcode
;
667 walk_state
->ascending_callback(walk_state
);
669 acpi_ps_next_parse_state(walk_state
, op
,
671 if (status
== AE_CTRL_PENDING
) {
673 acpi_ps_complete_op(walk_state
, &op
,
675 if (ACPI_FAILURE(status
)) {
676 return_ACPI_STATUS(status
);
680 if (status
== AE_CTRL_TERMINATE
) {
687 acpi_ps_complete_this_op
707 return_ACPI_STATUS(status
);
710 else if (ACPI_FAILURE(status
)) {
712 /* First error is most important */
715 acpi_ps_complete_this_op(walk_state
,
717 return_ACPI_STATUS(status
);
721 status2
= acpi_ps_complete_this_op(walk_state
, op
);
722 if (ACPI_FAILURE(status2
)) {
723 return_ACPI_STATUS(status2
);
727 acpi_ps_pop_scope(&(walk_state
->parser_state
), &op
,
728 &walk_state
->arg_types
,
729 &walk_state
->arg_count
);
733 return_ACPI_STATUS(status
);