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) {
99 /* The referenced object is the pseudo-node for the local/arg */
101 referenced_obj = obj_desc->reference.object;
106 ACPI_REPORT_ERROR (("Unknown Reference subtype in get ref %X\n",
107 obj_desc->reference.opcode));
108 return_ACPI_STATUS (AE_AML_INTERNAL);
113 case ACPI_DESC_TYPE_NAMED:
116 * A named reference that has already been resolved to a Node
118 referenced_obj = obj_desc;
124 ACPI_REPORT_ERROR (("Invalid descriptor type in get ref: %X\n",
125 ACPI_GET_DESCRIPTOR_TYPE (obj_desc)));
126 return_ACPI_STATUS (AE_TYPE);
130 /* Create a new reference object */
132 reference_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
133 if (!reference_obj) {
134 return_ACPI_STATUS (AE_NO_MEMORY);
137 reference_obj->reference.opcode = AML_REF_OF_OP;
138 reference_obj->reference.object = referenced_obj;
139 *return_desc = reference_obj;
141 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
142 obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
144 return_ACPI_STATUS (AE_OK);
148 /*******************************************************************************
150 * FUNCTION: acpi_ex_concat_template
152 * PARAMETERS: Operand0 - First source object
153 * Operand1 - Second source object
154 * actual_return_desc - Where to place the return object
155 * walk_state - Current walk state
159 * DESCRIPTION: Concatenate two resource templates
161 ******************************************************************************/
164 acpi_ex_concat_template (
165 union acpi_operand_object *operand0,
166 union acpi_operand_object *operand1,
167 union acpi_operand_object **actual_return_desc,
168 struct acpi_walk_state *walk_state)
170 union acpi_operand_object *return_desc;
178 ACPI_FUNCTION_TRACE ("ex_concat_template");
181 /* Find the end_tags in each resource template */
183 end_tag1 = acpi_ut_get_resource_end_tag (operand0);
184 end_tag2 = acpi_ut_get_resource_end_tag (operand1);
185 if (!end_tag1 || !end_tag2) {
186 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
189 /* Compute the length of each part */
191 length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
192 length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
193 2; /* Size of END_TAG */
195 /* Create a new buffer object for the result */
197 return_desc = acpi_ut_create_buffer_object (length1 + length2);
199 return_ACPI_STATUS (AE_NO_MEMORY);
202 /* Copy the templates to the new descriptor */
204 new_buf = return_desc->buffer.pointer;
205 ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
206 ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
208 /* Compute the new checksum */
210 new_buf[return_desc->buffer.length - 1] =
211 acpi_ut_generate_checksum (return_desc->buffer.pointer,
212 (return_desc->buffer.length - 1));
214 /* Return the completed template descriptor */
216 *actual_return_desc = return_desc;
217 return_ACPI_STATUS (AE_OK);
221 /*******************************************************************************
223 * FUNCTION: acpi_ex_do_concatenate
225 * PARAMETERS: Operand0 - First source object
226 * Operand1 - Second source object
227 * actual_return_desc - Where to place the return object
228 * walk_state - Current walk state
232 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
234 ******************************************************************************/
237 acpi_ex_do_concatenate (
238 union acpi_operand_object *operand0,
239 union acpi_operand_object *operand1,
240 union acpi_operand_object **actual_return_desc,
241 struct acpi_walk_state *walk_state)
243 union acpi_operand_object *local_operand1 = operand1;
244 union acpi_operand_object *return_desc;
247 acpi_size new_length;
250 ACPI_FUNCTION_TRACE ("ex_do_concatenate");
254 * Convert the second operand if necessary. The first operand
255 * determines the type of the second operand, (See the Data Types
256 * section of the ACPI specification.) Both object types are
257 * guaranteed to be either Integer/String/Buffer by the operand
258 * resolution mechanism.
260 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
261 case ACPI_TYPE_INTEGER:
262 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
265 case ACPI_TYPE_STRING:
266 status = acpi_ex_convert_to_string (operand1, &local_operand1,
267 ACPI_IMPLICIT_CONVERT_HEX);
270 case ACPI_TYPE_BUFFER:
271 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
275 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
276 ACPI_GET_OBJECT_TYPE (operand0)));
277 status = AE_AML_INTERNAL;
280 if (ACPI_FAILURE (status)) {
285 * Both operands are now known to be the same object type
286 * (Both are Integer, String, or Buffer), and we can now perform the
291 * There are three cases to handle:
293 * 1) Two Integers concatenated to produce a new Buffer
294 * 2) Two Strings concatenated to produce a new String
295 * 3) Two Buffers concatenated to produce a new Buffer
297 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
298 case ACPI_TYPE_INTEGER:
300 /* Result of two Integers is a Buffer */
301 /* Need enough buffer space for two integers */
303 return_desc = acpi_ut_create_buffer_object (
304 ACPI_MUL_2 (acpi_gbl_integer_byte_width));
306 status = AE_NO_MEMORY;
310 new_buf = (char *) return_desc->buffer.pointer;
312 /* Copy the first integer, LSB first */
314 ACPI_MEMCPY (new_buf,
315 &operand0->integer.value,
316 acpi_gbl_integer_byte_width);
318 /* Copy the second integer (LSB first) after the first */
320 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
321 &local_operand1->integer.value,
322 acpi_gbl_integer_byte_width);
325 case ACPI_TYPE_STRING:
327 /* Result of two Strings is a String */
329 new_length = (acpi_size) operand0->string.length +
330 (acpi_size) local_operand1->string.length;
331 if (new_length > ACPI_MAX_STRING_CONVERSION) {
332 status = AE_AML_STRING_LIMIT;
336 return_desc = acpi_ut_create_string_object (new_length);
338 status = AE_NO_MEMORY;
342 new_buf = return_desc->string.pointer;
344 /* Concatenate the strings */
346 ACPI_STRCPY (new_buf,
347 operand0->string.pointer);
348 ACPI_STRCPY (new_buf + operand0->string.length,
349 local_operand1->string.pointer);
352 case ACPI_TYPE_BUFFER:
354 /* Result of two Buffers is a Buffer */
356 return_desc = acpi_ut_create_buffer_object (
357 (acpi_size) operand0->buffer.length +
358 (acpi_size) local_operand1->buffer.length);
360 status = AE_NO_MEMORY;
364 new_buf = (char *) return_desc->buffer.pointer;
366 /* Concatenate the buffers */
368 ACPI_MEMCPY (new_buf,
369 operand0->buffer.pointer,
370 operand0->buffer.length);
371 ACPI_MEMCPY (new_buf + operand0->buffer.length,
372 local_operand1->buffer.pointer,
373 local_operand1->buffer.length);
378 /* Invalid object type, should not happen here */
380 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
381 ACPI_GET_OBJECT_TYPE (operand0)));
382 status =AE_AML_INTERNAL;
386 *actual_return_desc = return_desc;
389 if (local_operand1 != operand1) {
390 acpi_ut_remove_reference (local_operand1);
392 return_ACPI_STATUS (status);
396 /*******************************************************************************
398 * FUNCTION: acpi_ex_do_math_op
400 * PARAMETERS: Opcode - AML opcode
401 * Integer0 - Integer operand #0
402 * Integer1 - Integer operand #1
404 * RETURN: Integer result of the operation
406 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
407 * math functions here is to prevent a lot of pointer dereferencing
408 * to obtain the operands.
410 ******************************************************************************/
415 acpi_integer integer0,
416 acpi_integer integer1)
419 ACPI_FUNCTION_ENTRY ();
423 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
425 return (integer0 + integer1);
428 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
430 return (integer0 & integer1);
433 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
435 return (~(integer0 & integer1));
438 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
440 return (integer0 | integer1);
443 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
445 return (~(integer0 | integer1));
448 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
450 return (integer0 ^ integer1);
453 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
455 return (integer0 * integer1);
458 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
460 return (integer0 << integer1);
463 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
465 return (integer0 >> integer1);
468 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
470 return (integer0 - integer1);
479 /*******************************************************************************
481 * FUNCTION: acpi_ex_do_logical_numeric_op
483 * PARAMETERS: Opcode - AML opcode
484 * Integer0 - Integer operand #0
485 * Integer1 - Integer operand #1
486 * logical_result - TRUE/FALSE result of the operation
490 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
491 * operators (LAnd and LOr), both operands must be integers.
493 * Note: cleanest machine code seems to be produced by the code
494 * below, rather than using statements of the form:
495 * Result = (Integer0 && Integer1);
497 ******************************************************************************/
500 acpi_ex_do_logical_numeric_op (
502 acpi_integer integer0,
503 acpi_integer integer1,
506 acpi_status status = AE_OK;
507 u8 local_result = FALSE;
510 ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
514 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
516 if (integer0 && integer1) {
521 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
523 if (integer0 || integer1) {
529 status = AE_AML_INTERNAL;
533 /* Return the logical result and status */
535 *logical_result = local_result;
536 return_ACPI_STATUS (status);
540 /*******************************************************************************
542 * FUNCTION: acpi_ex_do_logical_op
544 * PARAMETERS: Opcode - AML opcode
545 * Operand0 - operand #0
546 * Operand1 - operand #1
547 * logical_result - TRUE/FALSE result of the operation
551 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
552 * functions here is to prevent a lot of pointer dereferencing
553 * to obtain the operands and to simplify the generation of the
554 * logical value. For the Numeric operators (LAnd and LOr), both
555 * operands must be integers. For the other logical operators,
556 * operands can be any combination of Integer/String/Buffer. The
557 * first operand determines the type to which the second operand
560 * Note: cleanest machine code seems to be produced by the code
561 * below, rather than using statements of the form:
562 * Result = (Operand0 == Operand1);
564 ******************************************************************************/
567 acpi_ex_do_logical_op (
569 union acpi_operand_object *operand0,
570 union acpi_operand_object *operand1,
573 union acpi_operand_object *local_operand1 = operand1;
574 acpi_integer integer0;
575 acpi_integer integer1;
578 acpi_status status = AE_OK;
579 u8 local_result = FALSE;
583 ACPI_FUNCTION_TRACE ("ex_do_logical_op");
587 * Convert the second operand if necessary. The first operand
588 * determines the type of the second operand, (See the Data Types
589 * section of the ACPI 3.0+ specification.) Both object types are
590 * guaranteed to be either Integer/String/Buffer by the operand
591 * resolution mechanism.
593 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
594 case ACPI_TYPE_INTEGER:
595 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
598 case ACPI_TYPE_STRING:
599 status = acpi_ex_convert_to_string (operand1, &local_operand1,
600 ACPI_IMPLICIT_CONVERT_HEX);
603 case ACPI_TYPE_BUFFER:
604 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
608 status = AE_AML_INTERNAL;
612 if (ACPI_FAILURE (status)) {
617 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
619 if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
621 * 1) Both operands are of type integer
622 * Note: local_operand1 may have changed above
624 integer0 = operand0->integer.value;
625 integer1 = local_operand1->integer.value;
628 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
630 if (integer0 == integer1) {
635 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
637 if (integer0 > integer1) {
642 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
644 if (integer0 < integer1) {
650 status = AE_AML_INTERNAL;
656 * 2) Both operands are Strings or both are Buffers
657 * Note: Code below takes advantage of common Buffer/String
658 * object fields. local_operand1 may have changed above. Use
659 * memcmp to handle nulls in buffers.
661 length0 = operand0->buffer.length;
662 length1 = local_operand1->buffer.length;
664 /* Lexicographic compare: compare the data bytes */
666 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
667 (const char * ) local_operand1->buffer.pointer,
668 (length0 > length1) ? length1 : length0);
671 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
673 /* Length and all bytes must be equal */
675 if ((length0 == length1) &&
677 /* Length and all bytes match ==> TRUE */
683 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
687 goto cleanup; /* TRUE */
690 goto cleanup; /* FALSE */
693 /* Bytes match (to shortest length), compare lengths */
695 if (length0 > length1) {
700 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
703 goto cleanup; /* FALSE */
707 goto cleanup; /* TRUE */
710 /* Bytes match (to shortest length), compare lengths */
712 if (length0 < length1) {
718 status = AE_AML_INTERNAL;
725 /* New object was created if implicit conversion performed - delete */
727 if (local_operand1 != operand1) {
728 acpi_ut_remove_reference (local_operand1);
731 /* Return the logical result and status */
733 *logical_result = local_result;
734 return_ACPI_STATUS (status);