1 /******************************************************************************
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exmisc")
55 /*******************************************************************************
57 * FUNCTION: AcpiExGetObjectReference
59 * PARAMETERS: ObjDesc - Create a reference to this object
60 * ReturnDesc - Where to store the reference
61 * WalkState - Current state
65 * DESCRIPTION: Obtain and return a "reference" to the target object
66 * Common code for the RefOfOp and the CondRefOfOp.
68 ******************************************************************************/
71 AcpiExGetObjectReference (
72 ACPI_OPERAND_OBJECT *ObjDesc,
73 ACPI_OPERAND_OBJECT **ReturnDesc,
74 ACPI_WALK_STATE *WalkState)
76 ACPI_OPERAND_OBJECT *ReferenceObj;
77 ACPI_OPERAND_OBJECT *ReferencedObj;
80 ACPI_FUNCTION_TRACE_PTR (ExGetObjectReference, ObjDesc);
85 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
87 case ACPI_DESC_TYPE_OPERAND:
89 if (ObjDesc->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
91 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
95 * Must be a reference to a Local or Arg
97 switch (ObjDesc->Reference.Class)
99 case ACPI_REFCLASS_LOCAL:
100 case ACPI_REFCLASS_ARG:
101 case ACPI_REFCLASS_DEBUG:
103 /* The referenced object is the pseudo-node for the local/arg */
105 ReferencedObj = ObjDesc->Reference.Object;
110 ACPI_ERROR ((AE_INFO, "Unknown Reference Class 0x%2.2X",
111 ObjDesc->Reference.Class));
112 return_ACPI_STATUS (AE_AML_INTERNAL);
116 case ACPI_DESC_TYPE_NAMED:
118 * A named reference that has already been resolved to a Node
120 ReferencedObj = ObjDesc;
125 ACPI_ERROR ((AE_INFO, "Invalid descriptor type 0x%X",
126 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
127 return_ACPI_STATUS (AE_TYPE);
131 /* Create a new reference object */
133 ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
136 return_ACPI_STATUS (AE_NO_MEMORY);
139 ReferenceObj->Reference.Class = ACPI_REFCLASS_REFOF;
140 ReferenceObj->Reference.Object = ReferencedObj;
141 *ReturnDesc = ReferenceObj;
143 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
144 "Object %p Type [%s], returning Reference %p\n",
145 ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
147 return_ACPI_STATUS (AE_OK);
151 /*******************************************************************************
153 * FUNCTION: AcpiExConcatTemplate
155 * PARAMETERS: Operand0 - First source object
156 * Operand1 - Second source object
157 * ActualReturnDesc - Where to place the return object
158 * WalkState - Current walk state
162 * DESCRIPTION: Concatenate two resource templates
164 ******************************************************************************/
167 AcpiExConcatTemplate (
168 ACPI_OPERAND_OBJECT *Operand0,
169 ACPI_OPERAND_OBJECT *Operand1,
170 ACPI_OPERAND_OBJECT **ActualReturnDesc,
171 ACPI_WALK_STATE *WalkState)
174 ACPI_OPERAND_OBJECT *ReturnDesc;
182 ACPI_FUNCTION_TRACE (ExConcatTemplate);
186 * Find the EndTag descriptor in each resource template.
187 * Note1: returned pointers point TO the EndTag, not past it.
188 * Note2: zero-length buffers are allowed; treated like one EndTag
191 /* Get the length of the first resource template */
193 Status = AcpiUtGetResourceEndTag (Operand0, &EndTag);
194 if (ACPI_FAILURE (Status))
196 return_ACPI_STATUS (Status);
199 Length0 = ACPI_PTR_DIFF (EndTag, Operand0->Buffer.Pointer);
201 /* Get the length of the second resource template */
203 Status = AcpiUtGetResourceEndTag (Operand1, &EndTag);
204 if (ACPI_FAILURE (Status))
206 return_ACPI_STATUS (Status);
209 Length1 = ACPI_PTR_DIFF (EndTag, Operand1->Buffer.Pointer);
211 /* Combine both lengths, minimum size will be 2 for EndTag */
213 NewLength = Length0 + Length1 + sizeof (AML_RESOURCE_END_TAG);
215 /* Create a new buffer object for the result (with one EndTag) */
217 ReturnDesc = AcpiUtCreateBufferObject (NewLength);
220 return_ACPI_STATUS (AE_NO_MEMORY);
224 * Copy the templates to the new buffer, 0 first, then 1 follows. One
225 * EndTag descriptor is copied from Operand1.
227 NewBuf = ReturnDesc->Buffer.Pointer;
228 ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer, Length0);
229 ACPI_MEMCPY (NewBuf + Length0, Operand1->Buffer.Pointer, Length1);
231 /* Insert EndTag and set the checksum to zero, means "ignore checksum" */
233 NewBuf[NewLength - 1] = 0;
234 NewBuf[NewLength - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
236 /* Return the completed resource template */
238 *ActualReturnDesc = ReturnDesc;
239 return_ACPI_STATUS (AE_OK);
243 /*******************************************************************************
245 * FUNCTION: AcpiExDoConcatenate
247 * PARAMETERS: Operand0 - First source object
248 * Operand1 - Second source object
249 * ActualReturnDesc - Where to place the return object
250 * WalkState - Current walk state
254 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
256 ******************************************************************************/
259 AcpiExDoConcatenate (
260 ACPI_OPERAND_OBJECT *Operand0,
261 ACPI_OPERAND_OBJECT *Operand1,
262 ACPI_OPERAND_OBJECT **ActualReturnDesc,
263 ACPI_WALK_STATE *WalkState)
265 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
266 ACPI_OPERAND_OBJECT *ReturnDesc;
271 ACPI_FUNCTION_TRACE (ExDoConcatenate);
275 * Convert the second operand if necessary. The first operand
276 * determines the type of the second operand, (See the Data Types
277 * section of the ACPI specification.) Both object types are
278 * guaranteed to be either Integer/String/Buffer by the operand
279 * resolution mechanism.
281 switch (Operand0->Common.Type)
283 case ACPI_TYPE_INTEGER:
285 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
288 case ACPI_TYPE_STRING:
290 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
291 ACPI_IMPLICIT_CONVERT_HEX);
294 case ACPI_TYPE_BUFFER:
296 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
301 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
302 Operand0->Common.Type));
303 Status = AE_AML_INTERNAL;
306 if (ACPI_FAILURE (Status))
312 * Both operands are now known to be the same object type
313 * (Both are Integer, String, or Buffer), and we can now perform the
318 * There are three cases to handle:
320 * 1) Two Integers concatenated to produce a new Buffer
321 * 2) Two Strings concatenated to produce a new String
322 * 3) Two Buffers concatenated to produce a new Buffer
324 switch (Operand0->Common.Type)
326 case ACPI_TYPE_INTEGER:
328 /* Result of two Integers is a Buffer */
329 /* Need enough buffer space for two integers */
331 ReturnDesc = AcpiUtCreateBufferObject ((ACPI_SIZE)
332 ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
335 Status = AE_NO_MEMORY;
339 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
341 /* Copy the first integer, LSB first */
343 ACPI_MEMCPY (NewBuf, &Operand0->Integer.Value,
344 AcpiGbl_IntegerByteWidth);
346 /* Copy the second integer (LSB first) after the first */
348 ACPI_MEMCPY (NewBuf + AcpiGbl_IntegerByteWidth,
349 &LocalOperand1->Integer.Value,
350 AcpiGbl_IntegerByteWidth);
353 case ACPI_TYPE_STRING:
355 /* Result of two Strings is a String */
357 ReturnDesc = AcpiUtCreateStringObject (
358 ((ACPI_SIZE) Operand0->String.Length +
359 LocalOperand1->String.Length));
362 Status = AE_NO_MEMORY;
366 NewBuf = ReturnDesc->String.Pointer;
368 /* Concatenate the strings */
370 ACPI_STRCPY (NewBuf, Operand0->String.Pointer);
371 ACPI_STRCPY (NewBuf + Operand0->String.Length,
372 LocalOperand1->String.Pointer);
375 case ACPI_TYPE_BUFFER:
377 /* Result of two Buffers is a Buffer */
379 ReturnDesc = AcpiUtCreateBufferObject (
380 ((ACPI_SIZE) Operand0->Buffer.Length +
381 LocalOperand1->Buffer.Length));
384 Status = AE_NO_MEMORY;
388 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
390 /* Concatenate the buffers */
392 ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer,
393 Operand0->Buffer.Length);
394 ACPI_MEMCPY (NewBuf + Operand0->Buffer.Length,
395 LocalOperand1->Buffer.Pointer,
396 LocalOperand1->Buffer.Length);
401 /* Invalid object type, should not happen here */
403 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
404 Operand0->Common.Type));
405 Status =AE_AML_INTERNAL;
409 *ActualReturnDesc = ReturnDesc;
412 if (LocalOperand1 != Operand1)
414 AcpiUtRemoveReference (LocalOperand1);
416 return_ACPI_STATUS (Status);
420 /*******************************************************************************
422 * FUNCTION: AcpiExDoMathOp
424 * PARAMETERS: Opcode - AML opcode
425 * Integer0 - Integer operand #0
426 * Integer1 - Integer operand #1
428 * RETURN: Integer result of the operation
430 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
431 * math functions here is to prevent a lot of pointer dereferencing
432 * to obtain the operands.
434 ******************************************************************************/
443 ACPI_FUNCTION_ENTRY ();
448 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
450 return (Integer0 + Integer1);
452 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
454 return (Integer0 & Integer1);
456 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
458 return (~(Integer0 & Integer1));
460 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
462 return (Integer0 | Integer1);
464 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
466 return (~(Integer0 | Integer1));
468 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
470 return (Integer0 ^ Integer1);
472 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
474 return (Integer0 * Integer1);
476 case AML_SHIFT_LEFT_OP: /* ShiftLeft (Operand, ShiftCount, Result)*/
479 * We need to check if the shiftcount is larger than the integer bit
480 * width since the behavior of this is not well-defined in the C language.
482 if (Integer1 >= AcpiGbl_IntegerBitWidth)
486 return (Integer0 << Integer1);
488 case AML_SHIFT_RIGHT_OP: /* ShiftRight (Operand, ShiftCount, Result) */
491 * We need to check if the shiftcount is larger than the integer bit
492 * width since the behavior of this is not well-defined in the C language.
494 if (Integer1 >= AcpiGbl_IntegerBitWidth)
498 return (Integer0 >> Integer1);
500 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
502 return (Integer0 - Integer1);
511 /*******************************************************************************
513 * FUNCTION: AcpiExDoLogicalNumericOp
515 * PARAMETERS: Opcode - AML opcode
516 * Integer0 - Integer operand #0
517 * Integer1 - Integer operand #1
518 * LogicalResult - TRUE/FALSE result of the operation
522 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
523 * operators (LAnd and LOr), both operands must be integers.
525 * Note: cleanest machine code seems to be produced by the code
526 * below, rather than using statements of the form:
527 * Result = (Integer0 && Integer1);
529 ******************************************************************************/
532 AcpiExDoLogicalNumericOp (
536 BOOLEAN *LogicalResult)
538 ACPI_STATUS Status = AE_OK;
539 BOOLEAN LocalResult = FALSE;
542 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp);
547 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
549 if (Integer0 && Integer1)
555 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
557 if (Integer0 || Integer1)
565 Status = AE_AML_INTERNAL;
569 /* Return the logical result and status */
571 *LogicalResult = LocalResult;
572 return_ACPI_STATUS (Status);
576 /*******************************************************************************
578 * FUNCTION: AcpiExDoLogicalOp
580 * PARAMETERS: Opcode - AML opcode
581 * Operand0 - operand #0
582 * Operand1 - operand #1
583 * LogicalResult - TRUE/FALSE result of the operation
587 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
588 * functions here is to prevent a lot of pointer dereferencing
589 * to obtain the operands and to simplify the generation of the
590 * logical value. For the Numeric operators (LAnd and LOr), both
591 * operands must be integers. For the other logical operators,
592 * operands can be any combination of Integer/String/Buffer. The
593 * first operand determines the type to which the second operand
596 * Note: cleanest machine code seems to be produced by the code
597 * below, rather than using statements of the form:
598 * Result = (Operand0 == Operand1);
600 ******************************************************************************/
605 ACPI_OPERAND_OBJECT *Operand0,
606 ACPI_OPERAND_OBJECT *Operand1,
607 BOOLEAN *LogicalResult)
609 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
614 ACPI_STATUS Status = AE_OK;
615 BOOLEAN LocalResult = FALSE;
619 ACPI_FUNCTION_TRACE (ExDoLogicalOp);
623 * Convert the second operand if necessary. The first operand
624 * determines the type of the second operand, (See the Data Types
625 * section of the ACPI 3.0+ specification.) Both object types are
626 * guaranteed to be either Integer/String/Buffer by the operand
627 * resolution mechanism.
629 switch (Operand0->Common.Type)
631 case ACPI_TYPE_INTEGER:
633 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
636 case ACPI_TYPE_STRING:
638 Status = AcpiExConvertToString (Operand1, &LocalOperand1,
639 ACPI_IMPLICIT_CONVERT_HEX);
642 case ACPI_TYPE_BUFFER:
644 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
649 Status = AE_AML_INTERNAL;
653 if (ACPI_FAILURE (Status))
659 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
661 if (Operand0->Common.Type == ACPI_TYPE_INTEGER)
664 * 1) Both operands are of type integer
665 * Note: LocalOperand1 may have changed above
667 Integer0 = Operand0->Integer.Value;
668 Integer1 = LocalOperand1->Integer.Value;
672 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
674 if (Integer0 == Integer1)
680 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
682 if (Integer0 > Integer1)
688 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
690 if (Integer0 < Integer1)
698 Status = AE_AML_INTERNAL;
705 * 2) Both operands are Strings or both are Buffers
706 * Note: Code below takes advantage of common Buffer/String
707 * object fields. LocalOperand1 may have changed above. Use
708 * memcmp to handle nulls in buffers.
710 Length0 = Operand0->Buffer.Length;
711 Length1 = LocalOperand1->Buffer.Length;
713 /* Lexicographic compare: compare the data bytes */
715 Compare = ACPI_MEMCMP (Operand0->Buffer.Pointer,
716 LocalOperand1->Buffer.Pointer,
717 (Length0 > Length1) ? Length1 : Length0);
721 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
723 /* Length and all bytes must be equal */
725 if ((Length0 == Length1) &&
728 /* Length and all bytes match ==> TRUE */
734 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
739 goto Cleanup; /* TRUE */
743 goto Cleanup; /* FALSE */
746 /* Bytes match (to shortest length), compare lengths */
748 if (Length0 > Length1)
754 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
758 goto Cleanup; /* FALSE */
763 goto Cleanup; /* TRUE */
766 /* Bytes match (to shortest length), compare lengths */
768 if (Length0 < Length1)
776 Status = AE_AML_INTERNAL;
783 /* New object was created if implicit conversion performed - delete */
785 if (LocalOperand1 != Operand1)
787 AcpiUtRemoveReference (LocalOperand1);
790 /* Return the logical result and status */
792 *LogicalResult = LocalResult;
793 return_ACPI_STATUS (Status);