exresop.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exresop - AML Interpreter operand/object resolution
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "amlcode.h"
  12. #include "acparser.h"
  13. #include "acinterp.h"
  14. #include "acnamesp.h"
  15. #define _COMPONENT ACPI_EXECUTER
  16. ACPI_MODULE_NAME("exresop")
  17. /* Local prototypes */
  18. static acpi_status
  19. acpi_ex_check_object_type(acpi_object_type type_needed,
  20. acpi_object_type this_type, void *object);
  21. /*******************************************************************************
  22. *
  23. * FUNCTION: acpi_ex_check_object_type
  24. *
  25. * PARAMETERS: type_needed Object type needed
  26. * this_type Actual object type
  27. * Object Object pointer
  28. *
  29. * RETURN: Status
  30. *
  31. * DESCRIPTION: Check required type against actual type
  32. *
  33. ******************************************************************************/
  34. static acpi_status
  35. acpi_ex_check_object_type(acpi_object_type type_needed,
  36. acpi_object_type this_type, void *object)
  37. {
  38. ACPI_FUNCTION_ENTRY();
  39. if (type_needed == ACPI_TYPE_ANY) {
  40. /* All types OK, so we don't perform any typechecks */
  41. return (AE_OK);
  42. }
  43. if (type_needed == ACPI_TYPE_LOCAL_REFERENCE) {
  44. /*
  45. * Allow the AML "Constant" opcodes (Zero, One, etc.) to be reference
  46. * objects and thus allow them to be targets. (As per the ACPI
  47. * specification, a store to a constant is a noop.)
  48. */
  49. if ((this_type == ACPI_TYPE_INTEGER) &&
  50. (((union acpi_operand_object *)object)->common.flags &
  51. AOPOBJ_AML_CONSTANT)) {
  52. return (AE_OK);
  53. }
  54. }
  55. if (type_needed != this_type) {
  56. ACPI_ERROR((AE_INFO,
  57. "Needed type [%s], found [%s] %p",
  58. acpi_ut_get_type_name(type_needed),
  59. acpi_ut_get_type_name(this_type), object));
  60. return (AE_AML_OPERAND_TYPE);
  61. }
  62. return (AE_OK);
  63. }
  64. /*******************************************************************************
  65. *
  66. * FUNCTION: acpi_ex_resolve_operands
  67. *
  68. * PARAMETERS: opcode - Opcode being interpreted
  69. * stack_ptr - Pointer to the operand stack to be
  70. * resolved
  71. * walk_state - Current state
  72. *
  73. * RETURN: Status
  74. *
  75. * DESCRIPTION: Convert multiple input operands to the types required by the
  76. * target operator.
  77. *
  78. * Each 5-bit group in arg_types represents one required
  79. * operand and indicates the required Type. The corresponding operand
  80. * will be converted to the required type if possible, otherwise we
  81. * abort with an exception.
  82. *
  83. ******************************************************************************/
  84. acpi_status
  85. acpi_ex_resolve_operands(u16 opcode,
  86. union acpi_operand_object **stack_ptr,
  87. struct acpi_walk_state *walk_state)
  88. {
  89. union acpi_operand_object *obj_desc;
  90. acpi_status status = AE_OK;
  91. u8 object_type;
  92. u32 arg_types;
  93. const struct acpi_opcode_info *op_info;
  94. u32 this_arg_type;
  95. acpi_object_type type_needed;
  96. u16 target_op = 0;
  97. ACPI_FUNCTION_TRACE_U32(ex_resolve_operands, opcode);
  98. op_info = acpi_ps_get_opcode_info(opcode);
  99. if (op_info->class == AML_CLASS_UNKNOWN) {
  100. return_ACPI_STATUS(AE_AML_BAD_OPCODE);
  101. }
  102. arg_types = op_info->runtime_args;
  103. if (arg_types == ARGI_INVALID_OPCODE) {
  104. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X", opcode));
  105. return_ACPI_STATUS(AE_AML_INTERNAL);
  106. }
  107. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  108. "Opcode %X [%s] RequiredOperandTypes=%8.8X\n",
  109. opcode, op_info->name, arg_types));
  110. /*
  111. * Normal exit is with (arg_types == 0) at end of argument list.
  112. * Function will return an exception from within the loop upon
  113. * finding an entry which is not (or cannot be converted
  114. * to) the required type; if stack underflows; or upon
  115. * finding a NULL stack entry (which should not happen).
  116. */
  117. while (GET_CURRENT_ARG_TYPE(arg_types)) {
  118. if (!stack_ptr || !*stack_ptr) {
  119. ACPI_ERROR((AE_INFO, "Null stack entry at %p",
  120. stack_ptr));
  121. return_ACPI_STATUS(AE_AML_INTERNAL);
  122. }
  123. /* Extract useful items */
  124. obj_desc = *stack_ptr;
  125. /* Decode the descriptor type */
  126. switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
  127. case ACPI_DESC_TYPE_NAMED:
  128. /* Namespace Node */
  129. object_type =
  130. ((struct acpi_namespace_node *)obj_desc)->type;
  131. /*
  132. * Resolve an alias object. The construction of these objects
  133. * guarantees that there is only one level of alias indirection;
  134. * thus, the attached object is always the aliased namespace node
  135. */
  136. if (object_type == ACPI_TYPE_LOCAL_ALIAS) {
  137. obj_desc = acpi_ns_get_attached_object((struct
  138. acpi_namespace_node
  139. *)
  140. obj_desc);
  141. *stack_ptr = obj_desc;
  142. object_type =
  143. ((struct acpi_namespace_node *)obj_desc)->
  144. type;
  145. }
  146. break;
  147. case ACPI_DESC_TYPE_OPERAND:
  148. /* ACPI internal object */
  149. object_type = obj_desc->common.type;
  150. /* Check for bad acpi_object_type */
  151. if (!acpi_ut_valid_object_type(object_type)) {
  152. ACPI_ERROR((AE_INFO,
  153. "Bad operand object type [0x%X]",
  154. object_type));
  155. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  156. }
  157. if (object_type == (u8) ACPI_TYPE_LOCAL_REFERENCE) {
  158. /* Validate the Reference */
  159. switch (obj_desc->reference.class) {
  160. case ACPI_REFCLASS_DEBUG:
  161. target_op = AML_DEBUG_OP;
  162. ACPI_FALLTHROUGH;
  163. case ACPI_REFCLASS_ARG:
  164. case ACPI_REFCLASS_LOCAL:
  165. case ACPI_REFCLASS_INDEX:
  166. case ACPI_REFCLASS_REFOF:
  167. case ACPI_REFCLASS_TABLE: /* ddb_handle from LOAD_OP or LOAD_TABLE_OP */
  168. case ACPI_REFCLASS_NAME: /* Reference to a named object */
  169. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  170. "Operand is a Reference, Class [%s] %2.2X\n",
  171. acpi_ut_get_reference_name
  172. (obj_desc),
  173. obj_desc->reference.
  174. class));
  175. break;
  176. default:
  177. ACPI_ERROR((AE_INFO,
  178. "Unknown Reference Class 0x%2.2X in %p",
  179. obj_desc->reference.class,
  180. obj_desc));
  181. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  182. }
  183. }
  184. break;
  185. default:
  186. /* Invalid descriptor */
  187. ACPI_ERROR((AE_INFO, "Invalid descriptor %p [%s]",
  188. obj_desc,
  189. acpi_ut_get_descriptor_name(obj_desc)));
  190. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  191. }
  192. /* Get one argument type, point to the next */
  193. this_arg_type = GET_CURRENT_ARG_TYPE(arg_types);
  194. INCREMENT_ARG_LIST(arg_types);
  195. /*
  196. * Handle cases where the object does not need to be
  197. * resolved to a value
  198. */
  199. switch (this_arg_type) {
  200. case ARGI_REF_OR_STRING: /* Can be a String or Reference */
  201. if ((ACPI_GET_DESCRIPTOR_TYPE(obj_desc) ==
  202. ACPI_DESC_TYPE_OPERAND) &&
  203. (obj_desc->common.type == ACPI_TYPE_STRING)) {
  204. /*
  205. * String found - the string references a named object and
  206. * must be resolved to a node
  207. */
  208. goto next_operand;
  209. }
  210. /*
  211. * Else not a string - fall through to the normal Reference
  212. * case below
  213. */
  214. ACPI_FALLTHROUGH;
  215. case ARGI_REFERENCE: /* References: */
  216. case ARGI_INTEGER_REF:
  217. case ARGI_OBJECT_REF:
  218. case ARGI_DEVICE_REF:
  219. case ARGI_TARGETREF: /* Allows implicit conversion rules before store */
  220. case ARGI_FIXED_TARGET: /* No implicit conversion before store to target */
  221. case ARGI_SIMPLE_TARGET: /* Name, Local, or arg - no implicit conversion */
  222. case ARGI_STORE_TARGET:
  223. /*
  224. * Need an operand of type ACPI_TYPE_LOCAL_REFERENCE
  225. * A Namespace Node is OK as-is
  226. */
  227. if (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) ==
  228. ACPI_DESC_TYPE_NAMED) {
  229. goto next_operand;
  230. }
  231. status =
  232. acpi_ex_check_object_type(ACPI_TYPE_LOCAL_REFERENCE,
  233. object_type, obj_desc);
  234. if (ACPI_FAILURE(status)) {
  235. return_ACPI_STATUS(status);
  236. }
  237. goto next_operand;
  238. case ARGI_DATAREFOBJ: /* Store operator only */
  239. /*
  240. * We don't want to resolve index_op reference objects during
  241. * a store because this would be an implicit de_ref_of operation.
  242. * Instead, we just want to store the reference object.
  243. * -- All others must be resolved below.
  244. */
  245. if ((opcode == AML_STORE_OP) &&
  246. ((*stack_ptr)->common.type ==
  247. ACPI_TYPE_LOCAL_REFERENCE)
  248. && ((*stack_ptr)->reference.class ==
  249. ACPI_REFCLASS_INDEX)) {
  250. goto next_operand;
  251. }
  252. break;
  253. default:
  254. /* All cases covered above */
  255. break;
  256. }
  257. /*
  258. * Resolve this object to a value
  259. */
  260. status = acpi_ex_resolve_to_value(stack_ptr, walk_state);
  261. if (ACPI_FAILURE(status)) {
  262. return_ACPI_STATUS(status);
  263. }
  264. /* Get the resolved object */
  265. obj_desc = *stack_ptr;
  266. /*
  267. * Check the resulting object (value) type
  268. */
  269. switch (this_arg_type) {
  270. /*
  271. * For the simple cases, only one type of resolved object
  272. * is allowed
  273. */
  274. case ARGI_MUTEX:
  275. /* Need an operand of type ACPI_TYPE_MUTEX */
  276. type_needed = ACPI_TYPE_MUTEX;
  277. break;
  278. case ARGI_EVENT:
  279. /* Need an operand of type ACPI_TYPE_EVENT */
  280. type_needed = ACPI_TYPE_EVENT;
  281. break;
  282. case ARGI_PACKAGE: /* Package */
  283. /* Need an operand of type ACPI_TYPE_PACKAGE */
  284. type_needed = ACPI_TYPE_PACKAGE;
  285. break;
  286. case ARGI_ANYTYPE:
  287. /* Any operand type will do */
  288. type_needed = ACPI_TYPE_ANY;
  289. break;
  290. case ARGI_DDBHANDLE:
  291. /* Need an operand of type ACPI_TYPE_DDB_HANDLE */
  292. type_needed = ACPI_TYPE_LOCAL_REFERENCE;
  293. break;
  294. /*
  295. * The more complex cases allow multiple resolved object types
  296. */
  297. case ARGI_INTEGER:
  298. /*
  299. * Need an operand of type ACPI_TYPE_INTEGER, but we can
  300. * implicitly convert from a STRING or BUFFER.
  301. *
  302. * Known as "Implicit Source Operand Conversion"
  303. */
  304. status = acpi_ex_convert_to_integer(obj_desc, stack_ptr,
  305. ACPI_IMPLICIT_CONVERSION);
  306. if (ACPI_FAILURE(status)) {
  307. if (status == AE_TYPE) {
  308. ACPI_ERROR((AE_INFO,
  309. "Needed [Integer/String/Buffer], found [%s] %p",
  310. acpi_ut_get_object_type_name
  311. (obj_desc), obj_desc));
  312. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  313. }
  314. return_ACPI_STATUS(status);
  315. }
  316. if (obj_desc != *stack_ptr) {
  317. acpi_ut_remove_reference(obj_desc);
  318. }
  319. goto next_operand;
  320. case ARGI_BUFFER:
  321. /*
  322. * Need an operand of type ACPI_TYPE_BUFFER,
  323. * But we can implicitly convert from a STRING or INTEGER
  324. * aka - "Implicit Source Operand Conversion"
  325. */
  326. status = acpi_ex_convert_to_buffer(obj_desc, stack_ptr);
  327. if (ACPI_FAILURE(status)) {
  328. if (status == AE_TYPE) {
  329. ACPI_ERROR((AE_INFO,
  330. "Needed [Integer/String/Buffer], found [%s] %p",
  331. acpi_ut_get_object_type_name
  332. (obj_desc), obj_desc));
  333. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  334. }
  335. return_ACPI_STATUS(status);
  336. }
  337. if (obj_desc != *stack_ptr) {
  338. acpi_ut_remove_reference(obj_desc);
  339. }
  340. goto next_operand;
  341. case ARGI_STRING:
  342. /*
  343. * Need an operand of type ACPI_TYPE_STRING,
  344. * But we can implicitly convert from a BUFFER or INTEGER
  345. * aka - "Implicit Source Operand Conversion"
  346. */
  347. status =
  348. acpi_ex_convert_to_string(obj_desc, stack_ptr,
  349. ACPI_IMPLICIT_CONVERT_HEX);
  350. if (ACPI_FAILURE(status)) {
  351. if (status == AE_TYPE) {
  352. ACPI_ERROR((AE_INFO,
  353. "Needed [Integer/String/Buffer], found [%s] %p",
  354. acpi_ut_get_object_type_name
  355. (obj_desc), obj_desc));
  356. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  357. }
  358. return_ACPI_STATUS(status);
  359. }
  360. if (obj_desc != *stack_ptr) {
  361. acpi_ut_remove_reference(obj_desc);
  362. }
  363. goto next_operand;
  364. case ARGI_COMPUTEDATA:
  365. /* Need an operand of type INTEGER, STRING or BUFFER */
  366. switch (obj_desc->common.type) {
  367. case ACPI_TYPE_INTEGER:
  368. case ACPI_TYPE_STRING:
  369. case ACPI_TYPE_BUFFER:
  370. /* Valid operand */
  371. break;
  372. default:
  373. ACPI_ERROR((AE_INFO,
  374. "Needed [Integer/String/Buffer], found [%s] %p",
  375. acpi_ut_get_object_type_name
  376. (obj_desc), obj_desc));
  377. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  378. }
  379. goto next_operand;
  380. case ARGI_BUFFER_OR_STRING:
  381. /* Need an operand of type STRING or BUFFER */
  382. switch (obj_desc->common.type) {
  383. case ACPI_TYPE_STRING:
  384. case ACPI_TYPE_BUFFER:
  385. /* Valid operand */
  386. break;
  387. case ACPI_TYPE_INTEGER:
  388. /* Highest priority conversion is to type Buffer */
  389. status =
  390. acpi_ex_convert_to_buffer(obj_desc,
  391. stack_ptr);
  392. if (ACPI_FAILURE(status)) {
  393. return_ACPI_STATUS(status);
  394. }
  395. if (obj_desc != *stack_ptr) {
  396. acpi_ut_remove_reference(obj_desc);
  397. }
  398. break;
  399. default:
  400. ACPI_ERROR((AE_INFO,
  401. "Needed [Integer/String/Buffer], found [%s] %p",
  402. acpi_ut_get_object_type_name
  403. (obj_desc), obj_desc));
  404. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  405. }
  406. goto next_operand;
  407. case ARGI_DATAOBJECT:
  408. /*
  409. * ARGI_DATAOBJECT is only used by the size_of operator.
  410. * Need a buffer, string, package, or ref_of reference.
  411. *
  412. * The only reference allowed here is a direct reference to
  413. * a namespace node.
  414. */
  415. switch (obj_desc->common.type) {
  416. case ACPI_TYPE_PACKAGE:
  417. case ACPI_TYPE_STRING:
  418. case ACPI_TYPE_BUFFER:
  419. case ACPI_TYPE_LOCAL_REFERENCE:
  420. /* Valid operand */
  421. break;
  422. default:
  423. ACPI_ERROR((AE_INFO,
  424. "Needed [Buffer/String/Package/Reference], found [%s] %p",
  425. acpi_ut_get_object_type_name
  426. (obj_desc), obj_desc));
  427. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  428. }
  429. goto next_operand;
  430. case ARGI_COMPLEXOBJ:
  431. /* Need a buffer or package or (ACPI 2.0) String */
  432. switch (obj_desc->common.type) {
  433. case ACPI_TYPE_PACKAGE:
  434. case ACPI_TYPE_STRING:
  435. case ACPI_TYPE_BUFFER:
  436. /* Valid operand */
  437. break;
  438. default:
  439. ACPI_ERROR((AE_INFO,
  440. "Needed [Buffer/String/Package], found [%s] %p",
  441. acpi_ut_get_object_type_name
  442. (obj_desc), obj_desc));
  443. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  444. }
  445. goto next_operand;
  446. case ARGI_REGION_OR_BUFFER: /* Used by Load() only */
  447. /*
  448. * Need an operand of type REGION or a BUFFER
  449. * (which could be a resolved region field)
  450. */
  451. switch (obj_desc->common.type) {
  452. case ACPI_TYPE_BUFFER:
  453. case ACPI_TYPE_REGION:
  454. /* Valid operand */
  455. break;
  456. default:
  457. ACPI_ERROR((AE_INFO,
  458. "Needed [Region/Buffer], found [%s] %p",
  459. acpi_ut_get_object_type_name
  460. (obj_desc), obj_desc));
  461. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  462. }
  463. goto next_operand;
  464. case ARGI_DATAREFOBJ:
  465. /* Used by the Store() operator only */
  466. switch (obj_desc->common.type) {
  467. case ACPI_TYPE_INTEGER:
  468. case ACPI_TYPE_PACKAGE:
  469. case ACPI_TYPE_STRING:
  470. case ACPI_TYPE_BUFFER:
  471. case ACPI_TYPE_BUFFER_FIELD:
  472. case ACPI_TYPE_LOCAL_REFERENCE:
  473. case ACPI_TYPE_LOCAL_REGION_FIELD:
  474. case ACPI_TYPE_LOCAL_BANK_FIELD:
  475. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  476. case ACPI_TYPE_DDB_HANDLE:
  477. /* Valid operand */
  478. break;
  479. default:
  480. if (acpi_gbl_enable_interpreter_slack) {
  481. /*
  482. * Enable original behavior of Store(), allowing any
  483. * and all objects as the source operand. The ACPI
  484. * spec does not allow this, however.
  485. */
  486. break;
  487. }
  488. if (target_op == AML_DEBUG_OP) {
  489. /* Allow store of any object to the Debug object */
  490. break;
  491. }
  492. ACPI_ERROR((AE_INFO,
  493. "Needed Integer/Buffer/String/Package/Ref/Ddb]"
  494. ", found [%s] %p",
  495. acpi_ut_get_object_type_name
  496. (obj_desc), obj_desc));
  497. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  498. }
  499. goto next_operand;
  500. default:
  501. /* Unknown type */
  502. ACPI_ERROR((AE_INFO,
  503. "Internal - Unknown ARGI (required operand) type 0x%X",
  504. this_arg_type));
  505. return_ACPI_STATUS(AE_BAD_PARAMETER);
  506. }
  507. /*
  508. * Make sure that the original object was resolved to the
  509. * required object type (Simple cases only).
  510. */
  511. status =
  512. acpi_ex_check_object_type(type_needed,
  513. (*stack_ptr)->common.type,
  514. *stack_ptr);
  515. if (ACPI_FAILURE(status)) {
  516. return_ACPI_STATUS(status);
  517. }
  518. next_operand:
  519. /*
  520. * If more operands needed, decrement stack_ptr to point
  521. * to next operand on stack
  522. */
  523. if (GET_CURRENT_ARG_TYPE(arg_types)) {
  524. stack_ptr--;
  525. }
  526. }
  527. ACPI_DUMP_OPERANDS(walk_state->operands,
  528. acpi_ps_get_opcode_name(opcode),
  529. walk_state->num_operands);
  530. return_ACPI_STATUS(status);
  531. }