Loading...
1// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2/******************************************************************************
3 *
4 * Module Name: psobject - Support for parse objects
5 *
6 * Copyright (C) 2000 - 2020, Intel Corp.
7 *
8 *****************************************************************************/
9
10#include <acpi/acpi.h>
11#include "accommon.h"
12#include "acparser.h"
13#include "amlcode.h"
14#include "acconvert.h"
15#include "acnamesp.h"
16
17#define _COMPONENT ACPI_PARSER
18ACPI_MODULE_NAME("psobject")
19
20/* Local prototypes */
21static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
22
23/*******************************************************************************
24 *
25 * FUNCTION: acpi_ps_get_aml_opcode
26 *
27 * PARAMETERS: walk_state - Current state
28 *
29 * RETURN: Status
30 *
31 * DESCRIPTION: Extract the next AML opcode from the input stream.
32 *
33 ******************************************************************************/
34
35static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
36{
37 ACPI_ERROR_ONLY(u32 aml_offset);
38
39 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
40
41 walk_state->aml = walk_state->parser_state.aml;
42 walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
43
44 /*
45 * First cut to determine what we have found:
46 * 1) A valid AML opcode
47 * 2) A name string
48 * 3) An unknown/invalid opcode
49 */
50 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
51
52 switch (walk_state->op_info->class) {
53 case AML_CLASS_ASCII:
54 case AML_CLASS_PREFIX:
55 /*
56 * Starts with a valid prefix or ASCII char, this is a name
57 * string. Convert the bare name string to a namepath.
58 */
59 walk_state->opcode = AML_INT_NAMEPATH_OP;
60 walk_state->arg_types = ARGP_NAMESTRING;
61 break;
62
63 case AML_CLASS_UNKNOWN:
64
65 /* The opcode is unrecognized. Complain and skip unknown opcodes */
66
67 if (walk_state->pass_number == 2) {
68 ACPI_ERROR_ONLY(aml_offset =
69 (u32)ACPI_PTR_DIFF(walk_state->aml,
70 walk_state->
71 parser_state.
72 aml_start));
73
74 ACPI_ERROR((AE_INFO,
75 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
76 walk_state->opcode,
77 (u32)(aml_offset +
78 sizeof(struct acpi_table_header))));
79
80 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
81 48);
82
83#ifdef ACPI_ASL_COMPILER
84 /*
85 * This is executed for the disassembler only. Output goes
86 * to the disassembled ASL output file.
87 */
88 acpi_os_printf
89 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
90 walk_state->opcode,
91 (u32)(aml_offset +
92 sizeof(struct acpi_table_header)));
93
94 ACPI_ERROR((AE_INFO,
95 "Aborting disassembly, AML byte code is corrupt"));
96
97 /* Dump the context surrounding the invalid opcode */
98
99 acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
100 aml - 16), 48, DB_BYTE_DISPLAY,
101 (aml_offset +
102 sizeof(struct acpi_table_header) -
103 16));
104 acpi_os_printf(" */\n");
105
106 /*
107 * Just abort the disassembly, cannot continue because the
108 * parser is essentially lost. The disassembler can then
109 * randomly fail because an ill-constructed parse tree
110 * can result.
111 */
112 return_ACPI_STATUS(AE_AML_BAD_OPCODE);
113#endif
114 }
115
116 /* Increment past one-byte or two-byte opcode */
117
118 walk_state->parser_state.aml++;
119 if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
120 walk_state->parser_state.aml++;
121 }
122
123 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
124
125 default:
126
127 /* Found opcode info, this is a normal opcode */
128
129 walk_state->parser_state.aml +=
130 acpi_ps_get_opcode_size(walk_state->opcode);
131 walk_state->arg_types = walk_state->op_info->parse_args;
132 break;
133 }
134
135 return_ACPI_STATUS(AE_OK);
136}
137
138/*******************************************************************************
139 *
140 * FUNCTION: acpi_ps_build_named_op
141 *
142 * PARAMETERS: walk_state - Current state
143 * aml_op_start - Begin of named Op in AML
144 * unnamed_op - Early Op (not a named Op)
145 * op - Returned Op
146 *
147 * RETURN: Status
148 *
149 * DESCRIPTION: Parse a named Op
150 *
151 ******************************************************************************/
152
153acpi_status
154acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
155 u8 *aml_op_start,
156 union acpi_parse_object *unnamed_op,
157 union acpi_parse_object **op)
158{
159 acpi_status status = AE_OK;
160 union acpi_parse_object *arg = NULL;
161
162 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
163
164 unnamed_op->common.value.arg = NULL;
165 unnamed_op->common.arg_list_length = 0;
166 unnamed_op->common.aml_opcode = walk_state->opcode;
167
168 /*
169 * Get and append arguments until we find the node that contains
170 * the name (the type ARGP_NAME).
171 */
172 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
173 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
174 ASL_CV_CAPTURE_COMMENTS(walk_state);
175 status =
176 acpi_ps_get_next_arg(walk_state,
177 &(walk_state->parser_state),
178 GET_CURRENT_ARG_TYPE(walk_state->
179 arg_types), &arg);
180 if (ACPI_FAILURE(status)) {
181 return_ACPI_STATUS(status);
182 }
183
184 acpi_ps_append_arg(unnamed_op, arg);
185 INCREMENT_ARG_LIST(walk_state->arg_types);
186 }
187
188 /* are there any inline comments associated with the name_seg?? If so, save this. */
189
190 ASL_CV_CAPTURE_COMMENTS(walk_state);
191
192#ifdef ACPI_ASL_COMPILER
193 if (acpi_gbl_current_inline_comment != NULL) {
194 unnamed_op->common.name_comment =
195 acpi_gbl_current_inline_comment;
196 acpi_gbl_current_inline_comment = NULL;
197 }
198#endif
199
200 /*
201 * Make sure that we found a NAME and didn't run out of arguments
202 */
203 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
204 return_ACPI_STATUS(AE_AML_NO_OPERAND);
205 }
206
207 /* We know that this arg is a name, move to next arg */
208
209 INCREMENT_ARG_LIST(walk_state->arg_types);
210
211 /*
212 * Find the object. This will either insert the object into
213 * the namespace or simply look it up
214 */
215 walk_state->op = NULL;
216
217 status = walk_state->descending_callback(walk_state, op);
218 if (ACPI_FAILURE(status)) {
219 if (status != AE_CTRL_TERMINATE) {
220 ACPI_EXCEPTION((AE_INFO, status,
221 "During name lookup/catalog"));
222 }
223 return_ACPI_STATUS(status);
224 }
225
226 if (!*op) {
227 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
228 }
229
230 status = acpi_ps_next_parse_state(walk_state, *op, status);
231 if (ACPI_FAILURE(status)) {
232 if (status == AE_CTRL_PENDING) {
233 status = AE_CTRL_PARSE_PENDING;
234 }
235 return_ACPI_STATUS(status);
236 }
237
238 acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
239
240#ifdef ACPI_ASL_COMPILER
241
242 /* save any comments that might be associated with unnamed_op. */
243
244 (*op)->common.inline_comment = unnamed_op->common.inline_comment;
245 (*op)->common.end_node_comment = unnamed_op->common.end_node_comment;
246 (*op)->common.close_brace_comment =
247 unnamed_op->common.close_brace_comment;
248 (*op)->common.name_comment = unnamed_op->common.name_comment;
249 (*op)->common.comment_list = unnamed_op->common.comment_list;
250 (*op)->common.end_blk_comment = unnamed_op->common.end_blk_comment;
251 (*op)->common.cv_filename = unnamed_op->common.cv_filename;
252 (*op)->common.cv_parent_filename =
253 unnamed_op->common.cv_parent_filename;
254 (*op)->named.aml = unnamed_op->common.aml;
255
256 unnamed_op->common.inline_comment = NULL;
257 unnamed_op->common.end_node_comment = NULL;
258 unnamed_op->common.close_brace_comment = NULL;
259 unnamed_op->common.name_comment = NULL;
260 unnamed_op->common.comment_list = NULL;
261 unnamed_op->common.end_blk_comment = NULL;
262#endif
263
264 if ((*op)->common.aml_opcode == AML_REGION_OP ||
265 (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
266 /*
267 * Defer final parsing of an operation_region body, because we don't
268 * have enough info in the first pass to parse it correctly (i.e.,
269 * there may be method calls within the term_arg elements of the body.)
270 *
271 * However, we must continue parsing because the opregion is not a
272 * standalone package -- we don't know where the end is at this point.
273 *
274 * (Length is unknown until parse of the body complete)
275 */
276 (*op)->named.data = aml_op_start;
277 (*op)->named.length = 0;
278 }
279
280 return_ACPI_STATUS(AE_OK);
281}
282
283/*******************************************************************************
284 *
285 * FUNCTION: acpi_ps_create_op
286 *
287 * PARAMETERS: walk_state - Current state
288 * aml_op_start - Op start in AML
289 * new_op - Returned Op
290 *
291 * RETURN: Status
292 *
293 * DESCRIPTION: Get Op from AML
294 *
295 ******************************************************************************/
296
297acpi_status
298acpi_ps_create_op(struct acpi_walk_state *walk_state,
299 u8 *aml_op_start, union acpi_parse_object **new_op)
300{
301 acpi_status status = AE_OK;
302 union acpi_parse_object *op;
303 union acpi_parse_object *named_op = NULL;
304 union acpi_parse_object *parent_scope;
305 u8 argument_count;
306 const struct acpi_opcode_info *op_info;
307
308 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
309
310 status = acpi_ps_get_aml_opcode(walk_state);
311 if (status == AE_CTRL_PARSE_CONTINUE) {
312 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
313 }
314 if (ACPI_FAILURE(status)) {
315 return_ACPI_STATUS(status);
316 }
317
318 /* Create Op structure and append to parent's argument list */
319
320 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
321 op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start);
322 if (!op) {
323 return_ACPI_STATUS(AE_NO_MEMORY);
324 }
325
326 if (walk_state->op_info->flags & AML_NAMED) {
327 status =
328 acpi_ps_build_named_op(walk_state, aml_op_start, op,
329 &named_op);
330 acpi_ps_free_op(op);
331
332#ifdef ACPI_ASL_COMPILER
333 if (acpi_gbl_disasm_flag
334 && walk_state->opcode == AML_EXTERNAL_OP
335 && status == AE_NOT_FOUND) {
336 /*
337 * If parsing of AML_EXTERNAL_OP's name path fails, then skip
338 * past this opcode and keep parsing. This is a much better
339 * alternative than to abort the entire disassembler. At this
340 * point, the parser_state is at the end of the namepath of the
341 * external declaration opcode. Setting walk_state->Aml to
342 * walk_state->parser_state.Aml + 2 moves increments the
343 * walk_state->Aml past the object type and the paramcount of the
344 * external opcode.
345 */
346 walk_state->aml = walk_state->parser_state.aml + 2;
347 walk_state->parser_state.aml = walk_state->aml;
348 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
349 }
350#endif
351 if (ACPI_FAILURE(status)) {
352 return_ACPI_STATUS(status);
353 }
354
355 *new_op = named_op;
356 return_ACPI_STATUS(AE_OK);
357 }
358
359 /* Not a named opcode, just allocate Op and append to parent */
360
361 if (walk_state->op_info->flags & AML_CREATE) {
362 /*
363 * Backup to beginning of create_XXXfield declaration
364 * body_length is unknown until we parse the body
365 */
366 op->named.data = aml_op_start;
367 op->named.length = 0;
368 }
369
370 if (walk_state->opcode == AML_BANK_FIELD_OP) {
371 /*
372 * Backup to beginning of bank_field declaration
373 * body_length is unknown until we parse the body
374 */
375 op->named.data = aml_op_start;
376 op->named.length = 0;
377 }
378
379 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
380 acpi_ps_append_arg(parent_scope, op);
381
382 if (parent_scope) {
383 op_info =
384 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
385 if (op_info->flags & AML_HAS_TARGET) {
386 argument_count =
387 acpi_ps_get_argument_count(op_info->type);
388 if (parent_scope->common.arg_list_length >
389 argument_count) {
390 op->common.flags |= ACPI_PARSEOP_TARGET;
391 }
392 }
393
394 /*
395 * Special case for both Increment() and Decrement(), where
396 * the lone argument is both a source and a target.
397 */
398 else if ((parent_scope->common.aml_opcode == AML_INCREMENT_OP)
399 || (parent_scope->common.aml_opcode ==
400 AML_DECREMENT_OP)) {
401 op->common.flags |= ACPI_PARSEOP_TARGET;
402 }
403 }
404
405 if (walk_state->descending_callback != NULL) {
406 /*
407 * Find the object. This will either insert the object into
408 * the namespace or simply look it up
409 */
410 walk_state->op = *new_op = op;
411
412 status = walk_state->descending_callback(walk_state, &op);
413 status = acpi_ps_next_parse_state(walk_state, op, status);
414 if (status == AE_CTRL_PENDING) {
415 status = AE_CTRL_PARSE_PENDING;
416 }
417 }
418
419 return_ACPI_STATUS(status);
420}
421
422/*******************************************************************************
423 *
424 * FUNCTION: acpi_ps_complete_op
425 *
426 * PARAMETERS: walk_state - Current state
427 * op - Returned Op
428 * status - Parse status before complete Op
429 *
430 * RETURN: Status
431 *
432 * DESCRIPTION: Complete Op
433 *
434 ******************************************************************************/
435
436acpi_status
437acpi_ps_complete_op(struct acpi_walk_state *walk_state,
438 union acpi_parse_object **op, acpi_status status)
439{
440 acpi_status status2;
441
442 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
443
444 /*
445 * Finished one argument of the containing scope
446 */
447 walk_state->parser_state.scope->parse_scope.arg_count--;
448
449 /* Close this Op (will result in parse subtree deletion) */
450
451 status2 = acpi_ps_complete_this_op(walk_state, *op);
452 if (ACPI_FAILURE(status2)) {
453 return_ACPI_STATUS(status2);
454 }
455
456 *op = NULL;
457
458 switch (status) {
459 case AE_OK:
460
461 break;
462
463 case AE_CTRL_TRANSFER:
464
465 /* We are about to transfer to a called method */
466
467 walk_state->prev_op = NULL;
468 walk_state->prev_arg_types = walk_state->arg_types;
469 return_ACPI_STATUS(status);
470
471 case AE_CTRL_END:
472
473 acpi_ps_pop_scope(&(walk_state->parser_state), op,
474 &walk_state->arg_types,
475 &walk_state->arg_count);
476
477 if (*op) {
478 walk_state->op = *op;
479 walk_state->op_info =
480 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
481 walk_state->opcode = (*op)->common.aml_opcode;
482
483 status = walk_state->ascending_callback(walk_state);
484 (void)acpi_ps_next_parse_state(walk_state, *op, status);
485
486 status2 = acpi_ps_complete_this_op(walk_state, *op);
487 if (ACPI_FAILURE(status2)) {
488 return_ACPI_STATUS(status2);
489 }
490 }
491
492 break;
493
494 case AE_CTRL_BREAK:
495 case AE_CTRL_CONTINUE:
496
497 /* Pop off scopes until we find the While */
498
499 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
500 acpi_ps_pop_scope(&(walk_state->parser_state), op,
501 &walk_state->arg_types,
502 &walk_state->arg_count);
503 }
504
505 /* Close this iteration of the While loop */
506
507 walk_state->op = *op;
508 walk_state->op_info =
509 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
510 walk_state->opcode = (*op)->common.aml_opcode;
511
512 status = walk_state->ascending_callback(walk_state);
513 (void)acpi_ps_next_parse_state(walk_state, *op, status);
514
515 status2 = acpi_ps_complete_this_op(walk_state, *op);
516 if (ACPI_FAILURE(status2)) {
517 return_ACPI_STATUS(status2);
518 }
519
520 break;
521
522 case AE_CTRL_TERMINATE:
523
524 /* Clean up */
525 do {
526 if (*op) {
527 status2 =
528 acpi_ps_complete_this_op(walk_state, *op);
529 if (ACPI_FAILURE(status2)) {
530 return_ACPI_STATUS(status2);
531 }
532
533 acpi_ut_delete_generic_state
534 (acpi_ut_pop_generic_state
535 (&walk_state->control_state));
536 }
537
538 acpi_ps_pop_scope(&(walk_state->parser_state), op,
539 &walk_state->arg_types,
540 &walk_state->arg_count);
541
542 } while (*op);
543
544 return_ACPI_STATUS(AE_OK);
545
546 default: /* All other non-AE_OK status */
547
548 do {
549 if (*op) {
550 /*
551 * These Opcodes need to be removed from the namespace because they
552 * get created even if these opcodes cannot be created due to
553 * errors.
554 */
555 if (((*op)->common.aml_opcode == AML_REGION_OP)
556 || ((*op)->common.aml_opcode ==
557 AML_DATA_REGION_OP)) {
558 acpi_ns_delete_children((*op)->common.
559 node);
560 acpi_ns_remove_node((*op)->common.node);
561 (*op)->common.node = NULL;
562 acpi_ps_delete_parse_tree(*op);
563 }
564
565 status2 =
566 acpi_ps_complete_this_op(walk_state, *op);
567 if (ACPI_FAILURE(status2)) {
568 return_ACPI_STATUS(status2);
569 }
570 }
571
572 acpi_ps_pop_scope(&(walk_state->parser_state), op,
573 &walk_state->arg_types,
574 &walk_state->arg_count);
575
576 } while (*op);
577
578#if 0
579 /*
580 * TBD: Cleanup parse ops on error
581 */
582 if (*op == NULL) {
583 acpi_ps_pop_scope(parser_state, op,
584 &walk_state->arg_types,
585 &walk_state->arg_count);
586 }
587#endif
588 walk_state->prev_op = NULL;
589 walk_state->prev_arg_types = walk_state->arg_types;
590
591 if (walk_state->parse_flags & ACPI_PARSE_MODULE_LEVEL) {
592 /*
593 * There was something that went wrong while executing code at the
594 * module-level. We need to skip parsing whatever caused the
595 * error and keep going. One runtime error during the table load
596 * should not cause the entire table to not be loaded. This is
597 * because there could be correct AML beyond the parts that caused
598 * the runtime error.
599 */
600 ACPI_INFO(("Ignoring error and continuing table load"));
601 return_ACPI_STATUS(AE_OK);
602 }
603 return_ACPI_STATUS(status);
604 }
605
606 /* This scope complete? */
607
608 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
609 acpi_ps_pop_scope(&(walk_state->parser_state), op,
610 &walk_state->arg_types,
611 &walk_state->arg_count);
612 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
613 } else {
614 *op = NULL;
615 }
616
617 return_ACPI_STATUS(AE_OK);
618}
619
620/*******************************************************************************
621 *
622 * FUNCTION: acpi_ps_complete_final_op
623 *
624 * PARAMETERS: walk_state - Current state
625 * op - Current Op
626 * status - Current parse status before complete last
627 * Op
628 *
629 * RETURN: Status
630 *
631 * DESCRIPTION: Complete last Op.
632 *
633 ******************************************************************************/
634
635acpi_status
636acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
637 union acpi_parse_object *op, acpi_status status)
638{
639 acpi_status status2;
640
641 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
642
643 /*
644 * Complete the last Op (if not completed), and clear the scope stack.
645 * It is easily possible to end an AML "package" with an unbounded number
646 * of open scopes (such as when several ASL blocks are closed with
647 * sequential closing braces). We want to terminate each one cleanly.
648 */
649 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
650 op));
651 do {
652 if (op) {
653 if (walk_state->ascending_callback != NULL) {
654 walk_state->op = op;
655 walk_state->op_info =
656 acpi_ps_get_opcode_info(op->common.
657 aml_opcode);
658 walk_state->opcode = op->common.aml_opcode;
659
660 status =
661 walk_state->ascending_callback(walk_state);
662 status =
663 acpi_ps_next_parse_state(walk_state, op,
664 status);
665 if (status == AE_CTRL_PENDING) {
666 status =
667 acpi_ps_complete_op(walk_state, &op,
668 AE_OK);
669 if (ACPI_FAILURE(status)) {
670 return_ACPI_STATUS(status);
671 }
672 }
673
674 if (status == AE_CTRL_TERMINATE) {
675 status = AE_OK;
676
677 /* Clean up */
678 do {
679 if (op) {
680 status2 =
681 acpi_ps_complete_this_op
682 (walk_state, op);
683 if (ACPI_FAILURE
684 (status2)) {
685 return_ACPI_STATUS
686 (status2);
687 }
688 }
689
690 acpi_ps_pop_scope(&
691 (walk_state->
692 parser_state),
693 &op,
694 &walk_state->
695 arg_types,
696 &walk_state->
697 arg_count);
698
699 } while (op);
700
701 return_ACPI_STATUS(status);
702 }
703
704 else if (ACPI_FAILURE(status)) {
705
706 /* First error is most important */
707
708 (void)
709 acpi_ps_complete_this_op(walk_state,
710 op);
711 return_ACPI_STATUS(status);
712 }
713 }
714
715 status2 = acpi_ps_complete_this_op(walk_state, op);
716 if (ACPI_FAILURE(status2)) {
717 return_ACPI_STATUS(status2);
718 }
719 }
720
721 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
722 &walk_state->arg_types,
723 &walk_state->arg_count);
724
725 } while (op);
726
727 return_ACPI_STATUS(status);
728}
1/******************************************************************************
2 *
3 * Module Name: psobject - Support for parse objects
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44#include <acpi/acpi.h>
45#include "accommon.h"
46#include "acparser.h"
47#include "amlcode.h"
48
49#define _COMPONENT ACPI_PARSER
50ACPI_MODULE_NAME("psobject")
51
52/* Local prototypes */
53static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
54
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_ps_get_aml_opcode
58 *
59 * PARAMETERS: walk_state - Current state
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Extract the next AML opcode from the input stream.
64 *
65 ******************************************************************************/
66
67static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
68{
69 u32 aml_offset;
70
71 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
72
73 walk_state->aml = walk_state->parser_state.aml;
74 walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
75
76 /*
77 * First cut to determine what we have found:
78 * 1) A valid AML opcode
79 * 2) A name string
80 * 3) An unknown/invalid opcode
81 */
82 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
83
84 switch (walk_state->op_info->class) {
85 case AML_CLASS_ASCII:
86 case AML_CLASS_PREFIX:
87 /*
88 * Starts with a valid prefix or ASCII char, this is a name
89 * string. Convert the bare name string to a namepath.
90 */
91 walk_state->opcode = AML_INT_NAMEPATH_OP;
92 walk_state->arg_types = ARGP_NAMESTRING;
93 break;
94
95 case AML_CLASS_UNKNOWN:
96
97 /* The opcode is unrecognized. Complain and skip unknown opcodes */
98
99 if (walk_state->pass_number == 2) {
100 aml_offset = (u32)ACPI_PTR_DIFF(walk_state->aml,
101 walk_state->
102 parser_state.aml_start);
103
104 ACPI_ERROR((AE_INFO,
105 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
106 walk_state->opcode,
107 (u32)(aml_offset +
108 sizeof(struct acpi_table_header))));
109
110 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
111 48);
112
113#ifdef ACPI_ASL_COMPILER
114 /*
115 * This is executed for the disassembler only. Output goes
116 * to the disassembled ASL output file.
117 */
118 acpi_os_printf
119 ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
120 walk_state->opcode,
121 (u32)(aml_offset +
122 sizeof(struct acpi_table_header)));
123
124 /* Dump the context surrounding the invalid opcode */
125
126 acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
127 aml - 16), 48, DB_BYTE_DISPLAY,
128 (aml_offset +
129 sizeof(struct acpi_table_header) -
130 16));
131 acpi_os_printf(" */\n");
132#endif
133 }
134
135 /* Increment past one-byte or two-byte opcode */
136
137 walk_state->parser_state.aml++;
138 if (walk_state->opcode > 0xFF) { /* Can only happen if first byte is 0x5B */
139 walk_state->parser_state.aml++;
140 }
141
142 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
143
144 default:
145
146 /* Found opcode info, this is a normal opcode */
147
148 walk_state->parser_state.aml +=
149 acpi_ps_get_opcode_size(walk_state->opcode);
150 walk_state->arg_types = walk_state->op_info->parse_args;
151 break;
152 }
153
154 return_ACPI_STATUS(AE_OK);
155}
156
157/*******************************************************************************
158 *
159 * FUNCTION: acpi_ps_build_named_op
160 *
161 * PARAMETERS: walk_state - Current state
162 * aml_op_start - Begin of named Op in AML
163 * unnamed_op - Early Op (not a named Op)
164 * op - Returned Op
165 *
166 * RETURN: Status
167 *
168 * DESCRIPTION: Parse a named Op
169 *
170 ******************************************************************************/
171
172acpi_status
173acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
174 u8 *aml_op_start,
175 union acpi_parse_object *unnamed_op,
176 union acpi_parse_object **op)
177{
178 acpi_status status = AE_OK;
179 union acpi_parse_object *arg = NULL;
180
181 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
182
183 unnamed_op->common.value.arg = NULL;
184 unnamed_op->common.arg_list_length = 0;
185 unnamed_op->common.aml_opcode = walk_state->opcode;
186
187 /*
188 * Get and append arguments until we find the node that contains
189 * the name (the type ARGP_NAME).
190 */
191 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
192 (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
193 status =
194 acpi_ps_get_next_arg(walk_state,
195 &(walk_state->parser_state),
196 GET_CURRENT_ARG_TYPE(walk_state->
197 arg_types), &arg);
198 if (ACPI_FAILURE(status)) {
199 return_ACPI_STATUS(status);
200 }
201
202 acpi_ps_append_arg(unnamed_op, arg);
203 INCREMENT_ARG_LIST(walk_state->arg_types);
204 }
205
206 /*
207 * Make sure that we found a NAME and didn't run out of arguments
208 */
209 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
210 return_ACPI_STATUS(AE_AML_NO_OPERAND);
211 }
212
213 /* We know that this arg is a name, move to next arg */
214
215 INCREMENT_ARG_LIST(walk_state->arg_types);
216
217 /*
218 * Find the object. This will either insert the object into
219 * the namespace or simply look it up
220 */
221 walk_state->op = NULL;
222
223 status = walk_state->descending_callback(walk_state, op);
224 if (ACPI_FAILURE(status)) {
225 if (status != AE_CTRL_TERMINATE) {
226 ACPI_EXCEPTION((AE_INFO, status,
227 "During name lookup/catalog"));
228 }
229 return_ACPI_STATUS(status);
230 }
231
232 if (!*op) {
233 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
234 }
235
236 status = acpi_ps_next_parse_state(walk_state, *op, status);
237 if (ACPI_FAILURE(status)) {
238 if (status == AE_CTRL_PENDING) {
239 status = AE_CTRL_PARSE_PENDING;
240 }
241 return_ACPI_STATUS(status);
242 }
243
244 acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
245
246 if ((*op)->common.aml_opcode == AML_REGION_OP ||
247 (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
248 /*
249 * Defer final parsing of an operation_region body, because we don't
250 * have enough info in the first pass to parse it correctly (i.e.,
251 * there may be method calls within the term_arg elements of the body.)
252 *
253 * However, we must continue parsing because the opregion is not a
254 * standalone package -- we don't know where the end is at this point.
255 *
256 * (Length is unknown until parse of the body complete)
257 */
258 (*op)->named.data = aml_op_start;
259 (*op)->named.length = 0;
260 }
261
262 return_ACPI_STATUS(AE_OK);
263}
264
265/*******************************************************************************
266 *
267 * FUNCTION: acpi_ps_create_op
268 *
269 * PARAMETERS: walk_state - Current state
270 * aml_op_start - Op start in AML
271 * new_op - Returned Op
272 *
273 * RETURN: Status
274 *
275 * DESCRIPTION: Get Op from AML
276 *
277 ******************************************************************************/
278
279acpi_status
280acpi_ps_create_op(struct acpi_walk_state *walk_state,
281 u8 *aml_op_start, union acpi_parse_object **new_op)
282{
283 acpi_status status = AE_OK;
284 union acpi_parse_object *op;
285 union acpi_parse_object *named_op = NULL;
286 union acpi_parse_object *parent_scope;
287 u8 argument_count;
288 const struct acpi_opcode_info *op_info;
289
290 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
291
292 status = acpi_ps_get_aml_opcode(walk_state);
293 if (status == AE_CTRL_PARSE_CONTINUE) {
294 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
295 }
296
297 /* Create Op structure and append to parent's argument list */
298
299 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
300 op = acpi_ps_alloc_op(walk_state->opcode, aml_op_start);
301 if (!op) {
302 return_ACPI_STATUS(AE_NO_MEMORY);
303 }
304
305 if (walk_state->op_info->flags & AML_NAMED) {
306 status =
307 acpi_ps_build_named_op(walk_state, aml_op_start, op,
308 &named_op);
309 acpi_ps_free_op(op);
310 if (ACPI_FAILURE(status)) {
311 return_ACPI_STATUS(status);
312 }
313
314 *new_op = named_op;
315 return_ACPI_STATUS(AE_OK);
316 }
317
318 /* Not a named opcode, just allocate Op and append to parent */
319
320 if (walk_state->op_info->flags & AML_CREATE) {
321 /*
322 * Backup to beginning of create_XXXfield declaration
323 * body_length is unknown until we parse the body
324 */
325 op->named.data = aml_op_start;
326 op->named.length = 0;
327 }
328
329 if (walk_state->opcode == AML_BANK_FIELD_OP) {
330 /*
331 * Backup to beginning of bank_field declaration
332 * body_length is unknown until we parse the body
333 */
334 op->named.data = aml_op_start;
335 op->named.length = 0;
336 }
337
338 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
339 acpi_ps_append_arg(parent_scope, op);
340
341 if (parent_scope) {
342 op_info =
343 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
344 if (op_info->flags & AML_HAS_TARGET) {
345 argument_count =
346 acpi_ps_get_argument_count(op_info->type);
347 if (parent_scope->common.arg_list_length >
348 argument_count) {
349 op->common.flags |= ACPI_PARSEOP_TARGET;
350 }
351 } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
352 op->common.flags |= ACPI_PARSEOP_TARGET;
353 }
354 }
355
356 if (walk_state->descending_callback != NULL) {
357 /*
358 * Find the object. This will either insert the object into
359 * the namespace or simply look it up
360 */
361 walk_state->op = *new_op = op;
362
363 status = walk_state->descending_callback(walk_state, &op);
364 status = acpi_ps_next_parse_state(walk_state, op, status);
365 if (status == AE_CTRL_PENDING) {
366 status = AE_CTRL_PARSE_PENDING;
367 }
368 }
369
370 return_ACPI_STATUS(status);
371}
372
373/*******************************************************************************
374 *
375 * FUNCTION: acpi_ps_complete_op
376 *
377 * PARAMETERS: walk_state - Current state
378 * op - Returned Op
379 * status - Parse status before complete Op
380 *
381 * RETURN: Status
382 *
383 * DESCRIPTION: Complete Op
384 *
385 ******************************************************************************/
386
387acpi_status
388acpi_ps_complete_op(struct acpi_walk_state *walk_state,
389 union acpi_parse_object **op, acpi_status status)
390{
391 acpi_status status2;
392
393 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
394
395 /*
396 * Finished one argument of the containing scope
397 */
398 walk_state->parser_state.scope->parse_scope.arg_count--;
399
400 /* Close this Op (will result in parse subtree deletion) */
401
402 status2 = acpi_ps_complete_this_op(walk_state, *op);
403 if (ACPI_FAILURE(status2)) {
404 return_ACPI_STATUS(status2);
405 }
406
407 *op = NULL;
408
409 switch (status) {
410 case AE_OK:
411
412 break;
413
414 case AE_CTRL_TRANSFER:
415
416 /* We are about to transfer to a called method */
417
418 walk_state->prev_op = NULL;
419 walk_state->prev_arg_types = walk_state->arg_types;
420 return_ACPI_STATUS(status);
421
422 case AE_CTRL_END:
423
424 acpi_ps_pop_scope(&(walk_state->parser_state), op,
425 &walk_state->arg_types,
426 &walk_state->arg_count);
427
428 if (*op) {
429 walk_state->op = *op;
430 walk_state->op_info =
431 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
432 walk_state->opcode = (*op)->common.aml_opcode;
433
434 status = walk_state->ascending_callback(walk_state);
435 status =
436 acpi_ps_next_parse_state(walk_state, *op, status);
437
438 status2 = acpi_ps_complete_this_op(walk_state, *op);
439 if (ACPI_FAILURE(status2)) {
440 return_ACPI_STATUS(status2);
441 }
442 }
443
444 status = AE_OK;
445 break;
446
447 case AE_CTRL_BREAK:
448 case AE_CTRL_CONTINUE:
449
450 /* Pop off scopes until we find the While */
451
452 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
453 acpi_ps_pop_scope(&(walk_state->parser_state), op,
454 &walk_state->arg_types,
455 &walk_state->arg_count);
456 }
457
458 /* Close this iteration of the While loop */
459
460 walk_state->op = *op;
461 walk_state->op_info =
462 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
463 walk_state->opcode = (*op)->common.aml_opcode;
464
465 status = walk_state->ascending_callback(walk_state);
466 status = acpi_ps_next_parse_state(walk_state, *op, status);
467
468 status2 = acpi_ps_complete_this_op(walk_state, *op);
469 if (ACPI_FAILURE(status2)) {
470 return_ACPI_STATUS(status2);
471 }
472
473 status = AE_OK;
474 break;
475
476 case AE_CTRL_TERMINATE:
477
478 /* Clean up */
479 do {
480 if (*op) {
481 status2 =
482 acpi_ps_complete_this_op(walk_state, *op);
483 if (ACPI_FAILURE(status2)) {
484 return_ACPI_STATUS(status2);
485 }
486
487 acpi_ut_delete_generic_state
488 (acpi_ut_pop_generic_state
489 (&walk_state->control_state));
490 }
491
492 acpi_ps_pop_scope(&(walk_state->parser_state), op,
493 &walk_state->arg_types,
494 &walk_state->arg_count);
495
496 } while (*op);
497
498 return_ACPI_STATUS(AE_OK);
499
500 default: /* All other non-AE_OK status */
501
502 do {
503 if (*op) {
504 status2 =
505 acpi_ps_complete_this_op(walk_state, *op);
506 if (ACPI_FAILURE(status2)) {
507 return_ACPI_STATUS(status2);
508 }
509 }
510
511 acpi_ps_pop_scope(&(walk_state->parser_state), op,
512 &walk_state->arg_types,
513 &walk_state->arg_count);
514
515 } while (*op);
516
517#if 0
518 /*
519 * TBD: Cleanup parse ops on error
520 */
521 if (*op == NULL) {
522 acpi_ps_pop_scope(parser_state, op,
523 &walk_state->arg_types,
524 &walk_state->arg_count);
525 }
526#endif
527 walk_state->prev_op = NULL;
528 walk_state->prev_arg_types = walk_state->arg_types;
529 return_ACPI_STATUS(status);
530 }
531
532 /* This scope complete? */
533
534 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
535 acpi_ps_pop_scope(&(walk_state->parser_state), op,
536 &walk_state->arg_types,
537 &walk_state->arg_count);
538 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
539 } else {
540 *op = NULL;
541 }
542
543 return_ACPI_STATUS(AE_OK);
544}
545
546/*******************************************************************************
547 *
548 * FUNCTION: acpi_ps_complete_final_op
549 *
550 * PARAMETERS: walk_state - Current state
551 * op - Current Op
552 * status - Current parse status before complete last
553 * Op
554 *
555 * RETURN: Status
556 *
557 * DESCRIPTION: Complete last Op.
558 *
559 ******************************************************************************/
560
561acpi_status
562acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
563 union acpi_parse_object *op, acpi_status status)
564{
565 acpi_status status2;
566
567 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
568
569 /*
570 * Complete the last Op (if not completed), and clear the scope stack.
571 * It is easily possible to end an AML "package" with an unbounded number
572 * of open scopes (such as when several ASL blocks are closed with
573 * sequential closing braces). We want to terminate each one cleanly.
574 */
575 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
576 op));
577 do {
578 if (op) {
579 if (walk_state->ascending_callback != NULL) {
580 walk_state->op = op;
581 walk_state->op_info =
582 acpi_ps_get_opcode_info(op->common.
583 aml_opcode);
584 walk_state->opcode = op->common.aml_opcode;
585
586 status =
587 walk_state->ascending_callback(walk_state);
588 status =
589 acpi_ps_next_parse_state(walk_state, op,
590 status);
591 if (status == AE_CTRL_PENDING) {
592 status =
593 acpi_ps_complete_op(walk_state, &op,
594 AE_OK);
595 if (ACPI_FAILURE(status)) {
596 return_ACPI_STATUS(status);
597 }
598 }
599
600 if (status == AE_CTRL_TERMINATE) {
601 status = AE_OK;
602
603 /* Clean up */
604 do {
605 if (op) {
606 status2 =
607 acpi_ps_complete_this_op
608 (walk_state, op);
609 if (ACPI_FAILURE
610 (status2)) {
611 return_ACPI_STATUS
612 (status2);
613 }
614 }
615
616 acpi_ps_pop_scope(&
617 (walk_state->
618 parser_state),
619 &op,
620 &walk_state->
621 arg_types,
622 &walk_state->
623 arg_count);
624
625 } while (op);
626
627 return_ACPI_STATUS(status);
628 }
629
630 else if (ACPI_FAILURE(status)) {
631
632 /* First error is most important */
633
634 (void)
635 acpi_ps_complete_this_op(walk_state,
636 op);
637 return_ACPI_STATUS(status);
638 }
639 }
640
641 status2 = acpi_ps_complete_this_op(walk_state, op);
642 if (ACPI_FAILURE(status2)) {
643 return_ACPI_STATUS(status2);
644 }
645 }
646
647 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
648 &walk_state->arg_types,
649 &walk_state->arg_count);
650
651 } while (op);
652
653 return_ACPI_STATUS(status);
654}