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 memcpy (NewBuf, Operand0->Buffer.Pointer, Length0);
229 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 (
291 Operand1, &LocalOperand1, 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 (
332 (ACPI_SIZE) ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
335 Status = AE_NO_MEMORY;
339 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
341 /* Copy the first integer, LSB first */
343 memcpy (NewBuf, &Operand0->Integer.Value,
344 AcpiGbl_IntegerByteWidth);
346 /* Copy the second integer (LSB first) after the first */
348 memcpy (NewBuf + AcpiGbl_IntegerByteWidth,
349 &LocalOperand1->Integer.Value, AcpiGbl_IntegerByteWidth);
352 case ACPI_TYPE_STRING:
354 /* Result of two Strings is a String */
356 ReturnDesc = AcpiUtCreateStringObject (
357 ((ACPI_SIZE) Operand0->String.Length +
358 LocalOperand1->String.Length));
361 Status = AE_NO_MEMORY;
365 NewBuf = ReturnDesc->String.Pointer;
367 /* Concatenate the strings */
369 strcpy (NewBuf, Operand0->String.Pointer);
370 strcpy (NewBuf + Operand0->String.Length,
371 LocalOperand1->String.Pointer);
374 case ACPI_TYPE_BUFFER:
376 /* Result of two Buffers is a Buffer */
378 ReturnDesc = AcpiUtCreateBufferObject (
379 ((ACPI_SIZE) Operand0->Buffer.Length +
380 LocalOperand1->Buffer.Length));
383 Status = AE_NO_MEMORY;
387 NewBuf = (char *) ReturnDesc->Buffer.Pointer;
389 /* Concatenate the buffers */
391 memcpy (NewBuf, Operand0->Buffer.Pointer,
392 Operand0->Buffer.Length);
393 memcpy (NewBuf + Operand0->Buffer.Length,
394 LocalOperand1->Buffer.Pointer,
395 LocalOperand1->Buffer.Length);
400 /* Invalid object type, should not happen here */
402 ACPI_ERROR ((AE_INFO, "Invalid object type: 0x%X",
403 Operand0->Common.Type));
404 Status =AE_AML_INTERNAL;
408 *ActualReturnDesc = ReturnDesc;
411 if (LocalOperand1 != Operand1)
413 AcpiUtRemoveReference (LocalOperand1);
415 return_ACPI_STATUS (Status);
419 /*******************************************************************************
421 * FUNCTION: AcpiExDoMathOp
423 * PARAMETERS: Opcode - AML opcode
424 * Integer0 - Integer operand #0
425 * Integer1 - Integer operand #1
427 * RETURN: Integer result of the operation
429 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
430 * math functions here is to prevent a lot of pointer dereferencing
431 * to obtain the operands.
433 ******************************************************************************/
442 ACPI_FUNCTION_ENTRY ();
447 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
449 return (Integer0 + Integer1);
451 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
453 return (Integer0 & Integer1);
455 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
457 return (~(Integer0 & Integer1));
459 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
461 return (Integer0 | Integer1);
463 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
465 return (~(Integer0 | Integer1));
467 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
469 return (Integer0 ^ Integer1);
471 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
473 return (Integer0 * Integer1);
475 case AML_SHIFT_LEFT_OP: /* ShiftLeft (Operand, ShiftCount, Result)*/
478 * We need to check if the shiftcount is larger than the integer bit
479 * width since the behavior of this is not well-defined in the C language.
481 if (Integer1 >= AcpiGbl_IntegerBitWidth)
485 return (Integer0 << Integer1);
487 case AML_SHIFT_RIGHT_OP: /* ShiftRight (Operand, ShiftCount, Result) */
490 * We need to check if the shiftcount is larger than the integer bit
491 * width since the behavior of this is not well-defined in the C language.
493 if (Integer1 >= AcpiGbl_IntegerBitWidth)
497 return (Integer0 >> Integer1);
499 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
501 return (Integer0 - Integer1);
510 /*******************************************************************************
512 * FUNCTION: AcpiExDoLogicalNumericOp
514 * PARAMETERS: Opcode - AML opcode
515 * Integer0 - Integer operand #0
516 * Integer1 - Integer operand #1
517 * LogicalResult - TRUE/FALSE result of the operation
521 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
522 * operators (LAnd and LOr), both operands must be integers.
524 * Note: cleanest machine code seems to be produced by the code
525 * below, rather than using statements of the form:
526 * Result = (Integer0 && Integer1);
528 ******************************************************************************/
531 AcpiExDoLogicalNumericOp (
535 BOOLEAN *LogicalResult)
537 ACPI_STATUS Status = AE_OK;
538 BOOLEAN LocalResult = FALSE;
541 ACPI_FUNCTION_TRACE (ExDoLogicalNumericOp);
546 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
548 if (Integer0 && Integer1)
554 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
556 if (Integer0 || Integer1)
564 Status = AE_AML_INTERNAL;
568 /* Return the logical result and status */
570 *LogicalResult = LocalResult;
571 return_ACPI_STATUS (Status);
575 /*******************************************************************************
577 * FUNCTION: AcpiExDoLogicalOp
579 * PARAMETERS: Opcode - AML opcode
580 * Operand0 - operand #0
581 * Operand1 - operand #1
582 * LogicalResult - TRUE/FALSE result of the operation
586 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
587 * functions here is to prevent a lot of pointer dereferencing
588 * to obtain the operands and to simplify the generation of the
589 * logical value. For the Numeric operators (LAnd and LOr), both
590 * operands must be integers. For the other logical operators,
591 * operands can be any combination of Integer/String/Buffer. The
592 * first operand determines the type to which the second operand
595 * Note: cleanest machine code seems to be produced by the code
596 * below, rather than using statements of the form:
597 * Result = (Operand0 == Operand1);
599 ******************************************************************************/
604 ACPI_OPERAND_OBJECT *Operand0,
605 ACPI_OPERAND_OBJECT *Operand1,
606 BOOLEAN *LogicalResult)
608 ACPI_OPERAND_OBJECT *LocalOperand1 = Operand1;
613 ACPI_STATUS Status = AE_OK;
614 BOOLEAN LocalResult = FALSE;
618 ACPI_FUNCTION_TRACE (ExDoLogicalOp);
622 * Convert the second operand if necessary. The first operand
623 * determines the type of the second operand, (See the Data Types
624 * section of the ACPI 3.0+ specification.) Both object types are
625 * guaranteed to be either Integer/String/Buffer by the operand
626 * resolution mechanism.
628 switch (Operand0->Common.Type)
630 case ACPI_TYPE_INTEGER:
632 Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
635 case ACPI_TYPE_STRING:
637 Status = AcpiExConvertToString (
638 Operand1, &LocalOperand1, ACPI_IMPLICIT_CONVERT_HEX);
641 case ACPI_TYPE_BUFFER:
643 Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
648 Status = AE_AML_INTERNAL;
652 if (ACPI_FAILURE (Status))
658 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
660 if (Operand0->Common.Type == ACPI_TYPE_INTEGER)
663 * 1) Both operands are of type integer
664 * Note: LocalOperand1 may have changed above
666 Integer0 = Operand0->Integer.Value;
667 Integer1 = LocalOperand1->Integer.Value;
671 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
673 if (Integer0 == Integer1)
679 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
681 if (Integer0 > Integer1)
687 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
689 if (Integer0 < Integer1)
697 Status = AE_AML_INTERNAL;
704 * 2) Both operands are Strings or both are Buffers
705 * Note: Code below takes advantage of common Buffer/String
706 * object fields. LocalOperand1 may have changed above. Use
707 * memcmp to handle nulls in buffers.
709 Length0 = Operand0->Buffer.Length;
710 Length1 = LocalOperand1->Buffer.Length;
712 /* Lexicographic compare: compare the data bytes */
714 Compare = memcmp (Operand0->Buffer.Pointer,
715 LocalOperand1->Buffer.Pointer,
716 (Length0 > Length1) ? Length1 : Length0);
720 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
722 /* Length and all bytes must be equal */
724 if ((Length0 == Length1) &&
727 /* Length and all bytes match ==> TRUE */
733 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
738 goto Cleanup; /* TRUE */
742 goto Cleanup; /* FALSE */
745 /* Bytes match (to shortest length), compare lengths */
747 if (Length0 > Length1)
753 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
757 goto Cleanup; /* FALSE */
762 goto Cleanup; /* TRUE */
765 /* Bytes match (to shortest length), compare lengths */
767 if (Length0 < Length1)
775 Status = AE_AML_INTERNAL;
782 /* New object was created if implicit conversion performed - delete */
784 if (LocalOperand1 != Operand1)
786 AcpiUtRemoveReference (LocalOperand1);
789 /* Return the logical result and status */
791 *LogicalResult = LocalResult;
792 return_ACPI_STATUS (Status);