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