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