1 /******************************************************************************
   2  *
   3  * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
   4  *
   5  *****************************************************************************/
   6 
   7 /*
   8  * Copyright (C) 2000 - 2013, Intel Corp.
   9  * All rights reserved.
  10  *
  11  * Redistribution and use in source and binary forms, with or without
  12  * modification, are permitted provided that the following conditions
  13  * are met:
  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.
  25  *
  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.
  29  *
  30  * NO WARRANTY
  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.
  42  */
  43 
  44 #define __EXOPARG3_C__
  45 
  46 #include "acpi.h"
  47 #include "accommon.h"
  48 #include "acinterp.h"
  49 #include "acparser.h"
  50 #include "amlcode.h"
  51 
  52 
  53 #define _COMPONENT          ACPI_EXECUTER
  54         ACPI_MODULE_NAME    ("exoparg3")
  55 
  56 
  57 /*!
  58  * Naming convention for AML interpreter execution routines.
  59  *
  60  * The routines that begin execution of AML opcodes are named with a common
  61  * convention based upon the number of arguments, the number of target operands,
  62  * and whether or not a value is returned:
  63  *
  64  *      AcpiExOpcode_xA_yT_zR
  65  *
  66  * Where:
  67  *
  68  * xA - ARGUMENTS:    The number of arguments (input operands) that are
  69  *                    required for this opcode type (1 through 6 args).
  70  * yT - TARGETS:      The number of targets (output operands) that are required
  71  *                    for this opcode type (0, 1, or 2 targets).
  72  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
  73  *                    as the function return (0 or 1).
  74  *
  75  * The AcpiExOpcode* functions are called via the Dispatcher component with
  76  * fully resolved operands.
  77 !*/
  78 
  79 
  80 /*******************************************************************************
  81  *
  82  * FUNCTION:    AcpiExOpcode_3A_0T_0R
  83  *
  84  * PARAMETERS:  WalkState           - Current walk state
  85  *
  86  * RETURN:      Status
  87  *
  88  * DESCRIPTION: Execute Triadic operator (3 operands)
  89  *
  90  ******************************************************************************/
  91 
  92 ACPI_STATUS
  93 AcpiExOpcode_3A_0T_0R (
  94     ACPI_WALK_STATE         *WalkState)
  95 {
  96     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
  97     ACPI_SIGNAL_FATAL_INFO  *Fatal;
  98     ACPI_STATUS             Status = AE_OK;
  99 
 100 
 101     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_0T_0R,
 102         AcpiPsGetOpcodeName (WalkState->Opcode));
 103 
 104 
 105     switch (WalkState->Opcode)
 106     {
 107     case AML_FATAL_OP:          /* Fatal (FatalType  FatalCode  FatalArg) */
 108 
 109         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
 110             "FatalOp: Type %X Code %X Arg %X <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
 111             (UINT32) Operand[0]->Integer.Value,
 112             (UINT32) Operand[1]->Integer.Value,
 113             (UINT32) Operand[2]->Integer.Value));
 114 
 115         Fatal = ACPI_ALLOCATE (sizeof (ACPI_SIGNAL_FATAL_INFO));
 116         if (Fatal)
 117         {
 118             Fatal->Type     = (UINT32) Operand[0]->Integer.Value;
 119             Fatal->Code     = (UINT32) Operand[1]->Integer.Value;
 120             Fatal->Argument = (UINT32) Operand[2]->Integer.Value;
 121         }
 122 
 123         /* Always signal the OS! */
 124 
 125         Status = AcpiOsSignal (ACPI_SIGNAL_FATAL, Fatal);
 126 
 127         /* Might return while OS is shutting down, just continue */
 128 
 129         ACPI_FREE (Fatal);
 130         break;
 131 
 132     default:
 133 
 134         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
 135             WalkState->Opcode));
 136         Status = AE_AML_BAD_OPCODE;
 137         goto Cleanup;
 138     }
 139 
 140 
 141 Cleanup:
 142 
 143     return_ACPI_STATUS (Status);
 144 }
 145 
 146 
 147 /*******************************************************************************
 148  *
 149  * FUNCTION:    AcpiExOpcode_3A_1T_1R
 150  *
 151  * PARAMETERS:  WalkState           - Current walk state
 152  *
 153  * RETURN:      Status
 154  *
 155  * DESCRIPTION: Execute Triadic operator (3 operands)
 156  *
 157  ******************************************************************************/
 158 
 159 ACPI_STATUS
 160 AcpiExOpcode_3A_1T_1R (
 161     ACPI_WALK_STATE         *WalkState)
 162 {
 163     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
 164     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
 165     char                    *Buffer = NULL;
 166     ACPI_STATUS             Status = AE_OK;
 167     UINT64                  Index;
 168     ACPI_SIZE               Length;
 169 
 170 
 171     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_1T_1R,
 172         AcpiPsGetOpcodeName (WalkState->Opcode));
 173 
 174 
 175     switch (WalkState->Opcode)
 176     {
 177     case AML_MID_OP:    /* Mid (Source[0], Index[1], Length[2], Result[3]) */
 178         /*
 179          * Create the return object. The Source operand is guaranteed to be
 180          * either a String or a Buffer, so just use its type.
 181          */
 182         ReturnDesc = AcpiUtCreateInternalObject (
 183                         (Operand[0])->Common.Type);
 184         if (!ReturnDesc)
 185         {
 186             Status = AE_NO_MEMORY;
 187             goto Cleanup;
 188         }
 189 
 190         /* Get the Integer values from the objects */
 191 
 192         Index = Operand[1]->Integer.Value;
 193         Length = (ACPI_SIZE) Operand[2]->Integer.Value;
 194 
 195         /*
 196          * If the index is beyond the length of the String/Buffer, or if the
 197          * requested length is zero, return a zero-length String/Buffer
 198          */
 199         if (Index >= Operand[0]->String.Length)
 200         {
 201             Length = 0;
 202         }
 203 
 204         /* Truncate request if larger than the actual String/Buffer */
 205 
 206         else if ((Index + Length) > Operand[0]->String.Length)
 207         {
 208             Length = (ACPI_SIZE) Operand[0]->String.Length -
 209                         (ACPI_SIZE) Index;
 210         }
 211 
 212         /* Strings always have a sub-pointer, not so for buffers */
 213 
 214         switch ((Operand[0])->Common.Type)
 215         {
 216         case ACPI_TYPE_STRING:
 217 
 218             /* Always allocate a new buffer for the String */
 219 
 220             Buffer = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) Length + 1);
 221             if (!Buffer)
 222             {
 223                 Status = AE_NO_MEMORY;
 224                 goto Cleanup;
 225             }
 226             break;
 227 
 228         case ACPI_TYPE_BUFFER:
 229 
 230             /* If the requested length is zero, don't allocate a buffer */
 231 
 232             if (Length > 0)
 233             {
 234                 /* Allocate a new buffer for the Buffer */
 235 
 236                 Buffer = ACPI_ALLOCATE_ZEROED (Length);
 237                 if (!Buffer)
 238                 {
 239                     Status = AE_NO_MEMORY;
 240                     goto Cleanup;
 241                 }
 242             }
 243             break;
 244 
 245         default:                        /* Should not happen */
 246 
 247             Status = AE_AML_OPERAND_TYPE;
 248             goto Cleanup;
 249         }
 250 
 251         if (Buffer)
 252         {
 253             /* We have a buffer, copy the portion requested */
 254 
 255             ACPI_MEMCPY (Buffer, Operand[0]->String.Pointer + Index,
 256                          Length);
 257         }
 258 
 259         /* Set the length of the new String/Buffer */
 260 
 261         ReturnDesc->String.Pointer = Buffer;
 262         ReturnDesc->String.Length = (UINT32) Length;
 263 
 264         /* Mark buffer initialized */
 265 
 266         ReturnDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
 267         break;
 268 
 269     default:
 270 
 271         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
 272             WalkState->Opcode));
 273         Status = AE_AML_BAD_OPCODE;
 274         goto Cleanup;
 275     }
 276 
 277     /* Store the result in the target */
 278 
 279     Status = AcpiExStore (ReturnDesc, Operand[3], WalkState);
 280 
 281 Cleanup:
 282 
 283     /* Delete return object on error */
 284 
 285     if (ACPI_FAILURE (Status) || WalkState->ResultObj)
 286     {
 287         AcpiUtRemoveReference (ReturnDesc);
 288         WalkState->ResultObj = NULL;
 289     }
 290 
 291     /* Set the return object and exit */
 292 
 293     else
 294     {
 295         WalkState->ResultObj = ReturnDesc;
 296     }
 297     return_ACPI_STATUS (Status);
 298 }