2 /******************************************************************************
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, R. Byron Moore
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.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exmisc")
55 /*******************************************************************************
57 * FUNCTION: acpi_ex_get_object_reference
59 * PARAMETERS: obj_desc - Create a reference to this object
60 * return_desc - Where to store the reference
61 * walk_state - Current state
65 * DESCRIPTION: Obtain and return a "reference" to the target object
66 * Common code for the ref_of_op and the cond_ref_of_op.
68 ******************************************************************************/
71 acpi_ex_get_object_reference (
72 union acpi_operand_object *obj_desc,
73 union acpi_operand_object **return_desc,
74 struct acpi_walk_state *walk_state)
76 union acpi_operand_object *reference_obj;
77 union acpi_operand_object *referenced_obj;
80 ACPI_FUNCTION_TRACE_PTR ("ex_get_object_reference", obj_desc);
85 switch (ACPI_GET_DESCRIPTOR_TYPE (obj_desc)) {
86 case ACPI_DESC_TYPE_OPERAND:
88 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
89 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
93 * Must be a reference to a Local or Arg
95 switch (obj_desc->reference.opcode) {
100 /* The referenced object is the pseudo-node for the local/arg */
102 referenced_obj = obj_desc->reference.object;
107 ACPI_REPORT_ERROR (("Unknown Reference opcode in get_reference %X\n",
108 obj_desc->reference.opcode));
109 return_ACPI_STATUS (AE_AML_INTERNAL);
114 case ACPI_DESC_TYPE_NAMED:
117 * A named reference that has already been resolved to a Node
119 referenced_obj = obj_desc;
125 ACPI_REPORT_ERROR (("Invalid descriptor type in get_reference: %X\n",
126 ACPI_GET_DESCRIPTOR_TYPE (obj_desc)));
127 return_ACPI_STATUS (AE_TYPE);
131 /* Create a new reference object */
133 reference_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
134 if (!reference_obj) {
135 return_ACPI_STATUS (AE_NO_MEMORY);
138 reference_obj->reference.opcode = AML_REF_OF_OP;
139 reference_obj->reference.object = referenced_obj;
140 *return_desc = reference_obj;
142 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
143 obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
145 return_ACPI_STATUS (AE_OK);
149 /*******************************************************************************
151 * FUNCTION: acpi_ex_concat_template
153 * PARAMETERS: Operand0 - First source object
154 * Operand1 - Second source object
155 * actual_return_desc - Where to place the return object
156 * walk_state - Current walk state
160 * DESCRIPTION: Concatenate two resource templates
162 ******************************************************************************/
165 acpi_ex_concat_template (
166 union acpi_operand_object *operand0,
167 union acpi_operand_object *operand1,
168 union acpi_operand_object **actual_return_desc,
169 struct acpi_walk_state *walk_state)
171 union acpi_operand_object *return_desc;
179 ACPI_FUNCTION_TRACE ("ex_concat_template");
182 /* Find the end_tags in each resource template */
184 end_tag1 = acpi_ut_get_resource_end_tag (operand0);
185 end_tag2 = acpi_ut_get_resource_end_tag (operand1);
186 if (!end_tag1 || !end_tag2) {
187 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
190 /* Compute the length of each part */
192 length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
193 length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
194 2; /* Size of END_TAG */
196 /* Create a new buffer object for the result */
198 return_desc = acpi_ut_create_buffer_object (length1 + length2);
200 return_ACPI_STATUS (AE_NO_MEMORY);
203 /* Copy the templates to the new descriptor */
205 new_buf = return_desc->buffer.pointer;
206 ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
207 ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
209 /* Compute the new checksum */
211 new_buf[return_desc->buffer.length - 1] =
212 acpi_ut_generate_checksum (return_desc->buffer.pointer,
213 (return_desc->buffer.length - 1));
215 /* Return the completed template descriptor */
217 *actual_return_desc = return_desc;
218 return_ACPI_STATUS (AE_OK);
222 /*******************************************************************************
224 * FUNCTION: acpi_ex_do_concatenate
226 * PARAMETERS: Operand0 - First source object
227 * Operand1 - Second source object
228 * actual_return_desc - Where to place the return object
229 * walk_state - Current walk state
233 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
235 ******************************************************************************/
238 acpi_ex_do_concatenate (
239 union acpi_operand_object *operand0,
240 union acpi_operand_object *operand1,
241 union acpi_operand_object **actual_return_desc,
242 struct acpi_walk_state *walk_state)
244 union acpi_operand_object *local_operand1 = operand1;
245 union acpi_operand_object *return_desc;
248 acpi_size new_length;
251 ACPI_FUNCTION_TRACE ("ex_do_concatenate");
255 * Convert the second operand if necessary. The first operand
256 * determines the type of the second operand, (See the Data Types
257 * section of the ACPI specification.) Both object types are
258 * guaranteed to be either Integer/String/Buffer by the operand
259 * resolution mechanism.
261 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
262 case ACPI_TYPE_INTEGER:
263 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
266 case ACPI_TYPE_STRING:
267 status = acpi_ex_convert_to_string (operand1, &local_operand1,
268 ACPI_IMPLICIT_CONVERT_HEX);
271 case ACPI_TYPE_BUFFER:
272 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
276 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
277 ACPI_GET_OBJECT_TYPE (operand0)));
278 status = AE_AML_INTERNAL;
281 if (ACPI_FAILURE (status)) {
286 * Both operands are now known to be the same object type
287 * (Both are Integer, String, or Buffer), and we can now perform the
292 * There are three cases to handle:
294 * 1) Two Integers concatenated to produce a new Buffer
295 * 2) Two Strings concatenated to produce a new String
296 * 3) Two Buffers concatenated to produce a new Buffer
298 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
299 case ACPI_TYPE_INTEGER:
301 /* Result of two Integers is a Buffer */
302 /* Need enough buffer space for two integers */
304 return_desc = acpi_ut_create_buffer_object (
305 ACPI_MUL_2 (acpi_gbl_integer_byte_width));
307 status = AE_NO_MEMORY;
311 new_buf = (char *) return_desc->buffer.pointer;
313 /* Copy the first integer, LSB first */
315 ACPI_MEMCPY (new_buf,
316 &operand0->integer.value,
317 acpi_gbl_integer_byte_width);
319 /* Copy the second integer (LSB first) after the first */
321 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
322 &local_operand1->integer.value,
323 acpi_gbl_integer_byte_width);
326 case ACPI_TYPE_STRING:
328 /* Result of two Strings is a String */
330 new_length = (acpi_size) operand0->string.length +
331 (acpi_size) local_operand1->string.length;
332 if (new_length > ACPI_MAX_STRING_CONVERSION) {
333 status = AE_AML_STRING_LIMIT;
337 return_desc = acpi_ut_create_string_object (new_length);
339 status = AE_NO_MEMORY;
343 new_buf = return_desc->string.pointer;
345 /* Concatenate the strings */
347 ACPI_STRCPY (new_buf,
348 operand0->string.pointer);
349 ACPI_STRCPY (new_buf + operand0->string.length,
350 local_operand1->string.pointer);
353 case ACPI_TYPE_BUFFER:
355 /* Result of two Buffers is a Buffer */
357 return_desc = acpi_ut_create_buffer_object (
358 (acpi_size) operand0->buffer.length +
359 (acpi_size) local_operand1->buffer.length);
361 status = AE_NO_MEMORY;
365 new_buf = (char *) return_desc->buffer.pointer;
367 /* Concatenate the buffers */
369 ACPI_MEMCPY (new_buf,
370 operand0->buffer.pointer,
371 operand0->buffer.length);
372 ACPI_MEMCPY (new_buf + operand0->buffer.length,
373 local_operand1->buffer.pointer,
374 local_operand1->buffer.length);
379 /* Invalid object type, should not happen here */
381 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
382 ACPI_GET_OBJECT_TYPE (operand0)));
383 status =AE_AML_INTERNAL;
387 *actual_return_desc = return_desc;
390 if (local_operand1 != operand1) {
391 acpi_ut_remove_reference (local_operand1);
393 return_ACPI_STATUS (status);
397 /*******************************************************************************
399 * FUNCTION: acpi_ex_do_math_op
401 * PARAMETERS: Opcode - AML opcode
402 * Integer0 - Integer operand #0
403 * Integer1 - Integer operand #1
405 * RETURN: Integer result of the operation
407 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
408 * math functions here is to prevent a lot of pointer dereferencing
409 * to obtain the operands.
411 ******************************************************************************/
416 acpi_integer integer0,
417 acpi_integer integer1)
420 ACPI_FUNCTION_ENTRY ();
424 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
426 return (integer0 + integer1);
429 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
431 return (integer0 & integer1);
434 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
436 return (~(integer0 & integer1));
439 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
441 return (integer0 | integer1);
444 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
446 return (~(integer0 | integer1));
449 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
451 return (integer0 ^ integer1);
454 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
456 return (integer0 * integer1);
459 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
461 return (integer0 << integer1);
464 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
466 return (integer0 >> integer1);
469 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
471 return (integer0 - integer1);
480 /*******************************************************************************
482 * FUNCTION: acpi_ex_do_logical_numeric_op
484 * PARAMETERS: Opcode - AML opcode
485 * Integer0 - Integer operand #0
486 * Integer1 - Integer operand #1
487 * logical_result - TRUE/FALSE result of the operation
491 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
492 * operators (LAnd and LOr), both operands must be integers.
494 * Note: cleanest machine code seems to be produced by the code
495 * below, rather than using statements of the form:
496 * Result = (Integer0 && Integer1);
498 ******************************************************************************/
501 acpi_ex_do_logical_numeric_op (
503 acpi_integer integer0,
504 acpi_integer integer1,
507 acpi_status status = AE_OK;
508 u8 local_result = FALSE;
511 ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
515 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
517 if (integer0 && integer1) {
522 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
524 if (integer0 || integer1) {
530 status = AE_AML_INTERNAL;
534 /* Return the logical result and status */
536 *logical_result = local_result;
537 return_ACPI_STATUS (status);
541 /*******************************************************************************
543 * FUNCTION: acpi_ex_do_logical_op
545 * PARAMETERS: Opcode - AML opcode
546 * Operand0 - operand #0
547 * Operand1 - operand #1
548 * logical_result - TRUE/FALSE result of the operation
552 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
553 * functions here is to prevent a lot of pointer dereferencing
554 * to obtain the operands and to simplify the generation of the
555 * logical value. For the Numeric operators (LAnd and LOr), both
556 * operands must be integers. For the other logical operators,
557 * operands can be any combination of Integer/String/Buffer. The
558 * first operand determines the type to which the second operand
561 * Note: cleanest machine code seems to be produced by the code
562 * below, rather than using statements of the form:
563 * Result = (Operand0 == Operand1);
565 ******************************************************************************/
568 acpi_ex_do_logical_op (
570 union acpi_operand_object *operand0,
571 union acpi_operand_object *operand1,
574 union acpi_operand_object *local_operand1 = operand1;
575 acpi_integer integer0;
576 acpi_integer integer1;
579 acpi_status status = AE_OK;
580 u8 local_result = FALSE;
584 ACPI_FUNCTION_TRACE ("ex_do_logical_op");
588 * Convert the second operand if necessary. The first operand
589 * determines the type of the second operand, (See the Data Types
590 * section of the ACPI 3.0+ specification.) Both object types are
591 * guaranteed to be either Integer/String/Buffer by the operand
592 * resolution mechanism.
594 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
595 case ACPI_TYPE_INTEGER:
596 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
599 case ACPI_TYPE_STRING:
600 status = acpi_ex_convert_to_string (operand1, &local_operand1,
601 ACPI_IMPLICIT_CONVERT_HEX);
604 case ACPI_TYPE_BUFFER:
605 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
609 status = AE_AML_INTERNAL;
613 if (ACPI_FAILURE (status)) {
618 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
620 if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
622 * 1) Both operands are of type integer
623 * Note: local_operand1 may have changed above
625 integer0 = operand0->integer.value;
626 integer1 = local_operand1->integer.value;
629 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
631 if (integer0 == integer1) {
636 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
638 if (integer0 > integer1) {
643 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
645 if (integer0 < integer1) {
651 status = AE_AML_INTERNAL;
657 * 2) Both operands are Strings or both are Buffers
658 * Note: Code below takes advantage of common Buffer/String
659 * object fields. local_operand1 may have changed above. Use
660 * memcmp to handle nulls in buffers.
662 length0 = operand0->buffer.length;
663 length1 = local_operand1->buffer.length;
665 /* Lexicographic compare: compare the data bytes */
667 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
668 (const char * ) local_operand1->buffer.pointer,
669 (length0 > length1) ? length1 : length0);
672 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
674 /* Length and all bytes must be equal */
676 if ((length0 == length1) &&
678 /* Length and all bytes match ==> TRUE */
684 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
688 goto cleanup; /* TRUE */
691 goto cleanup; /* FALSE */
694 /* Bytes match (to shortest length), compare lengths */
696 if (length0 > length1) {
701 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
704 goto cleanup; /* FALSE */
708 goto cleanup; /* TRUE */
711 /* Bytes match (to shortest length), compare lengths */
713 if (length0 < length1) {
719 status = AE_AML_INTERNAL;
726 /* New object was created if implicit conversion performed - delete */
728 if (local_operand1 != operand1) {
729 acpi_ut_remove_reference (local_operand1);
732 /* Return the logical result and status */
734 *logical_result = local_result;
735 return_ACPI_STATUS (status);