1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3 *
4 * Module Name: psloop - Main AML parse loop
5 *
6 * Copyright (C) 2000 - 2018, Intel Corp.
7 *
8 *****************************************************************************/
9
10 /*
11 * Parse the AML and build an operation tree as most interpreters, (such as
12 * Perl) do. Parsing is done by hand rather than with a YACC generated parser
13 * to tightly constrain stack and dynamic memory usage. Parsing is kept
14 * flexible and the code fairly compact by parsing based on a list of AML
15 * opcode templates in aml_op_info[].
16 */
17
18 #include <acpi/acpi.h>
19 #include "accommon.h"
20 #include "acinterp.h"
21 #include "acparser.h"
22 #include "acdispat.h"
23 #include "amlcode.h"
24 #include "acconvert.h"
25 #include "acnamesp.h"
26
27 #define _COMPONENT ACPI_PARSER
28 ACPI_MODULE_NAME("psloop")
29
30 /* Local prototypes */
31 static acpi_status
32 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
33 u8 * aml_op_start, union acpi_parse_object *op);
34
35 static void
36 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
37 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
38
39 /*******************************************************************************
40 *
41 * FUNCTION: acpi_ps_get_arguments
42 *
43 * PARAMETERS: walk_state - Current state
44 * aml_op_start - Op start in AML
45 * op - Current Op
46 *
47 * RETURN: Status
48 *
49 * DESCRIPTION: Get arguments for passed Op.
50 *
51 ******************************************************************************/
52
53 static acpi_status
acpi_ps_get_arguments(struct acpi_walk_state * walk_state,u8 * aml_op_start,union acpi_parse_object * op)54 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
55 u8 * aml_op_start, union acpi_parse_object *op)
56 {
57 acpi_status status = AE_OK;
58 union acpi_parse_object *arg = NULL;
59 const struct acpi_opcode_info *op_info;
60
61 ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
62
63 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
64 "Get arguments for opcode [%s]\n",
65 op->common.aml_op_name));
66
67 switch (op->common.aml_opcode) {
68 case AML_BYTE_OP: /* AML_BYTEDATA_ARG */
69 case AML_WORD_OP: /* AML_WORDDATA_ARG */
70 case AML_DWORD_OP: /* AML_DWORDATA_ARG */
71 case AML_QWORD_OP: /* AML_QWORDATA_ARG */
72 case AML_STRING_OP: /* AML_ASCIICHARLIST_ARG */
73
74 /* Fill in constant or string argument directly */
75
76 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
77 GET_CURRENT_ARG_TYPE(walk_state->
78 arg_types),
79 op);
80 break;
81
82 case AML_INT_NAMEPATH_OP: /* AML_NAMESTRING_ARG */
83
84 status = acpi_ps_get_next_namepath(walk_state,
85 &(walk_state->parser_state),
86 op,
87 ACPI_POSSIBLE_METHOD_CALL);
88 if (ACPI_FAILURE(status)) {
89 return_ACPI_STATUS(status);
90 }
91
92 walk_state->arg_types = 0;
93 break;
94
95 default:
96 /*
97 * Op is not a constant or string, append each argument to the Op
98 */
99 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
100 !walk_state->arg_count) {
101 walk_state->aml = walk_state->parser_state.aml;
102
103 switch (op->common.aml_opcode) {
104 case AML_METHOD_OP:
105 case AML_BUFFER_OP:
106 case AML_PACKAGE_OP:
107 case AML_VARIABLE_PACKAGE_OP:
108 case AML_WHILE_OP:
109
110 break;
111
112 default:
113
114 ASL_CV_CAPTURE_COMMENTS(walk_state);
115 break;
116 }
117
118 status =
119 acpi_ps_get_next_arg(walk_state,
120 &(walk_state->parser_state),
121 GET_CURRENT_ARG_TYPE
122 (walk_state->arg_types), &arg);
123 if (ACPI_FAILURE(status)) {
124 return_ACPI_STATUS(status);
125 }
126
127 if (arg) {
128 acpi_ps_append_arg(op, arg);
129 }
130
131 INCREMENT_ARG_LIST(walk_state->arg_types);
132 }
133
134 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
135 "Final argument count: %8.8X pass %u\n",
136 walk_state->arg_count,
137 walk_state->pass_number));
138
139 /*
140 * This case handles the legacy option that groups all module-level
141 * code blocks together and defers execution until all of the tables
142 * are loaded. Execute all of these blocks at this time.
143 * Execute any module-level code that was detected during the table
144 * load phase.
145 *
146 * Note: this option is deprecated and will be eliminated in the
147 * future. Use of this option can cause problems with AML code that
148 * depends upon in-order immediate execution of module-level code.
149 */
150 if (acpi_gbl_group_module_level_code &&
151 (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
152 ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
153 /*
154 * We want to skip If/Else/While constructs during Pass1 because we
155 * want to actually conditionally execute the code during Pass2.
156 *
157 * Except for disassembly, where we always want to walk the
158 * If/Else/While packages
159 */
160 switch (op->common.aml_opcode) {
161 case AML_IF_OP:
162 case AML_ELSE_OP:
163 case AML_WHILE_OP:
164 /*
165 * Currently supported module-level opcodes are:
166 * IF/ELSE/WHILE. These appear to be the most common,
167 * and easiest to support since they open an AML
168 * package.
169 */
170 if (walk_state->pass_number ==
171 ACPI_IMODE_LOAD_PASS1) {
172 acpi_ps_link_module_code(op->common.
173 parent,
174 aml_op_start,
175 (u32)
176 (walk_state->
177 parser_state.
178 pkg_end -
179 aml_op_start),
180 walk_state->
181 owner_id);
182 }
183
184 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
185 "Pass1: Skipping an If/Else/While body\n"));
186
187 /* Skip body of if/else/while in pass 1 */
188
189 walk_state->parser_state.aml =
190 walk_state->parser_state.pkg_end;
191 walk_state->arg_count = 0;
192 break;
193
194 default:
195 /*
196 * Check for an unsupported executable opcode at module
197 * level. We must be in PASS1, the parent must be a SCOPE,
198 * The opcode class must be EXECUTE, and the opcode must
199 * not be an argument to another opcode.
200 */
201 if ((walk_state->pass_number ==
202 ACPI_IMODE_LOAD_PASS1)
203 && (op->common.parent->common.aml_opcode ==
204 AML_SCOPE_OP)) {
205 op_info =
206 acpi_ps_get_opcode_info(op->common.
207 aml_opcode);
208 if ((op_info->class ==
209 AML_CLASS_EXECUTE) && (!arg)) {
210 ACPI_WARNING((AE_INFO,
211 "Unsupported module-level executable opcode "
212 "0x%.2X at table offset 0x%.4X",
213 op->common.
214 aml_opcode,
215 (u32)
216 (ACPI_PTR_DIFF
217 (aml_op_start,
218 walk_state->
219 parser_state.
220 aml_start) +
221 sizeof(struct
222 acpi_table_header))));
223 }
224 }
225 break;
226 }
227 }
228
229 /* Special processing for certain opcodes */
230
231 switch (op->common.aml_opcode) {
232 case AML_METHOD_OP:
233 /*
234 * Skip parsing of control method because we don't have enough
235 * info in the first pass to parse it correctly.
236 *
237 * Save the length and address of the body
238 */
239 op->named.data = walk_state->parser_state.aml;
240 op->named.length = (u32)
241 (walk_state->parser_state.pkg_end -
242 walk_state->parser_state.aml);
243
244 /* Skip body of method */
245
246 walk_state->parser_state.aml =
247 walk_state->parser_state.pkg_end;
248 walk_state->arg_count = 0;
249 break;
250
251 case AML_BUFFER_OP:
252 case AML_PACKAGE_OP:
253 case AML_VARIABLE_PACKAGE_OP:
254
255 if ((op->common.parent) &&
256 (op->common.parent->common.aml_opcode ==
257 AML_NAME_OP)
258 && (walk_state->pass_number <=
259 ACPI_IMODE_LOAD_PASS2)) {
260 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
261 "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
262 walk_state->pass_number,
263 aml_op_start));
264
265 /*
266 * Skip parsing of Buffers and Packages because we don't have
267 * enough info in the first pass to parse them correctly.
268 */
269 op->named.data = aml_op_start;
270 op->named.length = (u32)
271 (walk_state->parser_state.pkg_end -
272 aml_op_start);
273
274 /* Skip body */
275
276 walk_state->parser_state.aml =
277 walk_state->parser_state.pkg_end;
278 walk_state->arg_count = 0;
279 }
280 break;
281
282 case AML_WHILE_OP:
283
284 if (walk_state->control_state) {
285 walk_state->control_state->control.package_end =
286 walk_state->parser_state.pkg_end;
287 }
288 break;
289
290 default:
291
292 /* No action for all other opcodes */
293
294 break;
295 }
296
297 break;
298 }
299
300 return_ACPI_STATUS(AE_OK);
301 }
302
303 /*******************************************************************************
304 *
305 * FUNCTION: acpi_ps_link_module_code
306 *
307 * PARAMETERS: parent_op - Parent parser op
308 * aml_start - Pointer to the AML
309 * aml_length - Length of executable AML
310 * owner_id - owner_id of module level code
311 *
312 * RETURN: None.
313 *
314 * DESCRIPTION: Wrap the module-level code with a method object and link the
315 * object to the global list. Note, the mutex field of the method
316 * object is used to link multiple module-level code objects.
317 *
318 * NOTE: In this legacy option, each block of detected executable AML
319 * code that is outside of any control method is wrapped with a temporary
320 * control method object and placed on a global list below.
321 *
322 * This function executes the module-level code for all tables only after
323 * all of the tables have been loaded. It is a legacy option and is
324 * not compatible with other ACPI implementations. See acpi_ns_load_table.
325 *
326 * This function will be removed when the legacy option is removed.
327 *
328 ******************************************************************************/
329
330 static void
acpi_ps_link_module_code(union acpi_parse_object * parent_op,u8 * aml_start,u32 aml_length,acpi_owner_id owner_id)331 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
332 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
333 {
334 union acpi_operand_object *prev;
335 union acpi_operand_object *next;
336 union acpi_operand_object *method_obj;
337 struct acpi_namespace_node *parent_node;
338
339 ACPI_FUNCTION_TRACE(ps_link_module_code);
340
341 /* Get the tail of the list */
342
343 prev = next = acpi_gbl_module_code_list;
344 while (next) {
345 prev = next;
346 next = next->method.mutex;
347 }
348
349 /*
350 * Insert the module level code into the list. Merge it if it is
351 * adjacent to the previous element.
352 */
353 if (!prev ||
354 ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
355
356 /* Create, initialize, and link a new temporary method object */
357
358 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
359 if (!method_obj) {
360 return_VOID;
361 }
362
363 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
364 "Create/Link new code block: %p\n",
365 method_obj));
366
367 if (parent_op->common.node) {
368 parent_node = parent_op->common.node;
369 } else {
370 parent_node = acpi_gbl_root_node;
371 }
372
373 method_obj->method.aml_start = aml_start;
374 method_obj->method.aml_length = aml_length;
375 method_obj->method.owner_id = owner_id;
376 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
377
378 /*
379 * Save the parent node in next_object. This is cheating, but we
380 * don't want to expand the method object.
381 */
382 method_obj->method.next_object =
383 ACPI_CAST_PTR(union acpi_operand_object, parent_node);
384
385 if (!prev) {
386 acpi_gbl_module_code_list = method_obj;
387 } else {
388 prev->method.mutex = method_obj;
389 }
390 } else {
391 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
392 "Appending to existing code block: %p\n",
393 prev));
394
395 prev->method.aml_length += aml_length;
396 }
397
398 return_VOID;
399 }
400
401 /*******************************************************************************
402 *
403 * FUNCTION: acpi_ps_parse_loop
404 *
405 * PARAMETERS: walk_state - Current state
406 *
407 * RETURN: Status
408 *
409 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
410 * a tree of ops.
411 *
412 ******************************************************************************/
413
acpi_ps_parse_loop(struct acpi_walk_state * walk_state)414 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
415 {
416 acpi_status status = AE_OK;
417 union acpi_parse_object *op = NULL; /* current op */
418 struct acpi_parse_state *parser_state;
419 u8 *aml_op_start = NULL;
420
421 ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
422
423 if (walk_state->descending_callback == NULL) {
424 return_ACPI_STATUS(AE_BAD_PARAMETER);
425 }
426
427 parser_state = &walk_state->parser_state;
428 walk_state->arg_types = 0;
429
430 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
431
432 if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
433
434 /* We are restarting a preempted control method */
435
436 if (acpi_ps_has_completed_scope(parser_state)) {
437 /*
438 * We must check if a predicate to an IF or WHILE statement
439 * was just completed
440 */
441 if ((parser_state->scope->parse_scope.op) &&
442 ((parser_state->scope->parse_scope.op->common.
443 aml_opcode == AML_IF_OP)
444 || (parser_state->scope->parse_scope.op->common.
445 aml_opcode == AML_WHILE_OP))
446 && (walk_state->control_state)
447 && (walk_state->control_state->common.state ==
448 ACPI_CONTROL_PREDICATE_EXECUTING)) {
449 /*
450 * A predicate was just completed, get the value of the
451 * predicate and branch based on that value
452 */
453 walk_state->op = NULL;
454 status =
455 acpi_ds_get_predicate_value(walk_state,
456 ACPI_TO_POINTER
457 (TRUE));
458 if (ACPI_FAILURE(status)
459 && ((status & AE_CODE_MASK) !=
460 AE_CODE_CONTROL)) {
461 if (status == AE_AML_NO_RETURN_VALUE) {
462 ACPI_EXCEPTION((AE_INFO, status,
463 "Invoked method did not return a value"));
464 }
465
466 ACPI_EXCEPTION((AE_INFO, status,
467 "GetPredicate Failed"));
468 return_ACPI_STATUS(status);
469 }
470
471 status =
472 acpi_ps_next_parse_state(walk_state, op,
473 status);
474 }
475
476 acpi_ps_pop_scope(parser_state, &op,
477 &walk_state->arg_types,
478 &walk_state->arg_count);
479 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
480 "Popped scope, Op=%p\n", op));
481 } else if (walk_state->prev_op) {
482
483 /* We were in the middle of an op */
484
485 op = walk_state->prev_op;
486 walk_state->arg_types = walk_state->prev_arg_types;
487 }
488 }
489 #endif
490
491 /* Iterative parsing loop, while there is more AML to process: */
492
493 while ((parser_state->aml < parser_state->aml_end) || (op)) {
494 ASL_CV_CAPTURE_COMMENTS(walk_state);
495
496 aml_op_start = parser_state->aml;
497 if (!op) {
498 status =
499 acpi_ps_create_op(walk_state, aml_op_start, &op);
500 if (ACPI_FAILURE(status)) {
501 /*
502 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
503 * executing it as a control method. However, if we encounter
504 * an error while loading the table, we need to keep trying to
505 * load the table rather than aborting the table load. Set the
506 * status to AE_OK to proceed with the table load.
507 */
508 if ((walk_state->
509 parse_flags & ACPI_PARSE_MODULE_LEVEL)
510 && status == AE_ALREADY_EXISTS) {
511 status = AE_OK;
512 }
513 if (status == AE_CTRL_PARSE_CONTINUE) {
514 continue;
515 }
516
517 if (status == AE_CTRL_PARSE_PENDING) {
518 status = AE_OK;
519 }
520
521 if (status == AE_CTRL_TERMINATE) {
522 return_ACPI_STATUS(status);
523 }
524
525 status =
526 acpi_ps_complete_op(walk_state, &op,
527 status);
528 if (ACPI_FAILURE(status)) {
529 return_ACPI_STATUS(status);
530 }
531 if (acpi_ns_opens_scope
532 (acpi_ps_get_opcode_info
533 (walk_state->opcode)->object_type)) {
534 /*
535 * If the scope/device op fails to parse, skip the body of
536 * the scope op because the parse failure indicates that
537 * the device may not exist.
538 */
539 ACPI_ERROR((AE_INFO,
540 "Skip parsing opcode %s",
541 acpi_ps_get_opcode_name
542 (walk_state->opcode)));
543 walk_state->parser_state.aml =
544 walk_state->aml + 1;
545 walk_state->parser_state.aml =
546 acpi_ps_get_next_package_end
547 (&walk_state->parser_state);
548 walk_state->aml =
549 walk_state->parser_state.aml;
550 }
551
552 continue;
553 }
554
555 acpi_ex_start_trace_opcode(op, walk_state);
556 }
557
558 /*
559 * Start arg_count at zero because we don't know if there are
560 * any args yet
561 */
562 walk_state->arg_count = 0;
563
564 switch (op->common.aml_opcode) {
565 case AML_BYTE_OP:
566 case AML_WORD_OP:
567 case AML_DWORD_OP:
568 case AML_QWORD_OP:
569
570 break;
571
572 default:
573
574 ASL_CV_CAPTURE_COMMENTS(walk_state);
575 break;
576 }
577
578 /* Are there any arguments that must be processed? */
579
580 if (walk_state->arg_types) {
581
582 /* Get arguments */
583
584 status =
585 acpi_ps_get_arguments(walk_state, aml_op_start, op);
586 if (ACPI_FAILURE(status)) {
587 status =
588 acpi_ps_complete_op(walk_state, &op,
589 status);
590 if (ACPI_FAILURE(status)) {
591 return_ACPI_STATUS(status);
592 }
593 if ((walk_state->control_state) &&
594 ((walk_state->control_state->control.
595 opcode == AML_IF_OP)
596 || (walk_state->control_state->control.
597 opcode == AML_WHILE_OP))) {
598 /*
599 * If the if/while op fails to parse, we will skip parsing
600 * the body of the op.
601 */
602 parser_state->aml =
603 walk_state->control_state->control.
604 aml_predicate_start + 1;
605 parser_state->aml =
606 acpi_ps_get_next_package_end
607 (parser_state);
608 walk_state->aml = parser_state->aml;
609
610 ACPI_ERROR((AE_INFO,
611 "Skipping While/If block"));
612 if (*walk_state->aml == AML_ELSE_OP) {
613 ACPI_ERROR((AE_INFO,
614 "Skipping Else block"));
615 walk_state->parser_state.aml =
616 walk_state->aml + 1;
617 walk_state->parser_state.aml =
618 acpi_ps_get_next_package_end
619 (parser_state);
620 walk_state->aml =
621 parser_state->aml;
622 }
623 ACPI_FREE(acpi_ut_pop_generic_state
624 (&walk_state->control_state));
625 }
626 op = NULL;
627 continue;
628 }
629 }
630
631 /* Check for arguments that need to be processed */
632
633 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
634 "Parseloop: argument count: %8.8X\n",
635 walk_state->arg_count));
636
637 if (walk_state->arg_count) {
638 /*
639 * There are arguments (complex ones), push Op and
640 * prepare for argument
641 */
642 status = acpi_ps_push_scope(parser_state, op,
643 walk_state->arg_types,
644 walk_state->arg_count);
645 if (ACPI_FAILURE(status)) {
646 status =
647 acpi_ps_complete_op(walk_state, &op,
648 status);
649 if (ACPI_FAILURE(status)) {
650 return_ACPI_STATUS(status);
651 }
652
653 continue;
654 }
655
656 op = NULL;
657 continue;
658 }
659
660 /*
661 * All arguments have been processed -- Op is complete,
662 * prepare for next
663 */
664 walk_state->op_info =
665 acpi_ps_get_opcode_info(op->common.aml_opcode);
666 if (walk_state->op_info->flags & AML_NAMED) {
667 if (op->common.aml_opcode == AML_REGION_OP ||
668 op->common.aml_opcode == AML_DATA_REGION_OP) {
669 /*
670 * Skip parsing of control method or opregion body,
671 * because we don't have enough info in the first pass
672 * to parse them correctly.
673 *
674 * Completed parsing an op_region declaration, we now
675 * know the length.
676 */
677 op->named.length =
678 (u32) (parser_state->aml - op->named.data);
679 }
680 }
681
682 if (walk_state->op_info->flags & AML_CREATE) {
683 /*
684 * Backup to beginning of create_XXXfield declaration (1 for
685 * Opcode)
686 *
687 * body_length is unknown until we parse the body
688 */
689 op->named.length =
690 (u32) (parser_state->aml - op->named.data);
691 }
692
693 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
694 /*
695 * Backup to beginning of bank_field declaration
696 *
697 * body_length is unknown until we parse the body
698 */
699 op->named.length =
700 (u32) (parser_state->aml - op->named.data);
701 }
702
703 /* This op complete, notify the dispatcher */
704
705 if (walk_state->ascending_callback != NULL) {
706 walk_state->op = op;
707 walk_state->opcode = op->common.aml_opcode;
708
709 status = walk_state->ascending_callback(walk_state);
710 status =
711 acpi_ps_next_parse_state(walk_state, op, status);
712 if (status == AE_CTRL_PENDING) {
713 status = AE_OK;
714 } else
715 if ((walk_state->
716 parse_flags & ACPI_PARSE_MODULE_LEVEL)
717 && (ACPI_AML_EXCEPTION(status)
718 || status == AE_ALREADY_EXISTS
719 || status == AE_NOT_FOUND)) {
720 /*
721 * ACPI_PARSE_MODULE_LEVEL flag means that we
722 * are currently loading a table by executing
723 * it as a control method. However, if we
724 * encounter an error while loading the table,
725 * we need to keep trying to load the table
726 * rather than aborting the table load (setting
727 * the status to AE_OK continues the table
728 * load). If we get a failure at this point, it
729 * means that the dispatcher got an error while
730 * trying to execute the Op.
731 */
732 status = AE_OK;
733 }
734 }
735
736 status = acpi_ps_complete_op(walk_state, &op, status);
737 if (ACPI_FAILURE(status)) {
738 return_ACPI_STATUS(status);
739 }
740
741 } /* while parser_state->Aml */
742
743 status = acpi_ps_complete_final_op(walk_state, op, status);
744 return_ACPI_STATUS(status);
745 }
746