98bbeed3ff0941290aa97ef95fd33ace61bf3c8e
[reactos.git] / drivers / bus / acpi / acpica / executer / exresop.c
1 /******************************************************************************
2 *
3 * Module Name: exresop - AML Interpreter operand/object resolution
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2017, 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.h"
45 #include "accommon.h"
46 #include "amlcode.h"
47 #include "acparser.h"
48 #include "acinterp.h"
49 #include "acnamesp.h"
50
51
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exresop")
54
55 /* Local prototypes */
56
57 static ACPI_STATUS
58 AcpiExCheckObjectType (
59 ACPI_OBJECT_TYPE TypeNeeded,
60 ACPI_OBJECT_TYPE ThisType,
61 void *Object);
62
63
64 /*******************************************************************************
65 *
66 * FUNCTION: AcpiExCheckObjectType
67 *
68 * PARAMETERS: TypeNeeded Object type needed
69 * ThisType Actual object type
70 * Object Object pointer
71 *
72 * RETURN: Status
73 *
74 * DESCRIPTION: Check required type against actual type
75 *
76 ******************************************************************************/
77
78 static ACPI_STATUS
79 AcpiExCheckObjectType (
80 ACPI_OBJECT_TYPE TypeNeeded,
81 ACPI_OBJECT_TYPE ThisType,
82 void *Object)
83 {
84 ACPI_FUNCTION_ENTRY ();
85
86
87 if (TypeNeeded == ACPI_TYPE_ANY)
88 {
89 /* All types OK, so we don't perform any typechecks */
90
91 return (AE_OK);
92 }
93
94 if (TypeNeeded == ACPI_TYPE_LOCAL_REFERENCE)
95 {
96 /*
97 * Allow the AML "Constant" opcodes (Zero, One, etc.) to be reference
98 * objects and thus allow them to be targets. (As per the ACPI
99 * specification, a store to a constant is a noop.)
100 */
101 if ((ThisType == ACPI_TYPE_INTEGER) &&
102 (((ACPI_OPERAND_OBJECT *) Object)->Common.Flags &
103 AOPOBJ_AML_CONSTANT))
104 {
105 return (AE_OK);
106 }
107 }
108
109 if (TypeNeeded != ThisType)
110 {
111 ACPI_ERROR ((AE_INFO,
112 "Needed type [%s], found [%s] %p",
113 AcpiUtGetTypeName (TypeNeeded),
114 AcpiUtGetTypeName (ThisType), Object));
115
116 return (AE_AML_OPERAND_TYPE);
117 }
118
119 return (AE_OK);
120 }
121
122
123 /*******************************************************************************
124 *
125 * FUNCTION: AcpiExResolveOperands
126 *
127 * PARAMETERS: Opcode - Opcode being interpreted
128 * StackPtr - Pointer to the operand stack to be
129 * resolved
130 * WalkState - Current state
131 *
132 * RETURN: Status
133 *
134 * DESCRIPTION: Convert multiple input operands to the types required by the
135 * target operator.
136 *
137 * Each 5-bit group in ArgTypes represents one required
138 * operand and indicates the required Type. The corresponding operand
139 * will be converted to the required type if possible, otherwise we
140 * abort with an exception.
141 *
142 ******************************************************************************/
143
144 ACPI_STATUS
145 AcpiExResolveOperands (
146 UINT16 Opcode,
147 ACPI_OPERAND_OBJECT **StackPtr,
148 ACPI_WALK_STATE *WalkState)
149 {
150 ACPI_OPERAND_OBJECT *ObjDesc;
151 ACPI_STATUS Status = AE_OK;
152 UINT8 ObjectType;
153 UINT32 ArgTypes;
154 const ACPI_OPCODE_INFO *OpInfo;
155 UINT32 ThisArgType;
156 ACPI_OBJECT_TYPE TypeNeeded;
157 UINT16 TargetOp = 0;
158
159
160 ACPI_FUNCTION_TRACE_U32 (ExResolveOperands, Opcode);
161
162
163 OpInfo = AcpiPsGetOpcodeInfo (Opcode);
164 if (OpInfo->Class == AML_CLASS_UNKNOWN)
165 {
166 return_ACPI_STATUS (AE_AML_BAD_OPCODE);
167 }
168
169 ArgTypes = OpInfo->RuntimeArgs;
170 if (ArgTypes == ARGI_INVALID_OPCODE)
171 {
172 ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
173 Opcode));
174
175 return_ACPI_STATUS (AE_AML_INTERNAL);
176 }
177
178 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
179 "Opcode %X [%s] RequiredOperandTypes=%8.8X\n",
180 Opcode, OpInfo->Name, ArgTypes));
181
182 /*
183 * Normal exit is with (ArgTypes == 0) at end of argument list.
184 * Function will return an exception from within the loop upon
185 * finding an entry which is not (or cannot be converted
186 * to) the required type; if stack underflows; or upon
187 * finding a NULL stack entry (which should not happen).
188 */
189 while (GET_CURRENT_ARG_TYPE (ArgTypes))
190 {
191 if (!StackPtr || !*StackPtr)
192 {
193 ACPI_ERROR ((AE_INFO, "Null stack entry at %p",
194 StackPtr));
195
196 return_ACPI_STATUS (AE_AML_INTERNAL);
197 }
198
199 /* Extract useful items */
200
201 ObjDesc = *StackPtr;
202
203 /* Decode the descriptor type */
204
205 switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
206 {
207 case ACPI_DESC_TYPE_NAMED:
208
209 /* Namespace Node */
210
211 ObjectType = ((ACPI_NAMESPACE_NODE *) ObjDesc)->Type;
212
213 /*
214 * Resolve an alias object. The construction of these objects
215 * guarantees that there is only one level of alias indirection;
216 * thus, the attached object is always the aliased namespace node
217 */
218 if (ObjectType == ACPI_TYPE_LOCAL_ALIAS)
219 {
220 ObjDesc = AcpiNsGetAttachedObject (
221 (ACPI_NAMESPACE_NODE *) ObjDesc);
222 *StackPtr = ObjDesc;
223 ObjectType = ((ACPI_NAMESPACE_NODE *) ObjDesc)->Type;
224 }
225 break;
226
227 case ACPI_DESC_TYPE_OPERAND:
228
229 /* ACPI internal object */
230
231 ObjectType = ObjDesc->Common.Type;
232
233 /* Check for bad ACPI_OBJECT_TYPE */
234
235 if (!AcpiUtValidObjectType (ObjectType))
236 {
237 ACPI_ERROR ((AE_INFO,
238 "Bad operand object type [0x%X]", ObjectType));
239
240 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
241 }
242
243 if (ObjectType == (UINT8) ACPI_TYPE_LOCAL_REFERENCE)
244 {
245 /* Validate the Reference */
246
247 switch (ObjDesc->Reference.Class)
248 {
249 case ACPI_REFCLASS_DEBUG:
250
251 TargetOp = AML_DEBUG_OP;
252
253 /*lint -fallthrough */
254
255 case ACPI_REFCLASS_ARG:
256 case ACPI_REFCLASS_LOCAL:
257 case ACPI_REFCLASS_INDEX:
258 case ACPI_REFCLASS_REFOF:
259 case ACPI_REFCLASS_TABLE: /* DdbHandle from LOAD_OP or LOAD_TABLE_OP */
260 case ACPI_REFCLASS_NAME: /* Reference to a named object */
261
262 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
263 "Operand is a Reference, Class [%s] %2.2X\n",
264 AcpiUtGetReferenceName (ObjDesc),
265 ObjDesc->Reference.Class));
266 break;
267
268 default:
269
270 ACPI_ERROR ((AE_INFO,
271 "Unknown Reference Class 0x%2.2X in %p",
272 ObjDesc->Reference.Class, ObjDesc));
273
274 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
275 }
276 }
277 break;
278
279 default:
280
281 /* Invalid descriptor */
282
283 ACPI_ERROR ((AE_INFO, "Invalid descriptor %p [%s]",
284 ObjDesc, AcpiUtGetDescriptorName (ObjDesc)));
285
286 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
287 }
288
289 /* Get one argument type, point to the next */
290
291 ThisArgType = GET_CURRENT_ARG_TYPE (ArgTypes);
292 INCREMENT_ARG_LIST (ArgTypes);
293
294 /*
295 * Handle cases where the object does not need to be
296 * resolved to a value
297 */
298 switch (ThisArgType)
299 {
300 case ARGI_REF_OR_STRING: /* Can be a String or Reference */
301
302 if ((ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) ==
303 ACPI_DESC_TYPE_OPERAND) &&
304 (ObjDesc->Common.Type == ACPI_TYPE_STRING))
305 {
306 /*
307 * String found - the string references a named object and
308 * must be resolved to a node
309 */
310 goto NextOperand;
311 }
312
313 /*
314 * Else not a string - fall through to the normal Reference
315 * case below
316 */
317 /*lint -fallthrough */
318
319 case ARGI_REFERENCE: /* References: */
320 case ARGI_INTEGER_REF:
321 case ARGI_OBJECT_REF:
322 case ARGI_DEVICE_REF:
323 case ARGI_TARGETREF: /* Allows implicit conversion rules before store */
324 case ARGI_SIMPLE_TARGET: /* Name, Local, or Arg - no implicit conversion */
325 case ARGI_STORE_TARGET:
326
327 /*
328 * Need an operand of type ACPI_TYPE_LOCAL_REFERENCE
329 * A Namespace Node is OK as-is
330 */
331 if (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc) == ACPI_DESC_TYPE_NAMED)
332 {
333 goto NextOperand;
334 }
335
336 Status = AcpiExCheckObjectType (
337 ACPI_TYPE_LOCAL_REFERENCE, ObjectType, ObjDesc);
338 if (ACPI_FAILURE (Status))
339 {
340 return_ACPI_STATUS (Status);
341 }
342 goto NextOperand;
343
344 case ARGI_DATAREFOBJ: /* Store operator only */
345 /*
346 * We don't want to resolve IndexOp reference objects during
347 * a store because this would be an implicit DeRefOf operation.
348 * Instead, we just want to store the reference object.
349 * -- All others must be resolved below.
350 */
351 if ((Opcode == AML_STORE_OP) &&
352 ((*StackPtr)->Common.Type == ACPI_TYPE_LOCAL_REFERENCE) &&
353 ((*StackPtr)->Reference.Class == ACPI_REFCLASS_INDEX))
354 {
355 goto NextOperand;
356 }
357 break;
358
359 default:
360
361 /* All cases covered above */
362
363 break;
364 }
365
366 /*
367 * Resolve this object to a value
368 */
369 Status = AcpiExResolveToValue (StackPtr, WalkState);
370 if (ACPI_FAILURE (Status))
371 {
372 return_ACPI_STATUS (Status);
373 }
374
375 /* Get the resolved object */
376
377 ObjDesc = *StackPtr;
378
379 /*
380 * Check the resulting object (value) type
381 */
382 switch (ThisArgType)
383 {
384 /*
385 * For the simple cases, only one type of resolved object
386 * is allowed
387 */
388 case ARGI_MUTEX:
389
390 /* Need an operand of type ACPI_TYPE_MUTEX */
391
392 TypeNeeded = ACPI_TYPE_MUTEX;
393 break;
394
395 case ARGI_EVENT:
396
397 /* Need an operand of type ACPI_TYPE_EVENT */
398
399 TypeNeeded = ACPI_TYPE_EVENT;
400 break;
401
402 case ARGI_PACKAGE: /* Package */
403
404 /* Need an operand of type ACPI_TYPE_PACKAGE */
405
406 TypeNeeded = ACPI_TYPE_PACKAGE;
407 break;
408
409 case ARGI_ANYTYPE:
410
411 /* Any operand type will do */
412
413 TypeNeeded = ACPI_TYPE_ANY;
414 break;
415
416 case ARGI_DDBHANDLE:
417
418 /* Need an operand of type ACPI_TYPE_DDB_HANDLE */
419
420 TypeNeeded = ACPI_TYPE_LOCAL_REFERENCE;
421 break;
422
423
424 /*
425 * The more complex cases allow multiple resolved object types
426 */
427 case ARGI_INTEGER:
428
429 /*
430 * Need an operand of type ACPI_TYPE_INTEGER, but we can
431 * implicitly convert from a STRING or BUFFER.
432 *
433 * Known as "Implicit Source Operand Conversion"
434 */
435 Status = AcpiExConvertToInteger (ObjDesc, StackPtr,
436 ACPI_STRTOUL_BASE16);
437 if (ACPI_FAILURE (Status))
438 {
439 if (Status == AE_TYPE)
440 {
441 ACPI_ERROR ((AE_INFO,
442 "Needed [Integer/String/Buffer], found [%s] %p",
443 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
444
445 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
446 }
447
448 return_ACPI_STATUS (Status);
449 }
450
451 if (ObjDesc != *StackPtr)
452 {
453 AcpiUtRemoveReference (ObjDesc);
454 }
455 goto NextOperand;
456
457 case ARGI_BUFFER:
458 /*
459 * Need an operand of type ACPI_TYPE_BUFFER,
460 * But we can implicitly convert from a STRING or INTEGER
461 * Aka - "Implicit Source Operand Conversion"
462 */
463 Status = AcpiExConvertToBuffer (ObjDesc, StackPtr);
464 if (ACPI_FAILURE (Status))
465 {
466 if (Status == AE_TYPE)
467 {
468 ACPI_ERROR ((AE_INFO,
469 "Needed [Integer/String/Buffer], found [%s] %p",
470 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
471
472 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
473 }
474
475 return_ACPI_STATUS (Status);
476 }
477
478 if (ObjDesc != *StackPtr)
479 {
480 AcpiUtRemoveReference (ObjDesc);
481 }
482 goto NextOperand;
483
484 case ARGI_STRING:
485 /*
486 * Need an operand of type ACPI_TYPE_STRING,
487 * But we can implicitly convert from a BUFFER or INTEGER
488 * Aka - "Implicit Source Operand Conversion"
489 */
490 Status = AcpiExConvertToString (
491 ObjDesc, StackPtr, ACPI_IMPLICIT_CONVERT_HEX);
492 if (ACPI_FAILURE (Status))
493 {
494 if (Status == AE_TYPE)
495 {
496 ACPI_ERROR ((AE_INFO,
497 "Needed [Integer/String/Buffer], found [%s] %p",
498 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
499
500 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
501 }
502
503 return_ACPI_STATUS (Status);
504 }
505
506 if (ObjDesc != *StackPtr)
507 {
508 AcpiUtRemoveReference (ObjDesc);
509 }
510 goto NextOperand;
511
512 case ARGI_COMPUTEDATA:
513
514 /* Need an operand of type INTEGER, STRING or BUFFER */
515
516 switch (ObjDesc->Common.Type)
517 {
518 case ACPI_TYPE_INTEGER:
519 case ACPI_TYPE_STRING:
520 case ACPI_TYPE_BUFFER:
521
522 /* Valid operand */
523 break;
524
525 default:
526 ACPI_ERROR ((AE_INFO,
527 "Needed [Integer/String/Buffer], found [%s] %p",
528 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
529
530 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
531 }
532 goto NextOperand;
533
534 case ARGI_BUFFER_OR_STRING:
535
536 /* Need an operand of type STRING or BUFFER */
537
538 switch (ObjDesc->Common.Type)
539 {
540 case ACPI_TYPE_STRING:
541 case ACPI_TYPE_BUFFER:
542
543 /* Valid operand */
544 break;
545
546 case ACPI_TYPE_INTEGER:
547
548 /* Highest priority conversion is to type Buffer */
549
550 Status = AcpiExConvertToBuffer (ObjDesc, StackPtr);
551 if (ACPI_FAILURE (Status))
552 {
553 return_ACPI_STATUS (Status);
554 }
555
556 if (ObjDesc != *StackPtr)
557 {
558 AcpiUtRemoveReference (ObjDesc);
559 }
560 break;
561
562 default:
563 ACPI_ERROR ((AE_INFO,
564 "Needed [Integer/String/Buffer], found [%s] %p",
565 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
566
567 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
568 }
569 goto NextOperand;
570
571 case ARGI_DATAOBJECT:
572 /*
573 * ARGI_DATAOBJECT is only used by the SizeOf operator.
574 * Need a buffer, string, package, or RefOf reference.
575 *
576 * The only reference allowed here is a direct reference to
577 * a namespace node.
578 */
579 switch (ObjDesc->Common.Type)
580 {
581 case ACPI_TYPE_PACKAGE:
582 case ACPI_TYPE_STRING:
583 case ACPI_TYPE_BUFFER:
584 case ACPI_TYPE_LOCAL_REFERENCE:
585
586 /* Valid operand */
587 break;
588
589 default:
590
591 ACPI_ERROR ((AE_INFO,
592 "Needed [Buffer/String/Package/Reference], found [%s] %p",
593 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
594
595 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
596 }
597 goto NextOperand;
598
599 case ARGI_COMPLEXOBJ:
600
601 /* Need a buffer or package or (ACPI 2.0) String */
602
603 switch (ObjDesc->Common.Type)
604 {
605 case ACPI_TYPE_PACKAGE:
606 case ACPI_TYPE_STRING:
607 case ACPI_TYPE_BUFFER:
608
609 /* Valid operand */
610 break;
611
612 default:
613
614 ACPI_ERROR ((AE_INFO,
615 "Needed [Buffer/String/Package], found [%s] %p",
616 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
617
618 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
619 }
620 goto NextOperand;
621
622 case ARGI_REGION_OR_BUFFER: /* Used by Load() only */
623
624 /*
625 * Need an operand of type REGION or a BUFFER
626 * (which could be a resolved region field)
627 */
628 switch (ObjDesc->Common.Type)
629 {
630 case ACPI_TYPE_BUFFER:
631 case ACPI_TYPE_REGION:
632
633 /* Valid operand */
634 break;
635
636 default:
637
638 ACPI_ERROR ((AE_INFO,
639 "Needed [Region/Buffer], found [%s] %p",
640 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
641
642 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
643 }
644 goto NextOperand;
645
646 case ARGI_DATAREFOBJ:
647
648 /* Used by the Store() operator only */
649
650 switch (ObjDesc->Common.Type)
651 {
652 case ACPI_TYPE_INTEGER:
653 case ACPI_TYPE_PACKAGE:
654 case ACPI_TYPE_STRING:
655 case ACPI_TYPE_BUFFER:
656 case ACPI_TYPE_BUFFER_FIELD:
657 case ACPI_TYPE_LOCAL_REFERENCE:
658 case ACPI_TYPE_LOCAL_REGION_FIELD:
659 case ACPI_TYPE_LOCAL_BANK_FIELD:
660 case ACPI_TYPE_LOCAL_INDEX_FIELD:
661 case ACPI_TYPE_DDB_HANDLE:
662
663 /* Valid operand */
664 break;
665
666 default:
667
668 if (AcpiGbl_EnableInterpreterSlack)
669 {
670 /*
671 * Enable original behavior of Store(), allowing any
672 * and all objects as the source operand. The ACPI
673 * spec does not allow this, however.
674 */
675 break;
676 }
677
678 if (TargetOp == AML_DEBUG_OP)
679 {
680 /* Allow store of any object to the Debug object */
681
682 break;
683 }
684
685 ACPI_ERROR ((AE_INFO,
686 "Needed Integer/Buffer/String/Package/Ref/Ddb]"
687 ", found [%s] %p",
688 AcpiUtGetObjectTypeName (ObjDesc), ObjDesc));
689
690 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
691 }
692 goto NextOperand;
693
694 default:
695
696 /* Unknown type */
697
698 ACPI_ERROR ((AE_INFO,
699 "Internal - Unknown ARGI (required operand) type 0x%X",
700 ThisArgType));
701
702 return_ACPI_STATUS (AE_BAD_PARAMETER);
703 }
704
705 /*
706 * Make sure that the original object was resolved to the
707 * required object type (Simple cases only).
708 */
709 Status = AcpiExCheckObjectType (
710 TypeNeeded, (*StackPtr)->Common.Type, *StackPtr);
711 if (ACPI_FAILURE (Status))
712 {
713 return_ACPI_STATUS (Status);
714 }
715
716 NextOperand:
717 /*
718 * If more operands needed, decrement StackPtr to point
719 * to next operand on stack
720 */
721 if (GET_CURRENT_ARG_TYPE (ArgTypes))
722 {
723 StackPtr--;
724 }
725 }
726
727 ACPI_DUMP_OPERANDS (WalkState->Operands,
728 AcpiPsGetOpcodeName (Opcode), WalkState->NumOperands);
729
730 return_ACPI_STATUS (Status);
731 }