xref: /linux/drivers/acpi/acpica/psloop.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 - 2026, 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 /*******************************************************************************
36  *
37  * FUNCTION:    acpi_ps_get_arguments
38  *
39  * PARAMETERS:  walk_state          - Current state
40  *              aml_op_start        - Op start in AML
41  *              op                  - Current Op
42  *
43  * RETURN:      Status
44  *
45  * DESCRIPTION: Get arguments for passed Op.
46  *
47  ******************************************************************************/
48 
49 static acpi_status
50 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
51 		      u8 * aml_op_start, union acpi_parse_object *op)
52 {
53 	acpi_status status = AE_OK;
54 	union acpi_parse_object *arg = NULL;
55 
56 	ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
57 
58 	ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
59 			  "Get arguments for opcode [%s]\n",
60 			  op->common.aml_op_name));
61 
62 	switch (op->common.aml_opcode) {
63 	case AML_BYTE_OP:	/* AML_BYTEDATA_ARG */
64 	case AML_WORD_OP:	/* AML_WORDDATA_ARG */
65 	case AML_DWORD_OP:	/* AML_DWORDATA_ARG */
66 	case AML_QWORD_OP:	/* AML_QWORDATA_ARG */
67 	case AML_STRING_OP:	/* AML_ASCIICHARLIST_ARG */
68 
69 		/* Fill in constant or string argument directly */
70 
71 		acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
72 					    GET_CURRENT_ARG_TYPE(walk_state->
73 								 arg_types),
74 					    op);
75 		break;
76 
77 	case AML_INT_NAMEPATH_OP:	/* AML_NAMESTRING_ARG */
78 
79 		status = acpi_ps_get_next_namepath(walk_state,
80 						   &(walk_state->parser_state),
81 						   op,
82 						   ACPI_POSSIBLE_METHOD_CALL);
83 		if (ACPI_FAILURE(status)) {
84 			return_ACPI_STATUS(status);
85 		}
86 
87 		walk_state->arg_types = 0;
88 		break;
89 
90 	default:
91 		/*
92 		 * Op is not a constant or string, append each argument to the Op
93 		 */
94 		while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
95 		       !walk_state->arg_count) {
96 			walk_state->aml = walk_state->parser_state.aml;
97 
98 			switch (op->common.aml_opcode) {
99 			case AML_METHOD_OP:
100 			case AML_BUFFER_OP:
101 			case AML_PACKAGE_OP:
102 			case AML_VARIABLE_PACKAGE_OP:
103 			case AML_WHILE_OP:
104 
105 				break;
106 
107 			default:
108 
109 				ASL_CV_CAPTURE_COMMENTS(walk_state);
110 				break;
111 			}
112 
113 			status =
114 			    acpi_ps_get_next_arg(walk_state,
115 						 &(walk_state->parser_state),
116 						 GET_CURRENT_ARG_TYPE
117 						 (walk_state->arg_types), &arg);
118 			if (ACPI_FAILURE(status)) {
119 				return_ACPI_STATUS(status);
120 			}
121 
122 			if (arg) {
123 				acpi_ps_append_arg(op, arg);
124 			}
125 
126 			INCREMENT_ARG_LIST(walk_state->arg_types);
127 		}
128 
129 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
130 				  "Final argument count: %8.8X pass %u\n",
131 				  walk_state->arg_count,
132 				  walk_state->pass_number));
133 
134 		/* Special processing for certain opcodes */
135 
136 		switch (op->common.aml_opcode) {
137 		case AML_METHOD_OP:
138 			/*
139 			 * Skip parsing of control method because we don't have enough
140 			 * info in the first pass to parse it correctly.
141 			 *
142 			 * Save the length and address of the body
143 			 */
144 			op->named.data = walk_state->parser_state.aml;
145 			op->named.length = (u32)
146 			    (walk_state->parser_state.pkg_end -
147 			     walk_state->parser_state.aml);
148 
149 			/* Skip body of method */
150 
151 			walk_state->parser_state.aml =
152 			    walk_state->parser_state.pkg_end;
153 			walk_state->arg_count = 0;
154 			break;
155 
156 		case AML_BUFFER_OP:
157 		case AML_PACKAGE_OP:
158 		case AML_VARIABLE_PACKAGE_OP:
159 
160 			if ((op->common.parent) &&
161 			    (op->common.parent->common.aml_opcode ==
162 			     AML_NAME_OP)
163 			    && (walk_state->pass_number <=
164 				ACPI_IMODE_LOAD_PASS2)) {
165 				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
166 						  "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
167 						  walk_state->pass_number,
168 						  aml_op_start));
169 
170 				/*
171 				 * Skip parsing of Buffers and Packages because we don't have
172 				 * enough info in the first pass to parse them correctly.
173 				 */
174 				op->named.data = aml_op_start;
175 				op->named.length = (u32)
176 				    (walk_state->parser_state.pkg_end -
177 				     aml_op_start);
178 
179 				/* Skip body */
180 
181 				walk_state->parser_state.aml =
182 				    walk_state->parser_state.pkg_end;
183 				walk_state->arg_count = 0;
184 			}
185 			break;
186 
187 		case AML_WHILE_OP:
188 
189 			if (walk_state->control_state) {
190 				walk_state->control_state->control.package_end =
191 				    walk_state->parser_state.pkg_end;
192 			}
193 			break;
194 
195 		default:
196 
197 			/* No action for all other opcodes */
198 
199 			break;
200 		}
201 
202 		break;
203 	}
204 
205 	return_ACPI_STATUS(AE_OK);
206 }
207 
208 /*******************************************************************************
209  *
210  * FUNCTION:    acpi_ps_parse_loop
211  *
212  * PARAMETERS:  walk_state          - Current state
213  *
214  * RETURN:      Status
215  *
216  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
217  *              a tree of ops.
218  *
219  ******************************************************************************/
220 
221 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
222 {
223 	acpi_status status = AE_OK;
224 	union acpi_parse_object *op = NULL;	/* current op */
225 	struct acpi_parse_state *parser_state;
226 	u8 *aml_op_start = NULL;
227 	u8 opcode_length;
228 
229 	ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
230 
231 	if (walk_state->descending_callback == NULL) {
232 		return_ACPI_STATUS(AE_BAD_PARAMETER);
233 	}
234 
235 	parser_state = &walk_state->parser_state;
236 	walk_state->arg_types = 0;
237 
238 #ifndef ACPI_CONSTANT_EVAL_ONLY
239 
240 	if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
241 
242 		/* We are restarting a preempted control method */
243 
244 		if (acpi_ps_has_completed_scope(parser_state)) {
245 			/*
246 			 * We must check if a predicate to an IF or WHILE statement
247 			 * was just completed
248 			 */
249 			if ((parser_state->scope->parse_scope.op) &&
250 			    ((parser_state->scope->parse_scope.op->common.
251 			      aml_opcode == AML_IF_OP)
252 			     || (parser_state->scope->parse_scope.op->common.
253 				 aml_opcode == AML_WHILE_OP))
254 			    && (walk_state->control_state)
255 			    && (walk_state->control_state->common.state ==
256 				ACPI_CONTROL_PREDICATE_EXECUTING)) {
257 				/*
258 				 * A predicate was just completed, get the value of the
259 				 * predicate and branch based on that value
260 				 */
261 				walk_state->op = NULL;
262 				status =
263 				    acpi_ds_get_predicate_value(walk_state,
264 								ACPI_TO_POINTER
265 								(TRUE));
266 				if (ACPI_FAILURE(status)
267 				    && !ACPI_CNTL_EXCEPTION(status)) {
268 					if (status == AE_AML_NO_RETURN_VALUE) {
269 						ACPI_EXCEPTION((AE_INFO, status,
270 								"Invoked method did not return a value"));
271 					}
272 
273 					ACPI_EXCEPTION((AE_INFO, status,
274 							"GetPredicate Failed"));
275 					return_ACPI_STATUS(status);
276 				}
277 
278 				status =
279 				    acpi_ps_next_parse_state(walk_state, op,
280 							     status);
281 			}
282 
283 			acpi_ps_pop_scope(parser_state, &op,
284 					  &walk_state->arg_types,
285 					  &walk_state->arg_count);
286 			ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
287 					  "Popped scope, Op=%p\n", op));
288 		} else if (walk_state->prev_op) {
289 
290 			/* We were in the middle of an op */
291 
292 			op = walk_state->prev_op;
293 			walk_state->arg_types = walk_state->prev_arg_types;
294 		}
295 	}
296 #endif
297 
298 	/* Iterative parsing loop, while there is more AML to process: */
299 
300 	while ((parser_state->aml < parser_state->aml_end) || (op)) {
301 		ASL_CV_CAPTURE_COMMENTS(walk_state);
302 
303 		aml_op_start = parser_state->aml;
304 		if (!op) {
305 			status =
306 			    acpi_ps_create_op(walk_state, aml_op_start, &op);
307 			if (ACPI_FAILURE(status)) {
308 				/*
309 				 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
310 				 * executing it as a control method. However, if we encounter
311 				 * an error while loading the table, we need to keep trying to
312 				 * load the table rather than aborting the table load. Set the
313 				 * status to AE_OK to proceed with the table load.
314 				 */
315 				if ((walk_state->
316 				     parse_flags & ACPI_PARSE_MODULE_LEVEL)
317 				    && ((status == AE_ALREADY_EXISTS)
318 					|| (status == AE_NOT_FOUND))) {
319 					status = AE_OK;
320 				}
321 				if (status == AE_CTRL_PARSE_CONTINUE) {
322 					continue;
323 				}
324 
325 				if (status == AE_CTRL_PARSE_PENDING) {
326 					status = AE_OK;
327 				}
328 
329 				if (status == AE_CTRL_TERMINATE) {
330 					return_ACPI_STATUS(status);
331 				}
332 
333 				status =
334 				    acpi_ps_complete_op(walk_state, &op,
335 							status);
336 				if (ACPI_FAILURE(status)) {
337 					return_ACPI_STATUS(status);
338 				}
339 				if (acpi_ns_opens_scope
340 				    (acpi_ps_get_opcode_info
341 				     (walk_state->opcode)->object_type)) {
342 					/*
343 					 * If the scope/device op fails to parse, skip the body of
344 					 * the scope op because the parse failure indicates that
345 					 * the device may not exist.
346 					 */
347 					ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode));
348 
349 					/*
350 					 * Determine the opcode length before skipping the opcode.
351 					 * An opcode can be 1 byte or 2 bytes in length.
352 					 */
353 					opcode_length = 1;
354 					if ((walk_state->opcode & 0xFF00) ==
355 					    AML_EXTENDED_OPCODE) {
356 						opcode_length = 2;
357 					}
358 					walk_state->parser_state.aml =
359 					    walk_state->aml + opcode_length;
360 
361 					walk_state->parser_state.aml =
362 					    acpi_ps_get_next_package_end
363 					    (&walk_state->parser_state);
364 					if ((walk_state->parser_state.aml >
365 					     walk_state->parser_state.aml_end)
366 					    || (walk_state->parser_state.aml <
367 						walk_state->aml)) {
368 						return_ACPI_STATUS
369 						    (AE_AML_PACKAGE_LIMIT);
370 					}
371 					walk_state->aml =
372 					    walk_state->parser_state.aml;
373 				}
374 
375 				continue;
376 			}
377 
378 			acpi_ex_start_trace_opcode(op, walk_state);
379 		}
380 
381 		/*
382 		 * Start arg_count at zero because we don't know if there are
383 		 * any args yet
384 		 */
385 		walk_state->arg_count = 0;
386 
387 		switch (op->common.aml_opcode) {
388 		case AML_BYTE_OP:
389 		case AML_WORD_OP:
390 		case AML_DWORD_OP:
391 		case AML_QWORD_OP:
392 
393 			break;
394 
395 		default:
396 
397 			ASL_CV_CAPTURE_COMMENTS(walk_state);
398 			break;
399 		}
400 
401 		/* Are there any arguments that must be processed? */
402 
403 		if (walk_state->arg_types) {
404 
405 			/* Get arguments */
406 
407 			status =
408 			    acpi_ps_get_arguments(walk_state, aml_op_start, op);
409 			if (ACPI_FAILURE(status)) {
410 				status =
411 				    acpi_ps_complete_op(walk_state, &op,
412 							status);
413 				if (ACPI_FAILURE(status)) {
414 					return_ACPI_STATUS(status);
415 				}
416 				if ((walk_state->control_state) &&
417 				    ((walk_state->control_state->control.
418 				      opcode == AML_IF_OP)
419 				     || (walk_state->control_state->control.
420 					 opcode == AML_WHILE_OP))) {
421 					/*
422 					 * If the if/while op fails to parse, we will skip parsing
423 					 * the body of the op.
424 					 */
425 					parser_state->aml =
426 					    walk_state->control_state->control.
427 					    aml_predicate_start + 1;
428 					parser_state->aml =
429 					    acpi_ps_get_next_package_end
430 					    (parser_state);
431 					if ((parser_state->aml >
432 					     parser_state->aml_end)
433 					    || (parser_state->aml <
434 						walk_state->control_state->
435 						control.aml_predicate_start)) {
436 						return_ACPI_STATUS
437 						    (AE_AML_PACKAGE_LIMIT);
438 					}
439 					walk_state->aml = parser_state->aml;
440 
441 					ACPI_ERROR((AE_INFO,
442 						    "Skipping While/If block"));
443 					if ((walk_state->aml <
444 					     parser_state->aml_end)
445 					    && (*walk_state->aml ==
446 						AML_ELSE_OP)) {
447 						ACPI_ERROR((AE_INFO,
448 							    "Skipping Else block"));
449 						walk_state->parser_state.aml =
450 						    walk_state->aml + 1;
451 						walk_state->parser_state.aml =
452 						    acpi_ps_get_next_package_end
453 						    (parser_state);
454 						if ((walk_state->parser_state.
455 						     aml >
456 						     walk_state->parser_state.
457 						     aml_end)
458 						    || (walk_state->
459 							parser_state.aml <
460 							walk_state->aml)) {
461 							return_ACPI_STATUS
462 							    (AE_AML_PACKAGE_LIMIT);
463 						}
464 						walk_state->aml =
465 						    parser_state->aml;
466 					}
467 					ACPI_FREE(acpi_ut_pop_generic_state
468 						  (&walk_state->control_state));
469 				}
470 				op = NULL;
471 				continue;
472 			}
473 		}
474 
475 		/* Check for arguments that need to be processed */
476 
477 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
478 				  "Parseloop: argument count: %8.8X\n",
479 				  walk_state->arg_count));
480 
481 		if (walk_state->arg_count) {
482 			/*
483 			 * There are arguments (complex ones), push Op and
484 			 * prepare for argument
485 			 */
486 			status = acpi_ps_push_scope(parser_state, op,
487 						    walk_state->arg_types,
488 						    walk_state->arg_count);
489 			if (ACPI_FAILURE(status)) {
490 				status =
491 				    acpi_ps_complete_op(walk_state, &op,
492 							status);
493 				if (ACPI_FAILURE(status)) {
494 					return_ACPI_STATUS(status);
495 				}
496 
497 				continue;
498 			}
499 
500 			op = NULL;
501 			continue;
502 		}
503 
504 		/*
505 		 * All arguments have been processed -- Op is complete,
506 		 * prepare for next
507 		 */
508 		walk_state->op_info =
509 		    acpi_ps_get_opcode_info(op->common.aml_opcode);
510 		if (walk_state->op_info->flags & AML_NAMED) {
511 			if (op->common.aml_opcode == AML_REGION_OP ||
512 			    op->common.aml_opcode == AML_DATA_REGION_OP) {
513 				/*
514 				 * Skip parsing of control method or opregion body,
515 				 * because we don't have enough info in the first pass
516 				 * to parse them correctly.
517 				 *
518 				 * Completed parsing an op_region declaration, we now
519 				 * know the length.
520 				 */
521 				op->named.length =
522 				    (u32) (parser_state->aml - op->named.data);
523 			}
524 		}
525 
526 		if (walk_state->op_info->flags & AML_CREATE) {
527 			/*
528 			 * Backup to beginning of create_XXXfield declaration (1 for
529 			 * Opcode)
530 			 *
531 			 * body_length is unknown until we parse the body
532 			 */
533 			op->named.length =
534 			    (u32) (parser_state->aml - op->named.data);
535 		}
536 
537 		if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
538 			/*
539 			 * Backup to beginning of bank_field declaration
540 			 *
541 			 * body_length is unknown until we parse the body
542 			 */
543 			op->named.length =
544 			    (u32) (parser_state->aml - op->named.data);
545 		}
546 
547 		/* This op complete, notify the dispatcher */
548 
549 		if (walk_state->ascending_callback != NULL) {
550 			walk_state->op = op;
551 			walk_state->opcode = op->common.aml_opcode;
552 
553 			status = walk_state->ascending_callback(walk_state);
554 			status =
555 			    acpi_ps_next_parse_state(walk_state, op, status);
556 			if (status == AE_CTRL_PENDING) {
557 				status = AE_OK;
558 			} else
559 			    if ((walk_state->
560 				 parse_flags & ACPI_PARSE_MODULE_LEVEL)
561 				&& (ACPI_AML_EXCEPTION(status)
562 				    || status == AE_ALREADY_EXISTS
563 				    || status == AE_NOT_FOUND)) {
564 				/*
565 				 * ACPI_PARSE_MODULE_LEVEL flag means that we
566 				 * are currently loading a table by executing
567 				 * it as a control method. However, if we
568 				 * encounter an error while loading the table,
569 				 * we need to keep trying to load the table
570 				 * rather than aborting the table load (setting
571 				 * the status to AE_OK continues the table
572 				 * load). If we get a failure at this point, it
573 				 * means that the dispatcher got an error while
574 				 * trying to execute the Op.
575 				 */
576 				status = AE_OK;
577 			}
578 		}
579 
580 		status = acpi_ps_complete_op(walk_state, &op, status);
581 		if (ACPI_FAILURE(status)) {
582 			return_ACPI_STATUS(status);
583 		}
584 
585 	}			/* while parser_state->Aml */
586 
587 	status = acpi_ps_complete_final_op(walk_state, op, status);
588 	return_ACPI_STATUS(status);
589 }
590