dsobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: dsobject - Dispatcher object management routines
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acparser.h"
  12. #include "amlcode.h"
  13. #include "acdispat.h"
  14. #include "acnamesp.h"
  15. #include "acinterp.h"
  16. #define _COMPONENT ACPI_DISPATCHER
  17. ACPI_MODULE_NAME("dsobject")
  18. #ifndef ACPI_NO_METHOD_EXECUTION
  19. /*******************************************************************************
  20. *
  21. * FUNCTION: acpi_ds_build_internal_object
  22. *
  23. * PARAMETERS: walk_state - Current walk state
  24. * op - Parser object to be translated
  25. * obj_desc_ptr - Where the ACPI internal object is returned
  26. *
  27. * RETURN: Status
  28. *
  29. * DESCRIPTION: Translate a parser Op object to the equivalent namespace object
  30. * Simple objects are any objects other than a package object!
  31. *
  32. ******************************************************************************/
  33. acpi_status
  34. acpi_ds_build_internal_object(struct acpi_walk_state *walk_state,
  35. union acpi_parse_object *op,
  36. union acpi_operand_object **obj_desc_ptr)
  37. {
  38. union acpi_operand_object *obj_desc;
  39. acpi_status status;
  40. ACPI_FUNCTION_TRACE(ds_build_internal_object);
  41. *obj_desc_ptr = NULL;
  42. if (op->common.aml_opcode == AML_INT_NAMEPATH_OP) {
  43. /*
  44. * This is a named object reference. If this name was
  45. * previously looked up in the namespace, it was stored in
  46. * this op. Otherwise, go ahead and look it up now
  47. */
  48. if (!op->common.node) {
  49. /* Check if we are resolving a named reference within a package */
  50. if ((op->common.parent->common.aml_opcode ==
  51. AML_PACKAGE_OP)
  52. || (op->common.parent->common.aml_opcode ==
  53. AML_VARIABLE_PACKAGE_OP)) {
  54. /*
  55. * We won't resolve package elements here, we will do this
  56. * after all ACPI tables are loaded into the namespace. This
  57. * behavior supports both forward references to named objects
  58. * and external references to objects in other tables.
  59. */
  60. goto create_new_object;
  61. } else {
  62. status = acpi_ns_lookup(walk_state->scope_info,
  63. op->common.value.string,
  64. ACPI_TYPE_ANY,
  65. ACPI_IMODE_EXECUTE,
  66. ACPI_NS_SEARCH_PARENT |
  67. ACPI_NS_DONT_OPEN_SCOPE,
  68. NULL,
  69. ACPI_CAST_INDIRECT_PTR
  70. (struct
  71. acpi_namespace_node,
  72. &(op->common.node)));
  73. if (ACPI_FAILURE(status)) {
  74. ACPI_ERROR_NAMESPACE(walk_state->
  75. scope_info,
  76. op->common.value.
  77. string, status);
  78. return_ACPI_STATUS(status);
  79. }
  80. }
  81. }
  82. }
  83. create_new_object:
  84. /* Create and init a new internal ACPI object */
  85. obj_desc = acpi_ut_create_internal_object((acpi_ps_get_opcode_info
  86. (op->common.aml_opcode))->
  87. object_type);
  88. if (!obj_desc) {
  89. return_ACPI_STATUS(AE_NO_MEMORY);
  90. }
  91. status =
  92. acpi_ds_init_object_from_op(walk_state, op, op->common.aml_opcode,
  93. &obj_desc);
  94. if (ACPI_FAILURE(status)) {
  95. acpi_ut_remove_reference(obj_desc);
  96. return_ACPI_STATUS(status);
  97. }
  98. /*
  99. * Handling for unresolved package reference elements.
  100. * These are elements that are namepaths.
  101. */
  102. if ((op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
  103. (op->common.parent->common.aml_opcode == AML_VARIABLE_PACKAGE_OP)) {
  104. obj_desc->reference.resolved = TRUE;
  105. if ((op->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
  106. !obj_desc->reference.node) {
  107. /*
  108. * Name was unresolved above.
  109. * Get the prefix node for later lookup
  110. */
  111. obj_desc->reference.node =
  112. walk_state->scope_info->scope.node;
  113. obj_desc->reference.aml = op->common.aml;
  114. obj_desc->reference.resolved = FALSE;
  115. }
  116. }
  117. *obj_desc_ptr = obj_desc;
  118. return_ACPI_STATUS(status);
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_ds_build_internal_buffer_obj
  123. *
  124. * PARAMETERS: walk_state - Current walk state
  125. * op - Parser object to be translated
  126. * buffer_length - Length of the buffer
  127. * obj_desc_ptr - Where the ACPI internal object is returned
  128. *
  129. * RETURN: Status
  130. *
  131. * DESCRIPTION: Translate a parser Op package object to the equivalent
  132. * namespace object
  133. *
  134. ******************************************************************************/
  135. acpi_status
  136. acpi_ds_build_internal_buffer_obj(struct acpi_walk_state *walk_state,
  137. union acpi_parse_object *op,
  138. u32 buffer_length,
  139. union acpi_operand_object **obj_desc_ptr)
  140. {
  141. union acpi_parse_object *arg;
  142. union acpi_operand_object *obj_desc;
  143. union acpi_parse_object *byte_list;
  144. u32 byte_list_length = 0;
  145. ACPI_FUNCTION_TRACE(ds_build_internal_buffer_obj);
  146. /*
  147. * If we are evaluating a Named buffer object "Name (xxxx, Buffer)".
  148. * The buffer object already exists (from the NS node), otherwise it must
  149. * be created.
  150. */
  151. obj_desc = *obj_desc_ptr;
  152. if (!obj_desc) {
  153. /* Create a new buffer object */
  154. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_BUFFER);
  155. *obj_desc_ptr = obj_desc;
  156. if (!obj_desc) {
  157. return_ACPI_STATUS(AE_NO_MEMORY);
  158. }
  159. }
  160. /*
  161. * Second arg is the buffer data (optional) byte_list can be either
  162. * individual bytes or a string initializer. In either case, a
  163. * byte_list appears in the AML.
  164. */
  165. arg = op->common.value.arg; /* skip first arg */
  166. byte_list = arg->named.next;
  167. if (byte_list) {
  168. if (byte_list->common.aml_opcode != AML_INT_BYTELIST_OP) {
  169. ACPI_ERROR((AE_INFO,
  170. "Expecting bytelist, found AML opcode 0x%X in op %p",
  171. byte_list->common.aml_opcode, byte_list));
  172. acpi_ut_remove_reference(obj_desc);
  173. return (AE_TYPE);
  174. }
  175. byte_list_length = (u32) byte_list->common.value.integer;
  176. }
  177. /*
  178. * The buffer length (number of bytes) will be the larger of:
  179. * 1) The specified buffer length and
  180. * 2) The length of the initializer byte list
  181. */
  182. obj_desc->buffer.length = buffer_length;
  183. if (byte_list_length > buffer_length) {
  184. obj_desc->buffer.length = byte_list_length;
  185. }
  186. /* Allocate the buffer */
  187. if (obj_desc->buffer.length == 0) {
  188. obj_desc->buffer.pointer = NULL;
  189. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  190. "Buffer defined with zero length in AML, creating\n"));
  191. } else {
  192. obj_desc->buffer.pointer =
  193. ACPI_ALLOCATE_ZEROED(obj_desc->buffer.length);
  194. if (!obj_desc->buffer.pointer) {
  195. acpi_ut_delete_object_desc(obj_desc);
  196. return_ACPI_STATUS(AE_NO_MEMORY);
  197. }
  198. /* Initialize buffer from the byte_list (if present) */
  199. if (byte_list) {
  200. memcpy(obj_desc->buffer.pointer, byte_list->named.data,
  201. byte_list_length);
  202. }
  203. }
  204. obj_desc->buffer.flags |= AOPOBJ_DATA_VALID;
  205. op->common.node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_desc);
  206. return_ACPI_STATUS(AE_OK);
  207. }
  208. /*******************************************************************************
  209. *
  210. * FUNCTION: acpi_ds_create_node
  211. *
  212. * PARAMETERS: walk_state - Current walk state
  213. * node - NS Node to be initialized
  214. * op - Parser object to be translated
  215. *
  216. * RETURN: Status
  217. *
  218. * DESCRIPTION: Create the object to be associated with a namespace node
  219. *
  220. ******************************************************************************/
  221. acpi_status
  222. acpi_ds_create_node(struct acpi_walk_state *walk_state,
  223. struct acpi_namespace_node *node,
  224. union acpi_parse_object *op)
  225. {
  226. acpi_status status;
  227. union acpi_operand_object *obj_desc;
  228. ACPI_FUNCTION_TRACE_PTR(ds_create_node, op);
  229. /*
  230. * Because of the execution pass through the non-control-method
  231. * parts of the table, we can arrive here twice. Only init
  232. * the named object node the first time through
  233. */
  234. if (acpi_ns_get_attached_object(node)) {
  235. return_ACPI_STATUS(AE_OK);
  236. }
  237. if (!op->common.value.arg) {
  238. /* No arguments, there is nothing to do */
  239. return_ACPI_STATUS(AE_OK);
  240. }
  241. /* Build an internal object for the argument(s) */
  242. status =
  243. acpi_ds_build_internal_object(walk_state, op->common.value.arg,
  244. &obj_desc);
  245. if (ACPI_FAILURE(status)) {
  246. return_ACPI_STATUS(status);
  247. }
  248. /* Re-type the object according to its argument */
  249. node->type = obj_desc->common.type;
  250. /* Attach obj to node */
  251. status = acpi_ns_attach_object(node, obj_desc, node->type);
  252. /* Remove local reference to the object */
  253. acpi_ut_remove_reference(obj_desc);
  254. return_ACPI_STATUS(status);
  255. }
  256. #endif /* ACPI_NO_METHOD_EXECUTION */
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_ds_init_object_from_op
  260. *
  261. * PARAMETERS: walk_state - Current walk state
  262. * op - Parser op used to init the internal object
  263. * opcode - AML opcode associated with the object
  264. * ret_obj_desc - Namespace object to be initialized
  265. *
  266. * RETURN: Status
  267. *
  268. * DESCRIPTION: Initialize a namespace object from a parser Op and its
  269. * associated arguments. The namespace object is a more compact
  270. * representation of the Op and its arguments.
  271. *
  272. ******************************************************************************/
  273. acpi_status
  274. acpi_ds_init_object_from_op(struct acpi_walk_state *walk_state,
  275. union acpi_parse_object *op,
  276. u16 opcode,
  277. union acpi_operand_object **ret_obj_desc)
  278. {
  279. const struct acpi_opcode_info *op_info;
  280. union acpi_operand_object *obj_desc;
  281. acpi_status status = AE_OK;
  282. ACPI_FUNCTION_TRACE(ds_init_object_from_op);
  283. obj_desc = *ret_obj_desc;
  284. op_info = acpi_ps_get_opcode_info(opcode);
  285. if (op_info->class == AML_CLASS_UNKNOWN) {
  286. /* Unknown opcode */
  287. return_ACPI_STATUS(AE_TYPE);
  288. }
  289. /* Perform per-object initialization */
  290. switch (obj_desc->common.type) {
  291. case ACPI_TYPE_BUFFER:
  292. /*
  293. * Defer evaluation of Buffer term_arg operand
  294. */
  295. obj_desc->buffer.node =
  296. ACPI_CAST_PTR(struct acpi_namespace_node,
  297. walk_state->operands[0]);
  298. obj_desc->buffer.aml_start = op->named.data;
  299. obj_desc->buffer.aml_length = op->named.length;
  300. break;
  301. case ACPI_TYPE_PACKAGE:
  302. /*
  303. * Defer evaluation of Package term_arg operand and all
  304. * package elements. (01/2017): We defer the element
  305. * resolution to allow forward references from the package
  306. * in order to provide compatibility with other ACPI
  307. * implementations.
  308. */
  309. obj_desc->package.node =
  310. ACPI_CAST_PTR(struct acpi_namespace_node,
  311. walk_state->operands[0]);
  312. if (!op->named.data) {
  313. return_ACPI_STATUS(AE_OK);
  314. }
  315. obj_desc->package.aml_start = op->named.data;
  316. obj_desc->package.aml_length = op->named.length;
  317. break;
  318. case ACPI_TYPE_INTEGER:
  319. switch (op_info->type) {
  320. case AML_TYPE_CONSTANT:
  321. /*
  322. * Resolve AML Constants here - AND ONLY HERE!
  323. * All constants are integers.
  324. * We mark the integer with a flag that indicates that it started
  325. * life as a constant -- so that stores to constants will perform
  326. * as expected (noop). zero_op is used as a placeholder for optional
  327. * target operands.
  328. */
  329. obj_desc->common.flags = AOPOBJ_AML_CONSTANT;
  330. switch (opcode) {
  331. case AML_ZERO_OP:
  332. obj_desc->integer.value = 0;
  333. break;
  334. case AML_ONE_OP:
  335. obj_desc->integer.value = 1;
  336. break;
  337. case AML_ONES_OP:
  338. obj_desc->integer.value = ACPI_UINT64_MAX;
  339. /* Truncate value if we are executing from a 32-bit ACPI table */
  340. #ifndef ACPI_NO_METHOD_EXECUTION
  341. (void)acpi_ex_truncate_for32bit_table(obj_desc);
  342. #endif
  343. break;
  344. case AML_REVISION_OP:
  345. obj_desc->integer.value = ACPI_CA_VERSION;
  346. break;
  347. default:
  348. ACPI_ERROR((AE_INFO,
  349. "Unknown constant opcode 0x%X",
  350. opcode));
  351. status = AE_AML_OPERAND_TYPE;
  352. break;
  353. }
  354. break;
  355. case AML_TYPE_LITERAL:
  356. obj_desc->integer.value = op->common.value.integer;
  357. #ifndef ACPI_NO_METHOD_EXECUTION
  358. if (acpi_ex_truncate_for32bit_table(obj_desc)) {
  359. /* Warn if we found a 64-bit constant in a 32-bit table */
  360. ACPI_WARNING((AE_INFO,
  361. "Truncated 64-bit constant found in 32-bit table: %8.8X%8.8X => %8.8X",
  362. ACPI_FORMAT_UINT64(op->common.
  363. value.integer),
  364. (u32)obj_desc->integer.value));
  365. }
  366. #endif
  367. break;
  368. default:
  369. ACPI_ERROR((AE_INFO, "Unknown Integer type 0x%X",
  370. op_info->type));
  371. status = AE_AML_OPERAND_TYPE;
  372. break;
  373. }
  374. break;
  375. case ACPI_TYPE_STRING:
  376. obj_desc->string.pointer = op->common.value.string;
  377. obj_desc->string.length = (u32)strlen(op->common.value.string);
  378. /*
  379. * The string is contained in the ACPI table, don't ever try
  380. * to delete it
  381. */
  382. obj_desc->common.flags |= AOPOBJ_STATIC_POINTER;
  383. break;
  384. case ACPI_TYPE_METHOD:
  385. break;
  386. case ACPI_TYPE_LOCAL_REFERENCE:
  387. switch (op_info->type) {
  388. case AML_TYPE_LOCAL_VARIABLE:
  389. /* Local ID (0-7) is (AML opcode - base AML_FIRST_LOCAL_OP) */
  390. obj_desc->reference.value =
  391. ((u32)opcode) - AML_FIRST_LOCAL_OP;
  392. obj_desc->reference.class = ACPI_REFCLASS_LOCAL;
  393. #ifndef ACPI_NO_METHOD_EXECUTION
  394. status =
  395. acpi_ds_method_data_get_node(ACPI_REFCLASS_LOCAL,
  396. obj_desc->reference.
  397. value, walk_state,
  398. ACPI_CAST_INDIRECT_PTR
  399. (struct
  400. acpi_namespace_node,
  401. &obj_desc->reference.
  402. object));
  403. #endif
  404. break;
  405. case AML_TYPE_METHOD_ARGUMENT:
  406. /* Arg ID (0-6) is (AML opcode - base AML_FIRST_ARG_OP) */
  407. obj_desc->reference.value =
  408. ((u32)opcode) - AML_FIRST_ARG_OP;
  409. obj_desc->reference.class = ACPI_REFCLASS_ARG;
  410. #ifndef ACPI_NO_METHOD_EXECUTION
  411. status = acpi_ds_method_data_get_node(ACPI_REFCLASS_ARG,
  412. obj_desc->
  413. reference.value,
  414. walk_state,
  415. ACPI_CAST_INDIRECT_PTR
  416. (struct
  417. acpi_namespace_node,
  418. &obj_desc->
  419. reference.
  420. object));
  421. #endif
  422. break;
  423. default: /* Object name or Debug object */
  424. switch (op->common.aml_opcode) {
  425. case AML_INT_NAMEPATH_OP:
  426. /* Node was saved in Op */
  427. obj_desc->reference.node = op->common.node;
  428. obj_desc->reference.class = ACPI_REFCLASS_NAME;
  429. if (op->common.node) {
  430. obj_desc->reference.object =
  431. op->common.node->object;
  432. }
  433. break;
  434. case AML_DEBUG_OP:
  435. obj_desc->reference.class = ACPI_REFCLASS_DEBUG;
  436. break;
  437. default:
  438. ACPI_ERROR((AE_INFO,
  439. "Unimplemented reference type for AML opcode: 0x%4.4X",
  440. opcode));
  441. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  442. }
  443. break;
  444. }
  445. break;
  446. default:
  447. ACPI_ERROR((AE_INFO, "Unimplemented data type: 0x%X",
  448. obj_desc->common.type));
  449. status = AE_AML_OPERAND_TYPE;
  450. break;
  451. }
  452. return_ACPI_STATUS(status);
  453. }