exstore.c 17 KB

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