xref: /netbsd-src/sys/external/bsd/acpica/dist/executer/exoparg3.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
128c506b8Sjruoho /******************************************************************************
228c506b8Sjruoho  *
328c506b8Sjruoho  * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
428c506b8Sjruoho  *
528c506b8Sjruoho  *****************************************************************************/
628c506b8Sjruoho 
7124f4c82Sjruoho /*
8*046a2985Schristos  * Copyright (C) 2000 - 2023, Intel Corp.
928c506b8Sjruoho  * All rights reserved.
1028c506b8Sjruoho  *
11124f4c82Sjruoho  * Redistribution and use in source and binary forms, with or without
12124f4c82Sjruoho  * modification, are permitted provided that the following conditions
13124f4c82Sjruoho  * are met:
14124f4c82Sjruoho  * 1. Redistributions of source code must retain the above copyright
15124f4c82Sjruoho  *    notice, this list of conditions, and the following disclaimer,
16124f4c82Sjruoho  *    without modification.
17124f4c82Sjruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18124f4c82Sjruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
19124f4c82Sjruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
20124f4c82Sjruoho  *    including a substantially similar Disclaimer requirement for further
21124f4c82Sjruoho  *    binary redistribution.
22124f4c82Sjruoho  * 3. Neither the names of the above-listed copyright holders nor the names
23124f4c82Sjruoho  *    of any contributors may be used to endorse or promote products derived
24124f4c82Sjruoho  *    from this software without specific prior written permission.
2528c506b8Sjruoho  *
26124f4c82Sjruoho  * Alternatively, this software may be distributed under the terms of the
27124f4c82Sjruoho  * GNU General Public License ("GPL") version 2 as published by the Free
28124f4c82Sjruoho  * Software Foundation.
2928c506b8Sjruoho  *
30124f4c82Sjruoho  * NO WARRANTY
31124f4c82Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32124f4c82Sjruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3346a330b4Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34124f4c82Sjruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35124f4c82Sjruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36124f4c82Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37124f4c82Sjruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38124f4c82Sjruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39124f4c82Sjruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40124f4c82Sjruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41124f4c82Sjruoho  * POSSIBILITY OF SUCH DAMAGES.
42124f4c82Sjruoho  */
4328c506b8Sjruoho 
4428c506b8Sjruoho #include "acpi.h"
4528c506b8Sjruoho #include "accommon.h"
4628c506b8Sjruoho #include "acinterp.h"
4728c506b8Sjruoho #include "acparser.h"
4828c506b8Sjruoho #include "amlcode.h"
4928c506b8Sjruoho 
5028c506b8Sjruoho 
5128c506b8Sjruoho #define _COMPONENT          ACPI_EXECUTER
5228c506b8Sjruoho         ACPI_MODULE_NAME    ("exoparg3")
5328c506b8Sjruoho 
5428c506b8Sjruoho 
5528c506b8Sjruoho /*!
5628c506b8Sjruoho  * Naming convention for AML interpreter execution routines.
5728c506b8Sjruoho  *
5828c506b8Sjruoho  * The routines that begin execution of AML opcodes are named with a common
5928c506b8Sjruoho  * convention based upon the number of arguments, the number of target operands,
6028c506b8Sjruoho  * and whether or not a value is returned:
6128c506b8Sjruoho  *
6228c506b8Sjruoho  *      AcpiExOpcode_xA_yT_zR
6328c506b8Sjruoho  *
6428c506b8Sjruoho  * Where:
6528c506b8Sjruoho  *
6628c506b8Sjruoho  * xA - ARGUMENTS:    The number of arguments (input operands) that are
6728c506b8Sjruoho  *                    required for this opcode type (1 through 6 args).
6828c506b8Sjruoho  * yT - TARGETS:      The number of targets (output operands) that are required
6928c506b8Sjruoho  *                    for this opcode type (0, 1, or 2 targets).
7028c506b8Sjruoho  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
7128c506b8Sjruoho  *                    as the function return (0 or 1).
7228c506b8Sjruoho  *
7328c506b8Sjruoho  * The AcpiExOpcode* functions are called via the Dispatcher component with
7428c506b8Sjruoho  * fully resolved operands.
7528c506b8Sjruoho !*/
7628c506b8Sjruoho 
7728c506b8Sjruoho 
7828c506b8Sjruoho /*******************************************************************************
7928c506b8Sjruoho  *
8028c506b8Sjruoho  * FUNCTION:    AcpiExOpcode_3A_0T_0R
8128c506b8Sjruoho  *
8228c506b8Sjruoho  * PARAMETERS:  WalkState           - Current walk state
8328c506b8Sjruoho  *
8428c506b8Sjruoho  * RETURN:      Status
8528c506b8Sjruoho  *
8628c506b8Sjruoho  * DESCRIPTION: Execute Triadic operator (3 operands)
8728c506b8Sjruoho  *
8828c506b8Sjruoho  ******************************************************************************/
8928c506b8Sjruoho 
9028c506b8Sjruoho ACPI_STATUS
AcpiExOpcode_3A_0T_0R(ACPI_WALK_STATE * WalkState)9128c506b8Sjruoho AcpiExOpcode_3A_0T_0R (
9228c506b8Sjruoho     ACPI_WALK_STATE         *WalkState)
9328c506b8Sjruoho {
9428c506b8Sjruoho     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
9528c506b8Sjruoho     ACPI_SIGNAL_FATAL_INFO  *Fatal;
9628c506b8Sjruoho     ACPI_STATUS             Status = AE_OK;
9728c506b8Sjruoho 
9828c506b8Sjruoho 
9928c506b8Sjruoho     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_0T_0R,
10028c506b8Sjruoho         AcpiPsGetOpcodeName (WalkState->Opcode));
10128c506b8Sjruoho 
10228c506b8Sjruoho 
10328c506b8Sjruoho     switch (WalkState->Opcode)
10428c506b8Sjruoho     {
10528c506b8Sjruoho     case AML_FATAL_OP:          /* Fatal (FatalType  FatalCode  FatalArg) */
10628c506b8Sjruoho 
10728c506b8Sjruoho         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
10871e38f1dSchristos             "FatalOp: Type %X Code %X Arg %X "
10971e38f1dSchristos             "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
11028c506b8Sjruoho             (UINT32) Operand[0]->Integer.Value,
11128c506b8Sjruoho             (UINT32) Operand[1]->Integer.Value,
11228c506b8Sjruoho             (UINT32) Operand[2]->Integer.Value));
11328c506b8Sjruoho 
11428c506b8Sjruoho         Fatal = ACPI_ALLOCATE (sizeof (ACPI_SIGNAL_FATAL_INFO));
11528c506b8Sjruoho         if (Fatal)
11628c506b8Sjruoho         {
11728c506b8Sjruoho             Fatal->Type = (UINT32) Operand[0]->Integer.Value;
11828c506b8Sjruoho             Fatal->Code = (UINT32) Operand[1]->Integer.Value;
11928c506b8Sjruoho             Fatal->Argument = (UINT32) Operand[2]->Integer.Value;
12028c506b8Sjruoho         }
12128c506b8Sjruoho 
12228c506b8Sjruoho         /* Always signal the OS! */
12328c506b8Sjruoho 
12428c506b8Sjruoho         Status = AcpiOsSignal (ACPI_SIGNAL_FATAL, Fatal);
12528c506b8Sjruoho 
12628c506b8Sjruoho         /* Might return while OS is shutting down, just continue */
12728c506b8Sjruoho 
12828c506b8Sjruoho         ACPI_FREE (Fatal);
129679c17fdSchristos         goto Cleanup;
130679c17fdSchristos 
131679c17fdSchristos     case AML_EXTERNAL_OP:
132679c17fdSchristos         /*
133679c17fdSchristos          * If the interpreter sees this opcode, just ignore it. The External
134679c17fdSchristos          * op is intended for use by disassemblers in order to properly
135679c17fdSchristos          * disassemble control method invocations. The opcode or group of
136679c17fdSchristos          * opcodes should be surrounded by an "if (0)" clause to ensure that
137cfbb7280Schristos          * AML interpreters never see the opcode. Thus, something is
138cfbb7280Schristos          * wrong if an external opcode ever gets here.
139679c17fdSchristos          */
140cfbb7280Schristos         ACPI_ERROR ((AE_INFO, "Executed External Op"));
141679c17fdSchristos         Status = AE_OK;
142679c17fdSchristos         goto Cleanup;
14328c506b8Sjruoho 
14428c506b8Sjruoho     default:
14528c506b8Sjruoho 
14628c506b8Sjruoho         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
14728c506b8Sjruoho             WalkState->Opcode));
14871e38f1dSchristos 
14928c506b8Sjruoho         Status = AE_AML_BAD_OPCODE;
15028c506b8Sjruoho         goto Cleanup;
15128c506b8Sjruoho     }
15228c506b8Sjruoho 
15328c506b8Sjruoho 
15428c506b8Sjruoho Cleanup:
15528c506b8Sjruoho 
15628c506b8Sjruoho     return_ACPI_STATUS (Status);
15728c506b8Sjruoho }
15828c506b8Sjruoho 
15928c506b8Sjruoho 
16028c506b8Sjruoho /*******************************************************************************
16128c506b8Sjruoho  *
16228c506b8Sjruoho  * FUNCTION:    AcpiExOpcode_3A_1T_1R
16328c506b8Sjruoho  *
16428c506b8Sjruoho  * PARAMETERS:  WalkState           - Current walk state
16528c506b8Sjruoho  *
16628c506b8Sjruoho  * RETURN:      Status
16728c506b8Sjruoho  *
16828c506b8Sjruoho  * DESCRIPTION: Execute Triadic operator (3 operands)
16928c506b8Sjruoho  *
17028c506b8Sjruoho  ******************************************************************************/
17128c506b8Sjruoho 
17228c506b8Sjruoho ACPI_STATUS
AcpiExOpcode_3A_1T_1R(ACPI_WALK_STATE * WalkState)17328c506b8Sjruoho AcpiExOpcode_3A_1T_1R (
17428c506b8Sjruoho     ACPI_WALK_STATE         *WalkState)
17528c506b8Sjruoho {
17628c506b8Sjruoho     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
17728c506b8Sjruoho     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
17828c506b8Sjruoho     char                    *Buffer = NULL;
17928c506b8Sjruoho     ACPI_STATUS             Status = AE_OK;
18028c506b8Sjruoho     UINT64                  Index;
18128c506b8Sjruoho     ACPI_SIZE               Length;
18228c506b8Sjruoho 
18328c506b8Sjruoho 
18428c506b8Sjruoho     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_1T_1R,
18528c506b8Sjruoho         AcpiPsGetOpcodeName (WalkState->Opcode));
18628c506b8Sjruoho 
18728c506b8Sjruoho 
18828c506b8Sjruoho     switch (WalkState->Opcode)
18928c506b8Sjruoho     {
19028c506b8Sjruoho     case AML_MID_OP:    /* Mid (Source[0], Index[1], Length[2], Result[3]) */
19128c506b8Sjruoho         /*
19228c506b8Sjruoho          * Create the return object. The Source operand is guaranteed to be
19328c506b8Sjruoho          * either a String or a Buffer, so just use its type.
19428c506b8Sjruoho          */
19528c506b8Sjruoho         ReturnDesc = AcpiUtCreateInternalObject (
19628c506b8Sjruoho             (Operand[0])->Common.Type);
19728c506b8Sjruoho         if (!ReturnDesc)
19828c506b8Sjruoho         {
19928c506b8Sjruoho             Status = AE_NO_MEMORY;
20028c506b8Sjruoho             goto Cleanup;
20128c506b8Sjruoho         }
20228c506b8Sjruoho 
20328c506b8Sjruoho         /* Get the Integer values from the objects */
20428c506b8Sjruoho 
20528c506b8Sjruoho         Index = Operand[1]->Integer.Value;
20628c506b8Sjruoho         Length = (ACPI_SIZE) Operand[2]->Integer.Value;
20728c506b8Sjruoho 
20828c506b8Sjruoho         /*
20928c506b8Sjruoho          * If the index is beyond the length of the String/Buffer, or if the
21028c506b8Sjruoho          * requested length is zero, return a zero-length String/Buffer
21128c506b8Sjruoho          */
21228c506b8Sjruoho         if (Index >= Operand[0]->String.Length)
21328c506b8Sjruoho         {
21428c506b8Sjruoho             Length = 0;
21528c506b8Sjruoho         }
21628c506b8Sjruoho 
21728c506b8Sjruoho         /* Truncate request if larger than the actual String/Buffer */
21828c506b8Sjruoho 
21928c506b8Sjruoho         else if ((Index + Length) > Operand[0]->String.Length)
22028c506b8Sjruoho         {
22171e38f1dSchristos             Length =
22271e38f1dSchristos                 (ACPI_SIZE) Operand[0]->String.Length - (ACPI_SIZE) Index;
22328c506b8Sjruoho         }
22428c506b8Sjruoho 
22528c506b8Sjruoho         /* Strings always have a sub-pointer, not so for buffers */
22628c506b8Sjruoho 
22728c506b8Sjruoho         switch ((Operand[0])->Common.Type)
22828c506b8Sjruoho         {
22928c506b8Sjruoho         case ACPI_TYPE_STRING:
23028c506b8Sjruoho 
23128c506b8Sjruoho             /* Always allocate a new buffer for the String */
23228c506b8Sjruoho 
23328c506b8Sjruoho             Buffer = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) Length + 1);
23428c506b8Sjruoho             if (!Buffer)
23528c506b8Sjruoho             {
23628c506b8Sjruoho                 Status = AE_NO_MEMORY;
23728c506b8Sjruoho                 goto Cleanup;
23828c506b8Sjruoho             }
23928c506b8Sjruoho             break;
24028c506b8Sjruoho 
24128c506b8Sjruoho         case ACPI_TYPE_BUFFER:
24228c506b8Sjruoho 
24328c506b8Sjruoho             /* If the requested length is zero, don't allocate a buffer */
24428c506b8Sjruoho 
24528c506b8Sjruoho             if (Length > 0)
24628c506b8Sjruoho             {
24728c506b8Sjruoho                 /* Allocate a new buffer for the Buffer */
24828c506b8Sjruoho 
24928c506b8Sjruoho                 Buffer = ACPI_ALLOCATE_ZEROED (Length);
25028c506b8Sjruoho                 if (!Buffer)
25128c506b8Sjruoho                 {
25228c506b8Sjruoho                     Status = AE_NO_MEMORY;
25328c506b8Sjruoho                     goto Cleanup;
25428c506b8Sjruoho                 }
25528c506b8Sjruoho             }
25628c506b8Sjruoho             break;
25728c506b8Sjruoho 
25828c506b8Sjruoho         default:                        /* Should not happen */
25928c506b8Sjruoho 
26028c506b8Sjruoho             Status = AE_AML_OPERAND_TYPE;
26128c506b8Sjruoho             goto Cleanup;
26228c506b8Sjruoho         }
26328c506b8Sjruoho 
26428c506b8Sjruoho         if (Buffer)
26528c506b8Sjruoho         {
26628c506b8Sjruoho             /* We have a buffer, copy the portion requested */
26728c506b8Sjruoho 
26871e38f1dSchristos             memcpy (Buffer,
26971e38f1dSchristos                 Operand[0]->String.Pointer + Index, Length);
27028c506b8Sjruoho         }
27128c506b8Sjruoho 
27228c506b8Sjruoho         /* Set the length of the new String/Buffer */
27328c506b8Sjruoho 
27428c506b8Sjruoho         ReturnDesc->String.Pointer = Buffer;
27528c506b8Sjruoho         ReturnDesc->String.Length = (UINT32) Length;
27628c506b8Sjruoho 
27728c506b8Sjruoho         /* Mark buffer initialized */
27828c506b8Sjruoho 
27928c506b8Sjruoho         ReturnDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
28028c506b8Sjruoho         break;
28128c506b8Sjruoho 
28228c506b8Sjruoho     default:
28328c506b8Sjruoho 
28428c506b8Sjruoho         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
28528c506b8Sjruoho             WalkState->Opcode));
28671e38f1dSchristos 
28728c506b8Sjruoho         Status = AE_AML_BAD_OPCODE;
28828c506b8Sjruoho         goto Cleanup;
28928c506b8Sjruoho     }
29028c506b8Sjruoho 
29128c506b8Sjruoho     /* Store the result in the target */
29228c506b8Sjruoho 
29328c506b8Sjruoho     Status = AcpiExStore (ReturnDesc, Operand[3], WalkState);
29428c506b8Sjruoho 
29528c506b8Sjruoho Cleanup:
29628c506b8Sjruoho 
29728c506b8Sjruoho     /* Delete return object on error */
29828c506b8Sjruoho 
29928c506b8Sjruoho     if (ACPI_FAILURE (Status) || WalkState->ResultObj)
30028c506b8Sjruoho     {
30128c506b8Sjruoho         AcpiUtRemoveReference (ReturnDesc);
30228c506b8Sjruoho         WalkState->ResultObj = NULL;
30328c506b8Sjruoho     }
30428c506b8Sjruoho     else
30528c506b8Sjruoho     {
30671e38f1dSchristos         /* Set the return object and exit */
30771e38f1dSchristos 
30828c506b8Sjruoho         WalkState->ResultObj = ReturnDesc;
30928c506b8Sjruoho     }
31071e38f1dSchristos 
31128c506b8Sjruoho     return_ACPI_STATUS (Status);
31228c506b8Sjruoho }
313