1 /******************************************************************************
3 * Module Name: exutils - interpreter/scanner utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 * DEFINE_AML_GLOBALS is tested in amlcode.h
46 * to determine whether certain global names should be "defined" or only
47 * "declared" in the current compilation. This enhances maintainability
48 * by enabling a single header file to embody all knowledge of the names
51 * Exactly one module of any executable should #define DEFINE_GLOBALS
52 * before #including the header files which use this convention. The
53 * names in question will be defined and initialized in that module,
54 * and declared as extern in all other modules which #include those
58 #define DEFINE_AML_GLOBALS
65 #define _COMPONENT ACPI_EXECUTER
66 ACPI_MODULE_NAME ("exutils")
68 /* Local prototypes */
76 #ifndef ACPI_NO_METHOD_EXECUTION
77 /*******************************************************************************
79 * FUNCTION: AcpiExEnterInterpreter
85 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
86 * the interpreter region is a fatal system error. Used in
87 * conjunction with ExitInterpreter.
89 ******************************************************************************/
92 AcpiExEnterInterpreter (
98 ACPI_FUNCTION_TRACE (ExEnterInterpreter);
101 Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
102 if (ACPI_FAILURE (Status))
104 ACPI_ERROR ((AE_INFO, "Could not acquire AML Interpreter mutex"));
111 /*******************************************************************************
113 * FUNCTION: AcpiExExitInterpreter
119 * DESCRIPTION: Exit the interpreter execution region. This is the top level
120 * routine used to exit the interpreter when all processing has
121 * been completed, or when the method blocks.
123 * Cases where the interpreter is unlocked internally:
124 * 1) Method will be blocked on a Sleep() AML opcode
125 * 2) Method will be blocked on an Acquire() AML opcode
126 * 3) Method will be blocked on a Wait() AML opcode
127 * 4) Method will be blocked to acquire the global lock
128 * 5) Method will be blocked waiting to execute a serialized control
129 * method that is currently executing
130 * 6) About to invoke a user-installed opregion handler
132 ******************************************************************************/
135 AcpiExExitInterpreter (
141 ACPI_FUNCTION_TRACE (ExExitInterpreter);
144 Status = AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
145 if (ACPI_FAILURE (Status))
147 ACPI_ERROR ((AE_INFO, "Could not release AML Interpreter mutex"));
154 /*******************************************************************************
156 * FUNCTION: AcpiExTruncateFor32bitTable
158 * PARAMETERS: ObjDesc - Object to be truncated
160 * RETURN: TRUE if a truncation was performed, FALSE otherwise.
162 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
163 * 32-bit, as determined by the revision of the DSDT.
165 ******************************************************************************/
168 AcpiExTruncateFor32bitTable (
169 ACPI_OPERAND_OBJECT *ObjDesc)
172 ACPI_FUNCTION_ENTRY ();
176 * Object must be a valid number and we must be executing
177 * a control method. Object could be NS node for AML_INT_NAMEPATH_OP.
180 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) != ACPI_DESC_TYPE_OPERAND) ||
181 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
186 if ((AcpiGbl_IntegerByteWidth == 4) &&
187 (ObjDesc->Integer.Value > (UINT64) ACPI_UINT32_MAX))
190 * We are executing in a 32-bit ACPI table.
191 * Truncate the value to 32 bits by zeroing out the upper 32-bit field
193 ObjDesc->Integer.Value &= (UINT64) ACPI_UINT32_MAX;
201 /*******************************************************************************
203 * FUNCTION: AcpiExAcquireGlobalLock
205 * PARAMETERS: FieldFlags - Flags with Lock rule:
206 * AlwaysLock or NeverLock
210 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
211 * flags specifiy that it is to be obtained before field access.
213 ******************************************************************************/
216 AcpiExAcquireGlobalLock (
222 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock);
225 /* Only use the lock if the AlwaysLock bit is set */
227 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
232 /* Attempt to get the global lock, wait forever */
234 Status = AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER,
235 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
237 if (ACPI_FAILURE (Status))
239 ACPI_EXCEPTION ((AE_INFO, Status,
240 "Could not acquire Global Lock"));
247 /*******************************************************************************
249 * FUNCTION: AcpiExReleaseGlobalLock
251 * PARAMETERS: FieldFlags - Flags with Lock rule:
252 * AlwaysLock or NeverLock
256 * DESCRIPTION: Release the ACPI hardware Global Lock
258 ******************************************************************************/
261 AcpiExReleaseGlobalLock (
267 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock);
270 /* Only use the lock if the AlwaysLock bit is set */
272 if (!(FieldFlags & AML_FIELD_LOCK_RULE_MASK))
277 /* Release the global lock */
279 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
280 if (ACPI_FAILURE (Status))
282 /* Report the error, but there isn't much else we can do */
284 ACPI_EXCEPTION ((AE_INFO, Status,
285 "Could not release Global Lock"));
292 /*******************************************************************************
294 * FUNCTION: AcpiExDigitsNeeded
296 * PARAMETERS: Value - Value to be represented
297 * Base - Base of representation
299 * RETURN: The number of digits.
301 * DESCRIPTION: Calculate the number of digits needed to represent the Value
302 * in the given Base (Radix)
304 ******************************************************************************/
315 ACPI_FUNCTION_TRACE (ExDigitsNeeded);
318 /* UINT64 is unsigned, so we don't worry about a '-' prefix */
325 CurrentValue = Value;
328 /* Count the digits in the requested base */
332 (void) AcpiUtShortDivide (CurrentValue, Base, &CurrentValue, NULL);
336 return_UINT32 (NumDigits);
340 /*******************************************************************************
342 * FUNCTION: AcpiExEisaIdToString
344 * PARAMETERS: CompressedId - EISAID to be converted
345 * OutString - Where to put the converted string (8 bytes)
349 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
350 * buffer must be large enough to hold the string. The string
351 * returned is always exactly of length ACPI_EISAID_STRING_SIZE
352 * (includes null terminator). The EISAID is always 32 bits.
354 ******************************************************************************/
357 AcpiExEisaIdToString (
364 ACPI_FUNCTION_ENTRY ();
367 /* The EISAID should be a 32-bit integer */
369 if (CompressedId > ACPI_UINT32_MAX)
371 ACPI_WARNING ((AE_INFO,
372 "Expected EISAID is larger than 32 bits: 0x%8.8X%8.8X, truncating",
373 ACPI_FORMAT_UINT64 (CompressedId)));
376 /* Swap ID to big-endian to get contiguous bits */
378 SwappedId = AcpiUtDwordByteSwap ((UINT32) CompressedId);
380 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
382 OutString[0] = (char) (0x40 + (((unsigned long) SwappedId >> 26) & 0x1F));
383 OutString[1] = (char) (0x40 + ((SwappedId >> 21) & 0x1F));
384 OutString[2] = (char) (0x40 + ((SwappedId >> 16) & 0x1F));
385 OutString[3] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 12);
386 OutString[4] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 8);
387 OutString[5] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 4);
388 OutString[6] = AcpiUtHexToAsciiChar ((UINT64) SwappedId, 0);
393 /*******************************************************************************
395 * FUNCTION: AcpiExIntegerToString
397 * PARAMETERS: OutString - Where to put the converted string. At least
398 * 21 bytes are needed to hold the largest
399 * possible 64-bit integer.
400 * Value - Value to be converted
402 * RETURN: None, string
404 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
405 * Assumes string buffer is large enough to hold the string. The
406 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
408 ******************************************************************************/
411 AcpiExIntegerToString (
420 ACPI_FUNCTION_ENTRY ();
423 DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
424 OutString[DigitsNeeded] = 0;
426 for (Count = DigitsNeeded; Count > 0; Count--)
428 (void) AcpiUtShortDivide (Value, 10, &Value, &Remainder);
429 OutString[Count-1] = (char) ('0' + Remainder);\
434 /*******************************************************************************
436 * FUNCTION: AcpiIsValidSpaceId
438 * PARAMETERS: SpaceId - ID to be validated
440 * RETURN: TRUE if valid/supported ID.
442 * DESCRIPTION: Validate an operation region SpaceID.
444 ******************************************************************************/
451 if ((SpaceId >= ACPI_NUM_PREDEFINED_REGIONS) &&
452 (SpaceId < ACPI_USER_REGION_BEGIN) &&
453 (SpaceId != ACPI_ADR_SPACE_DATA_TABLE) &&
454 (SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE))