dsobject.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /******************************************************************************
  2. *
  3. * Module Name: dsobject - Dispatcher object management routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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(op->common.value.
  108. string, status);
  109. return_ACPI_STATUS(status);
  110. }
  111. }
  112. }
  113. }
  114. create_new_object:
  115. /* Create and init a new internal ACPI object */
  116. obj_desc = acpi_ut_create_internal_object((acpi_ps_get_opcode_info
  117. (op->common.aml_opcode))->
  118. object_type);
  119. if (!obj_desc) {
  120. return_ACPI_STATUS(AE_NO_MEMORY);
  121. }
  122. status =
  123. acpi_ds_init_object_from_op(walk_state, op, op->common.aml_opcode,
  124. &obj_desc);
  125. if (ACPI_FAILURE(status)) {
  126. acpi_ut_remove_reference(obj_desc);
  127. return_ACPI_STATUS(status);
  128. }
  129. /*
  130. * Handling for unresolved package reference elements.
  131. * These are elements that are namepaths.
  132. */
  133. if ((op->common.parent->common.aml_opcode == AML_PACKAGE_OP) ||
  134. (op->common.parent->common.aml_opcode == AML_VARIABLE_PACKAGE_OP)) {
  135. obj_desc->reference.resolved = TRUE;
  136. if ((op->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
  137. !obj_desc->reference.node) {
  138. /*
  139. * Name was unresolved above.
  140. * Get the prefix node for later lookup
  141. */
  142. obj_desc->reference.node =
  143. walk_state->scope_info->scope.node;
  144. obj_desc->reference.aml = op->common.aml;
  145. obj_desc->reference.resolved = FALSE;
  146. }
  147. }
  148. *obj_desc_ptr = obj_desc;
  149. return_ACPI_STATUS(status);
  150. }
  151. /*******************************************************************************
  152. *
  153. * FUNCTION: acpi_ds_build_internal_buffer_obj
  154. *
  155. * PARAMETERS: walk_state - Current walk state
  156. * op - Parser object to be translated
  157. * buffer_length - Length of the buffer
  158. * obj_desc_ptr - Where the ACPI internal object is returned
  159. *
  160. * RETURN: Status
  161. *
  162. * DESCRIPTION: Translate a parser Op package object to the equivalent
  163. * namespace object
  164. *
  165. ******************************************************************************/
  166. acpi_status
  167. acpi_ds_build_internal_buffer_obj(struct acpi_walk_state *walk_state,
  168. union acpi_parse_object *op,
  169. u32 buffer_length,
  170. union acpi_operand_object **obj_desc_ptr)
  171. {
  172. union acpi_parse_object *arg;
  173. union acpi_operand_object *obj_desc;
  174. union acpi_parse_object *byte_list;
  175. u32 byte_list_length = 0;
  176. ACPI_FUNCTION_TRACE(ds_build_internal_buffer_obj);
  177. /*
  178. * If we are evaluating a Named buffer object "Name (xxxx, Buffer)".
  179. * The buffer object already exists (from the NS node), otherwise it must
  180. * be created.
  181. */
  182. obj_desc = *obj_desc_ptr;
  183. if (!obj_desc) {
  184. /* Create a new buffer object */
  185. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_BUFFER);
  186. *obj_desc_ptr = obj_desc;
  187. if (!obj_desc) {
  188. return_ACPI_STATUS(AE_NO_MEMORY);
  189. }
  190. }
  191. /*
  192. * Second arg is the buffer data (optional) byte_list can be either
  193. * individual bytes or a string initializer. In either case, a
  194. * byte_list appears in the AML.
  195. */
  196. arg = op->common.value.arg; /* skip first arg */
  197. byte_list = arg->named.next;
  198. if (byte_list) {
  199. if (byte_list->common.aml_opcode != AML_INT_BYTELIST_OP) {
  200. ACPI_ERROR((AE_INFO,
  201. "Expecting bytelist, found AML opcode 0x%X in op %p",
  202. byte_list->common.aml_opcode, byte_list));
  203. acpi_ut_remove_reference(obj_desc);
  204. return (AE_TYPE);
  205. }
  206. byte_list_length = (u32) byte_list->common.value.integer;
  207. }
  208. /*
  209. * The buffer length (number of bytes) will be the larger of:
  210. * 1) The specified buffer length and
  211. * 2) The length of the initializer byte list
  212. */
  213. obj_desc->buffer.length = buffer_length;
  214. if (byte_list_length > buffer_length) {
  215. obj_desc->buffer.length = byte_list_length;
  216. }
  217. /* Allocate the buffer */
  218. if (obj_desc->buffer.length == 0) {
  219. obj_desc->buffer.pointer = NULL;
  220. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  221. "Buffer defined with zero length in AML, creating\n"));
  222. } else {
  223. obj_desc->buffer.pointer =
  224. ACPI_ALLOCATE_ZEROED(obj_desc->buffer.length);
  225. if (!obj_desc->buffer.pointer) {
  226. acpi_ut_delete_object_desc(obj_desc);
  227. return_ACPI_STATUS(AE_NO_MEMORY);
  228. }
  229. /* Initialize buffer from the byte_list (if present) */
  230. if (byte_list) {
  231. memcpy(obj_desc->buffer.pointer, byte_list->named.data,
  232. byte_list_length);
  233. }
  234. }
  235. obj_desc->buffer.flags |= AOPOBJ_DATA_VALID;
  236. op->common.node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_desc);
  237. return_ACPI_STATUS(AE_OK);
  238. }
  239. /*******************************************************************************
  240. *
  241. * FUNCTION: acpi_ds_create_node
  242. *
  243. * PARAMETERS: walk_state - Current walk state
  244. * node - NS Node to be initialized
  245. * op - Parser object to be translated
  246. *
  247. * RETURN: Status
  248. *
  249. * DESCRIPTION: Create the object to be associated with a namespace node
  250. *
  251. ******************************************************************************/
  252. acpi_status
  253. acpi_ds_create_node(struct acpi_walk_state *walk_state,
  254. struct acpi_namespace_node *node,
  255. union acpi_parse_object *op)
  256. {
  257. acpi_status status;
  258. union acpi_operand_object *obj_desc;
  259. ACPI_FUNCTION_TRACE_PTR(ds_create_node, op);
  260. /*
  261. * Because of the execution pass through the non-control-method
  262. * parts of the table, we can arrive here twice. Only init
  263. * the named object node the first time through
  264. */
  265. if (acpi_ns_get_attached_object(node)) {
  266. return_ACPI_STATUS(AE_OK);
  267. }
  268. if (!op->common.value.arg) {
  269. /* No arguments, there is nothing to do */
  270. return_ACPI_STATUS(AE_OK);
  271. }
  272. /* Build an internal object for the argument(s) */
  273. status =
  274. acpi_ds_build_internal_object(walk_state, op->common.value.arg,
  275. &obj_desc);
  276. if (ACPI_FAILURE(status)) {
  277. return_ACPI_STATUS(status);
  278. }
  279. /* Re-type the object according to its argument */
  280. node->type = obj_desc->common.type;
  281. /* Attach obj to node */
  282. status = acpi_ns_attach_object(node, obj_desc, node->type);
  283. /* Remove local reference to the object */
  284. acpi_ut_remove_reference(obj_desc);
  285. return_ACPI_STATUS(status);
  286. }
  287. #endif /* ACPI_NO_METHOD_EXECUTION */
  288. /*******************************************************************************
  289. *
  290. * FUNCTION: acpi_ds_init_object_from_op
  291. *
  292. * PARAMETERS: walk_state - Current walk state
  293. * op - Parser op used to init the internal object
  294. * opcode - AML opcode associated with the object
  295. * ret_obj_desc - Namespace object to be initialized
  296. *
  297. * RETURN: Status
  298. *
  299. * DESCRIPTION: Initialize a namespace object from a parser Op and its
  300. * associated arguments. The namespace object is a more compact
  301. * representation of the Op and its arguments.
  302. *
  303. ******************************************************************************/
  304. acpi_status
  305. acpi_ds_init_object_from_op(struct acpi_walk_state *walk_state,
  306. union acpi_parse_object *op,
  307. u16 opcode,
  308. union acpi_operand_object **ret_obj_desc)
  309. {
  310. const struct acpi_opcode_info *op_info;
  311. union acpi_operand_object *obj_desc;
  312. acpi_status status = AE_OK;
  313. ACPI_FUNCTION_TRACE(ds_init_object_from_op);
  314. obj_desc = *ret_obj_desc;
  315. op_info = acpi_ps_get_opcode_info(opcode);
  316. if (op_info->class == AML_CLASS_UNKNOWN) {
  317. /* Unknown opcode */
  318. return_ACPI_STATUS(AE_TYPE);
  319. }
  320. /* Perform per-object initialization */
  321. switch (obj_desc->common.type) {
  322. case ACPI_TYPE_BUFFER:
  323. /*
  324. * Defer evaluation of Buffer term_arg operand
  325. */
  326. obj_desc->buffer.node =
  327. ACPI_CAST_PTR(struct acpi_namespace_node,
  328. walk_state->operands[0]);
  329. obj_desc->buffer.aml_start = op->named.data;
  330. obj_desc->buffer.aml_length = op->named.length;
  331. break;
  332. case ACPI_TYPE_PACKAGE:
  333. /*
  334. * Defer evaluation of Package term_arg operand and all
  335. * package elements. (01/2017): We defer the element
  336. * resolution to allow forward references from the package
  337. * in order to provide compatibility with other ACPI
  338. * implementations.
  339. */
  340. obj_desc->package.node =
  341. ACPI_CAST_PTR(struct acpi_namespace_node,
  342. walk_state->operands[0]);
  343. if (!op->named.data) {
  344. return_ACPI_STATUS(AE_OK);
  345. }
  346. obj_desc->package.aml_start = op->named.data;
  347. obj_desc->package.aml_length = op->named.length;
  348. break;
  349. case ACPI_TYPE_INTEGER:
  350. switch (op_info->type) {
  351. case AML_TYPE_CONSTANT:
  352. /*
  353. * Resolve AML Constants here - AND ONLY HERE!
  354. * All constants are integers.
  355. * We mark the integer with a flag that indicates that it started
  356. * life as a constant -- so that stores to constants will perform
  357. * as expected (noop). zero_op is used as a placeholder for optional
  358. * target operands.
  359. */
  360. obj_desc->common.flags = AOPOBJ_AML_CONSTANT;
  361. switch (opcode) {
  362. case AML_ZERO_OP:
  363. obj_desc->integer.value = 0;
  364. break;
  365. case AML_ONE_OP:
  366. obj_desc->integer.value = 1;
  367. break;
  368. case AML_ONES_OP:
  369. obj_desc->integer.value = ACPI_UINT64_MAX;
  370. /* Truncate value if we are executing from a 32-bit ACPI table */
  371. #ifndef ACPI_NO_METHOD_EXECUTION
  372. (void)acpi_ex_truncate_for32bit_table(obj_desc);
  373. #endif
  374. break;
  375. case AML_REVISION_OP:
  376. obj_desc->integer.value = ACPI_CA_VERSION;
  377. break;
  378. default:
  379. ACPI_ERROR((AE_INFO,
  380. "Unknown constant opcode 0x%X",
  381. opcode));
  382. status = AE_AML_OPERAND_TYPE;
  383. break;
  384. }
  385. break;
  386. case AML_TYPE_LITERAL:
  387. obj_desc->integer.value = op->common.value.integer;
  388. #ifndef ACPI_NO_METHOD_EXECUTION
  389. if (acpi_ex_truncate_for32bit_table(obj_desc)) {
  390. /* Warn if we found a 64-bit constant in a 32-bit table */
  391. ACPI_WARNING((AE_INFO,
  392. "Truncated 64-bit constant found in 32-bit table: %8.8X%8.8X => %8.8X",
  393. ACPI_FORMAT_UINT64(op->common.
  394. value.integer),
  395. (u32)obj_desc->integer.value));
  396. }
  397. #endif
  398. break;
  399. default:
  400. ACPI_ERROR((AE_INFO, "Unknown Integer type 0x%X",
  401. op_info->type));
  402. status = AE_AML_OPERAND_TYPE;
  403. break;
  404. }
  405. break;
  406. case ACPI_TYPE_STRING:
  407. obj_desc->string.pointer = op->common.value.string;
  408. obj_desc->string.length = (u32)strlen(op->common.value.string);
  409. /*
  410. * The string is contained in the ACPI table, don't ever try
  411. * to delete it
  412. */
  413. obj_desc->common.flags |= AOPOBJ_STATIC_POINTER;
  414. break;
  415. case ACPI_TYPE_METHOD:
  416. break;
  417. case ACPI_TYPE_LOCAL_REFERENCE:
  418. switch (op_info->type) {
  419. case AML_TYPE_LOCAL_VARIABLE:
  420. /* Local ID (0-7) is (AML opcode - base AML_FIRST_LOCAL_OP) */
  421. obj_desc->reference.value =
  422. ((u32)opcode) - AML_FIRST_LOCAL_OP;
  423. obj_desc->reference.class = ACPI_REFCLASS_LOCAL;
  424. #ifndef ACPI_NO_METHOD_EXECUTION
  425. status =
  426. acpi_ds_method_data_get_node(ACPI_REFCLASS_LOCAL,
  427. obj_desc->reference.
  428. value, walk_state,
  429. ACPI_CAST_INDIRECT_PTR
  430. (struct
  431. acpi_namespace_node,
  432. &obj_desc->reference.
  433. object));
  434. #endif
  435. break;
  436. case AML_TYPE_METHOD_ARGUMENT:
  437. /* Arg ID (0-6) is (AML opcode - base AML_FIRST_ARG_OP) */
  438. obj_desc->reference.value =
  439. ((u32)opcode) - AML_FIRST_ARG_OP;
  440. obj_desc->reference.class = ACPI_REFCLASS_ARG;
  441. #ifndef ACPI_NO_METHOD_EXECUTION
  442. status = acpi_ds_method_data_get_node(ACPI_REFCLASS_ARG,
  443. obj_desc->
  444. reference.value,
  445. walk_state,
  446. ACPI_CAST_INDIRECT_PTR
  447. (struct
  448. acpi_namespace_node,
  449. &obj_desc->
  450. reference.
  451. object));
  452. #endif
  453. break;
  454. default: /* Object name or Debug object */
  455. switch (op->common.aml_opcode) {
  456. case AML_INT_NAMEPATH_OP:
  457. /* Node was saved in Op */
  458. obj_desc->reference.node = op->common.node;
  459. obj_desc->reference.class = ACPI_REFCLASS_NAME;
  460. if (op->common.node) {
  461. obj_desc->reference.object =
  462. op->common.node->object;
  463. }
  464. break;
  465. case AML_DEBUG_OP:
  466. obj_desc->reference.class = ACPI_REFCLASS_DEBUG;
  467. break;
  468. default:
  469. ACPI_ERROR((AE_INFO,
  470. "Unimplemented reference type for AML opcode: 0x%4.4X",
  471. opcode));
  472. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  473. }
  474. break;
  475. }
  476. break;
  477. default:
  478. ACPI_ERROR((AE_INFO, "Unimplemented data type: 0x%X",
  479. obj_desc->common.type));
  480. status = AE_AML_OPERAND_TYPE;
  481. break;
  482. }
  483. return_ACPI_STATUS(status);
  484. }