2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51 #include <contrib/dev/acpica/include/amlresrc.h>
54 #define _COMPONENT ACPI_EXECUTER
55 ACPI_MODULE_NAME ("exmisc")
58 /*******************************************************************************
60 * FUNCTION: AcpiExGetObjectReference
62 * PARAMETERS: ObjDesc - Create a reference to this object
63 * ReturnDesc - Where to store the reference
64 * WalkState - Current state
68 * DESCRIPTION: Obtain and return a "reference" to the target object
69 * Common code for the RefOfOp and the CondRefOfOp.
71 ******************************************************************************/
74 AcpiExGetObjectReference (
75 ACPI_OPERAND_OBJECT *ObjDesc,
76 ACPI_OPERAND_OBJECT **ReturnDesc,
77 ACPI_WALK_STATE *WalkState)
79 ACPI_OPERAND_OBJECT *ReferenceObj;
80 ACPI_OPERAND_OBJECT *ReferencedObj;
83 ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference, ObjDesc);
88 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
90 case ACPI_DESC_TYPE_OPERAND:
92 if (ObjDesc->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
94 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
98 * Must be a reference to a Local or Arg
100 switch (ObjDesc->Reference.Class)
102 case ACPI_REFCLASS_LOCAL:
103 case ACPI_REFCLASS_ARG:
104 case ACPI_REFCLASS_DEBUG:
106 /* The referenced object is the pseudo-node for the local/arg */
108 ReferencedObj = ObjDesc->Reference.Object;
113 ACPI_ERROR ((AE_INFO, "Unknown Reference Class 0x%2.2X",
114 ObjDesc->Reference.Class));
115 return_ACPI_STATUS (AE_AML_INTERNAL);
120 case ACPI_DESC_TYPE_NAMED:
123 * A named reference that has already been resolved to a Node
125 ReferencedObj = ObjDesc;
131 ACPI_ERROR ((AE_INFO, "Invalid descriptor type 0x%X",
132 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
133 return_ACPI_STATUS (AE_TYPE);
137 /* Create a new reference object */
139 ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
142 return_ACPI_STATUS (AE_NO_MEMORY);
145 ReferenceObj->Reference.Class = ACPI_REFCLASS_REFOF;
146 ReferenceObj->Reference.Object = ReferencedObj;
147 *ReturnDesc = ReferenceObj;
149 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
150 "Object %p Type [%s], returning Reference %p\n",
151 ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
153 return_ACPI_STATUS (AE_OK);
157 /*******************************************************************************
159 * FUNCTION: AcpiExConcatTemplate
161 * PARAMETERS: Operand0 - First source object
162 * Operand1 - Second source object
163 * ActualReturnDesc - Where to place the return object
164 * WalkState - Current walk state
168 * DESCRIPTION: Concatenate two resource templates
170 ******************************************************************************/
173 AcpiExConcatTemplate (
174 ACPI_OPERAND_OBJECT *Operand0,
175 ACPI_OPERAND_OBJECT *Operand1,
176 ACPI_OPERAND_OBJECT **ActualReturnDesc,
177 ACPI_WALK_STATE *WalkState)
180 ACPI_OPERAND_OBJECT *ReturnDesc;
188 ACPI_FUNCTION_TRACE (ExConcatTemplate);
192 * Find the EndTag descriptor in each resource template.
193 * Note1: returned pointers point TO the EndTag, not past it.
194 * Note2: zero-length buffers are allowed; treated like one EndTag
197 /* Get the length of the first resource template */
199 Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);
200 if (ACPI_FAILURE (Status))
202 return_ACPI_STATUS (Status);
205 Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);
207 /* Get the length of the second resource template */
209 Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);
210 if (ACPI_FAILURE (Status))
212 return_ACPI_STATUS (Status);
215 Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);
217 /* Combine both lengths, minimum size will be 2 for EndTag */
219 NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);
221 /* Create a new buffer object for the result (with one EndTag) */
223 ReturnDesc = AcpiUtCreateBufferObject (NewLength);
226 return_ACPI_STATUS (AE_NO_MEMORY);
230 * Copy the templates to the new buffer, 0 first, then 1 follows. One
231 * EndTag descriptor is copied from Operand1.
233 NewBuf = ReturnDesc->Buffer.Pointer;
234 ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer, Length0);
235 ACPI_MEMCPY (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);
237 /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
239 NewBuf[NewLength - 1] = 0;
240 NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
242 /* Return the completed resource template */
244 *ActualReturnDesc = ReturnDesc;
245 return_ACPI_STATUS (AE_OK);
249 /*******************************************************************************
251 * FUNCTION: AcpiExDoConcatenate
253 * PARAMETERS: Operand0 - First source object
254 * Operand1 - Second source object
255 * ActualReturnDesc - Where to place the return object
256 * WalkState - Current walk state
260 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
262 ******************************************************************************/
265 AcpiExDoConcatenate (
266 ACPI_OPERAND_OBJECT *Operand0,
267 ACPI_OPERAND_OBJECT *Operand1,
268 ACPI_OPERAND_OBJECT **ActualReturnDesc,
269 ACPI_WALK_STATE *WalkState)
271 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
272 ACPI_OPERAND_OBJECT *ReturnDesc;
277 ACPI_FUNCTION_TRACE (ExDoConcatenate);
281 * Convert the second operand if necessary. The first operand
282 * determines the type of the second operand, (See the Data Types
283 * section of the ACPI specification.) Both object types are
284 * guaranteed to be either Integer/String/Buffer by the operand
285 * resolution mechanism.
287 switch (Operand0->Common.Type)
289 case ACPI_TYPE_INTEGER:
290 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
293 case ACPI_TYPE_STRING:
294 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
295 ACPI_IMPLICIT_CONVERT_HEX);
298 case ACPI_TYPE_BUFFER:
299 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
303 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
304 Operand0->Common.Type));
305 Status = AE_AML_INTERNAL;
308 if (ACPI_FAILURE (Status))
314 * Both operands are now known to be the same object type
315 * (Both are Integer, String, or Buffer), and we can now perform the
320 * There are three cases to handle:
322 * 1) Two Integers concatenated to produce a new Buffer
323 * 2) Two Strings concatenated to produce a new String
324 * 3) Two Buffers concatenated to produce a new Buffer
326 switch (Operand0->Common.Type)
328 case ACPI_TYPE_INTEGER:
330 /* Result of two Integers is a Buffer */
331 /* Need enough buffer space for two integers */
333 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
334 ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
337 Status = AE_NO_MEMORY;
341 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
343 /* Copy the first integer, LSB first */
345 ACPI_MEMCPY (NewBuf, &Operand0->Integer.Value,
346 AcpiGbl_IntegerByteWidth);
348 /* Copy the second integer (LSB first) after the first */
350 ACPI_MEMCPY (NewBuf + AcpiGbl_IntegerByteWidth,
351 &LocalOperand1->Integer.Value,
352 AcpiGbl_IntegerByteWidth);
355 case ACPI_TYPE_STRING:
357 /* Result of two Strings is a String */
359 ReturnDesc = AcpiUtCreateStringObject (
360 ((ACPI_SIZE) Operand0->String.Length +
361 LocalOperand1->String.Length));
364 Status = AE_NO_MEMORY;
368 NewBuf = ReturnDesc->String.Pointer;
370 /* Concatenate the strings */
372 ACPI_STRCPY (NewBuf, Operand0->String.Pointer);
373 ACPI_STRCPY (NewBuf + Operand0->String.Length,
374 LocalOperand1->String.Pointer);
377 case ACPI_TYPE_BUFFER:
379 /* Result of two Buffers is a Buffer */
381 ReturnDesc = AcpiUtCreateBufferObject (
382 ((ACPI_SIZE) Operand0->Buffer.Length +
383 LocalOperand1->Buffer.Length));
386 Status = AE_NO_MEMORY;
390 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
392 /* Concatenate the buffers */
394 ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer,
395 Operand0->Buffer.Length);
396 ACPI_MEMCPY (NewBuf + Operand0->Buffer.Length,
397 LocalOperand1->Buffer.Pointer,
398 LocalOperand1->Buffer.Length);
403 /* Invalid object type, should not happen here */
405 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
406 Operand0->Common.Type));
407 Status =AE_AML_INTERNAL;
411 *ActualReturnDesc = ReturnDesc;
414 if (LocalOperand1 != Operand1)
416 AcpiUtRemoveReference (LocalOperand1);
418 return_ACPI_STATUS (Status);
422 /*******************************************************************************
424 * FUNCTION: AcpiExDoMathOp
426 * PARAMETERS: Opcode - AML opcode
427 * Integer0 - Integer operand #0
428 * Integer1 - Integer operand #1
430 * RETURN: Integer result of the operation
432 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
433 * math functions here is to prevent a lot of pointer dereferencing
434 * to obtain the operands.
436 ******************************************************************************/
445 ACPI_FUNCTION_ENTRY ();
450 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
452 return (Integer0 + Integer1);
455 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
457 return (Integer0 & Integer1);
460 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
462 return (~(Integer0 & Integer1));
465 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
467 return (Integer0 | Integer1);
470 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
472 return (~(Integer0 | Integer1));
475 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
477 return (Integer0 ^ Integer1);
480 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
482 return (Integer0 * Integer1);
485 case AML_SHIFT_LEFT_OP: /* ShiftLeft (Operand, ShiftCount, Result)*/
488 * We need to check if the shiftcount is larger than the integer bit
489 * width since the behavior of this is not well-defined in the C language.
491 if (Integer1 >= AcpiGbl_IntegerBitWidth)
495 return (Integer0 << Integer1);
498 case AML_SHIFT_RIGHT_OP: /* ShiftRight (Operand, ShiftCount, Result) */
501 * We need to check if the shiftcount is larger than the integer bit
502 * width since the behavior of this is not well-defined in the C language.
504 if (Integer1 >= AcpiGbl_IntegerBitWidth)
508 return (Integer0 >> Integer1);
511 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
513 return (Integer0 - Integer1);
522 /*******************************************************************************
524 * FUNCTION: AcpiExDoLogicalNumericOp
526 * PARAMETERS: Opcode - AML opcode
527 * Integer0 - Integer operand #0
528 * Integer1 - Integer operand #1
529 * LogicalResult - TRUE/FALSE result of the operation
533 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
534 * operators (LAnd and LOr), both operands must be integers.
536 * Note: cleanest machine code seems to be produced by the code
537 * below, rather than using statements of the form:
538 * Result = (Integer0 && Integer1);
540 ******************************************************************************/
543 AcpiExDoLogicalNumericOp (
547 BOOLEAN *LogicalResult)
549 ACPI_STATUS Status = AE_OK;
550 BOOLEAN LocalResult = FALSE;
553 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp);
558 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
560 if (Integer0 && Integer1)
566 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
568 if (Integer0 || Integer1)
575 Status = AE_AML_INTERNAL;
579 /* Return the logical result and status */
581 *LogicalResult = LocalResult;
582 return_ACPI_STATUS (Status);
586 /*******************************************************************************
588 * FUNCTION: AcpiExDoLogicalOp
590 * PARAMETERS: Opcode - AML opcode
591 * Operand0 - operand #0
592 * Operand1 - operand #1
593 * LogicalResult - TRUE/FALSE result of the operation
597 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
598 * functions here is to prevent a lot of pointer dereferencing
599 * to obtain the operands and to simplify the generation of the
600 * logical value. For the Numeric operators (LAnd and LOr), both
601 * operands must be integers. For the other logical operators,
602 * operands can be any combination of Integer/String/Buffer. The
603 * first operand determines the type to which the second operand
606 * Note: cleanest machine code seems to be produced by the code
607 * below, rather than using statements of the form:
608 * Result = (Operand0 == Operand1);
610 ******************************************************************************/
615 ACPI_OPERAND_OBJECT *Operand0,
616 ACPI_OPERAND_OBJECT *Operand1,
617 BOOLEAN *LogicalResult)
619 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
624 ACPI_STATUS Status = AE_OK;
625 BOOLEAN LocalResult = FALSE;
629 ACPI_FUNCTION_TRACE (ExDoLogicalOp);
633 * Convert the second operand if necessary. The first operand
634 * determines the type of the second operand, (See the Data Types
635 * section of the ACPI 3.0+ specification.) Both object types are
636 * guaranteed to be either Integer/String/Buffer by the operand
637 * resolution mechanism.
639 switch (Operand0->Common.Type)
641 case ACPI_TYPE_INTEGER:
642 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
645 case ACPI_TYPE_STRING:
646 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
647 ACPI_IMPLICIT_CONVERT_HEX);
650 case ACPI_TYPE_BUFFER:
651 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
655 Status = AE_AML_INTERNAL;
659 if (ACPI_FAILURE (Status))
665 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
667 if (Operand0->Common.Type == ACPI_TYPE_INTEGER)
670 * 1) Both operands are of type integer
671 * Note: LocalOperand1 may have changed above
673 Integer0 = Operand0->Integer.Value;
674 Integer1 = LocalOperand1->Integer.Value;
678 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
680 if (Integer0 == Integer1)
686 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
688 if (Integer0 > Integer1)
694 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
696 if (Integer0 < Integer1)
703 Status = AE_AML_INTERNAL;
710 * 2) Both operands are Strings or both are Buffers
711 * Note: Code below takes advantage of common Buffer/String
712 * object fields. LocalOperand1 may have changed above. Use
713 * memcmp to handle nulls in buffers.
715 Length0 = Operand0->Buffer.Length;
716 Length1 = LocalOperand1->Buffer.Length;
718 /* Lexicographic compare: compare the data bytes */
720 Compare = ACPI_MEMCMP (Operand0->Buffer.Pointer,
721 LocalOperand1->Buffer.Pointer,
722 (Length0 > Length1) ? Length1 : Length0);
726 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
728 /* Length and all bytes must be equal */
730 if ((Length0 == Length1) &&
733 /* Length and all bytes match ==> TRUE */
739 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
744 goto Cleanup; /* TRUE */
748 goto Cleanup; /* FALSE */
751 /* Bytes match (to shortest length), compare lengths */
753 if (Length0 > Length1)
759 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
763 goto Cleanup; /* FALSE */
768 goto Cleanup; /* TRUE */
771 /* Bytes match (to shortest length), compare lengths */
773 if (Length0 < Length1)
780 Status = AE_AML_INTERNAL;
787 /* New object was created if implicit conversion performed - delete */
789 if (LocalOperand1 != Operand1)
791 AcpiUtRemoveReference (LocalOperand1);
794 /* Return the logical result and status */
796 *LogicalResult = LocalResult;
797 return_ACPI_STATUS (Status);