1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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>
49 #define _COMPONENT ACPI_PARSER
50 ACPI_MODULE_NAME("psobject")
52 /* Local prototypes */
53 static acpi_status
acpi_ps_get_aml_opcode(struct acpi_walk_state
*walk_state
);
55 /*******************************************************************************
57 * FUNCTION: acpi_ps_get_aml_opcode
59 * PARAMETERS: walk_state - Current state
63 * DESCRIPTION: Extract the next AML opcode from the input stream.
65 ******************************************************************************/
67 static acpi_status
acpi_ps_get_aml_opcode(struct acpi_walk_state
*walk_state
)
70 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode
, walk_state
);
72 walk_state
->aml_offset
=
73 (u32
)ACPI_PTR_DIFF(walk_state
->parser_state
.aml
,
74 walk_state
->parser_state
.aml_start
);
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) {
102 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
104 (u32
)(walk_state
->aml_offset
+
105 sizeof(struct acpi_table_header
))));
107 ACPI_DUMP_BUFFER((walk_state
->parser_state
.aml
- 16),
110 #ifdef ACPI_ASL_COMPILER
112 * This is executed for the disassembler only. Output goes
113 * to the disassembled ASL output file.
116 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
118 (u32
)(walk_state
->aml_offset
+
119 sizeof(struct acpi_table_header
)));
121 /* Dump the context surrounding the invalid opcode */
123 acpi_ut_dump_buffer(((u8
*)walk_state
->parser_state
.
124 aml
- 16), 48, DB_BYTE_DISPLAY
,
125 (walk_state
->aml_offset
+
126 sizeof(struct acpi_table_header
) -
128 acpi_os_printf(" */\n");
132 /* Increment past one-byte or two-byte opcode */
134 walk_state
->parser_state
.aml
++;
135 if (walk_state
->opcode
> 0xFF) { /* Can only happen if first byte is 0x5B */
136 walk_state
->parser_state
.aml
++;
139 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
143 /* Found opcode info, this is a normal opcode */
145 walk_state
->parser_state
.aml
+=
146 acpi_ps_get_opcode_size(walk_state
->opcode
);
147 walk_state
->arg_types
= walk_state
->op_info
->parse_args
;
151 return_ACPI_STATUS(AE_OK
);
154 /*******************************************************************************
156 * FUNCTION: acpi_ps_build_named_op
158 * PARAMETERS: walk_state - Current state
159 * aml_op_start - Begin of named Op in AML
160 * unnamed_op - Early Op (not a named Op)
165 * DESCRIPTION: Parse a named Op
167 ******************************************************************************/
170 acpi_ps_build_named_op(struct acpi_walk_state
*walk_state
,
172 union acpi_parse_object
*unnamed_op
,
173 union acpi_parse_object
**op
)
175 acpi_status status
= AE_OK
;
176 union acpi_parse_object
*arg
= NULL
;
178 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op
, walk_state
);
180 unnamed_op
->common
.value
.arg
= NULL
;
181 unnamed_op
->common
.arg_list_length
= 0;
182 unnamed_op
->common
.aml_opcode
= walk_state
->opcode
;
185 * Get and append arguments until we find the node that contains
186 * the name (the type ARGP_NAME).
188 while (GET_CURRENT_ARG_TYPE(walk_state
->arg_types
) &&
189 (GET_CURRENT_ARG_TYPE(walk_state
->arg_types
) != ARGP_NAME
)) {
191 acpi_ps_get_next_arg(walk_state
,
192 &(walk_state
->parser_state
),
193 GET_CURRENT_ARG_TYPE(walk_state
->
195 if (ACPI_FAILURE(status
)) {
196 return_ACPI_STATUS(status
);
199 acpi_ps_append_arg(unnamed_op
, arg
);
200 INCREMENT_ARG_LIST(walk_state
->arg_types
);
204 * Make sure that we found a NAME and didn't run out of arguments
206 if (!GET_CURRENT_ARG_TYPE(walk_state
->arg_types
)) {
207 return_ACPI_STATUS(AE_AML_NO_OPERAND
);
210 /* We know that this arg is a name, move to next arg */
212 INCREMENT_ARG_LIST(walk_state
->arg_types
);
215 * Find the object. This will either insert the object into
216 * the namespace or simply look it up
218 walk_state
->op
= NULL
;
220 status
= walk_state
->descending_callback(walk_state
, op
);
221 if (ACPI_FAILURE(status
)) {
222 ACPI_EXCEPTION((AE_INFO
, status
, "During name lookup/catalog"));
223 return_ACPI_STATUS(status
);
227 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
230 status
= acpi_ps_next_parse_state(walk_state
, *op
, status
);
231 if (ACPI_FAILURE(status
)) {
232 if (status
== AE_CTRL_PENDING
) {
233 return_ACPI_STATUS(AE_CTRL_PARSE_PENDING
);
235 return_ACPI_STATUS(status
);
238 acpi_ps_append_arg(*op
, unnamed_op
->common
.value
.arg
);
240 if ((*op
)->common
.aml_opcode
== AML_REGION_OP
||
241 (*op
)->common
.aml_opcode
== AML_DATA_REGION_OP
) {
243 * Defer final parsing of an operation_region body, because we don't
244 * have enough info in the first pass to parse it correctly (i.e.,
245 * there may be method calls within the term_arg elements of the body.)
247 * However, we must continue parsing because the opregion is not a
248 * standalone package -- we don't know where the end is at this point.
250 * (Length is unknown until parse of the body complete)
252 (*op
)->named
.data
= aml_op_start
;
253 (*op
)->named
.length
= 0;
256 return_ACPI_STATUS(AE_OK
);
259 /*******************************************************************************
261 * FUNCTION: acpi_ps_create_op
263 * PARAMETERS: walk_state - Current state
264 * aml_op_start - Op start in AML
265 * new_op - Returned Op
269 * DESCRIPTION: Get Op from AML
271 ******************************************************************************/
274 acpi_ps_create_op(struct acpi_walk_state
*walk_state
,
275 u8
*aml_op_start
, union acpi_parse_object
**new_op
)
277 acpi_status status
= AE_OK
;
278 union acpi_parse_object
*op
;
279 union acpi_parse_object
*named_op
= NULL
;
280 union acpi_parse_object
*parent_scope
;
282 const struct acpi_opcode_info
*op_info
;
284 ACPI_FUNCTION_TRACE_PTR(ps_create_op
, walk_state
);
286 status
= acpi_ps_get_aml_opcode(walk_state
);
287 if (status
== AE_CTRL_PARSE_CONTINUE
) {
288 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE
);
291 /* Create Op structure and append to parent's argument list */
293 walk_state
->op_info
= acpi_ps_get_opcode_info(walk_state
->opcode
);
294 op
= acpi_ps_alloc_op(walk_state
->opcode
);
296 return_ACPI_STATUS(AE_NO_MEMORY
);
299 if (walk_state
->op_info
->flags
& AML_NAMED
) {
301 acpi_ps_build_named_op(walk_state
, aml_op_start
, op
,
304 if (ACPI_FAILURE(status
)) {
305 return_ACPI_STATUS(status
);
309 return_ACPI_STATUS(AE_OK
);
312 /* Not a named opcode, just allocate Op and append to parent */
314 if (walk_state
->op_info
->flags
& AML_CREATE
) {
316 * Backup to beginning of create_XXXfield declaration
317 * body_length is unknown until we parse the body
319 op
->named
.data
= aml_op_start
;
320 op
->named
.length
= 0;
323 if (walk_state
->opcode
== AML_BANK_FIELD_OP
) {
325 * Backup to beginning of bank_field declaration
326 * body_length is unknown until we parse the body
328 op
->named
.data
= aml_op_start
;
329 op
->named
.length
= 0;
332 parent_scope
= acpi_ps_get_parent_scope(&(walk_state
->parser_state
));
333 acpi_ps_append_arg(parent_scope
, op
);
337 acpi_ps_get_opcode_info(parent_scope
->common
.aml_opcode
);
338 if (op_info
->flags
& AML_HAS_TARGET
) {
340 acpi_ps_get_argument_count(op_info
->type
);
341 if (parent_scope
->common
.arg_list_length
>
343 op
->common
.flags
|= ACPI_PARSEOP_TARGET
;
345 } else if (parent_scope
->common
.aml_opcode
== AML_INCREMENT_OP
) {
346 op
->common
.flags
|= ACPI_PARSEOP_TARGET
;
350 if (walk_state
->descending_callback
!= NULL
) {
352 * Find the object. This will either insert the object into
353 * the namespace or simply look it up
355 walk_state
->op
= *new_op
= op
;
357 status
= walk_state
->descending_callback(walk_state
, &op
);
358 status
= acpi_ps_next_parse_state(walk_state
, op
, status
);
359 if (status
== AE_CTRL_PENDING
) {
360 status
= AE_CTRL_PARSE_PENDING
;
364 return_ACPI_STATUS(status
);
367 /*******************************************************************************
369 * FUNCTION: acpi_ps_complete_op
371 * PARAMETERS: walk_state - Current state
373 * status - Parse status before complete Op
377 * DESCRIPTION: Complete Op
379 ******************************************************************************/
382 acpi_ps_complete_op(struct acpi_walk_state
*walk_state
,
383 union acpi_parse_object
**op
, acpi_status status
)
387 ACPI_FUNCTION_TRACE_PTR(ps_complete_op
, walk_state
);
390 * Finished one argument of the containing scope
392 walk_state
->parser_state
.scope
->parse_scope
.arg_count
--;
394 /* Close this Op (will result in parse subtree deletion) */
396 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
397 if (ACPI_FAILURE(status2
)) {
398 return_ACPI_STATUS(status2
);
408 case AE_CTRL_TRANSFER
:
410 /* We are about to transfer to a called method */
412 walk_state
->prev_op
= NULL
;
413 walk_state
->prev_arg_types
= walk_state
->arg_types
;
414 return_ACPI_STATUS(status
);
418 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
419 &walk_state
->arg_types
,
420 &walk_state
->arg_count
);
423 walk_state
->op
= *op
;
424 walk_state
->op_info
=
425 acpi_ps_get_opcode_info((*op
)->common
.aml_opcode
);
426 walk_state
->opcode
= (*op
)->common
.aml_opcode
;
428 status
= walk_state
->ascending_callback(walk_state
);
430 acpi_ps_next_parse_state(walk_state
, *op
, status
);
432 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
433 if (ACPI_FAILURE(status2
)) {
434 return_ACPI_STATUS(status2
);
442 case AE_CTRL_CONTINUE
:
444 /* Pop off scopes until we find the While */
446 while (!(*op
) || ((*op
)->common
.aml_opcode
!= AML_WHILE_OP
)) {
447 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
448 &walk_state
->arg_types
,
449 &walk_state
->arg_count
);
452 /* Close this iteration of the While loop */
454 walk_state
->op
= *op
;
455 walk_state
->op_info
=
456 acpi_ps_get_opcode_info((*op
)->common
.aml_opcode
);
457 walk_state
->opcode
= (*op
)->common
.aml_opcode
;
459 status
= walk_state
->ascending_callback(walk_state
);
460 status
= acpi_ps_next_parse_state(walk_state
, *op
, status
);
462 status2
= acpi_ps_complete_this_op(walk_state
, *op
);
463 if (ACPI_FAILURE(status2
)) {
464 return_ACPI_STATUS(status2
);
470 case AE_CTRL_TERMINATE
:
476 acpi_ps_complete_this_op(walk_state
, *op
);
477 if (ACPI_FAILURE(status2
)) {
478 return_ACPI_STATUS(status2
);
481 acpi_ut_delete_generic_state
482 (acpi_ut_pop_generic_state
483 (&walk_state
->control_state
));
486 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
487 &walk_state
->arg_types
,
488 &walk_state
->arg_count
);
492 return_ACPI_STATUS(AE_OK
);
494 default: /* All other non-AE_OK status */
499 acpi_ps_complete_this_op(walk_state
, *op
);
500 if (ACPI_FAILURE(status2
)) {
501 return_ACPI_STATUS(status2
);
505 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
506 &walk_state
->arg_types
,
507 &walk_state
->arg_count
);
513 * TBD: Cleanup parse ops on error
516 acpi_ps_pop_scope(parser_state
, op
,
517 &walk_state
->arg_types
,
518 &walk_state
->arg_count
);
521 walk_state
->prev_op
= NULL
;
522 walk_state
->prev_arg_types
= walk_state
->arg_types
;
523 return_ACPI_STATUS(status
);
526 /* This scope complete? */
528 if (acpi_ps_has_completed_scope(&(walk_state
->parser_state
))) {
529 acpi_ps_pop_scope(&(walk_state
->parser_state
), op
,
530 &walk_state
->arg_types
,
531 &walk_state
->arg_count
);
532 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "Popped scope, Op=%p\n", *op
));
537 return_ACPI_STATUS(AE_OK
);
540 /*******************************************************************************
542 * FUNCTION: acpi_ps_complete_final_op
544 * PARAMETERS: walk_state - Current state
546 * status - Current parse status before complete last
551 * DESCRIPTION: Complete last Op.
553 ******************************************************************************/
556 acpi_ps_complete_final_op(struct acpi_walk_state
*walk_state
,
557 union acpi_parse_object
*op
, acpi_status status
)
561 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op
, walk_state
);
564 * Complete the last Op (if not completed), and clear the scope stack.
565 * It is easily possible to end an AML "package" with an unbounded number
566 * of open scopes (such as when several ASL blocks are closed with
567 * sequential closing braces). We want to terminate each one cleanly.
569 ACPI_DEBUG_PRINT((ACPI_DB_PARSE
, "AML package complete at Op %p\n",
573 if (walk_state
->ascending_callback
!= NULL
) {
575 walk_state
->op_info
=
576 acpi_ps_get_opcode_info(op
->common
.
578 walk_state
->opcode
= op
->common
.aml_opcode
;
581 walk_state
->ascending_callback(walk_state
);
583 acpi_ps_next_parse_state(walk_state
, op
,
585 if (status
== AE_CTRL_PENDING
) {
587 acpi_ps_complete_op(walk_state
, &op
,
589 if (ACPI_FAILURE(status
)) {
590 return_ACPI_STATUS(status
);
594 if (status
== AE_CTRL_TERMINATE
) {
601 acpi_ps_complete_this_op
621 return_ACPI_STATUS(status
);
624 else if (ACPI_FAILURE(status
)) {
626 /* First error is most important */
629 acpi_ps_complete_this_op(walk_state
,
631 return_ACPI_STATUS(status
);
635 status2
= acpi_ps_complete_this_op(walk_state
, op
);
636 if (ACPI_FAILURE(status2
)) {
637 return_ACPI_STATUS(status2
);
641 acpi_ps_pop_scope(&(walk_state
->parser_state
), &op
,
642 &walk_state
->arg_types
,
643 &walk_state
->arg_count
);
647 return_ACPI_STATUS(status
);