exutils.c revision 80062
167754Smsmith
267754Smsmith/******************************************************************************
367754Smsmith *
477424Smsmith * Module Name: exutils - interpreter/scanner utilities
580062Smsmith *              $Revision: 80 $
667754Smsmith *
767754Smsmith *****************************************************************************/
867754Smsmith
967754Smsmith/******************************************************************************
1067754Smsmith *
1167754Smsmith * 1. Copyright Notice
1267754Smsmith *
1371867Smsmith * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
1470243Smsmith * All rights reserved.
1567754Smsmith *
1667754Smsmith * 2. License
1767754Smsmith *
1867754Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property
1967754Smsmith * rights.  You may have additional license terms from the party that provided
2067754Smsmith * you this software, covering your right to use that party's intellectual
2167754Smsmith * property rights.
2267754Smsmith *
2367754Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2467754Smsmith * copy of the source code appearing in this file ("Covered Code") an
2567754Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2667754Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy,
2767754Smsmith * make derivatives, distribute, use and display any portion of the Covered
2867754Smsmith * Code in any form, with the right to sublicense such rights; and
2967754Smsmith *
3067754Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3167754Smsmith * license (with the right to sublicense), under only those claims of Intel
3267754Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell,
3367754Smsmith * offer to sell, and import the Covered Code and derivative works thereof
3467754Smsmith * solely to the minimum extent necessary to exercise the above copyright
3567754Smsmith * license, and in no event shall the patent license extend to any additions
3667754Smsmith * to or modifications of the Original Intel Code.  No other license or right
3767754Smsmith * is granted directly or by implication, estoppel or otherwise;
3867754Smsmith *
3967754Smsmith * The above copyright and patent license is granted only if the following
4067754Smsmith * conditions are met:
4167754Smsmith *
4267754Smsmith * 3. Conditions
4367754Smsmith *
4467754Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4567754Smsmith * Redistribution of source code of any substantial portion of the Covered
4667754Smsmith * Code or modification with rights to further distribute source must include
4767754Smsmith * the above Copyright Notice, the above License, this list of Conditions,
4867754Smsmith * and the following Disclaimer and Export Compliance provision.  In addition,
4967754Smsmith * Licensee must cause all Covered Code to which Licensee contributes to
5067754Smsmith * contain a file documenting the changes Licensee made to create that Covered
5167754Smsmith * Code and the date of any change.  Licensee must include in that file the
5267754Smsmith * documentation of any changes made by any predecessor Licensee.  Licensee
5367754Smsmith * must include a prominent statement that the modification is derived,
5467754Smsmith * directly or indirectly, from Original Intel Code.
5567754Smsmith *
5667754Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5767754Smsmith * Redistribution of source code of any substantial portion of the Covered
5867754Smsmith * Code or modification without rights to further distribute source must
5967754Smsmith * include the following Disclaimer and Export Compliance provision in the
6067754Smsmith * documentation and/or other materials provided with distribution.  In
6167754Smsmith * addition, Licensee may not authorize further sublicense of source of any
6267754Smsmith * portion of the Covered Code, and must include terms to the effect that the
6367754Smsmith * license from Licensee to its licensee is limited to the intellectual
6467754Smsmith * property embodied in the software Licensee provides to its licensee, and
6567754Smsmith * not to intellectual property embodied in modifications its licensee may
6667754Smsmith * make.
6767754Smsmith *
6867754Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any
6967754Smsmith * substantial portion of the Covered Code or modification must reproduce the
7067754Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance
7167754Smsmith * provision in the documentation and/or other materials provided with the
7267754Smsmith * distribution.
7367754Smsmith *
7467754Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original
7567754Smsmith * Intel Code.
7667754Smsmith *
7767754Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7867754Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or
7967754Smsmith * other dealings in products derived from or relating to the Covered Code
8067754Smsmith * without prior written authorization from Intel.
8167754Smsmith *
8267754Smsmith * 4. Disclaimer and Export Compliance
8367754Smsmith *
8467754Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8567754Smsmith * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8667754Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8767754Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8867754Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8967754Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
9067754Smsmith * PARTICULAR PURPOSE.
9167754Smsmith *
9267754Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9367754Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9467754Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9567754Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9667754Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9767754Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9867754Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9967754Smsmith * LIMITED REMEDY.
10067754Smsmith *
10167754Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this
10267754Smsmith * software or system incorporating such software without first obtaining any
10367754Smsmith * required license or other approval from the U. S. Department of Commerce or
10467754Smsmith * any other agency or department of the United States Government.  In the
10567754Smsmith * event Licensee exports any such software from the United States or
10667754Smsmith * re-exports any such software from a foreign destination, Licensee shall
10767754Smsmith * ensure that the distribution and export/re-export of the software is in
10867754Smsmith * compliance with all laws, regulations, orders, or other restrictions of the
10967754Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor
11067754Smsmith * any of its subsidiaries will export/re-export any technical data, process,
11167754Smsmith * software, or service, directly or indirectly, to any country for which the
11267754Smsmith * United States government or any agency thereof requires an export license,
11367754Smsmith * other governmental approval, or letter of assurance, without first obtaining
11467754Smsmith * such license, approval or letter.
11567754Smsmith *
11667754Smsmith *****************************************************************************/
11767754Smsmith
11877424Smsmith#define __EXUTILS_C__
11967754Smsmith
12067754Smsmith#include "acpi.h"
12167754Smsmith#include "acparser.h"
12267754Smsmith#include "acinterp.h"
12367754Smsmith#include "amlcode.h"
12467754Smsmith#include "acnamesp.h"
12567754Smsmith#include "acevents.h"
12667754Smsmith
12777424Smsmith#define _COMPONENT          ACPI_EXECUTER
12877424Smsmith        MODULE_NAME         ("exutils")
12967754Smsmith
13067754Smsmith
13167754Smsmith/*******************************************************************************
13267754Smsmith *
13377424Smsmith * FUNCTION:    AcpiExEnterInterpreter
13467754Smsmith *
13567754Smsmith * PARAMETERS:  None
13667754Smsmith *
13767754Smsmith * DESCRIPTION: Enter the interpreter execution region
13877424Smsmith *              TBD: should be a macro
13967754Smsmith *
14067754Smsmith ******************************************************************************/
14167754Smsmith
14277424SmsmithACPI_STATUS
14377424SmsmithAcpiExEnterInterpreter (void)
14467754Smsmith{
14577424Smsmith    ACPI_STATUS             Status;
14667754Smsmith
14777424Smsmith    FUNCTION_TRACE ("ExEnterInterpreter");
14867754Smsmith
14967754Smsmith
15077424Smsmith    Status = AcpiUtAcquireMutex (ACPI_MTX_EXECUTE);
15177424Smsmith    return_ACPI_STATUS (Status);
15267754Smsmith}
15367754Smsmith
15467754Smsmith
15567754Smsmith/*******************************************************************************
15667754Smsmith *
15777424Smsmith * FUNCTION:    AcpiExExitInterpreter
15867754Smsmith *
15967754Smsmith * PARAMETERS:  None
16067754Smsmith *
16167754Smsmith * DESCRIPTION: Exit the interpreter execution region
16267754Smsmith *
16367754Smsmith * Cases where the interpreter is unlocked:
16467754Smsmith *      1) Completion of the execution of a control method
16567754Smsmith *      2) Method blocked on a Sleep() AML opcode
16667754Smsmith *      3) Method blocked on an Acquire() AML opcode
16767754Smsmith *      4) Method blocked on a Wait() AML opcode
16867754Smsmith *      5) Method blocked to acquire the global lock
16967754Smsmith *      6) Method blocked to execute a serialized control method that is
17067754Smsmith *          already executing
17167754Smsmith *      7) About to invoke a user-installed opregion handler
17267754Smsmith *
17377424Smsmith *              TBD: should be a macro
17477424Smsmith *
17567754Smsmith ******************************************************************************/
17667754Smsmith
17767754Smsmithvoid
17877424SmsmithAcpiExExitInterpreter (void)
17967754Smsmith{
18077424Smsmith    FUNCTION_TRACE ("ExExitInterpreter");
18167754Smsmith
18267754Smsmith
18377424Smsmith    AcpiUtReleaseMutex (ACPI_MTX_EXECUTE);
18467754Smsmith
18567754Smsmith    return_VOID;
18667754Smsmith}
18767754Smsmith
18867754Smsmith
18967754Smsmith/*******************************************************************************
19067754Smsmith *
19177424Smsmith * FUNCTION:    AcpiExValidateObjectType
19267754Smsmith *
19367754Smsmith * PARAMETERS:  Type            Object type to validate
19467754Smsmith *
19567754Smsmith * DESCRIPTION: Determine if a type is a valid ACPI object type
19667754Smsmith *
19767754Smsmith ******************************************************************************/
19867754Smsmith
19967754SmsmithBOOLEAN
20077424SmsmithAcpiExValidateObjectType (
20167754Smsmith    ACPI_OBJECT_TYPE        Type)
20267754Smsmith{
20367754Smsmith
20467754Smsmith    if ((Type > ACPI_TYPE_MAX && Type < INTERNAL_TYPE_BEGIN) ||
20567754Smsmith        (Type > INTERNAL_TYPE_MAX))
20667754Smsmith    {
20767754Smsmith        return (FALSE);
20867754Smsmith    }
20967754Smsmith
21067754Smsmith    return (TRUE);
21167754Smsmith}
21267754Smsmith
21367754Smsmith
21467754Smsmith/*******************************************************************************
21567754Smsmith *
21677424Smsmith * FUNCTION:    AcpiExTruncateFor32bitTable
21767754Smsmith *
21869450Smsmith * PARAMETERS:  ObjDesc         - Object to be truncated
21969450Smsmith *              WalkState       - Current walk state
22069450Smsmith *                                (A method must be executing)
22167754Smsmith *
22269450Smsmith * RETURN:      none
22367754Smsmith *
22469450Smsmith * DESCRIPTION: Truncate a number to 32-bits if the currently executing method
22569450Smsmith *              belongs to a 32-bit ACPI table.
22667754Smsmith *
22767754Smsmith ******************************************************************************/
22867754Smsmith
22969450Smsmithvoid
23077424SmsmithAcpiExTruncateFor32bitTable (
23169450Smsmith    ACPI_OPERAND_OBJECT     *ObjDesc,
23269450Smsmith    ACPI_WALK_STATE         *WalkState)
23367754Smsmith{
23467754Smsmith
23569450Smsmith    /*
23669450Smsmith     * Object must be a valid number and we must be executing
23769450Smsmith     * a control method
23869450Smsmith     */
23969450Smsmith
24069450Smsmith    if ((!ObjDesc) ||
24171867Smsmith        (ObjDesc->Common.Type != ACPI_TYPE_INTEGER) ||
24269450Smsmith        (!WalkState->MethodNode))
24369450Smsmith    {
24469450Smsmith        return;
24569450Smsmith    }
24669450Smsmith
24769450Smsmith    if (WalkState->MethodNode->Flags & ANOBJ_DATA_WIDTH_32)
24869450Smsmith    {
24969450Smsmith        /*
25069450Smsmith         * We are running a method that exists in a 32-bit ACPI table.
25169450Smsmith         * Truncate the value to 32 bits by zeroing out the upper 32-bit field
25269450Smsmith         */
25371867Smsmith        ObjDesc->Integer.Value &= (ACPI_INTEGER) ACPI_UINT32_MAX;
25469450Smsmith    }
25567754Smsmith}
25667754Smsmith
25767754Smsmith
25867754Smsmith/*******************************************************************************
25967754Smsmith *
26077424Smsmith * FUNCTION:    AcpiExAcquireGlobalLock
26167754Smsmith *
26267754Smsmith * PARAMETERS:  Rule            - Lock rule: AlwaysLock, NeverLock
26367754Smsmith *
26467754Smsmith * RETURN:      TRUE/FALSE indicating whether the lock was actually acquired
26567754Smsmith *
26667754Smsmith * DESCRIPTION: Obtain the global lock and keep track of this fact via two
26767754Smsmith *              methods.  A global variable keeps the state of the lock, and
26867754Smsmith *              the state is returned to the caller.
26967754Smsmith *
27067754Smsmith ******************************************************************************/
27167754Smsmith
27267754SmsmithBOOLEAN
27377424SmsmithAcpiExAcquireGlobalLock (
27467754Smsmith    UINT32                  Rule)
27567754Smsmith{
27667754Smsmith    BOOLEAN                 Locked = FALSE;
27767754Smsmith    ACPI_STATUS             Status;
27867754Smsmith
27967754Smsmith
28077424Smsmith    FUNCTION_TRACE ("ExAcquireGlobalLock");
28167754Smsmith
28267754Smsmith
28377424Smsmith    /* Only attempt lock if the Rule says so */
28467754Smsmith
28567754Smsmith    if (Rule == (UINT32) GLOCK_ALWAYS_LOCK)
28667754Smsmith    {
28777424Smsmith        /* We should attempt to get the lock */
28867754Smsmith
28967754Smsmith        Status = AcpiEvAcquireGlobalLock ();
29077424Smsmith        if (ACPI_SUCCESS (Status))
29167754Smsmith        {
29277424Smsmith            Locked = TRUE;
29367754Smsmith        }
29467754Smsmith
29577424Smsmith        else
29667754Smsmith        {
29777424Smsmith            DEBUG_PRINTP (ACPI_ERROR, ("Could not acquire Global Lock, %s\n",
29880062Smsmith                AcpiFormatException (Status)));
29967754Smsmith        }
30067754Smsmith    }
30167754Smsmith
30267754Smsmith    return_VALUE (Locked);
30367754Smsmith}
30467754Smsmith
30567754Smsmith
30667754Smsmith/*******************************************************************************
30767754Smsmith *
30877424Smsmith * FUNCTION:    AcpiExReleaseGlobalLock
30967754Smsmith *
31067754Smsmith * PARAMETERS:  LockedByMe      - Return value from corresponding call to
31167754Smsmith *                                AcquireGlobalLock.
31267754Smsmith *
31367754Smsmith * RETURN:      Status
31467754Smsmith *
31567754Smsmith * DESCRIPTION: Release the global lock if it is locked.
31667754Smsmith *
31767754Smsmith ******************************************************************************/
31867754Smsmith
31967754SmsmithACPI_STATUS
32077424SmsmithAcpiExReleaseGlobalLock (
32167754Smsmith    BOOLEAN                 LockedByMe)
32267754Smsmith{
32367754Smsmith
32477424Smsmith    FUNCTION_TRACE ("ExReleaseGlobalLock");
32567754Smsmith
32667754Smsmith
32767754Smsmith    /* Only attempt unlock if the caller locked it */
32867754Smsmith
32967754Smsmith    if (LockedByMe)
33067754Smsmith    {
33177424Smsmith        /* OK, now release the lock */
33267754Smsmith
33377424Smsmith        AcpiEvReleaseGlobalLock ();
33467754Smsmith    }
33567754Smsmith
33667754Smsmith
33767754Smsmith    return_ACPI_STATUS (AE_OK);
33867754Smsmith}
33967754Smsmith
34067754Smsmith
34167754Smsmith/*******************************************************************************
34267754Smsmith *
34377424Smsmith * FUNCTION:    AcpiExDigitsNeeded
34467754Smsmith *
34567754Smsmith * PARAMETERS:  val             - Value to be represented
34667754Smsmith *              base            - Base of representation
34767754Smsmith *
34867754Smsmith * RETURN:      the number of digits needed to represent val in base
34967754Smsmith *
35067754Smsmith ******************************************************************************/
35167754Smsmith
35267754SmsmithUINT32
35377424SmsmithAcpiExDigitsNeeded (
35467754Smsmith    ACPI_INTEGER            val,
35567754Smsmith    UINT32                  base)
35667754Smsmith{
35767754Smsmith    UINT32                  NumDigits = 0;
35867754Smsmith
35967754Smsmith
36077424Smsmith    FUNCTION_TRACE ("ExDigitsNeeded");
36167754Smsmith
36267754Smsmith
36367754Smsmith    if (base < 1)
36467754Smsmith    {
36577424Smsmith        REPORT_ERROR (("ExDigitsNeeded: Internal error - Invalid base\n"));
36667754Smsmith    }
36767754Smsmith
36867754Smsmith    else
36967754Smsmith    {
37077424Smsmith        /*
37177424Smsmith         * ACPI_INTEGER is unsigned, which is why we don't worry about the '-'
37277424Smsmith         */
37377424Smsmith        for (NumDigits = 1; (val = ACPI_DIVIDE (val,base)); ++NumDigits)
37467754Smsmith        { ; }
37567754Smsmith    }
37667754Smsmith
37767754Smsmith    return_VALUE (NumDigits);
37867754Smsmith}
37967754Smsmith
38067754Smsmith
38167754Smsmith/*******************************************************************************
38267754Smsmith *
38367754Smsmith * FUNCTION:    ntohl
38467754Smsmith *
38567754Smsmith * PARAMETERS:  Value           - Value to be converted
38667754Smsmith *
38767754Smsmith * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
38867754Smsmith *
38967754Smsmith ******************************************************************************/
39067754Smsmith
39169450Smsmithstatic UINT32
39267754Smsmith_ntohl (
39367754Smsmith    UINT32                  Value)
39467754Smsmith{
39567754Smsmith    union
39667754Smsmith    {
39767754Smsmith        UINT32              Value;
39867754Smsmith        UINT8               Bytes[4];
39967754Smsmith    } Out;
40067754Smsmith
40167754Smsmith    union
40267754Smsmith    {
40367754Smsmith        UINT32              Value;
40467754Smsmith        UINT8               Bytes[4];
40567754Smsmith    } In;
40667754Smsmith
40767754Smsmith
40867754Smsmith    In.Value = Value;
40967754Smsmith
41067754Smsmith    Out.Bytes[0] = In.Bytes[3];
41167754Smsmith    Out.Bytes[1] = In.Bytes[2];
41267754Smsmith    Out.Bytes[2] = In.Bytes[1];
41367754Smsmith    Out.Bytes[3] = In.Bytes[0];
41467754Smsmith
41567754Smsmith    return (Out.Value);
41667754Smsmith}
41767754Smsmith
41867754Smsmith
41967754Smsmith/*******************************************************************************
42067754Smsmith *
42177424Smsmith * FUNCTION:    AcpiExEisaIdToString
42267754Smsmith *
42367754Smsmith * PARAMETERS:  NumericId       - EISA ID to be converted
42467754Smsmith *              OutString       - Where to put the converted string (8 bytes)
42567754Smsmith *
42667754Smsmith * DESCRIPTION: Convert a numeric EISA ID to string representation
42767754Smsmith *
42867754Smsmith ******************************************************************************/
42967754Smsmith
43067754SmsmithACPI_STATUS
43177424SmsmithAcpiExEisaIdToString (
43267754Smsmith    UINT32                  NumericId,
43367754Smsmith    NATIVE_CHAR             *OutString)
43467754Smsmith{
43567754Smsmith    UINT32                  id;
43667754Smsmith
43767754Smsmith    /* swap to big-endian to get contiguous bits */
43867754Smsmith
43967754Smsmith    id = _ntohl (NumericId);
44067754Smsmith
44167754Smsmith    OutString[0] = (char) ('@' + ((id >> 26) & 0x1f));
44267754Smsmith    OutString[1] = (char) ('@' + ((id >> 21) & 0x1f));
44367754Smsmith    OutString[2] = (char) ('@' + ((id >> 16) & 0x1f));
44471867Smsmith    OutString[3] = AcpiGbl_HexToAscii[(id >> 12) & 0xf];
44571867Smsmith    OutString[4] = AcpiGbl_HexToAscii[(id >> 8) & 0xf];
44671867Smsmith    OutString[5] = AcpiGbl_HexToAscii[(id >> 4) & 0xf];
44771867Smsmith    OutString[6] = AcpiGbl_HexToAscii[id & 0xf];
44867754Smsmith    OutString[7] = 0;
44967754Smsmith
45067754Smsmith    return (AE_OK);
45167754Smsmith}
45267754Smsmith
45367754Smsmith
45467754Smsmith/*******************************************************************************
45567754Smsmith *
45677424Smsmith * FUNCTION:    AcpiExUnsignedIntegerToString
45767754Smsmith *
45867754Smsmith * PARAMETERS:  Value           - Value to be converted
45967754Smsmith *              OutString       - Where to put the converted string (8 bytes)
46067754Smsmith *
46167754Smsmith * RETURN:      Convert a number to string representation
46267754Smsmith *
46367754Smsmith ******************************************************************************/
46467754Smsmith
46567754SmsmithACPI_STATUS
46677424SmsmithAcpiExUnsignedIntegerToString (
46767754Smsmith    ACPI_INTEGER            Value,
46867754Smsmith    NATIVE_CHAR             *OutString)
46967754Smsmith{
47067754Smsmith    UINT32                  Count;
47167754Smsmith    UINT32                  DigitsNeeded;
47267754Smsmith
47367754Smsmith
47477424Smsmith    DigitsNeeded = AcpiExDigitsNeeded (Value, 10);
47567754Smsmith
47667754Smsmith    OutString[DigitsNeeded] = '\0';
47767754Smsmith
47867754Smsmith    for (Count = DigitsNeeded; Count > 0; Count--)
47967754Smsmith    {
48069450Smsmith        OutString[Count-1] = (NATIVE_CHAR) ('0' + (ACPI_MODULO (Value, 10)));
48169450Smsmith        Value = ACPI_DIVIDE (Value, 10);
48267754Smsmith    }
48367754Smsmith
48467754Smsmith    return (AE_OK);
48567754Smsmith}
48667754Smsmith
48767754Smsmith
488