dsobject.c 16 KB

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