exstore.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /******************************************************************************
  2. *
  3. * Module Name: exstore - AML Interpreter object store support
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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 "acdispat.h"
  45. #include "acinterp.h"
  46. #include "amlcode.h"
  47. #include "acnamesp.h"
  48. #define _COMPONENT ACPI_EXECUTER
  49. ACPI_MODULE_NAME("exstore")
  50. /* Local prototypes */
  51. static acpi_status
  52. acpi_ex_store_object_to_index(union acpi_operand_object *val_desc,
  53. union acpi_operand_object *dest_desc,
  54. struct acpi_walk_state *walk_state);
  55. static acpi_status
  56. acpi_ex_store_direct_to_node(union acpi_operand_object *source_desc,
  57. struct acpi_namespace_node *node,
  58. struct acpi_walk_state *walk_state);
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_ex_store
  62. *
  63. * PARAMETERS: *source_desc - Value to be stored
  64. * *dest_desc - Where to store it. Must be an NS node
  65. * or union acpi_operand_object of type
  66. * Reference;
  67. * walk_state - Current walk state
  68. *
  69. * RETURN: Status
  70. *
  71. * DESCRIPTION: Store the value described by source_desc into the location
  72. * described by dest_desc. Called by various interpreter
  73. * functions to store the result of an operation into
  74. * the destination operand -- not just simply the actual "Store"
  75. * ASL operator.
  76. *
  77. ******************************************************************************/
  78. acpi_status
  79. acpi_ex_store(union acpi_operand_object *source_desc,
  80. union acpi_operand_object *dest_desc,
  81. struct acpi_walk_state *walk_state)
  82. {
  83. acpi_status status = AE_OK;
  84. union acpi_operand_object *ref_desc = dest_desc;
  85. ACPI_FUNCTION_TRACE_PTR(ex_store, dest_desc);
  86. /* Validate parameters */
  87. if (!source_desc || !dest_desc) {
  88. ACPI_ERROR((AE_INFO, "Null parameter"));
  89. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  90. }
  91. /* dest_desc can be either a namespace node or an ACPI object */
  92. if (ACPI_GET_DESCRIPTOR_TYPE(dest_desc) == ACPI_DESC_TYPE_NAMED) {
  93. /*
  94. * Dest is a namespace node,
  95. * Storing an object into a Named node.
  96. */
  97. status = acpi_ex_store_object_to_node(source_desc,
  98. (struct
  99. acpi_namespace_node *)
  100. dest_desc, walk_state,
  101. ACPI_IMPLICIT_CONVERSION);
  102. return_ACPI_STATUS(status);
  103. }
  104. /* Destination object must be a Reference or a Constant object */
  105. switch (dest_desc->common.type) {
  106. case ACPI_TYPE_LOCAL_REFERENCE:
  107. break;
  108. case ACPI_TYPE_INTEGER:
  109. /* Allow stores to Constants -- a Noop as per ACPI spec */
  110. if (dest_desc->common.flags & AOPOBJ_AML_CONSTANT) {
  111. return_ACPI_STATUS(AE_OK);
  112. }
  113. /*lint -fallthrough */
  114. default:
  115. /* Destination is not a Reference object */
  116. ACPI_ERROR((AE_INFO,
  117. "Target is not a Reference or Constant object - [%s] %p",
  118. acpi_ut_get_object_type_name(dest_desc),
  119. dest_desc));
  120. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  121. }
  122. /*
  123. * Examine the Reference class. These cases are handled:
  124. *
  125. * 1) Store to Name (Change the object associated with a name)
  126. * 2) Store to an indexed area of a Buffer or Package
  127. * 3) Store to a Method Local or Arg
  128. * 4) Store to the debug object
  129. */
  130. switch (ref_desc->reference.class) {
  131. case ACPI_REFCLASS_REFOF:
  132. /* Storing an object into a Name "container" */
  133. status = acpi_ex_store_object_to_node(source_desc,
  134. ref_desc->reference.
  135. object, walk_state,
  136. ACPI_IMPLICIT_CONVERSION);
  137. break;
  138. case ACPI_REFCLASS_INDEX:
  139. /* Storing to an Index (pointer into a packager or buffer) */
  140. status =
  141. acpi_ex_store_object_to_index(source_desc, ref_desc,
  142. walk_state);
  143. break;
  144. case ACPI_REFCLASS_LOCAL:
  145. case ACPI_REFCLASS_ARG:
  146. /* Store to a method local/arg */
  147. status =
  148. acpi_ds_store_object_to_local(ref_desc->reference.class,
  149. ref_desc->reference.value,
  150. source_desc, walk_state);
  151. break;
  152. case ACPI_REFCLASS_DEBUG:
  153. /*
  154. * Storing to the Debug object causes the value stored to be
  155. * displayed and otherwise has no effect -- see ACPI Specification
  156. */
  157. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  158. "**** Write to Debug Object: Object %p [%s] ****:\n\n",
  159. source_desc,
  160. acpi_ut_get_object_type_name(source_desc)));
  161. ACPI_DEBUG_OBJECT(source_desc, 0, 0);
  162. break;
  163. default:
  164. ACPI_ERROR((AE_INFO, "Unknown Reference Class 0x%2.2X",
  165. ref_desc->reference.class));
  166. ACPI_DUMP_ENTRY(ref_desc, ACPI_LV_INFO);
  167. status = AE_AML_INTERNAL;
  168. break;
  169. }
  170. return_ACPI_STATUS(status);
  171. }
  172. /*******************************************************************************
  173. *
  174. * FUNCTION: acpi_ex_store_object_to_index
  175. *
  176. * PARAMETERS: *source_desc - Value to be stored
  177. * *dest_desc - Named object to receive the value
  178. * walk_state - Current walk state
  179. *
  180. * RETURN: Status
  181. *
  182. * DESCRIPTION: Store the object to indexed Buffer or Package element
  183. *
  184. ******************************************************************************/
  185. static acpi_status
  186. acpi_ex_store_object_to_index(union acpi_operand_object *source_desc,
  187. union acpi_operand_object *index_desc,
  188. struct acpi_walk_state *walk_state)
  189. {
  190. acpi_status status = AE_OK;
  191. union acpi_operand_object *obj_desc;
  192. union acpi_operand_object *new_desc;
  193. u8 value = 0;
  194. u32 i;
  195. ACPI_FUNCTION_TRACE(ex_store_object_to_index);
  196. /*
  197. * Destination must be a reference pointer, and
  198. * must point to either a buffer or a package
  199. */
  200. switch (index_desc->reference.target_type) {
  201. case ACPI_TYPE_PACKAGE:
  202. /*
  203. * Storing to a package element. Copy the object and replace
  204. * any existing object with the new object. No implicit
  205. * conversion is performed.
  206. *
  207. * The object at *(index_desc->Reference.Where) is the
  208. * element within the package that is to be modified.
  209. * The parent package object is at index_desc->Reference.Object
  210. */
  211. obj_desc = *(index_desc->reference.where);
  212. if (source_desc->common.type == ACPI_TYPE_LOCAL_REFERENCE &&
  213. source_desc->reference.class == ACPI_REFCLASS_TABLE) {
  214. /* This is a DDBHandle, just add a reference to it */
  215. acpi_ut_add_reference(source_desc);
  216. new_desc = source_desc;
  217. } else {
  218. /* Normal object, copy it */
  219. status =
  220. acpi_ut_copy_iobject_to_iobject(source_desc,
  221. &new_desc,
  222. walk_state);
  223. if (ACPI_FAILURE(status)) {
  224. return_ACPI_STATUS(status);
  225. }
  226. }
  227. if (obj_desc) {
  228. /* Decrement reference count by the ref count of the parent package */
  229. for (i = 0; i < ((union acpi_operand_object *)
  230. index_desc->reference.object)->common.
  231. reference_count; i++) {
  232. acpi_ut_remove_reference(obj_desc);
  233. }
  234. }
  235. *(index_desc->reference.where) = new_desc;
  236. /* Increment ref count by the ref count of the parent package-1 */
  237. for (i = 1; i < ((union acpi_operand_object *)
  238. index_desc->reference.object)->common.
  239. reference_count; i++) {
  240. acpi_ut_add_reference(new_desc);
  241. }
  242. break;
  243. case ACPI_TYPE_BUFFER_FIELD:
  244. /*
  245. * Store into a Buffer or String (not actually a real buffer_field)
  246. * at a location defined by an Index.
  247. *
  248. * The first 8-bit element of the source object is written to the
  249. * 8-bit Buffer location defined by the Index destination object,
  250. * according to the ACPI 2.0 specification.
  251. */
  252. /*
  253. * Make sure the target is a Buffer or String. An error should
  254. * not happen here, since the reference_object was constructed
  255. * by the INDEX_OP code.
  256. */
  257. obj_desc = index_desc->reference.object;
  258. if ((obj_desc->common.type != ACPI_TYPE_BUFFER) &&
  259. (obj_desc->common.type != ACPI_TYPE_STRING)) {
  260. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  261. }
  262. /*
  263. * The assignment of the individual elements will be slightly
  264. * different for each source type.
  265. */
  266. switch (source_desc->common.type) {
  267. case ACPI_TYPE_INTEGER:
  268. /* Use the least-significant byte of the integer */
  269. value = (u8) (source_desc->integer.value);
  270. break;
  271. case ACPI_TYPE_BUFFER:
  272. case ACPI_TYPE_STRING:
  273. /* Note: Takes advantage of common string/buffer fields */
  274. value = source_desc->buffer.pointer[0];
  275. break;
  276. default:
  277. /* All other types are invalid */
  278. ACPI_ERROR((AE_INFO,
  279. "Source must be type [Integer/Buffer/String], found [%s]",
  280. acpi_ut_get_object_type_name(source_desc)));
  281. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  282. }
  283. /* Store the source value into the target buffer byte */
  284. obj_desc->buffer.pointer[index_desc->reference.value] = value;
  285. break;
  286. default:
  287. ACPI_ERROR((AE_INFO,
  288. "Target is not of type [Package/BufferField]"));
  289. status = AE_AML_TARGET_TYPE;
  290. break;
  291. }
  292. return_ACPI_STATUS(status);
  293. }
  294. /*******************************************************************************
  295. *
  296. * FUNCTION: acpi_ex_store_object_to_node
  297. *
  298. * PARAMETERS: source_desc - Value to be stored
  299. * node - Named object to receive the value
  300. * walk_state - Current walk state
  301. * implicit_conversion - Perform implicit conversion (yes/no)
  302. *
  303. * RETURN: Status
  304. *
  305. * DESCRIPTION: Store the object to the named object.
  306. *
  307. * The assignment of an object to a named object is handled here.
  308. * The value passed in will replace the current value (if any)
  309. * with the input value.
  310. *
  311. * When storing into an object the data is converted to the
  312. * target object type then stored in the object. This means
  313. * that the target object type (for an initialized target) will
  314. * not be changed by a store operation. A copy_object can change
  315. * the target type, however.
  316. *
  317. * The implicit_conversion flag is set to NO/FALSE only when
  318. * storing to an arg_x -- as per the rules of the ACPI spec.
  319. *
  320. * Assumes parameters are already validated.
  321. *
  322. ******************************************************************************/
  323. acpi_status
  324. acpi_ex_store_object_to_node(union acpi_operand_object *source_desc,
  325. struct acpi_namespace_node *node,
  326. struct acpi_walk_state *walk_state,
  327. u8 implicit_conversion)
  328. {
  329. acpi_status status = AE_OK;
  330. union acpi_operand_object *target_desc;
  331. union acpi_operand_object *new_desc;
  332. acpi_object_type target_type;
  333. ACPI_FUNCTION_TRACE_PTR(ex_store_object_to_node, source_desc);
  334. /* Get current type of the node, and object attached to Node */
  335. target_type = acpi_ns_get_type(node);
  336. target_desc = acpi_ns_get_attached_object(node);
  337. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Storing %p [%s] to node %p [%s]\n",
  338. source_desc,
  339. acpi_ut_get_object_type_name(source_desc), node,
  340. acpi_ut_get_type_name(target_type)));
  341. /* Only limited target types possible for everything except copy_object */
  342. if (walk_state->opcode != AML_COPY_OP) {
  343. /*
  344. * Only copy_object allows all object types to be overwritten. For
  345. * target_ref(s), there are restrictions on the object types that
  346. * are allowed.
  347. *
  348. * Allowable operations/typing for Store:
  349. *
  350. * 1) Simple Store
  351. * Integer --> Integer (Named/Local/Arg)
  352. * String --> String (Named/Local/Arg)
  353. * Buffer --> Buffer (Named/Local/Arg)
  354. * Package --> Package (Named/Local/Arg)
  355. *
  356. * 2) Store with implicit conversion
  357. * Integer --> String or Buffer (Named)
  358. * String --> Integer or Buffer (Named)
  359. * Buffer --> Integer or String (Named)
  360. */
  361. switch (target_type) {
  362. case ACPI_TYPE_PACKAGE:
  363. /*
  364. * Here, can only store a package to an existing package.
  365. * Storing a package to a Local/Arg is OK, and handled
  366. * elsewhere.
  367. */
  368. if (walk_state->opcode == AML_STORE_OP) {
  369. if (source_desc->common.type !=
  370. ACPI_TYPE_PACKAGE) {
  371. ACPI_ERROR((AE_INFO,
  372. "Cannot assign type [%s] to [Package] "
  373. "(source must be type Pkg)",
  374. acpi_ut_get_object_type_name
  375. (source_desc)));
  376. return_ACPI_STATUS(AE_AML_TARGET_TYPE);
  377. }
  378. break;
  379. }
  380. /* Fallthrough */
  381. case ACPI_TYPE_DEVICE:
  382. case ACPI_TYPE_EVENT:
  383. case ACPI_TYPE_MUTEX:
  384. case ACPI_TYPE_REGION:
  385. case ACPI_TYPE_POWER:
  386. case ACPI_TYPE_PROCESSOR:
  387. case ACPI_TYPE_THERMAL:
  388. ACPI_ERROR((AE_INFO,
  389. "Target must be [Buffer/Integer/String/Reference], found [%s] (%4.4s)",
  390. acpi_ut_get_type_name(node->type),
  391. node->name.ascii));
  392. return_ACPI_STATUS(AE_AML_TARGET_TYPE);
  393. default:
  394. break;
  395. }
  396. }
  397. /*
  398. * Resolve the source object to an actual value
  399. * (If it is a reference object)
  400. */
  401. status = acpi_ex_resolve_object(&source_desc, target_type, walk_state);
  402. if (ACPI_FAILURE(status)) {
  403. return_ACPI_STATUS(status);
  404. }
  405. /* Do the actual store operation */
  406. switch (target_type) {
  407. /*
  408. * The simple data types all support implicit source operand
  409. * conversion before the store.
  410. */
  411. case ACPI_TYPE_INTEGER:
  412. case ACPI_TYPE_STRING:
  413. case ACPI_TYPE_BUFFER:
  414. if ((walk_state->opcode == AML_COPY_OP) || !implicit_conversion) {
  415. /*
  416. * However, copy_object and Stores to arg_x do not perform
  417. * an implicit conversion, as per the ACPI specification.
  418. * A direct store is performed instead.
  419. */
  420. status = acpi_ex_store_direct_to_node(source_desc, node,
  421. walk_state);
  422. break;
  423. }
  424. /* Store with implicit source operand conversion support */
  425. status =
  426. acpi_ex_store_object_to_object(source_desc, target_desc,
  427. &new_desc, walk_state);
  428. if (ACPI_FAILURE(status)) {
  429. return_ACPI_STATUS(status);
  430. }
  431. if (new_desc != target_desc) {
  432. /*
  433. * Store the new new_desc as the new value of the Name, and set
  434. * the Name's type to that of the value being stored in it.
  435. * source_desc reference count is incremented by attach_object.
  436. *
  437. * Note: This may change the type of the node if an explicit
  438. * store has been performed such that the node/object type
  439. * has been changed.
  440. */
  441. status = acpi_ns_attach_object(node, new_desc,
  442. new_desc->common.type);
  443. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  444. "Store type [%s] into [%s] via Convert/Attach\n",
  445. acpi_ut_get_object_type_name
  446. (source_desc),
  447. acpi_ut_get_object_type_name
  448. (new_desc)));
  449. }
  450. break;
  451. case ACPI_TYPE_BUFFER_FIELD:
  452. case ACPI_TYPE_LOCAL_REGION_FIELD:
  453. case ACPI_TYPE_LOCAL_BANK_FIELD:
  454. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  455. /*
  456. * For all fields, always write the source data to the target
  457. * field. Any required implicit source operand conversion is
  458. * performed in the function below as necessary. Note, field
  459. * objects must retain their original type permanently.
  460. */
  461. status = acpi_ex_write_data_to_field(source_desc, target_desc,
  462. &walk_state->result_obj);
  463. break;
  464. default:
  465. /*
  466. * copy_object operator: No conversions for all other types.
  467. * Instead, directly store a copy of the source object.
  468. *
  469. * This is the ACPI spec-defined behavior for the copy_object
  470. * operator. (Note, for this default case, all normal
  471. * Store/Target operations exited above with an error).
  472. */
  473. status = acpi_ex_store_direct_to_node(source_desc, node,
  474. walk_state);
  475. break;
  476. }
  477. return_ACPI_STATUS(status);
  478. }
  479. /*******************************************************************************
  480. *
  481. * FUNCTION: acpi_ex_store_direct_to_node
  482. *
  483. * PARAMETERS: source_desc - Value to be stored
  484. * node - Named object to receive the value
  485. * walk_state - Current walk state
  486. *
  487. * RETURN: Status
  488. *
  489. * DESCRIPTION: "Store" an object directly to a node. This involves a copy
  490. * and an attach.
  491. *
  492. ******************************************************************************/
  493. static acpi_status
  494. acpi_ex_store_direct_to_node(union acpi_operand_object *source_desc,
  495. struct acpi_namespace_node *node,
  496. struct acpi_walk_state *walk_state)
  497. {
  498. acpi_status status;
  499. union acpi_operand_object *new_desc;
  500. ACPI_FUNCTION_TRACE(ex_store_direct_to_node);
  501. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  502. "Storing [%s] (%p) directly into node [%s] (%p)"
  503. " with no implicit conversion\n",
  504. acpi_ut_get_object_type_name(source_desc),
  505. source_desc, acpi_ut_get_type_name(node->type),
  506. node));
  507. /* Copy the source object to a new object */
  508. status =
  509. acpi_ut_copy_iobject_to_iobject(source_desc, &new_desc, walk_state);
  510. if (ACPI_FAILURE(status)) {
  511. return_ACPI_STATUS(status);
  512. }
  513. /* Attach the new object to the node */
  514. status = acpi_ns_attach_object(node, new_desc, new_desc->common.type);
  515. acpi_ut_remove_reference(new_desc);
  516. return_ACPI_STATUS(status);
  517. }