kernel: Sync ACPICA with Intel's version 20140214.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / parser / psobject.c
1 /******************************************************************************
2  *
3  * Module Name: psobject - Support for parse objects
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 #include "acpi.h"
46 #include "accommon.h"
47 #include "acparser.h"
48 #include "amlcode.h"
49
50 #define _COMPONENT          ACPI_PARSER
51         ACPI_MODULE_NAME    ("psobject")
52
53
54 /* Local prototypes */
55
56 static ACPI_STATUS
57 AcpiPsGetAmlOpcode (
58     ACPI_WALK_STATE         *WalkState);
59
60
61 /*******************************************************************************
62  *
63  * FUNCTION:    AcpiPsGetAmlOpcode
64  *
65  * PARAMETERS:  WalkState           - Current state
66  *
67  * RETURN:      Status
68  *
69  * DESCRIPTION: Extract the next AML opcode from the input stream.
70  *
71  ******************************************************************************/
72
73 static ACPI_STATUS
74 AcpiPsGetAmlOpcode (
75     ACPI_WALK_STATE         *WalkState)
76 {
77
78     ACPI_FUNCTION_TRACE_PTR (PsGetAmlOpcode, WalkState);
79
80
81     WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
82                                 WalkState->ParserState.AmlStart);
83     WalkState->Opcode = AcpiPsPeekOpcode (&(WalkState->ParserState));
84
85     /*
86      * First cut to determine what we have found:
87      * 1) A valid AML opcode
88      * 2) A name string
89      * 3) An unknown/invalid opcode
90      */
91     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
92
93     switch (WalkState->OpInfo->Class)
94     {
95     case AML_CLASS_ASCII:
96     case AML_CLASS_PREFIX:
97         /*
98          * Starts with a valid prefix or ASCII char, this is a name
99          * string. Convert the bare name string to a namepath.
100          */
101         WalkState->Opcode = AML_INT_NAMEPATH_OP;
102         WalkState->ArgTypes = ARGP_NAMESTRING;
103         break;
104
105     case AML_CLASS_UNKNOWN:
106
107         /* The opcode is unrecognized. Complain and skip unknown opcodes */
108
109         if (WalkState->PassNumber == 2)
110         {
111             ACPI_ERROR ((AE_INFO,
112                 "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
113                 WalkState->Opcode,
114                 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER))));
115
116             ACPI_DUMP_BUFFER ((WalkState->ParserState.Aml - 16), 48);
117
118 #ifdef ACPI_ASL_COMPILER
119             /*
120              * This is executed for the disassembler only. Output goes
121              * to the disassembled ASL output file.
122              */
123             AcpiOsPrintf (
124                 "/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
125                 WalkState->Opcode,
126                 (UINT32) (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER)));
127
128             /* Dump the context surrounding the invalid opcode */
129
130             AcpiUtDumpBuffer (((UINT8 *) WalkState->ParserState.Aml - 16),
131                 48, DB_BYTE_DISPLAY,
132                 (WalkState->AmlOffset + sizeof (ACPI_TABLE_HEADER) - 16));
133             AcpiOsPrintf (" */\n");
134 #endif
135         }
136
137         /* Increment past one-byte or two-byte opcode */
138
139         WalkState->ParserState.Aml++;
140         if (WalkState->Opcode > 0xFF) /* Can only happen if first byte is 0x5B */
141         {
142             WalkState->ParserState.Aml++;
143         }
144
145         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
146
147     default:
148
149         /* Found opcode info, this is a normal opcode */
150
151         WalkState->ParserState.Aml += AcpiPsGetOpcodeSize (WalkState->Opcode);
152         WalkState->ArgTypes = WalkState->OpInfo->ParseArgs;
153         break;
154     }
155
156     return_ACPI_STATUS (AE_OK);
157 }
158
159
160 /*******************************************************************************
161  *
162  * FUNCTION:    AcpiPsBuildNamedOp
163  *
164  * PARAMETERS:  WalkState           - Current state
165  *              AmlOpStart          - Begin of named Op in AML
166  *              UnnamedOp           - Early Op (not a named Op)
167  *              Op                  - Returned Op
168  *
169  * RETURN:      Status
170  *
171  * DESCRIPTION: Parse a named Op
172  *
173  ******************************************************************************/
174
175 ACPI_STATUS
176 AcpiPsBuildNamedOp (
177     ACPI_WALK_STATE         *WalkState,
178     UINT8                   *AmlOpStart,
179     ACPI_PARSE_OBJECT       *UnnamedOp,
180     ACPI_PARSE_OBJECT       **Op)
181 {
182     ACPI_STATUS             Status = AE_OK;
183     ACPI_PARSE_OBJECT       *Arg = NULL;
184
185
186     ACPI_FUNCTION_TRACE_PTR (PsBuildNamedOp, WalkState);
187
188
189     UnnamedOp->Common.Value.Arg = NULL;
190     UnnamedOp->Common.ArgListLength = 0;
191     UnnamedOp->Common.AmlOpcode = WalkState->Opcode;
192
193     /*
194      * Get and append arguments until we find the node that contains
195      * the name (the type ARGP_NAME).
196      */
197     while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) &&
198           (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) != ARGP_NAME))
199     {
200         Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
201                     GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
202         if (ACPI_FAILURE (Status))
203         {
204             return_ACPI_STATUS (Status);
205         }
206
207         AcpiPsAppendArg (UnnamedOp, Arg);
208         INCREMENT_ARG_LIST (WalkState->ArgTypes);
209     }
210
211     /*
212      * Make sure that we found a NAME and didn't run out of arguments
213      */
214     if (!GET_CURRENT_ARG_TYPE (WalkState->ArgTypes))
215     {
216         return_ACPI_STATUS (AE_AML_NO_OPERAND);
217     }
218
219     /* We know that this arg is a name, move to next arg */
220
221     INCREMENT_ARG_LIST (WalkState->ArgTypes);
222
223     /*
224      * Find the object. This will either insert the object into
225      * the namespace or simply look it up
226      */
227     WalkState->Op = NULL;
228
229     Status = WalkState->DescendingCallback (WalkState, Op);
230     if (ACPI_FAILURE (Status))
231     {
232         if (Status != AE_CTRL_TERMINATE)
233         {
234             ACPI_EXCEPTION ((AE_INFO, Status, "During name lookup/catalog"));
235         }
236         return_ACPI_STATUS (Status);
237     }
238
239     if (!*Op)
240     {
241         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
242     }
243
244     Status = AcpiPsNextParseState (WalkState, *Op, Status);
245     if (ACPI_FAILURE (Status))
246     {
247         if (Status == AE_CTRL_PENDING)
248         {
249             Status = AE_CTRL_PARSE_PENDING;
250         }
251         return_ACPI_STATUS (Status);
252     }
253
254     AcpiPsAppendArg (*Op, UnnamedOp->Common.Value.Arg);
255
256     if ((*Op)->Common.AmlOpcode == AML_REGION_OP ||
257         (*Op)->Common.AmlOpcode == AML_DATA_REGION_OP)
258     {
259         /*
260          * Defer final parsing of an OperationRegion body, because we don't
261          * have enough info in the first pass to parse it correctly (i.e.,
262          * there may be method calls within the TermArg elements of the body.)
263          *
264          * However, we must continue parsing because the opregion is not a
265          * standalone package -- we don't know where the end is at this point.
266          *
267          * (Length is unknown until parse of the body complete)
268          */
269         (*Op)->Named.Data = AmlOpStart;
270         (*Op)->Named.Length = 0;
271     }
272
273     return_ACPI_STATUS (AE_OK);
274 }
275
276
277 /*******************************************************************************
278  *
279  * FUNCTION:    AcpiPsCreateOp
280  *
281  * PARAMETERS:  WalkState           - Current state
282  *              AmlOpStart          - Op start in AML
283  *              NewOp               - Returned Op
284  *
285  * RETURN:      Status
286  *
287  * DESCRIPTION: Get Op from AML
288  *
289  ******************************************************************************/
290
291 ACPI_STATUS
292 AcpiPsCreateOp (
293     ACPI_WALK_STATE         *WalkState,
294     UINT8                   *AmlOpStart,
295     ACPI_PARSE_OBJECT       **NewOp)
296 {
297     ACPI_STATUS             Status = AE_OK;
298     ACPI_PARSE_OBJECT       *Op;
299     ACPI_PARSE_OBJECT       *NamedOp = NULL;
300     ACPI_PARSE_OBJECT       *ParentScope;
301     UINT8                   ArgumentCount;
302     const ACPI_OPCODE_INFO  *OpInfo;
303
304
305     ACPI_FUNCTION_TRACE_PTR (PsCreateOp, WalkState);
306
307
308     Status = AcpiPsGetAmlOpcode (WalkState);
309     if (Status == AE_CTRL_PARSE_CONTINUE)
310     {
311         return_ACPI_STATUS (AE_CTRL_PARSE_CONTINUE);
312     }
313
314     /* Create Op structure and append to parent's argument list */
315
316     WalkState->OpInfo = AcpiPsGetOpcodeInfo (WalkState->Opcode);
317     Op = AcpiPsAllocOp (WalkState->Opcode);
318     if (!Op)
319     {
320         return_ACPI_STATUS (AE_NO_MEMORY);
321     }
322
323     if (WalkState->OpInfo->Flags & AML_NAMED)
324     {
325         Status = AcpiPsBuildNamedOp (WalkState, AmlOpStart, Op, &NamedOp);
326         AcpiPsFreeOp (Op);
327         if (ACPI_FAILURE (Status))
328         {
329             return_ACPI_STATUS (Status);
330         }
331
332         *NewOp = NamedOp;
333         return_ACPI_STATUS (AE_OK);
334     }
335
336     /* Not a named opcode, just allocate Op and append to parent */
337
338     if (WalkState->OpInfo->Flags & AML_CREATE)
339     {
340         /*
341          * Backup to beginning of CreateXXXfield declaration
342          * BodyLength is unknown until we parse the body
343          */
344         Op->Named.Data = AmlOpStart;
345         Op->Named.Length = 0;
346     }
347
348     if (WalkState->Opcode == AML_BANK_FIELD_OP)
349     {
350         /*
351          * Backup to beginning of BankField declaration
352          * BodyLength is unknown until we parse the body
353          */
354         Op->Named.Data = AmlOpStart;
355         Op->Named.Length = 0;
356     }
357
358     ParentScope = AcpiPsGetParentScope (&(WalkState->ParserState));
359     AcpiPsAppendArg (ParentScope, Op);
360
361     if (ParentScope)
362     {
363         OpInfo = AcpiPsGetOpcodeInfo (ParentScope->Common.AmlOpcode);
364         if (OpInfo->Flags & AML_HAS_TARGET)
365         {
366             ArgumentCount = AcpiPsGetArgumentCount (OpInfo->Type);
367             if (ParentScope->Common.ArgListLength > ArgumentCount)
368             {
369                 Op->Common.Flags |= ACPI_PARSEOP_TARGET;
370             }
371         }
372         else if (ParentScope->Common.AmlOpcode == AML_INCREMENT_OP)
373         {
374             Op->Common.Flags |= ACPI_PARSEOP_TARGET;
375         }
376     }
377
378     if (WalkState->DescendingCallback != NULL)
379     {
380         /*
381          * Find the object. This will either insert the object into
382          * the namespace or simply look it up
383          */
384         WalkState->Op = *NewOp = Op;
385
386         Status = WalkState->DescendingCallback (WalkState, &Op);
387         Status = AcpiPsNextParseState (WalkState, Op, Status);
388         if (Status == AE_CTRL_PENDING)
389         {
390             Status = AE_CTRL_PARSE_PENDING;
391         }
392     }
393
394     return_ACPI_STATUS (Status);
395 }
396
397
398 /*******************************************************************************
399  *
400  * FUNCTION:    AcpiPsCompleteOp
401  *
402  * PARAMETERS:  WalkState           - Current state
403  *              Op                  - Returned Op
404  *              Status              - Parse status before complete Op
405  *
406  * RETURN:      Status
407  *
408  * DESCRIPTION: Complete Op
409  *
410  ******************************************************************************/
411
412 ACPI_STATUS
413 AcpiPsCompleteOp (
414     ACPI_WALK_STATE         *WalkState,
415     ACPI_PARSE_OBJECT       **Op,
416     ACPI_STATUS             Status)
417 {
418     ACPI_STATUS             Status2;
419
420
421     ACPI_FUNCTION_TRACE_PTR (PsCompleteOp, WalkState);
422
423
424     /*
425      * Finished one argument of the containing scope
426      */
427     WalkState->ParserState.Scope->ParseScope.ArgCount--;
428
429     /* Close this Op (will result in parse subtree deletion) */
430
431     Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
432     if (ACPI_FAILURE (Status2))
433     {
434         return_ACPI_STATUS (Status2);
435     }
436
437     *Op = NULL;
438
439     switch (Status)
440     {
441     case AE_OK:
442
443         break;
444
445     case AE_CTRL_TRANSFER:
446
447         /* We are about to transfer to a called method */
448
449         WalkState->PrevOp = NULL;
450         WalkState->PrevArgTypes = WalkState->ArgTypes;
451         return_ACPI_STATUS (Status);
452
453     case AE_CTRL_END:
454
455         AcpiPsPopScope (&(WalkState->ParserState), Op,
456             &WalkState->ArgTypes, &WalkState->ArgCount);
457
458         if (*Op)
459         {
460             WalkState->Op = *Op;
461             WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
462             WalkState->Opcode = (*Op)->Common.AmlOpcode;
463
464             Status = WalkState->AscendingCallback (WalkState);
465             Status = AcpiPsNextParseState (WalkState, *Op, Status);
466
467             Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
468             if (ACPI_FAILURE (Status2))
469             {
470                 return_ACPI_STATUS (Status2);
471             }
472         }
473
474         Status = AE_OK;
475         break;
476
477     case AE_CTRL_BREAK:
478     case AE_CTRL_CONTINUE:
479
480         /* Pop off scopes until we find the While */
481
482         while (!(*Op) || ((*Op)->Common.AmlOpcode != AML_WHILE_OP))
483         {
484             AcpiPsPopScope (&(WalkState->ParserState), Op,
485                 &WalkState->ArgTypes, &WalkState->ArgCount);
486         }
487
488         /* Close this iteration of the While loop */
489
490         WalkState->Op = *Op;
491         WalkState->OpInfo = AcpiPsGetOpcodeInfo ((*Op)->Common.AmlOpcode);
492         WalkState->Opcode = (*Op)->Common.AmlOpcode;
493
494         Status = WalkState->AscendingCallback (WalkState);
495         Status = AcpiPsNextParseState (WalkState, *Op, Status);
496
497         Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
498         if (ACPI_FAILURE (Status2))
499         {
500             return_ACPI_STATUS (Status2);
501         }
502
503         Status = AE_OK;
504         break;
505
506     case AE_CTRL_TERMINATE:
507
508         /* Clean up */
509         do
510         {
511             if (*Op)
512             {
513                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
514                 if (ACPI_FAILURE (Status2))
515                 {
516                     return_ACPI_STATUS (Status2);
517                 }
518
519                 AcpiUtDeleteGenericState (
520                     AcpiUtPopGenericState (&WalkState->ControlState));
521             }
522
523             AcpiPsPopScope (&(WalkState->ParserState), Op,
524                 &WalkState->ArgTypes, &WalkState->ArgCount);
525
526         } while (*Op);
527
528         return_ACPI_STATUS (AE_OK);
529
530     default:  /* All other non-AE_OK status */
531
532         do
533         {
534             if (*Op)
535             {
536                 Status2 = AcpiPsCompleteThisOp (WalkState, *Op);
537                 if (ACPI_FAILURE (Status2))
538                 {
539                     return_ACPI_STATUS (Status2);
540                 }
541             }
542
543             AcpiPsPopScope (&(WalkState->ParserState), Op,
544                 &WalkState->ArgTypes, &WalkState->ArgCount);
545
546         } while (*Op);
547
548
549 #if 0
550         /*
551          * TBD: Cleanup parse ops on error
552          */
553         if (*Op == NULL)
554         {
555             AcpiPsPopScope (ParserState, Op,
556                 &WalkState->ArgTypes, &WalkState->ArgCount);
557         }
558 #endif
559         WalkState->PrevOp = NULL;
560         WalkState->PrevArgTypes = WalkState->ArgTypes;
561         return_ACPI_STATUS (Status);
562     }
563
564     /* This scope complete? */
565
566     if (AcpiPsHasCompletedScope (&(WalkState->ParserState)))
567     {
568         AcpiPsPopScope (&(WalkState->ParserState), Op,
569             &WalkState->ArgTypes, &WalkState->ArgCount);
570         ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *Op));
571     }
572     else
573     {
574         *Op = NULL;
575     }
576
577     return_ACPI_STATUS (AE_OK);
578 }
579
580
581 /*******************************************************************************
582  *
583  * FUNCTION:    AcpiPsCompleteFinalOp
584  *
585  * PARAMETERS:  WalkState           - Current state
586  *              Op                  - Current Op
587  *              Status              - Current parse status before complete last
588  *                                    Op
589  *
590  * RETURN:      Status
591  *
592  * DESCRIPTION: Complete last Op.
593  *
594  ******************************************************************************/
595
596 ACPI_STATUS
597 AcpiPsCompleteFinalOp (
598     ACPI_WALK_STATE         *WalkState,
599     ACPI_PARSE_OBJECT       *Op,
600     ACPI_STATUS             Status)
601 {
602     ACPI_STATUS             Status2;
603
604
605     ACPI_FUNCTION_TRACE_PTR (PsCompleteFinalOp, WalkState);
606
607
608     /*
609      * Complete the last Op (if not completed), and clear the scope stack.
610      * It is easily possible to end an AML "package" with an unbounded number
611      * of open scopes (such as when several ASL blocks are closed with
612      * sequential closing braces). We want to terminate each one cleanly.
613      */
614     ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "AML package complete at Op %p\n", Op));
615     do
616     {
617         if (Op)
618         {
619             if (WalkState->AscendingCallback != NULL)
620             {
621                 WalkState->Op = Op;
622                 WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
623                 WalkState->Opcode = Op->Common.AmlOpcode;
624
625                 Status = WalkState->AscendingCallback (WalkState);
626                 Status = AcpiPsNextParseState (WalkState, Op, Status);
627                 if (Status == AE_CTRL_PENDING)
628                 {
629                     Status = AcpiPsCompleteOp (WalkState, &Op, AE_OK);
630                     if (ACPI_FAILURE (Status))
631                     {
632                         return_ACPI_STATUS (Status);
633                     }
634                 }
635
636                 if (Status == AE_CTRL_TERMINATE)
637                 {
638                     Status = AE_OK;
639
640                     /* Clean up */
641                     do
642                     {
643                         if (Op)
644                         {
645                             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
646                             if (ACPI_FAILURE (Status2))
647                             {
648                                 return_ACPI_STATUS (Status2);
649                             }
650                         }
651
652                         AcpiPsPopScope (&(WalkState->ParserState), &Op,
653                             &WalkState->ArgTypes, &WalkState->ArgCount);
654
655                     } while (Op);
656
657                     return_ACPI_STATUS (Status);
658                 }
659
660                 else if (ACPI_FAILURE (Status))
661                 {
662                     /* First error is most important */
663
664                     (void) AcpiPsCompleteThisOp (WalkState, Op);
665                     return_ACPI_STATUS (Status);
666                 }
667             }
668
669             Status2 = AcpiPsCompleteThisOp (WalkState, Op);
670             if (ACPI_FAILURE (Status2))
671             {
672                 return_ACPI_STATUS (Status2);
673             }
674         }
675
676         AcpiPsPopScope (&(WalkState->ParserState), &Op, &WalkState->ArgTypes,
677             &WalkState->ArgCount);
678
679     } while (Op);
680
681     return_ACPI_STATUS (Status);
682 }