Loading...
1
2/******************************************************************************
3 *
4 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
30 *
31 * NO WARRANTY
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.
43 */
44
45#include <acpi/acpi.h>
46#include "accommon.h"
47#include "acinterp.h"
48#include "amlcode.h"
49#include "amlresrc.h"
50
51#define _COMPONENT ACPI_EXECUTER
52ACPI_MODULE_NAME("exmisc")
53
54/*******************************************************************************
55 *
56 * FUNCTION: acpi_ex_get_object_reference
57 *
58 * PARAMETERS: obj_desc - Create a reference to this object
59 * return_desc - Where to store the reference
60 * walk_state - Current state
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Obtain and return a "reference" to the target object
65 * Common code for the ref_of_op and the cond_ref_of_op.
66 *
67 ******************************************************************************/
68acpi_status
69acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
70 union acpi_operand_object **return_desc,
71 struct acpi_walk_state *walk_state)
72{
73 union acpi_operand_object *reference_obj;
74 union acpi_operand_object *referenced_obj;
75
76 ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc);
77
78 *return_desc = NULL;
79
80 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
81 case ACPI_DESC_TYPE_OPERAND:
82
83 if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
84 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
85 }
86
87 /*
88 * Must be a reference to a Local or Arg
89 */
90 switch (obj_desc->reference.class) {
91 case ACPI_REFCLASS_LOCAL:
92 case ACPI_REFCLASS_ARG:
93 case ACPI_REFCLASS_DEBUG:
94
95 /* The referenced object is the pseudo-node for the local/arg */
96
97 referenced_obj = obj_desc->reference.object;
98 break;
99
100 default:
101
102 ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X",
103 obj_desc->reference.class));
104 return_ACPI_STATUS(AE_AML_INTERNAL);
105 }
106 break;
107
108 case ACPI_DESC_TYPE_NAMED:
109
110 /*
111 * A named reference that has already been resolved to a Node
112 */
113 referenced_obj = obj_desc;
114 break;
115
116 default:
117
118 ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X",
119 ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
120 return_ACPI_STATUS(AE_TYPE);
121 }
122
123 /* Create a new reference object */
124
125 reference_obj =
126 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
127 if (!reference_obj) {
128 return_ACPI_STATUS(AE_NO_MEMORY);
129 }
130
131 reference_obj->reference.class = ACPI_REFCLASS_REFOF;
132 reference_obj->reference.object = referenced_obj;
133 *return_desc = reference_obj;
134
135 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
136 "Object %p Type [%s], returning Reference %p\n",
137 obj_desc, acpi_ut_get_object_type_name(obj_desc),
138 *return_desc));
139
140 return_ACPI_STATUS(AE_OK);
141}
142
143/*******************************************************************************
144 *
145 * FUNCTION: acpi_ex_concat_template
146 *
147 * PARAMETERS: Operand0 - First source object
148 * Operand1 - Second source object
149 * actual_return_desc - Where to place the return object
150 * walk_state - Current walk state
151 *
152 * RETURN: Status
153 *
154 * DESCRIPTION: Concatenate two resource templates
155 *
156 ******************************************************************************/
157
158acpi_status
159acpi_ex_concat_template(union acpi_operand_object *operand0,
160 union acpi_operand_object *operand1,
161 union acpi_operand_object **actual_return_desc,
162 struct acpi_walk_state *walk_state)
163{
164 acpi_status status;
165 union acpi_operand_object *return_desc;
166 u8 *new_buf;
167 u8 *end_tag;
168 acpi_size length0;
169 acpi_size length1;
170 acpi_size new_length;
171
172 ACPI_FUNCTION_TRACE(ex_concat_template);
173
174 /*
175 * Find the end_tag descriptor in each resource template.
176 * Note1: returned pointers point TO the end_tag, not past it.
177 * Note2: zero-length buffers are allowed; treated like one end_tag
178 */
179
180 /* Get the length of the first resource template */
181
182 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
183 if (ACPI_FAILURE(status)) {
184 return_ACPI_STATUS(status);
185 }
186
187 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
188
189 /* Get the length of the second resource template */
190
191 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
192 if (ACPI_FAILURE(status)) {
193 return_ACPI_STATUS(status);
194 }
195
196 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer);
197
198 /* Combine both lengths, minimum size will be 2 for end_tag */
199
200 new_length = length0 + length1 + sizeof(struct aml_resource_end_tag);
201
202 /* Create a new buffer object for the result (with one end_tag) */
203
204 return_desc = acpi_ut_create_buffer_object(new_length);
205 if (!return_desc) {
206 return_ACPI_STATUS(AE_NO_MEMORY);
207 }
208
209 /*
210 * Copy the templates to the new buffer, 0 first, then 1 follows. One
211 * end_tag descriptor is copied from Operand1.
212 */
213 new_buf = return_desc->buffer.pointer;
214 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
215 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
216
217 /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
218
219 new_buf[new_length - 1] = 0;
220 new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
221
222 /* Return the completed resource template */
223
224 *actual_return_desc = return_desc;
225 return_ACPI_STATUS(AE_OK);
226}
227
228/*******************************************************************************
229 *
230 * FUNCTION: acpi_ex_do_concatenate
231 *
232 * PARAMETERS: Operand0 - First source object
233 * Operand1 - Second source object
234 * actual_return_desc - Where to place the return object
235 * walk_state - Current walk state
236 *
237 * RETURN: Status
238 *
239 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
240 *
241 ******************************************************************************/
242
243acpi_status
244acpi_ex_do_concatenate(union acpi_operand_object *operand0,
245 union acpi_operand_object *operand1,
246 union acpi_operand_object **actual_return_desc,
247 struct acpi_walk_state *walk_state)
248{
249 union acpi_operand_object *local_operand1 = operand1;
250 union acpi_operand_object *return_desc;
251 char *new_buf;
252 acpi_status status;
253
254 ACPI_FUNCTION_TRACE(ex_do_concatenate);
255
256 /*
257 * Convert the second operand if necessary. The first operand
258 * determines the type of the second operand, (See the Data Types
259 * section of the ACPI specification.) Both object types are
260 * guaranteed to be either Integer/String/Buffer by the operand
261 * resolution mechanism.
262 */
263 switch (operand0->common.type) {
264 case ACPI_TYPE_INTEGER:
265 status =
266 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
267 break;
268
269 case ACPI_TYPE_STRING:
270 status = acpi_ex_convert_to_string(operand1, &local_operand1,
271 ACPI_IMPLICIT_CONVERT_HEX);
272 break;
273
274 case ACPI_TYPE_BUFFER:
275 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
276 break;
277
278 default:
279 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
280 operand0->common.type));
281 status = AE_AML_INTERNAL;
282 }
283
284 if (ACPI_FAILURE(status)) {
285 goto cleanup;
286 }
287
288 /*
289 * Both operands are now known to be the same object type
290 * (Both are Integer, String, or Buffer), and we can now perform the
291 * concatenation.
292 */
293
294 /*
295 * There are three cases to handle:
296 *
297 * 1) Two Integers concatenated to produce a new Buffer
298 * 2) Two Strings concatenated to produce a new String
299 * 3) Two Buffers concatenated to produce a new Buffer
300 */
301 switch (operand0->common.type) {
302 case ACPI_TYPE_INTEGER:
303
304 /* Result of two Integers is a Buffer */
305 /* Need enough buffer space for two integers */
306
307 return_desc = acpi_ut_create_buffer_object((acpi_size)
308 ACPI_MUL_2
309 (acpi_gbl_integer_byte_width));
310 if (!return_desc) {
311 status = AE_NO_MEMORY;
312 goto cleanup;
313 }
314
315 new_buf = (char *)return_desc->buffer.pointer;
316
317 /* Copy the first integer, LSB first */
318
319 ACPI_MEMCPY(new_buf, &operand0->integer.value,
320 acpi_gbl_integer_byte_width);
321
322 /* Copy the second integer (LSB first) after the first */
323
324 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
325 &local_operand1->integer.value,
326 acpi_gbl_integer_byte_width);
327 break;
328
329 case ACPI_TYPE_STRING:
330
331 /* Result of two Strings is a String */
332
333 return_desc = acpi_ut_create_string_object(((acpi_size)
334 operand0->string.
335 length +
336 local_operand1->
337 string.length));
338 if (!return_desc) {
339 status = AE_NO_MEMORY;
340 goto cleanup;
341 }
342
343 new_buf = return_desc->string.pointer;
344
345 /* Concatenate the strings */
346
347 ACPI_STRCPY(new_buf, operand0->string.pointer);
348 ACPI_STRCPY(new_buf + operand0->string.length,
349 local_operand1->string.pointer);
350 break;
351
352 case ACPI_TYPE_BUFFER:
353
354 /* Result of two Buffers is a Buffer */
355
356 return_desc = acpi_ut_create_buffer_object(((acpi_size)
357 operand0->buffer.
358 length +
359 local_operand1->
360 buffer.length));
361 if (!return_desc) {
362 status = AE_NO_MEMORY;
363 goto cleanup;
364 }
365
366 new_buf = (char *)return_desc->buffer.pointer;
367
368 /* Concatenate the buffers */
369
370 ACPI_MEMCPY(new_buf, 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);
375 break;
376
377 default:
378
379 /* Invalid object type, should not happen here */
380
381 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
382 operand0->common.type));
383 status = AE_AML_INTERNAL;
384 goto cleanup;
385 }
386
387 *actual_return_desc = return_desc;
388
389 cleanup:
390 if (local_operand1 != operand1) {
391 acpi_ut_remove_reference(local_operand1);
392 }
393 return_ACPI_STATUS(status);
394}
395
396/*******************************************************************************
397 *
398 * FUNCTION: acpi_ex_do_math_op
399 *
400 * PARAMETERS: Opcode - AML opcode
401 * Integer0 - Integer operand #0
402 * Integer1 - Integer operand #1
403 *
404 * RETURN: Integer result of the operation
405 *
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.
409 *
410 ******************************************************************************/
411
412u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1)
413{
414
415 ACPI_FUNCTION_ENTRY();
416
417 switch (opcode) {
418 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
419
420 return (integer0 + integer1);
421
422 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
423
424 return (integer0 & integer1);
425
426 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
427
428 return (~(integer0 & integer1));
429
430 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
431
432 return (integer0 | integer1);
433
434 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
435
436 return (~(integer0 | integer1));
437
438 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
439
440 return (integer0 ^ integer1);
441
442 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
443
444 return (integer0 * integer1);
445
446 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
447
448 /*
449 * We need to check if the shiftcount is larger than the integer bit
450 * width since the behavior of this is not well-defined in the C language.
451 */
452 if (integer1 >= acpi_gbl_integer_bit_width) {
453 return (0);
454 }
455 return (integer0 << integer1);
456
457 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
458
459 /*
460 * We need to check if the shiftcount is larger than the integer bit
461 * width since the behavior of this is not well-defined in the C language.
462 */
463 if (integer1 >= acpi_gbl_integer_bit_width) {
464 return (0);
465 }
466 return (integer0 >> integer1);
467
468 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
469
470 return (integer0 - integer1);
471
472 default:
473
474 return (0);
475 }
476}
477
478/*******************************************************************************
479 *
480 * FUNCTION: acpi_ex_do_logical_numeric_op
481 *
482 * PARAMETERS: Opcode - AML opcode
483 * Integer0 - Integer operand #0
484 * Integer1 - Integer operand #1
485 * logical_result - TRUE/FALSE result of the operation
486 *
487 * RETURN: Status
488 *
489 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
490 * operators (LAnd and LOr), both operands must be integers.
491 *
492 * Note: cleanest machine code seems to be produced by the code
493 * below, rather than using statements of the form:
494 * Result = (Integer0 && Integer1);
495 *
496 ******************************************************************************/
497
498acpi_status
499acpi_ex_do_logical_numeric_op(u16 opcode,
500 u64 integer0, u64 integer1, u8 *logical_result)
501{
502 acpi_status status = AE_OK;
503 u8 local_result = FALSE;
504
505 ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op);
506
507 switch (opcode) {
508 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
509
510 if (integer0 && integer1) {
511 local_result = TRUE;
512 }
513 break;
514
515 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
516
517 if (integer0 || integer1) {
518 local_result = TRUE;
519 }
520 break;
521
522 default:
523 status = AE_AML_INTERNAL;
524 break;
525 }
526
527 /* Return the logical result and status */
528
529 *logical_result = local_result;
530 return_ACPI_STATUS(status);
531}
532
533/*******************************************************************************
534 *
535 * FUNCTION: acpi_ex_do_logical_op
536 *
537 * PARAMETERS: Opcode - AML opcode
538 * Operand0 - operand #0
539 * Operand1 - operand #1
540 * logical_result - TRUE/FALSE result of the operation
541 *
542 * RETURN: Status
543 *
544 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
545 * functions here is to prevent a lot of pointer dereferencing
546 * to obtain the operands and to simplify the generation of the
547 * logical value. For the Numeric operators (LAnd and LOr), both
548 * operands must be integers. For the other logical operators,
549 * operands can be any combination of Integer/String/Buffer. The
550 * first operand determines the type to which the second operand
551 * will be converted.
552 *
553 * Note: cleanest machine code seems to be produced by the code
554 * below, rather than using statements of the form:
555 * Result = (Operand0 == Operand1);
556 *
557 ******************************************************************************/
558
559acpi_status
560acpi_ex_do_logical_op(u16 opcode,
561 union acpi_operand_object *operand0,
562 union acpi_operand_object *operand1, u8 * logical_result)
563{
564 union acpi_operand_object *local_operand1 = operand1;
565 u64 integer0;
566 u64 integer1;
567 u32 length0;
568 u32 length1;
569 acpi_status status = AE_OK;
570 u8 local_result = FALSE;
571 int compare;
572
573 ACPI_FUNCTION_TRACE(ex_do_logical_op);
574
575 /*
576 * Convert the second operand if necessary. The first operand
577 * determines the type of the second operand, (See the Data Types
578 * section of the ACPI 3.0+ specification.) Both object types are
579 * guaranteed to be either Integer/String/Buffer by the operand
580 * resolution mechanism.
581 */
582 switch (operand0->common.type) {
583 case ACPI_TYPE_INTEGER:
584 status =
585 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
586 break;
587
588 case ACPI_TYPE_STRING:
589 status = acpi_ex_convert_to_string(operand1, &local_operand1,
590 ACPI_IMPLICIT_CONVERT_HEX);
591 break;
592
593 case ACPI_TYPE_BUFFER:
594 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
595 break;
596
597 default:
598 status = AE_AML_INTERNAL;
599 break;
600 }
601
602 if (ACPI_FAILURE(status)) {
603 goto cleanup;
604 }
605
606 /*
607 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
608 */
609 if (operand0->common.type == ACPI_TYPE_INTEGER) {
610 /*
611 * 1) Both operands are of type integer
612 * Note: local_operand1 may have changed above
613 */
614 integer0 = operand0->integer.value;
615 integer1 = local_operand1->integer.value;
616
617 switch (opcode) {
618 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
619
620 if (integer0 == integer1) {
621 local_result = TRUE;
622 }
623 break;
624
625 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
626
627 if (integer0 > integer1) {
628 local_result = TRUE;
629 }
630 break;
631
632 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
633
634 if (integer0 < integer1) {
635 local_result = TRUE;
636 }
637 break;
638
639 default:
640 status = AE_AML_INTERNAL;
641 break;
642 }
643 } else {
644 /*
645 * 2) Both operands are Strings or both are Buffers
646 * Note: Code below takes advantage of common Buffer/String
647 * object fields. local_operand1 may have changed above. Use
648 * memcmp to handle nulls in buffers.
649 */
650 length0 = operand0->buffer.length;
651 length1 = local_operand1->buffer.length;
652
653 /* Lexicographic compare: compare the data bytes */
654
655 compare = ACPI_MEMCMP(operand0->buffer.pointer,
656 local_operand1->buffer.pointer,
657 (length0 > length1) ? length1 : length0);
658
659 switch (opcode) {
660 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
661
662 /* Length and all bytes must be equal */
663
664 if ((length0 == length1) && (compare == 0)) {
665
666 /* Length and all bytes match ==> TRUE */
667
668 local_result = TRUE;
669 }
670 break;
671
672 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
673
674 if (compare > 0) {
675 local_result = TRUE;
676 goto cleanup; /* TRUE */
677 }
678 if (compare < 0) {
679 goto cleanup; /* FALSE */
680 }
681
682 /* Bytes match (to shortest length), compare lengths */
683
684 if (length0 > length1) {
685 local_result = TRUE;
686 }
687 break;
688
689 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
690
691 if (compare > 0) {
692 goto cleanup; /* FALSE */
693 }
694 if (compare < 0) {
695 local_result = TRUE;
696 goto cleanup; /* TRUE */
697 }
698
699 /* Bytes match (to shortest length), compare lengths */
700
701 if (length0 < length1) {
702 local_result = TRUE;
703 }
704 break;
705
706 default:
707 status = AE_AML_INTERNAL;
708 break;
709 }
710 }
711
712 cleanup:
713
714 /* New object was created if implicit conversion performed - delete */
715
716 if (local_operand1 != operand1) {
717 acpi_ut_remove_reference(local_operand1);
718 }
719
720 /* Return the logical result and status */
721
722 *logical_result = local_result;
723 return_ACPI_STATUS(status);
724}
1/******************************************************************************
2 *
3 * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2014, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44#include <acpi/acpi.h>
45#include "accommon.h"
46#include "acinterp.h"
47#include "amlcode.h"
48#include "amlresrc.h"
49
50#define _COMPONENT ACPI_EXECUTER
51ACPI_MODULE_NAME("exmisc")
52
53/*******************************************************************************
54 *
55 * FUNCTION: acpi_ex_get_object_reference
56 *
57 * PARAMETERS: obj_desc - Create a reference to this object
58 * return_desc - Where to store the reference
59 * walk_state - Current state
60 *
61 * RETURN: Status
62 *
63 * DESCRIPTION: Obtain and return a "reference" to the target object
64 * Common code for the ref_of_op and the cond_ref_of_op.
65 *
66 ******************************************************************************/
67acpi_status
68acpi_ex_get_object_reference(union acpi_operand_object *obj_desc,
69 union acpi_operand_object **return_desc,
70 struct acpi_walk_state *walk_state)
71{
72 union acpi_operand_object *reference_obj;
73 union acpi_operand_object *referenced_obj;
74
75 ACPI_FUNCTION_TRACE_PTR(ex_get_object_reference, obj_desc);
76
77 *return_desc = NULL;
78
79 switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
80 case ACPI_DESC_TYPE_OPERAND:
81
82 if (obj_desc->common.type != ACPI_TYPE_LOCAL_REFERENCE) {
83 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
84 }
85
86 /*
87 * Must be a reference to a Local or Arg
88 */
89 switch (obj_desc->reference.class) {
90 case ACPI_REFCLASS_LOCAL:
91 case ACPI_REFCLASS_ARG:
92 case ACPI_REFCLASS_DEBUG:
93
94 /* The referenced object is the pseudo-node for the local/arg */
95
96 referenced_obj = obj_desc->reference.object;
97 break;
98
99 default:
100
101 ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X",
102 obj_desc->reference.class));
103 return_ACPI_STATUS(AE_AML_INTERNAL);
104 }
105 break;
106
107 case ACPI_DESC_TYPE_NAMED:
108 /*
109 * A named reference that has already been resolved to a Node
110 */
111 referenced_obj = obj_desc;
112 break;
113
114 default:
115
116 ACPI_ERROR((AE_INFO, "Invalid descriptor type 0x%X",
117 ACPI_GET_DESCRIPTOR_TYPE(obj_desc)));
118 return_ACPI_STATUS(AE_TYPE);
119 }
120
121 /* Create a new reference object */
122
123 reference_obj =
124 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
125 if (!reference_obj) {
126 return_ACPI_STATUS(AE_NO_MEMORY);
127 }
128
129 reference_obj->reference.class = ACPI_REFCLASS_REFOF;
130 reference_obj->reference.object = referenced_obj;
131 *return_desc = reference_obj;
132
133 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
134 "Object %p Type [%s], returning Reference %p\n",
135 obj_desc, acpi_ut_get_object_type_name(obj_desc),
136 *return_desc));
137
138 return_ACPI_STATUS(AE_OK);
139}
140
141/*******************************************************************************
142 *
143 * FUNCTION: acpi_ex_concat_template
144 *
145 * PARAMETERS: operand0 - First source object
146 * operand1 - Second source object
147 * actual_return_desc - Where to place the return object
148 * walk_state - Current walk state
149 *
150 * RETURN: Status
151 *
152 * DESCRIPTION: Concatenate two resource templates
153 *
154 ******************************************************************************/
155
156acpi_status
157acpi_ex_concat_template(union acpi_operand_object *operand0,
158 union acpi_operand_object *operand1,
159 union acpi_operand_object **actual_return_desc,
160 struct acpi_walk_state *walk_state)
161{
162 acpi_status status;
163 union acpi_operand_object *return_desc;
164 u8 *new_buf;
165 u8 *end_tag;
166 acpi_size length0;
167 acpi_size length1;
168 acpi_size new_length;
169
170 ACPI_FUNCTION_TRACE(ex_concat_template);
171
172 /*
173 * Find the end_tag descriptor in each resource template.
174 * Note1: returned pointers point TO the end_tag, not past it.
175 * Note2: zero-length buffers are allowed; treated like one end_tag
176 */
177
178 /* Get the length of the first resource template */
179
180 status = acpi_ut_get_resource_end_tag(operand0, &end_tag);
181 if (ACPI_FAILURE(status)) {
182 return_ACPI_STATUS(status);
183 }
184
185 length0 = ACPI_PTR_DIFF(end_tag, operand0->buffer.pointer);
186
187 /* Get the length of the second resource template */
188
189 status = acpi_ut_get_resource_end_tag(operand1, &end_tag);
190 if (ACPI_FAILURE(status)) {
191 return_ACPI_STATUS(status);
192 }
193
194 length1 = ACPI_PTR_DIFF(end_tag, operand1->buffer.pointer);
195
196 /* Combine both lengths, minimum size will be 2 for end_tag */
197
198 new_length = length0 + length1 + sizeof(struct aml_resource_end_tag);
199
200 /* Create a new buffer object for the result (with one end_tag) */
201
202 return_desc = acpi_ut_create_buffer_object(new_length);
203 if (!return_desc) {
204 return_ACPI_STATUS(AE_NO_MEMORY);
205 }
206
207 /*
208 * Copy the templates to the new buffer, 0 first, then 1 follows. One
209 * end_tag descriptor is copied from Operand1.
210 */
211 new_buf = return_desc->buffer.pointer;
212 ACPI_MEMCPY(new_buf, operand0->buffer.pointer, length0);
213 ACPI_MEMCPY(new_buf + length0, operand1->buffer.pointer, length1);
214
215 /* Insert end_tag and set the checksum to zero, means "ignore checksum" */
216
217 new_buf[new_length - 1] = 0;
218 new_buf[new_length - 2] = ACPI_RESOURCE_NAME_END_TAG | 1;
219
220 /* Return the completed resource template */
221
222 *actual_return_desc = return_desc;
223 return_ACPI_STATUS(AE_OK);
224}
225
226/*******************************************************************************
227 *
228 * FUNCTION: acpi_ex_do_concatenate
229 *
230 * PARAMETERS: operand0 - First source object
231 * operand1 - Second source object
232 * actual_return_desc - Where to place the return object
233 * walk_state - Current walk state
234 *
235 * RETURN: Status
236 *
237 * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
238 *
239 ******************************************************************************/
240
241acpi_status
242acpi_ex_do_concatenate(union acpi_operand_object *operand0,
243 union acpi_operand_object *operand1,
244 union acpi_operand_object **actual_return_desc,
245 struct acpi_walk_state *walk_state)
246{
247 union acpi_operand_object *local_operand1 = operand1;
248 union acpi_operand_object *return_desc;
249 char *new_buf;
250 acpi_status status;
251
252 ACPI_FUNCTION_TRACE(ex_do_concatenate);
253
254 /*
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.
260 */
261 switch (operand0->common.type) {
262 case ACPI_TYPE_INTEGER:
263
264 status =
265 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
266 break;
267
268 case ACPI_TYPE_STRING:
269
270 status = acpi_ex_convert_to_string(operand1, &local_operand1,
271 ACPI_IMPLICIT_CONVERT_HEX);
272 break;
273
274 case ACPI_TYPE_BUFFER:
275
276 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
277 break;
278
279 default:
280
281 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
282 operand0->common.type));
283 status = AE_AML_INTERNAL;
284 }
285
286 if (ACPI_FAILURE(status)) {
287 goto cleanup;
288 }
289
290 /*
291 * Both operands are now known to be the same object type
292 * (Both are Integer, String, or Buffer), and we can now perform the
293 * concatenation.
294 */
295
296 /*
297 * There are three cases to handle:
298 *
299 * 1) Two Integers concatenated to produce a new Buffer
300 * 2) Two Strings concatenated to produce a new String
301 * 3) Two Buffers concatenated to produce a new Buffer
302 */
303 switch (operand0->common.type) {
304 case ACPI_TYPE_INTEGER:
305
306 /* Result of two Integers is a Buffer */
307 /* Need enough buffer space for two integers */
308
309 return_desc = acpi_ut_create_buffer_object((acpi_size)
310 ACPI_MUL_2
311 (acpi_gbl_integer_byte_width));
312 if (!return_desc) {
313 status = AE_NO_MEMORY;
314 goto cleanup;
315 }
316
317 new_buf = (char *)return_desc->buffer.pointer;
318
319 /* Copy the first integer, LSB first */
320
321 ACPI_MEMCPY(new_buf, &operand0->integer.value,
322 acpi_gbl_integer_byte_width);
323
324 /* Copy the second integer (LSB first) after the first */
325
326 ACPI_MEMCPY(new_buf + acpi_gbl_integer_byte_width,
327 &local_operand1->integer.value,
328 acpi_gbl_integer_byte_width);
329 break;
330
331 case ACPI_TYPE_STRING:
332
333 /* Result of two Strings is a String */
334
335 return_desc = acpi_ut_create_string_object(((acpi_size)
336 operand0->string.
337 length +
338 local_operand1->
339 string.length));
340 if (!return_desc) {
341 status = AE_NO_MEMORY;
342 goto cleanup;
343 }
344
345 new_buf = return_desc->string.pointer;
346
347 /* Concatenate the strings */
348
349 ACPI_STRCPY(new_buf, operand0->string.pointer);
350 ACPI_STRCPY(new_buf + operand0->string.length,
351 local_operand1->string.pointer);
352 break;
353
354 case ACPI_TYPE_BUFFER:
355
356 /* Result of two Buffers is a Buffer */
357
358 return_desc = acpi_ut_create_buffer_object(((acpi_size)
359 operand0->buffer.
360 length +
361 local_operand1->
362 buffer.length));
363 if (!return_desc) {
364 status = AE_NO_MEMORY;
365 goto cleanup;
366 }
367
368 new_buf = (char *)return_desc->buffer.pointer;
369
370 /* Concatenate the buffers */
371
372 ACPI_MEMCPY(new_buf, operand0->buffer.pointer,
373 operand0->buffer.length);
374 ACPI_MEMCPY(new_buf + operand0->buffer.length,
375 local_operand1->buffer.pointer,
376 local_operand1->buffer.length);
377 break;
378
379 default:
380
381 /* Invalid object type, should not happen here */
382
383 ACPI_ERROR((AE_INFO, "Invalid object type: 0x%X",
384 operand0->common.type));
385 status = AE_AML_INTERNAL;
386 goto cleanup;
387 }
388
389 *actual_return_desc = return_desc;
390
391cleanup:
392 if (local_operand1 != operand1) {
393 acpi_ut_remove_reference(local_operand1);
394 }
395 return_ACPI_STATUS(status);
396}
397
398/*******************************************************************************
399 *
400 * FUNCTION: acpi_ex_do_math_op
401 *
402 * PARAMETERS: opcode - AML opcode
403 * integer0 - Integer operand #0
404 * integer1 - Integer operand #1
405 *
406 * RETURN: Integer result of the operation
407 *
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.
411 *
412 ******************************************************************************/
413
414u64 acpi_ex_do_math_op(u16 opcode, u64 integer0, u64 integer1)
415{
416
417 ACPI_FUNCTION_ENTRY();
418
419 switch (opcode) {
420 case AML_ADD_OP: /* Add (Integer0, Integer1, Result) */
421
422 return (integer0 + integer1);
423
424 case AML_BIT_AND_OP: /* And (Integer0, Integer1, Result) */
425
426 return (integer0 & integer1);
427
428 case AML_BIT_NAND_OP: /* NAnd (Integer0, Integer1, Result) */
429
430 return (~(integer0 & integer1));
431
432 case AML_BIT_OR_OP: /* Or (Integer0, Integer1, Result) */
433
434 return (integer0 | integer1);
435
436 case AML_BIT_NOR_OP: /* NOr (Integer0, Integer1, Result) */
437
438 return (~(integer0 | integer1));
439
440 case AML_BIT_XOR_OP: /* XOr (Integer0, Integer1, Result) */
441
442 return (integer0 ^ integer1);
443
444 case AML_MULTIPLY_OP: /* Multiply (Integer0, Integer1, Result) */
445
446 return (integer0 * integer1);
447
448 case AML_SHIFT_LEFT_OP: /* shift_left (Operand, shift_count, Result) */
449
450 /*
451 * We need to check if the shiftcount is larger than the integer bit
452 * width since the behavior of this is not well-defined in the C language.
453 */
454 if (integer1 >= acpi_gbl_integer_bit_width) {
455 return (0);
456 }
457 return (integer0 << integer1);
458
459 case AML_SHIFT_RIGHT_OP: /* shift_right (Operand, shift_count, Result) */
460
461 /*
462 * We need to check if the shiftcount is larger than the integer bit
463 * width since the behavior of this is not well-defined in the C language.
464 */
465 if (integer1 >= acpi_gbl_integer_bit_width) {
466 return (0);
467 }
468 return (integer0 >> integer1);
469
470 case AML_SUBTRACT_OP: /* Subtract (Integer0, Integer1, Result) */
471
472 return (integer0 - integer1);
473
474 default:
475
476 return (0);
477 }
478}
479
480/*******************************************************************************
481 *
482 * FUNCTION: acpi_ex_do_logical_numeric_op
483 *
484 * PARAMETERS: opcode - AML opcode
485 * integer0 - Integer operand #0
486 * integer1 - Integer operand #1
487 * logical_result - TRUE/FALSE result of the operation
488 *
489 * RETURN: Status
490 *
491 * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
492 * operators (LAnd and LOr), both operands must be integers.
493 *
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);
497 *
498 ******************************************************************************/
499
500acpi_status
501acpi_ex_do_logical_numeric_op(u16 opcode,
502 u64 integer0, u64 integer1, u8 *logical_result)
503{
504 acpi_status status = AE_OK;
505 u8 local_result = FALSE;
506
507 ACPI_FUNCTION_TRACE(ex_do_logical_numeric_op);
508
509 switch (opcode) {
510 case AML_LAND_OP: /* LAnd (Integer0, Integer1) */
511
512 if (integer0 && integer1) {
513 local_result = TRUE;
514 }
515 break;
516
517 case AML_LOR_OP: /* LOr (Integer0, Integer1) */
518
519 if (integer0 || integer1) {
520 local_result = TRUE;
521 }
522 break;
523
524 default:
525
526 status = AE_AML_INTERNAL;
527 break;
528 }
529
530 /* Return the logical result and status */
531
532 *logical_result = local_result;
533 return_ACPI_STATUS(status);
534}
535
536/*******************************************************************************
537 *
538 * FUNCTION: acpi_ex_do_logical_op
539 *
540 * PARAMETERS: opcode - AML opcode
541 * operand0 - operand #0
542 * operand1 - operand #1
543 * logical_result - TRUE/FALSE result of the operation
544 *
545 * RETURN: Status
546 *
547 * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
548 * functions here is to prevent a lot of pointer dereferencing
549 * to obtain the operands and to simplify the generation of the
550 * logical value. For the Numeric operators (LAnd and LOr), both
551 * operands must be integers. For the other logical operators,
552 * operands can be any combination of Integer/String/Buffer. The
553 * first operand determines the type to which the second operand
554 * will be converted.
555 *
556 * Note: cleanest machine code seems to be produced by the code
557 * below, rather than using statements of the form:
558 * Result = (Operand0 == Operand1);
559 *
560 ******************************************************************************/
561
562acpi_status
563acpi_ex_do_logical_op(u16 opcode,
564 union acpi_operand_object *operand0,
565 union acpi_operand_object *operand1, u8 * logical_result)
566{
567 union acpi_operand_object *local_operand1 = operand1;
568 u64 integer0;
569 u64 integer1;
570 u32 length0;
571 u32 length1;
572 acpi_status status = AE_OK;
573 u8 local_result = FALSE;
574 int compare;
575
576 ACPI_FUNCTION_TRACE(ex_do_logical_op);
577
578 /*
579 * Convert the second operand if necessary. The first operand
580 * determines the type of the second operand, (See the Data Types
581 * section of the ACPI 3.0+ specification.) Both object types are
582 * guaranteed to be either Integer/String/Buffer by the operand
583 * resolution mechanism.
584 */
585 switch (operand0->common.type) {
586 case ACPI_TYPE_INTEGER:
587
588 status =
589 acpi_ex_convert_to_integer(operand1, &local_operand1, 16);
590 break;
591
592 case ACPI_TYPE_STRING:
593
594 status = acpi_ex_convert_to_string(operand1, &local_operand1,
595 ACPI_IMPLICIT_CONVERT_HEX);
596 break;
597
598 case ACPI_TYPE_BUFFER:
599
600 status = acpi_ex_convert_to_buffer(operand1, &local_operand1);
601 break;
602
603 default:
604
605 status = AE_AML_INTERNAL;
606 break;
607 }
608
609 if (ACPI_FAILURE(status)) {
610 goto cleanup;
611 }
612
613 /*
614 * Two cases: 1) Both Integers, 2) Both Strings or Buffers
615 */
616 if (operand0->common.type == ACPI_TYPE_INTEGER) {
617 /*
618 * 1) Both operands are of type integer
619 * Note: local_operand1 may have changed above
620 */
621 integer0 = operand0->integer.value;
622 integer1 = local_operand1->integer.value;
623
624 switch (opcode) {
625 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
626
627 if (integer0 == integer1) {
628 local_result = TRUE;
629 }
630 break;
631
632 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
633
634 if (integer0 > integer1) {
635 local_result = TRUE;
636 }
637 break;
638
639 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
640
641 if (integer0 < integer1) {
642 local_result = TRUE;
643 }
644 break;
645
646 default:
647
648 status = AE_AML_INTERNAL;
649 break;
650 }
651 } else {
652 /*
653 * 2) Both operands are Strings or both are Buffers
654 * Note: Code below takes advantage of common Buffer/String
655 * object fields. local_operand1 may have changed above. Use
656 * memcmp to handle nulls in buffers.
657 */
658 length0 = operand0->buffer.length;
659 length1 = local_operand1->buffer.length;
660
661 /* Lexicographic compare: compare the data bytes */
662
663 compare = ACPI_MEMCMP(operand0->buffer.pointer,
664 local_operand1->buffer.pointer,
665 (length0 > length1) ? length1 : length0);
666
667 switch (opcode) {
668 case AML_LEQUAL_OP: /* LEqual (Operand0, Operand1) */
669
670 /* Length and all bytes must be equal */
671
672 if ((length0 == length1) && (compare == 0)) {
673
674 /* Length and all bytes match ==> TRUE */
675
676 local_result = TRUE;
677 }
678 break;
679
680 case AML_LGREATER_OP: /* LGreater (Operand0, Operand1) */
681
682 if (compare > 0) {
683 local_result = TRUE;
684 goto cleanup; /* TRUE */
685 }
686 if (compare < 0) {
687 goto cleanup; /* FALSE */
688 }
689
690 /* Bytes match (to shortest length), compare lengths */
691
692 if (length0 > length1) {
693 local_result = TRUE;
694 }
695 break;
696
697 case AML_LLESS_OP: /* LLess (Operand0, Operand1) */
698
699 if (compare > 0) {
700 goto cleanup; /* FALSE */
701 }
702 if (compare < 0) {
703 local_result = TRUE;
704 goto cleanup; /* TRUE */
705 }
706
707 /* Bytes match (to shortest length), compare lengths */
708
709 if (length0 < length1) {
710 local_result = TRUE;
711 }
712 break;
713
714 default:
715
716 status = AE_AML_INTERNAL;
717 break;
718 }
719 }
720
721cleanup:
722
723 /* New object was created if implicit conversion performed - delete */
724
725 if (local_operand1 != operand1) {
726 acpi_ut_remove_reference(local_operand1);
727 }
728
729 /* Return the logical result and status */
730
731 *logical_result = local_result;
732 return_ACPI_STATUS(status);
733}