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,
143 "Object %p Type [%s], returning Reference %p\n",
144 obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
146 return_ACPI_STATUS (AE_OK);
150 /*******************************************************************************
152 * FUNCTION: acpi_ex_concat_template
154 * PARAMETERS: Operand0 - First source object
155 * Operand1 - Second source object
156 * actual_return_desc - Where to place the return object
157 * walk_state - Current walk state
161 * DESCRIPTION: Concatenate two resource templates
163 ******************************************************************************/
166 acpi_ex_concat_template (
167 union acpi_operand_object *operand0,
168 union acpi_operand_object *operand1,
169 union acpi_operand_object **actual_return_desc,
170 struct acpi_walk_state *walk_state)
172 union acpi_operand_object *return_desc;
180 ACPI_FUNCTION_TRACE ("ex_concat_template");
183 /* Find the end_tags in each resource template */
185 end_tag1 = acpi_ut_get_resource_end_tag (operand0);
186 end_tag2 = acpi_ut_get_resource_end_tag (operand1);
187 if (!end_tag1 || !end_tag2) {
188 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
191 /* Compute the length of each part */
193 length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
194 length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
195 2; /* Size of END_TAG */
197 /* Create a new buffer object for the result */
199 return_desc = acpi_ut_create_buffer_object (length1 + length2);
201 return_ACPI_STATUS (AE_NO_MEMORY);
204 /* Copy the templates to the new descriptor */
206 new_buf = return_desc->buffer.pointer;
207 ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
208 ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
210 /* Compute the new checksum */
212 new_buf[return_desc->buffer.length - 1] =
213 acpi_ut_generate_checksum (return_desc->buffer.pointer,
214 (return_desc->buffer.length - 1));
216 /* Return the completed template descriptor */
218 *actual_return_desc = return_desc;
219 return_ACPI_STATUS (AE_OK);
223 /*******************************************************************************
225 * FUNCTION: acpi_ex_do_concatenate
227 * PARAMETERS: Operand0 - First source object
228 * Operand1 - Second source object
229 * actual_return_desc - Where to place the return object
230 * walk_state - Current walk state
234 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
236 ******************************************************************************/
239 acpi_ex_do_concatenate (
240 union acpi_operand_object *operand0,
241 union acpi_operand_object *operand1,
242 union acpi_operand_object **actual_return_desc,
243 struct acpi_walk_state *walk_state)
245 union acpi_operand_object *local_operand1 = operand1;
246 union acpi_operand_object *return_desc;
249 acpi_size new_length;
252 ACPI_FUNCTION_TRACE ("ex_do_concatenate");
256 * Convert the second operand if necessary. The first operand
257 * determines the type of the second operand, (See the Data Types
258 * section of the ACPI specification.) Both object types are
259 * guaranteed to be either Integer/String/Buffer by the operand
260 * resolution mechanism.
262 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
263 case ACPI_TYPE_INTEGER:
264 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
267 case ACPI_TYPE_STRING:
268 status = acpi_ex_convert_to_string (operand1, &local_operand1,
269 ACPI_IMPLICIT_CONVERT_HEX);
272 case ACPI_TYPE_BUFFER:
273 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
277 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
278 ACPI_GET_OBJECT_TYPE (operand0)));
279 status = AE_AML_INTERNAL;
282 if (ACPI_FAILURE (status)) {
287 * Both operands are now known to be the same object type
288 * (Both are Integer, String, or Buffer), and we can now perform the
293 * There are three cases to handle:
295 * 1) Two Integers concatenated to produce a new Buffer
296 * 2) Two Strings concatenated to produce a new String
297 * 3) Two Buffers concatenated to produce a new Buffer
299 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
300 case ACPI_TYPE_INTEGER:
302 /* Result of two Integers is a Buffer */
303 /* Need enough buffer space for two integers */
305 return_desc = acpi_ut_create_buffer_object (
306 ACPI_MUL_2 (acpi_gbl_integer_byte_width));
308 status = AE_NO_MEMORY;
312 new_buf = (char *) return_desc->buffer.pointer;
314 /* Copy the first integer, LSB first */
316 ACPI_MEMCPY (new_buf,
317 &operand0->integer.value,
318 acpi_gbl_integer_byte_width);
320 /* Copy the second integer (LSB first) after the first */
322 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
323 &local_operand1->integer.value,
324 acpi_gbl_integer_byte_width);
327 case ACPI_TYPE_STRING:
329 /* Result of two Strings is a String */
331 new_length = (acpi_size) operand0->string.length +
332 (acpi_size) local_operand1->string.length;
333 if (new_length > ACPI_MAX_STRING_CONVERSION) {
334 status = AE_AML_STRING_LIMIT;
338 return_desc = acpi_ut_create_string_object (new_length);
340 status = AE_NO_MEMORY;
344 new_buf = return_desc->string.pointer;
346 /* Concatenate the strings */
348 ACPI_STRCPY (new_buf,
349 operand0->string.pointer);
350 ACPI_STRCPY (new_buf + operand0->string.length,
351 local_operand1->string.pointer);
354 case ACPI_TYPE_BUFFER:
356 /* Result of two Buffers is a Buffer */
358 return_desc = acpi_ut_create_buffer_object (
359 (acpi_size) operand0->buffer.length +
360 (acpi_size) local_operand1->buffer.length);
362 status = AE_NO_MEMORY;
366 new_buf = (char *) return_desc->buffer.pointer;
368 /* Concatenate the buffers */
370 ACPI_MEMCPY (new_buf,
371 operand0->buffer.pointer,
372 operand0->buffer.length);
373 ACPI_MEMCPY (new_buf + operand0->buffer.length,
374 local_operand1->buffer.pointer,
375 local_operand1->buffer.length);
380 /* Invalid object type, should not happen here */
382 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
383 ACPI_GET_OBJECT_TYPE (operand0)));
384 status =AE_AML_INTERNAL;
388 *actual_return_desc = return_desc;
391 if (local_operand1 != operand1) {
392 acpi_ut_remove_reference (local_operand1);
394 return_ACPI_STATUS (status);
398 /*******************************************************************************
400 * FUNCTION: acpi_ex_do_math_op
402 * PARAMETERS: Opcode - AML opcode
403 * Integer0 - Integer operand #0
404 * Integer1 - Integer operand #1
406 * RETURN: Integer result of the operation
408 * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
409 * math functions here is to prevent a lot of pointer dereferencing
410 * to obtain the operands.
412 ******************************************************************************/
417 acpi_integer integer0,
418 acpi_integer integer1)
421 ACPI_FUNCTION_ENTRY ();
425 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
427 return (integer0 + integer1);
430 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
432 return (integer0 & integer1);
435 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
437 return (~(integer0 & integer1));
440 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
442 return (integer0 | integer1);
445 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
447 return (~(integer0 | integer1));
450 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
452 return (integer0 ^ integer1);
455 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
457 return (integer0 * integer1);
460 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result)*/
462 return (integer0 << integer1);
465 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
467 return (integer0 >> integer1);
470 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
472 return (integer0 - integer1);
481 /*******************************************************************************
483 * FUNCTION: acpi_ex_do_logical_numeric_op
485 * PARAMETERS: Opcode - AML opcode
486 * Integer0 - Integer operand #0
487 * Integer1 - Integer operand #1
488 * logical_result - TRUE/FALSE result of the operation
492 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
493 * operators (LAnd and LOr), both operands must be integers.
495 * Note: cleanest machine code seems to be produced by the code
496 * below, rather than using statements of the form:
497 * Result = (Integer0 && Integer1);
499 ******************************************************************************/
502 acpi_ex_do_logical_numeric_op (
504 acpi_integer integer0,
505 acpi_integer integer1,
508 acpi_status status = AE_OK;
509 u8 local_result = FALSE;
512 ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
516 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
518 if (integer0 && integer1) {
523 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
525 if (integer0 || integer1) {
531 status = AE_AML_INTERNAL;
535 /* Return the logical result and status */
537 *logical_result = local_result;
538 return_ACPI_STATUS (status);
542 /*******************************************************************************
544 * FUNCTION: acpi_ex_do_logical_op
546 * PARAMETERS: Opcode - AML opcode
547 * Operand0 - operand #0
548 * Operand1 - operand #1
549 * logical_result - TRUE/FALSE result of the operation
553 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
554 * functions here is to prevent a lot of pointer dereferencing
555 * to obtain the operands and to simplify the generation of the
556 * logical value. For the Numeric operators (LAnd and LOr), both
557 * operands must be integers. For the other logical operators,
558 * operands can be any combination of Integer/String/Buffer. The
559 * first operand determines the type to which the second operand
562 * Note: cleanest machine code seems to be produced by the code
563 * below, rather than using statements of the form:
564 * Result = (Operand0 == Operand1);
566 ******************************************************************************/
569 acpi_ex_do_logical_op (
571 union acpi_operand_object *operand0,
572 union acpi_operand_object *operand1,
575 union acpi_operand_object *local_operand1 = operand1;
576 acpi_integer integer0;
577 acpi_integer integer1;
580 acpi_status status = AE_OK;
581 u8 local_result = FALSE;
585 ACPI_FUNCTION_TRACE ("ex_do_logical_op");
589 * Convert the second operand if necessary. The first operand
590 * determines the type of the second operand, (See the Data Types
591 * section of the ACPI 3.0+ specification.) Both object types are
592 * guaranteed to be either Integer/String/Buffer by the operand
593 * resolution mechanism.
595 switch (ACPI_GET_OBJECT_TYPE (operand0)) {
596 case ACPI_TYPE_INTEGER:
597 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
600 case ACPI_TYPE_STRING:
601 status = acpi_ex_convert_to_string (operand1, &local_operand1,
602 ACPI_IMPLICIT_CONVERT_HEX);
605 case ACPI_TYPE_BUFFER:
606 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
610 status = AE_AML_INTERNAL;
614 if (ACPI_FAILURE (status)) {
619 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
621 if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
623 * 1) Both operands are of type integer
624 * Note: local_operand1 may have changed above
626 integer0 = operand0->integer.value;
627 integer1 = local_operand1->integer.value;
630 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
632 if (integer0 == integer1) {
637 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
639 if (integer0 > integer1) {
644 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
646 if (integer0 < integer1) {
652 status = AE_AML_INTERNAL;
658 * 2) Both operands are Strings or both are Buffers
659 * Note: Code below takes advantage of common Buffer/String
660 * object fields. local_operand1 may have changed above. Use
661 * memcmp to handle nulls in buffers.
663 length0 = operand0->buffer.length;
664 length1 = local_operand1->buffer.length;
666 /* Lexicographic compare: compare the data bytes */
668 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
669 (const char * ) local_operand1->buffer.pointer,
670 (length0 > length1) ? length1 : length0);
673 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
675 /* Length and all bytes must be equal */
677 if ((length0 == length1) &&
679 /* Length and all bytes match ==> TRUE */
685 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
689 goto cleanup; /* TRUE */
692 goto cleanup; /* FALSE */
695 /* Bytes match (to shortest length), compare lengths */
697 if (length0 > length1) {
702 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
705 goto cleanup; /* FALSE */
709 goto cleanup; /* TRUE */
712 /* Bytes match (to shortest length), compare lengths */
714 if (length0 < length1) {
720 status = AE_AML_INTERNAL;
727 /* New object was created if implicit conversion performed - delete */
729 if (local_operand1 != operand1) {
730 acpi_ut_remove_reference (local_operand1);
733 /* Return the logical result and status */
735 *logical_result = local_result;
736 return_ACPI_STATUS (status);