1 /******************************************************************************
3 * Module Name: dtutils.c - Utility routines for the data table compiler
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include <contrib/dev/acpica/compiler/dtcompiler.h>
154 #include <contrib/dev/acpica/include/actables.h>
156 #define _COMPONENT DT_COMPILER
157 ACPI_MODULE_NAME ("dtutils")
159 /* Local prototypes */
163 DT_SUBTABLE *Subtable,
168 /******************************************************************************
172 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
173 * MessageId - Index into global message buffer
174 * Op - Parse node where error happened
175 * ExtraMessage - additional error message
179 * DESCRIPTION: Common error interface for data table compiler
181 *****************************************************************************/
187 DT_FIELD *FieldObject,
191 /* Check if user wants to ignore this exception */
193 if (AslIsExceptionDisabled (Level, MessageId))
200 AslCommonError (Level, MessageId,
203 FieldObject->ByteOffset,
205 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
209 AslCommonError (Level, MessageId, 0,
210 0, 0, 0, 0, ExtraMessage);
215 /******************************************************************************
217 * FUNCTION: DtNameError
219 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
220 * MessageId - Index into global message buffer
221 * Op - Parse node where error happened
222 * ExtraMessage - additional error message
226 * DESCRIPTION: Error interface for named objects
228 *****************************************************************************/
234 DT_FIELD *FieldObject,
243 if (Gbl_WarningLevel < Level)
256 AslCommonError (Level, MessageId,
259 FieldObject->ByteOffset,
260 FieldObject->NameColumn,
261 Gbl_Files[ASL_FILE_INPUT].Filename, ExtraMessage);
265 AslCommonError (Level, MessageId, 0,
266 0, 0, 0, 0, ExtraMessage);
271 /*******************************************************************************
279 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
280 * compile or I/O errors
282 ******************************************************************************/
287 DT_FIELD *FieldObject,
291 DtError (ASL_ERROR, MessageId, FieldObject, ExtraMessage);
294 * TBD: remove this entire function, DtFatal
296 * We cannot abort the compiler on error, because we may be compiling a
297 * list of files. We must move on to the next file.
306 /******************************************************************************
308 * FUNCTION: DtGetFieldValue
310 * PARAMETERS: Field - Current field list pointer
312 * RETURN: Field value
314 * DESCRIPTION: Get field value
316 *****************************************************************************/
327 return (Field->Value);
331 /******************************************************************************
333 * FUNCTION: DtGetFieldType
335 * PARAMETERS: Info - Data table info
339 * DESCRIPTION: Get field type
341 *****************************************************************************/
345 ACPI_DMTABLE_INFO *Info)
350 /* DT_FLAG means that this is the start of a block of flag bits */
351 /* TBD - we can make these a separate opcode later */
353 if (Info->Flags & DT_FLAG)
355 return (DT_FIELD_TYPE_FLAGS_INTEGER);
358 /* Type is based upon the opcode for this field in the info table */
360 switch (Info->Opcode)
370 case ACPI_DMT_FLAGS0:
371 case ACPI_DMT_FLAGS1:
372 case ACPI_DMT_FLAGS2:
373 case ACPI_DMT_FLAGS4:
375 Type = DT_FIELD_TYPE_FLAG;
382 case ACPI_DMT_STRING:
384 Type = DT_FIELD_TYPE_STRING;
387 case ACPI_DMT_BUFFER:
388 case ACPI_DMT_RAW_BUFFER:
393 case ACPI_DMT_BUF128:
394 case ACPI_DMT_PCI_PATH:
396 Type = DT_FIELD_TYPE_BUFFER;
400 case ACPI_DMT_HESTNTFY:
401 case ACPI_DMT_IORTMEM:
403 Type = DT_FIELD_TYPE_INLINE_SUBTABLE;
406 case ACPI_DMT_UNICODE:
408 Type = DT_FIELD_TYPE_UNICODE;
413 Type = DT_FIELD_TYPE_UUID;
416 case ACPI_DMT_DEVICE_PATH:
418 Type = DT_FIELD_TYPE_DEVICE_PATH;
423 Type = DT_FIELD_TYPE_LABEL;
428 Type = DT_FIELD_TYPE_INTEGER;
436 /******************************************************************************
438 * FUNCTION: DtGetBufferLength
440 * PARAMETERS: Buffer - List of integers,
441 * for example "10 3A 4F 2E"
443 * RETURN: Count of integer
445 * DESCRIPTION: Get length of bytes needed to store the integers
447 *****************************************************************************/
453 UINT32 ByteLength = 0;
462 while (*Buffer == ' ')
471 return (++ByteLength);
475 /******************************************************************************
477 * FUNCTION: DtGetFieldLength
479 * PARAMETERS: Field - Current field
480 * Info - Data table info
482 * RETURN: Field length
484 * DESCRIPTION: Get length of bytes needed to compile the field
486 * Note: This function must remain in sync with AcpiDmDumpTable.
488 *****************************************************************************/
493 ACPI_DMTABLE_INFO *Info)
495 UINT32 ByteLength = 0;
499 /* Length is based upon the opcode for this field in the info table */
501 switch (Info->Opcode)
511 case ACPI_DMT_FLAGS0:
512 case ACPI_DMT_FLAGS1:
513 case ACPI_DMT_FLAGS2:
514 case ACPI_DMT_FLAGS4:
516 case ACPI_DMT_EXTRA_TEXT:
522 case ACPI_DMT_CHKSUM:
523 case ACPI_DMT_SPACEID:
524 case ACPI_DMT_ACCWIDTH:
532 case ACPI_DMT_HESTNTYP:
533 case ACPI_DMT_FADTPM:
534 case ACPI_DMT_EINJACT:
535 case ACPI_DMT_EINJINST:
536 case ACPI_DMT_ERSTACT:
537 case ACPI_DMT_ERSTINST:
538 case ACPI_DMT_DMAR_SCOPE:
543 case ACPI_DMT_UINT16:
547 case ACPI_DMT_PCI_PATH:
552 case ACPI_DMT_UINT24:
557 case ACPI_DMT_UINT32:
565 case ACPI_DMT_UINT40:
570 case ACPI_DMT_UINT48:
576 case ACPI_DMT_UINT56:
582 case ACPI_DMT_UINT64:
588 case ACPI_DMT_STRING:
590 Value = DtGetFieldValue (Field);
593 ByteLength = strlen (Value) + 1;
596 { /* At this point, this is a fatal error */
598 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
599 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
606 ByteLength = sizeof (ACPI_GENERIC_ADDRESS);
609 case ACPI_DMT_HESTNTFY:
611 ByteLength = sizeof (ACPI_HEST_NOTIFY);
614 case ACPI_DMT_IORTMEM:
616 ByteLength = sizeof (ACPI_IORT_MEMORY_ACCESS);
619 case ACPI_DMT_BUFFER:
620 case ACPI_DMT_RAW_BUFFER:
622 Value = DtGetFieldValue (Field);
625 ByteLength = DtGetBufferLength (Value);
628 { /* At this point, this is a fatal error */
630 sprintf (MsgBuffer, "Expected \"%s\"", Info->Name);
631 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
652 case ACPI_DMT_BUF128:
657 case ACPI_DMT_UNICODE:
659 Value = DtGetFieldValue (Field);
661 /* TBD: error if Value is NULL? (as below?) */
663 ByteLength = (strlen (Value) + 1) * sizeof(UINT16);
668 DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid table opcode");
676 /******************************************************************************
680 * PARAMETERS: DT_WALK_CALLBACK:
681 * Subtable - Subtable
683 * ReturnValue - Store the checksum of subtable
687 * DESCRIPTION: Get the checksum of subtable
689 *****************************************************************************/
693 DT_SUBTABLE *Subtable,
698 UINT8 *Sum = ReturnValue;
701 Checksum = AcpiTbChecksum (Subtable->Buffer, Subtable->Length);
702 *Sum = (UINT8) (*Sum + Checksum);
706 /******************************************************************************
708 * FUNCTION: DtSetTableChecksum
710 * PARAMETERS: ChecksumPointer - Where to return the checksum
714 * DESCRIPTION: Set checksum of the whole data table into the checksum field
716 *****************************************************************************/
720 UINT8 *ChecksumPointer)
726 DtWalkTableTree (Gbl_RootTable, DtSum, NULL, &Checksum);
728 OldSum = *ChecksumPointer;
729 Checksum = (UINT8) (Checksum - OldSum);
731 /* Compute the final checksum */
733 Checksum = (UINT8) (0 - Checksum);
734 *ChecksumPointer = Checksum;
738 /******************************************************************************
740 * FUNCTION: DtSetTableLength
746 * DESCRIPTION: Walk the subtables and set all the length fields
748 *****************************************************************************/
754 DT_SUBTABLE *ParentTable;
755 DT_SUBTABLE *ChildTable;
758 ParentTable = Gbl_RootTable;
766 DtSetSubtableLength (ParentTable);
770 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
773 if (ChildTable->LengthField)
775 DtSetSubtableLength (ChildTable);
778 if (ChildTable->Child)
780 ParentTable = ChildTable;
785 ParentTable->TotalLength += ChildTable->TotalLength;
786 if (ParentTable->LengthField)
788 DtSetSubtableLength (ParentTable);
794 ChildTable = ParentTable;
796 if (ChildTable == Gbl_RootTable)
801 ParentTable = DtGetParentSubtable (ParentTable);
803 ParentTable->TotalLength += ChildTable->TotalLength;
804 if (ParentTable->LengthField)
806 DtSetSubtableLength (ParentTable);
813 /******************************************************************************
815 * FUNCTION: DtWalkTableTree
817 * PARAMETERS: StartTable - Subtable in the tree where walking begins
818 * UserFunction - Called during the walk
819 * Context - Passed to user function
820 * ReturnValue - The return value of UserFunction
824 * DESCRIPTION: Performs a depth-first walk of the subtable tree
826 *****************************************************************************/
830 DT_SUBTABLE *StartTable,
831 DT_WALK_CALLBACK UserFunction,
835 DT_SUBTABLE *ParentTable;
836 DT_SUBTABLE *ChildTable;
839 ParentTable = StartTable;
847 UserFunction (ParentTable, Context, ReturnValue);
851 ChildTable = DtGetNextSubtable (ParentTable, ChildTable);
854 UserFunction (ChildTable, Context, ReturnValue);
856 if (ChildTable->Child)
858 ParentTable = ChildTable;
864 ChildTable = ParentTable;
865 if (ChildTable == Gbl_RootTable)
870 ParentTable = DtGetParentSubtable (ParentTable);
872 if (ChildTable->Peer == StartTable)
881 /*******************************************************************************
883 * FUNCTION: UtSubtableCacheCalloc
887 * RETURN: Pointer to the buffer. Aborts on allocation failure
889 * DESCRIPTION: Allocate a subtable object buffer. Bypass the local
890 * dynamic memory manager for performance reasons (This has a
891 * major impact on the speed of the compiler.)
893 ******************************************************************************/
896 UtSubtableCacheCalloc (
899 ASL_CACHE_INFO *Cache;
902 if (Gbl_SubtableCacheNext >= Gbl_SubtableCacheLast)
904 /* Allocate a new buffer */
906 Cache = UtLocalCalloc (sizeof (Cache->Next) +
907 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE));
909 /* Link new cache buffer to head of list */
911 Cache->Next = Gbl_SubtableCacheList;
912 Gbl_SubtableCacheList = Cache;
914 /* Setup cache management pointers */
916 Gbl_SubtableCacheNext = ACPI_CAST_PTR (DT_SUBTABLE, Cache->Buffer);
917 Gbl_SubtableCacheLast = Gbl_SubtableCacheNext + ASL_SUBTABLE_CACHE_SIZE;
921 return (Gbl_SubtableCacheNext++);
925 /*******************************************************************************
927 * FUNCTION: UtFieldCacheCalloc
931 * RETURN: Pointer to the buffer. Aborts on allocation failure
933 * DESCRIPTION: Allocate a field object buffer. Bypass the local
934 * dynamic memory manager for performance reasons (This has a
935 * major impact on the speed of the compiler.)
937 ******************************************************************************/
943 ASL_CACHE_INFO *Cache;
946 if (Gbl_FieldCacheNext >= Gbl_FieldCacheLast)
948 /* Allocate a new buffer */
950 Cache = UtLocalCalloc (sizeof (Cache->Next) +
951 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE));
953 /* Link new cache buffer to head of list */
955 Cache->Next = Gbl_FieldCacheList;
956 Gbl_FieldCacheList = Cache;
958 /* Setup cache management pointers */
960 Gbl_FieldCacheNext = ACPI_CAST_PTR (DT_FIELD, Cache->Buffer);
961 Gbl_FieldCacheLast = Gbl_FieldCacheNext + ASL_FIELD_CACHE_SIZE;
965 return (Gbl_FieldCacheNext++);
969 /*******************************************************************************
971 * FUNCTION: DtDeleteCaches
977 * DESCRIPTION: Delete all local cache buffer blocks
979 ******************************************************************************/
986 ASL_CACHE_INFO *Next;
992 while (Gbl_FieldCacheList)
994 Next = Gbl_FieldCacheList->Next;
995 ACPI_FREE (Gbl_FieldCacheList);
996 Gbl_FieldCacheList = Next;
1000 DbgPrint (ASL_DEBUG_OUTPUT,
1001 "%u Fields, Buffer size: %u fields (%u bytes), %u Buffers\n",
1002 Gbl_FieldCount, ASL_FIELD_CACHE_SIZE,
1003 (sizeof (DT_FIELD) * ASL_FIELD_CACHE_SIZE), BufferCount);
1006 Gbl_FieldCacheNext = NULL;
1007 Gbl_FieldCacheLast = NULL;
1009 /* Subtable cache */
1012 while (Gbl_SubtableCacheList)
1014 Next = Gbl_SubtableCacheList->Next;
1015 ACPI_FREE (Gbl_SubtableCacheList);
1016 Gbl_SubtableCacheList = Next;
1020 DbgPrint (ASL_DEBUG_OUTPUT,
1021 "%u Subtables, Buffer size: %u subtables (%u bytes), %u Buffers\n",
1022 Gbl_SubtableCount, ASL_SUBTABLE_CACHE_SIZE,
1023 (sizeof (DT_SUBTABLE) * ASL_SUBTABLE_CACHE_SIZE), BufferCount);
1025 Gbl_SubtableCount = 0;
1026 Gbl_SubtableCacheNext = NULL;
1027 Gbl_SubtableCacheLast = NULL;