1/******************************************************************************
2 *
3 * Module Name: psloop - Main AML parse loop
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2007, R. Byron Moore
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 <acpi/acparser.h>
54#include <acpi/acdispat.h>
55#include <acpi/amlcode.h>
56
57#define _COMPONENT          ACPI_PARSER
58ACPI_MODULE_NAME("psloop")
59
60static u32 acpi_gbl_depth = 0;
61
62/* Local prototypes */
63
64static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
65
66static acpi_status
67acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
68		       u8 * aml_op_start,
69		       union acpi_parse_object *unnamed_op,
70		       union acpi_parse_object **op);
71
72static acpi_status
73acpi_ps_create_op(struct acpi_walk_state *walk_state,
74		  u8 * aml_op_start, union acpi_parse_object **new_op);
75
76static acpi_status
77acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
78		      u8 * aml_op_start, union acpi_parse_object *op);
79
80static acpi_status
81acpi_ps_complete_op(struct acpi_walk_state *walk_state,
82		    union acpi_parse_object **op, acpi_status status);
83
84static acpi_status
85acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
86			  union acpi_parse_object *op, acpi_status status);
87
88/*******************************************************************************
89 *
90 * FUNCTION:    acpi_ps_get_aml_opcode
91 *
92 * PARAMETERS:  walk_state          - Current state
93 *
94 * RETURN:      Status
95 *
96 * DESCRIPTION: Extract the next AML opcode from the input stream.
97 *
98 ******************************************************************************/
99
100static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
101{
102
103	ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
104
105	walk_state->aml_offset =
106	    (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
107				walk_state->parser_state.aml_start);
108	walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
109
110	/*
111	 * First cut to determine what we have found:
112	 * 1) A valid AML opcode
113	 * 2) A name string
114	 * 3) An unknown/invalid opcode
115	 */
116	walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
117
118	switch (walk_state->op_info->class) {
119	case AML_CLASS_ASCII:
120	case AML_CLASS_PREFIX:
121		/*
122		 * Starts with a valid prefix or ASCII char, this is a name
123		 * string. Convert the bare name string to a namepath.
124		 */
125		walk_state->opcode = AML_INT_NAMEPATH_OP;
126		walk_state->arg_types = ARGP_NAMESTRING;
127		break;
128
129	case AML_CLASS_UNKNOWN:
130
131		/* The opcode is unrecognized. Just skip unknown opcodes */
132
133		ACPI_ERROR((AE_INFO,
134			    "Found unknown opcode %X at AML address %p offset %X, ignoring",
135			    walk_state->opcode, walk_state->parser_state.aml,
136			    walk_state->aml_offset));
137
138		ACPI_DUMP_BUFFER(walk_state->parser_state.aml, 128);
139
140		/* Assume one-byte bad opcode */
141
142		walk_state->parser_state.aml++;
143		return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
144
145	default:
146
147		/* Found opcode info, this is a normal opcode */
148
149		walk_state->parser_state.aml +=
150		    acpi_ps_get_opcode_size(walk_state->opcode);
151		walk_state->arg_types = walk_state->op_info->parse_args;
152		break;
153	}
154
155	return_ACPI_STATUS(AE_OK);
156}
157
158/*******************************************************************************
159 *
160 * FUNCTION:    acpi_ps_build_named_op
161 *
162 * PARAMETERS:  walk_state          - Current state
163 *              aml_op_start        - Begin of named Op in AML
164 *              unnamed_op          - Early Op (not a named Op)
165 *              Op                  - Returned Op
166 *
167 * RETURN:      Status
168 *
169 * DESCRIPTION: Parse a named Op
170 *
171 ******************************************************************************/
172
173static acpi_status
174acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
175		       u8 * aml_op_start,
176		       union acpi_parse_object *unnamed_op,
177		       union acpi_parse_object **op)
178{
179	acpi_status status = AE_OK;
180	union acpi_parse_object *arg = NULL;
181
182	ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
183
184	unnamed_op->common.value.arg = NULL;
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		ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog"));
226		return_ACPI_STATUS(status);
227	}
228
229	if (!*op) {
230		return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
231	}
232
233	status = acpi_ps_next_parse_state(walk_state, *op, status);
234	if (ACPI_FAILURE(status)) {
235		if (status == AE_CTRL_PENDING) {
236			return_ACPI_STATUS(AE_CTRL_PARSE_PENDING);
237		}
238		return_ACPI_STATUS(status);
239	}
240
241	acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
242	acpi_gbl_depth++;
243
244	if ((*op)->common.aml_opcode == AML_REGION_OP) {
245		/*
246		 * Defer final parsing of an operation_region body, because we don't
247		 * have enough info in the first pass to parse it correctly (i.e.,
248		 * there may be method calls within the term_arg elements of the body.)
249		 *
250		 * However, we must continue parsing because the opregion is not a
251		 * standalone package -- we don't know where the end is at this point.
252		 *
253		 * (Length is unknown until parse of the body complete)
254		 */
255		(*op)->named.data = aml_op_start;
256		(*op)->named.length = 0;
257	}
258
259	return_ACPI_STATUS(AE_OK);
260}
261
262/*******************************************************************************
263 *
264 * FUNCTION:    acpi_ps_create_op
265 *
266 * PARAMETERS:  walk_state          - Current state
267 *              aml_op_start        - Op start in AML
268 *              new_op              - Returned Op
269 *
270 * RETURN:      Status
271 *
272 * DESCRIPTION: Get Op from AML
273 *
274 ******************************************************************************/
275
276static acpi_status
277acpi_ps_create_op(struct acpi_walk_state *walk_state,
278		  u8 * aml_op_start, union acpi_parse_object **new_op)
279{
280	acpi_status status = AE_OK;
281	union acpi_parse_object *op;
282	union acpi_parse_object *named_op = NULL;
283
284	ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
285
286	status = acpi_ps_get_aml_opcode(walk_state);
287	if (status == AE_CTRL_PARSE_CONTINUE) {
288		return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
289	}
290
291	/* Create Op structure and append to parent's argument list */
292
293	walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
294	op = acpi_ps_alloc_op(walk_state->opcode);
295	if (!op) {
296		return_ACPI_STATUS(AE_NO_MEMORY);
297	}
298
299	if (walk_state->op_info->flags & AML_NAMED) {
300		status =
301		    acpi_ps_build_named_op(walk_state, aml_op_start, op,
302					   &named_op);
303		acpi_ps_free_op(op);
304		if (ACPI_FAILURE(status)) {
305			return_ACPI_STATUS(status);
306		}
307
308		*new_op = named_op;
309		return_ACPI_STATUS(AE_OK);
310	}
311
312	/* Not a named opcode, just allocate Op and append to parent */
313
314	if (walk_state->op_info->flags & AML_CREATE) {
315		/*
316		 * Backup to beginning of create_xXXfield declaration
317		 * body_length is unknown until we parse the body
318		 */
319		op->named.data = aml_op_start;
320		op->named.length = 0;
321	}
322
323	acpi_ps_append_arg(acpi_ps_get_parent_scope
324			   (&(walk_state->parser_state)), op);
325
326	if (walk_state->descending_callback != NULL) {
327		/*
328		 * Find the object. This will either insert the object into
329		 * the namespace or simply look it up
330		 */
331		walk_state->op = *new_op = op;
332
333		status = walk_state->descending_callback(walk_state, &op);
334		status = acpi_ps_next_parse_state(walk_state, op, status);
335		if (status == AE_CTRL_PENDING) {
336			status = AE_CTRL_PARSE_PENDING;
337		}
338	}
339
340	return_ACPI_STATUS(status);
341}
342
343/*******************************************************************************
344 *
345 * FUNCTION:    acpi_ps_get_arguments
346 *
347 * PARAMETERS:  walk_state          - Current state
348 *              aml_op_start        - Op start in AML
349 *              Op                  - Current Op
350 *
351 * RETURN:      Status
352 *
353 * DESCRIPTION: Get arguments for passed Op.
354 *
355 ******************************************************************************/
356
357static acpi_status
358acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
359		      u8 * aml_op_start, union acpi_parse_object *op)
360{
361	acpi_status status = AE_OK;
362	union acpi_parse_object *arg = NULL;
363
364	ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
365
366	switch (op->common.aml_opcode) {
367	case AML_BYTE_OP:	/* AML_BYTEDATA_ARG */
368	case AML_WORD_OP:	/* AML_WORDDATA_ARG */
369	case AML_DWORD_OP:	/* AML_DWORDATA_ARG */
370	case AML_QWORD_OP:	/* AML_QWORDATA_ARG */
371	case AML_STRING_OP:	/* AML_ASCIICHARLIST_ARG */
372
373		/* Fill in constant or string argument directly */
374
375		acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
376					    GET_CURRENT_ARG_TYPE(walk_state->
377								 arg_types),
378					    op);
379		break;
380
381	case AML_INT_NAMEPATH_OP:	/* AML_NAMESTRING_ARG */
382
383		status =
384		    acpi_ps_get_next_namepath(walk_state,
385					      &(walk_state->parser_state), op,
386					      1);
387		if (ACPI_FAILURE(status)) {
388			return_ACPI_STATUS(status);
389		}
390
391		walk_state->arg_types = 0;
392		break;
393
394	default:
395		/*
396		 * Op is not a constant or string, append each argument to the Op
397		 */
398		while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
399		       && !walk_state->arg_count) {
400			walk_state->aml_offset =
401			    (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
402						walk_state->parser_state.
403						aml_start);
404
405			status =
406			    acpi_ps_get_next_arg(walk_state,
407						 &(walk_state->parser_state),
408						 GET_CURRENT_ARG_TYPE
409						 (walk_state->arg_types), &arg);
410			if (ACPI_FAILURE(status)) {
411				return_ACPI_STATUS(status);
412			}
413
414			if (arg) {
415				arg->common.aml_offset = walk_state->aml_offset;
416				acpi_ps_append_arg(op, arg);
417			}
418
419			INCREMENT_ARG_LIST(walk_state->arg_types);
420		}
421
422		/* Special processing for certain opcodes */
423
424		/* TBD (remove): Temporary mechanism to disable this code if needed */
425
426#ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
427
428		if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS1) &&
429		    ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
430			/*
431			 * We want to skip If/Else/While constructs during Pass1 because we
432			 * want to actually conditionally execute the code during Pass2.
433			 *
434			 * Except for disassembly, where we always want to walk the
435			 * If/Else/While packages
436			 */
437			switch (op->common.aml_opcode) {
438			case AML_IF_OP:
439			case AML_ELSE_OP:
440			case AML_WHILE_OP:
441
442				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
443						  "Pass1: Skipping an If/Else/While body\n"));
444
445				/* Skip body of if/else/while in pass 1 */
446
447				walk_state->parser_state.aml =
448				    walk_state->parser_state.pkg_end;
449				walk_state->arg_count = 0;
450				break;
451
452			default:
453				break;
454			}
455		}
456#endif
457
458		switch (op->common.aml_opcode) {
459		case AML_METHOD_OP:
460			/*
461			 * Skip parsing of control method because we don't have enough
462			 * info in the first pass to parse it correctly.
463			 *
464			 * Save the length and address of the body
465			 */
466			op->named.data = walk_state->parser_state.aml;
467			op->named.length = (u32)
468			    (walk_state->parser_state.pkg_end -
469			     walk_state->parser_state.aml);
470
471			/* Skip body of method */
472
473			walk_state->parser_state.aml =
474			    walk_state->parser_state.pkg_end;
475			walk_state->arg_count = 0;
476			break;
477
478		case AML_BUFFER_OP:
479		case AML_PACKAGE_OP:
480		case AML_VAR_PACKAGE_OP:
481
482			if ((op->common.parent) &&
483			    (op->common.parent->common.aml_opcode ==
484			     AML_NAME_OP)
485			    && (walk_state->pass_number <=
486				ACPI_IMODE_LOAD_PASS2)) {
487				/*
488				 * Skip parsing of Buffers and Packages because we don't have
489				 * enough info in the first pass to parse them correctly.
490				 */
491				op->named.data = aml_op_start;
492				op->named.length = (u32)
493				    (walk_state->parser_state.pkg_end -
494				     aml_op_start);
495
496				/* Skip body */
497
498				walk_state->parser_state.aml =
499				    walk_state->parser_state.pkg_end;
500				walk_state->arg_count = 0;
501			}
502			break;
503
504		case AML_WHILE_OP:
505
506			if (walk_state->control_state) {
507				walk_state->control_state->control.package_end =
508				    walk_state->parser_state.pkg_end;
509			}
510			break;
511
512		default:
513
514			/* No action for all other opcodes */
515			break;
516		}
517
518		break;
519	}
520
521	return_ACPI_STATUS(AE_OK);
522}
523
524/*******************************************************************************
525 *
526 * FUNCTION:    acpi_ps_complete_op
527 *
528 * PARAMETERS:  walk_state          - Current state
529 *              Op                  - Returned Op
530 *              Status              - Parse status before complete Op
531 *
532 * RETURN:      Status
533 *
534 * DESCRIPTION: Complete Op
535 *
536 ******************************************************************************/
537
538static acpi_status
539acpi_ps_complete_op(struct acpi_walk_state *walk_state,
540		    union acpi_parse_object **op, acpi_status status)
541{
542	acpi_status status2;
543
544	ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
545
546	/*
547	 * Finished one argument of the containing scope
548	 */
549	walk_state->parser_state.scope->parse_scope.arg_count--;
550
551	/* Close this Op (will result in parse subtree deletion) */
552
553	status2 = acpi_ps_complete_this_op(walk_state, *op);
554	if (ACPI_FAILURE(status2)) {
555		return_ACPI_STATUS(status2);
556	}
557
558	*op = NULL;
559
560	switch (status) {
561	case AE_OK:
562		break;
563
564	case AE_CTRL_TRANSFER:
565
566		/* We are about to transfer to a called method */
567
568		walk_state->prev_op = NULL;
569		walk_state->prev_arg_types = walk_state->arg_types;
570		return_ACPI_STATUS(status);
571
572	case AE_CTRL_END:
573
574		acpi_ps_pop_scope(&(walk_state->parser_state), op,
575				  &walk_state->arg_types,
576				  &walk_state->arg_count);
577
578		if (*op) {
579			walk_state->op = *op;
580			walk_state->op_info =
581			    acpi_ps_get_opcode_info((*op)->common.aml_opcode);
582			walk_state->opcode = (*op)->common.aml_opcode;
583
584			status = walk_state->ascending_callback(walk_state);
585			status =
586			    acpi_ps_next_parse_state(walk_state, *op, status);
587
588			status2 = acpi_ps_complete_this_op(walk_state, *op);
589			if (ACPI_FAILURE(status2)) {
590				return_ACPI_STATUS(status2);
591			}
592		}
593
594		status = AE_OK;
595		break;
596
597	case AE_CTRL_BREAK:
598	case AE_CTRL_CONTINUE:
599
600		/* Pop off scopes until we find the While */
601
602		while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
603			acpi_ps_pop_scope(&(walk_state->parser_state), op,
604					  &walk_state->arg_types,
605					  &walk_state->arg_count);
606
607			if ((*op)->common.aml_opcode != AML_WHILE_OP) {
608				status2 = acpi_ds_result_stack_pop(walk_state);
609				if (ACPI_FAILURE(status2)) {
610					return_ACPI_STATUS(status2);
611				}
612			}
613		}
614
615		/* Close this iteration of the While loop */
616
617		walk_state->op = *op;
618		walk_state->op_info =
619		    acpi_ps_get_opcode_info((*op)->common.aml_opcode);
620		walk_state->opcode = (*op)->common.aml_opcode;
621
622		status = walk_state->ascending_callback(walk_state);
623		status = acpi_ps_next_parse_state(walk_state, *op, status);
624
625		status2 = acpi_ps_complete_this_op(walk_state, *op);
626		if (ACPI_FAILURE(status2)) {
627			return_ACPI_STATUS(status2);
628		}
629
630		status = AE_OK;
631		break;
632
633	case AE_CTRL_TERMINATE:
634
635		/* Clean up */
636		do {
637			if (*op) {
638				status2 =
639				    acpi_ps_complete_this_op(walk_state, *op);
640				if (ACPI_FAILURE(status2)) {
641					return_ACPI_STATUS(status2);
642				}
643				status2 = acpi_ds_result_stack_pop(walk_state);
644				if (ACPI_FAILURE(status2)) {
645					return_ACPI_STATUS(status2);
646				}
647
648				acpi_ut_delete_generic_state
649				    (acpi_ut_pop_generic_state
650				     (&walk_state->control_state));
651			}
652
653			acpi_ps_pop_scope(&(walk_state->parser_state), op,
654					  &walk_state->arg_types,
655					  &walk_state->arg_count);
656
657		} while (*op);
658
659		return_ACPI_STATUS(AE_OK);
660
661	default:		/* All other non-AE_OK status */
662
663		do {
664			if (*op) {
665				status2 =
666				    acpi_ps_complete_this_op(walk_state, *op);
667				if (ACPI_FAILURE(status2)) {
668					return_ACPI_STATUS(status2);
669				}
670			}
671
672			acpi_ps_pop_scope(&(walk_state->parser_state), op,
673					  &walk_state->arg_types,
674					  &walk_state->arg_count);
675
676		} while (*op);
677
678		walk_state->prev_op = NULL;
679		walk_state->prev_arg_types = walk_state->arg_types;
680		return_ACPI_STATUS(status);
681	}
682
683	/* This scope complete? */
684
685	if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
686		acpi_ps_pop_scope(&(walk_state->parser_state), op,
687				  &walk_state->arg_types,
688				  &walk_state->arg_count);
689		ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
690	} else {
691		*op = NULL;
692	}
693
694	return_ACPI_STATUS(AE_OK);
695}
696
697/*******************************************************************************
698 *
699 * FUNCTION:    acpi_ps_complete_final_op
700 *
701 * PARAMETERS:  walk_state          - Current state
702 *              Op                  - Current Op
703 *              Status              - Current parse status before complete last
704 *                                    Op
705 *
706 * RETURN:      Status
707 *
708 * DESCRIPTION: Complete last Op.
709 *
710 ******************************************************************************/
711
712static acpi_status
713acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
714			  union acpi_parse_object *op, acpi_status status)
715{
716	acpi_status status2;
717
718	ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
719
720	/*
721	 * Complete the last Op (if not completed), and clear the scope stack.
722	 * It is easily possible to end an AML "package" with an unbounded number
723	 * of open scopes (such as when several ASL blocks are closed with
724	 * sequential closing braces). We want to terminate each one cleanly.
725	 */
726	ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
727			  op));
728	do {
729		if (op) {
730			if (walk_state->ascending_callback != NULL) {
731				walk_state->op = op;
732				walk_state->op_info =
733				    acpi_ps_get_opcode_info(op->common.
734							    aml_opcode);
735				walk_state->opcode = op->common.aml_opcode;
736
737				status =
738				    walk_state->ascending_callback(walk_state);
739				status =
740				    acpi_ps_next_parse_state(walk_state, op,
741							     status);
742				if (status == AE_CTRL_PENDING) {
743					status =
744					    acpi_ps_complete_op(walk_state, &op,
745								AE_OK);
746					if (ACPI_FAILURE(status)) {
747						return_ACPI_STATUS(status);
748					}
749				}
750
751				if (status == AE_CTRL_TERMINATE) {
752					status = AE_OK;
753
754					/* Clean up */
755					do {
756						if (op) {
757							status2 =
758							    acpi_ps_complete_this_op
759							    (walk_state, op);
760							if (ACPI_FAILURE
761							    (status2)) {
762								return_ACPI_STATUS
763								    (status2);
764							}
765						}
766
767						acpi_ps_pop_scope(&
768								  (walk_state->
769								   parser_state),
770								  &op,
771								  &walk_state->
772								  arg_types,
773								  &walk_state->
774								  arg_count);
775
776					} while (op);
777
778					return_ACPI_STATUS(status);
779				}
780
781				else if (ACPI_FAILURE(status)) {
782
783					/* First error is most important */
784
785					(void)
786					    acpi_ps_complete_this_op(walk_state,
787								     op);
788					return_ACPI_STATUS(status);
789				}
790			}
791
792			status2 = acpi_ps_complete_this_op(walk_state, op);
793			if (ACPI_FAILURE(status2)) {
794				return_ACPI_STATUS(status2);
795			}
796		}
797
798		acpi_ps_pop_scope(&(walk_state->parser_state), &op,
799				  &walk_state->arg_types,
800				  &walk_state->arg_count);
801
802	} while (op);
803
804	return_ACPI_STATUS(status);
805}
806
807/*******************************************************************************
808 *
809 * FUNCTION:    acpi_ps_parse_loop
810 *
811 * PARAMETERS:  walk_state          - Current state
812 *
813 * RETURN:      Status
814 *
815 * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
816 *              a tree of ops.
817 *
818 ******************************************************************************/
819
820acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
821{
822	acpi_status status = AE_OK;
823	union acpi_parse_object *op = NULL;	/* current op */
824	struct acpi_parse_state *parser_state;
825	u8 *aml_op_start = NULL;
826
827	ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
828
829	if (walk_state->descending_callback == NULL) {
830		return_ACPI_STATUS(AE_BAD_PARAMETER);
831	}
832
833	parser_state = &walk_state->parser_state;
834	walk_state->arg_types = 0;
835
836#if (!defined(ACPI_NO_METHOD_EXECUTION) && !defined(ACPI_CONSTANT_EVAL_ONLY))
837
838	if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
839
840		/* We are restarting a preempted control method */
841
842		if (acpi_ps_has_completed_scope(parser_state)) {
843			/*
844			 * We must check if a predicate to an IF or WHILE statement
845			 * was just completed
846			 */
847			if ((parser_state->scope->parse_scope.op) &&
848			    ((parser_state->scope->parse_scope.op->common.
849			      aml_opcode == AML_IF_OP)
850			     || (parser_state->scope->parse_scope.op->common.
851				 aml_opcode == AML_WHILE_OP))
852			    && (walk_state->control_state)
853			    && (walk_state->control_state->common.state ==
854				ACPI_CONTROL_PREDICATE_EXECUTING)) {
855				/*
856				 * A predicate was just completed, get the value of the
857				 * predicate and branch based on that value
858				 */
859				walk_state->op = NULL;
860				status =
861				    acpi_ds_get_predicate_value(walk_state,
862								ACPI_TO_POINTER
863								(TRUE));
864				if (ACPI_FAILURE(status)
865				    && ((status & AE_CODE_MASK) !=
866					AE_CODE_CONTROL)) {
867					if (status == AE_AML_NO_RETURN_VALUE) {
868						ACPI_EXCEPTION((AE_INFO, status,
869								"Invoked method did not return a value"));
870
871					}
872
873					ACPI_EXCEPTION((AE_INFO, status,
874							"GetPredicate Failed"));
875					return_ACPI_STATUS(status);
876				}
877
878				status =
879				    acpi_ps_next_parse_state(walk_state, op,
880							     status);
881			}
882
883			acpi_ps_pop_scope(parser_state, &op,
884					  &walk_state->arg_types,
885					  &walk_state->arg_count);
886			ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
887					  "Popped scope, Op=%p\n", op));
888		} else if (walk_state->prev_op) {
889
890			/* We were in the middle of an op */
891
892			op = walk_state->prev_op;
893			walk_state->arg_types = walk_state->prev_arg_types;
894		}
895	}
896#endif
897
898	/* Iterative parsing loop, while there is more AML to process: */
899
900	while ((parser_state->aml < parser_state->aml_end) || (op)) {
901		aml_op_start = parser_state->aml;
902		if (!op) {
903			status =
904			    acpi_ps_create_op(walk_state, aml_op_start, &op);
905			if (ACPI_FAILURE(status)) {
906				if (status == AE_CTRL_PARSE_CONTINUE) {
907					continue;
908				}
909
910				if (status == AE_CTRL_PARSE_PENDING) {
911					status = AE_OK;
912				}
913
914				status =
915				    acpi_ps_complete_op(walk_state, &op,
916							status);
917				if (ACPI_FAILURE(status)) {
918					return_ACPI_STATUS(status);
919				}
920
921				continue;
922			}
923
924			op->common.aml_offset = walk_state->aml_offset;
925
926			if (walk_state->op_info) {
927				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
928						  "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
929						  (u32) op->common.aml_opcode,
930						  walk_state->op_info->name, op,
931						  parser_state->aml,
932						  op->common.aml_offset));
933			}
934		}
935
936		/*
937		 * Start arg_count at zero because we don't know if there are
938		 * any args yet
939		 */
940		walk_state->arg_count = 0;
941
942		/* Are there any arguments that must be processed? */
943
944		if (walk_state->arg_types) {
945
946			/* Get arguments */
947
948			status =
949			    acpi_ps_get_arguments(walk_state, aml_op_start, op);
950			if (ACPI_FAILURE(status)) {
951				status =
952				    acpi_ps_complete_op(walk_state, &op,
953							status);
954				if (ACPI_FAILURE(status)) {
955					return_ACPI_STATUS(status);
956				}
957
958				continue;
959			}
960		}
961
962		/* Check for arguments that need to be processed */
963
964		if (walk_state->arg_count) {
965			/*
966			 * There are arguments (complex ones), push Op and
967			 * prepare for argument
968			 */
969			status = acpi_ps_push_scope(parser_state, op,
970						    walk_state->arg_types,
971						    walk_state->arg_count);
972			if (ACPI_FAILURE(status)) {
973				status =
974				    acpi_ps_complete_op(walk_state, &op,
975							status);
976				if (ACPI_FAILURE(status)) {
977					return_ACPI_STATUS(status);
978				}
979
980				continue;
981			}
982
983			op = NULL;
984			continue;
985		}
986
987		/*
988		 * All arguments have been processed -- Op is complete,
989		 * prepare for next
990		 */
991		walk_state->op_info =
992		    acpi_ps_get_opcode_info(op->common.aml_opcode);
993		if (walk_state->op_info->flags & AML_NAMED) {
994			if (acpi_gbl_depth) {
995				acpi_gbl_depth--;
996			}
997
998			if (op->common.aml_opcode == AML_REGION_OP) {
999				/*
1000				 * Skip parsing of control method or opregion body,
1001				 * because we don't have enough info in the first pass
1002				 * to parse them correctly.
1003				 *
1004				 * Completed parsing an op_region declaration, we now
1005				 * know the length.
1006				 */
1007				op->named.length =
1008				    (u32) (parser_state->aml - op->named.data);
1009			}
1010		}
1011
1012		if (walk_state->op_info->flags & AML_CREATE) {
1013			/*
1014			 * Backup to beginning of create_xXXfield declaration (1 for
1015			 * Opcode)
1016			 *
1017			 * body_length is unknown until we parse the body
1018			 */
1019			op->named.length =
1020			    (u32) (parser_state->aml - op->named.data);
1021		}
1022
1023		/* This op complete, notify the dispatcher */
1024
1025		if (walk_state->ascending_callback != NULL) {
1026			walk_state->op = op;
1027			walk_state->opcode = op->common.aml_opcode;
1028
1029			status = walk_state->ascending_callback(walk_state);
1030			status =
1031			    acpi_ps_next_parse_state(walk_state, op, status);
1032			if (status == AE_CTRL_PENDING) {
1033				status = AE_OK;
1034			}
1035		}
1036
1037		status = acpi_ps_complete_op(walk_state, &op, status);
1038		if (ACPI_FAILURE(status)) {
1039			return_ACPI_STATUS(status);
1040		}
1041
1042	}			/* while parser_state->Aml */
1043
1044	status = acpi_ps_complete_final_op(walk_state, op, status);
1045	return_ACPI_STATUS(status);
1046}
1047