2 /******************************************************************************
4 * Module Name: exutils - interpreter/scanner utilities
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #define __EXUTILS_C__
120 * DEFINE_AML_GLOBALS is tested in amlcode.h
121 * to determine whether certain global names should be "defined" or only
122 * "declared" in the current compilation. This enhances maintainability
123 * by enabling a single header file to embody all knowledge of the names
126 * Exactly one module of any executable should #define DEFINE_GLOBALS
127 * before #including the header files which use this convention. The
128 * names in question will be defined and initialized in that module,
129 * and declared as extern in all other modules which #include those
133 #define DEFINE_AML_GLOBALS
136 #include "accommon.h"
137 #include "acinterp.h"
140 #define _COMPONENT ACPI_EXECUTER
141 ACPI_MODULE_NAME ("exutils")
143 /* Local prototypes */
151 #ifndef ACPI_NO_METHOD_EXECUTION
152 /*******************************************************************************
154 * FUNCTION: AcpiExEnterInterpreter
160 * DESCRIPTION: Enter the interpreter execution region. Failure to enter
161 * the interpreter region is a fatal system error. Used in
162 * conjunction with ExitInterpreter.
164 ******************************************************************************/
167 AcpiExEnterInterpreter (
173 ACPI_FUNCTION_TRACE (ExEnterInterpreter
);
176 Status
= AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER
);
177 if (ACPI_FAILURE (Status
))
179 ACPI_ERROR ((AE_INFO
, "Could not acquire AML Interpreter mutex"));
186 /*******************************************************************************
188 * FUNCTION: AcpiExReacquireInterpreter
194 * DESCRIPTION: Reacquire the interpreter execution region from within the
195 * interpreter code. Failure to enter the interpreter region is a
196 * fatal system error. Used in conjuction with
197 * RelinquishInterpreter
199 ******************************************************************************/
202 AcpiExReacquireInterpreter (
205 ACPI_FUNCTION_TRACE (ExReacquireInterpreter
);
209 * If the global serialized flag is set, do not release the interpreter,
210 * since it was not actually released by AcpiExRelinquishInterpreter.
211 * This forces the interpreter to be single threaded.
213 if (!AcpiGbl_AllMethodsSerialized
)
215 AcpiExEnterInterpreter ();
222 /*******************************************************************************
224 * FUNCTION: AcpiExExitInterpreter
230 * DESCRIPTION: Exit the interpreter execution region. This is the top level
231 * routine used to exit the interpreter when all processing has
234 ******************************************************************************/
237 AcpiExExitInterpreter (
243 ACPI_FUNCTION_TRACE (ExExitInterpreter
);
246 Status
= AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER
);
247 if (ACPI_FAILURE (Status
))
249 ACPI_ERROR ((AE_INFO
, "Could not release AML Interpreter mutex"));
256 /*******************************************************************************
258 * FUNCTION: AcpiExRelinquishInterpreter
264 * DESCRIPTION: Exit the interpreter execution region, from within the
265 * interpreter - before attempting an operation that will possibly
266 * block the running thread.
268 * Cases where the interpreter is unlocked internally
269 * 1) Method to be blocked on a Sleep() AML opcode
270 * 2) Method to be blocked on an Acquire() AML opcode
271 * 3) Method to be blocked on a Wait() AML opcode
272 * 4) Method to be blocked to acquire the global lock
273 * 5) Method to be blocked waiting to execute a serialized control method
274 * that is currently executing
275 * 6) About to invoke a user-installed opregion handler
277 ******************************************************************************/
280 AcpiExRelinquishInterpreter (
283 ACPI_FUNCTION_TRACE (ExRelinquishInterpreter
);
287 * If the global serialized flag is set, do not release the interpreter.
288 * This forces the interpreter to be single threaded.
290 if (!AcpiGbl_AllMethodsSerialized
)
292 AcpiExExitInterpreter ();
299 /*******************************************************************************
301 * FUNCTION: AcpiExTruncateFor32bitTable
303 * PARAMETERS: ObjDesc - Object to be truncated
307 * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
308 * 32-bit, as determined by the revision of the DSDT.
310 ******************************************************************************/
313 AcpiExTruncateFor32bitTable (
314 ACPI_OPERAND_OBJECT
*ObjDesc
)
317 ACPI_FUNCTION_ENTRY ();
321 * Object must be a valid number and we must be executing
322 * a control method. NS node could be there for AML_INT_NAMEPATH_OP.
325 (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc
) != ACPI_DESC_TYPE_OPERAND
) ||
326 (ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
))
331 if (AcpiGbl_IntegerByteWidth
== 4)
334 * We are running a method that exists in a 32-bit ACPI table.
335 * Truncate the value to 32 bits by zeroing out the upper 32-bit field
337 ObjDesc
->Integer
.Value
&= (UINT64
) ACPI_UINT32_MAX
;
342 /*******************************************************************************
344 * FUNCTION: AcpiExAcquireGlobalLock
346 * PARAMETERS: FieldFlags - Flags with Lock rule:
347 * AlwaysLock or NeverLock
351 * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
352 * flags specifiy that it is to be obtained before field access.
354 ******************************************************************************/
357 AcpiExAcquireGlobalLock (
363 ACPI_FUNCTION_TRACE (ExAcquireGlobalLock
);
366 /* Only use the lock if the AlwaysLock bit is set */
368 if (!(FieldFlags
& AML_FIELD_LOCK_RULE_MASK
))
373 /* Attempt to get the global lock, wait forever */
375 Status
= AcpiExAcquireMutexObject (ACPI_WAIT_FOREVER
,
376 AcpiGbl_GlobalLockMutex
, AcpiOsGetThreadId ());
378 if (ACPI_FAILURE (Status
))
380 ACPI_EXCEPTION ((AE_INFO
, Status
,
381 "Could not acquire Global Lock"));
388 /*******************************************************************************
390 * FUNCTION: AcpiExReleaseGlobalLock
392 * PARAMETERS: FieldFlags - Flags with Lock rule:
393 * AlwaysLock or NeverLock
397 * DESCRIPTION: Release the ACPI hardware Global Lock
399 ******************************************************************************/
402 AcpiExReleaseGlobalLock (
408 ACPI_FUNCTION_TRACE (ExReleaseGlobalLock
);
411 /* Only use the lock if the AlwaysLock bit is set */
413 if (!(FieldFlags
& AML_FIELD_LOCK_RULE_MASK
))
418 /* Release the global lock */
420 Status
= AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex
);
421 if (ACPI_FAILURE (Status
))
423 /* Report the error, but there isn't much else we can do */
425 ACPI_EXCEPTION ((AE_INFO
, Status
,
426 "Could not release Global Lock"));
433 /*******************************************************************************
435 * FUNCTION: AcpiExDigitsNeeded
437 * PARAMETERS: Value - Value to be represented
438 * Base - Base of representation
440 * RETURN: The number of digits.
442 * DESCRIPTION: Calculate the number of digits needed to represent the Value
443 * in the given Base (Radix)
445 ******************************************************************************/
456 ACPI_FUNCTION_TRACE (ExDigitsNeeded
);
459 /* UINT64 is unsigned, so we don't worry about a '-' prefix */
466 CurrentValue
= Value
;
469 /* Count the digits in the requested base */
473 (void) AcpiUtShortDivide (CurrentValue
, Base
, &CurrentValue
, NULL
);
477 return_UINT32 (NumDigits
);
481 /*******************************************************************************
483 * FUNCTION: AcpiExEisaIdToString
485 * PARAMETERS: CompressedId - EISAID to be converted
486 * OutString - Where to put the converted string (8 bytes)
490 * DESCRIPTION: Convert a numeric EISAID to string representation. Return
491 * buffer must be large enough to hold the string. The string
492 * returned is always exactly of length ACPI_EISAID_STRING_SIZE
493 * (includes null terminator). The EISAID is always 32 bits.
495 ******************************************************************************/
498 AcpiExEisaIdToString (
505 ACPI_FUNCTION_ENTRY ();
508 /* The EISAID should be a 32-bit integer */
510 if (CompressedId
> ACPI_UINT32_MAX
)
512 ACPI_WARNING ((AE_INFO
,
513 "Expected EISAID is larger than 32 bits: 0x%8.8X%8.8X, truncating",
514 ACPI_FORMAT_UINT64 (CompressedId
)));
517 /* Swap ID to big-endian to get contiguous bits */
519 SwappedId
= AcpiUtDwordByteSwap ((UINT32
) CompressedId
);
521 /* First 3 bytes are uppercase letters. Next 4 bytes are hexadecimal */
523 OutString
[0] = (char) (0x40 + (((unsigned long) SwappedId
>> 26) & 0x1F));
524 OutString
[1] = (char) (0x40 + ((SwappedId
>> 21) & 0x1F));
525 OutString
[2] = (char) (0x40 + ((SwappedId
>> 16) & 0x1F));
526 OutString
[3] = AcpiUtHexToAsciiChar ((UINT64
) SwappedId
, 12);
527 OutString
[4] = AcpiUtHexToAsciiChar ((UINT64
) SwappedId
, 8);
528 OutString
[5] = AcpiUtHexToAsciiChar ((UINT64
) SwappedId
, 4);
529 OutString
[6] = AcpiUtHexToAsciiChar ((UINT64
) SwappedId
, 0);
534 /*******************************************************************************
536 * FUNCTION: AcpiExIntegerToString
538 * PARAMETERS: OutString - Where to put the converted string. At least
539 * 21 bytes are needed to hold the largest
540 * possible 64-bit integer.
541 * Value - Value to be converted
543 * RETURN: None, string
545 * DESCRIPTION: Convert a 64-bit integer to decimal string representation.
546 * Assumes string buffer is large enough to hold the string. The
547 * largest string is (ACPI_MAX64_DECIMAL_DIGITS + 1).
549 ******************************************************************************/
552 AcpiExIntegerToString (
561 ACPI_FUNCTION_ENTRY ();
564 DigitsNeeded
= AcpiExDigitsNeeded (Value
, 10);
565 OutString
[DigitsNeeded
] = 0;
567 for (Count
= DigitsNeeded
; Count
> 0; Count
--)
569 (void) AcpiUtShortDivide (Value
, 10, &Value
, &Remainder
);
570 OutString
[Count
-1] = (char) ('0' + Remainder
);\