exresolv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exresolv - AML Interpreter 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 "acdispat.h"
  13. #include "acinterp.h"
  14. #include "acnamesp.h"
  15. #define _COMPONENT ACPI_EXECUTER
  16. ACPI_MODULE_NAME("exresolv")
  17. /* Local prototypes */
  18. static acpi_status
  19. acpi_ex_resolve_object_to_value(union acpi_operand_object **stack_ptr,
  20. struct acpi_walk_state *walk_state);
  21. /*******************************************************************************
  22. *
  23. * FUNCTION: acpi_ex_resolve_to_value
  24. *
  25. * PARAMETERS: **stack_ptr - Points to entry on obj_stack, which can
  26. * be either an (union acpi_operand_object *)
  27. * or an acpi_handle.
  28. * walk_state - Current method state
  29. *
  30. * RETURN: Status
  31. *
  32. * DESCRIPTION: Convert Reference objects to values
  33. *
  34. ******************************************************************************/
  35. acpi_status
  36. acpi_ex_resolve_to_value(union acpi_operand_object **stack_ptr,
  37. struct acpi_walk_state *walk_state)
  38. {
  39. acpi_status status;
  40. ACPI_FUNCTION_TRACE_PTR(ex_resolve_to_value, stack_ptr);
  41. if (!stack_ptr || !*stack_ptr) {
  42. ACPI_ERROR((AE_INFO, "Internal - null pointer"));
  43. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  44. }
  45. /*
  46. * The entity pointed to by the stack_ptr can be either
  47. * 1) A valid union acpi_operand_object, or
  48. * 2) A struct acpi_namespace_node (named_obj)
  49. */
  50. if (ACPI_GET_DESCRIPTOR_TYPE(*stack_ptr) == ACPI_DESC_TYPE_OPERAND) {
  51. status = acpi_ex_resolve_object_to_value(stack_ptr, walk_state);
  52. if (ACPI_FAILURE(status)) {
  53. return_ACPI_STATUS(status);
  54. }
  55. if (!*stack_ptr) {
  56. ACPI_ERROR((AE_INFO, "Internal - null pointer"));
  57. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  58. }
  59. }
  60. /*
  61. * Object on the stack may have changed if acpi_ex_resolve_object_to_value()
  62. * was called (i.e., we can't use an _else_ here.)
  63. */
  64. if (ACPI_GET_DESCRIPTOR_TYPE(*stack_ptr) == ACPI_DESC_TYPE_NAMED) {
  65. status =
  66. acpi_ex_resolve_node_to_value(ACPI_CAST_INDIRECT_PTR
  67. (struct acpi_namespace_node,
  68. stack_ptr), walk_state);
  69. if (ACPI_FAILURE(status)) {
  70. return_ACPI_STATUS(status);
  71. }
  72. }
  73. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Resolved object %p\n", *stack_ptr));
  74. return_ACPI_STATUS(AE_OK);
  75. }
  76. /*******************************************************************************
  77. *
  78. * FUNCTION: acpi_ex_resolve_object_to_value
  79. *
  80. * PARAMETERS: stack_ptr - Pointer to an internal object
  81. * walk_state - Current method state
  82. *
  83. * RETURN: Status
  84. *
  85. * DESCRIPTION: Retrieve the value from an internal object. The Reference type
  86. * uses the associated AML opcode to determine the value.
  87. *
  88. ******************************************************************************/
  89. static acpi_status
  90. acpi_ex_resolve_object_to_value(union acpi_operand_object **stack_ptr,
  91. struct acpi_walk_state *walk_state)
  92. {
  93. acpi_status status = AE_OK;
  94. union acpi_operand_object *stack_desc;
  95. union acpi_operand_object *obj_desc = NULL;
  96. u8 ref_type;
  97. ACPI_FUNCTION_TRACE(ex_resolve_object_to_value);
  98. stack_desc = *stack_ptr;
  99. /* This is an object of type union acpi_operand_object */
  100. switch (stack_desc->common.type) {
  101. case ACPI_TYPE_LOCAL_REFERENCE:
  102. ref_type = stack_desc->reference.class;
  103. switch (ref_type) {
  104. case ACPI_REFCLASS_LOCAL:
  105. case ACPI_REFCLASS_ARG:
  106. /*
  107. * Get the local from the method's state info
  108. * Note: this increments the local's object reference count
  109. */
  110. status = acpi_ds_method_data_get_value(ref_type,
  111. stack_desc->
  112. reference.value,
  113. walk_state,
  114. &obj_desc);
  115. if (ACPI_FAILURE(status)) {
  116. return_ACPI_STATUS(status);
  117. }
  118. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  119. "[Arg/Local %X] ValueObj is %p\n",
  120. stack_desc->reference.value,
  121. obj_desc));
  122. /*
  123. * Now we can delete the original Reference Object and
  124. * replace it with the resolved value
  125. */
  126. acpi_ut_remove_reference(stack_desc);
  127. *stack_ptr = obj_desc;
  128. break;
  129. case ACPI_REFCLASS_INDEX:
  130. switch (stack_desc->reference.target_type) {
  131. case ACPI_TYPE_BUFFER_FIELD:
  132. /* Just return - do not dereference */
  133. break;
  134. case ACPI_TYPE_PACKAGE:
  135. /* If method call or copy_object - do not dereference */
  136. if ((walk_state->opcode ==
  137. AML_INT_METHODCALL_OP)
  138. || (walk_state->opcode ==
  139. AML_COPY_OBJECT_OP)) {
  140. break;
  141. }
  142. /* Otherwise, dereference the package_index to a package element */
  143. obj_desc = *stack_desc->reference.where;
  144. if (obj_desc) {
  145. /*
  146. * Valid object descriptor, copy pointer to return value
  147. * (i.e., dereference the package index)
  148. * Delete the ref object, increment the returned object
  149. */
  150. acpi_ut_add_reference(obj_desc);
  151. *stack_ptr = obj_desc;
  152. } else {
  153. /*
  154. * A NULL object descriptor means an uninitialized element of
  155. * the package, can't dereference it
  156. */
  157. ACPI_ERROR((AE_INFO,
  158. "Attempt to dereference an Index to "
  159. "NULL package element Idx=%p",
  160. stack_desc));
  161. status = AE_AML_UNINITIALIZED_ELEMENT;
  162. }
  163. break;
  164. default:
  165. /* Invalid reference object */
  166. ACPI_ERROR((AE_INFO,
  167. "Unknown TargetType 0x%X in Index/Reference object %p",
  168. stack_desc->reference.target_type,
  169. stack_desc));
  170. status = AE_AML_INTERNAL;
  171. break;
  172. }
  173. break;
  174. case ACPI_REFCLASS_REFOF:
  175. case ACPI_REFCLASS_DEBUG:
  176. case ACPI_REFCLASS_TABLE:
  177. /* Just leave the object as-is, do not dereference */
  178. break;
  179. case ACPI_REFCLASS_NAME: /* Reference to a named object */
  180. /* Dereference the name */
  181. if ((stack_desc->reference.node->type ==
  182. ACPI_TYPE_DEVICE)
  183. || (stack_desc->reference.node->type ==
  184. ACPI_TYPE_THERMAL)) {
  185. /* These node types do not have 'real' subobjects */
  186. *stack_ptr = (void *)stack_desc->reference.node;
  187. } else {
  188. /* Get the object pointed to by the namespace node */
  189. *stack_ptr =
  190. (stack_desc->reference.node)->object;
  191. acpi_ut_add_reference(*stack_ptr);
  192. }
  193. acpi_ut_remove_reference(stack_desc);
  194. break;
  195. default:
  196. ACPI_ERROR((AE_INFO,
  197. "Unknown Reference type 0x%X in %p",
  198. ref_type, stack_desc));
  199. status = AE_AML_INTERNAL;
  200. break;
  201. }
  202. break;
  203. case ACPI_TYPE_BUFFER:
  204. status = acpi_ds_get_buffer_arguments(stack_desc);
  205. break;
  206. case ACPI_TYPE_PACKAGE:
  207. status = acpi_ds_get_package_arguments(stack_desc);
  208. break;
  209. case ACPI_TYPE_BUFFER_FIELD:
  210. case ACPI_TYPE_LOCAL_REGION_FIELD:
  211. case ACPI_TYPE_LOCAL_BANK_FIELD:
  212. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  213. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  214. "FieldRead SourceDesc=%p Type=%X\n",
  215. stack_desc, stack_desc->common.type));
  216. status =
  217. acpi_ex_read_data_from_field(walk_state, stack_desc,
  218. &obj_desc);
  219. /* Remove a reference to the original operand, then override */
  220. acpi_ut_remove_reference(*stack_ptr);
  221. *stack_ptr = (void *)obj_desc;
  222. break;
  223. default:
  224. break;
  225. }
  226. return_ACPI_STATUS(status);
  227. }
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ex_resolve_multiple
  231. *
  232. * PARAMETERS: walk_state - Current state (contains AML opcode)
  233. * operand - Starting point for resolution
  234. * return_type - Where the object type is returned
  235. * return_desc - Where the resolved object is returned
  236. *
  237. * RETURN: Status
  238. *
  239. * DESCRIPTION: Return the base object and type. Traverse a reference list if
  240. * necessary to get to the base object.
  241. *
  242. ******************************************************************************/
  243. acpi_status
  244. acpi_ex_resolve_multiple(struct acpi_walk_state *walk_state,
  245. union acpi_operand_object *operand,
  246. acpi_object_type *return_type,
  247. union acpi_operand_object **return_desc)
  248. {
  249. union acpi_operand_object *obj_desc = ACPI_CAST_PTR(void, operand);
  250. struct acpi_namespace_node *node =
  251. ACPI_CAST_PTR(struct acpi_namespace_node, operand);
  252. acpi_object_type type;
  253. acpi_status status;
  254. ACPI_FUNCTION_TRACE(acpi_ex_resolve_multiple);
  255. /* Operand can be either a namespace node or an operand descriptor */
  256. switch (ACPI_GET_DESCRIPTOR_TYPE(obj_desc)) {
  257. case ACPI_DESC_TYPE_OPERAND:
  258. type = obj_desc->common.type;
  259. break;
  260. case ACPI_DESC_TYPE_NAMED:
  261. type = ((struct acpi_namespace_node *)obj_desc)->type;
  262. obj_desc = acpi_ns_get_attached_object(node);
  263. /* If we had an Alias node, use the attached object for type info */
  264. if (type == ACPI_TYPE_LOCAL_ALIAS) {
  265. type = ((struct acpi_namespace_node *)obj_desc)->type;
  266. obj_desc = acpi_ns_get_attached_object((struct
  267. acpi_namespace_node
  268. *)obj_desc);
  269. }
  270. switch (type) {
  271. case ACPI_TYPE_DEVICE:
  272. case ACPI_TYPE_THERMAL:
  273. /* These types have no attached subobject */
  274. break;
  275. default:
  276. /* All other types require a subobject */
  277. if (!obj_desc) {
  278. ACPI_ERROR((AE_INFO,
  279. "[%4.4s] Node is unresolved or uninitialized",
  280. acpi_ut_get_node_name(node)));
  281. return_ACPI_STATUS(AE_AML_UNINITIALIZED_NODE);
  282. }
  283. break;
  284. }
  285. break;
  286. default:
  287. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  288. }
  289. /* If type is anything other than a reference, we are done */
  290. if (type != ACPI_TYPE_LOCAL_REFERENCE) {
  291. goto exit;
  292. }
  293. /*
  294. * For reference objects created via the ref_of, Index, or Load/load_table
  295. * operators, we need to get to the base object (as per the ACPI
  296. * specification of the object_type and size_of operators). This means
  297. * traversing the list of possibly many nested references.
  298. */
  299. while (obj_desc->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  300. switch (obj_desc->reference.class) {
  301. case ACPI_REFCLASS_REFOF:
  302. case ACPI_REFCLASS_NAME:
  303. /* Dereference the reference pointer */
  304. if (obj_desc->reference.class == ACPI_REFCLASS_REFOF) {
  305. node = obj_desc->reference.object;
  306. } else { /* AML_INT_NAMEPATH_OP */
  307. node = obj_desc->reference.node;
  308. }
  309. /* All "References" point to a NS node */
  310. if (ACPI_GET_DESCRIPTOR_TYPE(node) !=
  311. ACPI_DESC_TYPE_NAMED) {
  312. ACPI_ERROR((AE_INFO,
  313. "Not a namespace node %p [%s]",
  314. node,
  315. acpi_ut_get_descriptor_name(node)));
  316. return_ACPI_STATUS(AE_AML_INTERNAL);
  317. }
  318. /* Get the attached object */
  319. obj_desc = acpi_ns_get_attached_object(node);
  320. if (!obj_desc) {
  321. /* No object, use the NS node type */
  322. type = acpi_ns_get_type(node);
  323. goto exit;
  324. }
  325. /* Check for circular references */
  326. if (obj_desc == operand) {
  327. return_ACPI_STATUS(AE_AML_CIRCULAR_REFERENCE);
  328. }
  329. break;
  330. case ACPI_REFCLASS_INDEX:
  331. /* Get the type of this reference (index into another object) */
  332. type = obj_desc->reference.target_type;
  333. if (type != ACPI_TYPE_PACKAGE) {
  334. goto exit;
  335. }
  336. /*
  337. * The main object is a package, we want to get the type
  338. * of the individual package element that is referenced by
  339. * the index.
  340. *
  341. * This could of course in turn be another reference object.
  342. */
  343. obj_desc = *(obj_desc->reference.where);
  344. if (!obj_desc) {
  345. /* NULL package elements are allowed */
  346. type = 0; /* Uninitialized */
  347. goto exit;
  348. }
  349. break;
  350. case ACPI_REFCLASS_TABLE:
  351. type = ACPI_TYPE_DDB_HANDLE;
  352. goto exit;
  353. case ACPI_REFCLASS_LOCAL:
  354. case ACPI_REFCLASS_ARG:
  355. if (return_desc) {
  356. status =
  357. acpi_ds_method_data_get_value(obj_desc->
  358. reference.
  359. class,
  360. obj_desc->
  361. reference.
  362. value,
  363. walk_state,
  364. &obj_desc);
  365. if (ACPI_FAILURE(status)) {
  366. return_ACPI_STATUS(status);
  367. }
  368. acpi_ut_remove_reference(obj_desc);
  369. } else {
  370. status =
  371. acpi_ds_method_data_get_node(obj_desc->
  372. reference.
  373. class,
  374. obj_desc->
  375. reference.
  376. value,
  377. walk_state,
  378. &node);
  379. if (ACPI_FAILURE(status)) {
  380. return_ACPI_STATUS(status);
  381. }
  382. obj_desc = acpi_ns_get_attached_object(node);
  383. if (!obj_desc) {
  384. type = ACPI_TYPE_ANY;
  385. goto exit;
  386. }
  387. }
  388. break;
  389. case ACPI_REFCLASS_DEBUG:
  390. /* The Debug Object is of type "DebugObject" */
  391. type = ACPI_TYPE_DEBUG_OBJECT;
  392. goto exit;
  393. default:
  394. ACPI_ERROR((AE_INFO,
  395. "Unknown Reference Class 0x%2.2X",
  396. obj_desc->reference.class));
  397. return_ACPI_STATUS(AE_AML_INTERNAL);
  398. }
  399. }
  400. /*
  401. * Now we are guaranteed to have an object that has not been created
  402. * via the ref_of or Index operators.
  403. */
  404. type = obj_desc->common.type;
  405. exit:
  406. /* Convert internal types to external types */
  407. switch (type) {
  408. case ACPI_TYPE_LOCAL_REGION_FIELD:
  409. case ACPI_TYPE_LOCAL_BANK_FIELD:
  410. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  411. type = ACPI_TYPE_FIELD_UNIT;
  412. break;
  413. case ACPI_TYPE_LOCAL_SCOPE:
  414. /* Per ACPI Specification, Scope is untyped */
  415. type = ACPI_TYPE_ANY;
  416. break;
  417. default:
  418. /* No change to Type required */
  419. break;
  420. }
  421. *return_type = type;
  422. if (return_desc) {
  423. *return_desc = obj_desc;
  424. }
  425. return_ACPI_STATUS(AE_OK);
  426. }